Allow setting SO_REUSEADDR.
[doldaconnect.git] / daemon / ui.c
... / ...
CommitLineData
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 {
743 if(!wcscmp(argv[i], L"hash"))
744 {
745 transfer->hash = parsehash(argv[i + 1]);
746 } else {
747 transferaddarg(transfer, argv[i], argv[i + 1]);
748 }
749 }
750 }
751 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
752 transfersetactivity(transfer, L"create");
753}
754
755static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
756{
757 struct transfer *transfer, *pt;
758
759 havepriv(PERM_TRANS);
760 pt = NULL;
761 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
762 {
763 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
764 {
765 if(pt != NULL)
766 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
767 L"%%i", pt->state, pt->peerid,
768 (pt->peernick == NULL)?L"":(pt->peernick),
769 (pt->path == NULL)?L"":(pt->path),
770 L"%%i", pt->size, L"%%i", pt->curpos,
771 (pt->hash == NULL)?L"":unparsehash(pt->hash),
772 NULL);
773 pt = transfer;
774 }
775 }
776 if(pt == NULL)
777 sq(sk, 0, L"201", L"No transfers", NULL);
778 else
779 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
780 L"%%i", pt->state, pt->peerid,
781 (pt->peernick == NULL)?L"":(pt->peernick),
782 (pt->path == NULL)?L"":(pt->path),
783 L"%%i", pt->size, L"%%i", pt->curpos,
784 (pt->hash == NULL)?L"":unparsehash(pt->hash),
785 NULL);
786}
787
788static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
789{
790 struct transfer *transfer;
791
792 haveargs(2);
793 havepriv(PERM_TRANS);
794 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
795 {
796 sq(sk, 0, L"512", L"No such transfer", NULL);
797 return;
798 }
799 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
800 {
801 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
802 return;
803 }
804 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
805 {
806 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
807 return;
808 }
809 transfer->close = 1;
810 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
811}
812
813static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
814{
815 int i, val;
816
817 if((argc % 2) != 1)
818 {
819 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
820 return;
821 }
822 for(i = 1; i < argc; i += 2)
823 {
824 if(!wcscasecmp(argv[i + 1], L"on"))
825 val = 1;
826 else
827 val = 0;
828 if(!wcscasecmp(argv[i], L"all"))
829 {
830 if(val)
831 data->notify.w = ~0;
832 else
833 data->notify.w = 0;
834 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
835 data->notify.b.fnchat = val;
836 } else if(!wcscasecmp(argv[i], L"fn:act")) {
837 data->notify.b.fnact = val;
838 } else if(!wcscasecmp(argv[i], L"trans:act")) {
839 data->notify.b.tract = val;
840 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
841 data->notify.b.trprog = val;
842 } else if(!wcscasecmp(argv[i], L"srch:act")) {
843 data->notify.b.srch = val;
844 }
845 }
846 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
847}
848
849static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
850{
851 struct fnetnode *fn;
852 int public;
853
854 haveargs(5);
855 havepriv(PERM_CHAT);
856 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
857 {
858 sq(sk, 0, L"510", L"No such node", NULL);
859 return;
860 }
861 public = wcstol(argv[2], NULL, 0);
862 if((public != 0) && (public != 1))
863 {
864 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
865 return;
866 }
867 if(fn->state != FNN_EST)
868 {
869 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
870 return;
871 }
872 if(fnetsendchat(fn, public, argv[3], argv[4]))
873 {
874 if(errno == ENOTSUP)
875 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
876 else if(errno == EPERM)
877 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
878 else if(errno == EILSEQ)
879 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
880 else
881 sq(sk, 0, L"505", L"Could not chat", NULL);
882 return;
883 }
884 sq(sk, 0, L"200", L"Chat string sent", NULL);
885}
886
887static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
888{
889 struct search *srch;
890 struct fnetnode *fn;
891 struct sexpr *sexpr;
892 int i;
893
894 haveargs(3);
895 havepriv(PERM_SRCH);
896 srch = newsearch(data->username, NULL);
897 for(i = 1; i < argc; i++)
898 {
899 if(!wcscmp(argv[i], L"all"))
900 {
901 for(fn = fnetnodes; fn != NULL; fn = fn->next)
902 {
903 if(fn->state == FNN_EST)
904 searchaddfn(srch, fn);
905 }
906 i++;
907 break;
908 } else if(!wcscmp(argv[i], L"prio")) {
909 if(++i == argc)
910 {
911 sq(sk, 0, L"501", L"No argument to prio", NULL);
912 freesearch(srch);
913 return;
914 }
915 srch->prio = wcstol(argv[i], NULL, 0);
916 } else if(iswdigit(*argv[i])) {
917 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
918 {
919 sq(sk, 0, L"510", L"No such node", NULL);
920 freesearch(srch);
921 return;
922 }
923 searchaddfn(srch, fn);
924 } else {
925 break;
926 }
927 }
928 if(srch->fnl == NULL)
929 {
930 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
931 freesearch(srch);
932 return;
933 }
934 if(i == argc)
935 {
936 sq(sk, 0, L"501", L"No search expression found on line", NULL);
937 freesearch(srch);
938 return;
939 }
940 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
941 {
942 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
943 freesearch(srch);
944 return;
945 }
946 optsexpr(sexpr);
947 getsexpr(srch->sexpr = sexpr);
948 queuesearch(srch);
949 CBREG(srch, search_eta, srcheta, NULL, NULL);
950 CBREG(srch, search_commit, srchcommit, NULL, NULL);
951 CBREG(srch, search_result, srchres, NULL, NULL);
952 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
953 putsexpr(sexpr);
954}
955
956static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
957{
958 struct search *srch, *pt;
959 time_t now;
960
961 havepriv(PERM_SRCH);
962 pt = NULL;
963 now = time(NULL);
964 for(srch = searches; srch != NULL; srch = srch->next)
965 {
966 if(!wcscmp(srch->owner, data->username))
967 {
968 if(pt != NULL)
969 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
970 pt = srch;
971 }
972 }
973 if(pt == NULL)
974 sq(sk, 0, L"201", L"No searches", NULL);
975 else
976 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
977}
978
979static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
980{
981 struct search *srch;
982 struct srchres *sr;
983 wchar_t buf[64];
984
985 haveargs(2);
986 havepriv(PERM_SRCH);
987 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
988 {
989 sq(sk, 0, L"514", L"No such search", NULL);
990 return;
991 }
992 if(srch->results == NULL)
993 {
994 sq(sk, 0, L"201", L"No results", NULL);
995 } else {
996 for(sr = srch->results; sr != NULL; sr = sr->next)
997 {
998 swprintf(buf, 64, L"%f", sr->time);
999 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);
1000 }
1001 }
1002}
1003
1004static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1005{
1006 struct search *srch;
1007 int i;
1008
1009 haveargs(2);
1010 havepriv(PERM_SRCH);
1011 /* Note - Programmatical user interfaces must only give one
1012 * argument per command, the multiple argument form is only for
1013 * convenience when manually controlling the daemon via
1014 * eg. telnet. The reason is that the return codes aren't clear
1015 * enough for the multiple argument form. */
1016 for(i = 1; i < argc; i++)
1017 {
1018 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1019 {
1020 sq(sk, 0, L"514", L"No such search", NULL);
1021 return;
1022 }
1023 freesearch(srch);
1024 }
1025 sq(sk, 0, L"200", L"Search cancelled", NULL);
1026}
1027
1028static void fcmdread(struct socket *sk, struct uidata *data)
1029{
1030 char *buf;
1031 size_t bufsize;
1032
1033 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1034 return;
1035 bufcat(data->fcmdbuf, buf, bufsize);
1036 free(buf);
1037}
1038
1039static void fcmderr(struct socket *sk, int err, struct uidata *data)
1040{
1041 wchar_t *wbuf, *p, *p2;
1042
1043 if(err)
1044 {
1045 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1046 kill(-data->fcmdpid, SIGHUP);
1047 putsock(data->fcmdsk);
1048 data->fcmdsk = NULL;
1049 if(data->fcmdbuf != NULL)
1050 {
1051 free(data->fcmdbuf);
1052 data->fcmdbuf = NULL;
1053 }
1054 data->fcmdbufsize = data->fcmdbufdata = 0;
1055 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1056 return;
1057 }
1058 putsock(data->fcmdsk);
1059 data->fcmdsk = NULL;
1060 data->fcmdpid = 0;
1061 if(data->fcmdbuf == NULL)
1062 {
1063 wbuf = swcsdup(L"");
1064 } else {
1065 addtobuf(data->fcmdbuf, 0);
1066 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1067 free(data->fcmdbuf);
1068 }
1069 data->fcmdbuf = NULL;
1070 data->fcmdbufsize = data->fcmdbufdata = 0;
1071 if(wbuf == NULL)
1072 {
1073 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1074 return;
1075 }
1076 p = wbuf;
1077 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1078 {
1079 *(p2++) = L'\0';
1080 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1081 p = p2;
1082 }
1083 if(*p == L'\0')
1084 {
1085 if(p == wbuf)
1086 sq(data->sk, 0, L"201", L"No data returned", NULL);
1087 } else {
1088 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1089 }
1090 free(wbuf);
1091}
1092
1093static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1094{
1095 int i;
1096 pid_t pid;
1097 int pipe;
1098 char **cargv, **pp;
1099 char *filtercmd, *argbuf;
1100 size_t cargvsize, cargvdata;
1101 struct passwd *pwent;
1102
1103 haveargs(2);
1104 havepriv(PERM_TRANS);
1105 if((pwent = getpwuid(data->uid)) == NULL)
1106 {
1107 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1108 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1109 return;
1110 }
1111 if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir)) == NULL)
1112 {
1113 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1114 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1115 return;
1116 }
1117 cargv = NULL;
1118 cargvsize = cargvdata = 0;
1119 addtobuf(cargv, filtercmd);
1120 for(i = 1; i < argc; i++)
1121 {
1122 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1123 {
1124 for(i = 0; i < cargvdata; i++)
1125 free(cargv[i]);
1126 free(cargv);
1127 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1128 return;
1129 }
1130 addtobuf(cargv, argbuf);
1131 }
1132 addtobuf(cargv, NULL);
1133 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)
1134 {
1135 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1136 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1137 return;
1138 }
1139 if(pid == 0)
1140 {
1141 execv(filtercmd, cargv);
1142 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1143 exit(127);
1144 }
1145 for(pp = cargv; *pp; pp++)
1146 free(*pp);
1147 free(cargv);
1148 data->fcmdsk = wrapsock(pipe);
1149 data->fcmdpid = pid;
1150 if(data->fcmdbuf != NULL)
1151 {
1152 free(data->fcmdbuf);
1153 data->fcmdbuf = NULL;
1154 }
1155 data->fcmdbufsize = data->fcmdbufdata = 0;
1156 data->fcmdsk->data = data;
1157 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1158 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1159}
1160
1161static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1162{
1163 struct transfer *transfer;
1164 struct transarg *ta;
1165
1166 haveargs(2);
1167 havepriv(PERM_TRANS);
1168 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1169 {
1170 sq(sk, 0, L"512", L"No such transfer", NULL);
1171 return;
1172 }
1173 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1174 {
1175 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1176 return;
1177 }
1178 if(transfer->args == NULL)
1179 {
1180 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1181 } else {
1182 for(ta = transfer->args; ta != NULL; ta = ta->next)
1183 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->rec, L"%%ls", ta->val, NULL);
1184 }
1185}
1186
1187static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1188{
1189 struct sharecache *node;
1190 int total, hashed;
1191
1192 total = hashed = 0;
1193 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1194 {
1195 if(node->f.b.type == FILE_REG)
1196 {
1197 total++;
1198 if(node->f.b.hastth)
1199 hashed++;
1200 }
1201 }
1202 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1203}
1204
1205#undef haveargs
1206#undef havepriv
1207
1208/*
1209 * Reserved command numbers for nameless commands:
1210 * 0: Issued when a client has connected
1211 * 1: Issued when a named command couldn't be found
1212 */
1213
1214static struct command commands[] =
1215{
1216 {NULL, cmd_connect},
1217 {NULL, cmd_notfound},
1218 {L"shutdown", cmd_shutdown},
1219 {L"quit", cmd_quit},
1220 {L"lsauth", cmd_lsauth},
1221 {L"login", cmd_login},
1222 {L"pass", cmd_pass},
1223 {L"cnct", cmd_fnetconnect},
1224 {L"lsnodes", cmd_lsnodes},
1225 {L"dcnct", cmd_disconnect},
1226 {L"lspa", cmd_lspa},
1227 {L"lspeers", cmd_lspeers},
1228 {L"download", cmd_download},
1229 {L"lstrans", cmd_lstrans},
1230 {L"cancel", cmd_cancel},
1231 {L"notify", cmd_notify},
1232 {L"sendchat", cmd_sendchat},
1233 {L"search", cmd_search},
1234 {L"lssrch", cmd_lssrch},
1235 {L"lssr", cmd_lssr},
1236 {L"cansrch", cmd_cansrch},
1237 {L"filtercmd", cmd_filtercmd},
1238 {L"lstrarg", cmd_lstrarg},
1239 {L"hashstatus", cmd_hashstatus},
1240 {NULL, NULL}
1241};
1242
1243static void freequeuecmd(struct qcommand *qcmd)
1244{
1245 int i;
1246
1247 if(qcmd->argv != NULL)
1248 {
1249 for(i = 0; i < qcmd->argc; i++)
1250 free(qcmd->argv[i]);
1251 free(qcmd->argv);
1252 }
1253 free(qcmd);
1254}
1255
1256static struct qcommand *unlinkqcmd(struct uidata *data)
1257{
1258 struct qcommand *qcmd;
1259
1260 qcmd = data->queue;
1261 if(qcmd != NULL)
1262 {
1263 data->queue = qcmd->next;
1264 if(qcmd == data->queuelast)
1265 data->queuelast = qcmd->next;
1266 }
1267 return(qcmd);
1268}
1269
1270static struct notif *newnotif(struct uidata *data, int code, ...)
1271{
1272 struct notif *notif;
1273 va_list args;
1274 int dt, ca;
1275
1276 notif = smalloc(sizeof(*notif));
1277 memset(notif, 0, sizeof(*notif));
1278 notif->rlimit = 0.0;
1279 notif->ui = data;
1280 notif->code = code;
1281 va_start(args, code);
1282 while((dt = va_arg(args, int)) != NOTIF_END)
1283 {
1284 ca = notif->argc;
1285 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1286 notif->argv[ca].dt = dt;
1287 switch(dt)
1288 {
1289 case NOTIF_INT:
1290 case NOTIF_ID:
1291 notif->argv[ca].d.n = va_arg(args, int);
1292 break;
1293 case NOTIF_STR:
1294 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1295 break;
1296 case NOTIF_FLOAT:
1297 notif->argv[ca].d.d = va_arg(args, double);
1298 break;
1299 }
1300 }
1301 va_end(args);
1302 notif->next = NULL;
1303 notif->prev = data->lnotif;
1304 if(data->lnotif != NULL)
1305 data->lnotif->next = notif;
1306 else
1307 data->fnotif = notif;
1308 data->lnotif = notif;
1309 return(notif);
1310}
1311
1312static void freenotif(struct notif *notif)
1313{
1314 int i;
1315
1316 if(notif->next != NULL)
1317 notif->next->prev = notif->prev;
1318 if(notif->prev != NULL)
1319 notif->prev->next = notif->next;
1320 if(notif == notif->ui->fnotif)
1321 notif->ui->fnotif = notif->next;
1322 if(notif == notif->ui->lnotif)
1323 notif->ui->lnotif = notif->prev;
1324 if(notif->exptimer != NULL)
1325 canceltimer(notif->exptimer);
1326 for(i = 0; i < notif->argc; i++)
1327 {
1328 if(notif->argv[i].dt == NOTIF_STR)
1329 free(notif->argv[i].d.s);
1330 }
1331 if(notif->argv != NULL)
1332 free(notif->argv);
1333 free(notif);
1334}
1335
1336static void notifexpire(int cancelled, struct notif *notif)
1337{
1338 notif->exptimer = NULL;
1339 if(!cancelled)
1340 freenotif(notif);
1341}
1342
1343static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1344{
1345 int i, cont;
1346
1347 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1348 {
1349 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1350 {
1351 cont = 0;
1352 if(id >= 0)
1353 {
1354 for(i = 0; i < notif->argc; i++)
1355 {
1356 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1357 {
1358 cont = 1;
1359 break;
1360 }
1361 }
1362 }
1363 if(cont)
1364 continue;
1365 break;
1366 }
1367 }
1368 return(notif);
1369}
1370
1371static void freeuidata(struct uidata *data)
1372{
1373 int i;
1374 struct qcommand *qcmd;
1375
1376 if(data->next != NULL)
1377 data->next->prev = data->prev;
1378 if(data->prev != NULL)
1379 data->prev->next = data->next;
1380 if(data == actives)
1381 actives = data->next;
1382 data->sk->readcb = NULL;
1383 data->sk->errcb = NULL;
1384 putsock(data->sk);
1385 while((qcmd = unlinkqcmd(data)) != NULL)
1386 freequeuecmd(qcmd);
1387 iconv_close(data->ichandle);
1388 if(data->cw != NULL)
1389 free(data->cw);
1390 if(data->cb != NULL)
1391 free(data->cb);
1392 if(data->argv != NULL)
1393 {
1394 for(i = 0; i < data->argc; i++)
1395 free(data->argv[i]);
1396 free(data->argv);
1397 }
1398 if(data->auth != NULL)
1399 authputhandle(data->auth);
1400 if(data->username != NULL)
1401 {
1402 if(data->userinfo != NULL)
1403 flog(LOG_INFO, "%ls logged out", data->username);
1404 free(data->username);
1405 }
1406 free(data->inbuf);
1407 while(data->fnotif != NULL)
1408 freenotif(data->fnotif);
1409 if(data->fcmdbuf != NULL)
1410 free(data->fcmdbuf);
1411 if(data->fcmdpid != 0)
1412 kill(-data->fcmdpid, SIGHUP);
1413 if(data->fcmdsk != NULL)
1414 putsock(data->fcmdsk);
1415 free(data);
1416}
1417
1418static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1419{
1420 struct qcommand *new;
1421
1422 new = smalloc(sizeof(*new));
1423 new->cmd = cmd;
1424 new->argc = argc;
1425 new->argv = argv;
1426 new->next = NULL;
1427 if(data->queuelast != NULL)
1428 data->queuelast->next = new;
1429 data->queuelast = new;
1430 if(data->queue == NULL)
1431 data->queue = new;
1432}
1433
1434static struct uidata *newuidata(struct socket *sk)
1435{
1436 struct uidata *data;
1437
1438 data = smalloc(sizeof(*data));
1439 memset(data, 0, sizeof(*data));
1440 data->sk = sk;
1441 getsock(sk);
1442 data->inbuf = smalloc(1024);
1443 data->uid = -1;
1444 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1445 {
1446 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1447 return(NULL);
1448 }
1449 data->next = actives;
1450 data->prev = NULL;
1451 if(actives != NULL)
1452 actives->prev = data;
1453 actives = data;
1454 return(data);
1455}
1456
1457static void uiread(struct socket *sk, struct uidata *data)
1458{
1459 int ret, done;
1460 char *newbuf;
1461 char *p1, *p2;
1462 wchar_t *porig;
1463 size_t datalen, len2;
1464 struct command *cur;
1465
1466 if(data->indata > 1024)
1467 data->indata = 0;
1468 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1469 return;
1470 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1471 memcpy(data->inbuf + data->indata, newbuf, datalen);
1472 free(newbuf);
1473 data->indata += datalen;
1474 if(data->cb == NULL)
1475 {
1476 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1477 data->cbdata = 0;
1478 data->pp = data->cb;
1479 }
1480 done = 0;
1481 while(!done)
1482 {
1483 if(data->cbsize == data->cbdata)
1484 {
1485 len2 = data->pp - data->cb;
1486 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1487 data->pp = data->cb + len2;
1488 }
1489 p1 = data->inbuf;
1490 p2 = (char *)(porig = (data->cb + data->cbdata));
1491 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1492 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1493 memmove(data->inbuf, p1, data->indata);
1494 /* Just a sanity check */
1495 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1496 {
1497 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1498 abort();
1499 }
1500 data->cbdata += (((wchar_t *)p2) - porig);
1501 if(ret < 0)
1502 {
1503 switch(errno)
1504 {
1505 case EILSEQ:
1506 /* XXX: Should this really just ignore it? */
1507 data->indata = 0;
1508 done = 1;
1509 break;
1510 case EINVAL:
1511 done = 1;
1512 break;
1513 case E2BIG:
1514 /* Just a sanity check */
1515 if(data->cbsize != data->cbdata)
1516 {
1517 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1518 abort();
1519 }
1520 break;
1521 default:
1522 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1523 break;
1524 }
1525 } else {
1526 done = 1;
1527 }
1528 }
1529 done = 0;
1530 while(!done && (data->pp - data->cb < data->cbdata))
1531 {
1532 switch(data->ps)
1533 {
1534 case 0:
1535 if(iswspace(*data->pp))
1536 {
1537 if(*data->pp == L'\r')
1538 {
1539 if(data->pp == data->cb + data->cbdata - 1)
1540 {
1541 done = 1;
1542 break;
1543 }
1544 if(*(++data->pp) == L'\n')
1545 {
1546 if((data->argv != NULL) && (data->argv[0] != NULL))
1547 {
1548 for(cur = commands; cur->handler != NULL; cur++)
1549 {
1550 if(cur->name == NULL)
1551 continue;
1552 if(!wcscasecmp(cur->name, data->argv[0]))
1553 {
1554 queuecmd(data, cur, data->argc, data->argv);
1555 break;
1556 }
1557 }
1558 if(cur->handler == NULL)
1559 queuecmd(data, &commands[1], data->argc, data->argv);
1560 } else {
1561 queuecmd(data, &commands[1], data->argc, data->argv);
1562 }
1563 data->argv = NULL;
1564 data->args = 0;
1565 data->argc = 0;
1566 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1567 data->pp = data->cb;
1568 } else {
1569 data->pp++;
1570 }
1571 } else {
1572 data->pp++;
1573 }
1574 } else {
1575 data->ps = 1;
1576 data->cwdata = 0;
1577 }
1578 break;
1579 case 1:
1580 if(iswspace(*data->pp))
1581 {
1582 addtobuf(data->cw, L'\0');
1583 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1584 data->argv[data->argc++] = data->cw;
1585 data->cw = NULL;
1586 data->cwsize = 0;
1587 data->cwdata = 0;
1588 data->ps = 0;
1589 } else if(*data->pp == L'\"') {
1590 data->ps = 2;
1591 data->pp++;
1592 } else if(*data->pp == L'\\') {
1593 if(data->pp == data->cb + data->cbdata - 1)
1594 {
1595 done = 1;
1596 break;
1597 }
1598 addtobuf(data->cw, *(++data->pp));
1599 data->pp++;
1600 } else {
1601 addtobuf(data->cw, *(data->pp++));
1602 }
1603 break;
1604 case 2:
1605 if(*data->pp == L'\"')
1606 {
1607 data->ps = 1;
1608 } else if(*data->pp == L'\\') {
1609 if(data->pp == data->cb + data->cbdata - 1)
1610 {
1611 done = 1;
1612 break;
1613 }
1614 addtobuf(data->cw, *(++(data->pp)));
1615 } else {
1616 addtobuf(data->cw, *data->pp);
1617 }
1618 data->pp++;
1619 break;
1620 }
1621 }
1622}
1623
1624static void uierror(struct socket *sk, int err, struct uidata *data)
1625{
1626 if(err)
1627 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1628 freeuidata(data);
1629}
1630
1631static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1632{
1633 struct uidata *uidata;
1634
1635 newsk->data = uidata = newuidata(newsk);
1636 socksettos(newsk, confgetint("ui", "uitos"));
1637 if(uidata == NULL)
1638 return;
1639 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1640 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1641 queuecmd(uidata, &commands[0], 0, NULL);
1642}
1643
1644static int srcheta(struct search *srch, void *uudata)
1645{
1646 struct uidata *data;
1647
1648 for(data = actives; data != NULL; data = data->next)
1649 {
1650 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1651 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1652 }
1653 return(0);
1654}
1655
1656static int srchcommit(struct search *srch, void *uudata)
1657{
1658 struct uidata *data;
1659
1660 for(data = actives; data != NULL; data = data->next)
1661 {
1662 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1663 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1664 }
1665 return(0);
1666}
1667
1668static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1669{
1670 struct uidata *data;
1671
1672 for(data = actives; data != NULL; data = data->next)
1673 {
1674 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1675 {
1676 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1677 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);
1678 }
1679 }
1680 return(0);
1681}
1682
1683static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1684{
1685 struct uidata *data;
1686
1687 for(data = actives; data != NULL; data = data->next)
1688 {
1689 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1690 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1691 }
1692 return(0);
1693}
1694
1695static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1696{
1697 struct uidata *data;
1698 struct notif *notif;
1699
1700 if(!wcscmp(attrib, L"state"))
1701 {
1702 for(data = actives; data != NULL; data = data->next)
1703 {
1704 if(data->notify.b.fnact)
1705 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1706 }
1707 } else if(!wcscmp(attrib, L"name")) {
1708 for(data = actives; data != NULL; data = data->next)
1709 {
1710 if(data->notify.b.fnact)
1711 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1712 }
1713 } else if(!wcscmp(attrib, L"numpeers")) {
1714 for(data = actives; data != NULL; data = data->next)
1715 {
1716 if(data->notify.b.fnact)
1717 {
1718 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1719 notif->argv[1].d.n = fn->numpeers;
1720 else
1721 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END);
1722 }
1723 }
1724 }
1725 return(0);
1726}
1727
1728static int fnunlink(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, 603, NOTIF_ID, fn->id, NOTIF_END);
1736 }
1737 return(0);
1738}
1739
1740static int newfnetnode(struct fnetnode *fn, void *uudata)
1741{
1742 struct uidata *data;
1743
1744 for(data = actives; data != NULL; data = data->next)
1745 {
1746 if(data->notify.b.fnact)
1747 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1748 }
1749 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1750 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1751 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1752 return(0);
1753}
1754
1755static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1756{
1757 struct uidata *data;
1758
1759 if(!wcscmp(attrib, L"state"))
1760 {
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, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1765 }
1766 } else if(!wcscmp(attrib, L"nick")) {
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, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1771 }
1772 } else if(!wcscmp(attrib, L"size")) {
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, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1777 }
1778 } else if(!wcscmp(attrib, L"error")) {
1779 for(data = actives; data != NULL; data = data->next)
1780 {
1781 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1782 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1783 }
1784 } else if(!wcscmp(attrib, L"path")) {
1785 for(data = actives; data != NULL; data = data->next)
1786 {
1787 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1788 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1789 }
1790 }
1791 return(0);
1792}
1793
1794static int transferprog(struct transfer *transfer, void *uudata)
1795{
1796 struct uidata *data;
1797 struct notif *notif;
1798
1799 for(data = actives; data != NULL; data = data->next)
1800 {
1801 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1802 {
1803 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
1804 notif->argv[1].d.n = transfer->curpos;
1805 else
1806 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
1807 }
1808 }
1809 return(0);
1810}
1811
1812static int transferdestroyed(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, 617, NOTIF_ID, transfer->id, NOTIF_END);
1820 }
1821 return(0);
1822}
1823
1824static int newtransfernotify(struct transfer *transfer, void *uudata)
1825{
1826 struct uidata *data;
1827
1828 for(data = actives; data != NULL; data = data->next)
1829 {
1830 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1831 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);
1832 }
1833 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
1834 CBREG(transfer, trans_p, transferprog, NULL, NULL);
1835 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
1836 return(0);
1837}
1838
1839static struct uiuser *newuser(wchar_t *name, unsigned long perms)
1840{
1841 struct uiuser *new;
1842
1843 new = smalloc(sizeof(*new));
1844 new->used = 0;
1845 new->name = swcsdup(name);
1846 new->perms = perms;
1847 new->delete = 0;
1848 new->next = users;
1849 new->prev = NULL;
1850 if(users != NULL)
1851 users->prev = new;
1852 users = new;
1853 return(new);
1854}
1855
1856static void freeuser(struct uiuser *user)
1857{
1858 if(user->next != NULL)
1859 user->next->prev = user->prev;
1860 if(user->prev != NULL)
1861 user->prev->next = user->next;
1862 if(user == users)
1863 users = user->next;
1864 free(user->name);
1865 free(user);
1866}
1867
1868static int conf_user(int argc, wchar_t **argv)
1869{
1870 int i, perms, permmod;
1871 struct uiuser *user;
1872 wchar_t *p;
1873
1874 if(argc < 3)
1875 {
1876 flog(LOG_WARNING, "not enough arguments given for user command");
1877 return(1);
1878 }
1879 perms = 0;
1880 for(i = 2; i < argc; i++)
1881 {
1882 if(!iswalpha(argv[i][0]))
1883 p = argv[i] + 1;
1884 else
1885 p = argv[i];
1886 if(!wcscmp(p, L"disallow"))
1887 permmod = PERM_DISALLOW;
1888 if(!wcscmp(p, L"admin"))
1889 permmod = PERM_ADMIN;
1890 if(!wcscmp(p, L"fnetctl"))
1891 permmod = PERM_FNETCTL;
1892 if(!wcscmp(p, L"trans"))
1893 permmod = PERM_TRANS;
1894 if(!wcscmp(p, L"transcu"))
1895 permmod = PERM_TRANSCU;
1896 if(!wcscmp(p, L"chat"))
1897 permmod = PERM_CHAT;
1898 if(!wcscmp(p, L"srch"))
1899 permmod = PERM_SRCH;
1900 if(!wcscmp(p, L"all"))
1901 permmod = ~0;
1902 if(argv[i][0] == L'-')
1903 perms &= ~permmod;
1904 else
1905 perms |= permmod;
1906 }
1907 if((user = finduser(argv[1])) == NULL)
1908 {
1909 newuser(argv[1], perms);
1910 } else {
1911 user->delete = 0;
1912 user->perms = perms;
1913 }
1914 return(0);
1915}
1916
1917static void preinit(int hup)
1918{
1919 struct uiuser *user;
1920
1921 if(!hup)
1922 {
1923 newuser(L"default", 0);
1924 } else {
1925 for(user = users; user != NULL; user = user->next)
1926 {
1927 if(!wcscmp(user->name, L"default"))
1928 user->delete = 1;
1929 }
1930 }
1931}
1932
1933#ifdef HAVE_IPV6
1934static struct sockaddr *getnameforport(int port, socklen_t *len)
1935{
1936 static struct sockaddr_in6 addr;
1937
1938 memset(&addr, 0, sizeof(addr));
1939 addr.sin6_family = AF_INET6;
1940 addr.sin6_port = htons(port);
1941 addr.sin6_addr = in6addr_any;
1942 if(len != NULL)
1943 *len = sizeof(addr);
1944 return((struct sockaddr *)&addr);
1945}
1946#else
1947static struct sockaddr *getnameforport(int port, socklen_t *len)
1948{
1949 static struct sockaddr_in addr;
1950
1951 memset(&addr, 0, sizeof(addr));
1952 addr.sin_family = AF_INET;
1953 addr.sin_port = htons(port);
1954 if(len != NULL)
1955 *len = sizeof(addr);
1956 return((struct sockaddr *)&addr);
1957}
1958#endif
1959
1960static int portupdate(struct configvar *var, void *uudata)
1961{
1962 struct sockaddr *addr;
1963 socklen_t addrlen;
1964 struct socket *newsock;
1965
1966 addr = getnameforport(var->val.num, &addrlen);
1967 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
1968 {
1969 flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno));
1970 return(0);
1971 }
1972 if(uisocket != NULL)
1973 putsock(uisocket);
1974 uisocket = newsock;
1975 return(0);
1976}
1977
1978static int init(int hup)
1979{
1980 struct sockaddr *addr;
1981 socklen_t addrlen;
1982 struct uiuser *user, *next;
1983
1984 if(hup)
1985 {
1986 for(user = users; user != NULL; user = next)
1987 {
1988 next = user->next;
1989 if(user->delete)
1990 freeuser(user);
1991 }
1992 }
1993 if(!hup)
1994 {
1995 if(uisocket != NULL)
1996 putsock(uisocket);
1997 addr = getnameforport(confgetint("ui", "port"), &addrlen);
1998 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
1999 {
2000 flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno));
2001 return(1);
2002 }
2003 CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL);
2004 GCBREG(newfncb, newfnetnode, NULL);
2005 GCBREG(newtransfercb, newtransfernotify, NULL);
2006 }
2007 return(0);
2008}
2009
2010static int run(void)
2011{
2012 int i, id;
2013 struct uidata *data, *next;
2014 struct qcommand *qcmd;
2015 struct notif *notif, *nnotif;
2016 wchar_t buf[64];
2017
2018 for(data = actives; data != NULL; data = next)
2019 {
2020 next = data->next;
2021 if(data->close)
2022 freeuidata(data);
2023 }
2024 for(data = actives; data != NULL; data = data->next)
2025 {
2026 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2027 {
2028 nnotif = notif->next;
2029 if(notif->state == NOTIF_WAIT)
2030 continue;
2031 id = -1;
2032 for(i = 0; i < notif->argc; i++)
2033 {
2034 if(notif->argv[i].dt == NOTIF_ID)
2035 {
2036 id = notif->argv[i].d.n;
2037 break;
2038 }
2039 }
2040 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2041 continue;
2042 sq(data->sk, 2, L"%%i", notif->code, NULL);
2043 for(i = 0; i < notif->argc; i++)
2044 {
2045 switch(notif->argv[i].dt)
2046 {
2047 case NOTIF_INT:
2048 case NOTIF_ID:
2049 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2050 break;
2051 case NOTIF_STR:
2052 if(notif->argv[i].d.s[0] == L'%')
2053 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2054 else
2055 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2056 break;
2057 case NOTIF_FLOAT:
2058 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2059 sq(data->sk, 2, buf, NULL);
2060 break;
2061 }
2062 }
2063 sq(data->sk, 0, NULL);
2064 if(notif->rlimit != 0)
2065 {
2066 notif->state = NOTIF_WAIT;
2067 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2068 } else {
2069 freenotif(notif);
2070 }
2071 }
2072 if((qcmd = unlinkqcmd(data)) != NULL)
2073 {
2074 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2075 freequeuecmd(qcmd);
2076 return(1);
2077 }
2078 }
2079 return(0);
2080}
2081
2082static void terminate(void)
2083{
2084 while(users != NULL)
2085 freeuser(users);
2086}
2087
2088static struct configvar myvars[] =
2089{
2090 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2091 {CONF_VAR_INT, "port", {.num = 1500}},
2092 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2093 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2094 {CONF_VAR_END}
2095};
2096
2097static struct configcmd mycmds[] =
2098{
2099 {"user", conf_user},
2100 {NULL}
2101};
2102
2103static struct module me =
2104{
2105 .name = "ui",
2106 .conf =
2107 {
2108 .vars = myvars,
2109 .cmds = mycmds
2110 },
2111 .preinit = preinit,
2112 .init = init,
2113 .run = run,
2114 .terminate = terminate
2115};
2116
2117MODULE(me)