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)
742 transferaddarg(transfer, argv[i], argv[i + 1]);
744 sq(sk, 0, L"200", L"%%i", transfer->id, L"Download queued", NULL);
745 transfersetactivity(transfer, L"create");
748 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
750 struct transfer *transfer, *pt;
752 havepriv(PERM_TRANS);
754 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
756 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
759 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->dir,
760 L"%%i", pt->state, pt->peerid,
761 (pt->peernick == NULL)?L"":(pt->peernick),
762 (pt->path == NULL)?L"":(pt->path),
763 L"%%i", pt->size, L"%%i", pt->curpos,
769 sq(sk, 0, L"201", L"No transfers", NULL);
771 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->dir,
772 L"%%i", pt->state, pt->peerid,
773 (pt->peernick == NULL)?L"":(pt->peernick),
774 (pt->path == NULL)?L"":(pt->path),
775 L"%%i", pt->size, L"%%i", pt->curpos,
779 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
781 struct transfer *transfer;
784 havepriv(PERM_TRANS);
785 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
787 sq(sk, 0, L"512", L"No such transfer", NULL);
790 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
792 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
795 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
797 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
801 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
804 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
810 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
813 for(i = 1; i < argc; i += 2)
815 if(!wcscasecmp(argv[i + 1], L"on"))
819 if(!wcscasecmp(argv[i], L"all"))
825 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
826 data->notify.b.fnchat = val;
827 } else if(!wcscasecmp(argv[i], L"fn:act")) {
828 data->notify.b.fnact = val;
829 } else if(!wcscasecmp(argv[i], L"trans:act")) {
830 data->notify.b.tract = val;
831 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
832 data->notify.b.trprog = val;
833 } else if(!wcscasecmp(argv[i], L"srch:act")) {
834 data->notify.b.srch = val;
837 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
840 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
847 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
849 sq(sk, 0, L"510", L"No such node", NULL);
852 public = wcstol(argv[2], NULL, 0);
853 if((public != 0) && (public != 1))
855 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
858 if(fn->state != FNN_EST)
860 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
863 if(fnetsendchat(fn, public, argv[3], argv[4]))
866 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
867 else if(errno == EPERM)
868 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
869 else if(errno == EILSEQ)
870 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
872 sq(sk, 0, L"505", L"Could not chat", NULL);
875 sq(sk, 0, L"200", L"Chat string sent", NULL);
878 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
887 srch = newsearch(data->username, NULL);
888 for(i = 1; i < argc; i++)
890 if(!wcscmp(argv[i], L"all"))
892 for(fn = fnetnodes; fn != NULL; fn = fn->next)
894 if(fn->state == FNN_EST)
895 searchaddfn(srch, fn);
899 } else if(!wcscmp(argv[i], L"prio")) {
902 sq(sk, 0, L"501", L"No argument to prio", NULL);
906 srch->prio = wcstol(argv[i], NULL, 0);
907 } else if(iswdigit(*argv[i])) {
908 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
910 sq(sk, 0, L"510", L"No such node", NULL);
914 searchaddfn(srch, fn);
919 if(srch->fnl == NULL)
921 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
927 sq(sk, 0, L"501", L"No search expression found on line", NULL);
931 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
933 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
938 getsexpr(srch->sexpr = sexpr);
940 CBREG(srch, search_eta, srcheta, NULL, NULL);
941 CBREG(srch, search_commit, srchcommit, NULL, NULL);
942 CBREG(srch, search_result, srchres, NULL, NULL);
943 sq(sk, 0, L"200", L"%%i", srch->id, L"%%i", srch->eta - time(NULL), NULL);
947 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
949 struct search *srch, *pt;
955 for(srch = searches; srch != NULL; srch = srch->next)
957 if(!wcscmp(srch->owner, data->username))
960 sq(sk, 1, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
965 sq(sk, 0, L"201", L"No searches", NULL);
967 sq(sk, 0, L"200", L"%%i", pt->id, L"%%i", pt->state, L"%%i", pt->eta - now, L"%%i", pt->numres, NULL);
970 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
978 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
980 sq(sk, 0, L"514", L"No such search", NULL);
983 if(srch->results == NULL)
985 sq(sk, 0, L"201", L"No results", NULL);
987 for(sr = srch->results; sr != NULL; sr = sr->next)
989 swprintf(buf, 64, L"%f", sr->time);
990 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);
995 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1001 havepriv(PERM_SRCH);
1002 /* Note - Programmatical user interfaces must only give one
1003 * argument per command, the multiple argument form is only for
1004 * convenience when manually controlling the daemon via
1005 * eg. telnet. The reason is that the return codes aren't clear
1006 * enough for the multiple argument form. */
1007 for(i = 1; i < argc; i++)
1009 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1011 sq(sk, 0, L"514", L"No such search", NULL);
1016 sq(sk, 0, L"200", L"Search cancelled", NULL);
1019 static void fcmdread(struct socket *sk, struct uidata *data)
1024 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1026 bufcat(data->fcmdbuf, buf, bufsize);
1030 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1032 wchar_t *wbuf, *p, *p2;
1036 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1037 kill(-data->fcmdpid, SIGHUP);
1038 putsock(data->fcmdsk);
1039 data->fcmdsk = NULL;
1040 if(data->fcmdbuf != NULL)
1042 free(data->fcmdbuf);
1043 data->fcmdbuf = NULL;
1045 data->fcmdbufsize = data->fcmdbufdata = 0;
1046 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL);
1049 putsock(data->fcmdsk);
1050 data->fcmdsk = NULL;
1052 if(data->fcmdbuf == NULL)
1054 wbuf = swcsdup(L"");
1056 addtobuf(data->fcmdbuf, 0);
1057 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1058 free(data->fcmdbuf);
1060 data->fcmdbuf = NULL;
1061 data->fcmdbufsize = data->fcmdbufdata = 0;
1064 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1068 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1071 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%%ls", p, NULL);
1077 sq(data->sk, 0, L"201", L"No data returned", NULL);
1079 sq(data->sk, 0, L"200", L"%%ls", p, NULL);
1084 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1090 char *filtercmd, *argbuf;
1091 size_t cargvsize, cargvdata;
1092 struct passwd *pwent;
1095 havepriv(PERM_TRANS);
1096 if((pwent = getpwuid(data->uid)) == NULL)
1098 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1099 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1102 if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir)) == NULL)
1104 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1105 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1109 cargvsize = cargvdata = 0;
1110 addtobuf(cargv, filtercmd);
1111 for(i = 1; i < argc; i++)
1113 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1115 for(i = 0; i < cargvdata; i++)
1118 sq(sk, 0, L"504", L"%Could not convert argument %i into local character set", i, L"%%s", strerror(errno), NULL);
1121 addtobuf(cargv, argbuf);
1123 addtobuf(cargv, NULL);
1124 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)
1126 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1127 sq(sk, 0, L"505", L"System error - Could not fork session", L"%%s", strerror(errno), NULL);
1132 execv(filtercmd, cargv);
1133 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1136 for(pp = cargv; *pp; pp++)
1139 data->fcmdsk = wrapsock(pipe);
1140 data->fcmdpid = pid;
1141 if(data->fcmdbuf != NULL)
1143 free(data->fcmdbuf);
1144 data->fcmdbuf = NULL;
1146 data->fcmdbufsize = data->fcmdbufdata = 0;
1147 data->fcmdsk->data = data;
1148 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1149 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1152 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1154 struct transfer *transfer;
1155 struct transarg *ta;
1158 havepriv(PERM_TRANS);
1159 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1161 sq(sk, 0, L"512", L"No such transfer", NULL);
1164 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1166 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1169 if(transfer->args == NULL)
1171 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1173 for(ta = transfer->args; ta != NULL; ta = ta->next)
1174 sq(sk, ta->next != NULL, L"200", L"%%ls", ta->rec, L"%%ls", ta->val, NULL);
1178 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1180 struct sharecache *node;
1184 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1186 if(node->f.b.type == FILE_REG)
1189 if(node->f.b.hastth)
1193 sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
1200 * Reserved command numbers for nameless commands:
1201 * 0: Issued when a client has connected
1202 * 1: Issued when a named command couldn't be found
1205 static struct command commands[] =
1207 {NULL, cmd_connect},
1208 {NULL, cmd_notfound},
1209 {L"shutdown", cmd_shutdown},
1210 {L"quit", cmd_quit},
1211 {L"lsauth", cmd_lsauth},
1212 {L"login", cmd_login},
1213 {L"pass", cmd_pass},
1214 {L"cnct", cmd_fnetconnect},
1215 {L"lsnodes", cmd_lsnodes},
1216 {L"dcnct", cmd_disconnect},
1217 {L"lspa", cmd_lspa},
1218 {L"lspeers", cmd_lspeers},
1219 {L"download", cmd_download},
1220 {L"lstrans", cmd_lstrans},
1221 {L"cancel", cmd_cancel},
1222 {L"notify", cmd_notify},
1223 {L"sendchat", cmd_sendchat},
1224 {L"search", cmd_search},
1225 {L"lssrch", cmd_lssrch},
1226 {L"lssr", cmd_lssr},
1227 {L"cansrch", cmd_cansrch},
1228 {L"filtercmd", cmd_filtercmd},
1229 {L"lstrarg", cmd_lstrarg},
1230 {L"hashstatus", cmd_hashstatus},
1234 static void freequeuecmd(struct qcommand *qcmd)
1238 if(qcmd->argv != NULL)
1240 for(i = 0; i < qcmd->argc; i++)
1241 free(qcmd->argv[i]);
1247 static struct qcommand *unlinkqcmd(struct uidata *data)
1249 struct qcommand *qcmd;
1254 data->queue = qcmd->next;
1255 if(qcmd == data->queuelast)
1256 data->queuelast = qcmd->next;
1261 static struct notif *newnotif(struct uidata *data, int code, ...)
1263 struct notif *notif;
1267 notif = smalloc(sizeof(*notif));
1268 memset(notif, 0, sizeof(*notif));
1269 notif->rlimit = 0.0;
1272 va_start(args, code);
1273 while((dt = va_arg(args, int)) != NOTIF_END)
1276 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1277 notif->argv[ca].dt = dt;
1282 notif->argv[ca].d.n = va_arg(args, int);
1285 notif->argv[ca].d.s = wcsdup(va_arg(args, wchar_t *));
1288 notif->argv[ca].d.d = va_arg(args, double);
1294 notif->prev = data->lnotif;
1295 if(data->lnotif != NULL)
1296 data->lnotif->next = notif;
1298 data->fnotif = notif;
1299 data->lnotif = notif;
1303 static void freenotif(struct notif *notif)
1307 if(notif->next != NULL)
1308 notif->next->prev = notif->prev;
1309 if(notif->prev != NULL)
1310 notif->prev->next = notif->next;
1311 if(notif == notif->ui->fnotif)
1312 notif->ui->fnotif = notif->next;
1313 if(notif == notif->ui->lnotif)
1314 notif->ui->lnotif = notif->prev;
1315 if(notif->exptimer != NULL)
1316 canceltimer(notif->exptimer);
1317 for(i = 0; i < notif->argc; i++)
1319 if(notif->argv[i].dt == NOTIF_STR)
1320 free(notif->argv[i].d.s);
1322 if(notif->argv != NULL)
1327 static void notifexpire(int cancelled, struct notif *notif)
1329 notif->exptimer = NULL;
1334 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1338 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1340 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1345 for(i = 0; i < notif->argc; i++)
1347 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1362 static void freeuidata(struct uidata *data)
1365 struct qcommand *qcmd;
1367 if(data->next != NULL)
1368 data->next->prev = data->prev;
1369 if(data->prev != NULL)
1370 data->prev->next = data->next;
1372 actives = data->next;
1373 data->sk->readcb = NULL;
1374 data->sk->errcb = NULL;
1376 while((qcmd = unlinkqcmd(data)) != NULL)
1378 iconv_close(data->ichandle);
1379 if(data->cw != NULL)
1381 if(data->cb != NULL)
1383 if(data->argv != NULL)
1385 for(i = 0; i < data->argc; i++)
1386 free(data->argv[i]);
1389 if(data->auth != NULL)
1390 authputhandle(data->auth);
1391 if(data->username != NULL)
1393 if(data->userinfo != NULL)
1394 flog(LOG_INFO, "%ls logged out", data->username);
1395 free(data->username);
1398 while(data->fnotif != NULL)
1399 freenotif(data->fnotif);
1400 if(data->fcmdbuf != NULL)
1401 free(data->fcmdbuf);
1402 if(data->fcmdpid != 0)
1403 kill(-data->fcmdpid, SIGHUP);
1404 if(data->fcmdsk != NULL)
1405 putsock(data->fcmdsk);
1409 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1411 struct qcommand *new;
1413 new = smalloc(sizeof(*new));
1418 if(data->queuelast != NULL)
1419 data->queuelast->next = new;
1420 data->queuelast = new;
1421 if(data->queue == NULL)
1425 static struct uidata *newuidata(struct socket *sk)
1427 struct uidata *data;
1429 data = smalloc(sizeof(*data));
1430 memset(data, 0, sizeof(*data));
1433 data->inbuf = smalloc(1024);
1435 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1437 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1440 data->next = actives;
1443 actives->prev = data;
1448 static void uiread(struct socket *sk, struct uidata *data)
1454 size_t datalen, len2;
1455 struct command *cur;
1457 if(data->indata > 1024)
1459 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1461 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1462 memcpy(data->inbuf + data->indata, newbuf, datalen);
1464 data->indata += datalen;
1465 if(data->cb == NULL)
1467 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1469 data->pp = data->cb;
1474 if(data->cbsize == data->cbdata)
1476 len2 = data->pp - data->cb;
1477 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1478 data->pp = data->cb + len2;
1481 p2 = (char *)(porig = (data->cb + data->cbdata));
1482 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1483 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1484 memmove(data->inbuf, p1, data->indata);
1485 /* Just a sanity check */
1486 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1488 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1491 data->cbdata += (((wchar_t *)p2) - porig);
1497 /* XXX: Should this really just ignore it? */
1505 /* Just a sanity check */
1506 if(data->cbsize != data->cbdata)
1508 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1513 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1521 while(!done && (data->pp - data->cb < data->cbdata))
1526 if(iswspace(*data->pp))
1528 if(*data->pp == L'\r')
1530 if(data->pp == data->cb + data->cbdata - 1)
1535 if(*(++data->pp) == L'\n')
1537 if((data->argv != NULL) && (data->argv[0] != NULL))
1539 for(cur = commands; cur->handler != NULL; cur++)
1541 if(cur->name == NULL)
1543 if(!wcscasecmp(cur->name, data->argv[0]))
1545 queuecmd(data, cur, data->argc, data->argv);
1549 if(cur->handler == NULL)
1550 queuecmd(data, &commands[1], data->argc, data->argv);
1552 queuecmd(data, &commands[1], data->argc, data->argv);
1557 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1558 data->pp = data->cb;
1571 if(iswspace(*data->pp))
1573 addtobuf(data->cw, L'\0');
1574 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1575 data->argv[data->argc++] = data->cw;
1580 } else if(*data->pp == L'\"') {
1583 } else if(*data->pp == L'\\') {
1584 if(data->pp == data->cb + data->cbdata - 1)
1589 addtobuf(data->cw, *(++data->pp));
1592 addtobuf(data->cw, *(data->pp++));
1596 if(*data->pp == L'\"')
1599 } else if(*data->pp == L'\\') {
1600 if(data->pp == data->cb + data->cbdata - 1)
1605 addtobuf(data->cw, *(++(data->pp)));
1607 addtobuf(data->cw, *data->pp);
1615 static void uierror(struct socket *sk, int err, struct uidata *data)
1618 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1622 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1624 struct uidata *uidata;
1626 newsk->data = uidata = newuidata(newsk);
1627 socksettos(newsk, confgetint("ui", "uitos"));
1630 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1631 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1632 queuecmd(uidata, &commands[0], 0, NULL);
1635 static int srcheta(struct search *srch, void *uudata)
1637 struct uidata *data;
1639 for(data = actives; data != NULL; data = data->next)
1641 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1642 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1647 static int srchcommit(struct search *srch, void *uudata)
1649 struct uidata *data;
1651 for(data = actives; data != NULL; data = data->next)
1653 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1654 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1659 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1661 struct uidata *data;
1663 for(data = actives; data != NULL; data = data->next)
1665 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1666 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_INT, sr->size, NOTIF_INT, sr->slots, NOTIF_INT, (sr->fn == NULL)?-1:(sr->fn->id), NOTIF_FLOAT, sr->time, NOTIF_END);
1671 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1673 struct uidata *data;
1675 for(data = actives; data != NULL; data = data->next)
1677 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1678 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1683 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1685 struct uidata *data;
1686 struct notif *notif;
1688 if(!wcscmp(attrib, L"state"))
1690 for(data = actives; data != NULL; data = data->next)
1692 if(data->notify.b.fnact)
1693 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1695 } else if(!wcscmp(attrib, L"name")) {
1696 for(data = actives; data != NULL; data = data->next)
1698 if(data->notify.b.fnact)
1699 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1701 } else if(!wcscmp(attrib, L"numpeers")) {
1702 for(data = actives; data != NULL; data = data->next)
1704 if(data->notify.b.fnact)
1706 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1707 notif->argv[1].d.n = fn->numpeers;
1709 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END);
1716 static int fnunlink(struct fnetnode *fn, void *uudata)
1718 struct uidata *data;
1720 for(data = actives; data != NULL; data = data->next)
1722 if(data->notify.b.fnact)
1723 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1728 static int newfnetnode(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, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1737 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1738 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1739 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1743 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
1745 struct uidata *data;
1747 if(!wcscmp(attrib, L"state"))
1749 for(data = actives; data != NULL; data = data->next)
1751 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1752 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
1754 } else if(!wcscmp(attrib, L"nick")) {
1755 for(data = actives; data != NULL; data = data->next)
1757 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1758 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
1760 } else if(!wcscmp(attrib, L"size")) {
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, 613, NOTIF_ID, transfer->id, NOTIF_INT, transfer->size, NOTIF_END);
1766 } else if(!wcscmp(attrib, L"error")) {
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, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
1772 } else if(!wcscmp(attrib, L"path")) {
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, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
1782 static int transferprog(struct transfer *transfer, void *uudata)
1784 struct uidata *data;
1785 struct notif *notif;
1787 for(data = actives; data != NULL; data = data->next)
1789 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1791 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
1792 notif->argv[1].d.n = transfer->curpos;
1794 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_INT, transfer->curpos, NOTIF_END)->rlimit = 0.5;
1800 static int transferdestroyed(struct transfer *transfer, void *uudata)
1802 struct uidata *data;
1804 for(data = actives; data != NULL; data = data->next)
1806 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
1807 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_END);
1812 static int newtransfernotify(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, 610, NOTIF_ID, transfer->id, NOTIF_INT, transfer->dir, NOTIF_STR, transfer->peerid, NOTIF_STR, (transfer->path == NULL)?L"":transfer->path, NOTIF_END);
1821 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
1822 CBREG(transfer, trans_p, transferprog, NULL, NULL);
1823 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
1827 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
1831 new = smalloc(sizeof(*new));
1833 new->name = swcsdup(name);
1844 static void freeuser(struct uiuser *user)
1846 if(user->next != NULL)
1847 user->next->prev = user->prev;
1848 if(user->prev != NULL)
1849 user->prev->next = user->next;
1856 static int conf_user(int argc, wchar_t **argv)
1858 int i, perms, permmod;
1859 struct uiuser *user;
1864 flog(LOG_WARNING, "not enough arguments given for user command");
1868 for(i = 2; i < argc; i++)
1870 if(!iswalpha(argv[i][0]))
1874 if(!wcscmp(p, L"disallow"))
1875 permmod = PERM_DISALLOW;
1876 if(!wcscmp(p, L"admin"))
1877 permmod = PERM_ADMIN;
1878 if(!wcscmp(p, L"fnetctl"))
1879 permmod = PERM_FNETCTL;
1880 if(!wcscmp(p, L"trans"))
1881 permmod = PERM_TRANS;
1882 if(!wcscmp(p, L"transcu"))
1883 permmod = PERM_TRANSCU;
1884 if(!wcscmp(p, L"chat"))
1885 permmod = PERM_CHAT;
1886 if(!wcscmp(p, L"srch"))
1887 permmod = PERM_SRCH;
1888 if(!wcscmp(p, L"all"))
1890 if(argv[i][0] == L'-')
1895 if((user = finduser(argv[1])) == NULL)
1897 newuser(argv[1], perms);
1900 user->perms = perms;
1905 static void preinit(int hup)
1907 struct uiuser *user;
1911 newuser(L"default", 0);
1913 for(user = users; user != NULL; user = user->next)
1915 if(!wcscmp(user->name, L"default"))
1922 static struct sockaddr *getnameforport(int port, socklen_t *len)
1924 static struct sockaddr_in6 addr;
1926 memset(&addr, 0, sizeof(addr));
1927 addr.sin6_family = AF_INET6;
1928 addr.sin6_port = htons(port);
1929 addr.sin6_addr = in6addr_any;
1931 *len = sizeof(addr);
1932 return((struct sockaddr *)&addr);
1935 static struct sockaddr *getnameforport(int port, socklen_t *len)
1937 static struct sockaddr_in addr;
1939 memset(&addr, 0, sizeof(addr));
1940 addr.sin_family = AF_INET;
1941 addr.sin_port = htons(port);
1943 *len = sizeof(addr);
1944 return((struct sockaddr *)&addr);
1948 static int portupdate(struct configvar *var, void *uudata)
1950 struct sockaddr *addr;
1952 struct socket *newsock;
1954 addr = getnameforport(var->val.num, &addrlen);
1955 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
1957 flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno));
1960 if(uisocket != NULL)
1966 static int init(int hup)
1968 struct sockaddr *addr;
1970 struct uiuser *user, *next;
1974 for(user = users; user != NULL; user = next)
1983 if(uisocket != NULL)
1985 addr = getnameforport(confgetint("ui", "port"), &addrlen);
1986 if((uisocket = netcslistenlocal(SOCK_STREAM, addr, addrlen, uiaccept, NULL)) == NULL)
1988 flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno));
1991 CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL);
1992 GCBREG(newfncb, newfnetnode, NULL);
1993 GCBREG(newtransfercb, newtransfernotify, NULL);
1998 static int run(void)
2001 struct uidata *data, *next;
2002 struct qcommand *qcmd;
2003 struct notif *notif, *nnotif;
2006 for(data = actives; data != NULL; data = next)
2012 for(data = actives; data != NULL; data = data->next)
2014 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2016 nnotif = notif->next;
2017 if(notif->state == NOTIF_WAIT)
2020 for(i = 0; i < notif->argc; i++)
2022 if(notif->argv[i].dt == NOTIF_ID)
2024 id = notif->argv[i].d.n;
2028 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2030 sq(data->sk, 2, L"%%i", notif->code, NULL);
2031 for(i = 0; i < notif->argc; i++)
2033 switch(notif->argv[i].dt)
2037 sq(data->sk, 2, L"%%i", notif->argv[i].d.n, NULL);
2040 if(notif->argv[i].d.s[0] == L'%')
2041 sq(data->sk, 2, L"%%s", notif->argv[i].d.s, NULL);
2043 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2046 swprintf(buf, 64, L"%f", notif->argv[i].d.d);
2047 sq(data->sk, 2, buf, NULL);
2051 sq(data->sk, 0, NULL);
2052 if(notif->rlimit != 0)
2054 notif->state = NOTIF_WAIT;
2055 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2060 if((qcmd = unlinkqcmd(data)) != NULL)
2062 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2070 static void terminate(void)
2072 while(users != NULL)
2076 static struct configvar myvars[] =
2078 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2079 {CONF_VAR_INT, "port", {.num = 1500}},
2080 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2081 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2085 static struct configcmd mycmds[] =
2087 {"user", conf_user},
2091 static struct module me =
2102 .terminate = terminate