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 from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
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 from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
447 sq(sk, 0, L"200", L"Welcome", NULL);
448 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
452 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
453 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
457 switch(data->auth->prompt)
475 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
478 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
482 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
483 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
489 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
495 if((buf = icwcstombs(argv[1], NULL)) == NULL)
497 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
500 if((data->auth == NULL) || (data->userinfo != NULL))
502 sq(sk, 0, L"507", L"Data not expected", NULL);
505 switch(authenticate(data->auth, buf))
508 data->userinfo = finduser(data->username);
509 if(data->userinfo == NULL)
510 data->userinfo = finduser(L"default");
513 sq(sk, 0, L"506", L"Authentication error", NULL);
514 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
516 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
517 sq(sk, 0, L"506", L"Authentication error", NULL);
518 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
521 sq(sk, 0, L"200", L"Welcome", NULL);
522 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
526 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
527 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
531 switch(data->auth->prompt)
549 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
552 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
556 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
557 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
564 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
570 struct wcspair *args;
573 havepriv(PERM_FNETCTL);
574 if((buf = icwcstombs(argv[2], NULL)) == NULL)
576 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
580 for(i = 3; i < argc - 1; i += 2)
581 newwcspair(argv[i], argv[i + 1], &args);
582 fn = fnetinitconnect(argv[1], buf, args);
587 if(errno == EPROTONOSUPPORT)
588 sq(sk, 0, L"511", L"No such network name", NULL);
590 sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
594 fnetsetname(fn, argv[2]);
596 sq(sk, 0, L"200", L"Connection under way", NULL);
599 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
603 if(fnetnodes == NULL)
605 sq(sk, 0, L"201", L"No connected nodes", NULL);
608 for(fn = fnetnodes; fn != NULL; fn = fn->next)
610 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);
614 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
620 havepriv(PERM_FNETCTL);
621 /* Note - Programmatical user interfaces must only give one
622 * argument per command, the multiple argument form is only for
623 * convenience when manually controlling the daemon via
624 * eg. telnet. The reason is that the return codes aren't clear
625 * enough for the multiple argument form. */
626 for(i = 1; i < argc; i++)
628 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
630 sq(sk, 0, L"510", L"No such node", NULL);
636 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
639 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
642 struct fnetpeerdatum *datum;
645 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
647 sq(sk, 0, L"510", L"No such node", NULL);
650 if(fn->peerdata == NULL)
652 sq(sk, 0, L"201", L"No data available", NULL);
654 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
655 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%%i", datum->datatype, NULL);
659 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
663 struct fnetpeer *peer;
667 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
669 sq(sk, 0, L"510", L"No such node", NULL);
672 if(fn->peers == NULL)
674 sq(sk, 0, L"201", L"No peers avaiable", NULL);
676 for(peer = fn->peers; peer != NULL; peer = peer->next)
678 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", peer->id, peer->nick, NULL);
679 for(i = 0; i < peer->dinum; i++)
681 if(peer->peerdi[i].datum->datatype == FNPD_INT)
682 sq(sk, 2, peer->peerdi[i].datum->id, L"%%i", peer->peerdi[i].data.num, NULL);
683 /* Note: A long long is not the size of an int, so
684 * sq() can't handle the conversion itself. */
685 if(peer->peerdi[i].datum->datatype == FNPD_LL)
687 swprintf(buf, 40, L"%lli", peer->peerdi[i].data.lnum);
688 sq(sk, 2, peer->peerdi[i].datum->id, buf, NULL);
690 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
691 sq(sk, 2, peer->peerdi[i].datum->id, peer->peerdi[i].data.str, NULL);
698 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
703 struct transfer *transfer;
704 struct fnetpeer *peer;
707 if((argc > 5) && ((argc % 2) == 0))
709 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
712 havepriv(PERM_TRANS);
713 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
715 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
717 sq(sk, 0, L"510", L"No such node", NULL);
723 if((net = findfnet(argv[1])) == NULL)
725 sq(sk, 0, L"511", L"No such network name", NULL);
729 transfer = newtransfer();
730 authgethandle(transfer->auth = data->auth);
731 transfer->fnet = net;
732 transfer->peerid = swcsdup(argv[2]);
733 transfer->path = swcsdup(argv[3]);
734 transfer->dir = TRNSD_DOWN;
735 transfer->owner = data->uid;
740 linktransfer(transfer);
741 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
742 transfersetnick(transfer, peer->nick);
744 linktransfer(transfer);
747 transfersetsize(transfer, wcstol(argv[4], NULL, 0));
750 for(i = 5; i < argc; i += 2)
752 if(!wcscmp(argv[i], L"hash"))
754 transfersethash(transfer, parsehash(argv[i + 1]));
756 newwcspair(argv[i], argv[i + 1], &transfer->args);
760 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
761 transfersetactivity(transfer, L"create");
764 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
766 struct transfer *transfer, *pt;
768 havepriv(PERM_TRANS);
770 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
772 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
775 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
776 L"%%i", pt->state, pt->peerid,
777 (pt->peernick == NULL)?L"":(pt->peernick),
778 (pt->path == NULL)?L"":(pt->path),
779 L"%%i", pt->size, L"%%i", pt->curpos,
780 (pt->hash == NULL)?L"":unparsehash(pt->hash),
786 sq(sk, 0, L"201", L"No transfers", NULL);
788 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
789 L"%%i", pt->state, pt->peerid,
790 (pt->peernick == NULL)?L"":(pt->peernick),
791 (pt->path == NULL)?L"":(pt->path),
792 L"%%i", pt->size, L"%%i", pt->curpos,
793 (pt->hash == NULL)?L"":unparsehash(pt->hash),
797 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
799 struct transfer *transfer;
802 havepriv(PERM_TRANS);
803 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
805 sq(sk, 0, L"512", L"No such transfer", NULL);
808 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
810 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
813 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
815 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
819 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
822 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
828 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
831 for(i = 1; i < argc; i += 2)
833 if(!wcscasecmp(argv[i + 1], L"on"))
837 if(!wcscasecmp(argv[i], L"all"))
843 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
844 data->notify.b.fnchat = val;
845 } else if(!wcscasecmp(argv[i], L"fn:act")) {
846 data->notify.b.fnact = val;
847 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
848 data->notify.b.fnpeer = val;
849 } else if(!wcscasecmp(argv[i], L"trans:act")) {
850 data->notify.b.tract = val;
851 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
852 data->notify.b.trprog = val;
853 } else if(!wcscasecmp(argv[i], L"srch:act")) {
854 data->notify.b.srch = val;
857 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
860 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
867 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
869 sq(sk, 0, L"510", L"No such node", NULL);
872 public = wcstol(argv[2], NULL, 0);
873 if((public != 0) && (public != 1))
875 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
878 if(fn->state != FNN_EST)
880 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
883 if(fnetsendchat(fn, public, argv[3], argv[4]))
886 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
887 else if(errno == EPERM)
888 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
889 else if(errno == EILSEQ)
890 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
892 sq(sk, 0, L"505", L"Could not chat", NULL);
895 sq(sk, 0, L"200", L"Chat string sent", NULL);
898 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
907 srch = newsearch(data->username, NULL);
908 for(i = 1; i < argc; i++)
910 if(!wcscmp(argv[i], L"all"))
912 for(fn = fnetnodes; fn != NULL; fn = fn->next)
914 if(fn->state == FNN_EST)
915 searchaddfn(srch, fn);
919 } else if(!wcscmp(argv[i], L"prio")) {
922 sq(sk, 0, L"501", L"No argument to prio", NULL);
926 srch->prio = wcstol(argv[i], NULL, 0);
927 } else if(iswdigit(*argv[i])) {
928 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
930 sq(sk, 0, L"510", L"No such node", NULL);
934 searchaddfn(srch, fn);
939 if(srch->fnl == NULL)
941 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
947 sq(sk, 0, L"501", L"No search expression found on line", NULL);
951 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
953 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
958 getsexpr(srch->sexpr = sexpr);
960 CBREG(srch, search_eta, srcheta, NULL, NULL);
961 CBREG(srch, search_commit, srchcommit, NULL, NULL);
962 CBREG(srch, search_result, srchres, NULL, NULL);
963 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
967 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
969 struct search *srch, *pt;
975 for(srch = searches; srch != NULL; srch = srch->next)
977 if(!wcscmp(srch->owner, data->username))
980 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
985 sq(sk, 0, L"201", L"No searches", NULL);
987 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
990 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
998 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1000 sq(sk, 0, L"514", L"No such search", NULL);
1003 if(srch->results == NULL)
1005 sq(sk, 0, L"201", L"No results", NULL);
1007 for(sr = srch->results; sr != NULL; sr = sr->next)
1009 swprintf(buf, 64, L"%f", sr->time);
1010 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);
1015 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1017 struct search *srch;
1021 havepriv(PERM_SRCH);
1022 /* Note - Programmatical user interfaces must only give one
1023 * argument per command, the multiple argument form is only for
1024 * convenience when manually controlling the daemon via
1025 * eg. telnet. The reason is that the return codes aren't clear
1026 * enough for the multiple argument form. */
1027 for(i = 1; i < argc; i++)
1029 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1031 sq(sk, 0, L"514", L"No such search", NULL);
1036 sq(sk, 0, L"200", L"Search cancelled", NULL);
1039 static void fcmdread(struct socket *sk, struct uidata *data)
1044 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1046 bufcat(data->fcmdbuf, buf, bufsize);
1050 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1052 wchar_t *wbuf, *p, *p2;
1056 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1057 kill(-data->fcmdpid, SIGHUP);
1058 putsock(data->fcmdsk);
1059 data->fcmdsk = NULL;
1060 if(data->fcmdbuf != NULL)
1062 free(data->fcmdbuf);
1063 data->fcmdbuf = NULL;
1065 data->fcmdbufsize = data->fcmdbufdata = 0;
1066 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1069 putsock(data->fcmdsk);
1070 data->fcmdsk = NULL;
1072 if(data->fcmdbuf == NULL)
1074 wbuf = swcsdup(L"");
1076 addtobuf(data->fcmdbuf, 0);
1077 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1078 free(data->fcmdbuf);
1080 data->fcmdbuf = NULL;
1081 data->fcmdbufsize = data->fcmdbufdata = 0;
1084 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1088 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1091 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1097 sq(data->sk, 0, L"201", L"No data returned", NULL);
1099 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1104 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1110 char *filtercmd, *argbuf;
1111 size_t cargvsize, cargvdata;
1112 struct passwd *pwent;
1115 havepriv(PERM_TRANS);
1116 if((pwent = getpwuid(data->uid)) == NULL)
1118 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1119 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1122 if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir)) == NULL)
1124 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1125 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1129 cargvsize = cargvdata = 0;
1130 addtobuf(cargv, filtercmd);
1131 for(i = 1; i < argc; i++)
1133 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1135 for(i = 0; i < cargvdata; i++)
1138 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1141 addtobuf(cargv, argbuf);
1143 addtobuf(cargv, NULL);
1144 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)
1146 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1147 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1152 execv(filtercmd, cargv);
1153 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1156 for(pp = cargv; *pp; pp++)
1159 data->fcmdsk = wrapsock(pipe);
1160 data->fcmdpid = pid;
1161 if(data->fcmdbuf != NULL)
1163 free(data->fcmdbuf);
1164 data->fcmdbuf = NULL;
1166 data->fcmdbufsize = data->fcmdbufdata = 0;
1167 data->fcmdsk->data = data;
1168 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1169 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1172 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1174 struct transfer *transfer;
1178 havepriv(PERM_TRANS);
1179 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1181 sq(sk, 0, L"512", L"No such transfer", NULL);
1184 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1186 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1189 if(transfer->args == NULL)
1191 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1193 for(ta = transfer->args; ta != NULL; ta = ta->next)
1194 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->key, L"%%ls", ta->val, NULL);
1198 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1200 struct sharecache *node;
1204 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1206 if(node->f.b.type == FILE_REG)
1209 if(node->f.b.hastth)
1213 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1216 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1218 wchar_t *buf1, *buf2;
1220 havepriv(PERM_TRANS);
1221 buf1 = swprintf2(L"%lli", bytesdownload);
1222 buf2 = swprintf2(L"%lli", bytesupload);
1223 sq(sk, 0, L"200", L"%%ls", buf1, L"%%ls", buf2, NULL);
1232 * Reserved command numbers for nameless commands:
1233 * 0: Issued when a client has connected
1234 * 1: Issued when a named command couldn't be found
1237 static struct command commands[] =
1239 {NULL, cmd_connect},
1240 {NULL, cmd_notfound},
1241 {L"shutdown", cmd_shutdown},
1242 {L"quit", cmd_quit},
1243 {L"lsauth", cmd_lsauth},
1244 {L"login", cmd_login},
1245 {L"pass", cmd_pass},
1246 {L"cnct", cmd_fnetconnect},
1247 {L"lsnodes", cmd_lsnodes},
1248 {L"dcnct", cmd_disconnect},
1249 {L"lspa", cmd_lspa},
1250 {L"lspeers", cmd_lspeers},
1251 {L"download", cmd_download},
1252 {L"lstrans", cmd_lstrans},
1253 {L"cancel", cmd_cancel},
1254 {L"notify", cmd_notify},
1255 {L"sendchat", cmd_sendchat},
1256 {L"search", cmd_search},
1257 {L"lssrch", cmd_lssrch},
1258 {L"lssr", cmd_lssr},
1259 {L"cansrch", cmd_cansrch},
1260 {L"filtercmd", cmd_filtercmd},
1261 {L"lstrarg", cmd_lstrarg},
1262 {L"hashstatus", cmd_hashstatus},
1263 {L"transstatus", cmd_transstatus},
1267 static void freequeuecmd(struct qcommand *qcmd)
1271 if(qcmd->argv != NULL)
1273 for(i = 0; i < qcmd->argc; i++)
1274 free(qcmd->argv[i]);
1280 static struct qcommand *unlinkqcmd(struct uidata *data)
1282 struct qcommand *qcmd;
1287 data->queue = qcmd->next;
1288 if(qcmd == data->queuelast)
1289 data->queuelast = qcmd->next;
1294 static void notifappendv(struct notif *notif, va_list args)
1298 while((dt = va_arg(args, int)) != NOTIF_END)
1301 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1302 notif->argv[ca].dt = dt;
1307 notif->argv[ca].d.n = va_arg(args, int);
1310 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1313 notif->argv[ca].d.d = va_arg(args, double);
1319 static void notifappend(struct notif *notif, ...)
1323 va_start(args, notif);
1324 notifappendv(notif, args);
1328 static struct notif *newnotif(struct uidata *data, int code, ...)
1330 struct notif *notif;
1333 notif = smalloc(sizeof(*notif));
1334 memset(notif, 0, sizeof(*notif));
1335 notif->rlimit = 0.0;
1338 va_start(args, code);
1339 notifappendv(notif, args);
1342 notif->prev = data->lnotif;
1343 if(data->lnotif != NULL)
1344 data->lnotif->next = notif;
1346 data->fnotif = notif;
1347 data->lnotif = notif;
1351 static void freenotif(struct notif *notif)
1355 if(notif->next != NULL)
1356 notif->next->prev = notif->prev;
1357 if(notif->prev != NULL)
1358 notif->prev->next = notif->next;
1359 if(notif == notif->ui->fnotif)
1360 notif->ui->fnotif = notif->next;
1361 if(notif == notif->ui->lnotif)
1362 notif->ui->lnotif = notif->prev;
1363 if(notif->exptimer != NULL)
1364 canceltimer(notif->exptimer);
1365 for(i = 0; i < notif->argc; i++)
1367 if(notif->argv[i].dt == NOTIF_STR)
1368 free(notif->argv[i].d.s);
1370 if(notif->argv != NULL)
1375 static void notifexpire(int cancelled, struct notif *notif)
1377 notif->exptimer = NULL;
1382 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1386 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1388 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1393 for(i = 0; i < notif->argc; i++)
1395 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1410 static void freeuidata(struct uidata *data)
1413 struct qcommand *qcmd;
1415 if(data->next != NULL)
1416 data->next->prev = data->prev;
1417 if(data->prev != NULL)
1418 data->prev->next = data->next;
1420 actives = data->next;
1421 data->sk->readcb = NULL;
1422 data->sk->errcb = NULL;
1424 while((qcmd = unlinkqcmd(data)) != NULL)
1426 iconv_close(data->ichandle);
1427 if(data->cw != NULL)
1429 if(data->cb != NULL)
1431 if(data->argv != NULL)
1433 for(i = 0; i < data->argc; i++)
1434 free(data->argv[i]);
1437 if(data->auth != NULL)
1438 authputhandle(data->auth);
1439 if(data->username != NULL)
1441 if(data->userinfo != NULL)
1442 flog(LOG_INFO, "%ls logged out", data->username);
1443 free(data->username);
1446 while(data->fnotif != NULL)
1447 freenotif(data->fnotif);
1448 if(data->fcmdbuf != NULL)
1449 free(data->fcmdbuf);
1450 if(data->fcmdpid != 0)
1451 kill(-data->fcmdpid, SIGHUP);
1452 if(data->fcmdsk != NULL)
1453 putsock(data->fcmdsk);
1457 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1459 struct qcommand *new;
1461 new = smalloc(sizeof(*new));
1466 if(data->queuelast != NULL)
1467 data->queuelast->next = new;
1468 data->queuelast = new;
1469 if(data->queue == NULL)
1473 static struct uidata *newuidata(struct socket *sk)
1475 struct uidata *data;
1477 data = smalloc(sizeof(*data));
1478 memset(data, 0, sizeof(*data));
1481 data->inbuf = smalloc(1024);
1483 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1485 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1488 data->next = actives;
1491 actives->prev = data;
1496 static void uiread(struct socket *sk, struct uidata *data)
1502 size_t datalen, len2;
1503 struct command *cur;
1505 if(data->indata > 1024)
1507 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1509 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1510 memcpy(data->inbuf + data->indata, newbuf, datalen);
1512 data->indata += datalen;
1513 if(data->cb == NULL)
1515 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1517 data->pp = data->cb;
1522 if(data->cbsize == data->cbdata)
1524 len2 = data->pp - data->cb;
1525 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1526 data->pp = data->cb + len2;
1529 p2 = (char *)(porig = (data->cb + data->cbdata));
1530 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1531 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1532 memmove(data->inbuf, p1, data->indata);
1533 /* Just a sanity check */
1534 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1536 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1539 data->cbdata += (((wchar_t *)p2) - porig);
1545 /* XXX: Should this really just ignore it? */
1553 /* Just a sanity check */
1554 if(data->cbsize != data->cbdata)
1556 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1561 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1569 while(!done && (data->pp - data->cb < data->cbdata))
1574 if(iswspace(*data->pp))
1576 if(*data->pp == L'\r')
1578 if(data->pp == data->cb + data->cbdata - 1)
1583 if(*(++data->pp) == L'\n')
1585 if((data->argv != NULL) && (data->argv[0] != NULL))
1587 for(cur = commands; cur->handler != NULL; cur++)
1589 if(cur->name == NULL)
1591 if(!wcscasecmp(cur->name, data->argv[0]))
1593 queuecmd(data, cur, data->argc, data->argv);
1597 if(cur->handler == NULL)
1598 queuecmd(data, &commands[1], data->argc, data->argv);
1600 queuecmd(data, &commands[1], data->argc, data->argv);
1605 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1606 data->pp = data->cb;
1619 if(iswspace(*data->pp))
1621 addtobuf(data->cw, L'\0');
1622 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1623 data->argv[data->argc++] = data->cw;
1628 } else if(*data->pp == L'\"') {
1631 } else if(*data->pp == L'\\') {
1632 if(data->pp == data->cb + data->cbdata - 1)
1637 addtobuf(data->cw, *(++data->pp));
1640 addtobuf(data->cw, *(data->pp++));
1644 if(*data->pp == L'\"')
1647 } else if(*data->pp == L'\\') {
1648 if(data->pp == data->cb + data->cbdata - 1)
1653 addtobuf(data->cw, *(++(data->pp)));
1655 addtobuf(data->cw, *data->pp);
1663 static void uierror(struct socket *sk, int err, struct uidata *data)
1666 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1670 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1672 struct uidata *uidata;
1674 newsk->data = uidata = newuidata(newsk);
1675 socksettos(newsk, confgetint("ui", "uitos"));
1678 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1679 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1680 queuecmd(uidata, &commands[0], 0, NULL);
1683 static int srcheta(struct search *srch, void *uudata)
1685 struct uidata *data;
1687 for(data = actives; data != NULL; data = data->next)
1689 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1690 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1695 static int srchcommit(struct search *srch, void *uudata)
1697 struct uidata *data;
1699 for(data = actives; data != NULL; data = data->next)
1701 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1702 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1707 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1709 struct uidata *data;
1711 for(data = actives; data != NULL; data = data->next)
1713 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1715 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1716 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);
1722 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1724 struct uidata *data;
1726 for(data = actives; data != NULL; data = data->next)
1728 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1729 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1734 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1736 struct uidata *data;
1737 struct notif *notif;
1739 if(!wcscmp(attrib, L"state"))
1741 for(data = actives; data != NULL; data = data->next)
1743 if(data->notify.b.fnact)
1744 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1746 } else if(!wcscmp(attrib, L"name")) {
1747 for(data = actives; data != NULL; data = data->next)
1749 if(data->notify.b.fnact)
1750 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1752 } else if(!wcscmp(attrib, L"numpeers")) {
1753 for(data = actives; data != NULL; data = data->next)
1755 if(data->notify.b.fnact)
1757 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1758 notif->argv[1].d.n = fn->numpeers;
1760 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1767 static int fnunlink(struct fnetnode *fn, void *uudata)
1769 struct uidata *data;
1771 for(data = actives; data != NULL; data = data->next)
1773 if(data->notify.b.fnact)
1774 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1779 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1781 struct uidata *data;
1783 for(data = actives; data != NULL; data = data->next)
1785 if(data->notify.b.fnpeer)
1786 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1791 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1793 struct uidata *data;
1795 for(data = actives; data != NULL; data = data->next)
1797 if(data->notify.b.fnpeer)
1798 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1803 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1805 struct uidata *data;
1806 struct notif *notif;
1809 for(data = actives; data != NULL; data = data->next)
1811 if(data->notify.b.fnpeer)
1813 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1815 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1819 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1820 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1821 switch(di->datum->datatype)
1824 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1827 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1830 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1831 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1839 static int newfnetnode(struct fnetnode *fn, void *uudata)
1841 struct uidata *data;
1843 for(data = actives; data != NULL; data = data->next)
1845 if(data->notify.b.fnact)
1846 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1848 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1849 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1850 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1851 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1852 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1853 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
1857 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1859 struct uidata *data;
1861 if(!wcscmp(attrib, L"state"))
1863 for(data = actives; data != NULL; data = data->next)
1865 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1866 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1868 } else if(!wcscmp(attrib, L"nick")) {
1869 for(data = actives; data != NULL; data = data->next)
1871 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1872 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1874 } else if(!wcscmp(attrib, L"size")) {
1875 for(data = actives; data != NULL; data = data->next)
1877 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1878 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1880 } else if(!wcscmp(attrib, L"error")) {
1881 for(data = actives; data != NULL; data = data->next)
1883 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1884 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1886 } else if(!wcscmp(attrib, L"path")) {
1887 for(data = actives; data != NULL; data = data->next)
1889 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1890 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1892 } else if(!wcscmp(attrib, L"hash")) {
1893 for(data = actives; data != NULL; data = data->next)
1895 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1896 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
1902 static int transferprog(struct transfer *transfer, void *uudata)
1904 struct uidata *data;
1905 struct notif *notif;
1907 for(data = actives; data != NULL; data = data->next)
1909 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1911 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
1912 notif->argv[1].d.n = transfer->curpos;
1914 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
1920 static int transferdestroyed(struct transfer *transfer, void *uudata)
1922 struct uidata *data;
1924 for(data = actives; data != NULL; data = data->next)
1926 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1927 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
1932 static int newtransfernotify(struct transfer *transfer, void *uudata)
1934 struct uidata *data;
1936 for(data = actives; data != NULL; data = data->next)
1938 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1939 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);
1941 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
1942 CBREG(transfer, trans_p, transferprog, NULL, NULL);
1943 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
1947 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
1951 new = smalloc(sizeof(*new));
1953 new->name = swcsdup(name);
1964 static void freeuser(struct uiuser *user)
1966 if(user->next != NULL)
1967 user->next->prev = user->prev;
1968 if(user->prev != NULL)
1969 user->prev->next = user->next;
1976 static int conf_user(int argc, wchar_t **argv)
1978 int i, perms, permmod;
1979 struct uiuser *user;
1984 flog(LOG_WARNING, "not enough arguments given for user command");
1988 for(i = 2; i < argc; i++)
1990 if(!iswalpha(argv[i][0]))
1994 if(!wcscmp(p, L"disallow"))
1995 permmod = PERM_DISALLOW;
1996 if(!wcscmp(p, L"admin"))
1997 permmod = PERM_ADMIN;
1998 if(!wcscmp(p, L"fnetctl"))
1999 permmod = PERM_FNETCTL;
2000 if(!wcscmp(p, L"trans"))
2001 permmod = PERM_TRANS;
2002 if(!wcscmp(p, L"transcu"))
2003 permmod = PERM_TRANSCU;
2004 if(!wcscmp(p, L"chat"))
2005 permmod = PERM_CHAT;
2006 if(!wcscmp(p, L"srch"))
2007 permmod = PERM_SRCH;
2008 if(!wcscmp(p, L"all"))
2010 if(argv[i][0] == L'-')
2015 if((user = finduser(argv[1])) == NULL)
2017 newuser(argv[1], perms);
2020 user->perms = perms;
2025 static void preinit(int hup)
2027 struct uiuser *user;
2031 newuser(L"default", 0);
2033 for(user = users; user != NULL; user = user->next)
2035 if(!wcscmp(user->name, L"default"))
2042 static struct sockaddr *getnameforport(int port, socklen_t *len)
2044 static struct sockaddr_in6 addr;
2046 memset(&addr, 0, sizeof(addr));
2047 addr.sin6_family = AF_INET6;
2048 addr.sin6_port = htons(port);
2049 addr.sin6_addr = in6addr_any;
2051 *len = sizeof(addr);
2052 return((struct sockaddr *)&addr);
2055 static struct sockaddr *getnameforport(int port, socklen_t *len)
2057 static struct sockaddr_in addr;
2059 memset(&addr, 0, sizeof(addr));
2060 addr.sin_family = AF_INET;
2061 addr.sin_port = htons(port);
2063 *len = sizeof(addr);
2064 return((struct sockaddr *)&addr);
2068 static int portupdate(struct configvar *var, void *uudata)
2070 struct sockaddr *addr;
2072 struct socket *newsock;
2074 addr = getnameforport(var->val.num, &addrlen);
2075 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
2077 flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno));
2080 if(uisocket != NULL)
2086 static int init(int hup)
2088 struct sockaddr *addr;
2090 struct uiuser *user, *next;
2094 for(user = users; user != NULL; user = next)
2103 if(uisocket != NULL)
2105 addr = getnameforport(confgetint("ui", "port"), &addrlen);
2106 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
2108 flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno));
2111 CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL);
2112 GCBREG(newfncb, newfnetnode, NULL);
2113 GCBREG(newtransfercb, newtransfernotify, NULL);
2118 static int run(void)
2121 struct uidata *data, *next;
2122 struct qcommand *qcmd;
2123 struct notif *notif, *nnotif;
2126 for(data = actives; data != NULL; data = next)
2132 for(data = actives; data != NULL; data = data->next)
2134 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2136 nnotif = notif->next;
2137 if(notif->state == NOTIF_WAIT)
2140 for(i = 0; i < notif->argc; i++)
2142 if(notif->argv[i].dt == NOTIF_ID)
2144 id = notif->argv[i].d.n;
2148 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2150 sq(data->sk, 2, L"%%i", notif->code, NULL);
2151 for(i = 0; i < notif->argc; i++)
2153 switch(notif->argv[i].dt)
2157 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2160 if(notif->argv[i].d.s[0] == L'%')
2161 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2163 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2166 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2167 sq(data->sk, 2, buf, NULL);
2171 sq(data->sk, 0, NULL);
2172 if(notif->rlimit != 0)
2174 notif->state = NOTIF_WAIT;
2175 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2180 if((qcmd = unlinkqcmd(data)) != NULL)
2182 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2190 static void terminate(void)
2192 while(users != NULL)
2196 static struct configvar myvars[] =
2198 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2199 {CONF_VAR_INT, "port", {.num = 1500}},
2200 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2201 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2205 static struct configcmd mycmds[] =
2207 {"user", conf_user},
2211 static struct module me =
2222 .terminate = terminate