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