1 /*
2   Copyright 2021 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 
26 #ifndef CFENGINE_CFNET_H
27 #define CFENGINE_CFNET_H
28 
29 
30 #include <platform.h>
31 #include <definitions.h>                        /* CF_BUFSIZE, CF_SMALLBUF */
32 #include <protocol_version.h> // ProtocolVersion
33 
34 /* Only set with DetermineCfenginePort() and from cf-serverd */
35 extern char CFENGINE_PORT_STR[16];                     /* GLOBAL_P GLOBAL_E */
36 extern int CFENGINE_PORT;                              /* GLOBAL_P GLOBAL_E */
37 
38 
39 #define CF_MAX_IP_LEN 64                    /* max IPv4/IPv6 address length */
40 #define CF_MAX_PORT_LEN 6
41 #define CF_MAX_HOST_LEN 256
42 // Since both have 1 extra for null byte, we don't need to add 1 for ':'
43 #define CF_MAX_SERVER_LEN (CF_MAX_HOST_LEN + CF_MAX_PORT_LEN)
44 
45 #define CF_DONE 't'
46 #define CF_MORE 'm'
47 #define SOCKET_INVALID -1
48 #define MAXIP4CHARLEN 16
49 #define CF_RSA_PROTO_OFFSET 24
50 #define CF_PROTO_OFFSET 16
51 #define CF_INBAND_OFFSET 8
52 #define CF_MSGSIZE (CF_BUFSIZE - CF_INBAND_OFFSET)
53 
54 typedef struct
55 {
56     ProtocolVersion protocol_version : 3;
57     bool            cache_connection : 1;
58     bool            force_ipv4       : 1;
59     bool            trust_server     : 1;
60     bool            off_the_record   : 1;
61 } ConnectionFlags;
62 
ConnectionFlagsEqual(const ConnectionFlags * f1,const ConnectionFlags * f2)63 static inline bool ConnectionFlagsEqual(const ConnectionFlags *f1,
64                                         const ConnectionFlags *f2)
65 {
66     if (f1->protocol_version == f2->protocol_version &&
67         f1->cache_connection == f2->cache_connection &&
68         f1->force_ipv4 == f2->force_ipv4 &&
69         f1->trust_server == f2->trust_server &&
70         f1->off_the_record == f2->off_the_record)
71     {
72         return true;
73     }
74     else
75     {
76         return false;
77     }
78 }
79 
80 
81 #include "connection_info.h"                       /* needs ProtocolVersion */
82 
83 
84 /*
85  * TLS support
86  */
87 #define DEFAULT_TLS_TIMEOUT_SECONDS     5
88 #define DEFAULT_TLS_TIMEOUT_USECONDS    0
89 #define SET_DEFAULT_TLS_TIMEOUT(x) \
90     x.tv_sec = DEFAULT_TLS_TIMEOUT_SECONDS; \
91     x.tv_usec = DEFAULT_TLS_TIMEOUT_USECONDS
92 #define DEFAULT_TLS_TRIES 5
93 
94 struct Stat_;              /* defined in stat_cache.h, typedef'ed to "Stat" */
95 
96 typedef struct
97 {
98     ConnectionInfo *conn_info;
99     int authenticated;
100     char username[CF_SMALLBUF];
101     /* Unused for now... */
102     /* char localip[CF_MAX_IP_LEN]; */
103     char remoteip[CF_MAX_IP_LEN];
104     unsigned char *session_key;
105     char encryption_type;
106     short error;
107     struct Stat_ *cache;                          /* cache for remote STATs */
108 
109     /* The following consistutes the ID of a server host, mostly taken from
110      * the copy_from connection attributes. */
111     ConnectionFlags flags;
112     char *this_server;
113     char *this_port;
114 } AgentConnection;
115 
116 
117 
118 /* misc.c */
119 
120 void EnforceBwLimit(int tosend);
121 int cf_closesocket(int sd);
122 
123 
124 /* client_protocol.c */
125 void SetSkipIdentify(bool enabled);
126 
127 /* net.c */
128 void SetBindInterface(const char *ip);
129 
130 #endif
131