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>
45 #include "sysevents.h"
51 #define PERM_DISALLOW 1
53 #define PERM_FNETCTL 4
55 #define PERM_TRANSCU 16
72 void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv);
77 struct qcommand *next;
85 struct uiuser *next, *prev;
94 struct notif *next, *prev;
100 struct timer *exptimer;
115 struct uidata *next, *prev;
117 struct qcommand *queue, *queuelast;
118 struct authhandle *auth;
133 struct uiuser *userinfo;
135 struct notif *fnotif, *lnotif;
137 size_t fcmdbufdata, fcmdbufsize;
139 struct socket *fcmdsk;
142 size_t inbufsize, indata;
143 /* Wordset storage */
146 /* WCS conversation stuff */
147 wchar_t *cb; /* Conversation buffer */
148 size_t cbsize, cbdata;
151 int ps; /* Parser state */
152 wchar_t *pp; /* Current parse pointer */
153 wchar_t *cw; /* Current word (building storage) */
154 size_t cwsize, cwdata;
157 static int srcheta(struct search *srch, void *uudata);
158 static int srchcommit(struct search *srch, void *uudata);
159 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
161 struct uiuser *users = NULL;
162 struct uidata *actives = NULL;
163 struct socket *uisocket = NULL;
165 static wchar_t *quoteword(wchar_t *word)
167 wchar_t *wp, *buf, *bp;
177 for(wp = word; *wp != L'\0'; wp++)
179 if(!dq && iswspace(*wp))
181 if((*wp == L'\\') || (*wp == L'\"'))
188 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
191 for(wp = word; *wp != L'\0'; wp++)
193 if((*wp == L'\\') || (*wp == L'\"'))
203 static void sq(struct socket *sk, int cont, ...)
209 wchar_t *part, *tpart;
210 size_t bufsize, bufdata;
213 bufsize = bufdata = 0;
216 while((part = va_arg(al, wchar_t *)) != NULL)
221 * This kludge demands that all arguments that you call it
222 * with are the size of an int. That happens to be the
223 * case for most datatypes on most platforms and
224 * compilers, but I don't know exactly which ones, and
225 * also a long long is a notable candidate of an arg that
226 * is not the size of an int on 32-bit archs. If it breaks
227 * some existing call on your architecture, please tell
230 part = vswprintf2(tpart = (part + 1), al);
231 for(; *tpart != L'\0'; tpart++)
245 if((tpart = quoteword(part)) != NULL)
252 if((num > 1) || ((num == 1) && !(cont & 1)))
254 bufcat(buf, part, wcslen(part));
255 if((num == 0) && (cont & 1))
262 bufcat(buf, L" \0", 2);
264 bufcat(buf, L"\r\n\0", 3);
265 if((final = icwcstombs(buf, "utf-8")) == NULL)
267 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
273 sockqueue(sk, final, strlen(final));
277 struct uiuser *finduser(wchar_t *name)
281 for(user = users; user != NULL; user = user->next)
283 if(!wcscmp(user->name, name))
289 static void logout(struct uidata *data)
291 data->userinfo = NULL;
292 if(data->username != NULL)
293 free(data->username);
294 data->username = NULL;
295 if(data->auth != NULL)
296 authputhandle(data->auth);
300 static int haspriv(struct uidata *data, int perm)
302 if(data->userinfo == NULL)
304 if(data->userinfo->perms & perm)
310 /* Useful macros for the command functions: */
311 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
312 #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)
314 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
318 if(confgetint("ui", "onlylocal"))
320 switch(sk->remote->sa_family)
323 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
326 valid = !memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback));
334 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
340 sq(sk, 0, L"200", L"%Dolda Connect daemon v%s", VERSION, NULL);
343 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
345 if((argv != NULL) && (argv[0] != NULL))
346 sq(sk, 0, L"500", L"%Command not found: %ls", argv[0], NULL);
348 sq(sk, 0, L"500", L"No command", NULL);
351 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
353 extern volatile int running;
355 havepriv(PERM_ADMIN);
356 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
358 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
361 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
363 sq(sk, 0, L"200", L"Closing connection", NULL);
367 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
369 struct authmech *mech, *prev;
372 for(mech = mechs; mech != NULL; mech = mech->next)
377 sq(sk, 1, L"200", prev->name, NULL);
382 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
384 sq(sk, 0, L"200", prev->name, NULL);
387 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
394 if(data->username != NULL)
396 if(data->userinfo != NULL)
397 sq(sk, 0, L"503", L"Already logged in", NULL);
399 sq(sk, 0, L"503", L"Already logging in", NULL);
402 if((buf = icwcstombs(argv[2], NULL)) == NULL)
404 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
407 data->username = swcsdup(argv[2]);
408 if((pwd = getpwnam(buf)) == NULL)
411 data->uid = pwd->pw_uid;
412 if((data->auth = initauth(argv[1], buf)) == NULL)
415 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
417 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%%s", strerror(errno), NULL);
423 switch(authenticate(data->auth, NULL))
426 data->userinfo = finduser(data->username);
427 if(data->userinfo == NULL)
428 data->userinfo = finduser(L"default");
431 sq(sk, 0, L"506", L"Authentication error", NULL);
432 flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
434 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
435 sq(sk, 0, L"506", L"Authentication error", NULL);
436 flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
439 sq(sk, 0, L"200", L"Welcome", NULL);
440 flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
444 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
448 switch(data->auth->prompt)
466 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
469 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
473 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
474 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
480 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
486 if((buf = icwcstombs(argv[1], NULL)) == NULL)
488 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
491 if((data->auth == NULL) || (data->userinfo != NULL))
493 sq(sk, 0, L"507", L"Data not expected", NULL);
496 switch(authenticate(data->auth, buf))
499 data->userinfo = finduser(data->username);
500 if(data->userinfo == NULL)
501 data->userinfo = finduser(L"default");
504 sq(sk, 0, L"506", L"Authentication error", NULL);
505 flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
507 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
508 sq(sk, 0, L"506", L"Authentication error", NULL);
509 flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
512 sq(sk, 0, L"200", L"Welcome", NULL);
513 flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
517 sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
521 switch(data->auth->prompt)
539 sq(sk, 0, L"%%i", code, data->auth->text, NULL);
542 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
546 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
547 sq(sk, 0, L"505", L"System error", L"%%s", strerror(errno), NULL);
554 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
562 havepriv(PERM_FNETCTL);
563 if((buf = icwcstombs(argv[2], NULL)) == NULL)
565 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
568 fn = fnetinitconnect(argv[1], buf);
573 if(errno == EPROTONOSUPPORT)
574 sq(sk, 0, L"511", L"No such network name", NULL);
576 sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
579 for(i = 3; i < argc - 1; i += 2)
581 if(!wcscmp(argv[i], L"nick"))
582 fnetsetnick(fn, argv[i + 1]);
585 fnetsetname(fn, argv[2]);
587 sq(sk, 0, L"200", L"Connection under way", NULL);
590 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
594 if(fnetnodes == NULL)
596 sq(sk, 0, L"201", L"No connected nodes", NULL);
599 for(fn = fnetnodes; fn != NULL; fn = fn->next)
601 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);
605 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
611 havepriv(PERM_FNETCTL);
612 /* Note - Programmatical user interfaces must only give one
613 * argument per command, the multiple argument form is only for
614 * convenience when manually controlling the daemon via
615 * eg. telnet. The reason is that the return codes aren't clear
616 * enough for the multiple argument form. */
617 for(i = 1; i < argc; i++)
619 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
621 sq(sk, 0, L"510", L"No such node", NULL);
627 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
630 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
633 struct fnetpeerdatum *datum;
636 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
638 sq(sk, 0, L"510", L"No such node", NULL);
641 if(fn->peerdata == NULL)
643 sq(sk, 0, L"201", L"No data available", NULL);
645 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
646 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%%i", datum->datatype, NULL);
650 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
654 struct fnetpeer *peer;
658 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
660 sq(sk, 0, L"510", L"No such node", NULL);
663 if(fn->peers == NULL)
665 sq(sk, 0, L"201", L"No peers avaiable", NULL);
667 for(peer = fn->peers; peer != NULL; peer = peer->next)
669 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", peer->id, peer->nick, NULL);
670 for(i = 0; i < peer->dinum; i++)
672 if(peer->peerdi[i].datum->datatype == FNPD_INT)
673 sq(sk, 2, peer->peerdi[i].datum->id, L"%%i", peer->peerdi[i].data.num, NULL);
674 /* Note: A long long is not the size of an int, so
675 * sq() can't handle the conversion itself. */
676 if(peer->peerdi[i].datum->datatype == FNPD_LL)
678 swprintf(buf, 40, L"%lli", peer->peerdi[i].data.lnum);
679 sq(sk, 2, peer->peerdi[i].datum->id, buf, NULL);
681 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
682 sq(sk, 2, peer->peerdi[i].datum->id, peer->peerdi[i].data.str, NULL);
689 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
694 struct transfer *transfer;
695 struct fnetpeer *peer;
698 if((argc > 5) && ((argc % 2) == 0))
700 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
703 havepriv(PERM_TRANS);
704 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
706 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
708 sq(sk, 0, L"510", L"No such node", NULL);
714 if((net = findfnet(argv[1])) == NULL)
716 sq(sk, 0, L"511", L"No such network name", NULL);
720 transfer = newtransfer();
721 authgethandle(transfer->auth = data->auth);
722 transfer->fnet = net;
723 transfer->peerid = swcsdup(argv[2]);
724 transfer->path = swcsdup(argv[3]);
725 transfer->dir = TRNSD_DOWN;
726 transfer->owner = data->uid;
731 linktransfer(transfer);
732 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
733 transfersetnick(transfer, peer->nick);
735 linktransfer(transfer);
738 transfersetsize(transfer, wcstol(argv[4], NULL, 0));
741 for(i = 5; i < argc; i += 2)
743 if(!wcscmp(argv[i], L"hash"))
745 transfer->hash = parsehash(argv[i + 1]);
747 transferaddarg(transfer, argv[i], argv[i + 1]);
751 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
752 transfersetactivity(transfer, L"create");
755 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
757 struct transfer *transfer, *pt;
759 havepriv(PERM_TRANS);
761 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
763 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
766 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
767 L"%%i", pt->state, pt->peerid,
768 (pt->peernick == NULL)?L"":(pt->peernick),
769 (pt->path == NULL)?L"":(pt->path),
770 L"%%i", pt->size, L"%%i", pt->curpos,
771 (pt->hash == NULL)?L"":unparsehash(pt->hash),
777 sq(sk, 0, L"201", L"No transfers", NULL);
779 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
780 L"%%i", pt->state, pt->peerid,
781 (pt->peernick == NULL)?L"":(pt->peernick),
782 (pt->path == NULL)?L"":(pt->path),
783 L"%%i", pt->size, L"%%i", pt->curpos,
784 (pt->hash == NULL)?L"":unparsehash(pt->hash),
788 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
790 struct transfer *transfer;
793 havepriv(PERM_TRANS);
794 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
796 sq(sk, 0, L"512", L"No such transfer", NULL);
799 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
801 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
804 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
806 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
810 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
813 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
819 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
822 for(i = 1; i < argc; i += 2)
824 if(!wcscasecmp(argv[i + 1], L"on"))
828 if(!wcscasecmp(argv[i], L"all"))
834 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
835 data->notify.b.fnchat = val;
836 } else if(!wcscasecmp(argv[i], L"fn:act")) {
837 data->notify.b.fnact = val;
838 } else if(!wcscasecmp(argv[i], L"trans:act")) {
839 data->notify.b.tract = val;
840 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
841 data->notify.b.trprog = val;
842 } else if(!wcscasecmp(argv[i], L"srch:act")) {
843 data->notify.b.srch = val;
846 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
849 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
856 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
858 sq(sk, 0, L"510", L"No such node", NULL);
861 public = wcstol(argv[2], NULL, 0);
862 if((public != 0) && (public != 1))
864 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
867 if(fn->state != FNN_EST)
869 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
872 if(fnetsendchat(fn, public, argv[3], argv[4]))
875 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
876 else if(errno == EPERM)
877 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
878 else if(errno == EILSEQ)
879 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
881 sq(sk, 0, L"505", L"Could not chat", NULL);
884 sq(sk, 0, L"200", L"Chat string sent", NULL);
887 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
896 srch = newsearch(data->username, NULL);
897 for(i = 1; i < argc; i++)
899 if(!wcscmp(argv[i], L"all"))
901 for(fn = fnetnodes; fn != NULL; fn = fn->next)
903 if(fn->state == FNN_EST)
904 searchaddfn(srch, fn);
908 } else if(!wcscmp(argv[i], L"prio")) {
911 sq(sk, 0, L"501", L"No argument to prio", NULL);
915 srch->prio = wcstol(argv[i], NULL, 0);
916 } else if(iswdigit(*argv[i])) {
917 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
919 sq(sk, 0, L"510", L"No such node", NULL);
923 searchaddfn(srch, fn);
928 if(srch->fnl == NULL)
930 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
936 sq(sk, 0, L"501", L"No search expression found on line", NULL);
940 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
942 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
947 getsexpr(srch->sexpr = sexpr);
949 CBREG(srch, search_eta, srcheta, NULL, NULL);
950 CBREG(srch, search_commit, srchcommit, NULL, NULL);
951 CBREG(srch, search_result, srchres, NULL, NULL);
952 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
956 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
958 struct search *srch, *pt;
964 for(srch = searches; srch != NULL; srch = srch->next)
966 if(!wcscmp(srch->owner, data->username))
969 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
974 sq(sk, 0, L"201", L"No searches", NULL);
976 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
979 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
987 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
989 sq(sk, 0, L"514", L"No such search", NULL);
992 if(srch->results == NULL)
994 sq(sk, 0, L"201", L"No results", NULL);
996 for(sr = srch->results; sr != NULL; sr = sr->next)
998 swprintf(buf, 64, L"%f", sr->time);
999 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, NULL);
1004 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1006 struct search *srch;
1010 havepriv(PERM_SRCH);
1011 /* Note - Programmatical user interfaces must only give one
1012 * argument per command, the multiple argument form is only for
1013 * convenience when manually controlling the daemon via
1014 * eg. telnet. The reason is that the return codes aren't clear
1015 * enough for the multiple argument form. */
1016 for(i = 1; i < argc; i++)
1018 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1020 sq(sk, 0, L"514", L"No such search", NULL);
1025 sq(sk, 0, L"200", L"Search cancelled", NULL);
1028 static void fcmdread(struct socket *sk, struct uidata *data)
1033 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1035 bufcat(data->fcmdbuf, buf, bufsize);
1039 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1041 wchar_t *wbuf, *p, *p2;
1045 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1046 kill(-data->fcmdpid, SIGHUP);
1047 putsock(data->fcmdsk);
1048 data->fcmdsk = NULL;
1049 if(data->fcmdbuf != NULL)
1051 free(data->fcmdbuf);
1052 data->fcmdbuf = NULL;
1054 data->fcmdbufsize = data->fcmdbufdata = 0;
1055 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1058 putsock(data->fcmdsk);
1059 data->fcmdsk = NULL;
1061 if(data->fcmdbuf == NULL)
1063 wbuf = swcsdup(L"");
1065 addtobuf(data->fcmdbuf, 0);
1066 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1067 free(data->fcmdbuf);
1069 data->fcmdbuf = NULL;
1070 data->fcmdbufsize = data->fcmdbufdata = 0;
1073 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1077 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1080 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1086 sq(data->sk, 0, L"201", L"No data returned", NULL);
1088 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1093 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1099 char *filtercmd, *argbuf;
1100 size_t cargvsize, cargvdata;
1101 struct passwd *pwent;
1104 havepriv(PERM_TRANS);
1105 if((pwent = getpwuid(data->uid)) == NULL)
1107 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1108 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1111 if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir)) == NULL)
1113 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1114 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1118 cargvsize = cargvdata = 0;
1119 addtobuf(cargv, filtercmd);
1120 for(i = 1; i < argc; i++)
1122 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1124 for(i = 0; i < cargvdata; i++)
1127 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1130 addtobuf(cargv, argbuf);
1132 addtobuf(cargv, NULL);
1133 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)
1135 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1136 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1141 execv(filtercmd, cargv);
1142 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1145 for(pp = cargv; *pp; pp++)
1148 data->fcmdsk = wrapsock(pipe);
1149 data->fcmdpid = pid;
1150 if(data->fcmdbuf != NULL)
1152 free(data->fcmdbuf);
1153 data->fcmdbuf = NULL;
1155 data->fcmdbufsize = data->fcmdbufdata = 0;
1156 data->fcmdsk->data = data;
1157 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1158 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1161 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1163 struct transfer *transfer;
1164 struct transarg *ta;
1167 havepriv(PERM_TRANS);
1168 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1170 sq(sk, 0, L"512", L"No such transfer", NULL);
1173 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1175 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1178 if(transfer->args == NULL)
1180 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1182 for(ta = transfer->args; ta != NULL; ta = ta->next)
1183 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->rec, L"%%ls", ta->val, NULL);
1187 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1189 struct sharecache *node;
1193 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1195 if(node->f.b.type == FILE_REG)
1198 if(node->f.b.hastth)
1202 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1209 * Reserved command numbers for nameless commands:
1210 * 0: Issued when a client has connected
1211 * 1: Issued when a named command couldn't be found
1214 static struct command commands[] =
1216 {NULL, cmd_connect},
1217 {NULL, cmd_notfound},
1218 {L"shutdown", cmd_shutdown},
1219 {L"quit", cmd_quit},
1220 {L"lsauth", cmd_lsauth},
1221 {L"login", cmd_login},
1222 {L"pass", cmd_pass},
1223 {L"cnct", cmd_fnetconnect},
1224 {L"lsnodes", cmd_lsnodes},
1225 {L"dcnct", cmd_disconnect},
1226 {L"lspa", cmd_lspa},
1227 {L"lspeers", cmd_lspeers},
1228 {L"download", cmd_download},
1229 {L"lstrans", cmd_lstrans},
1230 {L"cancel", cmd_cancel},
1231 {L"notify", cmd_notify},
1232 {L"sendchat", cmd_sendchat},
1233 {L"search", cmd_search},
1234 {L"lssrch", cmd_lssrch},
1235 {L"lssr", cmd_lssr},
1236 {L"cansrch", cmd_cansrch},
1237 {L"filtercmd", cmd_filtercmd},
1238 {L"lstrarg", cmd_lstrarg},
1239 {L"hashstatus", cmd_hashstatus},
1243 static void freequeuecmd(struct qcommand *qcmd)
1247 if(qcmd->argv != NULL)
1249 for(i = 0; i < qcmd->argc; i++)
1250 free(qcmd->argv[i]);
1256 static struct qcommand *unlinkqcmd(struct uidata *data)
1258 struct qcommand *qcmd;
1263 data->queue = qcmd->next;
1264 if(qcmd == data->queuelast)
1265 data->queuelast = qcmd->next;
1270 static struct notif *newnotif(struct uidata *data, int code, ...)
1272 struct notif *notif;
1276 notif = smalloc(sizeof(*notif));
1277 memset(notif, 0, sizeof(*notif));
1278 notif->rlimit = 0.0;
1281 va_start(args, code);
1282 while((dt = va_arg(args, int)) != NOTIF_END)
1285 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1286 notif->argv[ca].dt = dt;
1291 notif->argv[ca].d.n = va_arg(args, int);
1294 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1297 notif->argv[ca].d.d = va_arg(args, double);
1303 notif->prev = data->lnotif;
1304 if(data->lnotif != NULL)
1305 data->lnotif->next = notif;
1307 data->fnotif = notif;
1308 data->lnotif = notif;
1312 static void freenotif(struct notif *notif)
1316 if(notif->next != NULL)
1317 notif->next->prev = notif->prev;
1318 if(notif->prev != NULL)
1319 notif->prev->next = notif->next;
1320 if(notif == notif->ui->fnotif)
1321 notif->ui->fnotif = notif->next;
1322 if(notif == notif->ui->lnotif)
1323 notif->ui->lnotif = notif->prev;
1324 if(notif->exptimer != NULL)
1325 canceltimer(notif->exptimer);
1326 for(i = 0; i < notif->argc; i++)
1328 if(notif->argv[i].dt == NOTIF_STR)
1329 free(notif->argv[i].d.s);
1331 if(notif->argv != NULL)
1336 static void notifexpire(int cancelled, struct notif *notif)
1338 notif->exptimer = NULL;
1343 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1347 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1349 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1354 for(i = 0; i < notif->argc; i++)
1356 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1371 static void freeuidata(struct uidata *data)
1374 struct qcommand *qcmd;
1376 if(data->next != NULL)
1377 data->next->prev = data->prev;
1378 if(data->prev != NULL)
1379 data->prev->next = data->next;
1381 actives = data->next;
1382 data->sk->readcb = NULL;
1383 data->sk->errcb = NULL;
1385 while((qcmd = unlinkqcmd(data)) != NULL)
1387 iconv_close(data->ichandle);
1388 if(data->cw != NULL)
1390 if(data->cb != NULL)
1392 if(data->argv != NULL)
1394 for(i = 0; i < data->argc; i++)
1395 free(data->argv[i]);
1398 if(data->auth != NULL)
1399 authputhandle(data->auth);
1400 if(data->username != NULL)
1402 if(data->userinfo != NULL)
1403 flog(LOG_INFO, "%ls logged out", data->username);
1404 free(data->username);
1407 while(data->fnotif != NULL)
1408 freenotif(data->fnotif);
1409 if(data->fcmdbuf != NULL)
1410 free(data->fcmdbuf);
1411 if(data->fcmdpid != 0)
1412 kill(-data->fcmdpid, SIGHUP);
1413 if(data->fcmdsk != NULL)
1414 putsock(data->fcmdsk);
1418 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1420 struct qcommand *new;
1422 new = smalloc(sizeof(*new));
1427 if(data->queuelast != NULL)
1428 data->queuelast->next = new;
1429 data->queuelast = new;
1430 if(data->queue == NULL)
1434 static struct uidata *newuidata(struct socket *sk)
1436 struct uidata *data;
1438 data = smalloc(sizeof(*data));
1439 memset(data, 0, sizeof(*data));
1442 data->inbuf = smalloc(1024);
1444 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1446 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1449 data->next = actives;
1452 actives->prev = data;
1457 static void uiread(struct socket *sk, struct uidata *data)
1463 size_t datalen, len2;
1464 struct command *cur;
1466 if(data->indata > 1024)
1468 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1470 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1471 memcpy(data->inbuf + data->indata, newbuf, datalen);
1473 data->indata += datalen;
1474 if(data->cb == NULL)
1476 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1478 data->pp = data->cb;
1483 if(data->cbsize == data->cbdata)
1485 len2 = data->pp - data->cb;
1486 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1487 data->pp = data->cb + len2;
1490 p2 = (char *)(porig = (data->cb + data->cbdata));
1491 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1492 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1493 memmove(data->inbuf, p1, data->indata);
1494 /* Just a sanity check */
1495 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1497 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1500 data->cbdata += (((wchar_t *)p2) - porig);
1506 /* XXX: Should this really just ignore it? */
1514 /* Just a sanity check */
1515 if(data->cbsize != data->cbdata)
1517 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1522 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1530 while(!done && (data->pp - data->cb < data->cbdata))
1535 if(iswspace(*data->pp))
1537 if(*data->pp == L'\r')
1539 if(data->pp == data->cb + data->cbdata - 1)
1544 if(*(++data->pp) == L'\n')
1546 if((data->argv != NULL) && (data->argv[0] != NULL))
1548 for(cur = commands; cur->handler != NULL; cur++)
1550 if(cur->name == NULL)
1552 if(!wcscasecmp(cur->name, data->argv[0]))
1554 queuecmd(data, cur, data->argc, data->argv);
1558 if(cur->handler == NULL)
1559 queuecmd(data, &commands[1], data->argc, data->argv);
1561 queuecmd(data, &commands[1], data->argc, data->argv);
1566 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1567 data->pp = data->cb;
1580 if(iswspace(*data->pp))
1582 addtobuf(data->cw, L'\0');
1583 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1584 data->argv[data->argc++] = data->cw;
1589 } else if(*data->pp == L'\"') {
1592 } else if(*data->pp == L'\\') {
1593 if(data->pp == data->cb + data->cbdata - 1)
1598 addtobuf(data->cw, *(++data->pp));
1601 addtobuf(data->cw, *(data->pp++));
1605 if(*data->pp == L'\"')
1608 } else if(*data->pp == L'\\') {
1609 if(data->pp == data->cb + data->cbdata - 1)
1614 addtobuf(data->cw, *(++(data->pp)));
1616 addtobuf(data->cw, *data->pp);
1624 static void uierror(struct socket *sk, int err, struct uidata *data)
1627 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1631 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1633 struct uidata *uidata;
1635 newsk->data = uidata = newuidata(newsk);
1636 socksettos(newsk, confgetint("ui", "uitos"));
1639 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1640 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1641 queuecmd(uidata, &commands[0], 0, NULL);
1644 static int srcheta(struct search *srch, void *uudata)
1646 struct uidata *data;
1648 for(data = actives; data != NULL; data = data->next)
1650 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1651 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1656 static int srchcommit(struct search *srch, void *uudata)
1658 struct uidata *data;
1660 for(data = actives; data != NULL; data = data->next)
1662 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1663 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1668 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1670 struct uidata *data;
1672 for(data = actives; data != NULL; data = data->next)
1674 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1676 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size,
1677 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);
1683 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1685 struct uidata *data;
1687 for(data = actives; data != NULL; data = data->next)
1689 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1690 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1695 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1697 struct uidata *data;
1698 struct notif *notif;
1700 if(!wcscmp(attrib, L"state"))
1702 for(data = actives; data != NULL; data = data->next)
1704 if(data->notify.b.fnact)
1705 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1707 } else if(!wcscmp(attrib, L"name")) {
1708 for(data = actives; data != NULL; data = data->next)
1710 if(data->notify.b.fnact)
1711 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1713 } else if(!wcscmp(attrib, L"numpeers")) {
1714 for(data = actives; data != NULL; data = data->next)
1716 if(data->notify.b.fnact)
1718 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1719 notif->argv[1].d.n = fn->numpeers;
1721 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END);
1728 static int fnunlink(struct fnetnode *fn, void *uudata)
1730 struct uidata *data;
1732 for(data = actives; data != NULL; data = data->next)
1734 if(data->notify.b.fnact)
1735 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1740 static int newfnetnode(struct fnetnode *fn, void *uudata)
1742 struct uidata *data;
1744 for(data = actives; data != NULL; data = data->next)
1746 if(data->notify.b.fnact)
1747 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1749 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1750 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1751 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1755 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1757 struct uidata *data;
1759 if(!wcscmp(attrib, L"state"))
1761 for(data = actives; data != NULL; data = data->next)
1763 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1764 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1766 } else if(!wcscmp(attrib, L"nick")) {
1767 for(data = actives; data != NULL; data = data->next)
1769 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1770 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1772 } else if(!wcscmp(attrib, L"size")) {
1773 for(data = actives; data != NULL; data = data->next)
1775 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1776 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1778 } else if(!wcscmp(attrib, L"error")) {
1779 for(data = actives; data != NULL; data = data->next)
1781 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1782 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1784 } else if(!wcscmp(attrib, L"path")) {
1785 for(data = actives; data != NULL; data = data->next)
1787 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1788 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1794 static int transferprog(struct transfer *transfer, void *uudata)
1796 struct uidata *data;
1797 struct notif *notif;
1799 for(data = actives; data != NULL; data = data->next)
1801 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1803 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
1804 notif->argv[1].d.n = transfer->curpos;
1806 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
1812 static int transferdestroyed(struct transfer *transfer, void *uudata)
1814 struct uidata *data;
1816 for(data = actives; data != NULL; data = data->next)
1818 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1819 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_END);
1824 static int newtransfernotify(struct transfer *transfer, void *uudata)
1826 struct uidata *data;
1828 for(data = actives; data != NULL; data = data->next)
1830 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1831 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);
1833 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
1834 CBREG(transfer, trans_p, transferprog, NULL, NULL);
1835 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
1839 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
1843 new = smalloc(sizeof(*new));
1845 new->name = swcsdup(name);
1856 static void freeuser(struct uiuser *user)
1858 if(user->next != NULL)
1859 user->next->prev = user->prev;
1860 if(user->prev != NULL)
1861 user->prev->next = user->next;
1868 static int conf_user(int argc, wchar_t **argv)
1870 int i, perms, permmod;
1871 struct uiuser *user;
1876 flog(LOG_WARNING, "not enough arguments given for user command");
1880 for(i = 2; i < argc; i++)
1882 if(!iswalpha(argv[i][0]))
1886 if(!wcscmp(p, L"disallow"))
1887 permmod = PERM_DISALLOW;
1888 if(!wcscmp(p, L"admin"))
1889 permmod = PERM_ADMIN;
1890 if(!wcscmp(p, L"fnetctl"))
1891 permmod = PERM_FNETCTL;
1892 if(!wcscmp(p, L"trans"))
1893 permmod = PERM_TRANS;
1894 if(!wcscmp(p, L"transcu"))
1895 permmod = PERM_TRANSCU;
1896 if(!wcscmp(p, L"chat"))
1897 permmod = PERM_CHAT;
1898 if(!wcscmp(p, L"srch"))
1899 permmod = PERM_SRCH;
1900 if(!wcscmp(p, L"all"))
1902 if(argv[i][0] == L'-')
1907 if((user = finduser(argv[1])) == NULL)
1909 newuser(argv[1], perms);
1912 user->perms = perms;
1917 static void preinit(int hup)
1919 struct uiuser *user;
1923 newuser(L"default", 0);
1925 for(user = users; user != NULL; user = user->next)
1927 if(!wcscmp(user->name, L"default"))
1934 static struct sockaddr *getnameforport(int port, socklen_t *len)
1936 static struct sockaddr_in6 addr;
1938 memset(&addr, 0, sizeof(addr));
1939 addr.sin6_family = AF_INET6;
1940 addr.sin6_port = htons(port);
1941 addr.sin6_addr = in6addr_any;
1943 *len = sizeof(addr);
1944 return((struct sockaddr *)&addr);
1947 static struct sockaddr *getnameforport(int port, socklen_t *len)
1949 static struct sockaddr_in addr;
1951 memset(&addr, 0, sizeof(addr));
1952 addr.sin_family = AF_INET;
1953 addr.sin_port = htons(port);
1955 *len = sizeof(addr);
1956 return((struct sockaddr *)&addr);
1960 static int portupdate(struct configvar *var, void *uudata)
1962 struct sockaddr *addr;
1964 struct socket *newsock;
1966 addr = getnameforport(var->val.num, &addrlen);
1967 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
1969 flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno));
1972 if(uisocket != NULL)
1978 static int init(int hup)
1980 struct sockaddr *addr;
1982 struct uiuser *user, *next;
1986 for(user = users; user != NULL; user = next)
1995 if(uisocket != NULL)
1997 addr = getnameforport(confgetint("ui", "port"), &addrlen);
1998 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
2000 flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno));
2003 CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL);
2004 GCBREG(newfncb, newfnetnode, NULL);
2005 GCBREG(newtransfercb, newtransfernotify, NULL);
2010 static int run(void)
2013 struct uidata *data, *next;
2014 struct qcommand *qcmd;
2015 struct notif *notif, *nnotif;
2018 for(data = actives; data != NULL; data = next)
2024 for(data = actives; data != NULL; data = data->next)
2026 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2028 nnotif = notif->next;
2029 if(notif->state == NOTIF_WAIT)
2032 for(i = 0; i < notif->argc; i++)
2034 if(notif->argv[i].dt == NOTIF_ID)
2036 id = notif->argv[i].d.n;
2040 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2042 sq(data->sk, 2, L"%%i", notif->code, NULL);
2043 for(i = 0; i < notif->argc; i++)
2045 switch(notif->argv[i].dt)
2049 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2052 if(notif->argv[i].d.s[0] == L'%')
2053 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2055 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2058 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2059 sq(data->sk, 2, buf, NULL);
2063 sq(data->sk, 0, NULL);
2064 if(notif->rlimit != 0)
2066 notif->state = NOTIF_WAIT;
2067 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2072 if((qcmd = unlinkqcmd(data)) != NULL)
2074 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2082 static void terminate(void)
2084 while(users != NULL)
2088 static struct configvar myvars[] =
2090 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2091 {CONF_VAR_INT, "port", {.num = 1500}},
2092 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2093 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2097 static struct configcmd mycmds[] =
2099 {"user", conf_user},
2103 static struct module me =
2114 .terminate = terminate