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
73 void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv);
78 struct qcommand *next;
86 struct uiuser *next, *prev;
95 struct notif *next, *prev;
101 struct timer *exptimer;
116 struct uidata *next, *prev;
118 struct qcommand *queue, *queuelast;
119 struct authhandle *auth;
136 struct uiuser *userinfo;
140 struct notif *fnotif, *lnotif;
142 size_t fcmdbufdata, fcmdbufsize;
144 struct socket *fcmdsk;
147 size_t inbufsize, indata;
148 /* Wordset storage */
151 /* WCS conversation stuff */
152 wchar_t *cb; /* Conversation buffer */
153 size_t cbsize, cbdata;
156 int ps; /* Parser state */
157 wchar_t *pp; /* Current parse pointer */
158 wchar_t *cw; /* Current word (building storage) */
159 size_t cwsize, cwdata;
162 static int srcheta(struct search *srch, void *uudata);
163 static int srchcommit(struct search *srch, void *uudata);
164 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
165 static struct notif *newnotif(struct uidata *data, int code, ...);
166 static void notifappend(struct notif *notif, ...);
168 struct uiuser *users = NULL;
169 struct uidata *actives = NULL;
170 struct socket *tcpsocket = NULL;
171 struct socket *unixsocket = NULL;
172 static time_t starttime;
174 static wchar_t *quoteword(wchar_t *word)
176 wchar_t *wp, *buf, *bp;
186 for(wp = word; *wp != L'\0'; wp++)
188 if(!dq && iswspace(*wp))
190 if((*wp == L'\\') || (*wp == L'\"'))
197 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
200 for(wp = word; *wp != L'\0'; wp++)
202 if((*wp == L'\\') || (*wp == L'\"'))
212 static void sq(struct socket *sk, int cont, ...)
218 wchar_t *part, *tpart;
219 size_t bufsize, bufdata;
222 bufsize = bufdata = 0;
225 while((part = va_arg(al, wchar_t *)) != NULL)
231 if(!wcscmp(tpart, L"i"))
234 part = swprintf2(L"%i", va_arg(al, int));
235 } else if(!wcscmp(tpart, L"s")) {
237 part = icmbstowcs(sarg = va_arg(al, char *), NULL);
242 flog(LOG_ERR, "could not convert local string to wcs: %s", sarg);
244 } else if(!wcscmp(tpart, L"ls")) {
245 part = va_arg(al, wchar_t *);
246 } else if(!wcscmp(tpart, L"ll")) {
248 part = swprintf2(L"%lli", va_arg(al, long long));
249 } else if(!wcscmp(tpart, L"f")) {
251 part = swprintf2(L"%f", va_arg(al, double));
252 } else if(!wcscmp(tpart, L"x")) {
254 part = swprintf2(L"%x", va_arg(al, int));
256 flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart);
260 if((tpart = quoteword(part)) != NULL)
267 if((num > 1) || ((num == 1) && !(cont & 1)))
269 bufcat(buf, part, wcslen(part));
270 if((num == 0) && (cont & 1))
277 bufcat(buf, L" \0", 2);
279 bufcat(buf, L"\r\n\0", 3);
280 if((final = icwcstombs(buf, "utf-8")) == NULL)
282 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
288 sockqueue(sk, final, strlen(final));
292 struct uiuser *finduser(wchar_t *name)
296 for(user = users; user != NULL; user = user->next)
298 if(!wcscmp(user->name, name))
304 static void logout(struct uidata *data)
306 data->userinfo = NULL;
307 if(data->username != NULL)
308 free(data->username);
309 data->username = NULL;
310 if(data->auth != NULL)
311 authputhandle(data->auth);
315 static int haspriv(struct uidata *data, int perm)
317 if(data->userinfo == NULL)
319 if(data->userinfo->perms & perm)
325 /* Useful macros for the command functions: */
326 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
327 #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)
329 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
332 struct in6_addr mv4lo;
334 if(confgetint("ui", "onlylocal"))
336 switch(sk->remote->sa_family)
339 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
342 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
344 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
346 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
358 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
364 sq(sk, 0, L"201", L"1", L"2", L"Dolda Connect daemon v" VERSION, NULL);
367 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
369 if((argv != NULL) && (argv[0] != NULL))
370 sq(sk, 0, L"500", L"Command not found", NULL);
372 sq(sk, 0, L"500", L"No command", NULL);
375 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
377 extern volatile int running;
379 havepriv(PERM_ADMIN);
380 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
382 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
385 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
387 sq(sk, 0, L"200", L"Closing connection", NULL);
391 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
393 struct authmech *mech, *prev;
396 for(mech = mechs; mech != NULL; mech = mech->next)
398 if(mech->enabled && authavailable(mech, sk))
401 sq(sk, 1, L"200", prev->name, NULL);
406 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
408 sq(sk, 0, L"200", prev->name, NULL);
411 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
418 if(data->username != NULL)
420 if(data->userinfo != NULL)
421 sq(sk, 0, L"503", L"Already logged in", NULL);
423 sq(sk, 0, L"503", L"Already logging in", NULL);
426 if((buf = icwcstombs(argv[2], NULL)) == NULL)
428 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
431 data->username = swcsdup(argv[2]);
432 if((pwd = getpwnam(buf)) == NULL)
435 data->uid = pwd->pw_uid;
436 if((data->auth = initauth(argv[1], buf)) == NULL)
439 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
441 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL);
447 switch(authenticate(data->auth, sk, NULL))
450 data->userinfo = finduser(data->username);
451 if(data->userinfo == NULL)
452 data->userinfo = finduser(L"default");
455 sq(sk, 0, L"506", L"Authentication error", NULL);
456 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
458 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
459 sq(sk, 0, L"506", L"Authentication error", NULL);
460 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
463 sq(sk, 0, L"200", L"Welcome", NULL);
464 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
468 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
469 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
473 switch(data->auth->prompt)
491 sq(sk, 0, L"%i", code, data->auth->text, NULL);
494 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
498 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
499 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
505 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
511 if((buf = icwcstombs(argv[1], NULL)) == NULL)
513 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
516 if((data->auth == NULL) || (data->userinfo != NULL))
518 sq(sk, 0, L"507", L"Data not expected", NULL);
521 switch(authenticate(data->auth, sk, buf))
524 data->userinfo = finduser(data->username);
525 if(data->userinfo == NULL)
526 data->userinfo = finduser(L"default");
529 sq(sk, 0, L"506", L"Authentication error", NULL);
530 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
532 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
533 sq(sk, 0, L"506", L"Authentication error", NULL);
534 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
537 sq(sk, 0, L"200", L"Welcome", NULL);
538 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
542 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
543 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
547 switch(data->auth->prompt)
565 sq(sk, 0, L"%i", code, data->auth->text, NULL);
568 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
572 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
573 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
580 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
586 struct wcspair *args;
589 havepriv(PERM_FNETCTL);
590 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
591 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
592 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
595 if((buf = icwcstombs(argv[2], NULL)) == NULL)
597 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
601 for(i = 3; i < argc - 1; i += 2)
602 newwcspair(argv[i], argv[i + 1], &args);
603 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
608 if(errno == EPROTONOSUPPORT)
609 sq(sk, 0, L"511", L"No such network name", NULL);
611 sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL);
615 fnetsetname(fn, argv[2]);
616 sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL);
620 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
624 if(fnetnodes == NULL)
626 sq(sk, 0, L"201", L"No connected nodes", NULL);
629 for(fn = fnetnodes; fn != NULL; fn = fn->next)
631 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);
635 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
641 havepriv(PERM_FNETCTL);
642 /* Note - Programmatical user interfaces must only give one
643 * argument per command, the multiple argument form is only for
644 * convenience when manually controlling the daemon via
645 * eg. telnet. The reason is that the return codes aren't clear
646 * enough for the multiple argument form. */
647 for(i = 1; i < argc; i++)
649 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
651 sq(sk, 0, L"510", L"No such node", NULL);
654 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
656 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
662 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
665 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
668 struct fnetpeerdatum *datum;
671 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
673 sq(sk, 0, L"510", L"No such node", NULL);
676 if(fn->peerdata == NULL)
678 sq(sk, 0, L"201", L"No data available", NULL);
680 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
681 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL);
685 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
689 struct fnetpeer *peer;
692 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
694 sq(sk, 0, L"510", L"No such node", NULL);
697 if(fn->peers == NULL)
699 sq(sk, 0, L"201", L"No peers avaiable", NULL);
701 for(peer = fn->peers; peer != NULL; peer = peer->next)
703 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL);
704 for(i = 0; i < peer->dinum; i++)
706 if(peer->peerdi[i].datum->datatype == FNPD_INT)
707 sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL);
708 if(peer->peerdi[i].datum->datatype == FNPD_LL)
709 sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL);
710 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
711 sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL);
718 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
723 struct transfer *transfer;
724 struct fnetpeer *peer;
727 if((argc > 5) && ((argc % 2) == 0))
729 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
732 havepriv(PERM_TRANS);
733 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
735 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
737 sq(sk, 0, L"510", L"No such node", NULL);
743 if((net = findfnet(argv[1])) == NULL)
745 sq(sk, 0, L"511", L"No such network name", NULL);
749 transfer = newtransfer();
750 authgethandle(transfer->auth = data->auth);
751 transfer->fnet = net;
752 transfer->peerid = swcsdup(argv[2]);
753 transfer->path = swcsdup(argv[3]);
754 transfer->dir = TRNSD_DOWN;
755 transfer->owner = data->uid;
760 linktransfer(transfer);
761 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
762 transfersetnick(transfer, peer->nick);
764 linktransfer(transfer);
767 transfersetsize(transfer, wcstol(argv[4], NULL, 0));
770 for(i = 5; i < argc; i += 2)
772 if(!wcscmp(argv[i], L"hash"))
774 transfersethash(transfer, parsehash(argv[i + 1]));
776 newwcspair(argv[i], argv[i + 1], &transfer->args);
780 sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL);
781 transfersetactivity(transfer, L"create");
784 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
786 struct transfer *transfer, *pt;
788 havepriv(PERM_TRANS);
790 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
792 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
795 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir,
796 L"%i", pt->state, pt->peerid,
797 (pt->peernick == NULL)?L"":(pt->peernick),
798 (pt->path == NULL)?L"":(pt->path),
799 L"%i", pt->size, L"%i", pt->curpos,
800 (pt->hash == NULL)?L"":unparsehash(pt->hash),
806 sq(sk, 0, L"201", L"No transfers", NULL);
808 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir,
809 L"%i", pt->state, pt->peerid,
810 (pt->peernick == NULL)?L"":(pt->peernick),
811 (pt->path == NULL)?L"":(pt->path),
812 L"%i", pt->size, L"%i", pt->curpos,
813 (pt->hash == NULL)?L"":unparsehash(pt->hash),
817 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
819 struct transfer *transfer;
822 havepriv(PERM_TRANS);
823 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
825 sq(sk, 0, L"512", L"No such transfer", NULL);
828 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
830 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
833 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
835 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
839 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
842 static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
844 struct transfer *transfer;
847 havepriv(PERM_TRANS);
848 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
850 sq(sk, 0, L"512", L"No such transfer", NULL);
853 if(transfer->dir == TRNSD_UP)
855 sq(sk, 0, L"512", L"Only applicable to downloads", NULL);
858 resettransfer(transfer);
859 sq(sk, 0, L"200", L"Transfer reset", NULL);
862 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
868 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
871 for(i = 1; i < argc; i += 2)
873 if(!wcscasecmp(argv[i + 1], L"on"))
877 if(!wcscasecmp(argv[i], L"all"))
883 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
884 data->notify.b.fnchat = val;
885 } else if(!wcscasecmp(argv[i], L"fn:act")) {
886 data->notify.b.fnact = val;
887 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
888 data->notify.b.fnpeer = val;
889 } else if(!wcscasecmp(argv[i], L"trans:act")) {
890 data->notify.b.tract = val;
891 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
892 data->notify.b.trprog = val;
893 } else if(!wcscasecmp(argv[i], L"srch:act")) {
894 data->notify.b.srch = val;
895 } else if(!wcscasecmp(argv[i], L"msg")) {
896 data->notify.b.msg = val;
899 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
902 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
909 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
911 sq(sk, 0, L"510", L"No such node", NULL);
914 public = wcstol(argv[2], NULL, 0);
915 if((public != 0) && (public != 1))
917 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
920 if(fn->state != FNN_EST)
922 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
925 if(fnetsendchat(fn, public, argv[3], argv[4]))
928 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
929 else if(errno == EPERM)
930 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
931 else if(errno == EILSEQ)
932 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
934 sq(sk, 0, L"505", L"Could not chat", NULL);
937 sq(sk, 0, L"200", L"Chat string sent", NULL);
940 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
949 srch = newsearch(data->username, NULL);
950 for(i = 1; i < argc; i++)
952 if(!wcscmp(argv[i], L"all"))
954 for(fn = fnetnodes; fn != NULL; fn = fn->next)
956 if(fn->state == FNN_EST)
957 searchaddfn(srch, fn);
961 } else if(!wcscmp(argv[i], L"prio")) {
964 sq(sk, 0, L"501", L"No argument to prio", NULL);
968 srch->prio = wcstol(argv[i], NULL, 0);
969 } else if(iswdigit(*argv[i])) {
970 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
972 sq(sk, 0, L"510", L"No such node", NULL);
976 searchaddfn(srch, fn);
981 if(srch->fnl == NULL)
983 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
989 sq(sk, 0, L"501", L"No search expression found on line", NULL);
993 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
995 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
1000 getsexpr(srch->sexpr = sexpr);
1002 CBREG(srch, search_eta, srcheta, NULL, NULL);
1003 CBREG(srch, search_commit, srchcommit, NULL, NULL);
1004 CBREG(srch, search_result, srchres, NULL, NULL);
1005 sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL);
1009 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1011 struct search *srch, *pt;
1014 havepriv(PERM_SRCH);
1017 for(srch = searches; srch != NULL; srch = srch->next)
1019 if(!wcscmp(srch->owner, data->username))
1022 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1027 sq(sk, 0, L"201", L"No searches", NULL);
1029 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1032 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1034 struct search *srch;
1038 havepriv(PERM_SRCH);
1039 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1041 sq(sk, 0, L"514", L"No such search", NULL);
1044 if(srch->results == NULL)
1046 sq(sk, 0, L"201", L"No results", NULL);
1048 for(sr = srch->results; sr != NULL; sr = sr->next)
1050 sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename,
1051 sr->fnet->name, L"%ls", sr->peerid, L"%i", sr->size,
1052 L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id),
1054 L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1059 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1061 struct search *srch;
1065 havepriv(PERM_SRCH);
1066 /* Note - Programmatical user interfaces must only give one
1067 * argument per command, the multiple argument form is only for
1068 * convenience when manually controlling the daemon via
1069 * eg. telnet. The reason is that the return codes aren't clear
1070 * enough for the multiple argument form. */
1071 for(i = 1; i < argc; i++)
1073 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1075 sq(sk, 0, L"514", L"No such search", NULL);
1080 sq(sk, 0, L"200", L"Search cancelled", NULL);
1083 static void fcmdread(struct socket *sk, struct uidata *data)
1088 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1090 bufcat(data->fcmdbuf, buf, bufsize);
1094 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1096 wchar_t *wbuf, *p, *p2;
1100 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1101 kill(-data->fcmdpid, SIGHUP);
1102 putsock(data->fcmdsk);
1103 data->fcmdsk = NULL;
1104 if(data->fcmdbuf != NULL)
1106 free(data->fcmdbuf);
1107 data->fcmdbuf = NULL;
1109 data->fcmdbufsize = data->fcmdbufdata = 0;
1110 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL);
1113 putsock(data->fcmdsk);
1114 data->fcmdsk = NULL;
1116 if(data->fcmdbuf == NULL)
1118 wbuf = swcsdup(L"");
1120 addtobuf(data->fcmdbuf, 0);
1121 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1122 free(data->fcmdbuf);
1124 data->fcmdbuf = NULL;
1125 data->fcmdbufsize = data->fcmdbufdata = 0;
1128 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1132 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1135 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL);
1141 sq(data->sk, 0, L"201", L"No data returned", NULL);
1143 sq(data->sk, 0, L"200", L"%ls", p, NULL);
1148 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1154 char *filtercmd, *argbuf;
1155 size_t cargvsize, cargvdata;
1156 struct passwd *pwent;
1159 havepriv(PERM_TRANS);
1160 if((pwent = getpwuid(data->uid)) == NULL)
1162 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1163 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1166 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1167 if(filtercmd == NULL)
1168 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1169 if(filtercmd == NULL)
1171 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1172 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1176 cargvsize = cargvdata = 0;
1177 addtobuf(cargv, filtercmd);
1178 for(i = 1; i < argc; i++)
1180 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1182 for(i = 0; i < cargvdata; i++)
1185 sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL);
1188 addtobuf(cargv, argbuf);
1190 addtobuf(cargv, NULL);
1191 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)
1193 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1194 sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL);
1199 execv(filtercmd, cargv);
1200 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1203 for(pp = cargv; *pp; pp++)
1206 data->fcmdsk = wrapsock(pipe);
1207 data->fcmdpid = pid;
1208 if(data->fcmdbuf != NULL)
1210 free(data->fcmdbuf);
1211 data->fcmdbuf = NULL;
1213 data->fcmdbufsize = data->fcmdbufdata = 0;
1214 data->fcmdsk->data = data;
1215 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1216 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1219 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1221 struct transfer *transfer;
1225 havepriv(PERM_TRANS);
1226 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1228 sq(sk, 0, L"512", L"No such transfer", NULL);
1231 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1233 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1236 if(transfer->args == NULL)
1238 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1240 for(ta = transfer->args; ta != NULL; ta = ta->next)
1241 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1245 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1247 struct sharecache *node;
1251 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1253 if(node->f.b.type == FILE_REG)
1256 if(node->f.b.hastth)
1260 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1263 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1265 havepriv(PERM_TRANS);
1266 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1269 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1274 if(data->userinfo == NULL) {
1275 sq(sk, 0, L"502", L"Must be logged in", NULL);
1278 if(argv[1][0] == L'#') {
1279 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1282 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1283 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1284 sq(sk, 0, L"516", L"Name already in use", NULL);
1288 if(data->regname != NULL)
1289 free(data->regname);
1290 data->regname = swcsdup(argv[1]);
1291 sq(sk, 0, L"200", L"Registered", NULL);
1294 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1297 struct uidata *rcpt;
1299 struct notif *notif;
1302 if(data->userinfo == NULL) {
1303 sq(sk, 0, L"502", L"Must be logged in", NULL);
1306 if(argv[1][0] == L'#') {
1307 rcptid = wcstol(argv[1] + 1, NULL, 0);
1308 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1309 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1313 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1314 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1319 sq(sk, 0, L"517", L"No such recipient", NULL);
1322 if(!rcpt->notify.b.msg) {
1323 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1326 if(data->regname != NULL)
1327 myname = swcsdup(data->regname);
1329 myname = swprintf2(L"#%i", data->id);
1330 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1331 for(i = 2; i < argc; i++)
1332 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1333 sq(sk, 0, L"200", L"Message sent", NULL);
1336 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1338 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1341 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1343 extern volatile int reinit;
1345 havepriv(PERM_ADMIN);
1346 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1348 sq(sk, 0, L"200", L"Will reinit", NULL);
1355 * Reserved command numbers for nameless commands:
1356 * 0: Issued when a client has connected
1357 * 1: Issued when a named command couldn't be found
1360 static struct command commands[] =
1362 {NULL, cmd_connect},
1363 {NULL, cmd_notfound},
1364 {L"shutdown", cmd_shutdown},
1365 {L"quit", cmd_quit},
1366 {L"lsauth", cmd_lsauth},
1367 {L"login", cmd_login},
1368 {L"pass", cmd_pass},
1369 {L"cnct", cmd_fnetconnect},
1370 {L"lsnodes", cmd_lsnodes},
1371 {L"dcnct", cmd_disconnect},
1372 {L"lspa", cmd_lspa},
1373 {L"lspeers", cmd_lspeers},
1374 {L"download", cmd_download},
1375 {L"lstrans", cmd_lstrans},
1376 {L"cancel", cmd_cancel},
1377 {L"reset", cmd_reset},
1378 {L"notify", cmd_notify},
1379 {L"sendchat", cmd_sendchat},
1380 {L"search", cmd_search},
1381 {L"lssrch", cmd_lssrch},
1382 {L"lssr", cmd_lssr},
1383 {L"cansrch", cmd_cansrch},
1384 {L"filtercmd", cmd_filtercmd},
1385 {L"lstrarg", cmd_lstrarg},
1386 {L"hashstatus", cmd_hashstatus},
1387 {L"transstatus", cmd_transstatus},
1388 {L"register", cmd_register},
1389 {L"sendmsg", cmd_sendmsg},
1390 {L"uptime", cmd_uptime},
1395 static void freequeuecmd(struct qcommand *qcmd)
1399 if(qcmd->argv != NULL)
1401 for(i = 0; i < qcmd->argc; i++)
1402 free(qcmd->argv[i]);
1408 static struct qcommand *unlinkqcmd(struct uidata *data)
1410 struct qcommand *qcmd;
1415 data->queue = qcmd->next;
1416 if(qcmd == data->queuelast)
1417 data->queuelast = qcmd->next;
1422 static void notifappendv(struct notif *notif, va_list args)
1426 while((dt = va_arg(args, int)) != NOTIF_END)
1429 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1430 notif->argv[ca].dt = dt;
1435 notif->argv[ca].d.n = va_arg(args, int);
1438 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1441 notif->argv[ca].d.d = va_arg(args, double);
1447 static void notifappend(struct notif *notif, ...)
1451 va_start(args, notif);
1452 notifappendv(notif, args);
1456 static struct notif *newnotif(struct uidata *data, int code, ...)
1458 struct notif *notif;
1461 notif = smalloc(sizeof(*notif));
1462 memset(notif, 0, sizeof(*notif));
1463 notif->rlimit = 0.0;
1466 va_start(args, code);
1467 notifappendv(notif, args);
1470 notif->prev = data->lnotif;
1471 if(data->lnotif != NULL)
1472 data->lnotif->next = notif;
1474 data->fnotif = notif;
1475 data->lnotif = notif;
1479 static void freenotif(struct notif *notif)
1483 if(notif->next != NULL)
1484 notif->next->prev = notif->prev;
1485 if(notif->prev != NULL)
1486 notif->prev->next = notif->next;
1487 if(notif == notif->ui->fnotif)
1488 notif->ui->fnotif = notif->next;
1489 if(notif == notif->ui->lnotif)
1490 notif->ui->lnotif = notif->prev;
1491 if(notif->exptimer != NULL)
1492 canceltimer(notif->exptimer);
1493 for(i = 0; i < notif->argc; i++)
1495 if(notif->argv[i].dt == NOTIF_STR)
1496 free(notif->argv[i].d.s);
1498 if(notif->argv != NULL)
1503 static void notifexpire(int cancelled, struct notif *notif)
1505 notif->exptimer = NULL;
1510 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1514 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1516 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1521 for(i = 0; i < notif->argc; i++)
1523 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1538 static void freeuidata(struct uidata *data)
1541 struct qcommand *qcmd;
1543 if(data->next != NULL)
1544 data->next->prev = data->prev;
1545 if(data->prev != NULL)
1546 data->prev->next = data->next;
1548 actives = data->next;
1549 data->sk->readcb = NULL;
1550 data->sk->errcb = NULL;
1552 while((qcmd = unlinkqcmd(data)) != NULL)
1554 iconv_close(data->ichandle);
1555 if(data->cw != NULL)
1557 if(data->cb != NULL)
1559 if(data->argv != NULL)
1561 for(i = 0; i < data->argc; i++)
1562 free(data->argv[i]);
1565 if(data->auth != NULL)
1566 authputhandle(data->auth);
1567 if(data->regname != NULL)
1568 free(data->regname);
1569 if(data->username != NULL)
1571 if(data->userinfo != NULL)
1572 flog(LOG_INFO, "%ls logged out", data->username);
1573 free(data->username);
1576 while(data->fnotif != NULL)
1577 freenotif(data->fnotif);
1578 if(data->fcmdbuf != NULL)
1579 free(data->fcmdbuf);
1580 if(data->fcmdpid != 0)
1581 kill(-data->fcmdpid, SIGHUP);
1582 if(data->fcmdsk != NULL)
1583 putsock(data->fcmdsk);
1587 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1589 struct qcommand *new;
1591 new = smalloc(sizeof(*new));
1596 if(data->queuelast != NULL)
1597 data->queuelast->next = new;
1598 data->queuelast = new;
1599 if(data->queue == NULL)
1603 static struct uidata *newuidata(struct socket *sk)
1605 struct uidata *data;
1606 static int curid = 0;
1608 data = smalloc(sizeof(*data));
1609 memset(data, 0, sizeof(*data));
1613 data->inbuf = smalloc(1024);
1615 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1617 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1620 data->next = actives;
1623 actives->prev = data;
1628 static void uiread(struct socket *sk, struct uidata *data)
1634 size_t datalen, len2;
1635 struct command *cur;
1637 if(data->indata > 1024)
1639 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1641 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1642 memcpy(data->inbuf + data->indata, newbuf, datalen);
1644 data->indata += datalen;
1645 if(data->cb == NULL)
1647 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1649 data->pp = data->cb;
1654 if(data->cbsize == data->cbdata)
1656 len2 = data->pp - data->cb;
1657 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1658 data->pp = data->cb + len2;
1661 p2 = (char *)(porig = (data->cb + data->cbdata));
1662 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1663 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1664 memmove(data->inbuf, p1, data->indata);
1665 /* Just a sanity check */
1666 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1668 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1671 data->cbdata += (((wchar_t *)p2) - porig);
1677 /* XXX: Should this really just ignore it? */
1685 /* Just a sanity check */
1686 if(data->cbsize != data->cbdata)
1688 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1693 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1701 while(!done && (data->pp - data->cb < data->cbdata))
1706 if(iswspace(*data->pp))
1708 if(*data->pp == L'\r')
1710 if(data->pp == data->cb + data->cbdata - 1)
1715 if(*(++data->pp) == L'\n')
1717 if((data->argv != NULL) && (data->argv[0] != NULL))
1719 for(cur = commands; cur->handler != NULL; cur++)
1721 if(cur->name == NULL)
1723 if(!wcscasecmp(cur->name, data->argv[0]))
1725 queuecmd(data, cur, data->argc, data->argv);
1729 if(cur->handler == NULL)
1730 queuecmd(data, &commands[1], data->argc, data->argv);
1732 queuecmd(data, &commands[1], data->argc, data->argv);
1737 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1738 data->pp = data->cb;
1751 if(iswspace(*data->pp))
1753 addtobuf(data->cw, L'\0');
1754 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1755 data->argv[data->argc++] = data->cw;
1760 } else if(*data->pp == L'\"') {
1763 } else if(*data->pp == L'\\') {
1764 if(data->pp == data->cb + data->cbdata - 1)
1769 addtobuf(data->cw, *(++data->pp));
1772 addtobuf(data->cw, *(data->pp++));
1776 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)));
1787 addtobuf(data->cw, *data->pp);
1795 static void uierror(struct socket *sk, int err, struct uidata *data)
1798 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1802 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1804 struct uidata *uidata;
1806 newsk->data = uidata = newuidata(newsk);
1807 socksettos(newsk, confgetint("ui", "uitos"));
1810 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1811 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1812 queuecmd(uidata, &commands[0], 0, NULL);
1815 static int srcheta(struct search *srch, void *uudata)
1817 struct uidata *data;
1819 for(data = actives; data != NULL; data = data->next)
1821 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1822 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1827 static int srchcommit(struct search *srch, void *uudata)
1829 struct uidata *data;
1831 for(data = actives; data != NULL; data = data->next)
1833 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1834 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1839 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1841 struct uidata *data;
1843 for(data = actives; data != NULL; data = data->next)
1845 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1847 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1848 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);
1854 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1856 struct uidata *data;
1858 for(data = actives; data != NULL; data = data->next)
1860 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1861 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1866 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1868 struct uidata *data;
1869 struct notif *notif;
1871 if(!wcscmp(attrib, L"state"))
1873 for(data = actives; data != NULL; data = data->next)
1875 if(data->notify.b.fnact)
1876 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1878 } else if(!wcscmp(attrib, L"name")) {
1879 for(data = actives; data != NULL; data = data->next)
1881 if(data->notify.b.fnact)
1882 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1884 } else if(!wcscmp(attrib, L"numpeers")) {
1885 for(data = actives; data != NULL; data = data->next)
1887 if(data->notify.b.fnact)
1889 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1890 notif->argv[1].d.n = fn->numpeers;
1892 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1899 static int fnunlink(struct fnetnode *fn, void *uudata)
1901 struct uidata *data;
1903 for(data = actives; data != NULL; data = data->next)
1905 if(data->notify.b.fnact)
1906 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1911 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1913 struct uidata *data;
1915 for(data = actives; data != NULL; data = data->next)
1917 if(data->notify.b.fnpeer)
1918 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1923 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1925 struct uidata *data;
1927 for(data = actives; data != NULL; data = data->next)
1929 if(data->notify.b.fnpeer)
1930 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1935 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1937 struct uidata *data;
1938 struct notif *notif;
1941 for(data = actives; data != NULL; data = data->next)
1943 if(data->notify.b.fnpeer)
1945 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1947 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1951 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1952 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1953 switch(di->datum->datatype)
1956 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1959 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1962 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1963 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1971 static int newfnetnode(struct fnetnode *fn, void *uudata)
1973 struct uidata *data;
1975 for(data = actives; data != NULL; data = data->next)
1977 if(data->notify.b.fnact)
1978 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1980 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1981 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1982 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1983 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1984 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1985 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
1989 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1991 struct uidata *data;
1993 if(!wcscmp(attrib, L"state"))
1995 for(data = actives; data != NULL; data = data->next)
1997 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1998 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2000 } else if(!wcscmp(attrib, L"nick")) {
2001 for(data = actives; data != NULL; data = data->next)
2003 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2004 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2006 } else if(!wcscmp(attrib, L"size")) {
2007 for(data = actives; data != NULL; data = data->next)
2009 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2010 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
2012 } else if(!wcscmp(attrib, L"error")) {
2013 for(data = actives; data != NULL; data = data->next)
2015 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2016 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2018 } else if(!wcscmp(attrib, L"path")) {
2019 for(data = actives; data != NULL; data = data->next)
2021 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2022 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2024 } else if(!wcscmp(attrib, L"hash")) {
2025 for(data = actives; data != NULL; data = data->next)
2027 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2028 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2034 static int transferprog(struct transfer *transfer, void *uudata)
2036 struct uidata *data;
2037 struct notif *notif;
2039 for(data = actives; data != NULL; data = data->next)
2041 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2043 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2044 notif->argv[1].d.n = transfer->curpos;
2046 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2052 static int transferdestroyed(struct transfer *transfer, void *uudata)
2054 struct uidata *data;
2056 for(data = actives; data != NULL; data = data->next)
2058 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2059 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2064 static int newtransfernotify(struct transfer *transfer, void *uudata)
2066 struct uidata *data;
2068 for(data = actives; data != NULL; data = data->next)
2070 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2071 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);
2073 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2074 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2075 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2079 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2083 new = smalloc(sizeof(*new));
2085 new->name = swcsdup(name);
2096 static void freeuser(struct uiuser *user)
2098 if(user->next != NULL)
2099 user->next->prev = user->prev;
2100 if(user->prev != NULL)
2101 user->prev->next = user->next;
2108 static int conf_user(int argc, wchar_t **argv)
2110 int i, perms, permmod;
2111 struct uiuser *user;
2116 flog(LOG_WARNING, "not enough arguments given for user command");
2120 for(i = 2; i < argc; i++)
2122 if(!iswalpha(argv[i][0]))
2126 if(!wcscmp(p, L"disallow"))
2127 permmod = PERM_DISALLOW;
2128 if(!wcscmp(p, L"admin"))
2129 permmod = PERM_ADMIN;
2130 if(!wcscmp(p, L"fnetctl"))
2131 permmod = PERM_FNETCTL;
2132 if(!wcscmp(p, L"trans"))
2133 permmod = PERM_TRANS;
2134 if(!wcscmp(p, L"transcu"))
2135 permmod = PERM_TRANSCU;
2136 if(!wcscmp(p, L"chat"))
2137 permmod = PERM_CHAT;
2138 if(!wcscmp(p, L"srch"))
2139 permmod = PERM_SRCH;
2140 if(!wcscmp(p, L"all"))
2142 if(argv[i][0] == L'-')
2147 if((user = finduser(argv[1])) == NULL)
2149 newuser(argv[1], perms);
2152 user->perms = perms;
2157 static void preinit(int hup)
2159 struct uiuser *user;
2163 newuser(L"default", PERM_DISALLOW);
2165 for(user = users; user != NULL; user = user->next)
2167 if(!wcscmp(user->name, L"default"))
2173 static struct sockaddr_un *makeunixname(void)
2175 static struct sockaddr_un buf;
2180 memset(&buf, 0, sizeof(buf));
2181 buf.sun_family = PF_UNIX;
2182 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2183 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2186 if(!strcmp(val, "none"))
2188 if(!strcmp(val, "default"))
2190 if((uid = getuid()) == 0)
2192 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2195 if((pwd = getpwuid(uid)) == NULL)
2197 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2200 strcpy(buf.sun_path, "/tmp/doldacond-");
2201 strcat(buf.sun_path, pwd->pw_name);
2205 if(strchr(val, '/'))
2207 strcpy(buf.sun_path, val);
2210 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2214 static int tcpportupdate(struct configvar *var, void *uudata)
2216 struct socket *newsock;
2219 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2221 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2224 if(tcpsocket != NULL)
2229 tcpsocket = newsock;
2233 static int unixsockupdate(struct configvar *var, void *uudata)
2235 struct socket *newsock;
2236 struct sockaddr_un *un;
2241 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2244 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2248 if(unixsocket != NULL)
2250 putsock(unixsocket);
2253 unixsocket = newsock;
2257 static int init(int hup)
2259 struct uiuser *user, *next;
2260 struct sockaddr_un *un;
2267 for(user = users; user != NULL; user = next)
2276 starttime = time(NULL);
2277 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2279 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2282 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2284 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2287 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2291 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2292 GCBREG(newfncb, newfnetnode, NULL);
2293 GCBREG(newtransfercb, newtransfernotify, NULL);
2297 for(user = users; user != NULL; user = user->next)
2299 if(wcscmp(user->name, L"default"))
2304 if((pwd = getpwuid(getuid())) == NULL)
2306 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2309 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2311 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2314 newuser(wcsname, ~PERM_DISALLOW);
2321 static int run(void)
2324 struct uidata *data, *next;
2325 struct qcommand *qcmd;
2326 struct notif *notif, *nnotif;
2328 for(data = actives; data != NULL; data = next)
2334 for(data = actives; data != NULL; data = data->next)
2336 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2338 nnotif = notif->next;
2339 if(notif->state == NOTIF_WAIT)
2342 for(i = 0; i < notif->argc; i++)
2344 if(notif->argv[i].dt == NOTIF_ID)
2346 id = notif->argv[i].d.n;
2350 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2352 sq(data->sk, 2, L"%i", notif->code, NULL);
2353 for(i = 0; i < notif->argc; i++)
2355 switch(notif->argv[i].dt)
2359 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2362 if(notif->argv[i].d.s[0] == L'%')
2363 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2365 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2368 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2372 sq(data->sk, 0, NULL);
2373 if(notif->rlimit != 0)
2375 notif->state = NOTIF_WAIT;
2376 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2381 if((qcmd = unlinkqcmd(data)) != NULL)
2383 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2391 static void terminate(void)
2393 while(users != NULL)
2395 if(tcpsocket != NULL)
2397 if(unixsocket != NULL)
2398 putsock(unixsocket);
2401 static struct configvar myvars[] =
2403 /** If true, UI connections will only be accepted from localhost
2404 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2405 * completely sure that you know what you are doing, never turn
2406 * this off when auth.authless is on. */
2407 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2408 /** The TCP port number on which to accept UI client connections,
2409 * or -1 to not listen on TCP. */
2410 {CONF_VAR_INT, "port", {.num = 1500}},
2412 * Controls the the name to use for the Unix socket on which to
2413 * accept UI client connections. If the name contains a slash, it
2414 * is treated as a file name to bind on. If the name is "default",
2415 * the file name will be "/var/run/doldacond.sock" if doldacond
2416 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2417 * NAME is the user name of the UID which doldacond runs as. If
2418 * the name is "none", no Unix socket will be used. Otherwise, an
2419 * error is signaled.
2421 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2422 /** The TOS value to use for UI connections (see the TOS VALUES
2424 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2425 /** The name of the filtercmd script (see the FILES section for
2426 * lookup information). */
2427 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2431 static struct configcmd mycmds[] =
2433 {"user", conf_user},
2437 static struct module me =
2448 .terminate = terminate