Commit | Line | Data |
---|---|---|
d3372da9 | 1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client | |
302a2600 | 3 | * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com> |
d3372da9 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | */ | |
19 | #define _GNU_SOURCE | |
20 | #include <unistd.h> | |
21 | #include <stdlib.h> | |
d3372da9 | 22 | #include <sys/socket.h> |
23 | #include <netinet/in.h> | |
24 | #include <netinet/ip6.h> | |
504ba854 | 25 | #include <arpa/inet.h> |
ddb933f1 | 26 | #include <sys/un.h> |
d3372da9 | 27 | #include <errno.h> |
28 | #include <string.h> | |
29 | #include <stdarg.h> | |
30 | #include <wchar.h> | |
31 | #include <wctype.h> | |
32 | #include <iconv.h> | |
33 | #include <pwd.h> | |
34 | #include <time.h> | |
35 | #include <fcntl.h> | |
36 | #include <signal.h> | |
f9455250 | 37 | #include <stdint.h> |
d3372da9 | 38 | |
39 | #ifdef HAVE_CONFIG_H | |
40 | #include <config.h> | |
41 | #endif | |
42 | #include "conf.h" | |
43 | #include "auth.h" | |
44 | #include "utils.h" | |
45 | #include "net.h" | |
46 | #include "module.h" | |
47 | #include "sysevents.h" | |
48 | #include "filenet.h" | |
49 | #include "transfer.h" | |
50 | #include "search.h" | |
51 | #include "client.h" | |
52 | ||
53 | #define PERM_DISALLOW 1 | |
54 | #define PERM_ADMIN 2 | |
55 | #define PERM_FNETCTL 4 | |
56 | #define PERM_TRANS 8 | |
57 | #define PERM_TRANSCU 16 | |
58 | #define PERM_CHAT 32 | |
59 | #define PERM_SRCH 64 | |
60 | ||
61 | #define NOTIF_END 0 | |
62 | #define NOTIF_INT 1 | |
63 | #define NOTIF_STR 2 | |
64 | #define NOTIF_FLOAT 3 | |
65 | #define NOTIF_ID 4 | |
dcf7a1a2 | 66 | #define NOTIF_OFF 5 |
d3372da9 | 67 | #define NOTIF_PEND 0 |
68 | #define NOTIF_WAIT 1 | |
69 | ||
70 | struct uidata; | |
71 | ||
72 | struct command | |
73 | { | |
74 | wchar_t *name; | |
75 | void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv); | |
76 | }; | |
77 | ||
78 | struct qcommand | |
79 | { | |
80 | struct qcommand *next; | |
81 | struct command *cmd; | |
82 | int argc; | |
83 | wchar_t **argv; | |
84 | }; | |
85 | ||
86 | struct uiuser | |
87 | { | |
88 | struct uiuser *next, *prev; | |
89 | int used; | |
90 | wchar_t *name; | |
91 | unsigned long perms; | |
92 | int delete; | |
93 | }; | |
94 | ||
95 | struct notif | |
96 | { | |
97 | struct notif *next, *prev; | |
98 | struct uidata *ui; | |
99 | int state; | |
100 | int code; | |
101 | double rlimit; | |
102 | size_t argc; | |
103 | struct timer *exptimer; | |
104 | struct notifarg | |
105 | { | |
106 | int dt; | |
107 | union | |
108 | { | |
109 | int n; | |
dcf7a1a2 | 110 | off_t o; |
d3372da9 | 111 | wchar_t *s; |
112 | double d; | |
113 | } d; | |
114 | } *argv; | |
115 | }; | |
116 | ||
117 | struct uidata | |
118 | { | |
119 | struct uidata *next, *prev; | |
120 | struct socket *sk; | |
121 | struct qcommand *queue, *queuelast; | |
821e3843 | 122 | size_t queuesize; |
d3372da9 | 123 | struct authhandle *auth; |
124 | int close; | |
125 | union | |
126 | { | |
127 | struct | |
128 | { | |
129 | int fnact:1; | |
130 | int fnchat:1; | |
ba86d49a | 131 | int fnpeer:1; |
d3372da9 | 132 | int tract:1; |
133 | int trprog:1; | |
134 | int srch:1; | |
5f151349 | 135 | int msg:1; |
d3372da9 | 136 | } b; |
137 | int w; | |
138 | } notify; | |
139 | wchar_t *username; | |
140 | struct uiuser *userinfo; | |
5f151349 | 141 | int id; |
142 | wchar_t *regname; | |
d3372da9 | 143 | uid_t uid; |
144 | struct notif *fnotif, *lnotif; | |
145 | char *fcmdbuf; | |
146 | size_t fcmdbufdata, fcmdbufsize; | |
147 | pid_t fcmdpid; | |
148 | struct socket *fcmdsk; | |
149 | /* Read buffer */ | |
150 | char *inbuf; | |
151 | size_t inbufsize, indata; | |
152 | /* Wordset storage */ | |
153 | wchar_t **argv; | |
76236762 | 154 | size_t argc, args; |
d3372da9 | 155 | /* WCS conversation stuff */ |
156 | wchar_t *cb; /* Conversation buffer */ | |
157 | size_t cbsize, cbdata; | |
158 | iconv_t ichandle; | |
159 | /* Parser data */ | |
160 | int ps; /* Parser state */ | |
161 | wchar_t *pp; /* Current parse pointer */ | |
162 | wchar_t *cw; /* Current word (building storage) */ | |
163 | size_t cwsize, cwdata; | |
164 | }; | |
165 | ||
166 | static int srcheta(struct search *srch, void *uudata); | |
167 | static int srchcommit(struct search *srch, void *uudata); | |
168 | static int srchres(struct search *srch, struct srchres *sr, void *uudata); | |
5f151349 | 169 | static struct notif *newnotif(struct uidata *data, int code, ...); |
170 | static void notifappend(struct notif *notif, ...); | |
d3372da9 | 171 | |
172 | struct uiuser *users = NULL; | |
173 | struct uidata *actives = NULL; | |
d1ef0200 FT |
174 | struct lport *tcpsocket = NULL; |
175 | struct lport *unixsocket = NULL; | |
9a64a4f7 | 176 | static time_t starttime; |
d3372da9 | 177 | |
178 | static wchar_t *quoteword(wchar_t *word) | |
179 | { | |
180 | wchar_t *wp, *buf, *bp; | |
181 | int dq, numbs, numc; | |
182 | ||
183 | dq = 0; | |
184 | numbs = 0; | |
185 | numc = 0; | |
186 | if(*word == L'\0') | |
187 | { | |
188 | dq = 1; | |
189 | } else { | |
190 | for(wp = word; *wp != L'\0'; wp++) | |
191 | { | |
192 | if(!dq && iswspace(*wp)) | |
193 | dq = 1; | |
194 | if((*wp == L'\\') || (*wp == L'\"')) | |
195 | numbs++; | |
196 | numc++; | |
197 | } | |
198 | } | |
199 | if(!dq && !numbs) | |
200 | return(NULL); | |
201 | bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1)); | |
202 | if(dq) | |
203 | *(bp++) = L'\"'; | |
204 | for(wp = word; *wp != L'\0'; wp++) | |
205 | { | |
206 | if((*wp == L'\\') || (*wp == L'\"')) | |
207 | *(bp++) = L'\\'; | |
208 | *(bp++) = *wp; | |
209 | } | |
210 | if(dq) | |
211 | *(bp++) = L'\"'; | |
212 | *(bp++) = L'\0'; | |
213 | return(buf); | |
214 | } | |
215 | ||
216 | static void sq(struct socket *sk, int cont, ...) | |
217 | { | |
218 | int num, freepart; | |
219 | va_list al; | |
f7292a8b | 220 | char *final, *sarg; |
d3372da9 | 221 | wchar_t *buf; |
222 | wchar_t *part, *tpart; | |
223 | size_t bufsize, bufdata; | |
224 | ||
225 | buf = NULL; | |
226 | bufsize = bufdata = 0; | |
227 | num = 0; | |
228 | va_start(al, cont); | |
229 | while((part = va_arg(al, wchar_t *)) != NULL) | |
230 | { | |
f7292a8b | 231 | freepart = 0; |
d3372da9 | 232 | if(*part == L'%') |
233 | { | |
f7292a8b | 234 | tpart = part + 1; |
235 | if(!wcscmp(tpart, L"i")) | |
d3372da9 | 236 | { |
f7292a8b | 237 | freepart = 1; |
238 | part = swprintf2(L"%i", va_arg(al, int)); | |
dcf7a1a2 FT |
239 | } else if(!wcscmp(tpart, L"zi")) { |
240 | freepart = 1; | |
241 | part = swprintf2(L"%zi", va_arg(al, size_t)); | |
242 | } else if(!wcscmp(tpart, L"oi")) { | |
243 | freepart = 1; | |
244 | part = swprintf2(L"%ji", (intmax_t)va_arg(al, off_t)); | |
f7292a8b | 245 | } else if(!wcscmp(tpart, L"s")) { |
246 | freepart = 1; | |
247 | part = icmbstowcs(sarg = va_arg(al, char *), NULL); | |
248 | if(part == NULL) | |
d3372da9 | 249 | { |
f7292a8b | 250 | freepart = 0; |
251 | part = L"ERROR"; | |
252 | flog(LOG_ERR, "could not convert local string to wcs: %s", sarg); | |
d3372da9 | 253 | } |
f7292a8b | 254 | } else if(!wcscmp(tpart, L"ls")) { |
255 | part = va_arg(al, wchar_t *); | |
256 | } else if(!wcscmp(tpart, L"ll")) { | |
257 | freepart = 1; | |
258 | part = swprintf2(L"%lli", va_arg(al, long long)); | |
259 | } else if(!wcscmp(tpart, L"f")) { | |
260 | freepart = 1; | |
261 | part = swprintf2(L"%f", va_arg(al, double)); | |
262 | } else if(!wcscmp(tpart, L"x")) { | |
263 | freepart = 1; | |
264 | part = swprintf2(L"%x", va_arg(al, int)); | |
265 | } else { | |
266 | flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart); | |
267 | abort(); | |
d3372da9 | 268 | } |
d3372da9 | 269 | } |
270 | if((tpart = quoteword(part)) != NULL) | |
271 | { | |
272 | if(freepart) | |
273 | free(part); | |
274 | part = tpart; | |
275 | freepart = 1; | |
276 | } | |
277 | if((num > 1) || ((num == 1) && !(cont & 1))) | |
278 | addtobuf(buf, L' '); | |
279 | bufcat(buf, part, wcslen(part)); | |
280 | if((num == 0) && (cont & 1)) | |
281 | addtobuf(buf, L'-'); | |
282 | num++; | |
283 | if(freepart) | |
284 | free(part); | |
285 | } | |
286 | if(cont & 2) | |
287 | bufcat(buf, L" \0", 2); | |
288 | else | |
289 | bufcat(buf, L"\r\n\0", 3); | |
290 | if((final = icwcstombs(buf, "utf-8")) == NULL) | |
291 | { | |
292 | flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno)); | |
293 | free(buf); | |
294 | return; | |
295 | } | |
296 | va_end(al); | |
297 | free(buf); | |
298 | sockqueue(sk, final, strlen(final)); | |
299 | free(final); | |
300 | } | |
301 | ||
302 | struct uiuser *finduser(wchar_t *name) | |
303 | { | |
304 | struct uiuser *user; | |
305 | ||
306 | for(user = users; user != NULL; user = user->next) | |
307 | { | |
308 | if(!wcscmp(user->name, name)) | |
309 | break; | |
310 | } | |
311 | return(user); | |
312 | } | |
313 | ||
314 | static void logout(struct uidata *data) | |
315 | { | |
316 | data->userinfo = NULL; | |
317 | if(data->username != NULL) | |
318 | free(data->username); | |
319 | data->username = NULL; | |
320 | if(data->auth != NULL) | |
321 | authputhandle(data->auth); | |
322 | data->auth = NULL; | |
323 | } | |
324 | ||
325 | static int haspriv(struct uidata *data, int perm) | |
326 | { | |
327 | if(data->userinfo == NULL) | |
328 | return(0); | |
329 | if(data->userinfo->perms & perm) | |
330 | return(1); | |
331 | else | |
332 | return(0); | |
333 | } | |
334 | ||
335 | /* Useful macros for the command functions: */ | |
336 | #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0) | |
a9bfddfe | 337 | #define havepriv(p) do { if((data->userinfo == NULL) || ((data->userinfo->perms & (p)) != (p))) { sq(sk, 0, L"502", L"Unauthorized request", L"needed", L"%x", (p), NULL); return; } } while(0) |
d3372da9 | 338 | |
339 | static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
340 | { | |
341 | int valid; | |
504ba854 | 342 | struct in6_addr mv4lo; |
d1ef0200 | 343 | struct sockaddr *remote; |
d3372da9 | 344 | |
345 | if(confgetint("ui", "onlylocal")) | |
346 | { | |
d1ef0200 FT |
347 | valid = 0; |
348 | if(!sockpeeraddr(sk, &remote, NULL)) { | |
349 | switch(remote->sa_family) | |
350 | { | |
351 | case AF_INET: | |
352 | valid = ((struct sockaddr_in *)remote)->sin_addr.s_addr == INADDR_LOOPBACK; | |
353 | break; | |
354 | case AF_INET6: | |
355 | inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo); | |
356 | valid = 0; | |
357 | if(!memcmp(&((struct sockaddr_in6 *)remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback))) | |
358 | valid = 1; | |
359 | if(!memcmp(&((struct sockaddr_in6 *)remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback))) | |
360 | valid = 1; | |
361 | break; | |
362 | case AF_UNIX: | |
504ba854 | 363 | valid = 1; |
d1ef0200 FT |
364 | break; |
365 | } | |
366 | free(remote); | |
d3372da9 | 367 | } |
368 | if(!valid) | |
369 | { | |
370 | sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL); | |
d1ef0200 | 371 | closesock(sk); |
d3372da9 | 372 | data->close = 1; |
373 | return; | |
374 | } | |
375 | } | |
040118de | 376 | sq(sk, 0, L"201", L"1", L"3", L"Dolda Connect daemon v" VERSION, NULL); |
d3372da9 | 377 | } |
378 | ||
379 | static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
380 | { | |
381 | if((argv != NULL) && (argv[0] != NULL)) | |
f7292a8b | 382 | sq(sk, 0, L"500", L"Command not found", NULL); |
d3372da9 | 383 | else |
384 | sq(sk, 0, L"500", L"No command", NULL); | |
385 | } | |
386 | ||
387 | static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
388 | { | |
389 | extern volatile int running; | |
390 | ||
391 | havepriv(PERM_ADMIN); | |
392 | flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username); | |
393 | running = 0; | |
394 | sq(sk, 0, L"200", L"Daemon shutting down", NULL); | |
395 | } | |
396 | ||
397 | static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
398 | { | |
399 | sq(sk, 0, L"200", L"Closing connection", NULL); | |
400 | data->close = 1; | |
401 | } | |
402 | ||
403 | static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
404 | { | |
405 | struct authmech *mech, *prev; | |
406 | ||
407 | prev = NULL; | |
408 | for(mech = mechs; mech != NULL; mech = mech->next) | |
409 | { | |
3616b334 | 410 | if(mech->enabled && authavailable(mech, sk)) |
d3372da9 | 411 | { |
412 | if(prev != NULL) | |
413 | sq(sk, 1, L"200", prev->name, NULL); | |
414 | prev = mech; | |
415 | } | |
416 | } | |
417 | if(prev == NULL) | |
418 | sq(sk, 0, L"201", L"No authentication methods supported", NULL); | |
419 | else | |
420 | sq(sk, 0, L"200", prev->name, NULL); | |
421 | } | |
422 | ||
423 | static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
424 | { | |
425 | char *buf; | |
426 | int code; | |
427 | struct passwd *pwd; | |
428 | ||
429 | haveargs(3); | |
430 | if(data->username != NULL) | |
431 | { | |
432 | if(data->userinfo != NULL) | |
433 | sq(sk, 0, L"503", L"Already logged in", NULL); | |
434 | else | |
435 | sq(sk, 0, L"503", L"Already logging in", NULL); | |
436 | return; | |
437 | } | |
438 | if((buf = icwcstombs(argv[2], NULL)) == NULL) | |
439 | { | |
440 | sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL); | |
441 | return; | |
442 | } | |
443 | data->username = swcsdup(argv[2]); | |
444 | if((pwd = getpwnam(buf)) == NULL) | |
445 | data->uid = -1; | |
446 | else | |
447 | data->uid = pwd->pw_uid; | |
448 | if((data->auth = initauth(argv[1], buf)) == NULL) | |
449 | { | |
450 | if(errno == ENOENT) | |
451 | sq(sk, 0, L"508", L"No such authentication mechanism", NULL); | |
452 | else | |
f7292a8b | 453 | sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL); |
d3372da9 | 454 | free(buf); |
455 | logout(data); | |
456 | return; | |
457 | } | |
458 | free(buf); | |
3616b334 | 459 | switch(authenticate(data->auth, sk, NULL)) |
d3372da9 | 460 | { |
461 | case AUTH_SUCCESS: | |
462 | data->userinfo = finduser(data->username); | |
463 | if(data->userinfo == NULL) | |
464 | data->userinfo = finduser(L"default"); | |
465 | if(data->uid == -1) | |
466 | { | |
467 | sq(sk, 0, L"506", L"Authentication error", NULL); | |
d1ef0200 | 468 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formatsockpeer(sk)); |
d3372da9 | 469 | logout(data); |
470 | } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) { | |
471 | sq(sk, 0, L"506", L"Authentication error", NULL); | |
d1ef0200 | 472 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formatsockpeer(sk)); |
d3372da9 | 473 | logout(data); |
474 | } else { | |
475 | sq(sk, 0, L"200", L"Welcome", NULL); | |
d1ef0200 | 476 | flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formatsockpeer(sk)); |
d3372da9 | 477 | } |
478 | break; | |
479 | case AUTH_DENIED: | |
f7292a8b | 480 | sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL); |
d1ef0200 | 481 | flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formatsockpeer(sk)); |
d3372da9 | 482 | logout(data); |
483 | break; | |
484 | case AUTH_PASS: | |
485 | switch(data->auth->prompt) | |
486 | { | |
487 | case AUTH_PR_AUTO: | |
488 | code = 300; | |
489 | break; | |
490 | case AUTH_PR_NOECHO: | |
491 | code = 301; | |
492 | break; | |
493 | case AUTH_PR_ECHO: | |
494 | code = 302; | |
495 | break; | |
496 | case AUTH_PR_INFO: | |
497 | code = 303; | |
498 | break; | |
499 | case AUTH_PR_ERROR: | |
500 | code = 304; | |
501 | break; | |
502 | } | |
f7292a8b | 503 | sq(sk, 0, L"%i", code, data->auth->text, NULL); |
d3372da9 | 504 | break; |
505 | case AUTH_ERR: | |
f7292a8b | 506 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 | 507 | logout(data); |
508 | break; | |
509 | default: | |
510 | flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login"); | |
f7292a8b | 511 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 | 512 | logout(data); |
513 | break; | |
514 | } | |
515 | } | |
516 | ||
517 | static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
518 | { | |
519 | char *buf; | |
520 | int code; | |
521 | ||
522 | haveargs(2); | |
523 | if((buf = icwcstombs(argv[1], NULL)) == NULL) | |
524 | { | |
525 | sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL); | |
526 | return; | |
527 | } | |
528 | if((data->auth == NULL) || (data->userinfo != NULL)) | |
529 | { | |
530 | sq(sk, 0, L"507", L"Data not expected", NULL); | |
531 | return; | |
532 | } | |
3616b334 | 533 | switch(authenticate(data->auth, sk, buf)) |
d3372da9 | 534 | { |
535 | case AUTH_SUCCESS: | |
536 | data->userinfo = finduser(data->username); | |
537 | if(data->userinfo == NULL) | |
538 | data->userinfo = finduser(L"default"); | |
539 | if(data->uid == -1) | |
540 | { | |
541 | sq(sk, 0, L"506", L"Authentication error", NULL); | |
d1ef0200 | 542 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formatsockpeer(sk)); |
d3372da9 | 543 | logout(data); |
544 | } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) { | |
545 | sq(sk, 0, L"506", L"Authentication error", NULL); | |
d1ef0200 | 546 | flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formatsockpeer(sk)); |
d3372da9 | 547 | logout(data); |
548 | } else { | |
549 | sq(sk, 0, L"200", L"Welcome", NULL); | |
d1ef0200 | 550 | flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formatsockpeer(sk)); |
d3372da9 | 551 | } |
552 | break; | |
553 | case AUTH_DENIED: | |
f7292a8b | 554 | sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL); |
d1ef0200 | 555 | flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formatsockpeer(sk)); |
d3372da9 | 556 | logout(data); |
557 | break; | |
558 | case AUTH_PASS: | |
559 | switch(data->auth->prompt) | |
560 | { | |
561 | case AUTH_PR_AUTO: | |
562 | code = 300; | |
563 | break; | |
564 | case AUTH_PR_NOECHO: | |
565 | code = 301; | |
566 | break; | |
567 | case AUTH_PR_ECHO: | |
568 | code = 302; | |
569 | break; | |
570 | case AUTH_PR_INFO: | |
571 | code = 303; | |
572 | break; | |
573 | case AUTH_PR_ERROR: | |
574 | code = 304; | |
575 | break; | |
576 | } | |
f7292a8b | 577 | sq(sk, 0, L"%i", code, data->auth->text, NULL); |
d3372da9 | 578 | break; |
579 | case AUTH_ERR: | |
f7292a8b | 580 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 | 581 | logout(data); |
582 | break; | |
583 | default: | |
584 | flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass"); | |
f7292a8b | 585 | sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL); |
d3372da9 | 586 | logout(data); |
587 | break; | |
588 | } | |
589 | free(buf); | |
590 | } | |
591 | ||
592 | static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
593 | { | |
594 | int i; | |
595 | char *buf; | |
596 | int err; | |
597 | struct fnetnode *fn; | |
efc05613 | 598 | struct wcspair *args; |
d3372da9 | 599 | |
600 | haveargs(3); | |
601 | havepriv(PERM_FNETCTL); | |
dd31fcfa | 602 | for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next); |
603 | if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) { | |
604 | sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL); | |
605 | return; | |
606 | } | |
d3372da9 | 607 | if((buf = icwcstombs(argv[2], NULL)) == NULL) |
608 | { | |
609 | sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL); | |
610 | return; | |
611 | } | |
efc05613 | 612 | args = NULL; |
613 | for(i = 3; i < argc - 1; i += 2) | |
614 | newwcspair(argv[i], argv[i + 1], &args); | |
a3a4a3a7 | 615 | fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args); |
d3372da9 | 616 | err = errno; |
617 | free(buf); | |
618 | if(fn == NULL) | |
619 | { | |
620 | if(errno == EPROTONOSUPPORT) | |
621 | sq(sk, 0, L"511", L"No such network name", NULL); | |
622 | else | |
f7292a8b | 623 | sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL); |
d3372da9 | 624 | return; |
625 | } | |
d3372da9 | 626 | linkfnetnode(fn); |
627 | fnetsetname(fn, argv[2]); | |
f7292a8b | 628 | sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL); |
d3372da9 | 629 | putfnetnode(fn); |
d3372da9 | 630 | } |
631 | ||
632 | static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
633 | { | |
634 | struct fnetnode *fn; | |
635 | ||
636 | if(fnetnodes == NULL) | |
637 | { | |
638 | sq(sk, 0, L"201", L"No connected nodes", NULL); | |
639 | return; | |
640 | } | |
641 | for(fn = fnetnodes; fn != NULL; fn = fn->next) | |
642 | { | |
f7292a8b | 643 | sq(sk, (fn->next != NULL)?1:0, L"200", L"%i", fn->id, fn->fnet->name, (fn->name == NULL)?L"":fn->name, L"%i", fn->numpeers, L"%i", fn->state, L"%ls", fn->pubid, NULL); |
d3372da9 | 644 | } |
645 | } | |
646 | ||
647 | static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
648 | { | |
649 | struct fnetnode *fn; | |
650 | int i; | |
651 | ||
652 | haveargs(2); | |
653 | havepriv(PERM_FNETCTL); | |
654 | /* Note - Programmatical user interfaces must only give one | |
655 | * argument per command, the multiple argument form is only for | |
656 | * convenience when manually controlling the daemon via | |
657 | * eg. telnet. The reason is that the return codes aren't clear | |
658 | * enough for the multiple argument form. */ | |
659 | for(i = 1; i < argc; i++) | |
660 | { | |
661 | if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL) | |
662 | { | |
663 | sq(sk, 0, L"510", L"No such node", NULL); | |
664 | return; | |
665 | } | |
18603153 | 666 | if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner))) |
667 | { | |
668 | sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL); | |
669 | return; | |
670 | } | |
d3372da9 | 671 | killfnetnode(fn); |
672 | unlinkfnetnode(fn); | |
673 | } | |
674 | sq(sk, 0, L"200", L"Node flagged for disconnection", NULL); | |
675 | } | |
676 | ||
677 | static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
678 | { | |
679 | struct fnetnode *fn; | |
680 | struct fnetpeerdatum *datum; | |
681 | ||
682 | haveargs(2); | |
683 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) | |
684 | { | |
685 | sq(sk, 0, L"510", L"No such node", NULL); | |
686 | return; | |
687 | } | |
688 | if(fn->peerdata == NULL) | |
689 | { | |
690 | sq(sk, 0, L"201", L"No data available", NULL); | |
691 | } else { | |
692 | for(datum = fn->peerdata; datum != NULL; datum = datum->next) | |
f7292a8b | 693 | sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL); |
d3372da9 | 694 | } |
695 | } | |
696 | ||
697 | static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
698 | { | |
699 | int i; | |
700 | struct fnetnode *fn; | |
0e227c1d | 701 | struct fnetpeer *peer, *npeer; |
d3372da9 | 702 | |
703 | haveargs(2); | |
704 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) | |
705 | { | |
706 | sq(sk, 0, L"510", L"No such node", NULL); | |
707 | return; | |
708 | } | |
709 | if(fn->peers == NULL) | |
710 | { | |
821e3843 | 711 | sq(sk, 0, L"201", L"No peers available", NULL); |
d3372da9 | 712 | } else { |
0e227c1d | 713 | for(peer = btreeiter(fn->peers); peer != NULL; peer = npeer) |
d3372da9 | 714 | { |
0e227c1d FT |
715 | npeer = btreeiter(NULL); |
716 | sq(sk, 2 | ((npeer != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL); | |
d3372da9 | 717 | for(i = 0; i < peer->dinum; i++) |
718 | { | |
719 | if(peer->peerdi[i].datum->datatype == FNPD_INT) | |
f7292a8b | 720 | sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL); |
d3372da9 | 721 | if(peer->peerdi[i].datum->datatype == FNPD_LL) |
f7292a8b | 722 | sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL); |
d3372da9 | 723 | if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL)) |
f7292a8b | 724 | sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL); |
d3372da9 | 725 | } |
726 | sq(sk, 0, NULL); | |
727 | } | |
728 | } | |
729 | } | |
730 | ||
731 | static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
732 | { | |
733 | int i; | |
734 | struct fnet *net; | |
735 | struct fnetnode *fn; | |
736 | struct transfer *transfer; | |
737 | struct fnetpeer *peer; | |
738 | ||
739 | haveargs(4); | |
740 | if((argc > 5) && ((argc % 2) == 0)) | |
741 | { | |
742 | sq(sk, 0, L"501", L"Must have an even number of arguments", NULL); | |
743 | return; | |
744 | } | |
745 | havepriv(PERM_TRANS); | |
746 | if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9')) | |
747 | { | |
748 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) | |
749 | { | |
750 | sq(sk, 0, L"510", L"No such node", NULL); | |
751 | return; | |
752 | } | |
753 | net = fn->fnet; | |
754 | } else { | |
755 | fn = NULL; | |
756 | if((net = findfnet(argv[1])) == NULL) | |
757 | { | |
758 | sq(sk, 0, L"511", L"No such network name", NULL); | |
759 | return; | |
760 | } | |
761 | } | |
762 | transfer = newtransfer(); | |
763 | authgethandle(transfer->auth = data->auth); | |
764 | transfer->fnet = net; | |
765 | transfer->peerid = swcsdup(argv[2]); | |
766 | transfer->path = swcsdup(argv[3]); | |
767 | transfer->dir = TRNSD_DOWN; | |
768 | transfer->owner = data->uid; | |
769 | if(fn != NULL) | |
770 | { | |
771 | transfer->fn = fn; | |
772 | getfnetnode(fn); | |
773 | linktransfer(transfer); | |
774 | if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL)) | |
775 | transfersetnick(transfer, peer->nick); | |
776 | } else { | |
777 | linktransfer(transfer); | |
778 | } | |
779 | if(argc > 4) | |
dcf7a1a2 | 780 | transfersetsize(transfer, wcstoll(argv[4], NULL, 0)); |
d3372da9 | 781 | if(argc > 5) |
782 | { | |
783 | for(i = 5; i < argc; i += 2) | |
5123bec0 | 784 | { |
785 | if(!wcscmp(argv[i], L"hash")) | |
786 | { | |
a04fc9ff | 787 | transfersethash(transfer, parsehash(argv[i + 1])); |
5123bec0 | 788 | } else { |
9ec790e8 | 789 | newwcspair(argv[i], argv[i + 1], &transfer->args); |
5123bec0 | 790 | } |
791 | } | |
d3372da9 | 792 | } |
f7292a8b | 793 | sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL); |
d3372da9 | 794 | transfersetactivity(transfer, L"create"); |
795 | } | |
796 | ||
797 | static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
798 | { | |
799 | struct transfer *transfer, *pt; | |
800 | ||
801 | havepriv(PERM_TRANS); | |
802 | pt = NULL; | |
803 | for(transfer = transfers; transfer != NULL; transfer = transfer->next) | |
804 | { | |
805 | if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid)) | |
806 | { | |
807 | if(pt != NULL) | |
f7292a8b | 808 | sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir, |
809 | L"%i", pt->state, pt->peerid, | |
d3372da9 | 810 | (pt->peernick == NULL)?L"":(pt->peernick), |
811 | (pt->path == NULL)?L"":(pt->path), | |
dcf7a1a2 | 812 | L"%oi", pt->size, L"%oi", pt->curpos, |
5123bec0 | 813 | (pt->hash == NULL)?L"":unparsehash(pt->hash), |
d3372da9 | 814 | NULL); |
815 | pt = transfer; | |
816 | } | |
817 | } | |
818 | if(pt == NULL) | |
819 | sq(sk, 0, L"201", L"No transfers", NULL); | |
820 | else | |
f7292a8b | 821 | sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir, |
822 | L"%i", pt->state, pt->peerid, | |
d3372da9 | 823 | (pt->peernick == NULL)?L"":(pt->peernick), |
824 | (pt->path == NULL)?L"":(pt->path), | |
dcf7a1a2 | 825 | L"%oi", pt->size, L"%oi", pt->curpos, |
5123bec0 | 826 | (pt->hash == NULL)?L"":unparsehash(pt->hash), |
d3372da9 | 827 | NULL); |
828 | } | |
829 | ||
830 | static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
831 | { | |
832 | struct transfer *transfer; | |
833 | ||
834 | haveargs(2); | |
835 | havepriv(PERM_TRANS); | |
836 | if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL) | |
837 | { | |
838 | sq(sk, 0, L"512", L"No such transfer", NULL); | |
839 | return; | |
840 | } | |
841 | if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU)) | |
842 | { | |
843 | sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL); | |
844 | return; | |
845 | } | |
846 | if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid)) | |
847 | { | |
848 | sq(sk, 0, L"502", L"You do not own that transfer", NULL); | |
849 | return; | |
850 | } | |
851 | transfer->close = 1; | |
852 | sq(sk, 0, L"200", L"Transfer cancelled", NULL); | |
853 | } | |
854 | ||
4a9a79b3 | 855 | static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
856 | { | |
857 | struct transfer *transfer; | |
858 | ||
859 | haveargs(2); | |
860 | havepriv(PERM_TRANS); | |
861 | if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL) | |
862 | { | |
863 | sq(sk, 0, L"512", L"No such transfer", NULL); | |
864 | return; | |
865 | } | |
866 | if(transfer->dir == TRNSD_UP) | |
867 | { | |
868 | sq(sk, 0, L"512", L"Only applicable to downloads", NULL); | |
869 | return; | |
870 | } | |
871 | resettransfer(transfer); | |
872 | sq(sk, 0, L"200", L"Transfer reset", NULL); | |
873 | } | |
874 | ||
d3372da9 | 875 | static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
876 | { | |
877 | int i, val; | |
878 | ||
879 | if((argc % 2) != 1) | |
880 | { | |
881 | sq(sk, 0, L"501", L"Must have an even number of arguments", NULL); | |
882 | return; | |
883 | } | |
884 | for(i = 1; i < argc; i += 2) | |
885 | { | |
886 | if(!wcscasecmp(argv[i + 1], L"on")) | |
887 | val = 1; | |
888 | else | |
889 | val = 0; | |
890 | if(!wcscasecmp(argv[i], L"all")) | |
891 | { | |
892 | if(val) | |
893 | data->notify.w = ~0; | |
894 | else | |
895 | data->notify.w = 0; | |
896 | } else if(!wcscasecmp(argv[i], L"fn:chat")) { | |
897 | data->notify.b.fnchat = val; | |
898 | } else if(!wcscasecmp(argv[i], L"fn:act")) { | |
899 | data->notify.b.fnact = val; | |
ba86d49a | 900 | } else if(!wcscasecmp(argv[i], L"fn:peer")) { |
901 | data->notify.b.fnpeer = val; | |
d3372da9 | 902 | } else if(!wcscasecmp(argv[i], L"trans:act")) { |
903 | data->notify.b.tract = val; | |
904 | } else if(!wcscasecmp(argv[i], L"trans:prog")) { | |
905 | data->notify.b.trprog = val; | |
906 | } else if(!wcscasecmp(argv[i], L"srch:act")) { | |
907 | data->notify.b.srch = val; | |
5f151349 | 908 | } else if(!wcscasecmp(argv[i], L"msg")) { |
909 | data->notify.b.msg = val; | |
d3372da9 | 910 | } |
911 | } | |
912 | sq(sk, 0, L"200", L"Notification alteration succeeded", NULL); | |
913 | } | |
914 | ||
915 | static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
916 | { | |
917 | struct fnetnode *fn; | |
918 | int public; | |
919 | ||
920 | haveargs(5); | |
921 | havepriv(PERM_CHAT); | |
922 | if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL) | |
923 | { | |
924 | sq(sk, 0, L"510", L"No such node", NULL); | |
925 | return; | |
926 | } | |
927 | public = wcstol(argv[2], NULL, 0); | |
928 | if((public != 0) && (public != 1)) | |
929 | { | |
930 | sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL); | |
931 | return; | |
932 | } | |
933 | if(fn->state != FNN_EST) | |
934 | { | |
935 | sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL); | |
936 | return; | |
937 | } | |
938 | if(fnetsendchat(fn, public, argv[3], argv[4])) | |
939 | { | |
940 | if(errno == ENOTSUP) | |
941 | sq(sk, 0, L"513", L"This network does not support chatting", NULL); | |
942 | else if(errno == EPERM) | |
943 | sq(sk, 0, L"502", L"This node does not allow you to chat", NULL); | |
944 | else if(errno == EILSEQ) | |
945 | sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL); | |
946 | else | |
947 | sq(sk, 0, L"505", L"Could not chat", NULL); | |
948 | return; | |
949 | } | |
950 | sq(sk, 0, L"200", L"Chat string sent", NULL); | |
951 | } | |
952 | ||
953 | static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
954 | { | |
955 | struct search *srch; | |
956 | struct fnetnode *fn; | |
957 | struct sexpr *sexpr; | |
958 | int i; | |
959 | ||
960 | haveargs(3); | |
961 | havepriv(PERM_SRCH); | |
962 | srch = newsearch(data->username, NULL); | |
963 | for(i = 1; i < argc; i++) | |
964 | { | |
965 | if(!wcscmp(argv[i], L"all")) | |
966 | { | |
967 | for(fn = fnetnodes; fn != NULL; fn = fn->next) | |
968 | { | |
969 | if(fn->state == FNN_EST) | |
970 | searchaddfn(srch, fn); | |
971 | } | |
972 | i++; | |
973 | break; | |
974 | } else if(!wcscmp(argv[i], L"prio")) { | |
975 | if(++i == argc) | |
976 | { | |
977 | sq(sk, 0, L"501", L"No argument to prio", NULL); | |
978 | freesearch(srch); | |
979 | return; | |
980 | } | |
981 | srch->prio = wcstol(argv[i], NULL, 0); | |
982 | } else if(iswdigit(*argv[i])) { | |
983 | if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL) | |
984 | { | |
985 | sq(sk, 0, L"510", L"No such node", NULL); | |
986 | freesearch(srch); | |
987 | return; | |
988 | } | |
989 | searchaddfn(srch, fn); | |
990 | } else { | |
991 | break; | |
992 | } | |
993 | } | |
994 | if(srch->fnl == NULL) | |
995 | { | |
996 | sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL); | |
997 | freesearch(srch); | |
998 | return; | |
999 | } | |
1000 | if(i == argc) | |
1001 | { | |
1002 | sq(sk, 0, L"501", L"No search expression found on line", NULL); | |
1003 | freesearch(srch); | |
1004 | return; | |
1005 | } | |
1006 | if((sexpr = parsesexpr(argc - i, argv + i)) == NULL) | |
1007 | { | |
1008 | sq(sk, 0, L"509", L"Could not parse search expression", NULL); | |
1009 | freesearch(srch); | |
1010 | return; | |
1011 | } | |
1012 | optsexpr(sexpr); | |
1013 | getsexpr(srch->sexpr = sexpr); | |
1014 | queuesearch(srch); | |
1015 | CBREG(srch, search_eta, srcheta, NULL, NULL); | |
1016 | CBREG(srch, search_commit, srchcommit, NULL, NULL); | |
1017 | CBREG(srch, search_result, srchres, NULL, NULL); | |
f7292a8b | 1018 | sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL); |
d3372da9 | 1019 | putsexpr(sexpr); |
1020 | } | |
1021 | ||
1022 | static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1023 | { | |
1024 | struct search *srch, *pt; | |
1025 | time_t now; | |
1026 | ||
1027 | havepriv(PERM_SRCH); | |
1028 | pt = NULL; | |
1029 | now = time(NULL); | |
1030 | for(srch = searches; srch != NULL; srch = srch->next) | |
1031 | { | |
1032 | if(!wcscmp(srch->owner, data->username)) | |
1033 | { | |
1034 | if(pt != NULL) | |
f7292a8b | 1035 | sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL); |
d3372da9 | 1036 | pt = srch; |
1037 | } | |
1038 | } | |
1039 | if(pt == NULL) | |
1040 | sq(sk, 0, L"201", L"No searches", NULL); | |
1041 | else | |
f7292a8b | 1042 | sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL); |
d3372da9 | 1043 | } |
1044 | ||
1045 | static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1046 | { | |
1047 | struct search *srch; | |
1048 | struct srchres *sr; | |
d3372da9 | 1049 | |
1050 | haveargs(2); | |
1051 | havepriv(PERM_SRCH); | |
1052 | if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL) | |
1053 | { | |
1054 | sq(sk, 0, L"514", L"No such search", NULL); | |
1055 | return; | |
1056 | } | |
1057 | if(srch->results == NULL) | |
1058 | { | |
1059 | sq(sk, 0, L"201", L"No results", NULL); | |
1060 | } else { | |
1061 | for(sr = srch->results; sr != NULL; sr = sr->next) | |
1062 | { | |
f7292a8b | 1063 | sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename, |
dcf7a1a2 | 1064 | sr->fnet->name, L"%ls", sr->peerid, L"%oi", sr->size, |
f7292a8b | 1065 | L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id), |
1066 | L"%f", sr->time, | |
1067 | L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL); | |
d3372da9 | 1068 | } |
1069 | } | |
1070 | } | |
1071 | ||
1072 | static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1073 | { | |
1074 | struct search *srch; | |
1075 | int i; | |
1076 | ||
1077 | haveargs(2); | |
1078 | havepriv(PERM_SRCH); | |
1079 | /* Note - Programmatical user interfaces must only give one | |
1080 | * argument per command, the multiple argument form is only for | |
1081 | * convenience when manually controlling the daemon via | |
1082 | * eg. telnet. The reason is that the return codes aren't clear | |
1083 | * enough for the multiple argument form. */ | |
1084 | for(i = 1; i < argc; i++) | |
1085 | { | |
1086 | if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL) | |
1087 | { | |
1088 | sq(sk, 0, L"514", L"No such search", NULL); | |
1089 | return; | |
1090 | } | |
1091 | freesearch(srch); | |
1092 | } | |
1093 | sq(sk, 0, L"200", L"Search cancelled", NULL); | |
1094 | } | |
1095 | ||
cab0b442 | 1096 | static void fcmdread(struct socket *sk, struct uidata *data) |
d3372da9 | 1097 | { |
1098 | char *buf; | |
1099 | size_t bufsize; | |
1100 | ||
1101 | if((buf = sockgetinbuf(sk, &bufsize)) == NULL) | |
cab0b442 | 1102 | return; |
d3372da9 | 1103 | bufcat(data->fcmdbuf, buf, bufsize); |
1104 | free(buf); | |
1105 | } | |
1106 | ||
cab0b442 | 1107 | static void fcmderr(struct socket *sk, int err, struct uidata *data) |
d3372da9 | 1108 | { |
1109 | wchar_t *wbuf, *p, *p2; | |
1110 | ||
1111 | if(err) | |
1112 | { | |
1113 | flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err)); | |
1114 | kill(-data->fcmdpid, SIGHUP); | |
1115 | putsock(data->fcmdsk); | |
1116 | data->fcmdsk = NULL; | |
1117 | if(data->fcmdbuf != NULL) | |
1118 | { | |
1119 | free(data->fcmdbuf); | |
1120 | data->fcmdbuf = NULL; | |
1121 | } | |
1122 | data->fcmdbufsize = data->fcmdbufdata = 0; | |
f7292a8b | 1123 | sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL); |
cab0b442 | 1124 | return; |
d3372da9 | 1125 | } |
1126 | putsock(data->fcmdsk); | |
1127 | data->fcmdsk = NULL; | |
1128 | data->fcmdpid = 0; | |
1129 | if(data->fcmdbuf == NULL) | |
1130 | { | |
1131 | wbuf = swcsdup(L""); | |
1132 | } else { | |
1133 | addtobuf(data->fcmdbuf, 0); | |
1134 | wbuf = icmbstowcs(data->fcmdbuf, NULL); | |
1135 | free(data->fcmdbuf); | |
1136 | } | |
1137 | data->fcmdbuf = NULL; | |
1138 | data->fcmdbufsize = data->fcmdbufdata = 0; | |
1139 | if(wbuf == NULL) | |
1140 | { | |
1141 | sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL); | |
cab0b442 | 1142 | return; |
d3372da9 | 1143 | } |
1144 | p = wbuf; | |
1145 | for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n')) | |
1146 | { | |
1147 | *(p2++) = L'\0'; | |
f7292a8b | 1148 | sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL); |
d3372da9 | 1149 | p = p2; |
1150 | } | |
1151 | if(*p == L'\0') | |
1152 | { | |
1153 | if(p == wbuf) | |
1154 | sq(data->sk, 0, L"201", L"No data returned", NULL); | |
1155 | } else { | |
f7292a8b | 1156 | sq(data->sk, 0, L"200", L"%ls", p, NULL); |
d3372da9 | 1157 | } |
1158 | free(wbuf); | |
1159 | } | |
1160 | ||
1161 | static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1162 | { | |
1163 | int i; | |
1164 | pid_t pid; | |
1165 | int pipe; | |
1166 | char **cargv, **pp; | |
1167 | char *filtercmd, *argbuf; | |
1168 | size_t cargvsize, cargvdata; | |
1169 | struct passwd *pwent; | |
1170 | ||
1171 | haveargs(2); | |
1172 | havepriv(PERM_TRANS); | |
1173 | if((pwent = getpwuid(data->uid)) == NULL) | |
1174 | { | |
1175 | flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid); | |
1176 | sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL); | |
1177 | return; | |
1178 | } | |
c0845d5b | 1179 | filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0); |
ddb933f1 | 1180 | if(filtercmd == NULL) |
c0845d5b | 1181 | filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0); |
ddb933f1 | 1182 | if(filtercmd == NULL) |
d3372da9 | 1183 | { |
1184 | flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name); | |
1185 | sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL); | |
1186 | return; | |
1187 | } | |
1188 | cargv = NULL; | |
1189 | cargvsize = cargvdata = 0; | |
1190 | addtobuf(cargv, filtercmd); | |
1191 | for(i = 1; i < argc; i++) | |
1192 | { | |
1193 | if((argbuf = icwcstombs(argv[i], NULL)) == NULL) | |
1194 | { | |
1195 | for(i = 0; i < cargvdata; i++) | |
1196 | free(cargv[i]); | |
1197 | free(cargv); | |
f7292a8b | 1198 | sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL); |
d3372da9 | 1199 | return; |
1200 | } | |
1201 | addtobuf(cargv, argbuf); | |
1202 | } | |
1203 | addtobuf(cargv, NULL); | |
1204 | if((pid = forksess(data->uid, data->auth, NULL, NULL, FD_FILE, 0, O_RDWR, "/dev/null", FD_PIPE, 1, O_RDONLY, &pipe, FD_FILE, 2, O_RDWR, "/dev/null", FD_END)) < 0) | |
1205 | { | |
1206 | flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno)); | |
f7292a8b | 1207 | sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL); |
d3372da9 | 1208 | return; |
1209 | } | |
1210 | if(pid == 0) | |
1211 | { | |
1212 | execv(filtercmd, cargv); | |
1213 | flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno)); | |
1214 | exit(127); | |
1215 | } | |
1216 | for(pp = cargv; *pp; pp++) | |
1217 | free(*pp); | |
1218 | free(cargv); | |
1219 | data->fcmdsk = wrapsock(pipe); | |
1220 | data->fcmdpid = pid; | |
1221 | if(data->fcmdbuf != NULL) | |
1222 | { | |
1223 | free(data->fcmdbuf); | |
1224 | data->fcmdbuf = NULL; | |
1225 | } | |
1226 | data->fcmdbufsize = data->fcmdbufdata = 0; | |
cab0b442 | 1227 | data->fcmdsk->data = data; |
1228 | data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread; | |
1229 | data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr; | |
d3372da9 | 1230 | } |
1231 | ||
1232 | static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1233 | { | |
1234 | struct transfer *transfer; | |
9ec790e8 | 1235 | struct wcspair *ta; |
d3372da9 | 1236 | |
1237 | haveargs(2); | |
1238 | havepriv(PERM_TRANS); | |
1239 | if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL) | |
1240 | { | |
1241 | sq(sk, 0, L"512", L"No such transfer", NULL); | |
1242 | return; | |
1243 | } | |
1244 | if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid)) | |
1245 | { | |
1246 | sq(sk, 0, L"502", L"You do not own that transfer", NULL); | |
1247 | return; | |
1248 | } | |
1249 | if(transfer->args == NULL) | |
1250 | { | |
1251 | sq(sk, 0, L"201", L"Transfer has no arguments", NULL); | |
1252 | } else { | |
1253 | for(ta = transfer->args; ta != NULL; ta = ta->next) | |
f7292a8b | 1254 | sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL); |
d3372da9 | 1255 | } |
1256 | } | |
1257 | ||
1258 | static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1259 | { | |
1260 | struct sharecache *node; | |
1261 | int total, hashed; | |
1262 | ||
1263 | total = hashed = 0; | |
1264 | for(node = shareroot->child; node != NULL; node = nextscnode(node)) | |
1265 | { | |
1266 | if(node->f.b.type == FILE_REG) | |
1267 | { | |
1268 | total++; | |
1269 | if(node->f.b.hastth) | |
1270 | hashed++; | |
1271 | } | |
1272 | } | |
f7292a8b | 1273 | sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL); |
d3372da9 | 1274 | } |
1275 | ||
796283c6 | 1276 | static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1277 | { | |
796283c6 | 1278 | havepriv(PERM_TRANS); |
f7292a8b | 1279 | sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL); |
796283c6 | 1280 | } |
1281 | ||
5f151349 | 1282 | static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1283 | { | |
1284 | struct uidata *d2; | |
1285 | ||
1286 | haveargs(2); | |
1287 | if(data->userinfo == NULL) { | |
1288 | sq(sk, 0, L"502", L"Must be logged in", NULL); | |
1289 | return; | |
1290 | } | |
1291 | if(argv[1][0] == L'#') { | |
1292 | sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL); | |
1293 | return; | |
1294 | } | |
1295 | for(d2 = actives; d2 != NULL; d2 = d2->next) { | |
7b2663a4 | 1296 | if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) { |
5f151349 | 1297 | sq(sk, 0, L"516", L"Name already in use", NULL); |
1298 | return; | |
1299 | } | |
1300 | } | |
1301 | if(data->regname != NULL) | |
1302 | free(data->regname); | |
1303 | data->regname = swcsdup(argv[1]); | |
1304 | sq(sk, 0, L"200", L"Registered", NULL); | |
1305 | } | |
1306 | ||
1307 | static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) | |
1308 | { | |
1309 | int i, rcptid; | |
1310 | struct uidata *rcpt; | |
1311 | wchar_t *myname; | |
1312 | struct notif *notif; | |
1313 | ||
1314 | haveargs(2); | |
1315 | if(data->userinfo == NULL) { | |
1316 | sq(sk, 0, L"502", L"Must be logged in", NULL); | |
1317 | return; | |
1318 | } | |
1319 | if(argv[1][0] == L'#') { | |
1320 | rcptid = wcstol(argv[1] + 1, NULL, 0); | |
1321 | for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) { | |
7b2663a4 | 1322 | if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid)) |
5f151349 | 1323 | break; |
1324 | } | |
1325 | } else { | |
1326 | for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) { | |
7b2663a4 | 1327 | if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1])) |
5f151349 | 1328 | break; |
1329 | } | |
1330 | } | |
1331 | if(rcpt == NULL) { | |
1332 | sq(sk, 0, L"517", L"No such recipient", NULL); | |
1333 | return; | |
1334 | } | |
1335 | if(!rcpt->notify.b.msg) { | |
1336 | sq(sk, 0, L"518", L"Recipient not listening for messages", NULL); | |
1337 | return; | |
1338 | } | |
1339 | if(data->regname != NULL) | |
1340 | myname = swcsdup(data->regname); | |
1341 | else | |
1342 | myname = swprintf2(L"#%i", data->id); | |
1343 | notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END); | |
1344 | for(i = 2; i < argc; i++) | |
1345 | notifappend(notif, NOTIF_STR, argv[i], NOTIF_END); | |
1346 | sq(sk, 0, L"200", L"Message sent", NULL); | |
1347 | } | |
1348 | ||
9a64a4f7 | 1349 | static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1350 | { | |
f7292a8b | 1351 | sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL); |
9a64a4f7 | 1352 | } |
1353 | ||
c45c9b79 | 1354 | static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) |
1355 | { | |
1356 | extern volatile int reinit; | |
1357 | ||
1358 | havepriv(PERM_ADMIN); | |
1359 | flog(LOG_NOTICE, "UI HUP request from %ls", data->username); | |
1360 | reinit = 1; | |
1361 | sq(sk, 0, L"200", L"Will reinit", NULL); | |
1362 | } | |
1363 | ||
d3372da9 | 1364 | #undef haveargs |
1365 | #undef havepriv | |
1366 | ||
1367 | /* | |
1368 | * Reserved command numbers for nameless commands: | |
1369 | * 0: Issued when a client has connected | |
1370 | * 1: Issued when a named command couldn't be found | |
1371 | */ | |
1372 | ||
1373 | static struct command commands[] = | |
1374 | { | |
1375 | {NULL, cmd_connect}, | |
1376 | {NULL, cmd_notfound}, | |
1377 | {L"shutdown", cmd_shutdown}, | |
1378 | {L"quit", cmd_quit}, | |
1379 | {L"lsauth", cmd_lsauth}, | |
1380 | {L"login", cmd_login}, | |
1381 | {L"pass", cmd_pass}, | |
1382 | {L"cnct", cmd_fnetconnect}, | |
1383 | {L"lsnodes", cmd_lsnodes}, | |
1384 | {L"dcnct", cmd_disconnect}, | |
1385 | {L"lspa", cmd_lspa}, | |
1386 | {L"lspeers", cmd_lspeers}, | |
1387 | {L"download", cmd_download}, | |
1388 | {L"lstrans", cmd_lstrans}, | |
1389 | {L"cancel", cmd_cancel}, | |
4a9a79b3 | 1390 | {L"reset", cmd_reset}, |
d3372da9 | 1391 | {L"notify", cmd_notify}, |
1392 | {L"sendchat", cmd_sendchat}, | |
1393 | {L"search", cmd_search}, | |
1394 | {L"lssrch", cmd_lssrch}, | |
1395 | {L"lssr", cmd_lssr}, | |
1396 | {L"cansrch", cmd_cansrch}, | |
1397 | {L"filtercmd", cmd_filtercmd}, | |
1398 | {L"lstrarg", cmd_lstrarg}, | |
1399 | {L"hashstatus", cmd_hashstatus}, | |
796283c6 | 1400 | {L"transstatus", cmd_transstatus}, |
5f151349 | 1401 | {L"register", cmd_register}, |
1402 | {L"sendmsg", cmd_sendmsg}, | |
9a64a4f7 | 1403 | {L"uptime", cmd_uptime}, |
c45c9b79 | 1404 | {L"hup", cmd_hup}, |
d3372da9 | 1405 | {NULL, NULL} |
1406 | }; | |
1407 | ||
1408 | static void freequeuecmd(struct qcommand *qcmd) | |
1409 | { | |
1410 | int i; | |
1411 | ||
1412 | if(qcmd->argv != NULL) | |
1413 | { | |
1414 | for(i = 0; i < qcmd->argc; i++) | |
1415 | free(qcmd->argv[i]); | |
1416 | free(qcmd->argv); | |
1417 | } | |
1418 | free(qcmd); | |
1419 | } | |
1420 | ||
1421 | static struct qcommand *unlinkqcmd(struct uidata *data) | |
1422 | { | |
1423 | struct qcommand *qcmd; | |
1424 | ||
1425 | qcmd = data->queue; | |
1426 | if(qcmd != NULL) | |
1427 | { | |
821e3843 | 1428 | data->queuesize--; |
d3372da9 | 1429 | data->queue = qcmd->next; |
1430 | if(qcmd == data->queuelast) | |
1431 | data->queuelast = qcmd->next; | |
1432 | } | |
1433 | return(qcmd); | |
1434 | } | |
1435 | ||
ba86d49a | 1436 | static void notifappendv(struct notif *notif, va_list args) |
d3372da9 | 1437 | { |
d3372da9 | 1438 | int dt, ca; |
1439 | ||
d3372da9 | 1440 | while((dt = va_arg(args, int)) != NOTIF_END) |
1441 | { | |
1442 | ca = notif->argc; | |
1443 | notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc); | |
1444 | notif->argv[ca].dt = dt; | |
1445 | switch(dt) | |
1446 | { | |
1447 | case NOTIF_INT: | |
1448 | case NOTIF_ID: | |
1449 | notif->argv[ca].d.n = va_arg(args, int); | |
1450 | break; | |
dcf7a1a2 FT |
1451 | case NOTIF_OFF: |
1452 | notif->argv[ca].d.o = va_arg(args, off_t); | |
1453 | break; | |
d3372da9 | 1454 | case NOTIF_STR: |
2af7e5b0 | 1455 | notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *)); |
d3372da9 | 1456 | break; |
1457 | case NOTIF_FLOAT: | |
1458 | notif->argv[ca].d.d = va_arg(args, double); | |
1459 | break; | |
1460 | } | |
1461 | } | |
ba86d49a | 1462 | } |
1463 | ||
1464 | static void notifappend(struct notif *notif, ...) | |
1465 | { | |
1466 | va_list args; | |
1467 | ||
1468 | va_start(args, notif); | |
1469 | notifappendv(notif, args); | |
1470 | va_end(args); | |
1471 | } | |
1472 | ||
1473 | static struct notif *newnotif(struct uidata *data, int code, ...) | |
1474 | { | |
1475 | struct notif *notif; | |
1476 | va_list args; | |
1477 | ||
1478 | notif = smalloc(sizeof(*notif)); | |
1479 | memset(notif, 0, sizeof(*notif)); | |
1480 | notif->rlimit = 0.0; | |
1481 | notif->ui = data; | |
1482 | notif->code = code; | |
1483 | va_start(args, code); | |
1484 | notifappendv(notif, args); | |
d3372da9 | 1485 | va_end(args); |
1486 | notif->next = NULL; | |
1487 | notif->prev = data->lnotif; | |
1488 | if(data->lnotif != NULL) | |
1489 | data->lnotif->next = notif; | |
1490 | else | |
1491 | data->fnotif = notif; | |
1492 | data->lnotif = notif; | |
1493 | return(notif); | |
1494 | } | |
1495 | ||
1496 | static void freenotif(struct notif *notif) | |
1497 | { | |
1498 | int i; | |
1499 | ||
1500 | if(notif->next != NULL) | |
1501 | notif->next->prev = notif->prev; | |
1502 | if(notif->prev != NULL) | |
1503 | notif->prev->next = notif->next; | |
1504 | if(notif == notif->ui->fnotif) | |
1505 | notif->ui->fnotif = notif->next; | |
1506 | if(notif == notif->ui->lnotif) | |
1507 | notif->ui->lnotif = notif->prev; | |
1508 | if(notif->exptimer != NULL) | |
1509 | canceltimer(notif->exptimer); | |
1510 | for(i = 0; i < notif->argc; i++) | |
1511 | { | |
1512 | if(notif->argv[i].dt == NOTIF_STR) | |
1513 | free(notif->argv[i].d.s); | |
1514 | } | |
1515 | if(notif->argv != NULL) | |
1516 | free(notif->argv); | |
1517 | free(notif); | |
1518 | } | |
1519 | ||
1520 | static void notifexpire(int cancelled, struct notif *notif) | |
1521 | { | |
1522 | notif->exptimer = NULL; | |
1523 | if(!cancelled) | |
1524 | freenotif(notif); | |
1525 | } | |
1526 | ||
1527 | static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id) | |
1528 | { | |
1529 | int i, cont; | |
1530 | ||
1531 | for(; notif != NULL; notif = (dir?notif->next:notif->prev)) | |
1532 | { | |
1533 | if((notif->code == code) && ((state < 0) || (state == notif->state))) | |
1534 | { | |
1535 | cont = 0; | |
1536 | if(id >= 0) | |
1537 | { | |
1538 | for(i = 0; i < notif->argc; i++) | |
1539 | { | |
1540 | if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id)) | |
1541 | { | |
1542 | cont = 1; | |
1543 | break; | |
1544 | } | |
1545 | } | |
1546 | } | |
1547 | if(cont) | |
1548 | continue; | |
1549 | break; | |
1550 | } | |
1551 | } | |
1552 | return(notif); | |
1553 | } | |
1554 | ||
1555 | static void freeuidata(struct uidata *data) | |
1556 | { | |
1557 | int i; | |
1558 | struct qcommand *qcmd; | |
1559 | ||
1560 | if(data->next != NULL) | |
1561 | data->next->prev = data->prev; | |
1562 | if(data->prev != NULL) | |
1563 | data->prev->next = data->next; | |
1564 | if(data == actives) | |
1565 | actives = data->next; | |
cab0b442 | 1566 | data->sk->readcb = NULL; |
1567 | data->sk->errcb = NULL; | |
68f4d429 | 1568 | closesock(data->sk); |
d3372da9 | 1569 | putsock(data->sk); |
1570 | while((qcmd = unlinkqcmd(data)) != NULL) | |
1571 | freequeuecmd(qcmd); | |
1572 | iconv_close(data->ichandle); | |
1573 | if(data->cw != NULL) | |
1574 | free(data->cw); | |
1575 | if(data->cb != NULL) | |
1576 | free(data->cb); | |
1577 | if(data->argv != NULL) | |
1578 | { | |
1579 | for(i = 0; i < data->argc; i++) | |
1580 | free(data->argv[i]); | |
1581 | free(data->argv); | |
1582 | } | |
1583 | if(data->auth != NULL) | |
1584 | authputhandle(data->auth); | |
5f151349 | 1585 | if(data->regname != NULL) |
1586 | free(data->regname); | |
d3372da9 | 1587 | if(data->username != NULL) |
1588 | { | |
1589 | if(data->userinfo != NULL) | |
1590 | flog(LOG_INFO, "%ls logged out", data->username); | |
1591 | free(data->username); | |
1592 | } | |
1593 | free(data->inbuf); | |
1594 | while(data->fnotif != NULL) | |
1595 | freenotif(data->fnotif); | |
1596 | if(data->fcmdbuf != NULL) | |
1597 | free(data->fcmdbuf); | |
1598 | if(data->fcmdpid != 0) | |
1599 | kill(-data->fcmdpid, SIGHUP); | |
1600 | if(data->fcmdsk != NULL) | |
1601 | putsock(data->fcmdsk); | |
1602 | free(data); | |
1603 | } | |
1604 | ||
1605 | static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv) | |
1606 | { | |
1607 | struct qcommand *new; | |
1608 | ||
1609 | new = smalloc(sizeof(*new)); | |
1610 | new->cmd = cmd; | |
1611 | new->argc = argc; | |
1612 | new->argv = argv; | |
1613 | new->next = NULL; | |
1614 | if(data->queuelast != NULL) | |
1615 | data->queuelast->next = new; | |
1616 | data->queuelast = new; | |
1617 | if(data->queue == NULL) | |
1618 | data->queue = new; | |
821e3843 | 1619 | data->queuesize++; |
d3372da9 | 1620 | } |
1621 | ||
1622 | static struct uidata *newuidata(struct socket *sk) | |
1623 | { | |
1624 | struct uidata *data; | |
5f151349 | 1625 | static int curid = 0; |
d3372da9 | 1626 | |
1627 | data = smalloc(sizeof(*data)); | |
1628 | memset(data, 0, sizeof(*data)); | |
5f151349 | 1629 | data->id = curid++; |
d3372da9 | 1630 | data->sk = sk; |
1631 | getsock(sk); | |
1632 | data->inbuf = smalloc(1024); | |
1633 | data->uid = -1; | |
1634 | if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1) | |
1635 | { | |
1636 | flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno)); | |
1637 | return(NULL); | |
1638 | } | |
1639 | data->next = actives; | |
1640 | data->prev = NULL; | |
1641 | if(actives != NULL) | |
1642 | actives->prev = data; | |
1643 | actives = data; | |
1644 | return(data); | |
1645 | } | |
1646 | ||
cab0b442 | 1647 | static void uiread(struct socket *sk, struct uidata *data) |
d3372da9 | 1648 | { |
1649 | int ret, done; | |
1650 | char *newbuf; | |
1651 | char *p1, *p2; | |
1652 | wchar_t *porig; | |
1653 | size_t datalen, len2; | |
1654 | struct command *cur; | |
1655 | ||
1656 | if(data->indata > 1024) | |
1657 | data->indata = 0; | |
1658 | if((newbuf = sockgetinbuf(sk, &datalen)) == NULL) | |
cab0b442 | 1659 | return; |
d3372da9 | 1660 | sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1); |
1661 | memcpy(data->inbuf + data->indata, newbuf, datalen); | |
1662 | free(newbuf); | |
1663 | data->indata += datalen; | |
1664 | if(data->cb == NULL) | |
1665 | { | |
1666 | data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64)); | |
1667 | data->cbdata = 0; | |
1668 | data->pp = data->cb; | |
1669 | } | |
1670 | done = 0; | |
1671 | while(!done) | |
1672 | { | |
1673 | if(data->cbsize == data->cbdata) | |
1674 | { | |
1675 | len2 = data->pp - data->cb; | |
1676 | data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2)); | |
1677 | data->pp = data->cb + len2; | |
1678 | } | |
1679 | p1 = data->inbuf; | |
1680 | p2 = (char *)(porig = (data->cb + data->cbdata)); | |
1681 | len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata); | |
1682 | ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2); | |
1683 | memmove(data->inbuf, p1, data->indata); | |
1684 | /* Just a sanity check */ | |
1685 | if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0) | |
1686 | { | |
1687 | flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!"); | |
1688 | abort(); | |
1689 | } | |
1690 | data->cbdata += (((wchar_t *)p2) - porig); | |
1691 | if(ret < 0) | |
1692 | { | |
1693 | switch(errno) | |
1694 | { | |
1695 | case EILSEQ: | |
1696 | /* XXX: Should this really just ignore it? */ | |
1697 | data->indata = 0; | |
1698 | done = 1; | |
1699 | break; | |
1700 | case EINVAL: | |
1701 | done = 1; | |
1702 | break; | |
1703 | case E2BIG: | |
1704 | /* Just a sanity check */ | |
1705 | if(data->cbsize != data->cbdata) | |
1706 | { | |
1707 | flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!"); | |
1708 | abort(); | |
1709 | } | |
1710 | break; | |
1711 | default: | |
1712 | flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno)); | |
1713 | break; | |
1714 | } | |
1715 | } else { | |
1716 | done = 1; | |
1717 | } | |
1718 | } | |
1719 | done = 0; | |
1720 | while(!done && (data->pp - data->cb < data->cbdata)) | |
1721 | { | |
1722 | switch(data->ps) | |
1723 | { | |
1724 | case 0: | |
1725 | if(iswspace(*data->pp)) | |
1726 | { | |
1727 | if(*data->pp == L'\r') | |
1728 | { | |
1729 | if(data->pp == data->cb + data->cbdata - 1) | |
1730 | { | |
1731 | done = 1; | |
1732 | break; | |
1733 | } | |
1734 | if(*(++data->pp) == L'\n') | |
1735 | { | |
1736 | if((data->argv != NULL) && (data->argv[0] != NULL)) | |
1737 | { | |
1738 | for(cur = commands; cur->handler != NULL; cur++) | |
1739 | { | |
1740 | if(cur->name == NULL) | |
1741 | continue; | |
1742 | if(!wcscasecmp(cur->name, data->argv[0])) | |
1743 | { | |
1744 | queuecmd(data, cur, data->argc, data->argv); | |
1745 | break; | |
1746 | } | |
1747 | } | |
1748 | if(cur->handler == NULL) | |
1749 | queuecmd(data, &commands[1], data->argc, data->argv); | |
1750 | } else { | |
1751 | queuecmd(data, &commands[1], data->argc, data->argv); | |
1752 | } | |
1753 | data->argv = NULL; | |
1754 | data->args = 0; | |
1755 | data->argc = 0; | |
1756 | wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb)); | |
1757 | data->pp = data->cb; | |
1758 | } else { | |
1759 | data->pp++; | |
1760 | } | |
1761 | } else { | |
1762 | data->pp++; | |
1763 | } | |
1764 | } else { | |
1765 | data->ps = 1; | |
1766 | data->cwdata = 0; | |
1767 | } | |
1768 | break; | |
1769 | case 1: | |
1770 | if(iswspace(*data->pp)) | |
1771 | { | |
1772 | addtobuf(data->cw, L'\0'); | |
1773 | sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1); | |
1774 | data->argv[data->argc++] = data->cw; | |
1775 | data->cw = NULL; | |
1776 | data->cwsize = 0; | |
1777 | data->cwdata = 0; | |
1778 | data->ps = 0; | |
1779 | } else if(*data->pp == L'\"') { | |
1780 | data->ps = 2; | |
1781 | data->pp++; | |
1782 | } else if(*data->pp == L'\\') { | |
1783 | if(data->pp == data->cb + data->cbdata - 1) | |
1784 | { | |
1785 | done = 1; | |
1786 | break; | |
1787 | } | |
1788 | addtobuf(data->cw, *(++data->pp)); | |
1789 | data->pp++; | |
1790 | } else { | |
1791 | addtobuf(data->cw, *(data->pp++)); | |
1792 | } | |
1793 | break; | |
1794 | case 2: | |
1795 | if(*data->pp == L'\"') | |
1796 | { | |
1797 | data->ps = 1; | |
1798 | } else if(*data->pp == L'\\') { | |
1799 | if(data->pp == data->cb + data->cbdata - 1) | |
1800 | { | |
1801 | done = 1; | |
1802 | break; | |
1803 | } | |
1804 | addtobuf(data->cw, *(++(data->pp))); | |
1805 | } else { | |
1806 | addtobuf(data->cw, *data->pp); | |
1807 | } | |
1808 | data->pp++; | |
1809 | break; | |
1810 | } | |
1811 | } | |
821e3843 FT |
1812 | if(data->cbdata > 16384) |
1813 | { | |
1814 | /* Kill clients that send us unreasonably long lines */ | |
1815 | data->close = 1; | |
1816 | } | |
d3372da9 | 1817 | } |
1818 | ||
cab0b442 | 1819 | static void uierror(struct socket *sk, int err, struct uidata *data) |
d3372da9 | 1820 | { |
1821 | if(err) | |
1822 | flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err)); | |
1823 | freeuidata(data); | |
1824 | } | |
1825 | ||
d1ef0200 | 1826 | static void uiaccept(struct lport *lp, struct socket *newsk, void *data) |
d3372da9 | 1827 | { |
1828 | struct uidata *uidata; | |
1829 | ||
cab0b442 | 1830 | newsk->data = uidata = newuidata(newsk); |
d3372da9 | 1831 | socksettos(newsk, confgetint("ui", "uitos")); |
1832 | if(uidata == NULL) | |
cab0b442 | 1833 | return; |
1834 | newsk->errcb = (void (*)(struct socket *, int, void *))uierror; | |
1835 | newsk->readcb = (void (*)(struct socket *, void *))uiread; | |
d3372da9 | 1836 | queuecmd(uidata, &commands[0], 0, NULL); |
1837 | } | |
1838 | ||
1839 | static int srcheta(struct search *srch, void *uudata) | |
1840 | { | |
1841 | struct uidata *data; | |
1842 | ||
1843 | for(data = actives; data != NULL; data = data->next) | |
1844 | { | |
1845 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) | |
1846 | newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END); | |
1847 | } | |
1848 | return(0); | |
1849 | } | |
1850 | ||
1851 | static int srchcommit(struct search *srch, void *uudata) | |
1852 | { | |
1853 | struct uidata *data; | |
1854 | ||
1855 | for(data = actives; data != NULL; data = data->next) | |
1856 | { | |
1857 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) | |
1858 | newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END); | |
1859 | } | |
1860 | return(0); | |
1861 | } | |
1862 | ||
1863 | static int srchres(struct search *srch, struct srchres *sr, void *uudata) | |
1864 | { | |
1865 | struct uidata *data; | |
1866 | ||
1867 | for(data = actives; data != NULL; data = data->next) | |
1868 | { | |
1869 | if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username)) | |
ebbc13f3 | 1870 | { |
dcf7a1a2 | 1871 | newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size, |
5123bec0 | 1872 | NOTIF_INT, sr->slots, NOTIF_INT, (sr->fn == NULL)?-1:(sr->fn->id), NOTIF_FLOAT, sr->time, NOTIF_STR, (sr->hash == NULL)?L"":unparsehash(sr->hash), NOTIF_END); |
ebbc13f3 | 1873 | } |
d3372da9 | 1874 | } |
1875 | return(0); | |
1876 | } | |
1877 | ||
1878 | static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata) | |
1879 | { | |
1880 | struct uidata *data; | |
1881 | ||
1882 | for(data = actives; data != NULL; data = data->next) | |
1883 | { | |
1884 | if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat) | |
1885 | newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END); | |
1886 | } | |
1887 | return(0); | |
1888 | } | |
1889 | ||
1890 | static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata) | |
1891 | { | |
1892 | struct uidata *data; | |
1893 | struct notif *notif; | |
1894 | ||
1895 | if(!wcscmp(attrib, L"state")) | |
1896 | { | |
1897 | for(data = actives; data != NULL; data = data->next) | |
1898 | { | |
1899 | if(data->notify.b.fnact) | |
1900 | newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END); | |
1901 | } | |
1902 | } else if(!wcscmp(attrib, L"name")) { | |
1903 | for(data = actives; data != NULL; data = data->next) | |
1904 | { | |
1905 | if(data->notify.b.fnact) | |
1906 | newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END); | |
1907 | } | |
1908 | } else if(!wcscmp(attrib, L"numpeers")) { | |
1909 | for(data = actives; data != NULL; data = data->next) | |
1910 | { | |
1911 | if(data->notify.b.fnact) | |
1912 | { | |
1913 | if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL) | |
1914 | notif->argv[1].d.n = fn->numpeers; | |
1915 | else | |
ba86d49a | 1916 | newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5; |
d3372da9 | 1917 | } |
1918 | } | |
1919 | } | |
1920 | return(0); | |
1921 | } | |
1922 | ||
1923 | static int fnunlink(struct fnetnode *fn, void *uudata) | |
1924 | { | |
1925 | struct uidata *data; | |
1926 | ||
1927 | for(data = actives; data != NULL; data = data->next) | |
1928 | { | |
1929 | if(data->notify.b.fnact) | |
1930 | newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END); | |
1931 | } | |
1932 | return(0); | |
1933 | } | |
1934 | ||
ba86d49a | 1935 | static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata) |
1936 | { | |
1937 | struct uidata *data; | |
1938 | ||
1939 | for(data = actives; data != NULL; data = data->next) | |
1940 | { | |
1941 | if(data->notify.b.fnpeer) | |
1942 | newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END); | |
1943 | } | |
1944 | return(0); | |
1945 | } | |
1946 | ||
1947 | static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata) | |
1948 | { | |
1949 | struct uidata *data; | |
1950 | ||
1951 | for(data = actives; data != NULL; data = data->next) | |
1952 | { | |
1953 | if(data->notify.b.fnpeer) | |
1954 | newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END); | |
1955 | } | |
1956 | return(0); | |
1957 | } | |
1958 | ||
1959 | static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata) | |
1960 | { | |
1961 | struct uidata *data; | |
1962 | struct notif *notif; | |
1963 | wchar_t buf[32]; | |
1964 | ||
1965 | for(data = actives; data != NULL; data = data->next) | |
1966 | { | |
1967 | if(data->notify.b.fnpeer) | |
1968 | { | |
1969 | for(notif = data->fnotif; notif != NULL; notif = notif->next) | |
1970 | { | |
1971 | if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id)) | |
1972 | break; | |
1973 | } | |
1974 | if(notif == NULL) | |
1975 | notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END); | |
1976 | notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END); | |
1977 | switch(di->datum->datatype) | |
1978 | { | |
1979 | case FNPD_INT: | |
1980 | notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END); | |
1981 | break; | |
1982 | case FNPD_STR: | |
1983 | notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END); | |
1984 | break; | |
1985 | case FNPD_LL: | |
1986 | swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum); | |
1987 | notifappend(notif, NOTIF_STR, buf, NOTIF_END); | |
1988 | break; | |
1989 | } | |
1990 | } | |
1991 | } | |
1992 | return(0); | |
1993 | } | |
1994 | ||
d3372da9 | 1995 | static int newfnetnode(struct fnetnode *fn, void *uudata) |
1996 | { | |
1997 | struct uidata *data; | |
1998 | ||
1999 | for(data = actives; data != NULL; data = data->next) | |
2000 | { | |
2001 | if(data->notify.b.fnact) | |
2002 | newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END); | |
2003 | } | |
2004 | CBREG(fn, fnetnode_ac, fnactive, NULL, NULL); | |
2005 | CBREG(fn, fnetnode_chat, recvchat, NULL, NULL); | |
2006 | CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL); | |
ba86d49a | 2007 | CBREG(fn, fnetpeer_new, peernew, NULL, NULL); |
2008 | CBREG(fn, fnetpeer_del, peerdel, NULL, NULL); | |
2009 | CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL); | |
d3372da9 | 2010 | return(0); |
2011 | } | |
2012 | ||
2013 | static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata) | |
2014 | { | |
2015 | struct uidata *data; | |
2016 | ||
2017 | if(!wcscmp(attrib, L"state")) | |
2018 | { | |
2019 | for(data = actives; data != NULL; data = data->next) | |
2020 | { | |
2021 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2022 | newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END); | |
2023 | } | |
2024 | } else if(!wcscmp(attrib, L"nick")) { | |
2025 | for(data = actives; data != NULL; data = data->next) | |
2026 | { | |
2027 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2028 | newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END); | |
2029 | } | |
2030 | } else if(!wcscmp(attrib, L"size")) { | |
2031 | for(data = actives; data != NULL; data = data->next) | |
2032 | { | |
2033 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
dcf7a1a2 | 2034 | newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END); |
d3372da9 | 2035 | } |
2036 | } else if(!wcscmp(attrib, L"error")) { | |
2037 | for(data = actives; data != NULL; data = data->next) | |
2038 | { | |
2039 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2040 | newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END); | |
2041 | } | |
2042 | } else if(!wcscmp(attrib, L"path")) { | |
2043 | for(data = actives; data != NULL; data = data->next) | |
2044 | { | |
2045 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2046 | newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END); | |
2047 | } | |
a04fc9ff | 2048 | } else if(!wcscmp(attrib, L"hash")) { |
2049 | for(data = actives; data != NULL; data = data->next) | |
2050 | { | |
2051 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
5e13cdca | 2052 | newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END); |
a04fc9ff | 2053 | } |
d3372da9 | 2054 | } |
2055 | return(0); | |
2056 | } | |
2057 | ||
2058 | static int transferprog(struct transfer *transfer, void *uudata) | |
2059 | { | |
2060 | struct uidata *data; | |
2061 | struct notif *notif; | |
2062 | ||
2063 | for(data = actives; data != NULL; data = data->next) | |
2064 | { | |
2065 | if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2066 | { | |
2067 | if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL) | |
dcf7a1a2 | 2068 | notif->argv[1].d.o = transfer->curpos; |
d3372da9 | 2069 | else |
dcf7a1a2 | 2070 | newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5; |
d3372da9 | 2071 | } |
2072 | } | |
2073 | return(0); | |
2074 | } | |
2075 | ||
2076 | static int transferdestroyed(struct transfer *transfer, void *uudata) | |
2077 | { | |
2078 | struct uidata *data; | |
2079 | ||
2080 | for(data = actives; data != NULL; data = data->next) | |
2081 | { | |
2082 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
3d204c2d | 2083 | newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END); |
d3372da9 | 2084 | } |
2085 | return(0); | |
2086 | } | |
2087 | ||
2088 | static int newtransfernotify(struct transfer *transfer, void *uudata) | |
2089 | { | |
2090 | struct uidata *data; | |
2091 | ||
2092 | for(data = actives; data != NULL; data = data->next) | |
2093 | { | |
2094 | if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid))) | |
2095 | newnotif(data, 610, NOTIF_ID, transfer->id, NOTIF_INT, transfer->dir, NOTIF_STR, transfer->peerid, NOTIF_STR, (transfer->path == NULL)?L"":transfer->path, NOTIF_END); | |
2096 | } | |
2097 | CBREG(transfer, trans_ac, transferchattr, NULL, NULL); | |
2098 | CBREG(transfer, trans_p, transferprog, NULL, NULL); | |
2099 | CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL); | |
2100 | return(0); | |
2101 | } | |
2102 | ||
2103 | static struct uiuser *newuser(wchar_t *name, unsigned long perms) | |
2104 | { | |
2105 | struct uiuser *new; | |
2106 | ||
2107 | new = smalloc(sizeof(*new)); | |
2108 | new->used = 0; | |
2109 | new->name = swcsdup(name); | |
2110 | new->perms = perms; | |
2111 | new->delete = 0; | |
2112 | new->next = users; | |
2113 | new->prev = NULL; | |
2114 | if(users != NULL) | |
2115 | users->prev = new; | |
2116 | users = new; | |
2117 | return(new); | |
2118 | } | |
2119 | ||
2120 | static void freeuser(struct uiuser *user) | |
2121 | { | |
2122 | if(user->next != NULL) | |
2123 | user->next->prev = user->prev; | |
2124 | if(user->prev != NULL) | |
2125 | user->prev->next = user->next; | |
2126 | if(user == users) | |
2127 | users = user->next; | |
2128 | free(user->name); | |
2129 | free(user); | |
2130 | } | |
2131 | ||
2132 | static int conf_user(int argc, wchar_t **argv) | |
2133 | { | |
2134 | int i, perms, permmod; | |
2135 | struct uiuser *user; | |
2136 | wchar_t *p; | |
2137 | ||
2138 | if(argc < 3) | |
2139 | { | |
2140 | flog(LOG_WARNING, "not enough arguments given for user command"); | |
2141 | return(1); | |
2142 | } | |
2143 | perms = 0; | |
2144 | for(i = 2; i < argc; i++) | |
2145 | { | |
2146 | if(!iswalpha(argv[i][0])) | |
2147 | p = argv[i] + 1; | |
2148 | else | |
2149 | p = argv[i]; | |
2150 | if(!wcscmp(p, L"disallow")) | |
2151 | permmod = PERM_DISALLOW; | |
2152 | if(!wcscmp(p, L"admin")) | |
2153 | permmod = PERM_ADMIN; | |
2154 | if(!wcscmp(p, L"fnetctl")) | |
2155 | permmod = PERM_FNETCTL; | |
2156 | if(!wcscmp(p, L"trans")) | |
2157 | permmod = PERM_TRANS; | |
2158 | if(!wcscmp(p, L"transcu")) | |
2159 | permmod = PERM_TRANSCU; | |
2160 | if(!wcscmp(p, L"chat")) | |
2161 | permmod = PERM_CHAT; | |
2162 | if(!wcscmp(p, L"srch")) | |
2163 | permmod = PERM_SRCH; | |
2164 | if(!wcscmp(p, L"all")) | |
2165 | permmod = ~0; | |
2166 | if(argv[i][0] == L'-') | |
2167 | perms &= ~permmod; | |
2168 | else | |
2169 | perms |= permmod; | |
2170 | } | |
2171 | if((user = finduser(argv[1])) == NULL) | |
2172 | { | |
2173 | newuser(argv[1], perms); | |
2174 | } else { | |
2175 | user->delete = 0; | |
2176 | user->perms = perms; | |
2177 | } | |
2178 | return(0); | |
2179 | } | |
2180 | ||
2181 | static void preinit(int hup) | |
2182 | { | |
2183 | struct uiuser *user; | |
2184 | ||
2185 | if(!hup) | |
2186 | { | |
8754fb85 | 2187 | newuser(L"default", PERM_DISALLOW); |
d3372da9 | 2188 | } else { |
2189 | for(user = users; user != NULL; user = user->next) | |
2190 | { | |
2191 | if(!wcscmp(user->name, L"default")) | |
2192 | user->delete = 1; | |
2193 | } | |
2194 | } | |
2195 | } | |
2196 | ||
ddb933f1 | 2197 | static struct sockaddr_un *makeunixname(void) |
2198 | { | |
2199 | static struct sockaddr_un buf; | |
2200 | char *val; | |
2201 | struct passwd *pwd; | |
2202 | uid_t uid; | |
2203 | ||
2204 | memset(&buf, 0, sizeof(buf)); | |
2205 | buf.sun_family = PF_UNIX; | |
2206 | if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) { | |
2207 | flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno)); | |
2208 | return(NULL); | |
2209 | } | |
2210 | if(!strcmp(val, "none")) | |
2211 | return(NULL); | |
2212 | if(!strcmp(val, "default")) | |
2213 | { | |
2214 | if((uid = getuid()) == 0) | |
2215 | { | |
2216 | strcpy(buf.sun_path, "/var/run/doldacond.sock"); | |
2217 | return(&buf); | |
2218 | } else { | |
2219 | if((pwd = getpwuid(uid)) == NULL) | |
2220 | { | |
2221 | flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno)); | |
2222 | return(NULL); | |
2223 | } | |
2224 | strcpy(buf.sun_path, "/tmp/doldacond-"); | |
2225 | strcat(buf.sun_path, pwd->pw_name); | |
2226 | return(&buf); | |
2227 | } | |
2228 | } | |
2229 | if(strchr(val, '/')) | |
2230 | { | |
2231 | strcpy(buf.sun_path, val); | |
2232 | return(&buf); | |
2233 | } | |
2234 | flog(LOG_WARNING, "invalid Unix socket name: %s", val); | |
2235 | return(NULL); | |
2236 | } | |
2237 | ||
2238 | static int tcpportupdate(struct configvar *var, void *uudata) | |
d3372da9 | 2239 | { |
d1ef0200 | 2240 | struct lport *newsock; |
d3372da9 | 2241 | |
ddb933f1 | 2242 | newsock = NULL; |
2243 | if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL)) | |
d3372da9 | 2244 | { |
ddb933f1 | 2245 | flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno)); |
d3372da9 | 2246 | return(0); |
2247 | } | |
ddb933f1 | 2248 | if(tcpsocket != NULL) |
2249 | { | |
d1ef0200 | 2250 | closelport(tcpsocket); |
ddb933f1 | 2251 | tcpsocket = NULL; |
2252 | } | |
2253 | tcpsocket = newsock; | |
2254 | return(0); | |
2255 | } | |
2256 | ||
2257 | static int unixsockupdate(struct configvar *var, void *uudata) | |
2258 | { | |
d1ef0200 | 2259 | struct lport *newsock; |
ddb933f1 | 2260 | struct sockaddr_un *un; |
4cb04c42 | 2261 | mode_t ou; |
ddb933f1 | 2262 | |
2263 | newsock = NULL; | |
4cb04c42 | 2264 | ou = umask(0111); |
ddb933f1 | 2265 | if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) |
2266 | { | |
4cb04c42 | 2267 | umask(ou); |
ddb933f1 | 2268 | flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno)); |
2269 | return(0); | |
2270 | } | |
4cb04c42 | 2271 | umask(ou); |
ddb933f1 | 2272 | if(unixsocket != NULL) |
2273 | { | |
d1ef0200 | 2274 | closelport(unixsocket); |
ddb933f1 | 2275 | unixsocket = NULL; |
2276 | } | |
2277 | unixsocket = newsock; | |
d3372da9 | 2278 | return(0); |
2279 | } | |
2280 | ||
2281 | static int init(int hup) | |
2282 | { | |
d3372da9 | 2283 | struct uiuser *user, *next; |
ddb933f1 | 2284 | struct sockaddr_un *un; |
8754fb85 | 2285 | struct passwd *pwd; |
2286 | wchar_t *wcsname; | |
4cb04c42 | 2287 | mode_t ou; |
d3372da9 | 2288 | |
2289 | if(hup) | |
2290 | { | |
2291 | for(user = users; user != NULL; user = next) | |
2292 | { | |
2293 | next = user->next; | |
2294 | if(user->delete) | |
2295 | freeuser(user); | |
2296 | } | |
2297 | } | |
2298 | if(!hup) | |
2299 | { | |
9a64a4f7 | 2300 | starttime = time(NULL); |
ddb933f1 | 2301 | if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL)) |
2302 | { | |
2303 | flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno)); | |
2304 | return(1); | |
2305 | } | |
2306 | CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL); | |
4cb04c42 | 2307 | ou = umask(0111); |
ddb933f1 | 2308 | if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) |
d3372da9 | 2309 | { |
4cb04c42 | 2310 | umask(ou); |
ddb933f1 | 2311 | flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno)); |
d3372da9 | 2312 | return(1); |
2313 | } | |
4cb04c42 | 2314 | umask(ou); |
ddb933f1 | 2315 | CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL); |
d3372da9 | 2316 | GCBREG(newfncb, newfnetnode, NULL); |
2317 | GCBREG(newtransfercb, newtransfernotify, NULL); | |
2318 | } | |
8754fb85 | 2319 | if(getuid() != 0) |
2320 | { | |
2321 | for(user = users; user != NULL; user = user->next) | |
2322 | { | |
2323 | if(wcscmp(user->name, L"default")) | |
2324 | break; | |
2325 | } | |
2326 | if(!user) | |
2327 | { | |
2328 | if((pwd = getpwuid(getuid())) == NULL) | |
2329 | { | |
2330 | flog(LOG_CRIT, "could not get login info: %s", strerror(errno)); | |
2331 | return(1); | |
2332 | } | |
2333 | if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL) | |
2334 | { | |
2335 | flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno)); | |
2336 | return(1); | |
2337 | } | |
2338 | newuser(wcsname, ~PERM_DISALLOW); | |
2339 | free(wcsname); | |
2340 | } | |
2341 | } | |
d3372da9 | 2342 | return(0); |
2343 | } | |
2344 | ||
2345 | static int run(void) | |
2346 | { | |
2347 | int i, id; | |
2348 | struct uidata *data, *next; | |
2349 | struct qcommand *qcmd; | |
2350 | struct notif *notif, *nnotif; | |
d3372da9 | 2351 | |
2352 | for(data = actives; data != NULL; data = next) | |
2353 | { | |
2354 | next = data->next; | |
2355 | if(data->close) | |
2356 | freeuidata(data); | |
2357 | } | |
2358 | for(data = actives; data != NULL; data = data->next) | |
2359 | { | |
2360 | for(notif = data->fnotif; notif != NULL; notif = nnotif) | |
2361 | { | |
2362 | nnotif = notif->next; | |
2363 | if(notif->state == NOTIF_WAIT) | |
2364 | continue; | |
2365 | id = -1; | |
2366 | for(i = 0; i < notif->argc; i++) | |
2367 | { | |
2368 | if(notif->argv[i].dt == NOTIF_ID) | |
2369 | { | |
2370 | id = notif->argv[i].d.n; | |
2371 | break; | |
2372 | } | |
2373 | } | |
2374 | if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL) | |
2375 | continue; | |
f7292a8b | 2376 | sq(data->sk, 2, L"%i", notif->code, NULL); |
d3372da9 | 2377 | for(i = 0; i < notif->argc; i++) |
2378 | { | |
2379 | switch(notif->argv[i].dt) | |
2380 | { | |
2381 | case NOTIF_INT: | |
2382 | case NOTIF_ID: | |
f7292a8b | 2383 | sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL); |
d3372da9 | 2384 | break; |
dcf7a1a2 FT |
2385 | case NOTIF_OFF: |
2386 | sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL); | |
2387 | break; | |
d3372da9 | 2388 | case NOTIF_STR: |
2389 | if(notif->argv[i].d.s[0] == L'%') | |
f7292a8b | 2390 | sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL); |
d3372da9 | 2391 | else |
2392 | sq(data->sk, 2, notif->argv[i].d.s, NULL); | |
2393 | break; | |
2394 | case NOTIF_FLOAT: | |
f7292a8b | 2395 | sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL); |
d3372da9 | 2396 | break; |
2397 | } | |
2398 | } | |
2399 | sq(data->sk, 0, NULL); | |
2400 | if(notif->rlimit != 0) | |
2401 | { | |
2402 | notif->state = NOTIF_WAIT; | |
2403 | notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif); | |
2404 | } else { | |
2405 | freenotif(notif); | |
2406 | } | |
2407 | } | |
2408 | if((qcmd = unlinkqcmd(data)) != NULL) | |
2409 | { | |
2410 | qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv); | |
2411 | freequeuecmd(qcmd); | |
2412 | return(1); | |
2413 | } | |
821e3843 FT |
2414 | if(data->queuesize > 10) |
2415 | { | |
2416 | /* Clients should not be queue up commands at all, since | |
2417 | * they should not send a new command before receiving a | |
2418 | * reply to the previous command. Therefore, we | |
2419 | * mercilessly massacre clients which are stacking up too | |
2420 | * many commands. */ | |
2421 | data->close = 1; | |
2422 | } | |
d3372da9 | 2423 | } |
2424 | return(0); | |
2425 | } | |
2426 | ||
2427 | static void terminate(void) | |
2428 | { | |
2429 | while(users != NULL) | |
2430 | freeuser(users); | |
ddb933f1 | 2431 | if(tcpsocket != NULL) |
d1ef0200 | 2432 | closelport(tcpsocket); |
ddb933f1 | 2433 | if(unixsocket != NULL) |
d1ef0200 | 2434 | closelport(unixsocket); |
d3372da9 | 2435 | } |
2436 | ||
2437 | static struct configvar myvars[] = | |
2438 | { | |
d9f89ef5 | 2439 | /** If true, UI connections will only be accepted from localhost |
2440 | * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are | |
2441 | * completely sure that you know what you are doing, never turn | |
2442 | * this off when auth.authless is on. */ | |
d3372da9 | 2443 | {CONF_VAR_BOOL, "onlylocal", {.num = 1}}, |
ddb933f1 | 2444 | /** The TCP port number on which to accept UI client connections, |
2445 | * or -1 to not listen on TCP. */ | |
d3372da9 | 2446 | {CONF_VAR_INT, "port", {.num = 1500}}, |
ddb933f1 | 2447 | /** |
2448 | * Controls the the name to use for the Unix socket on which to | |
2449 | * accept UI client connections. If the name contains a slash, it | |
2450 | * is treated as a file name to bind on. If the name is "default", | |
2451 | * the file name will be "/var/run/doldacond.sock" if doldacond | |
2452 | * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where | |
2453 | * NAME is the user name of the UID which doldacond runs as. If | |
2454 | * the name is "none", no Unix socket will be used. Otherwise, an | |
2455 | * error is signaled. | |
2456 | */ | |
2457 | {CONF_VAR_STRING, "unixsock", {.str = L"default"}}, | |
d9f89ef5 | 2458 | /** The TOS value to use for UI connections (see the TOS VALUES |
2459 | * section). */ | |
d3372da9 | 2460 | {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}}, |
d9f89ef5 | 2461 | /** The name of the filtercmd script (see the FILES section for |
2462 | * lookup information). */ | |
d3372da9 | 2463 | {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}}, |
2464 | {CONF_VAR_END} | |
2465 | }; | |
2466 | ||
2467 | static struct configcmd mycmds[] = | |
2468 | { | |
2469 | {"user", conf_user}, | |
2470 | {NULL} | |
2471 | }; | |
2472 | ||
2473 | static struct module me = | |
2474 | { | |
2475 | .name = "ui", | |
2476 | .conf = | |
2477 | { | |
2478 | .vars = myvars, | |
2479 | .cmds = mycmds | |
2480 | }, | |
2481 | .preinit = preinit, | |
2482 | .init = init, | |
2483 | .run = run, | |
2484 | .terminate = terminate | |
2485 | }; | |
2486 | ||
2487 | MODULE(me) |