2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/ip6.h>
26 #include <arpa/inet.h>
47 #include "sysevents.h"
53 #define PERM_DISALLOW 1
55 #define PERM_FNETCTL 4
57 #define PERM_TRANSCU 16
74 void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv);
79 struct qcommand *next;
87 struct uiuser *next, *prev;
96 struct notif *next, *prev;
102 struct timer *exptimer;
117 struct uidata *next, *prev;
119 struct qcommand *queue, *queuelast;
120 struct authhandle *auth;
137 struct uiuser *userinfo;
141 struct notif *fnotif, *lnotif;
143 size_t fcmdbufdata, fcmdbufsize;
145 struct socket *fcmdsk;
148 size_t inbufsize, indata;
149 /* Wordset storage */
152 /* WCS conversation stuff */
153 wchar_t *cb; /* Conversation buffer */
154 size_t cbsize, cbdata;
157 int ps; /* Parser state */
158 wchar_t *pp; /* Current parse pointer */
159 wchar_t *cw; /* Current word (building storage) */
160 size_t cwsize, cwdata;
163 static int srcheta(struct search *srch, void *uudata);
164 static int srchcommit(struct search *srch, void *uudata);
165 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
166 static struct notif *newnotif(struct uidata *data, int code, ...);
167 static void notifappend(struct notif *notif, ...);
169 struct uiuser *users = NULL;
170 struct uidata *actives = NULL;
171 struct socket *tcpsocket = NULL;
172 struct socket *unixsocket = NULL;
173 static time_t starttime;
175 static wchar_t *quoteword(wchar_t *word)
177 wchar_t *wp, *buf, *bp;
187 for(wp = word; *wp != L'\0'; wp++)
189 if(!dq && iswspace(*wp))
191 if((*wp == L'\\') || (*wp == L'\"'))
198 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
201 for(wp = word; *wp != L'\0'; wp++)
203 if((*wp == L'\\') || (*wp == L'\"'))
213 static void sq(struct socket *sk, int cont, ...)
219 wchar_t *part, *tpart;
220 size_t bufsize, bufdata;
223 bufsize = bufdata = 0;
226 while((part = va_arg(al, wchar_t *)) != NULL)
231 * This kludge demands that all arguments that you call it
232 * with are the size of an int. That happens to be the
233 * case for most datatypes on most platforms and
234 * compilers, but I don't know exactly which ones, and
235 * also a long long is a notable candidate of an arg that
236 * is not the size of an int on 32-bit archs. If it breaks
237 * some existing call on your architecture, please tell
240 part = vswprintf2(tpart = (part + 1), al);
241 for(; *tpart != L'\0'; tpart++)
255 if((tpart = quoteword(part)) != NULL)
262 if((num > 1) || ((num == 1) && !(cont & 1)))
264 bufcat(buf, part, wcslen(part));
265 if((num == 0) && (cont & 1))
272 bufcat(buf, L" \0", 2);
274 bufcat(buf, L"\r\n\0", 3);
275 if((final = icwcstombs(buf, "utf-8")) == NULL)
277 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
283 sockqueue(sk, final, strlen(final));
287 struct uiuser *finduser(wchar_t *name)
291 for(user = users; user != NULL; user = user->next)
293 if(!wcscmp(user->name, name))
299 static void logout(struct uidata *data)
301 data->userinfo = NULL;
302 if(data->username != NULL)
303 free(data->username);
304 data->username = NULL;
305 if(data->auth != NULL)
306 authputhandle(data->auth);
310 static int haspriv(struct uidata *data, int perm)
312 if(data->userinfo == NULL)
314 if(data->userinfo->perms & perm)
320 /* Useful macros for the command functions: */
321 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
322 #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)
324 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
327 struct in6_addr mv4lo;
329 if(confgetint("ui", "onlylocal"))
331 switch(sk->remote->sa_family)
334 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
337 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
339 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
341 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
353 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
359 sq(sk, 0, L"200", L"%Dolda Connect daemon v%s", VERSION, NULL);
362 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
364 if((argv != NULL) && (argv[0] != NULL))
365 sq(sk, 0, L"500", L"%Command not found: %ls", argv[0], NULL);
367 sq(sk, 0, L"500", L"No command", NULL);
370 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
372 extern volatile int running;
374 havepriv(PERM_ADMIN);
375 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
377 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
380 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
382 sq(sk, 0, L"200", L"Closing connection", NULL);
386 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
388 struct authmech *mech, *prev;
391 for(mech = mechs; mech != NULL; mech = mech->next)
396 sq(sk, 1, L"200", prev->name, NULL);
401 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
403 sq(sk, 0, L"200", prev->name, NULL);
406 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
413 if(data->username != NULL)
415 if(data->userinfo != NULL)
416 sq(sk, 0, L"503", L"Already logged in", NULL);
418 sq(sk, 0, L"503", L"Already logging in", NULL);
421 if((buf = icwcstombs(argv[2], NULL)) == NULL)
423 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
426 data->username = swcsdup(argv[2]);
427 if((pwd = getpwnam(buf)) == NULL)
430 data->uid = pwd->pw_uid;
431 if((data->auth = initauth(argv[1], buf)) == NULL)
434 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
436 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%%s", strerror(errno), NULL);
442 switch(authenticate(data->auth, NULL))
445 data->userinfo = finduser(data->username);
446 if(data->userinfo == NULL)
447 data->userinfo = finduser(L"default");
450 sq(sk, 0, L"506", L"Authentication error", NULL);
451 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
453 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
454 sq(sk, 0, L"506", L"Authentication error", NULL);
455 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
458 sq(sk, 0, L"200", L"Welcome", NULL);
459 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
463 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
464 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
468 switch(data->auth->prompt)
486 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
489 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
493 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
494 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
500 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
506 if((buf = icwcstombs(argv[1], NULL)) == NULL)
508 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
511 if((data->auth == NULL) || (data->userinfo != NULL))
513 sq(sk, 0, L"507", L"Data not expected", NULL);
516 switch(authenticate(data->auth, buf))
519 data->userinfo = finduser(data->username);
520 if(data->userinfo == NULL)
521 data->userinfo = finduser(L"default");
524 sq(sk, 0, L"506", L"Authentication error", NULL);
525 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
527 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
528 sq(sk, 0, L"506", L"Authentication error", NULL);
529 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
532 sq(sk, 0, L"200", L"Welcome", NULL);
533 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
537 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
538 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
542 switch(data->auth->prompt)
560 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
563 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
567 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
568 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
575 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
581 struct wcspair *args;
584 havepriv(PERM_FNETCTL);
585 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
586 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
587 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
590 if((buf = icwcstombs(argv[2], NULL)) == NULL)
592 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
596 for(i = 3; i < argc - 1; i += 2)
597 newwcspair(argv[i], argv[i + 1], &args);
598 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
603 if(errno == EPROTONOSUPPORT)
604 sq(sk, 0, L"511", L"No such network name", NULL);
606 sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
610 fnetsetname(fn, argv[2]);
611 sq(sk, 0, L"200", L"%%i", fn->id, L"Connection under way", NULL);
615 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
619 if(fnetnodes == NULL)
621 sq(sk, 0, L"201", L"No connected nodes", NULL);
624 for(fn = fnetnodes; fn != NULL; fn = fn->next)
626 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);
630 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
636 havepriv(PERM_FNETCTL);
637 /* Note - Programmatical user interfaces must only give one
638 * argument per command, the multiple argument form is only for
639 * convenience when manually controlling the daemon via
640 * eg. telnet. The reason is that the return codes aren't clear
641 * enough for the multiple argument form. */
642 for(i = 1; i < argc; i++)
644 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
646 sq(sk, 0, L"510", L"No such node", NULL);
649 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
651 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
657 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
660 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
663 struct fnetpeerdatum *datum;
666 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
668 sq(sk, 0, L"510", L"No such node", NULL);
671 if(fn->peerdata == NULL)
673 sq(sk, 0, L"201", L"No data available", NULL);
675 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
676 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%%i", datum->datatype, NULL);
680 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
684 struct fnetpeer *peer;
688 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
690 sq(sk, 0, L"510", L"No such node", NULL);
693 if(fn->peers == NULL)
695 sq(sk, 0, L"201", L"No peers avaiable", NULL);
697 for(peer = fn->peers; peer != NULL; peer = peer->next)
699 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%%ls", peer->id, L"%%ls", peer->nick, NULL);
700 for(i = 0; i < peer->dinum; i++)
702 if(peer->peerdi[i].datum->datatype == FNPD_INT)
703 sq(sk, 2, peer->peerdi[i].datum->id, L"%%i", peer->peerdi[i].data.num, NULL);
704 /* Note: A long long is not the size of an int, so
705 * sq() can't handle the conversion itself. */
706 if(peer->peerdi[i].datum->datatype == FNPD_LL)
708 swprintf(buf, 40, L"%lli", peer->peerdi[i].data.lnum);
709 sq(sk, 2, peer->peerdi[i].datum->id, buf, NULL);
711 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
712 sq(sk, 2, peer->peerdi[i].datum->id, L"%%ls", peer->peerdi[i].data.str, NULL);
719 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
724 struct transfer *transfer;
725 struct fnetpeer *peer;
728 if((argc > 5) && ((argc % 2) == 0))
730 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
733 havepriv(PERM_TRANS);
734 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
736 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
738 sq(sk, 0, L"510", L"No such node", NULL);
744 if((net = findfnet(argv[1])) == NULL)
746 sq(sk, 0, L"511", L"No such network name", NULL);
750 transfer = newtransfer();
751 authgethandle(transfer->auth = data->auth);
752 transfer->fnet = net;
753 transfer->peerid = swcsdup(argv[2]);
754 transfer->path = swcsdup(argv[3]);
755 transfer->dir = TRNSD_DOWN;
756 transfer->owner = data->uid;
761 linktransfer(transfer);
762 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
763 transfersetnick(transfer, peer->nick);
765 linktransfer(transfer);
768 transfersetsize(transfer, wcstol(argv[4], NULL, 0));
771 for(i = 5; i < argc; i += 2)
773 if(!wcscmp(argv[i], L"hash"))
775 transfersethash(transfer, parsehash(argv[i + 1]));
777 newwcspair(argv[i], argv[i + 1], &transfer->args);
781 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
782 transfersetactivity(transfer, L"create");
785 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
787 struct transfer *transfer, *pt;
789 havepriv(PERM_TRANS);
791 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
793 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
796 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
797 L"%%i", pt->state, pt->peerid,
798 (pt->peernick == NULL)?L"":(pt->peernick),
799 (pt->path == NULL)?L"":(pt->path),
800 L"%%i", pt->size, L"%%i", pt->curpos,
801 (pt->hash == NULL)?L"":unparsehash(pt->hash),
807 sq(sk, 0, L"201", L"No transfers", NULL);
809 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
810 L"%%i", pt->state, pt->peerid,
811 (pt->peernick == NULL)?L"":(pt->peernick),
812 (pt->path == NULL)?L"":(pt->path),
813 L"%%i", pt->size, L"%%i", pt->curpos,
814 (pt->hash == NULL)?L"":unparsehash(pt->hash),
818 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
820 struct transfer *transfer;
823 havepriv(PERM_TRANS);
824 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
826 sq(sk, 0, L"512", L"No such transfer", NULL);
829 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
831 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
834 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
836 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
840 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
843 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
849 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
852 for(i = 1; i < argc; i += 2)
854 if(!wcscasecmp(argv[i + 1], L"on"))
858 if(!wcscasecmp(argv[i], L"all"))
864 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
865 data->notify.b.fnchat = val;
866 } else if(!wcscasecmp(argv[i], L"fn:act")) {
867 data->notify.b.fnact = val;
868 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
869 data->notify.b.fnpeer = val;
870 } else if(!wcscasecmp(argv[i], L"trans:act")) {
871 data->notify.b.tract = val;
872 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
873 data->notify.b.trprog = val;
874 } else if(!wcscasecmp(argv[i], L"srch:act")) {
875 data->notify.b.srch = val;
876 } else if(!wcscasecmp(argv[i], L"msg")) {
877 data->notify.b.msg = val;
880 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
883 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
890 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
892 sq(sk, 0, L"510", L"No such node", NULL);
895 public = wcstol(argv[2], NULL, 0);
896 if((public != 0) && (public != 1))
898 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
901 if(fn->state != FNN_EST)
903 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
906 if(fnetsendchat(fn, public, argv[3], argv[4]))
909 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
910 else if(errno == EPERM)
911 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
912 else if(errno == EILSEQ)
913 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
915 sq(sk, 0, L"505", L"Could not chat", NULL);
918 sq(sk, 0, L"200", L"Chat string sent", NULL);
921 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
930 srch = newsearch(data->username, NULL);
931 for(i = 1; i < argc; i++)
933 if(!wcscmp(argv[i], L"all"))
935 for(fn = fnetnodes; fn != NULL; fn = fn->next)
937 if(fn->state == FNN_EST)
938 searchaddfn(srch, fn);
942 } else if(!wcscmp(argv[i], L"prio")) {
945 sq(sk, 0, L"501", L"No argument to prio", NULL);
949 srch->prio = wcstol(argv[i], NULL, 0);
950 } else if(iswdigit(*argv[i])) {
951 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
953 sq(sk, 0, L"510", L"No such node", NULL);
957 searchaddfn(srch, fn);
962 if(srch->fnl == NULL)
964 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
970 sq(sk, 0, L"501", L"No search expression found on line", NULL);
974 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
976 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
981 getsexpr(srch->sexpr = sexpr);
983 CBREG(srch, search_eta, srcheta, NULL, NULL);
984 CBREG(srch, search_commit, srchcommit, NULL, NULL);
985 CBREG(srch, search_result, srchres, NULL, NULL);
986 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
990 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
992 struct search *srch, *pt;
998 for(srch = searches; srch != NULL; srch = srch->next)
1000 if(!wcscmp(srch->owner, data->username))
1003 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
1008 sq(sk, 0, L"201", L"No searches", NULL);
1010 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
1013 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1015 struct search *srch;
1020 havepriv(PERM_SRCH);
1021 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1023 sq(sk, 0, L"514", L"No such search", NULL);
1026 if(srch->results == NULL)
1028 sq(sk, 0, L"201", L"No results", NULL);
1030 for(sr = srch->results; sr != NULL; sr = sr->next)
1032 swprintf(buf, 64, L"%f", sr->time);
1033 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);
1038 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1040 struct search *srch;
1044 havepriv(PERM_SRCH);
1045 /* Note - Programmatical user interfaces must only give one
1046 * argument per command, the multiple argument form is only for
1047 * convenience when manually controlling the daemon via
1048 * eg. telnet. The reason is that the return codes aren't clear
1049 * enough for the multiple argument form. */
1050 for(i = 1; i < argc; i++)
1052 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1054 sq(sk, 0, L"514", L"No such search", NULL);
1059 sq(sk, 0, L"200", L"Search cancelled", NULL);
1062 static void fcmdread(struct socket *sk, struct uidata *data)
1067 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1069 bufcat(data->fcmdbuf, buf, bufsize);
1073 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1075 wchar_t *wbuf, *p, *p2;
1079 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1080 kill(-data->fcmdpid, SIGHUP);
1081 putsock(data->fcmdsk);
1082 data->fcmdsk = NULL;
1083 if(data->fcmdbuf != NULL)
1085 free(data->fcmdbuf);
1086 data->fcmdbuf = NULL;
1088 data->fcmdbufsize = data->fcmdbufdata = 0;
1089 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1092 putsock(data->fcmdsk);
1093 data->fcmdsk = NULL;
1095 if(data->fcmdbuf == NULL)
1097 wbuf = swcsdup(L"");
1099 addtobuf(data->fcmdbuf, 0);
1100 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1101 free(data->fcmdbuf);
1103 data->fcmdbuf = NULL;
1104 data->fcmdbufsize = data->fcmdbufdata = 0;
1107 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1111 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1114 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1120 sq(data->sk, 0, L"201", L"No data returned", NULL);
1122 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1127 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1133 char *filtercmd, *argbuf;
1134 size_t cargvsize, cargvdata;
1135 struct passwd *pwent;
1138 havepriv(PERM_TRANS);
1139 if((pwent = getpwuid(data->uid)) == NULL)
1141 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1142 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1145 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1146 if(filtercmd == NULL)
1147 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1148 if(filtercmd == NULL)
1150 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1151 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1155 cargvsize = cargvdata = 0;
1156 addtobuf(cargv, filtercmd);
1157 for(i = 1; i < argc; i++)
1159 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1161 for(i = 0; i < cargvdata; i++)
1164 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1167 addtobuf(cargv, argbuf);
1169 addtobuf(cargv, NULL);
1170 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)
1172 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1173 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1178 execv(filtercmd, cargv);
1179 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1182 for(pp = cargv; *pp; pp++)
1185 data->fcmdsk = wrapsock(pipe);
1186 data->fcmdpid = pid;
1187 if(data->fcmdbuf != NULL)
1189 free(data->fcmdbuf);
1190 data->fcmdbuf = NULL;
1192 data->fcmdbufsize = data->fcmdbufdata = 0;
1193 data->fcmdsk->data = data;
1194 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1195 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1198 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1200 struct transfer *transfer;
1204 havepriv(PERM_TRANS);
1205 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1207 sq(sk, 0, L"512", L"No such transfer", NULL);
1210 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1212 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1215 if(transfer->args == NULL)
1217 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1219 for(ta = transfer->args; ta != NULL; ta = ta->next)
1220 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->key, L"%%ls", ta->val, NULL);
1224 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1226 struct sharecache *node;
1230 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1232 if(node->f.b.type == FILE_REG)
1235 if(node->f.b.hastth)
1239 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1242 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1244 wchar_t *buf1, *buf2;
1246 havepriv(PERM_TRANS);
1247 buf1 = swprintf2(L"%lli", bytesdownload);
1248 buf2 = swprintf2(L"%lli", bytesupload);
1249 sq(sk, 0, L"200", L"down", L"%%ls", buf1, L"up", L"%%ls", buf2, NULL);
1254 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1259 if(data->userinfo == NULL) {
1260 sq(sk, 0, L"502", L"Must be logged in", NULL);
1263 if(argv[1][0] == L'#') {
1264 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1267 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1268 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1269 sq(sk, 0, L"516", L"Name already in use", NULL);
1273 if(data->regname != NULL)
1274 free(data->regname);
1275 data->regname = swcsdup(argv[1]);
1276 sq(sk, 0, L"200", L"Registered", NULL);
1279 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1282 struct uidata *rcpt;
1284 struct notif *notif;
1287 if(data->userinfo == NULL) {
1288 sq(sk, 0, L"502", L"Must be logged in", NULL);
1291 if(argv[1][0] == L'#') {
1292 rcptid = wcstol(argv[1] + 1, NULL, 0);
1293 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1294 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1298 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1299 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1304 sq(sk, 0, L"517", L"No such recipient", NULL);
1307 if(!rcpt->notify.b.msg) {
1308 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1311 if(data->regname != NULL)
1312 myname = swcsdup(data->regname);
1314 myname = swprintf2(L"#%i", data->id);
1315 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1316 for(i = 2; i < argc; i++)
1317 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1318 sq(sk, 0, L"200", L"Message sent", NULL);
1321 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1323 sq(sk, 0, L"200", L"%%i", time(NULL) - starttime, NULL);
1330 * Reserved command numbers for nameless commands:
1331 * 0: Issued when a client has connected
1332 * 1: Issued when a named command couldn't be found
1335 static struct command commands[] =
1337 {NULL, cmd_connect},
1338 {NULL, cmd_notfound},
1339 {L"shutdown", cmd_shutdown},
1340 {L"quit", cmd_quit},
1341 {L"lsauth", cmd_lsauth},
1342 {L"login", cmd_login},
1343 {L"pass", cmd_pass},
1344 {L"cnct", cmd_fnetconnect},
1345 {L"lsnodes", cmd_lsnodes},
1346 {L"dcnct", cmd_disconnect},
1347 {L"lspa", cmd_lspa},
1348 {L"lspeers", cmd_lspeers},
1349 {L"download", cmd_download},
1350 {L"lstrans", cmd_lstrans},
1351 {L"cancel", cmd_cancel},
1352 {L"notify", cmd_notify},
1353 {L"sendchat", cmd_sendchat},
1354 {L"search", cmd_search},
1355 {L"lssrch", cmd_lssrch},
1356 {L"lssr", cmd_lssr},
1357 {L"cansrch", cmd_cansrch},
1358 {L"filtercmd", cmd_filtercmd},
1359 {L"lstrarg", cmd_lstrarg},
1360 {L"hashstatus", cmd_hashstatus},
1361 {L"transstatus", cmd_transstatus},
1362 {L"register", cmd_register},
1363 {L"sendmsg", cmd_sendmsg},
1364 {L"uptime", cmd_uptime},
1368 static void freequeuecmd(struct qcommand *qcmd)
1372 if(qcmd->argv != NULL)
1374 for(i = 0; i < qcmd->argc; i++)
1375 free(qcmd->argv[i]);
1381 static struct qcommand *unlinkqcmd(struct uidata *data)
1383 struct qcommand *qcmd;
1388 data->queue = qcmd->next;
1389 if(qcmd == data->queuelast)
1390 data->queuelast = qcmd->next;
1395 static void notifappendv(struct notif *notif, va_list args)
1399 while((dt = va_arg(args, int)) != NOTIF_END)
1402 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1403 notif->argv[ca].dt = dt;
1408 notif->argv[ca].d.n = va_arg(args, int);
1411 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1414 notif->argv[ca].d.d = va_arg(args, double);
1420 static void notifappend(struct notif *notif, ...)
1424 va_start(args, notif);
1425 notifappendv(notif, args);
1429 static struct notif *newnotif(struct uidata *data, int code, ...)
1431 struct notif *notif;
1434 notif = smalloc(sizeof(*notif));
1435 memset(notif, 0, sizeof(*notif));
1436 notif->rlimit = 0.0;
1439 va_start(args, code);
1440 notifappendv(notif, args);
1443 notif->prev = data->lnotif;
1444 if(data->lnotif != NULL)
1445 data->lnotif->next = notif;
1447 data->fnotif = notif;
1448 data->lnotif = notif;
1452 static void freenotif(struct notif *notif)
1456 if(notif->next != NULL)
1457 notif->next->prev = notif->prev;
1458 if(notif->prev != NULL)
1459 notif->prev->next = notif->next;
1460 if(notif == notif->ui->fnotif)
1461 notif->ui->fnotif = notif->next;
1462 if(notif == notif->ui->lnotif)
1463 notif->ui->lnotif = notif->prev;
1464 if(notif->exptimer != NULL)
1465 canceltimer(notif->exptimer);
1466 for(i = 0; i < notif->argc; i++)
1468 if(notif->argv[i].dt == NOTIF_STR)
1469 free(notif->argv[i].d.s);
1471 if(notif->argv != NULL)
1476 static void notifexpire(int cancelled, struct notif *notif)
1478 notif->exptimer = NULL;
1483 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1487 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1489 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1494 for(i = 0; i < notif->argc; i++)
1496 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1511 static void freeuidata(struct uidata *data)
1514 struct qcommand *qcmd;
1516 if(data->next != NULL)
1517 data->next->prev = data->prev;
1518 if(data->prev != NULL)
1519 data->prev->next = data->next;
1521 actives = data->next;
1522 data->sk->readcb = NULL;
1523 data->sk->errcb = NULL;
1525 while((qcmd = unlinkqcmd(data)) != NULL)
1527 iconv_close(data->ichandle);
1528 if(data->cw != NULL)
1530 if(data->cb != NULL)
1532 if(data->argv != NULL)
1534 for(i = 0; i < data->argc; i++)
1535 free(data->argv[i]);
1538 if(data->auth != NULL)
1539 authputhandle(data->auth);
1540 if(data->regname != NULL)
1541 free(data->regname);
1542 if(data->username != NULL)
1544 if(data->userinfo != NULL)
1545 flog(LOG_INFO, "%ls logged out", data->username);
1546 free(data->username);
1549 while(data->fnotif != NULL)
1550 freenotif(data->fnotif);
1551 if(data->fcmdbuf != NULL)
1552 free(data->fcmdbuf);
1553 if(data->fcmdpid != 0)
1554 kill(-data->fcmdpid, SIGHUP);
1555 if(data->fcmdsk != NULL)
1556 putsock(data->fcmdsk);
1560 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1562 struct qcommand *new;
1564 new = smalloc(sizeof(*new));
1569 if(data->queuelast != NULL)
1570 data->queuelast->next = new;
1571 data->queuelast = new;
1572 if(data->queue == NULL)
1576 static struct uidata *newuidata(struct socket *sk)
1578 struct uidata *data;
1579 static int curid = 0;
1581 data = smalloc(sizeof(*data));
1582 memset(data, 0, sizeof(*data));
1586 data->inbuf = smalloc(1024);
1588 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1590 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1593 data->next = actives;
1596 actives->prev = data;
1601 static void uiread(struct socket *sk, struct uidata *data)
1607 size_t datalen, len2;
1608 struct command *cur;
1610 if(data->indata > 1024)
1612 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1614 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1615 memcpy(data->inbuf + data->indata, newbuf, datalen);
1617 data->indata += datalen;
1618 if(data->cb == NULL)
1620 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1622 data->pp = data->cb;
1627 if(data->cbsize == data->cbdata)
1629 len2 = data->pp - data->cb;
1630 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1631 data->pp = data->cb + len2;
1634 p2 = (char *)(porig = (data->cb + data->cbdata));
1635 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1636 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1637 memmove(data->inbuf, p1, data->indata);
1638 /* Just a sanity check */
1639 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1641 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1644 data->cbdata += (((wchar_t *)p2) - porig);
1650 /* XXX: Should this really just ignore it? */
1658 /* Just a sanity check */
1659 if(data->cbsize != data->cbdata)
1661 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1666 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1674 while(!done && (data->pp - data->cb < data->cbdata))
1679 if(iswspace(*data->pp))
1681 if(*data->pp == L'\r')
1683 if(data->pp == data->cb + data->cbdata - 1)
1688 if(*(++data->pp) == L'\n')
1690 if((data->argv != NULL) && (data->argv[0] != NULL))
1692 for(cur = commands; cur->handler != NULL; cur++)
1694 if(cur->name == NULL)
1696 if(!wcscasecmp(cur->name, data->argv[0]))
1698 queuecmd(data, cur, data->argc, data->argv);
1702 if(cur->handler == NULL)
1703 queuecmd(data, &commands[1], data->argc, data->argv);
1705 queuecmd(data, &commands[1], data->argc, data->argv);
1710 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1711 data->pp = data->cb;
1724 if(iswspace(*data->pp))
1726 addtobuf(data->cw, L'\0');
1727 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1728 data->argv[data->argc++] = data->cw;
1733 } else if(*data->pp == L'\"') {
1736 } else if(*data->pp == L'\\') {
1737 if(data->pp == data->cb + data->cbdata - 1)
1742 addtobuf(data->cw, *(++data->pp));
1745 addtobuf(data->cw, *(data->pp++));
1749 if(*data->pp == L'\"')
1752 } else if(*data->pp == L'\\') {
1753 if(data->pp == data->cb + data->cbdata - 1)
1758 addtobuf(data->cw, *(++(data->pp)));
1760 addtobuf(data->cw, *data->pp);
1768 static void uierror(struct socket *sk, int err, struct uidata *data)
1771 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1775 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1777 struct uidata *uidata;
1779 newsk->data = uidata = newuidata(newsk);
1780 socksettos(newsk, confgetint("ui", "uitos"));
1783 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1784 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1785 queuecmd(uidata, &commands[0], 0, NULL);
1788 static int srcheta(struct search *srch, void *uudata)
1790 struct uidata *data;
1792 for(data = actives; data != NULL; data = data->next)
1794 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1795 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1800 static int srchcommit(struct search *srch, void *uudata)
1802 struct uidata *data;
1804 for(data = actives; data != NULL; data = data->next)
1806 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1807 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1812 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1814 struct uidata *data;
1816 for(data = actives; data != NULL; data = data->next)
1818 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1820 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1821 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);
1827 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1829 struct uidata *data;
1831 for(data = actives; data != NULL; data = data->next)
1833 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1834 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1839 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1841 struct uidata *data;
1842 struct notif *notif;
1844 if(!wcscmp(attrib, L"state"))
1846 for(data = actives; data != NULL; data = data->next)
1848 if(data->notify.b.fnact)
1849 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1851 } else if(!wcscmp(attrib, L"name")) {
1852 for(data = actives; data != NULL; data = data->next)
1854 if(data->notify.b.fnact)
1855 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1857 } else if(!wcscmp(attrib, L"numpeers")) {
1858 for(data = actives; data != NULL; data = data->next)
1860 if(data->notify.b.fnact)
1862 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1863 notif->argv[1].d.n = fn->numpeers;
1865 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1872 static int fnunlink(struct fnetnode *fn, void *uudata)
1874 struct uidata *data;
1876 for(data = actives; data != NULL; data = data->next)
1878 if(data->notify.b.fnact)
1879 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1884 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1886 struct uidata *data;
1888 for(data = actives; data != NULL; data = data->next)
1890 if(data->notify.b.fnpeer)
1891 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1896 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1898 struct uidata *data;
1900 for(data = actives; data != NULL; data = data->next)
1902 if(data->notify.b.fnpeer)
1903 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1908 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1910 struct uidata *data;
1911 struct notif *notif;
1914 for(data = actives; data != NULL; data = data->next)
1916 if(data->notify.b.fnpeer)
1918 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1920 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1924 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1925 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1926 switch(di->datum->datatype)
1929 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1932 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1935 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1936 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1944 static int newfnetnode(struct fnetnode *fn, void *uudata)
1946 struct uidata *data;
1948 for(data = actives; data != NULL; data = data->next)
1950 if(data->notify.b.fnact)
1951 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1953 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1954 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1955 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1956 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1957 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1958 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
1962 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1964 struct uidata *data;
1966 if(!wcscmp(attrib, L"state"))
1968 for(data = actives; data != NULL; data = data->next)
1970 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1971 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1973 } else if(!wcscmp(attrib, L"nick")) {
1974 for(data = actives; data != NULL; data = data->next)
1976 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1977 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1979 } else if(!wcscmp(attrib, L"size")) {
1980 for(data = actives; data != NULL; data = data->next)
1982 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1983 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1985 } else if(!wcscmp(attrib, L"error")) {
1986 for(data = actives; data != NULL; data = data->next)
1988 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1989 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1991 } else if(!wcscmp(attrib, L"path")) {
1992 for(data = actives; data != NULL; data = data->next)
1994 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1995 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1997 } else if(!wcscmp(attrib, L"hash")) {
1998 for(data = actives; data != NULL; data = data->next)
2000 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2001 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2007 static int transferprog(struct transfer *transfer, void *uudata)
2009 struct uidata *data;
2010 struct notif *notif;
2012 for(data = actives; data != NULL; data = data->next)
2014 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2016 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2017 notif->argv[1].d.n = transfer->curpos;
2019 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2025 static int transferdestroyed(struct transfer *transfer, void *uudata)
2027 struct uidata *data;
2029 for(data = actives; data != NULL; data = data->next)
2031 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2032 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2037 static int newtransfernotify(struct transfer *transfer, void *uudata)
2039 struct uidata *data;
2041 for(data = actives; data != NULL; data = data->next)
2043 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2044 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);
2046 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2047 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2048 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2052 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2056 new = smalloc(sizeof(*new));
2058 new->name = swcsdup(name);
2069 static void freeuser(struct uiuser *user)
2071 if(user->next != NULL)
2072 user->next->prev = user->prev;
2073 if(user->prev != NULL)
2074 user->prev->next = user->next;
2081 static int conf_user(int argc, wchar_t **argv)
2083 int i, perms, permmod;
2084 struct uiuser *user;
2089 flog(LOG_WARNING, "not enough arguments given for user command");
2093 for(i = 2; i < argc; i++)
2095 if(!iswalpha(argv[i][0]))
2099 if(!wcscmp(p, L"disallow"))
2100 permmod = PERM_DISALLOW;
2101 if(!wcscmp(p, L"admin"))
2102 permmod = PERM_ADMIN;
2103 if(!wcscmp(p, L"fnetctl"))
2104 permmod = PERM_FNETCTL;
2105 if(!wcscmp(p, L"trans"))
2106 permmod = PERM_TRANS;
2107 if(!wcscmp(p, L"transcu"))
2108 permmod = PERM_TRANSCU;
2109 if(!wcscmp(p, L"chat"))
2110 permmod = PERM_CHAT;
2111 if(!wcscmp(p, L"srch"))
2112 permmod = PERM_SRCH;
2113 if(!wcscmp(p, L"all"))
2115 if(argv[i][0] == L'-')
2120 if((user = finduser(argv[1])) == NULL)
2122 newuser(argv[1], perms);
2125 user->perms = perms;
2130 static void preinit(int hup)
2132 struct uiuser *user;
2136 newuser(L"default", 0);
2138 for(user = users; user != NULL; user = user->next)
2140 if(!wcscmp(user->name, L"default"))
2146 static struct sockaddr_un *makeunixname(void)
2148 static struct sockaddr_un buf;
2153 memset(&buf, 0, sizeof(buf));
2154 buf.sun_family = PF_UNIX;
2155 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2156 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2159 if(!strcmp(val, "none"))
2161 if(!strcmp(val, "default"))
2163 if((uid = getuid()) == 0)
2165 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2168 if((pwd = getpwuid(uid)) == NULL)
2170 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2173 strcpy(buf.sun_path, "/tmp/doldacond-");
2174 strcat(buf.sun_path, pwd->pw_name);
2178 if(strchr(val, '/'))
2180 strcpy(buf.sun_path, val);
2183 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2187 static int tcpportupdate(struct configvar *var, void *uudata)
2189 struct socket *newsock;
2192 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2194 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2197 if(tcpsocket != NULL)
2202 tcpsocket = newsock;
2206 static int unixsockupdate(struct configvar *var, void *uudata)
2208 struct socket *newsock;
2209 struct sockaddr_un *un;
2212 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2214 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2217 if(unixsocket != NULL)
2219 putsock(unixsocket);
2222 unixsocket = newsock;
2226 static int init(int hup)
2228 struct uiuser *user, *next;
2229 struct sockaddr_un *un;
2233 for(user = users; user != NULL; user = next)
2242 starttime = time(NULL);
2243 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2245 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2248 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2249 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2251 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2254 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2255 GCBREG(newfncb, newfnetnode, NULL);
2256 GCBREG(newtransfercb, newtransfernotify, NULL);
2261 static int run(void)
2264 struct uidata *data, *next;
2265 struct qcommand *qcmd;
2266 struct notif *notif, *nnotif;
2269 for(data = actives; data != NULL; data = next)
2275 for(data = actives; data != NULL; data = data->next)
2277 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2279 nnotif = notif->next;
2280 if(notif->state == NOTIF_WAIT)
2283 for(i = 0; i < notif->argc; i++)
2285 if(notif->argv[i].dt == NOTIF_ID)
2287 id = notif->argv[i].d.n;
2291 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2293 sq(data->sk, 2, L"%%i", notif->code, NULL);
2294 for(i = 0; i < notif->argc; i++)
2296 switch(notif->argv[i].dt)
2300 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2303 if(notif->argv[i].d.s[0] == L'%')
2304 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2306 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2309 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2310 sq(data->sk, 2, buf, NULL);
2314 sq(data->sk, 0, NULL);
2315 if(notif->rlimit != 0)
2317 notif->state = NOTIF_WAIT;
2318 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2323 if((qcmd = unlinkqcmd(data)) != NULL)
2325 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2333 static void terminate(void)
2335 while(users != NULL)
2337 if(tcpsocket != NULL)
2339 if(unixsocket != NULL)
2340 putsock(unixsocket);
2343 static struct configvar myvars[] =
2345 /** If true, UI connections will only be accepted from localhost
2346 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2347 * completely sure that you know what you are doing, never turn
2348 * this off when auth.authless is on. */
2349 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2350 /** The TCP port number on which to accept UI client connections,
2351 * or -1 to not listen on TCP. */
2352 {CONF_VAR_INT, "port", {.num = 1500}},
2354 * Controls the the name to use for the Unix socket on which to
2355 * accept UI client connections. If the name contains a slash, it
2356 * is treated as a file name to bind on. If the name is "default",
2357 * the file name will be "/var/run/doldacond.sock" if doldacond
2358 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2359 * NAME is the user name of the UID which doldacond runs as. If
2360 * the name is "none", no Unix socket will be used. Otherwise, an
2361 * error is signaled.
2363 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2364 /** The TOS value to use for UI connections (see the TOS VALUES
2366 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2367 /** The name of the filtercmd script (see the FILES section for
2368 * lookup information). */
2369 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2373 static struct configcmd mycmds[] =
2375 {"user", conf_user},
2379 static struct module me =
2390 .terminate = terminate