2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * 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")))
95 else if (!strncmp("tcp", str
, strlen("tcp")))
98 syslog(LOG_ERR
, "Incorrect carrier type !(udp|tcp) in server spec.\n");
104 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
107 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
108 memcpy(elem
->auth_token
, pkey
, sizeof(elem
->auth_token
));
109 ret
= curve25519_proto_init(&elem
->proto_inf
,
111 sizeof(elem
->publickey
),
123 str
= strtok(NULL
, ";");
127 return s
== PARSE_DONE
? 0 : -EIO
;
130 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
133 char path
[PATH_MAX
], buff
[1024];
136 memset(path
, 0, sizeof(path
));
137 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
139 rwlock_init(&store_lock
);
140 rwlock_wr_lock(&store_lock
);
142 fp
= fopen(path
, "r");
144 panic("Cannot open server file!\n");
146 memset(buff
, 0, sizeof(buff
));
147 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
148 buff
[sizeof(buff
) - 1] = 0;
149 /* A comment. Skip this line */
150 if (buff
[0] == '#' || buff
[0] == '\n') {
151 memset(buff
, 0, sizeof(buff
));
156 ret
= parse_line(buff
, homedir
);
158 panic("Cannot parse line %d from clients!\n", line
);
160 memset(buff
, 0, sizeof(buff
));
166 panic("No registered servers found!\n");
168 rwlock_unlock(&store_lock
);
171 void dump_serv_store(void)
174 struct server_store
*elem
;
176 rwlock_rd_lock(&store_lock
);
179 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
180 elem
->host
, elem
->port
,
181 elem
->udp
? "udp" : "tcp");
182 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
183 if (i
== (sizeof(elem
->publickey
) - 1))
184 printf("%02x\n", (unsigned char)
187 printf("%02x:", (unsigned char)
191 rwlock_unlock(&store_lock
);
194 void destroy_serv_store(void)
196 struct server_store
*elem
, *nelem
= NULL
;
198 rwlock_wr_lock(&store_lock
);
204 server_store_free(elem
);
207 rwlock_unlock(&store_lock
);
208 rwlock_destroy(&store_lock
);
211 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
212 char **host
, char **port
, int *udp
)
214 struct server_store
*elem
;
216 rwlock_rd_lock(&store_lock
);
219 while (elem
&& elem
->next
)
222 (*host
) = elem
->host
;
223 (*port
) = elem
->port
;
227 rwlock_unlock(&store_lock
);
232 if (!strncmp(elem
->alias
, alias
,
233 min(len
, strlen(elem
->alias
) + 1)))
238 (*host
) = elem
->host
;
239 (*port
) = elem
->port
;
243 rwlock_unlock(&store_lock
);
247 rwlock_unlock(&store_lock
);
256 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
258 struct curve25519_proto
*ret
= NULL
;
260 rwlock_rd_lock(&store_lock
);
262 ret
= &selected
->proto_inf
;
263 rwlock_unlock(&store_lock
);
268 unsigned char *get_serv_store_entry_auth_token(void)
270 unsigned char *ret
= NULL
;
272 rwlock_rd_lock(&store_lock
);
274 ret
= selected
->auth_token
;
275 rwlock_unlock(&store_lock
);