2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2011 Daniel Borkmann.
5 * Subject to the GPL, version 2.
21 #include "ct_servmgmt.h"
22 #include "crypto_box_curve25519xsalsa20poly1305.h"
23 #include "crypto_auth_hmacsha512256.h"
25 #define crypto_box_pub_key_size crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
27 /* Config line format: alias;serverip|servername;port;udp|tcp;pubkey\n */
33 char port
[6]; /* 5 + \0 */
34 unsigned char publickey
[crypto_box_pub_key_size
];
35 struct curve25519_proto proto_inf
;
36 unsigned char auth_token
[crypto_auth_hmacsha512256_KEYBYTES
];
37 struct server_store
*next
;
40 static struct server_store
*store
= NULL
;
41 static struct server_store
*selected
= NULL
;
42 static struct rwlock store_lock
;
44 static struct server_store
*server_store_alloc(void)
46 return xzmalloc(sizeof(struct server_store
));
49 static void server_store_free(struct server_store
*ss
)
53 memset(ss
, 0, sizeof(struct server_store
));
66 static int parse_line(char *line
, char *homedir
)
70 enum parse_states s
= PARSE_ALIAS
;
71 struct server_store
*elem
;
72 unsigned char pkey
[crypto_box_pub_key_size
];
74 elem
= server_store_alloc();
77 str
= strtok(line
, ";");
78 for (; str
!= NULL
;) {
81 strlcpy(elem
->alias
, str
, sizeof(elem
->alias
));
85 strlcpy(elem
->host
, str
, sizeof(elem
->host
));
89 strlcpy(elem
->port
, str
, sizeof(elem
->port
));
93 if (!strncmp("udp", str
, strlen("udp")))
100 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
103 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
104 memcpy(elem
->auth_token
, pkey
, sizeof(elem
->auth_token
));
105 ret
= curve25519_proto_init(&elem
->proto_inf
,
107 sizeof(elem
->publickey
),
119 str
= strtok(NULL
, ";");
123 return s
== PARSE_DONE
? 0 : -EIO
;
126 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
129 char path
[PATH_MAX
], buff
[1024];
132 memset(path
, 0, sizeof(path
));
133 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
135 rwlock_init(&store_lock
);
136 rwlock_wr_lock(&store_lock
);
138 fp
= fopen(path
, "r");
140 panic("Cannot open server file!\n");
142 memset(buff
, 0, sizeof(buff
));
143 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
144 buff
[sizeof(buff
) - 1] = 0;
145 /* A comment. Skip this line */
146 if (buff
[0] == '#' || buff
[0] == '\n') {
147 memset(buff
, 0, sizeof(buff
));
152 ret
= parse_line(buff
, homedir
);
154 panic("Cannot parse line %d from clients!\n", line
);
156 memset(buff
, 0, sizeof(buff
));
162 panic("No registered servers found!\n");
164 rwlock_unlock(&store_lock
);
167 void dump_serv_store(void)
170 struct server_store
*elem
;
172 rwlock_rd_lock(&store_lock
);
175 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
176 elem
->host
, elem
->port
,
177 elem
->udp
? "udp" : "tcp");
178 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
179 if (i
== (sizeof(elem
->publickey
) - 1))
180 printf("%02x\n", (unsigned char)
183 printf("%02x:", (unsigned char)
187 rwlock_unlock(&store_lock
);
190 void destroy_serv_store(void)
192 struct server_store
*elem
, *nelem
= NULL
;
194 rwlock_wr_lock(&store_lock
);
200 server_store_free(elem
);
203 rwlock_unlock(&store_lock
);
204 rwlock_destroy(&store_lock
);
207 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
208 char **host
, char **port
, int *udp
)
210 struct server_store
*elem
;
212 rwlock_rd_lock(&store_lock
);
215 while (elem
&& elem
->next
)
218 (*host
) = elem
->host
;
219 (*port
) = elem
->port
;
223 rwlock_unlock(&store_lock
);
228 if (!strncmp(elem
->alias
, alias
,
229 min(len
, strlen(elem
->alias
) + 1)))
234 (*host
) = elem
->host
;
235 (*port
) = elem
->port
;
239 rwlock_unlock(&store_lock
);
243 rwlock_unlock(&store_lock
);
252 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
254 struct curve25519_proto
*ret
= NULL
;
256 rwlock_rd_lock(&store_lock
);
258 ret
= &selected
->proto_inf
;
259 rwlock_unlock(&store_lock
);
264 unsigned char *get_serv_store_entry_auth_token(void)
266 unsigned char *ret
= NULL
;
268 rwlock_rd_lock(&store_lock
);
270 ret
= selected
->auth_token
;
271 rwlock_unlock(&store_lock
);