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