2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
22 #include "ct_servmgmt.h"
23 #include "crypto_box_curve25519xsalsa20poly1305.h"
24 #include "crypto_auth_hmacsha512256.h"
26 #define crypto_box_pub_key_size crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
28 /* Config line format: alias;serverip|servername;port;udp|tcp;pubkey\n */
34 char port
[6]; /* 5 + \0 */
35 unsigned char publickey
[crypto_box_pub_key_size
];
36 struct curve25519_proto proto_inf
;
37 unsigned char auth_token
[crypto_auth_hmacsha512256_KEYBYTES
];
38 struct server_store
*next
;
41 static struct server_store
*store
= NULL
;
42 static struct server_store
*selected
= NULL
;
43 static struct rwlock store_lock
;
45 static struct server_store
*server_store_alloc(void)
47 return xzmalloc(sizeof(struct server_store
));
50 static void server_store_free(struct server_store
*ss
)
54 memset(ss
, 0, sizeof(struct server_store
));
67 static int parse_line(char *line
, char *homedir
)
71 enum parse_states s
= PARSE_ALIAS
;
72 struct server_store
*elem
;
73 unsigned char pkey
[crypto_box_pub_key_size
];
75 elem
= server_store_alloc();
78 str
= strtok(line
, ";");
79 for (; str
!= NULL
;) {
82 strlcpy(elem
->alias
, str
, sizeof(elem
->alias
));
86 strlcpy(elem
->host
, str
, sizeof(elem
->host
));
90 strlcpy(elem
->port
, str
, sizeof(elem
->port
));
94 if (!strncmp("udp", str
, strlen("udp")))
96 else if (!strncmp("tcp", str
, strlen("tcp")))
99 syslog(LOG_ERR
, "Incorrect carrier type !(udp|tcp) in server spec.\n");
105 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
108 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
109 memcpy(elem
->auth_token
, pkey
, sizeof(elem
->auth_token
));
110 ret
= curve25519_proto_init(&elem
->proto_inf
,
112 sizeof(elem
->publickey
),
124 str
= strtok(NULL
, ";");
128 return s
== PARSE_DONE
? 0 : -EIO
;
131 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
134 char path
[PATH_MAX
], buff
[1024];
137 memset(path
, 0, sizeof(path
));
138 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
140 rwlock_init(&store_lock
);
141 rwlock_wr_lock(&store_lock
);
143 fp
= fopen(path
, "r");
145 panic("Cannot open server file!\n");
147 memset(buff
, 0, sizeof(buff
));
148 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
149 buff
[sizeof(buff
) - 1] = 0;
150 /* A comment. Skip this line */
151 if (buff
[0] == '#' || buff
[0] == '\n') {
152 memset(buff
, 0, sizeof(buff
));
157 ret
= parse_line(buff
, homedir
);
159 panic("Cannot parse line %d from clients!\n", line
);
161 memset(buff
, 0, sizeof(buff
));
167 panic("No registered servers found!\n");
169 rwlock_unlock(&store_lock
);
172 void dump_serv_store(void)
175 struct server_store
*elem
;
177 rwlock_rd_lock(&store_lock
);
180 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
181 elem
->host
, elem
->port
,
182 elem
->udp
? "udp" : "tcp");
183 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
184 if (i
== (sizeof(elem
->publickey
) - 1))
185 printf("%02x\n", (unsigned char)
188 printf("%02x:", (unsigned char)
192 rwlock_unlock(&store_lock
);
195 void destroy_serv_store(void)
197 struct server_store
*elem
, *nelem
= NULL
;
199 rwlock_wr_lock(&store_lock
);
205 server_store_free(elem
);
208 rwlock_unlock(&store_lock
);
209 rwlock_destroy(&store_lock
);
212 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
213 char **host
, char **port
, int *udp
)
215 struct server_store
*elem
;
217 rwlock_rd_lock(&store_lock
);
220 while (elem
&& elem
->next
)
223 (*host
) = elem
->host
;
224 (*port
) = elem
->port
;
228 rwlock_unlock(&store_lock
);
233 if (!strncmp(elem
->alias
, alias
,
234 min(len
, strlen(elem
->alias
) + 1)))
239 (*host
) = elem
->host
;
240 (*port
) = elem
->port
;
244 rwlock_unlock(&store_lock
);
248 rwlock_unlock(&store_lock
);
257 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
259 struct curve25519_proto
*ret
= NULL
;
261 rwlock_rd_lock(&store_lock
);
263 ret
= &selected
->proto_inf
;
264 rwlock_unlock(&store_lock
);
269 unsigned char *get_serv_store_entry_auth_token(void)
271 unsigned char *ret
= NULL
;
273 rwlock_rd_lock(&store_lock
);
275 ret
= selected
->auth_token
;
276 rwlock_unlock(&store_lock
);