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
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip6.h>
25 #include <arpa/inet.h>
46 #include "sysevents.h"
52 #define PERM_DISALLOW 1
54 #define PERM_FNETCTL 4
56 #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;
118 struct uidata *next, *prev;
120 struct qcommand *queue, *queuelast;
122 struct authhandle *auth;
139 struct uiuser *userinfo;
143 struct notif *fnotif, *lnotif;
145 size_t fcmdbufdata, fcmdbufsize;
147 struct socket *fcmdsk;
150 size_t inbufsize, indata;
151 /* Wordset storage */
154 /* WCS conversation stuff */
155 wchar_t *cb; /* Conversation buffer */
156 size_t cbsize, cbdata;
159 int ps; /* Parser state */
160 wchar_t *pp; /* Current parse pointer */
161 wchar_t *cw; /* Current word (building storage) */
162 size_t cwsize, cwdata;
165 static int srcheta(struct search *srch, void *uudata);
166 static int srchcommit(struct search *srch, void *uudata);
167 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
168 static struct notif *newnotif(struct uidata *data, int code, ...);
169 static void notifappend(struct notif *notif, ...);
171 struct uiuser *users = NULL;
172 struct uidata *actives = NULL;
173 struct socket *tcpsocket = NULL;
174 struct socket *unixsocket = NULL;
175 static time_t starttime;
177 static wchar_t *quoteword(wchar_t *word)
179 wchar_t *wp, *buf, *bp;
189 for(wp = word; *wp != L'\0'; wp++)
191 if(!dq && iswspace(*wp))
193 if((*wp == L'\\') || (*wp == L'\"'))
200 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
203 for(wp = word; *wp != L'\0'; wp++)
205 if((*wp == L'\\') || (*wp == L'\"'))
215 static void sq(struct socket *sk, int cont, ...)
221 wchar_t *part, *tpart;
222 size_t bufsize, bufdata;
225 bufsize = bufdata = 0;
228 while((part = va_arg(al, wchar_t *)) != NULL)
234 if(!wcscmp(tpart, L"i"))
237 part = swprintf2(L"%i", va_arg(al, int));
238 } else if(!wcscmp(tpart, L"zi")) {
240 part = swprintf2(L"%zi", va_arg(al, size_t));
241 } else if(!wcscmp(tpart, L"oi")) {
243 part = swprintf2(L"%ji", (intmax_t)va_arg(al, off_t));
244 } else if(!wcscmp(tpart, L"s")) {
246 part = icmbstowcs(sarg = va_arg(al, char *), NULL);
251 flog(LOG_ERR, "could not convert local string to wcs: %s", sarg);
253 } else if(!wcscmp(tpart, L"ls")) {
254 part = va_arg(al, wchar_t *);
255 } else if(!wcscmp(tpart, L"ll")) {
257 part = swprintf2(L"%lli", va_arg(al, long long));
258 } else if(!wcscmp(tpart, L"f")) {
260 part = swprintf2(L"%f", va_arg(al, double));
261 } else if(!wcscmp(tpart, L"x")) {
263 part = swprintf2(L"%x", va_arg(al, int));
265 flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart);
269 if((tpart = quoteword(part)) != NULL)
276 if((num > 1) || ((num == 1) && !(cont & 1)))
278 bufcat(buf, part, wcslen(part));
279 if((num == 0) && (cont & 1))
286 bufcat(buf, L" \0", 2);
288 bufcat(buf, L"\r\n\0", 3);
289 if((final = icwcstombs(buf, "utf-8")) == NULL)
291 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
297 sockqueue(sk, final, strlen(final));
301 struct uiuser *finduser(wchar_t *name)
305 for(user = users; user != NULL; user = user->next)
307 if(!wcscmp(user->name, name))
313 static void logout(struct uidata *data)
315 data->userinfo = NULL;
316 if(data->username != NULL)
317 free(data->username);
318 data->username = NULL;
319 if(data->auth != NULL)
320 authputhandle(data->auth);
324 static int haspriv(struct uidata *data, int perm)
326 if(data->userinfo == NULL)
328 if(data->userinfo->perms & perm)
334 /* Useful macros for the command functions: */
335 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
336 #define havepriv(p) do { if((data->userinfo == NULL) || ((data->userinfo->perms & (p)) != (p))) { sq(sk, 0, L"502", L"Unauthorized request", L"needed", L"%x", (p), NULL); return; } } while(0)
338 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
341 struct in6_addr mv4lo;
343 if(confgetint("ui", "onlylocal"))
345 switch(sk->remote->sa_family)
348 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
351 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
353 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
355 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
367 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
373 sq(sk, 0, L"201", L"1", L"2", L"Dolda Connect daemon v" VERSION, NULL);
376 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
378 if((argv != NULL) && (argv[0] != NULL))
379 sq(sk, 0, L"500", L"Command not found", NULL);
381 sq(sk, 0, L"500", L"No command", NULL);
384 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
386 extern volatile int running;
388 havepriv(PERM_ADMIN);
389 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
391 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
394 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
396 sq(sk, 0, L"200", L"Closing connection", NULL);
400 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
402 struct authmech *mech, *prev;
405 for(mech = mechs; mech != NULL; mech = mech->next)
407 if(mech->enabled && authavailable(mech, sk))
410 sq(sk, 1, L"200", prev->name, NULL);
415 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
417 sq(sk, 0, L"200", prev->name, NULL);
420 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
427 if(data->username != NULL)
429 if(data->userinfo != NULL)
430 sq(sk, 0, L"503", L"Already logged in", NULL);
432 sq(sk, 0, L"503", L"Already logging in", NULL);
435 if((buf = icwcstombs(argv[2], NULL)) == NULL)
437 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
440 data->username = swcsdup(argv[2]);
441 if((pwd = getpwnam(buf)) == NULL)
444 data->uid = pwd->pw_uid;
445 if((data->auth = initauth(argv[1], buf)) == NULL)
448 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
450 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL);
456 switch(authenticate(data->auth, sk, NULL))
459 data->userinfo = finduser(data->username);
460 if(data->userinfo == NULL)
461 data->userinfo = finduser(L"default");
464 sq(sk, 0, L"506", L"Authentication error", NULL);
465 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
467 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
468 sq(sk, 0, L"506", L"Authentication error", NULL);
469 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
472 sq(sk, 0, L"200", L"Welcome", NULL);
473 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
477 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
478 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
482 switch(data->auth->prompt)
500 sq(sk, 0, L"%i", code, data->auth->text, NULL);
503 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
507 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
508 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
514 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
520 if((buf = icwcstombs(argv[1], NULL)) == NULL)
522 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
525 if((data->auth == NULL) || (data->userinfo != NULL))
527 sq(sk, 0, L"507", L"Data not expected", NULL);
530 switch(authenticate(data->auth, sk, buf))
533 data->userinfo = finduser(data->username);
534 if(data->userinfo == NULL)
535 data->userinfo = finduser(L"default");
538 sq(sk, 0, L"506", L"Authentication error", NULL);
539 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
541 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
542 sq(sk, 0, L"506", L"Authentication error", NULL);
543 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
546 sq(sk, 0, L"200", L"Welcome", NULL);
547 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
551 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
552 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
556 switch(data->auth->prompt)
574 sq(sk, 0, L"%i", code, data->auth->text, NULL);
577 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
581 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
582 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
589 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
595 struct wcspair *args;
598 havepriv(PERM_FNETCTL);
599 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
600 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
601 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
604 if((buf = icwcstombs(argv[2], NULL)) == NULL)
606 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
610 for(i = 3; i < argc - 1; i += 2)
611 newwcspair(argv[i], argv[i + 1], &args);
612 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
617 if(errno == EPROTONOSUPPORT)
618 sq(sk, 0, L"511", L"No such network name", NULL);
620 sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL);
624 fnetsetname(fn, argv[2]);
625 sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL);
629 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
633 if(fnetnodes == NULL)
635 sq(sk, 0, L"201", L"No connected nodes", NULL);
638 for(fn = fnetnodes; fn != NULL; fn = fn->next)
640 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);
644 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
650 havepriv(PERM_FNETCTL);
651 /* Note - Programmatical user interfaces must only give one
652 * argument per command, the multiple argument form is only for
653 * convenience when manually controlling the daemon via
654 * eg. telnet. The reason is that the return codes aren't clear
655 * enough for the multiple argument form. */
656 for(i = 1; i < argc; i++)
658 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
660 sq(sk, 0, L"510", L"No such node", NULL);
663 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
665 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
671 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
674 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
677 struct fnetpeerdatum *datum;
680 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
682 sq(sk, 0, L"510", L"No such node", NULL);
685 if(fn->peerdata == NULL)
687 sq(sk, 0, L"201", L"No data available", NULL);
689 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
690 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL);
694 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
698 struct fnetpeer *peer, *npeer;
701 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
703 sq(sk, 0, L"510", L"No such node", NULL);
706 if(fn->peers == NULL)
708 sq(sk, 0, L"201", L"No peers available", NULL);
710 for(peer = btreeiter(fn->peers); peer != NULL; peer = npeer)
712 npeer = btreeiter(NULL);
713 sq(sk, 2 | ((npeer != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL);
714 for(i = 0; i < peer->dinum; i++)
716 if(peer->peerdi[i].datum->datatype == FNPD_INT)
717 sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL);
718 if(peer->peerdi[i].datum->datatype == FNPD_LL)
719 sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL);
720 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
721 sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL);
728 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
733 struct transfer *transfer;
734 struct fnetpeer *peer;
737 if((argc > 5) && ((argc % 2) == 0))
739 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
742 havepriv(PERM_TRANS);
743 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
745 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
747 sq(sk, 0, L"510", L"No such node", NULL);
753 if((net = findfnet(argv[1])) == NULL)
755 sq(sk, 0, L"511", L"No such network name", NULL);
759 transfer = newtransfer();
760 authgethandle(transfer->auth = data->auth);
761 transfer->fnet = net;
762 transfer->peerid = swcsdup(argv[2]);
763 transfer->path = swcsdup(argv[3]);
764 transfer->dir = TRNSD_DOWN;
765 transfer->owner = data->uid;
770 linktransfer(transfer);
771 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
772 transfersetnick(transfer, peer->nick);
774 linktransfer(transfer);
777 transfersetsize(transfer, wcstoll(argv[4], NULL, 0));
780 for(i = 5; i < argc; i += 2)
782 if(!wcscmp(argv[i], L"hash"))
784 transfersethash(transfer, parsehash(argv[i + 1]));
786 newwcspair(argv[i], argv[i + 1], &transfer->args);
790 sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL);
791 transfersetactivity(transfer, L"create");
794 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
796 struct transfer *transfer, *pt;
798 havepriv(PERM_TRANS);
800 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
802 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
805 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir,
806 L"%i", pt->state, pt->peerid,
807 (pt->peernick == NULL)?L"":(pt->peernick),
808 (pt->path == NULL)?L"":(pt->path),
809 L"%oi", pt->size, L"%oi", pt->curpos,
810 (pt->hash == NULL)?L"":unparsehash(pt->hash),
816 sq(sk, 0, L"201", L"No transfers", NULL);
818 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir,
819 L"%i", pt->state, pt->peerid,
820 (pt->peernick == NULL)?L"":(pt->peernick),
821 (pt->path == NULL)?L"":(pt->path),
822 L"%oi", pt->size, L"%oi", pt->curpos,
823 (pt->hash == NULL)?L"":unparsehash(pt->hash),
827 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
829 struct transfer *transfer;
832 havepriv(PERM_TRANS);
833 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
835 sq(sk, 0, L"512", L"No such transfer", NULL);
838 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
840 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
843 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
845 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
849 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
852 static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
854 struct transfer *transfer;
857 havepriv(PERM_TRANS);
858 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
860 sq(sk, 0, L"512", L"No such transfer", NULL);
863 if(transfer->dir == TRNSD_UP)
865 sq(sk, 0, L"512", L"Only applicable to downloads", NULL);
868 resettransfer(transfer);
869 sq(sk, 0, L"200", L"Transfer reset", NULL);
872 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
878 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
881 for(i = 1; i < argc; i += 2)
883 if(!wcscasecmp(argv[i + 1], L"on"))
887 if(!wcscasecmp(argv[i], L"all"))
893 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
894 data->notify.b.fnchat = val;
895 } else if(!wcscasecmp(argv[i], L"fn:act")) {
896 data->notify.b.fnact = val;
897 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
898 data->notify.b.fnpeer = val;
899 } else if(!wcscasecmp(argv[i], L"trans:act")) {
900 data->notify.b.tract = val;
901 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
902 data->notify.b.trprog = val;
903 } else if(!wcscasecmp(argv[i], L"srch:act")) {
904 data->notify.b.srch = val;
905 } else if(!wcscasecmp(argv[i], L"msg")) {
906 data->notify.b.msg = val;
909 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
912 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
919 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
921 sq(sk, 0, L"510", L"No such node", NULL);
924 public = wcstol(argv[2], NULL, 0);
925 if((public != 0) && (public != 1))
927 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
930 if(fn->state != FNN_EST)
932 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
935 if(fnetsendchat(fn, public, argv[3], argv[4]))
938 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
939 else if(errno == EPERM)
940 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
941 else if(errno == EILSEQ)
942 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
944 sq(sk, 0, L"505", L"Could not chat", NULL);
947 sq(sk, 0, L"200", L"Chat string sent", NULL);
950 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
959 srch = newsearch(data->username, NULL);
960 for(i = 1; i < argc; i++)
962 if(!wcscmp(argv[i], L"all"))
964 for(fn = fnetnodes; fn != NULL; fn = fn->next)
966 if(fn->state == FNN_EST)
967 searchaddfn(srch, fn);
971 } else if(!wcscmp(argv[i], L"prio")) {
974 sq(sk, 0, L"501", L"No argument to prio", NULL);
978 srch->prio = wcstol(argv[i], NULL, 0);
979 } else if(iswdigit(*argv[i])) {
980 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
982 sq(sk, 0, L"510", L"No such node", NULL);
986 searchaddfn(srch, fn);
991 if(srch->fnl == NULL)
993 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
999 sq(sk, 0, L"501", L"No search expression found on line", NULL);
1003 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
1005 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
1010 getsexpr(srch->sexpr = sexpr);
1012 CBREG(srch, search_eta, srcheta, NULL, NULL);
1013 CBREG(srch, search_commit, srchcommit, NULL, NULL);
1014 CBREG(srch, search_result, srchres, NULL, NULL);
1015 sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL);
1019 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1021 struct search *srch, *pt;
1024 havepriv(PERM_SRCH);
1027 for(srch = searches; srch != NULL; srch = srch->next)
1029 if(!wcscmp(srch->owner, data->username))
1032 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1037 sq(sk, 0, L"201", L"No searches", NULL);
1039 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1042 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1044 struct search *srch;
1048 havepriv(PERM_SRCH);
1049 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1051 sq(sk, 0, L"514", L"No such search", NULL);
1054 if(srch->results == NULL)
1056 sq(sk, 0, L"201", L"No results", NULL);
1058 for(sr = srch->results; sr != NULL; sr = sr->next)
1060 sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename,
1061 sr->fnet->name, L"%ls", sr->peerid, L"%oi", sr->size,
1062 L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id),
1064 L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1069 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1071 struct search *srch;
1075 havepriv(PERM_SRCH);
1076 /* Note - Programmatical user interfaces must only give one
1077 * argument per command, the multiple argument form is only for
1078 * convenience when manually controlling the daemon via
1079 * eg. telnet. The reason is that the return codes aren't clear
1080 * enough for the multiple argument form. */
1081 for(i = 1; i < argc; i++)
1083 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1085 sq(sk, 0, L"514", L"No such search", NULL);
1090 sq(sk, 0, L"200", L"Search cancelled", NULL);
1093 static void fcmdread(struct socket *sk, struct uidata *data)
1098 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1100 bufcat(data->fcmdbuf, buf, bufsize);
1104 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1106 wchar_t *wbuf, *p, *p2;
1110 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1111 kill(-data->fcmdpid, SIGHUP);
1112 putsock(data->fcmdsk);
1113 data->fcmdsk = NULL;
1114 if(data->fcmdbuf != NULL)
1116 free(data->fcmdbuf);
1117 data->fcmdbuf = NULL;
1119 data->fcmdbufsize = data->fcmdbufdata = 0;
1120 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL);
1123 putsock(data->fcmdsk);
1124 data->fcmdsk = NULL;
1126 if(data->fcmdbuf == NULL)
1128 wbuf = swcsdup(L"");
1130 addtobuf(data->fcmdbuf, 0);
1131 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1132 free(data->fcmdbuf);
1134 data->fcmdbuf = NULL;
1135 data->fcmdbufsize = data->fcmdbufdata = 0;
1138 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1142 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1145 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL);
1151 sq(data->sk, 0, L"201", L"No data returned", NULL);
1153 sq(data->sk, 0, L"200", L"%ls", p, NULL);
1158 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1164 char *filtercmd, *argbuf;
1165 size_t cargvsize, cargvdata;
1166 struct passwd *pwent;
1169 havepriv(PERM_TRANS);
1170 if((pwent = getpwuid(data->uid)) == NULL)
1172 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1173 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1176 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1177 if(filtercmd == NULL)
1178 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1179 if(filtercmd == NULL)
1181 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1182 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1186 cargvsize = cargvdata = 0;
1187 addtobuf(cargv, filtercmd);
1188 for(i = 1; i < argc; i++)
1190 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1192 for(i = 0; i < cargvdata; i++)
1195 sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL);
1198 addtobuf(cargv, argbuf);
1200 addtobuf(cargv, NULL);
1201 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)
1203 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1204 sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL);
1209 execv(filtercmd, cargv);
1210 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1213 for(pp = cargv; *pp; pp++)
1216 data->fcmdsk = wrapsock(pipe);
1217 data->fcmdpid = pid;
1218 if(data->fcmdbuf != NULL)
1220 free(data->fcmdbuf);
1221 data->fcmdbuf = NULL;
1223 data->fcmdbufsize = data->fcmdbufdata = 0;
1224 data->fcmdsk->data = data;
1225 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1226 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1229 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1231 struct transfer *transfer;
1235 havepriv(PERM_TRANS);
1236 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1238 sq(sk, 0, L"512", L"No such transfer", NULL);
1241 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1243 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1246 if(transfer->args == NULL)
1248 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1250 for(ta = transfer->args; ta != NULL; ta = ta->next)
1251 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1255 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1257 struct sharecache *node;
1261 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1263 if(node->f.b.type == FILE_REG)
1266 if(node->f.b.hastth)
1270 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1273 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1275 havepriv(PERM_TRANS);
1276 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1279 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1284 if(data->userinfo == NULL) {
1285 sq(sk, 0, L"502", L"Must be logged in", NULL);
1288 if(argv[1][0] == L'#') {
1289 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1292 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1293 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1294 sq(sk, 0, L"516", L"Name already in use", NULL);
1298 if(data->regname != NULL)
1299 free(data->regname);
1300 data->regname = swcsdup(argv[1]);
1301 sq(sk, 0, L"200", L"Registered", NULL);
1304 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1307 struct uidata *rcpt;
1309 struct notif *notif;
1312 if(data->userinfo == NULL) {
1313 sq(sk, 0, L"502", L"Must be logged in", NULL);
1316 if(argv[1][0] == L'#') {
1317 rcptid = wcstol(argv[1] + 1, NULL, 0);
1318 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1319 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1323 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1324 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1329 sq(sk, 0, L"517", L"No such recipient", NULL);
1332 if(!rcpt->notify.b.msg) {
1333 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1336 if(data->regname != NULL)
1337 myname = swcsdup(data->regname);
1339 myname = swprintf2(L"#%i", data->id);
1340 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1341 for(i = 2; i < argc; i++)
1342 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1343 sq(sk, 0, L"200", L"Message sent", NULL);
1346 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1348 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1351 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1353 extern volatile int reinit;
1355 havepriv(PERM_ADMIN);
1356 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1358 sq(sk, 0, L"200", L"Will reinit", NULL);
1365 * Reserved command numbers for nameless commands:
1366 * 0: Issued when a client has connected
1367 * 1: Issued when a named command couldn't be found
1370 static struct command commands[] =
1372 {NULL, cmd_connect},
1373 {NULL, cmd_notfound},
1374 {L"shutdown", cmd_shutdown},
1375 {L"quit", cmd_quit},
1376 {L"lsauth", cmd_lsauth},
1377 {L"login", cmd_login},
1378 {L"pass", cmd_pass},
1379 {L"cnct", cmd_fnetconnect},
1380 {L"lsnodes", cmd_lsnodes},
1381 {L"dcnct", cmd_disconnect},
1382 {L"lspa", cmd_lspa},
1383 {L"lspeers", cmd_lspeers},
1384 {L"download", cmd_download},
1385 {L"lstrans", cmd_lstrans},
1386 {L"cancel", cmd_cancel},
1387 {L"reset", cmd_reset},
1388 {L"notify", cmd_notify},
1389 {L"sendchat", cmd_sendchat},
1390 {L"search", cmd_search},
1391 {L"lssrch", cmd_lssrch},
1392 {L"lssr", cmd_lssr},
1393 {L"cansrch", cmd_cansrch},
1394 {L"filtercmd", cmd_filtercmd},
1395 {L"lstrarg", cmd_lstrarg},
1396 {L"hashstatus", cmd_hashstatus},
1397 {L"transstatus", cmd_transstatus},
1398 {L"register", cmd_register},
1399 {L"sendmsg", cmd_sendmsg},
1400 {L"uptime", cmd_uptime},
1405 static void freequeuecmd(struct qcommand *qcmd)
1409 if(qcmd->argv != NULL)
1411 for(i = 0; i < qcmd->argc; i++)
1412 free(qcmd->argv[i]);
1418 static struct qcommand *unlinkqcmd(struct uidata *data)
1420 struct qcommand *qcmd;
1426 data->queue = qcmd->next;
1427 if(qcmd == data->queuelast)
1428 data->queuelast = qcmd->next;
1433 static void notifappendv(struct notif *notif, va_list args)
1437 while((dt = va_arg(args, int)) != NOTIF_END)
1440 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1441 notif->argv[ca].dt = dt;
1446 notif->argv[ca].d.n = va_arg(args, int);
1449 notif->argv[ca].d.o = va_arg(args, off_t);
1452 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1455 notif->argv[ca].d.d = va_arg(args, double);
1461 static void notifappend(struct notif *notif, ...)
1465 va_start(args, notif);
1466 notifappendv(notif, args);
1470 static struct notif *newnotif(struct uidata *data, int code, ...)
1472 struct notif *notif;
1475 notif = smalloc(sizeof(*notif));
1476 memset(notif, 0, sizeof(*notif));
1477 notif->rlimit = 0.0;
1480 va_start(args, code);
1481 notifappendv(notif, args);
1484 notif->prev = data->lnotif;
1485 if(data->lnotif != NULL)
1486 data->lnotif->next = notif;
1488 data->fnotif = notif;
1489 data->lnotif = notif;
1493 static void freenotif(struct notif *notif)
1497 if(notif->next != NULL)
1498 notif->next->prev = notif->prev;
1499 if(notif->prev != NULL)
1500 notif->prev->next = notif->next;
1501 if(notif == notif->ui->fnotif)
1502 notif->ui->fnotif = notif->next;
1503 if(notif == notif->ui->lnotif)
1504 notif->ui->lnotif = notif->prev;
1505 if(notif->exptimer != NULL)
1506 canceltimer(notif->exptimer);
1507 for(i = 0; i < notif->argc; i++)
1509 if(notif->argv[i].dt == NOTIF_STR)
1510 free(notif->argv[i].d.s);
1512 if(notif->argv != NULL)
1517 static void notifexpire(int cancelled, struct notif *notif)
1519 notif->exptimer = NULL;
1524 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1528 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1530 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1535 for(i = 0; i < notif->argc; i++)
1537 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1552 static void freeuidata(struct uidata *data)
1555 struct qcommand *qcmd;
1557 if(data->next != NULL)
1558 data->next->prev = data->prev;
1559 if(data->prev != NULL)
1560 data->prev->next = data->next;
1562 actives = data->next;
1563 data->sk->readcb = NULL;
1564 data->sk->errcb = NULL;
1565 closesock(data->sk);
1567 while((qcmd = unlinkqcmd(data)) != NULL)
1569 iconv_close(data->ichandle);
1570 if(data->cw != NULL)
1572 if(data->cb != NULL)
1574 if(data->argv != NULL)
1576 for(i = 0; i < data->argc; i++)
1577 free(data->argv[i]);
1580 if(data->auth != NULL)
1581 authputhandle(data->auth);
1582 if(data->regname != NULL)
1583 free(data->regname);
1584 if(data->username != NULL)
1586 if(data->userinfo != NULL)
1587 flog(LOG_INFO, "%ls logged out", data->username);
1588 free(data->username);
1591 while(data->fnotif != NULL)
1592 freenotif(data->fnotif);
1593 if(data->fcmdbuf != NULL)
1594 free(data->fcmdbuf);
1595 if(data->fcmdpid != 0)
1596 kill(-data->fcmdpid, SIGHUP);
1597 if(data->fcmdsk != NULL)
1598 putsock(data->fcmdsk);
1602 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1604 struct qcommand *new;
1606 new = smalloc(sizeof(*new));
1611 if(data->queuelast != NULL)
1612 data->queuelast->next = new;
1613 data->queuelast = new;
1614 if(data->queue == NULL)
1619 static struct uidata *newuidata(struct socket *sk)
1621 struct uidata *data;
1622 static int curid = 0;
1624 data = smalloc(sizeof(*data));
1625 memset(data, 0, sizeof(*data));
1629 data->inbuf = smalloc(1024);
1631 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1633 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1636 data->next = actives;
1639 actives->prev = data;
1644 static void uiread(struct socket *sk, struct uidata *data)
1650 size_t datalen, len2;
1651 struct command *cur;
1653 if(data->indata > 1024)
1655 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1657 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1658 memcpy(data->inbuf + data->indata, newbuf, datalen);
1660 data->indata += datalen;
1661 if(data->cb == NULL)
1663 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1665 data->pp = data->cb;
1670 if(data->cbsize == data->cbdata)
1672 len2 = data->pp - data->cb;
1673 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1674 data->pp = data->cb + len2;
1677 p2 = (char *)(porig = (data->cb + data->cbdata));
1678 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1679 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1680 memmove(data->inbuf, p1, data->indata);
1681 /* Just a sanity check */
1682 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1684 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1687 data->cbdata += (((wchar_t *)p2) - porig);
1693 /* XXX: Should this really just ignore it? */
1701 /* Just a sanity check */
1702 if(data->cbsize != data->cbdata)
1704 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1709 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1717 while(!done && (data->pp - data->cb < data->cbdata))
1722 if(iswspace(*data->pp))
1724 if(*data->pp == L'\r')
1726 if(data->pp == data->cb + data->cbdata - 1)
1731 if(*(++data->pp) == L'\n')
1733 if((data->argv != NULL) && (data->argv[0] != NULL))
1735 for(cur = commands; cur->handler != NULL; cur++)
1737 if(cur->name == NULL)
1739 if(!wcscasecmp(cur->name, data->argv[0]))
1741 queuecmd(data, cur, data->argc, data->argv);
1745 if(cur->handler == NULL)
1746 queuecmd(data, &commands[1], data->argc, data->argv);
1748 queuecmd(data, &commands[1], data->argc, data->argv);
1753 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1754 data->pp = data->cb;
1767 if(iswspace(*data->pp))
1769 addtobuf(data->cw, L'\0');
1770 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1771 data->argv[data->argc++] = data->cw;
1776 } else if(*data->pp == L'\"') {
1779 } else if(*data->pp == L'\\') {
1780 if(data->pp == data->cb + data->cbdata - 1)
1785 addtobuf(data->cw, *(++data->pp));
1788 addtobuf(data->cw, *(data->pp++));
1792 if(*data->pp == L'\"')
1795 } else if(*data->pp == L'\\') {
1796 if(data->pp == data->cb + data->cbdata - 1)
1801 addtobuf(data->cw, *(++(data->pp)));
1803 addtobuf(data->cw, *data->pp);
1809 if(data->cbdata > 16384)
1811 /* Kill clients that send us unreasonably long lines */
1816 static void uierror(struct socket *sk, int err, struct uidata *data)
1819 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1823 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1825 struct uidata *uidata;
1827 newsk->data = uidata = newuidata(newsk);
1828 socksettos(newsk, confgetint("ui", "uitos"));
1831 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1832 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1833 queuecmd(uidata, &commands[0], 0, NULL);
1836 static int srcheta(struct search *srch, void *uudata)
1838 struct uidata *data;
1840 for(data = actives; data != NULL; data = data->next)
1842 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1843 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1848 static int srchcommit(struct search *srch, void *uudata)
1850 struct uidata *data;
1852 for(data = actives; data != NULL; data = data->next)
1854 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1855 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1860 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1862 struct uidata *data;
1864 for(data = actives; data != NULL; data = data->next)
1866 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1868 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size,
1869 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);
1875 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1877 struct uidata *data;
1879 for(data = actives; data != NULL; data = data->next)
1881 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1882 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1887 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1889 struct uidata *data;
1890 struct notif *notif;
1892 if(!wcscmp(attrib, L"state"))
1894 for(data = actives; data != NULL; data = data->next)
1896 if(data->notify.b.fnact)
1897 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1899 } else if(!wcscmp(attrib, L"name")) {
1900 for(data = actives; data != NULL; data = data->next)
1902 if(data->notify.b.fnact)
1903 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1905 } else if(!wcscmp(attrib, L"numpeers")) {
1906 for(data = actives; data != NULL; data = data->next)
1908 if(data->notify.b.fnact)
1910 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1911 notif->argv[1].d.n = fn->numpeers;
1913 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1920 static int fnunlink(struct fnetnode *fn, void *uudata)
1922 struct uidata *data;
1924 for(data = actives; data != NULL; data = data->next)
1926 if(data->notify.b.fnact)
1927 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1932 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1934 struct uidata *data;
1936 for(data = actives; data != NULL; data = data->next)
1938 if(data->notify.b.fnpeer)
1939 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1944 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1946 struct uidata *data;
1948 for(data = actives; data != NULL; data = data->next)
1950 if(data->notify.b.fnpeer)
1951 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1956 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1958 struct uidata *data;
1959 struct notif *notif;
1962 for(data = actives; data != NULL; data = data->next)
1964 if(data->notify.b.fnpeer)
1966 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1968 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1972 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1973 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1974 switch(di->datum->datatype)
1977 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1980 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1983 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1984 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1992 static int newfnetnode(struct fnetnode *fn, void *uudata)
1994 struct uidata *data;
1996 for(data = actives; data != NULL; data = data->next)
1998 if(data->notify.b.fnact)
1999 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
2001 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
2002 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
2003 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
2004 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
2005 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
2006 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
2010 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
2012 struct uidata *data;
2014 if(!wcscmp(attrib, L"state"))
2016 for(data = actives; data != NULL; data = data->next)
2018 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2019 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2021 } else if(!wcscmp(attrib, L"nick")) {
2022 for(data = actives; data != NULL; data = data->next)
2024 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2025 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2027 } else if(!wcscmp(attrib, L"size")) {
2028 for(data = actives; data != NULL; data = data->next)
2030 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2031 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END);
2033 } else if(!wcscmp(attrib, L"error")) {
2034 for(data = actives; data != NULL; data = data->next)
2036 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2037 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2039 } else if(!wcscmp(attrib, L"path")) {
2040 for(data = actives; data != NULL; data = data->next)
2042 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2043 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2045 } else if(!wcscmp(attrib, L"hash")) {
2046 for(data = actives; data != NULL; data = data->next)
2048 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2049 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2055 static int transferprog(struct transfer *transfer, void *uudata)
2057 struct uidata *data;
2058 struct notif *notif;
2060 for(data = actives; data != NULL; data = data->next)
2062 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2064 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2065 notif->argv[1].d.o = transfer->curpos;
2067 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2073 static int transferdestroyed(struct transfer *transfer, void *uudata)
2075 struct uidata *data;
2077 for(data = actives; data != NULL; data = data->next)
2079 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2080 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2085 static int newtransfernotify(struct transfer *transfer, void *uudata)
2087 struct uidata *data;
2089 for(data = actives; data != NULL; data = data->next)
2091 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2092 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);
2094 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2095 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2096 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2100 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2104 new = smalloc(sizeof(*new));
2106 new->name = swcsdup(name);
2117 static void freeuser(struct uiuser *user)
2119 if(user->next != NULL)
2120 user->next->prev = user->prev;
2121 if(user->prev != NULL)
2122 user->prev->next = user->next;
2129 static int conf_user(int argc, wchar_t **argv)
2131 int i, perms, permmod;
2132 struct uiuser *user;
2137 flog(LOG_WARNING, "not enough arguments given for user command");
2141 for(i = 2; i < argc; i++)
2143 if(!iswalpha(argv[i][0]))
2147 if(!wcscmp(p, L"disallow"))
2148 permmod = PERM_DISALLOW;
2149 if(!wcscmp(p, L"admin"))
2150 permmod = PERM_ADMIN;
2151 if(!wcscmp(p, L"fnetctl"))
2152 permmod = PERM_FNETCTL;
2153 if(!wcscmp(p, L"trans"))
2154 permmod = PERM_TRANS;
2155 if(!wcscmp(p, L"transcu"))
2156 permmod = PERM_TRANSCU;
2157 if(!wcscmp(p, L"chat"))
2158 permmod = PERM_CHAT;
2159 if(!wcscmp(p, L"srch"))
2160 permmod = PERM_SRCH;
2161 if(!wcscmp(p, L"all"))
2163 if(argv[i][0] == L'-')
2168 if((user = finduser(argv[1])) == NULL)
2170 newuser(argv[1], perms);
2173 user->perms = perms;
2178 static void preinit(int hup)
2180 struct uiuser *user;
2184 newuser(L"default", PERM_DISALLOW);
2186 for(user = users; user != NULL; user = user->next)
2188 if(!wcscmp(user->name, L"default"))
2194 static struct sockaddr_un *makeunixname(void)
2196 static struct sockaddr_un buf;
2201 memset(&buf, 0, sizeof(buf));
2202 buf.sun_family = PF_UNIX;
2203 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2204 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2207 if(!strcmp(val, "none"))
2209 if(!strcmp(val, "default"))
2211 if((uid = getuid()) == 0)
2213 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2216 if((pwd = getpwuid(uid)) == NULL)
2218 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2221 strcpy(buf.sun_path, "/tmp/doldacond-");
2222 strcat(buf.sun_path, pwd->pw_name);
2226 if(strchr(val, '/'))
2228 strcpy(buf.sun_path, val);
2231 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2235 static int tcpportupdate(struct configvar *var, void *uudata)
2237 struct socket *newsock;
2240 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2242 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2245 if(tcpsocket != NULL)
2250 tcpsocket = newsock;
2254 static int unixsockupdate(struct configvar *var, void *uudata)
2256 struct socket *newsock;
2257 struct sockaddr_un *un;
2262 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2265 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2269 if(unixsocket != NULL)
2271 putsock(unixsocket);
2274 unixsocket = newsock;
2278 static int init(int hup)
2280 struct uiuser *user, *next;
2281 struct sockaddr_un *un;
2288 for(user = users; user != NULL; user = next)
2297 starttime = time(NULL);
2298 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2300 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2303 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2305 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2308 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2312 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2313 GCBREG(newfncb, newfnetnode, NULL);
2314 GCBREG(newtransfercb, newtransfernotify, NULL);
2318 for(user = users; user != NULL; user = user->next)
2320 if(wcscmp(user->name, L"default"))
2325 if((pwd = getpwuid(getuid())) == NULL)
2327 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2330 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2332 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2335 newuser(wcsname, ~PERM_DISALLOW);
2342 static int run(void)
2345 struct uidata *data, *next;
2346 struct qcommand *qcmd;
2347 struct notif *notif, *nnotif;
2349 for(data = actives; data != NULL; data = next)
2355 for(data = actives; data != NULL; data = data->next)
2357 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2359 nnotif = notif->next;
2360 if(notif->state == NOTIF_WAIT)
2363 for(i = 0; i < notif->argc; i++)
2365 if(notif->argv[i].dt == NOTIF_ID)
2367 id = notif->argv[i].d.n;
2371 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2373 sq(data->sk, 2, L"%i", notif->code, NULL);
2374 for(i = 0; i < notif->argc; i++)
2376 switch(notif->argv[i].dt)
2380 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2383 sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL);
2386 if(notif->argv[i].d.s[0] == L'%')
2387 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2389 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2392 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2396 sq(data->sk, 0, NULL);
2397 if(notif->rlimit != 0)
2399 notif->state = NOTIF_WAIT;
2400 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2405 if((qcmd = unlinkqcmd(data)) != NULL)
2407 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2411 if(data->queuesize > 10)
2413 /* Clients should not be queue up commands at all, since
2414 * they should not send a new command before receiving a
2415 * reply to the previous command. Therefore, we
2416 * mercilessly massacre clients which are stacking up too
2424 static void terminate(void)
2426 while(users != NULL)
2428 if(tcpsocket != NULL)
2430 if(unixsocket != NULL)
2431 putsock(unixsocket);
2434 static struct configvar myvars[] =
2436 /** If true, UI connections will only be accepted from localhost
2437 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2438 * completely sure that you know what you are doing, never turn
2439 * this off when auth.authless is on. */
2440 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2441 /** The TCP port number on which to accept UI client connections,
2442 * or -1 to not listen on TCP. */
2443 {CONF_VAR_INT, "port", {.num = 1500}},
2445 * Controls the the name to use for the Unix socket on which to
2446 * accept UI client connections. If the name contains a slash, it
2447 * is treated as a file name to bind on. If the name is "default",
2448 * the file name will be "/var/run/doldacond.sock" if doldacond
2449 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2450 * NAME is the user name of the UID which doldacond runs as. If
2451 * the name is "none", no Unix socket will be used. Otherwise, an
2452 * error is signaled.
2454 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2455 /** The TOS value to use for UI connections (see the TOS VALUES
2457 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2458 /** The name of the filtercmd script (see the FILES section for
2459 * lookup information). */
2460 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2464 static struct configcmd mycmds[] =
2466 {"user", conf_user},
2470 static struct module me =
2481 .terminate = terminate