2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <netinet/ip6.h>
26 #include <arpa/inet.h>
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;
135 struct uiuser *userinfo;
137 struct notif *fnotif, *lnotif;
139 size_t fcmdbufdata, fcmdbufsize;
141 struct socket *fcmdsk;
144 size_t inbufsize, indata;
145 /* Wordset storage */
148 /* WCS conversation stuff */
149 wchar_t *cb; /* Conversation buffer */
150 size_t cbsize, cbdata;
153 int ps; /* Parser state */
154 wchar_t *pp; /* Current parse pointer */
155 wchar_t *cw; /* Current word (building storage) */
156 size_t cwsize, cwdata;
159 static int srcheta(struct search *srch, void *uudata);
160 static int srchcommit(struct search *srch, void *uudata);
161 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
163 struct uiuser *users = NULL;
164 struct uidata *actives = NULL;
165 struct socket *uisocket = NULL;
167 static wchar_t *quoteword(wchar_t *word)
169 wchar_t *wp, *buf, *bp;
179 for(wp = word; *wp != L'\0'; wp++)
181 if(!dq && iswspace(*wp))
183 if((*wp == L'\\') || (*wp == L'\"'))
190 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
193 for(wp = word; *wp != L'\0'; wp++)
195 if((*wp == L'\\') || (*wp == L'\"'))
205 static void sq(struct socket *sk, int cont, ...)
211 wchar_t *part, *tpart;
212 size_t bufsize, bufdata;
215 bufsize = bufdata = 0;
218 while((part = va_arg(al, wchar_t *)) != NULL)
223 * This kludge demands that all arguments that you call it
224 * with are the size of an int. That happens to be the
225 * case for most datatypes on most platforms and
226 * compilers, but I don't know exactly which ones, and
227 * also a long long is a notable candidate of an arg that
228 * is not the size of an int on 32-bit archs. If it breaks
229 * some existing call on your architecture, please tell
232 part = vswprintf2(tpart = (part + 1), al);
233 for(; *tpart != L'\0'; tpart++)
247 if((tpart = quoteword(part)) != NULL)
254 if((num > 1) || ((num == 1) && !(cont & 1)))
256 bufcat(buf, part, wcslen(part));
257 if((num == 0) && (cont & 1))
264 bufcat(buf, L" \0", 2);
266 bufcat(buf, L"\r\n\0", 3);
267 if((final = icwcstombs(buf, "utf-8")) == NULL)
269 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
275 sockqueue(sk, final, strlen(final));
279 struct uiuser *finduser(wchar_t *name)
283 for(user = users; user != NULL; user = user->next)
285 if(!wcscmp(user->name, name))
291 static void logout(struct uidata *data)
293 data->userinfo = NULL;
294 if(data->username != NULL)
295 free(data->username);
296 data->username = NULL;
297 if(data->auth != NULL)
298 authputhandle(data->auth);
302 static int haspriv(struct uidata *data, int perm)
304 if(data->userinfo == NULL)
306 if(data->userinfo->perms & perm)
312 /* Useful macros for the command functions: */
313 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
314 #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)
316 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
319 struct in6_addr mv4lo;
321 if(confgetint("ui", "onlylocal"))
323 switch(sk->remote->sa_family)
326 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
329 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
331 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
333 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
342 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
348 sq(sk, 0, L"200", L"%Dolda Connect daemon v%s", VERSION, NULL);
351 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
353 if((argv != NULL) && (argv[0] != NULL))
354 sq(sk, 0, L"500", L"%Command not found: %ls", argv[0], NULL);
356 sq(sk, 0, L"500", L"No command", NULL);
359 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
361 extern volatile int running;
363 havepriv(PERM_ADMIN);
364 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
366 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
369 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
371 sq(sk, 0, L"200", L"Closing connection", NULL);
375 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
377 struct authmech *mech, *prev;
380 for(mech = mechs; mech != NULL; mech = mech->next)
385 sq(sk, 1, L"200", prev->name, NULL);
390 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
392 sq(sk, 0, L"200", prev->name, NULL);
395 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
402 if(data->username != NULL)
404 if(data->userinfo != NULL)
405 sq(sk, 0, L"503", L"Already logged in", NULL);
407 sq(sk, 0, L"503", L"Already logging in", NULL);
410 if((buf = icwcstombs(argv[2], NULL)) == NULL)
412 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
415 data->username = swcsdup(argv[2]);
416 if((pwd = getpwnam(buf)) == NULL)
419 data->uid = pwd->pw_uid;
420 if((data->auth = initauth(argv[1], buf)) == NULL)
423 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
425 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%%s", strerror(errno), NULL);
431 switch(authenticate(data->auth, NULL))
434 data->userinfo = finduser(data->username);
435 if(data->userinfo == NULL)
436 data->userinfo = finduser(L"default");
439 sq(sk, 0, L"506", L"Authentication error", NULL);
440 flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
442 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
443 sq(sk, 0, L"506", L"Authentication error", NULL);
444 flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
447 sq(sk, 0, L"200", L"Welcome", NULL);
448 flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
452 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
456 switch(data->auth->prompt)
474 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
477 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
481 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
482 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
488 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
494 if((buf = icwcstombs(argv[1], NULL)) == NULL)
496 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
499 if((data->auth == NULL) || (data->userinfo != NULL))
501 sq(sk, 0, L"507", L"Data not expected", NULL);
504 switch(authenticate(data->auth, buf))
507 data->userinfo = finduser(data->username);
508 if(data->userinfo == NULL)
509 data->userinfo = finduser(L"default");
512 sq(sk, 0, L"506", L"Authentication error", NULL);
513 flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
515 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
516 sq(sk, 0, L"506", L"Authentication error", NULL);
517 flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
520 sq(sk, 0, L"200", L"Welcome", NULL);
521 flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
525 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
529 switch(data->auth->prompt)
547 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
550 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
554 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
555 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
562 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
568 struct wcspair *args;
571 havepriv(PERM_FNETCTL);
572 if((buf = icwcstombs(argv[2], NULL)) == NULL)
574 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
578 for(i = 3; i < argc - 1; i += 2)
579 newwcspair(argv[i], argv[i + 1], &args);
580 fn = fnetinitconnect(argv[1], buf, args);
585 if(errno == EPROTONOSUPPORT)
586 sq(sk, 0, L"511", L"No such network name", NULL);
588 sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
592 fnetsetname(fn, argv[2]);
594 sq(sk, 0, L"200", L"Connection under way", NULL);
597 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
601 if(fnetnodes == NULL)
603 sq(sk, 0, L"201", L"No connected nodes", NULL);
606 for(fn = fnetnodes; fn != NULL; fn = fn->next)
608 sq(sk, (fn->next != NULL)?1:0, L"200", L"%%i", fn->id, fn->fnet->name, (fn->name == NULL)?L"":fn->name, L"%%i", fn->numpeers, L"%%i", fn->state, NULL);
612 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
618 havepriv(PERM_FNETCTL);
619 /* Note - Programmatical user interfaces must only give one
620 * argument per command, the multiple argument form is only for
621 * convenience when manually controlling the daemon via
622 * eg. telnet. The reason is that the return codes aren't clear
623 * enough for the multiple argument form. */
624 for(i = 1; i < argc; i++)
626 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
628 sq(sk, 0, L"510", L"No such node", NULL);
634 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
637 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
640 struct fnetpeerdatum *datum;
643 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
645 sq(sk, 0, L"510", L"No such node", NULL);
648 if(fn->peerdata == NULL)
650 sq(sk, 0, L"201", L"No data available", NULL);
652 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
653 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%%i", datum->datatype, NULL);
657 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
661 struct fnetpeer *peer;
665 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
667 sq(sk, 0, L"510", L"No such node", NULL);
670 if(fn->peers == NULL)
672 sq(sk, 0, L"201", L"No peers avaiable", NULL);
674 for(peer = fn->peers; peer != NULL; peer = peer->next)
676 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", peer->id, peer->nick, NULL);
677 for(i = 0; i < peer->dinum; i++)
679 if(peer->peerdi[i].datum->datatype == FNPD_INT)
680 sq(sk, 2, peer->peerdi[i].datum->id, L"%%i", peer->peerdi[i].data.num, NULL);
681 /* Note: A long long is not the size of an int, so
682 * sq() can't handle the conversion itself. */
683 if(peer->peerdi[i].datum->datatype == FNPD_LL)
685 swprintf(buf, 40, L"%lli", peer->peerdi[i].data.lnum);
686 sq(sk, 2, peer->peerdi[i].datum->id, buf, NULL);
688 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
689 sq(sk, 2, peer->peerdi[i].datum->id, peer->peerdi[i].data.str, NULL);
696 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
701 struct transfer *transfer;
702 struct fnetpeer *peer;
705 if((argc > 5) && ((argc % 2) == 0))
707 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
710 havepriv(PERM_TRANS);
711 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
713 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
715 sq(sk, 0, L"510", L"No such node", NULL);
721 if((net = findfnet(argv[1])) == NULL)
723 sq(sk, 0, L"511", L"No such network name", NULL);
727 transfer = newtransfer();
728 authgethandle(transfer->auth = data->auth);
729 transfer->fnet = net;
730 transfer->peerid = swcsdup(argv[2]);
731 transfer->path = swcsdup(argv[3]);
732 transfer->dir = TRNSD_DOWN;
733 transfer->owner = data->uid;
738 linktransfer(transfer);
739 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
740 transfersetnick(transfer, peer->nick);
742 linktransfer(transfer);
745 transfersetsize(transfer, wcstol(argv[4], NULL, 0));
748 for(i = 5; i < argc; i += 2)
750 if(!wcscmp(argv[i], L"hash"))
752 transfersethash(transfer, parsehash(argv[i + 1]));
754 newwcspair(argv[i], argv[i + 1], &transfer->args);
758 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
759 transfersetactivity(transfer, L"create");
762 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
764 struct transfer *transfer, *pt;
766 havepriv(PERM_TRANS);
768 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
770 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
773 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
774 L"%%i", pt->state, pt->peerid,
775 (pt->peernick == NULL)?L"":(pt->peernick),
776 (pt->path == NULL)?L"":(pt->path),
777 L"%%i", pt->size, L"%%i", pt->curpos,
778 (pt->hash == NULL)?L"":unparsehash(pt->hash),
784 sq(sk, 0, L"201", L"No transfers", NULL);
786 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
787 L"%%i", pt->state, pt->peerid,
788 (pt->peernick == NULL)?L"":(pt->peernick),
789 (pt->path == NULL)?L"":(pt->path),
790 L"%%i", pt->size, L"%%i", pt->curpos,
791 (pt->hash == NULL)?L"":unparsehash(pt->hash),
795 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
797 struct transfer *transfer;
800 havepriv(PERM_TRANS);
801 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
803 sq(sk, 0, L"512", L"No such transfer", NULL);
806 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
808 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
811 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
813 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
817 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
820 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
826 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
829 for(i = 1; i < argc; i += 2)
831 if(!wcscasecmp(argv[i + 1], L"on"))
835 if(!wcscasecmp(argv[i], L"all"))
841 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
842 data->notify.b.fnchat = val;
843 } else if(!wcscasecmp(argv[i], L"fn:act")) {
844 data->notify.b.fnact = val;
845 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
846 data->notify.b.fnpeer = val;
847 } else if(!wcscasecmp(argv[i], L"trans:act")) {
848 data->notify.b.tract = val;
849 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
850 data->notify.b.trprog = val;
851 } else if(!wcscasecmp(argv[i], L"srch:act")) {
852 data->notify.b.srch = val;
855 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
858 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
865 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
867 sq(sk, 0, L"510", L"No such node", NULL);
870 public = wcstol(argv[2], NULL, 0);
871 if((public != 0) && (public != 1))
873 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
876 if(fn->state != FNN_EST)
878 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
881 if(fnetsendchat(fn, public, argv[3], argv[4]))
884 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
885 else if(errno == EPERM)
886 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
887 else if(errno == EILSEQ)
888 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
890 sq(sk, 0, L"505", L"Could not chat", NULL);
893 sq(sk, 0, L"200", L"Chat string sent", NULL);
896 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
905 srch = newsearch(data->username, NULL);
906 for(i = 1; i < argc; i++)
908 if(!wcscmp(argv[i], L"all"))
910 for(fn = fnetnodes; fn != NULL; fn = fn->next)
912 if(fn->state == FNN_EST)
913 searchaddfn(srch, fn);
917 } else if(!wcscmp(argv[i], L"prio")) {
920 sq(sk, 0, L"501", L"No argument to prio", NULL);
924 srch->prio = wcstol(argv[i], NULL, 0);
925 } else if(iswdigit(*argv[i])) {
926 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
928 sq(sk, 0, L"510", L"No such node", NULL);
932 searchaddfn(srch, fn);
937 if(srch->fnl == NULL)
939 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
945 sq(sk, 0, L"501", L"No search expression found on line", NULL);
949 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
951 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
956 getsexpr(srch->sexpr = sexpr);
958 CBREG(srch, search_eta, srcheta, NULL, NULL);
959 CBREG(srch, search_commit, srchcommit, NULL, NULL);
960 CBREG(srch, search_result, srchres, NULL, NULL);
961 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
965 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
967 struct search *srch, *pt;
973 for(srch = searches; srch != NULL; srch = srch->next)
975 if(!wcscmp(srch->owner, data->username))
978 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
983 sq(sk, 0, L"201", L"No searches", NULL);
985 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
988 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
996 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
998 sq(sk, 0, L"514", L"No such search", NULL);
1001 if(srch->results == NULL)
1003 sq(sk, 0, L"201", L"No results", NULL);
1005 for(sr = srch->results; sr != NULL; sr = sr->next)
1007 swprintf(buf, 64, L"%f", sr->time);
1008 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);
1013 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1015 struct search *srch;
1019 havepriv(PERM_SRCH);
1020 /* Note - Programmatical user interfaces must only give one
1021 * argument per command, the multiple argument form is only for
1022 * convenience when manually controlling the daemon via
1023 * eg. telnet. The reason is that the return codes aren't clear
1024 * enough for the multiple argument form. */
1025 for(i = 1; i < argc; i++)
1027 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1029 sq(sk, 0, L"514", L"No such search", NULL);
1034 sq(sk, 0, L"200", L"Search cancelled", NULL);
1037 static void fcmdread(struct socket *sk, struct uidata *data)
1042 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1044 bufcat(data->fcmdbuf, buf, bufsize);
1048 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1050 wchar_t *wbuf, *p, *p2;
1054 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1055 kill(-data->fcmdpid, SIGHUP);
1056 putsock(data->fcmdsk);
1057 data->fcmdsk = NULL;
1058 if(data->fcmdbuf != NULL)
1060 free(data->fcmdbuf);
1061 data->fcmdbuf = NULL;
1063 data->fcmdbufsize = data->fcmdbufdata = 0;
1064 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1067 putsock(data->fcmdsk);
1068 data->fcmdsk = NULL;
1070 if(data->fcmdbuf == NULL)
1072 wbuf = swcsdup(L"");
1074 addtobuf(data->fcmdbuf, 0);
1075 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1076 free(data->fcmdbuf);
1078 data->fcmdbuf = NULL;
1079 data->fcmdbufsize = data->fcmdbufdata = 0;
1082 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1086 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1089 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1095 sq(data->sk, 0, L"201", L"No data returned", NULL);
1097 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1102 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1108 char *filtercmd, *argbuf;
1109 size_t cargvsize, cargvdata;
1110 struct passwd *pwent;
1113 havepriv(PERM_TRANS);
1114 if((pwent = getpwuid(data->uid)) == NULL)
1116 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1117 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1120 if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir)) == NULL)
1122 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1123 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1127 cargvsize = cargvdata = 0;
1128 addtobuf(cargv, filtercmd);
1129 for(i = 1; i < argc; i++)
1131 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1133 for(i = 0; i < cargvdata; i++)
1136 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1139 addtobuf(cargv, argbuf);
1141 addtobuf(cargv, NULL);
1142 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)
1144 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1145 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1150 execv(filtercmd, cargv);
1151 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1154 for(pp = cargv; *pp; pp++)
1157 data->fcmdsk = wrapsock(pipe);
1158 data->fcmdpid = pid;
1159 if(data->fcmdbuf != NULL)
1161 free(data->fcmdbuf);
1162 data->fcmdbuf = NULL;
1164 data->fcmdbufsize = data->fcmdbufdata = 0;
1165 data->fcmdsk->data = data;
1166 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1167 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1170 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1172 struct transfer *transfer;
1176 havepriv(PERM_TRANS);
1177 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1179 sq(sk, 0, L"512", L"No such transfer", NULL);
1182 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1184 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1187 if(transfer->args == NULL)
1189 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1191 for(ta = transfer->args; ta != NULL; ta = ta->next)
1192 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->key, L"%%ls", ta->val, NULL);
1196 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1198 struct sharecache *node;
1202 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1204 if(node->f.b.type == FILE_REG)
1207 if(node->f.b.hastth)
1211 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1214 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1216 wchar_t *buf1, *buf2;
1218 havepriv(PERM_TRANS);
1219 buf1 = swprintf2(L"%lli", bytesdownload);
1220 buf2 = swprintf2(L"%lli", bytesupload);
1221 sq(sk, 0, L"200", L"%%ls", buf1, L"%%ls", buf2, NULL);
1230 * Reserved command numbers for nameless commands:
1231 * 0: Issued when a client has connected
1232 * 1: Issued when a named command couldn't be found
1235 static struct command commands[] =
1237 {NULL, cmd_connect},
1238 {NULL, cmd_notfound},
1239 {L"shutdown", cmd_shutdown},
1240 {L"quit", cmd_quit},
1241 {L"lsauth", cmd_lsauth},
1242 {L"login", cmd_login},
1243 {L"pass", cmd_pass},
1244 {L"cnct", cmd_fnetconnect},
1245 {L"lsnodes", cmd_lsnodes},
1246 {L"dcnct", cmd_disconnect},
1247 {L"lspa", cmd_lspa},
1248 {L"lspeers", cmd_lspeers},
1249 {L"download", cmd_download},
1250 {L"lstrans", cmd_lstrans},
1251 {L"cancel", cmd_cancel},
1252 {L"notify", cmd_notify},
1253 {L"sendchat", cmd_sendchat},
1254 {L"search", cmd_search},
1255 {L"lssrch", cmd_lssrch},
1256 {L"lssr", cmd_lssr},
1257 {L"cansrch", cmd_cansrch},
1258 {L"filtercmd", cmd_filtercmd},
1259 {L"lstrarg", cmd_lstrarg},
1260 {L"hashstatus", cmd_hashstatus},
1261 {L"transstatus", cmd_transstatus},
1265 static void freequeuecmd(struct qcommand *qcmd)
1269 if(qcmd->argv != NULL)
1271 for(i = 0; i < qcmd->argc; i++)
1272 free(qcmd->argv[i]);
1278 static struct qcommand *unlinkqcmd(struct uidata *data)
1280 struct qcommand *qcmd;
1285 data->queue = qcmd->next;
1286 if(qcmd == data->queuelast)
1287 data->queuelast = qcmd->next;
1292 static void notifappendv(struct notif *notif, va_list args)
1296 while((dt = va_arg(args, int)) != NOTIF_END)
1299 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1300 notif->argv[ca].dt = dt;
1305 notif->argv[ca].d.n = va_arg(args, int);
1308 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1311 notif->argv[ca].d.d = va_arg(args, double);
1317 static void notifappend(struct notif *notif, ...)
1321 va_start(args, notif);
1322 notifappendv(notif, args);
1326 static struct notif *newnotif(struct uidata *data, int code, ...)
1328 struct notif *notif;
1331 notif = smalloc(sizeof(*notif));
1332 memset(notif, 0, sizeof(*notif));
1333 notif->rlimit = 0.0;
1336 va_start(args, code);
1337 notifappendv(notif, args);
1340 notif->prev = data->lnotif;
1341 if(data->lnotif != NULL)
1342 data->lnotif->next = notif;
1344 data->fnotif = notif;
1345 data->lnotif = notif;
1349 static void freenotif(struct notif *notif)
1353 if(notif->next != NULL)
1354 notif->next->prev = notif->prev;
1355 if(notif->prev != NULL)
1356 notif->prev->next = notif->next;
1357 if(notif == notif->ui->fnotif)
1358 notif->ui->fnotif = notif->next;
1359 if(notif == notif->ui->lnotif)
1360 notif->ui->lnotif = notif->prev;
1361 if(notif->exptimer != NULL)
1362 canceltimer(notif->exptimer);
1363 for(i = 0; i < notif->argc; i++)
1365 if(notif->argv[i].dt == NOTIF_STR)
1366 free(notif->argv[i].d.s);
1368 if(notif->argv != NULL)
1373 static void notifexpire(int cancelled, struct notif *notif)
1375 notif->exptimer = NULL;
1380 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1384 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1386 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1391 for(i = 0; i < notif->argc; i++)
1393 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1408 static void freeuidata(struct uidata *data)
1411 struct qcommand *qcmd;
1413 if(data->next != NULL)
1414 data->next->prev = data->prev;
1415 if(data->prev != NULL)
1416 data->prev->next = data->next;
1418 actives = data->next;
1419 data->sk->readcb = NULL;
1420 data->sk->errcb = NULL;
1422 while((qcmd = unlinkqcmd(data)) != NULL)
1424 iconv_close(data->ichandle);
1425 if(data->cw != NULL)
1427 if(data->cb != NULL)
1429 if(data->argv != NULL)
1431 for(i = 0; i < data->argc; i++)
1432 free(data->argv[i]);
1435 if(data->auth != NULL)
1436 authputhandle(data->auth);
1437 if(data->username != NULL)
1439 if(data->userinfo != NULL)
1440 flog(LOG_INFO, "%ls logged out", data->username);
1441 free(data->username);
1444 while(data->fnotif != NULL)
1445 freenotif(data->fnotif);
1446 if(data->fcmdbuf != NULL)
1447 free(data->fcmdbuf);
1448 if(data->fcmdpid != 0)
1449 kill(-data->fcmdpid, SIGHUP);
1450 if(data->fcmdsk != NULL)
1451 putsock(data->fcmdsk);
1455 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1457 struct qcommand *new;
1459 new = smalloc(sizeof(*new));
1464 if(data->queuelast != NULL)
1465 data->queuelast->next = new;
1466 data->queuelast = new;
1467 if(data->queue == NULL)
1471 static struct uidata *newuidata(struct socket *sk)
1473 struct uidata *data;
1475 data = smalloc(sizeof(*data));
1476 memset(data, 0, sizeof(*data));
1479 data->inbuf = smalloc(1024);
1481 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1483 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1486 data->next = actives;
1489 actives->prev = data;
1494 static void uiread(struct socket *sk, struct uidata *data)
1500 size_t datalen, len2;
1501 struct command *cur;
1503 if(data->indata > 1024)
1505 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1507 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1508 memcpy(data->inbuf + data->indata, newbuf, datalen);
1510 data->indata += datalen;
1511 if(data->cb == NULL)
1513 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1515 data->pp = data->cb;
1520 if(data->cbsize == data->cbdata)
1522 len2 = data->pp - data->cb;
1523 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1524 data->pp = data->cb + len2;
1527 p2 = (char *)(porig = (data->cb + data->cbdata));
1528 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1529 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1530 memmove(data->inbuf, p1, data->indata);
1531 /* Just a sanity check */
1532 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1534 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1537 data->cbdata += (((wchar_t *)p2) - porig);
1543 /* XXX: Should this really just ignore it? */
1551 /* Just a sanity check */
1552 if(data->cbsize != data->cbdata)
1554 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1559 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1567 while(!done && (data->pp - data->cb < data->cbdata))
1572 if(iswspace(*data->pp))
1574 if(*data->pp == L'\r')
1576 if(data->pp == data->cb + data->cbdata - 1)
1581 if(*(++data->pp) == L'\n')
1583 if((data->argv != NULL) && (data->argv[0] != NULL))
1585 for(cur = commands; cur->handler != NULL; cur++)
1587 if(cur->name == NULL)
1589 if(!wcscasecmp(cur->name, data->argv[0]))
1591 queuecmd(data, cur, data->argc, data->argv);
1595 if(cur->handler == NULL)
1596 queuecmd(data, &commands[1], data->argc, data->argv);
1598 queuecmd(data, &commands[1], data->argc, data->argv);
1603 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1604 data->pp = data->cb;
1617 if(iswspace(*data->pp))
1619 addtobuf(data->cw, L'\0');
1620 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1621 data->argv[data->argc++] = data->cw;
1626 } else if(*data->pp == L'\"') {
1629 } else if(*data->pp == L'\\') {
1630 if(data->pp == data->cb + data->cbdata - 1)
1635 addtobuf(data->cw, *(++data->pp));
1638 addtobuf(data->cw, *(data->pp++));
1642 if(*data->pp == L'\"')
1645 } else if(*data->pp == L'\\') {
1646 if(data->pp == data->cb + data->cbdata - 1)
1651 addtobuf(data->cw, *(++(data->pp)));
1653 addtobuf(data->cw, *data->pp);
1661 static void uierror(struct socket *sk, int err, struct uidata *data)
1664 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1668 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1670 struct uidata *uidata;
1672 newsk->data = uidata = newuidata(newsk);
1673 socksettos(newsk, confgetint("ui", "uitos"));
1676 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1677 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1678 queuecmd(uidata, &commands[0], 0, NULL);
1681 static int srcheta(struct search *srch, void *uudata)
1683 struct uidata *data;
1685 for(data = actives; data != NULL; data = data->next)
1687 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1688 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1693 static int srchcommit(struct search *srch, void *uudata)
1695 struct uidata *data;
1697 for(data = actives; data != NULL; data = data->next)
1699 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1700 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1705 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1707 struct uidata *data;
1709 for(data = actives; data != NULL; data = data->next)
1711 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1713 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1714 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);
1720 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1722 struct uidata *data;
1724 for(data = actives; data != NULL; data = data->next)
1726 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1727 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1732 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1734 struct uidata *data;
1735 struct notif *notif;
1737 if(!wcscmp(attrib, L"state"))
1739 for(data = actives; data != NULL; data = data->next)
1741 if(data->notify.b.fnact)
1742 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1744 } else if(!wcscmp(attrib, L"name")) {
1745 for(data = actives; data != NULL; data = data->next)
1747 if(data->notify.b.fnact)
1748 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1750 } else if(!wcscmp(attrib, L"numpeers")) {
1751 for(data = actives; data != NULL; data = data->next)
1753 if(data->notify.b.fnact)
1755 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1756 notif->argv[1].d.n = fn->numpeers;
1758 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1765 static int fnunlink(struct fnetnode *fn, void *uudata)
1767 struct uidata *data;
1769 for(data = actives; data != NULL; data = data->next)
1771 if(data->notify.b.fnact)
1772 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1777 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1779 struct uidata *data;
1781 for(data = actives; data != NULL; data = data->next)
1783 if(data->notify.b.fnpeer)
1784 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1789 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1791 struct uidata *data;
1793 for(data = actives; data != NULL; data = data->next)
1795 if(data->notify.b.fnpeer)
1796 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1801 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1803 struct uidata *data;
1804 struct notif *notif;
1807 for(data = actives; data != NULL; data = data->next)
1809 if(data->notify.b.fnpeer)
1811 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1813 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1817 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1818 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1819 switch(di->datum->datatype)
1822 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1825 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1828 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1829 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1837 static int newfnetnode(struct fnetnode *fn, void *uudata)
1839 struct uidata *data;
1841 for(data = actives; data != NULL; data = data->next)
1843 if(data->notify.b.fnact)
1844 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1846 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1847 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1848 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1849 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1850 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1851 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
1855 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1857 struct uidata *data;
1859 if(!wcscmp(attrib, L"state"))
1861 for(data = actives; data != NULL; data = data->next)
1863 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1864 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1866 } else if(!wcscmp(attrib, L"nick")) {
1867 for(data = actives; data != NULL; data = data->next)
1869 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1870 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1872 } else if(!wcscmp(attrib, L"size")) {
1873 for(data = actives; data != NULL; data = data->next)
1875 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1876 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1878 } else if(!wcscmp(attrib, L"error")) {
1879 for(data = actives; data != NULL; data = data->next)
1881 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1882 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1884 } else if(!wcscmp(attrib, L"path")) {
1885 for(data = actives; data != NULL; data = data->next)
1887 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1888 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1890 } else if(!wcscmp(attrib, L"hash")) {
1891 for(data = actives; data != NULL; data = data->next)
1893 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1894 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
1900 static int transferprog(struct transfer *transfer, void *uudata)
1902 struct uidata *data;
1903 struct notif *notif;
1905 for(data = actives; data != NULL; data = data->next)
1907 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1909 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
1910 notif->argv[1].d.n = transfer->curpos;
1912 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
1918 static int transferdestroyed(struct transfer *transfer, void *uudata)
1920 struct uidata *data;
1922 for(data = actives; data != NULL; data = data->next)
1924 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1925 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_END);
1930 static int newtransfernotify(struct transfer *transfer, void *uudata)
1932 struct uidata *data;
1934 for(data = actives; data != NULL; data = data->next)
1936 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1937 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);
1939 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
1940 CBREG(transfer, trans_p, transferprog, NULL, NULL);
1941 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
1945 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
1949 new = smalloc(sizeof(*new));
1951 new->name = swcsdup(name);
1962 static void freeuser(struct uiuser *user)
1964 if(user->next != NULL)
1965 user->next->prev = user->prev;
1966 if(user->prev != NULL)
1967 user->prev->next = user->next;
1974 static int conf_user(int argc, wchar_t **argv)
1976 int i, perms, permmod;
1977 struct uiuser *user;
1982 flog(LOG_WARNING, "not enough arguments given for user command");
1986 for(i = 2; i < argc; i++)
1988 if(!iswalpha(argv[i][0]))
1992 if(!wcscmp(p, L"disallow"))
1993 permmod = PERM_DISALLOW;
1994 if(!wcscmp(p, L"admin"))
1995 permmod = PERM_ADMIN;
1996 if(!wcscmp(p, L"fnetctl"))
1997 permmod = PERM_FNETCTL;
1998 if(!wcscmp(p, L"trans"))
1999 permmod = PERM_TRANS;
2000 if(!wcscmp(p, L"transcu"))
2001 permmod = PERM_TRANSCU;
2002 if(!wcscmp(p, L"chat"))
2003 permmod = PERM_CHAT;
2004 if(!wcscmp(p, L"srch"))
2005 permmod = PERM_SRCH;
2006 if(!wcscmp(p, L"all"))
2008 if(argv[i][0] == L'-')
2013 if((user = finduser(argv[1])) == NULL)
2015 newuser(argv[1], perms);
2018 user->perms = perms;
2023 static void preinit(int hup)
2025 struct uiuser *user;
2029 newuser(L"default", 0);
2031 for(user = users; user != NULL; user = user->next)
2033 if(!wcscmp(user->name, L"default"))
2040 static struct sockaddr *getnameforport(int port, socklen_t *len)
2042 static struct sockaddr_in6 addr;
2044 memset(&addr, 0, sizeof(addr));
2045 addr.sin6_family = AF_INET6;
2046 addr.sin6_port = htons(port);
2047 addr.sin6_addr = in6addr_any;
2049 *len = sizeof(addr);
2050 return((struct sockaddr *)&addr);
2053 static struct sockaddr *getnameforport(int port, socklen_t *len)
2055 static struct sockaddr_in addr;
2057 memset(&addr, 0, sizeof(addr));
2058 addr.sin_family = AF_INET;
2059 addr.sin_port = htons(port);
2061 *len = sizeof(addr);
2062 return((struct sockaddr *)&addr);
2066 static int portupdate(struct configvar *var, void *uudata)
2068 struct sockaddr *addr;
2070 struct socket *newsock;
2072 addr = getnameforport(var->val.num, &addrlen);
2073 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
2075 flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno));
2078 if(uisocket != NULL)
2084 static int init(int hup)
2086 struct sockaddr *addr;
2088 struct uiuser *user, *next;
2092 for(user = users; user != NULL; user = next)
2101 if(uisocket != NULL)
2103 addr = getnameforport(confgetint("ui", "port"), &addrlen);
2104 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
2106 flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno));
2109 CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL);
2110 GCBREG(newfncb, newfnetnode, NULL);
2111 GCBREG(newtransfercb, newtransfernotify, NULL);
2116 static int run(void)
2119 struct uidata *data, *next;
2120 struct qcommand *qcmd;
2121 struct notif *notif, *nnotif;
2124 for(data = actives; data != NULL; data = next)
2130 for(data = actives; data != NULL; data = data->next)
2132 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2134 nnotif = notif->next;
2135 if(notif->state == NOTIF_WAIT)
2138 for(i = 0; i < notif->argc; i++)
2140 if(notif->argv[i].dt == NOTIF_ID)
2142 id = notif->argv[i].d.n;
2146 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2148 sq(data->sk, 2, L"%%i", notif->code, NULL);
2149 for(i = 0; i < notif->argc; i++)
2151 switch(notif->argv[i].dt)
2155 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2158 if(notif->argv[i].d.s[0] == L'%')
2159 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2161 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2164 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2165 sq(data->sk, 2, buf, NULL);
2169 sq(data->sk, 0, NULL);
2170 if(notif->rlimit != 0)
2172 notif->state = NOTIF_WAIT;
2173 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2178 if((qcmd = unlinkqcmd(data)) != NULL)
2180 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2188 static void terminate(void)
2190 while(users != NULL)
2194 static struct configvar myvars[] =
2196 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2197 {CONF_VAR_INT, "port", {.num = 1500}},
2198 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2199 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2203 static struct configcmd mycmds[] =
2205 {"user", conf_user},
2209 static struct module me =
2220 .terminate = terminate