2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2011 Daniel Borkmann.
4 * Subject to the GPL, version 2.
22 #include "ct_servmgmt.h"
28 char port
[6]; /* 5 + \0 */
29 unsigned char publickey
[crypto_box_pub_key_size
];
30 struct curve25519_proto proto_inf
;
31 unsigned char auth_token
[crypto_auth_hmacsha512256_KEYBYTES
];
32 struct server_store
*next
;
35 static struct server_store
*store
= NULL
;
36 static struct server_store
*selected
= NULL
;
37 static struct rwlock store_lock
;
39 static struct server_store
*server_store_alloc(void)
41 return xzmalloc(sizeof(struct server_store
));
44 static void server_store_free(struct server_store
*ss
)
48 memset(ss
, 0, sizeof(struct server_store
));
61 static int parse_line(char *line
, char *homedir
)
65 enum parse_states s
= PARSE_ALIAS
;
66 struct server_store
*elem
;
67 unsigned char pkey
[crypto_box_pub_key_size
];
69 elem
= server_store_alloc();
72 str
= strtok(line
, ";");
73 for (; str
!= NULL
;) {
76 strlcpy(elem
->alias
, str
, sizeof(elem
->alias
));
80 strlcpy(elem
->host
, str
, sizeof(elem
->host
));
84 strlcpy(elem
->port
, str
, sizeof(elem
->port
));
88 if (!strncmp("udp", str
, strlen("udp")))
90 else if (!strncmp("tcp", str
, strlen("tcp")))
93 syslog(LOG_ERR
, "Incorrect carrier type !(udp|tcp) in server spec.\n");
99 if (!curve25519_pubkey_hexparse_32(pkey
, sizeof(pkey
),
102 memcpy(elem
->publickey
, pkey
, sizeof(elem
->publickey
));
103 memcpy(elem
->auth_token
, pkey
, sizeof(elem
->auth_token
));
104 ret
= curve25519_proto_init(&elem
->proto_inf
,
106 sizeof(elem
->publickey
),
118 str
= strtok(NULL
, ";");
122 return s
== PARSE_DONE
? 0 : -EIO
;
125 void parse_userfile_and_generate_serv_store_or_die(char *homedir
)
128 char path
[PATH_MAX
], buff
[1024];
131 memset(path
, 0, sizeof(path
));
132 slprintf(path
, sizeof(path
), "%s/%s", homedir
, FILE_SERVERS
);
134 rwlock_init(&store_lock
);
135 rwlock_wr_lock(&store_lock
);
137 fp
= fopen(path
, "r");
139 panic("Cannot open server file!\n");
141 memset(buff
, 0, sizeof(buff
));
142 while (fgets(buff
, sizeof(buff
), fp
) != NULL
) {
143 buff
[sizeof(buff
) - 1] = 0;
144 /* A comment. Skip this line */
145 if (buff
[0] == '#' || buff
[0] == '\n') {
146 memset(buff
, 0, sizeof(buff
));
151 ret
= parse_line(buff
, homedir
);
153 panic("Cannot parse line %d from clients!\n", line
);
155 memset(buff
, 0, sizeof(buff
));
161 panic("No registered servers found!\n");
163 rwlock_unlock(&store_lock
);
166 void dump_serv_store(void)
169 struct server_store
*elem
;
171 rwlock_rd_lock(&store_lock
);
174 printf("[%s] -> %s:%s via %s -> ", elem
->alias
,
175 elem
->host
, elem
->port
,
176 elem
->udp
? "udp" : "tcp");
177 for (i
= 0; i
< sizeof(elem
->publickey
); ++i
)
178 if (i
== (sizeof(elem
->publickey
) - 1))
179 printf("%02x\n", (unsigned char)
182 printf("%02x:", (unsigned char)
186 rwlock_unlock(&store_lock
);
189 void destroy_serv_store(void)
191 struct server_store
*elem
, *nelem
= NULL
;
193 rwlock_wr_lock(&store_lock
);
199 server_store_free(elem
);
202 rwlock_unlock(&store_lock
);
203 rwlock_destroy(&store_lock
);
206 void get_serv_store_entry_by_alias(char *alias
, size_t len
,
207 char **host
, char **port
, int *udp
)
209 struct server_store
*elem
;
211 rwlock_rd_lock(&store_lock
);
214 while (elem
&& elem
->next
)
217 (*host
) = elem
->host
;
218 (*port
) = elem
->port
;
222 rwlock_unlock(&store_lock
);
227 if (!strncmp(elem
->alias
, alias
,
228 min(len
, strlen(elem
->alias
) + 1)))
233 (*host
) = elem
->host
;
234 (*port
) = elem
->port
;
238 rwlock_unlock(&store_lock
);
242 rwlock_unlock(&store_lock
);
251 struct curve25519_proto
*get_serv_store_entry_proto_inf(void)
253 struct curve25519_proto
*ret
= NULL
;
255 rwlock_rd_lock(&store_lock
);
257 ret
= &selected
->proto_inf
;
258 rwlock_unlock(&store_lock
);
263 unsigned char *get_serv_store_entry_auth_token(void)
265 unsigned char *ret
= NULL
;
267 rwlock_rd_lock(&store_lock
);
269 ret
= selected
->auth_token
;
270 rwlock_unlock(&store_lock
);