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