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.
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 */
33 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
;
43 static struct server_store
*selected
= NULL
;
45 static struct rwlock store_lock
;
47 static struct server_store
*server_store_alloc(void)
49 return xzmalloc(sizeof(struct server_store
));
52 static void server_store_free(struct server_store
*ss
)
56 memset(ss
, 0, sizeof(struct server_store
));
60 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
63 char path
[PATH_MAX
], buff
[1024], *alias
, *host
, *port
, *udp
, *key
;
64 unsigned char pkey
[crypto_box_pub_key_size
];
65 int line
= 1, __udp
= 0, ret
;
66 struct server_store
*elem
;
68 memset(path
, 0, sizeof(path
));
69 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
71 rwlock_init(&store_lock
);
72 rwlock_wr_lock(&store_lock
);
74 fp
= fopen(path
, "r");
76 panic("Cannot open server file!\n");
77 memset(buff
, 0, sizeof(buff
));
79 /* TODO: this is huge crap. needs to be rewritten! */
80 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
81 buff
[sizeof(buff
) - 1] = 0;
82 /* A comment. Skip this line */
83 if (buff
[0] == '#' || buff
[0] == '\n') {
84 memset(buff
, 0, sizeof(buff
));
90 while (*host
!= ';' &&
96 panic("Parse error! No alias found in l.%d!\n", line
);
100 panic("Parse error! No host found in l.%d!\n", line
);
102 while (*port
!= ';' &&
108 panic("Parse error! No host found in l.%d!\n", line
);
112 panic("Parse error! No port found in l.%d!\n", line
);
114 while (*udp
!= ';' &&
120 panic("Parse error! No port found in l.%d!\n", line
);
124 panic("Parse error! No udp|tcp found in l.%d!\n", line
);
125 if (udp
[0] == 'u' && udp
[1] == 'd' && udp
[2] == 'p')
127 else if (udp
[0] == 't' && udp
[1] == 'c' && udp
[2] == 'p')
130 panic("Parse error! No udp|tcp found in l.%d!\n", line
);
133 panic("Parse error! No key found in l.%d!\n", line
);
137 panic("Parse error! No key found in l.%d!\n", line
);
139 key
[strlen(key
) - 1] = 0;
140 memset(pkey
, 0, sizeof(pkey
));
141 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
143 panic("Parse error! No key found in l.%d!\n", line
);
145 if (strlen(alias
) + 1 > sizeof(elem
->alias
))
146 panic("Alias too long in l.%d!\n", line
);
147 if (strlen(host
) + 1 > sizeof(elem
->host
))
148 panic("Host too long in l.%d!\n", line
);
149 if (strlen(port
) + 1 > sizeof(elem
->port
))
150 panic("Port too long in l.%d!\n", line
);
151 if (strstr(alias
, " ") || strstr(alias
, "\t"))
152 panic("Alias consists of whitespace in l.%d!\n", line
);
153 if (strstr(host
, " ") || strstr(host
, "\t"))
154 panic("Host consists of whitespace in l.%d!\n", line
);
155 if (strstr(port
, " ") || strstr(port
, "\t"))
156 panic("Port consists of whitespace in l.%d!\n", line
);
158 elem
= server_store_alloc();
161 strlcpy(elem
->alias
, alias
, sizeof(elem
->alias
));
162 strlcpy(elem
->host
, host
, sizeof(elem
->host
));
163 strlcpy(elem
->port
, port
, sizeof(elem
->port
));
164 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
165 memcpy(elem
->auth_token
, elem
->publickey
, sizeof(elem
->auth_token
));
166 ret
= curve25519_proto_init(&elem
->proto_inf
,
168 sizeof(elem
->publickey
),
171 panic("Cannot init curve25519 proto on server!\n");
174 memset(buff
, 0, sizeof(buff
));
180 panic("No registered servers found!\n");
181 rwlock_unlock(&store_lock
);
184 void dump_serv_store(void)
187 struct server_store
*elem
;
189 rwlock_rd_lock(&store_lock
);
192 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
193 elem
->host
, elem
->port
,
194 elem
->udp
? "udp" : "tcp");
195 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
196 if (i
== (sizeof(elem
->publickey
) - 1))
197 printf("%02x\n", (unsigned char)
200 printf("%02x:", (unsigned char)
204 rwlock_unlock(&store_lock
);
207 void destroy_serv_store(void)
209 struct server_store
*elem
, *nelem
= NULL
;
211 rwlock_wr_lock(&store_lock
);
217 server_store_free(elem
);
220 rwlock_unlock(&store_lock
);
221 rwlock_destroy(&store_lock
);
224 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
225 char **host
, char **port
, int *udp
)
227 struct server_store
*elem
;
229 rwlock_rd_lock(&store_lock
);
232 while (elem
&& elem
->next
)
235 (*host
) = elem
->host
;
236 (*port
) = elem
->port
;
240 rwlock_unlock(&store_lock
);
245 if (!strncmp(elem
->alias
, alias
,
246 min(len
, strlen(elem
->alias
) + 1)))
251 (*host
) = elem
->host
;
252 (*port
) = elem
->port
;
256 rwlock_unlock(&store_lock
);
260 rwlock_unlock(&store_lock
);
268 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
270 struct curve25519_proto
*ret
= NULL
;
271 rwlock_rd_lock(&store_lock
);
273 ret
= &selected
->proto_inf
;
274 rwlock_unlock(&store_lock
);
278 unsigned char *get_serv_store_entry_auth_token(void)
280 unsigned char *ret
= NULL
;
281 rwlock_rd_lock(&store_lock
);
283 ret
= selected
->auth_token
;
284 rwlock_unlock(&store_lock
);