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)
230 * This kludge demands that all arguments that you call it
231 * with are the size of an int. That happens to be the
232 * case for most datatypes on most platforms and
233 * compilers, but I don't know exactly which ones, and
234 * also a long long is a notable candidate of an arg that
235 * is not the size of an int on 32-bit archs. If it breaks
236 * some existing call on your architecture, please tell
239 part = vswprintf2(tpart = (part + 1), al);
240 for(; *tpart != L'\0'; tpart++)
254 if((tpart = quoteword(part)) != NULL)
261 if((num > 1) || ((num == 1) && !(cont & 1)))
263 bufcat(buf, part, wcslen(part));
264 if((num == 0) && (cont & 1))
271 bufcat(buf, L" \0", 2);
273 bufcat(buf, L"\r\n\0", 3);
274 if((final = icwcstombs(buf, "utf-8")) == NULL)
276 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
282 sockqueue(sk, final, strlen(final));
286 struct uiuser *finduser(wchar_t *name)
290 for(user = users; user != NULL; user = user->next)
292 if(!wcscmp(user->name, name))
298 static void logout(struct uidata *data)
300 data->userinfo = NULL;
301 if(data->username != NULL)
302 free(data->username);
303 data->username = NULL;
304 if(data->auth != NULL)
305 authputhandle(data->auth);
309 static int haspriv(struct uidata *data, int perm)
311 if(data->userinfo == NULL)
313 if(data->userinfo->perms & perm)
319 /* Useful macros for the command functions: */
320 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
321 #define havepriv(p) do { if((data->userinfo == NULL) || ((data->userinfo->perms & (p)) != (p))) { sq(sk, 0, L"502", L"%Unauthorized request - %x needed", (p), NULL); return; } } while(0)
323 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
326 struct in6_addr mv4lo;
328 if(confgetint("ui", "onlylocal"))
330 switch(sk->remote->sa_family)
333 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
336 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
338 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
340 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
352 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
358 sq(sk, 0, L"200", L"%Dolda Connect daemon v%s", VERSION, NULL);
361 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
363 if((argv != NULL) && (argv[0] != NULL))
364 sq(sk, 0, L"500", L"%Command not found: %ls", argv[0], NULL);
366 sq(sk, 0, L"500", L"No command", NULL);
369 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
371 extern volatile int running;
373 havepriv(PERM_ADMIN);
374 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
376 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
379 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
381 sq(sk, 0, L"200", L"Closing connection", NULL);
385 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
387 struct authmech *mech, *prev;
390 for(mech = mechs; mech != NULL; mech = mech->next)
395 sq(sk, 1, L"200", prev->name, NULL);
400 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
402 sq(sk, 0, L"200", prev->name, NULL);
405 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
412 if(data->username != NULL)
414 if(data->userinfo != NULL)
415 sq(sk, 0, L"503", L"Already logged in", NULL);
417 sq(sk, 0, L"503", L"Already logging in", NULL);
420 if((buf = icwcstombs(argv[2], NULL)) == NULL)
422 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
425 data->username = swcsdup(argv[2]);
426 if((pwd = getpwnam(buf)) == NULL)
429 data->uid = pwd->pw_uid;
430 if((data->auth = initauth(argv[1], buf)) == NULL)
433 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
435 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%%s", strerror(errno), NULL);
441 switch(authenticate(data->auth, NULL))
444 data->userinfo = finduser(data->username);
445 if(data->userinfo == NULL)
446 data->userinfo = finduser(L"default");
449 sq(sk, 0, L"506", L"Authentication error", NULL);
450 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
452 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
453 sq(sk, 0, L"506", L"Authentication error", NULL);
454 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
457 sq(sk, 0, L"200", L"Welcome", NULL);
458 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
462 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
463 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
467 switch(data->auth->prompt)
485 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
488 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
492 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
493 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
499 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
505 if((buf = icwcstombs(argv[1], NULL)) == NULL)
507 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
510 if((data->auth == NULL) || (data->userinfo != NULL))
512 sq(sk, 0, L"507", L"Data not expected", NULL);
515 switch(authenticate(data->auth, buf))
518 data->userinfo = finduser(data->username);
519 if(data->userinfo == NULL)
520 data->userinfo = finduser(L"default");
523 sq(sk, 0, L"506", L"Authentication error", NULL);
524 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
526 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
527 sq(sk, 0, L"506", L"Authentication error", NULL);
528 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
531 sq(sk, 0, L"200", L"Welcome", NULL);
532 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
536 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
537 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
541 switch(data->auth->prompt)
559 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
562 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
566 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
567 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
574 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
580 struct wcspair *args;
583 havepriv(PERM_FNETCTL);
584 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
585 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
586 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
589 if((buf = icwcstombs(argv[2], NULL)) == NULL)
591 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
595 for(i = 3; i < argc - 1; i += 2)
596 newwcspair(argv[i], argv[i + 1], &args);
597 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
602 if(errno == EPROTONOSUPPORT)
603 sq(sk, 0, L"511", L"No such network name", NULL);
605 sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
609 fnetsetname(fn, argv[2]);
610 sq(sk, 0, L"200", L"%%i", fn->id, L"Connection under way", NULL);
614 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
618 if(fnetnodes == NULL)
620 sq(sk, 0, L"201", L"No connected nodes", NULL);
623 for(fn = fnetnodes; fn != NULL; fn = fn->next)
625 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);
629 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
635 havepriv(PERM_FNETCTL);
636 /* Note - Programmatical user interfaces must only give one
637 * argument per command, the multiple argument form is only for
638 * convenience when manually controlling the daemon via
639 * eg. telnet. The reason is that the return codes aren't clear
640 * enough for the multiple argument form. */
641 for(i = 1; i < argc; i++)
643 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
645 sq(sk, 0, L"510", L"No such node", NULL);
648 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
650 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
656 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
659 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
662 struct fnetpeerdatum *datum;
665 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
667 sq(sk, 0, L"510", L"No such node", NULL);
670 if(fn->peerdata == NULL)
672 sq(sk, 0, L"201", L"No data available", NULL);
674 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
675 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%%i", datum->datatype, NULL);
679 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
683 struct fnetpeer *peer;
687 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
689 sq(sk, 0, L"510", L"No such node", NULL);
692 if(fn->peers == NULL)
694 sq(sk, 0, L"201", L"No peers avaiable", NULL);
696 for(peer = fn->peers; peer != NULL; peer = peer->next)
698 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%%ls", peer->id, L"%%ls", peer->nick, NULL);
699 for(i = 0; i < peer->dinum; i++)
701 if(peer->peerdi[i].datum->datatype == FNPD_INT)
702 sq(sk, 2, peer->peerdi[i].datum->id, L"%%i", peer->peerdi[i].data.num, NULL);
703 /* Note: A long long is not the size of an int, so
704 * sq() can't handle the conversion itself. */
705 if(peer->peerdi[i].datum->datatype == FNPD_LL)
707 swprintf(buf, 40, L"%lli", peer->peerdi[i].data.lnum);
708 sq(sk, 2, peer->peerdi[i].datum->id, buf, 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_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
848 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
851 for(i = 1; i < argc; i += 2)
853 if(!wcscasecmp(argv[i + 1], L"on"))
857 if(!wcscasecmp(argv[i], L"all"))
863 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
864 data->notify.b.fnchat = val;
865 } else if(!wcscasecmp(argv[i], L"fn:act")) {
866 data->notify.b.fnact = val;
867 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
868 data->notify.b.fnpeer = val;
869 } else if(!wcscasecmp(argv[i], L"trans:act")) {
870 data->notify.b.tract = val;
871 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
872 data->notify.b.trprog = val;
873 } else if(!wcscasecmp(argv[i], L"srch:act")) {
874 data->notify.b.srch = val;
875 } else if(!wcscasecmp(argv[i], L"msg")) {
876 data->notify.b.msg = val;
879 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
882 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
889 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
891 sq(sk, 0, L"510", L"No such node", NULL);
894 public = wcstol(argv[2], NULL, 0);
895 if((public != 0) && (public != 1))
897 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
900 if(fn->state != FNN_EST)
902 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
905 if(fnetsendchat(fn, public, argv[3], argv[4]))
908 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
909 else if(errno == EPERM)
910 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
911 else if(errno == EILSEQ)
912 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
914 sq(sk, 0, L"505", L"Could not chat", NULL);
917 sq(sk, 0, L"200", L"Chat string sent", NULL);
920 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
929 srch = newsearch(data->username, NULL);
930 for(i = 1; i < argc; i++)
932 if(!wcscmp(argv[i], L"all"))
934 for(fn = fnetnodes; fn != NULL; fn = fn->next)
936 if(fn->state == FNN_EST)
937 searchaddfn(srch, fn);
941 } else if(!wcscmp(argv[i], L"prio")) {
944 sq(sk, 0, L"501", L"No argument to prio", NULL);
948 srch->prio = wcstol(argv[i], NULL, 0);
949 } else if(iswdigit(*argv[i])) {
950 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
952 sq(sk, 0, L"510", L"No such node", NULL);
956 searchaddfn(srch, fn);
961 if(srch->fnl == NULL)
963 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
969 sq(sk, 0, L"501", L"No search expression found on line", NULL);
973 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
975 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
980 getsexpr(srch->sexpr = sexpr);
982 CBREG(srch, search_eta, srcheta, NULL, NULL);
983 CBREG(srch, search_commit, srchcommit, NULL, NULL);
984 CBREG(srch, search_result, srchres, NULL, NULL);
985 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
989 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
991 struct search *srch, *pt;
997 for(srch = searches; srch != NULL; srch = srch->next)
999 if(!wcscmp(srch->owner, data->username))
1002 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
1007 sq(sk, 0, L"201", L"No searches", NULL);
1009 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
1012 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1014 struct search *srch;
1019 havepriv(PERM_SRCH);
1020 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1022 sq(sk, 0, L"514", L"No such search", NULL);
1025 if(srch->results == NULL)
1027 sq(sk, 0, L"201", L"No results", NULL);
1029 for(sr = srch->results; sr != NULL; sr = sr->next)
1031 swprintf(buf, 64, L"%f", sr->time);
1032 sq(sk, (sr->next != NULL)?1:0, L"200", L"%%ls", sr->filename, sr->fnet->name, L"%%ls", sr->peerid, L"%%i", sr->size, L"%%i", sr->slots, L"%%i", (sr->fn == NULL)?-1:(sr->fn->id), buf, L"%%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1037 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1039 struct search *srch;
1043 havepriv(PERM_SRCH);
1044 /* Note - Programmatical user interfaces must only give one
1045 * argument per command, the multiple argument form is only for
1046 * convenience when manually controlling the daemon via
1047 * eg. telnet. The reason is that the return codes aren't clear
1048 * enough for the multiple argument form. */
1049 for(i = 1; i < argc; i++)
1051 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1053 sq(sk, 0, L"514", L"No such search", NULL);
1058 sq(sk, 0, L"200", L"Search cancelled", NULL);
1061 static void fcmdread(struct socket *sk, struct uidata *data)
1066 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1068 bufcat(data->fcmdbuf, buf, bufsize);
1072 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1074 wchar_t *wbuf, *p, *p2;
1078 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1079 kill(-data->fcmdpid, SIGHUP);
1080 putsock(data->fcmdsk);
1081 data->fcmdsk = NULL;
1082 if(data->fcmdbuf != NULL)
1084 free(data->fcmdbuf);
1085 data->fcmdbuf = NULL;
1087 data->fcmdbufsize = data->fcmdbufdata = 0;
1088 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1091 putsock(data->fcmdsk);
1092 data->fcmdsk = NULL;
1094 if(data->fcmdbuf == NULL)
1096 wbuf = swcsdup(L"");
1098 addtobuf(data->fcmdbuf, 0);
1099 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1100 free(data->fcmdbuf);
1102 data->fcmdbuf = NULL;
1103 data->fcmdbufsize = data->fcmdbufdata = 0;
1106 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1110 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1113 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1119 sq(data->sk, 0, L"201", L"No data returned", NULL);
1121 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1126 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1132 char *filtercmd, *argbuf;
1133 size_t cargvsize, cargvdata;
1134 struct passwd *pwent;
1137 havepriv(PERM_TRANS);
1138 if((pwent = getpwuid(data->uid)) == NULL)
1140 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1141 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1144 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1145 if(filtercmd == NULL)
1146 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1147 if(filtercmd == NULL)
1149 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1150 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1154 cargvsize = cargvdata = 0;
1155 addtobuf(cargv, filtercmd);
1156 for(i = 1; i < argc; i++)
1158 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1160 for(i = 0; i < cargvdata; i++)
1163 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1166 addtobuf(cargv, argbuf);
1168 addtobuf(cargv, NULL);
1169 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)
1171 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1172 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1177 execv(filtercmd, cargv);
1178 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1181 for(pp = cargv; *pp; pp++)
1184 data->fcmdsk = wrapsock(pipe);
1185 data->fcmdpid = pid;
1186 if(data->fcmdbuf != NULL)
1188 free(data->fcmdbuf);
1189 data->fcmdbuf = NULL;
1191 data->fcmdbufsize = data->fcmdbufdata = 0;
1192 data->fcmdsk->data = data;
1193 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1194 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1197 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1199 struct transfer *transfer;
1203 havepriv(PERM_TRANS);
1204 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1206 sq(sk, 0, L"512", L"No such transfer", NULL);
1209 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1211 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1214 if(transfer->args == NULL)
1216 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1218 for(ta = transfer->args; ta != NULL; ta = ta->next)
1219 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->key, L"%%ls", ta->val, NULL);
1223 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1225 struct sharecache *node;
1229 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1231 if(node->f.b.type == FILE_REG)
1234 if(node->f.b.hastth)
1238 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1241 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1243 wchar_t *buf1, *buf2;
1245 havepriv(PERM_TRANS);
1246 buf1 = swprintf2(L"%lli", bytesdownload);
1247 buf2 = swprintf2(L"%lli", bytesupload);
1248 sq(sk, 0, L"200", L"down", L"%%ls", buf1, L"up", L"%%ls", buf2, NULL);
1253 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1258 if(data->userinfo == NULL) {
1259 sq(sk, 0, L"502", L"Must be logged in", NULL);
1262 if(argv[1][0] == L'#') {
1263 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1266 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1267 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1268 sq(sk, 0, L"516", L"Name already in use", NULL);
1272 if(data->regname != NULL)
1273 free(data->regname);
1274 data->regname = swcsdup(argv[1]);
1275 sq(sk, 0, L"200", L"Registered", NULL);
1278 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1281 struct uidata *rcpt;
1283 struct notif *notif;
1286 if(data->userinfo == NULL) {
1287 sq(sk, 0, L"502", L"Must be logged in", NULL);
1290 if(argv[1][0] == L'#') {
1291 rcptid = wcstol(argv[1] + 1, NULL, 0);
1292 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1293 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1297 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1298 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1303 sq(sk, 0, L"517", L"No such recipient", NULL);
1306 if(!rcpt->notify.b.msg) {
1307 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1310 if(data->regname != NULL)
1311 myname = swcsdup(data->regname);
1313 myname = swprintf2(L"#%i", data->id);
1314 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1315 for(i = 2; i < argc; i++)
1316 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1317 sq(sk, 0, L"200", L"Message sent", NULL);
1320 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1322 sq(sk, 0, L"200", L"%%i", time(NULL) - starttime, NULL);
1329 * Reserved command numbers for nameless commands:
1330 * 0: Issued when a client has connected
1331 * 1: Issued when a named command couldn't be found
1334 static struct command commands[] =
1336 {NULL, cmd_connect},
1337 {NULL, cmd_notfound},
1338 {L"shutdown", cmd_shutdown},
1339 {L"quit", cmd_quit},
1340 {L"lsauth", cmd_lsauth},
1341 {L"login", cmd_login},
1342 {L"pass", cmd_pass},
1343 {L"cnct", cmd_fnetconnect},
1344 {L"lsnodes", cmd_lsnodes},
1345 {L"dcnct", cmd_disconnect},
1346 {L"lspa", cmd_lspa},
1347 {L"lspeers", cmd_lspeers},
1348 {L"download", cmd_download},
1349 {L"lstrans", cmd_lstrans},
1350 {L"cancel", cmd_cancel},
1351 {L"notify", cmd_notify},
1352 {L"sendchat", cmd_sendchat},
1353 {L"search", cmd_search},
1354 {L"lssrch", cmd_lssrch},
1355 {L"lssr", cmd_lssr},
1356 {L"cansrch", cmd_cansrch},
1357 {L"filtercmd", cmd_filtercmd},
1358 {L"lstrarg", cmd_lstrarg},
1359 {L"hashstatus", cmd_hashstatus},
1360 {L"transstatus", cmd_transstatus},
1361 {L"register", cmd_register},
1362 {L"sendmsg", cmd_sendmsg},
1363 {L"uptime", cmd_uptime},
1367 static void freequeuecmd(struct qcommand *qcmd)
1371 if(qcmd->argv != NULL)
1373 for(i = 0; i < qcmd->argc; i++)
1374 free(qcmd->argv[i]);
1380 static struct qcommand *unlinkqcmd(struct uidata *data)
1382 struct qcommand *qcmd;
1387 data->queue = qcmd->next;
1388 if(qcmd == data->queuelast)
1389 data->queuelast = qcmd->next;
1394 static void notifappendv(struct notif *notif, va_list args)
1398 while((dt = va_arg(args, int)) != NOTIF_END)
1401 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1402 notif->argv[ca].dt = dt;
1407 notif->argv[ca].d.n = va_arg(args, int);
1410 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1413 notif->argv[ca].d.d = va_arg(args, double);
1419 static void notifappend(struct notif *notif, ...)
1423 va_start(args, notif);
1424 notifappendv(notif, args);
1428 static struct notif *newnotif(struct uidata *data, int code, ...)
1430 struct notif *notif;
1433 notif = smalloc(sizeof(*notif));
1434 memset(notif, 0, sizeof(*notif));
1435 notif->rlimit = 0.0;
1438 va_start(args, code);
1439 notifappendv(notif, args);
1442 notif->prev = data->lnotif;
1443 if(data->lnotif != NULL)
1444 data->lnotif->next = notif;
1446 data->fnotif = notif;
1447 data->lnotif = notif;
1451 static void freenotif(struct notif *notif)
1455 if(notif->next != NULL)
1456 notif->next->prev = notif->prev;
1457 if(notif->prev != NULL)
1458 notif->prev->next = notif->next;
1459 if(notif == notif->ui->fnotif)
1460 notif->ui->fnotif = notif->next;
1461 if(notif == notif->ui->lnotif)
1462 notif->ui->lnotif = notif->prev;
1463 if(notif->exptimer != NULL)
1464 canceltimer(notif->exptimer);
1465 for(i = 0; i < notif->argc; i++)
1467 if(notif->argv[i].dt == NOTIF_STR)
1468 free(notif->argv[i].d.s);
1470 if(notif->argv != NULL)
1475 static void notifexpire(int cancelled, struct notif *notif)
1477 notif->exptimer = NULL;
1482 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1486 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1488 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1493 for(i = 0; i < notif->argc; i++)
1495 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1510 static void freeuidata(struct uidata *data)
1513 struct qcommand *qcmd;
1515 if(data->next != NULL)
1516 data->next->prev = data->prev;
1517 if(data->prev != NULL)
1518 data->prev->next = data->next;
1520 actives = data->next;
1521 data->sk->readcb = NULL;
1522 data->sk->errcb = NULL;
1524 while((qcmd = unlinkqcmd(data)) != NULL)
1526 iconv_close(data->ichandle);
1527 if(data->cw != NULL)
1529 if(data->cb != NULL)
1531 if(data->argv != NULL)
1533 for(i = 0; i < data->argc; i++)
1534 free(data->argv[i]);
1537 if(data->auth != NULL)
1538 authputhandle(data->auth);
1539 if(data->regname != NULL)
1540 free(data->regname);
1541 if(data->username != NULL)
1543 if(data->userinfo != NULL)
1544 flog(LOG_INFO, "%ls logged out", data->username);
1545 free(data->username);
1548 while(data->fnotif != NULL)
1549 freenotif(data->fnotif);
1550 if(data->fcmdbuf != NULL)
1551 free(data->fcmdbuf);
1552 if(data->fcmdpid != 0)
1553 kill(-data->fcmdpid, SIGHUP);
1554 if(data->fcmdsk != NULL)
1555 putsock(data->fcmdsk);
1559 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1561 struct qcommand *new;
1563 new = smalloc(sizeof(*new));
1568 if(data->queuelast != NULL)
1569 data->queuelast->next = new;
1570 data->queuelast = new;
1571 if(data->queue == NULL)
1575 static struct uidata *newuidata(struct socket *sk)
1577 struct uidata *data;
1578 static int curid = 0;
1580 data = smalloc(sizeof(*data));
1581 memset(data, 0, sizeof(*data));
1585 data->inbuf = smalloc(1024);
1587 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1589 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1592 data->next = actives;
1595 actives->prev = data;
1600 static void uiread(struct socket *sk, struct uidata *data)
1606 size_t datalen, len2;
1607 struct command *cur;
1609 if(data->indata > 1024)
1611 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1613 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1614 memcpy(data->inbuf + data->indata, newbuf, datalen);
1616 data->indata += datalen;
1617 if(data->cb == NULL)
1619 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1621 data->pp = data->cb;
1626 if(data->cbsize == data->cbdata)
1628 len2 = data->pp - data->cb;
1629 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1630 data->pp = data->cb + len2;
1633 p2 = (char *)(porig = (data->cb + data->cbdata));
1634 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1635 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1636 memmove(data->inbuf, p1, data->indata);
1637 /* Just a sanity check */
1638 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1640 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1643 data->cbdata += (((wchar_t *)p2) - porig);
1649 /* XXX: Should this really just ignore it? */
1657 /* Just a sanity check */
1658 if(data->cbsize != data->cbdata)
1660 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1665 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1673 while(!done && (data->pp - data->cb < data->cbdata))
1678 if(iswspace(*data->pp))
1680 if(*data->pp == L'\r')
1682 if(data->pp == data->cb + data->cbdata - 1)
1687 if(*(++data->pp) == L'\n')
1689 if((data->argv != NULL) && (data->argv[0] != NULL))
1691 for(cur = commands; cur->handler != NULL; cur++)
1693 if(cur->name == NULL)
1695 if(!wcscasecmp(cur->name, data->argv[0]))
1697 queuecmd(data, cur, data->argc, data->argv);
1701 if(cur->handler == NULL)
1702 queuecmd(data, &commands[1], data->argc, data->argv);
1704 queuecmd(data, &commands[1], data->argc, data->argv);
1709 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1710 data->pp = data->cb;
1723 if(iswspace(*data->pp))
1725 addtobuf(data->cw, L'\0');
1726 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1727 data->argv[data->argc++] = data->cw;
1732 } else if(*data->pp == L'\"') {
1735 } else if(*data->pp == L'\\') {
1736 if(data->pp == data->cb + data->cbdata - 1)
1741 addtobuf(data->cw, *(++data->pp));
1744 addtobuf(data->cw, *(data->pp++));
1748 if(*data->pp == L'\"')
1751 } else if(*data->pp == L'\\') {
1752 if(data->pp == data->cb + data->cbdata - 1)
1757 addtobuf(data->cw, *(++(data->pp)));
1759 addtobuf(data->cw, *data->pp);
1767 static void uierror(struct socket *sk, int err, struct uidata *data)
1770 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1774 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1776 struct uidata *uidata;
1778 newsk->data = uidata = newuidata(newsk);
1779 socksettos(newsk, confgetint("ui", "uitos"));
1782 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1783 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1784 queuecmd(uidata, &commands[0], 0, NULL);
1787 static int srcheta(struct search *srch, void *uudata)
1789 struct uidata *data;
1791 for(data = actives; data != NULL; data = data->next)
1793 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1794 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1799 static int srchcommit(struct search *srch, void *uudata)
1801 struct uidata *data;
1803 for(data = actives; data != NULL; data = data->next)
1805 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1806 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1811 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1813 struct uidata *data;
1815 for(data = actives; data != NULL; data = data->next)
1817 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1819 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1820 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);
1826 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1828 struct uidata *data;
1830 for(data = actives; data != NULL; data = data->next)
1832 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1833 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1838 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1840 struct uidata *data;
1841 struct notif *notif;
1843 if(!wcscmp(attrib, L"state"))
1845 for(data = actives; data != NULL; data = data->next)
1847 if(data->notify.b.fnact)
1848 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1850 } else if(!wcscmp(attrib, L"name")) {
1851 for(data = actives; data != NULL; data = data->next)
1853 if(data->notify.b.fnact)
1854 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1856 } else if(!wcscmp(attrib, L"numpeers")) {
1857 for(data = actives; data != NULL; data = data->next)
1859 if(data->notify.b.fnact)
1861 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1862 notif->argv[1].d.n = fn->numpeers;
1864 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1871 static int fnunlink(struct fnetnode *fn, void *uudata)
1873 struct uidata *data;
1875 for(data = actives; data != NULL; data = data->next)
1877 if(data->notify.b.fnact)
1878 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1883 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1885 struct uidata *data;
1887 for(data = actives; data != NULL; data = data->next)
1889 if(data->notify.b.fnpeer)
1890 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1895 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1897 struct uidata *data;
1899 for(data = actives; data != NULL; data = data->next)
1901 if(data->notify.b.fnpeer)
1902 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1907 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1909 struct uidata *data;
1910 struct notif *notif;
1913 for(data = actives; data != NULL; data = data->next)
1915 if(data->notify.b.fnpeer)
1917 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1919 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1923 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1924 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1925 switch(di->datum->datatype)
1928 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1931 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1934 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1935 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1943 static int newfnetnode(struct fnetnode *fn, void *uudata)
1945 struct uidata *data;
1947 for(data = actives; data != NULL; data = data->next)
1949 if(data->notify.b.fnact)
1950 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1952 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1953 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1954 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1955 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1956 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1957 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
1961 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1963 struct uidata *data;
1965 if(!wcscmp(attrib, L"state"))
1967 for(data = actives; data != NULL; data = data->next)
1969 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1970 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1972 } else if(!wcscmp(attrib, L"nick")) {
1973 for(data = actives; data != NULL; data = data->next)
1975 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1976 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1978 } else if(!wcscmp(attrib, L"size")) {
1979 for(data = actives; data != NULL; data = data->next)
1981 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1982 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1984 } else if(!wcscmp(attrib, L"error")) {
1985 for(data = actives; data != NULL; data = data->next)
1987 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1988 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1990 } else if(!wcscmp(attrib, L"path")) {
1991 for(data = actives; data != NULL; data = data->next)
1993 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1994 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1996 } else if(!wcscmp(attrib, L"hash")) {
1997 for(data = actives; data != NULL; data = data->next)
1999 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2000 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2006 static int transferprog(struct transfer *transfer, void *uudata)
2008 struct uidata *data;
2009 struct notif *notif;
2011 for(data = actives; data != NULL; data = data->next)
2013 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2015 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2016 notif->argv[1].d.n = transfer->curpos;
2018 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2024 static int transferdestroyed(struct transfer *transfer, void *uudata)
2026 struct uidata *data;
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, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2036 static int newtransfernotify(struct transfer *transfer, void *uudata)
2038 struct uidata *data;
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, 610, NOTIF_ID, transfer->id, NOTIF_INT, transfer->dir, NOTIF_STR, transfer->peerid, NOTIF_STR, (transfer->path == NULL)?L"":transfer->path, NOTIF_END);
2045 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2046 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2047 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2051 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2055 new = smalloc(sizeof(*new));
2057 new->name = swcsdup(name);
2068 static void freeuser(struct uiuser *user)
2070 if(user->next != NULL)
2071 user->next->prev = user->prev;
2072 if(user->prev != NULL)
2073 user->prev->next = user->next;
2080 static int conf_user(int argc, wchar_t **argv)
2082 int i, perms, permmod;
2083 struct uiuser *user;
2088 flog(LOG_WARNING, "not enough arguments given for user command");
2092 for(i = 2; i < argc; i++)
2094 if(!iswalpha(argv[i][0]))
2098 if(!wcscmp(p, L"disallow"))
2099 permmod = PERM_DISALLOW;
2100 if(!wcscmp(p, L"admin"))
2101 permmod = PERM_ADMIN;
2102 if(!wcscmp(p, L"fnetctl"))
2103 permmod = PERM_FNETCTL;
2104 if(!wcscmp(p, L"trans"))
2105 permmod = PERM_TRANS;
2106 if(!wcscmp(p, L"transcu"))
2107 permmod = PERM_TRANSCU;
2108 if(!wcscmp(p, L"chat"))
2109 permmod = PERM_CHAT;
2110 if(!wcscmp(p, L"srch"))
2111 permmod = PERM_SRCH;
2112 if(!wcscmp(p, L"all"))
2114 if(argv[i][0] == L'-')
2119 if((user = finduser(argv[1])) == NULL)
2121 newuser(argv[1], perms);
2124 user->perms = perms;
2129 static void preinit(int hup)
2131 struct uiuser *user;
2135 newuser(L"default", 0);
2137 for(user = users; user != NULL; user = user->next)
2139 if(!wcscmp(user->name, L"default"))
2145 static struct sockaddr_un *makeunixname(void)
2147 static struct sockaddr_un buf;
2152 memset(&buf, 0, sizeof(buf));
2153 buf.sun_family = PF_UNIX;
2154 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2155 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2158 if(!strcmp(val, "none"))
2160 if(!strcmp(val, "default"))
2162 if((uid = getuid()) == 0)
2164 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2167 if((pwd = getpwuid(uid)) == NULL)
2169 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2172 strcpy(buf.sun_path, "/tmp/doldacond-");
2173 strcat(buf.sun_path, pwd->pw_name);
2177 if(strchr(val, '/'))
2179 strcpy(buf.sun_path, val);
2182 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2186 static int tcpportupdate(struct configvar *var, void *uudata)
2188 struct socket *newsock;
2191 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2193 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2196 if(tcpsocket != NULL)
2201 tcpsocket = newsock;
2205 static int unixsockupdate(struct configvar *var, void *uudata)
2207 struct socket *newsock;
2208 struct sockaddr_un *un;
2211 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2213 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2216 if(unixsocket != NULL)
2218 putsock(unixsocket);
2221 unixsocket = newsock;
2225 static int init(int hup)
2227 struct uiuser *user, *next;
2228 struct sockaddr_un *un;
2232 for(user = users; user != NULL; user = next)
2241 starttime = time(NULL);
2242 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2244 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2247 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2248 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2250 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2253 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2254 GCBREG(newfncb, newfnetnode, NULL);
2255 GCBREG(newtransfercb, newtransfernotify, NULL);
2260 static int run(void)
2263 struct uidata *data, *next;
2264 struct qcommand *qcmd;
2265 struct notif *notif, *nnotif;
2268 for(data = actives; data != NULL; data = next)
2274 for(data = actives; data != NULL; data = data->next)
2276 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2278 nnotif = notif->next;
2279 if(notif->state == NOTIF_WAIT)
2282 for(i = 0; i < notif->argc; i++)
2284 if(notif->argv[i].dt == NOTIF_ID)
2286 id = notif->argv[i].d.n;
2290 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2292 sq(data->sk, 2, L"%%i", notif->code, NULL);
2293 for(i = 0; i < notif->argc; i++)
2295 switch(notif->argv[i].dt)
2299 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2302 if(notif->argv[i].d.s[0] == L'%')
2303 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2305 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2308 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2309 sq(data->sk, 2, buf, NULL);
2313 sq(data->sk, 0, NULL);
2314 if(notif->rlimit != 0)
2316 notif->state = NOTIF_WAIT;
2317 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2322 if((qcmd = unlinkqcmd(data)) != NULL)
2324 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2332 static void terminate(void)
2334 while(users != NULL)
2336 if(tcpsocket != NULL)
2338 if(unixsocket != NULL)
2339 putsock(unixsocket);
2342 static struct configvar myvars[] =
2344 /** If true, UI connections will only be accepted from localhost
2345 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2346 * completely sure that you know what you are doing, never turn
2347 * this off when auth.authless is on. */
2348 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2349 /** The TCP port number on which to accept UI client connections,
2350 * or -1 to not listen on TCP. */
2351 {CONF_VAR_INT, "port", {.num = 1500}},
2353 * Controls the the name to use for the Unix socket on which to
2354 * accept UI client connections. If the name contains a slash, it
2355 * is treated as a file name to bind on. If the name is "default",
2356 * the file name will be "/var/run/doldacond.sock" if doldacond
2357 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2358 * NAME is the user name of the UID which doldacond runs as. If
2359 * the name is "none", no Unix socket will be used. Otherwise, an
2360 * error is signaled.
2362 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2363 /** The TOS value to use for UI connections (see the TOS VALUES
2365 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2366 /** The name of the filtercmd script (see the FILES section for
2367 * lookup information). */
2368 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2372 static struct configcmd mycmds[] =
2374 {"user", conf_user},
2378 static struct module me =
2389 .terminate = terminate