2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/ip6.h>
25 #include <arpa/inet.h>
47 #include "sysevents.h"
53 #define PERM_DISALLOW 1
55 #define PERM_FNETCTL 4
57 #define PERM_TRANSCU 16
75 void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv);
80 struct qcommand *next;
88 struct uiuser *next, *prev;
97 struct notif *next, *prev;
103 struct timer *exptimer;
119 struct uidata *next, *prev;
121 struct qcommand *queue, *queuelast;
123 struct authhandle *auth;
140 struct uiuser *userinfo;
144 struct notif *fnotif, *lnotif;
146 size_t fcmdbufdata, fcmdbufsize;
148 struct socket *fcmdsk;
151 size_t inbufsize, indata;
152 /* Wordset storage */
155 /* WCS conversation stuff */
156 wchar_t *cb; /* Conversation buffer */
157 size_t cbsize, cbdata;
160 int ps; /* Parser state */
161 wchar_t *pp; /* Current parse pointer */
162 wchar_t *cw; /* Current word (building storage) */
163 size_t cwsize, cwdata;
166 static int srcheta(struct search *srch, void *uudata);
167 static int srchcommit(struct search *srch, void *uudata);
168 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
169 static struct notif *newnotif(struct uidata *data, int code, ...);
170 static void notifappend(struct notif *notif, ...);
172 struct uiuser *users = NULL;
173 struct uidata *actives = NULL;
174 struct socket *tcpsocket = NULL;
175 struct socket *unixsocket = NULL;
176 static time_t starttime;
178 static wchar_t *quoteword(wchar_t *word)
180 wchar_t *wp, *buf, *bp;
190 for(wp = word; *wp != L'\0'; wp++)
192 if(!dq && iswspace(*wp))
194 if((*wp == L'\\') || (*wp == L'\"'))
201 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
204 for(wp = word; *wp != L'\0'; wp++)
206 if((*wp == L'\\') || (*wp == L'\"'))
216 static void sq(struct socket *sk, int cont, ...)
222 wchar_t *part, *tpart;
223 size_t bufsize, bufdata;
226 bufsize = bufdata = 0;
229 while((part = va_arg(al, wchar_t *)) != NULL)
235 if(!wcscmp(tpart, L"i"))
238 part = swprintf2(L"%i", va_arg(al, int));
239 } else if(!wcscmp(tpart, L"zi")) {
241 part = swprintf2(L"%zi", va_arg(al, size_t));
242 } else if(!wcscmp(tpart, L"oi")) {
244 part = swprintf2(L"%ji", (intmax_t)va_arg(al, off_t));
245 } else if(!wcscmp(tpart, L"s")) {
247 part = icmbstowcs(sarg = va_arg(al, char *), NULL);
252 flog(LOG_ERR, "could not convert local string to wcs: %s", sarg);
254 } else if(!wcscmp(tpart, L"ls")) {
255 part = va_arg(al, wchar_t *);
256 } else if(!wcscmp(tpart, L"ll")) {
258 part = swprintf2(L"%lli", va_arg(al, long long));
259 } else if(!wcscmp(tpart, L"f")) {
261 part = swprintf2(L"%f", va_arg(al, double));
262 } else if(!wcscmp(tpart, L"x")) {
264 part = swprintf2(L"%x", va_arg(al, int));
266 flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart);
270 if((tpart = quoteword(part)) != NULL)
277 if((num > 1) || ((num == 1) && !(cont & 1)))
279 bufcat(buf, part, wcslen(part));
280 if((num == 0) && (cont & 1))
287 bufcat(buf, L" \0", 2);
289 bufcat(buf, L"\r\n\0", 3);
290 if((final = icwcstombs(buf, "utf-8")) == NULL)
292 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
298 sockqueue(sk, final, strlen(final));
302 struct uiuser *finduser(wchar_t *name)
306 for(user = users; user != NULL; user = user->next)
308 if(!wcscmp(user->name, name))
314 static void logout(struct uidata *data)
316 data->userinfo = NULL;
317 if(data->username != NULL)
318 free(data->username);
319 data->username = NULL;
320 if(data->auth != NULL)
321 authputhandle(data->auth);
325 static int haspriv(struct uidata *data, int perm)
327 if(data->userinfo == NULL)
329 if(data->userinfo->perms & perm)
335 /* Useful macros for the command functions: */
336 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
337 #define havepriv(p) do { if((data->userinfo == NULL) || ((data->userinfo->perms & (p)) != (p))) { sq(sk, 0, L"502", L"Unauthorized request", L"needed", L"%x", (p), NULL); return; } } while(0)
339 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
342 struct in6_addr mv4lo;
344 if(confgetint("ui", "onlylocal"))
346 switch(sk->remote->sa_family)
349 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
352 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
354 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
356 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
368 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
374 sq(sk, 0, L"201", L"1", L"3", L"Dolda Connect daemon v" VERSION, NULL);
377 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
379 if((argv != NULL) && (argv[0] != NULL))
380 sq(sk, 0, L"500", L"Command not found", NULL);
382 sq(sk, 0, L"500", L"No command", NULL);
385 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
387 extern volatile int running;
389 havepriv(PERM_ADMIN);
390 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
392 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
395 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
397 sq(sk, 0, L"200", L"Closing connection", NULL);
401 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
403 struct authmech *mech, *prev;
406 for(mech = mechs; mech != NULL; mech = mech->next)
408 if(mech->enabled && authavailable(mech, sk))
411 sq(sk, 1, L"200", prev->name, NULL);
416 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
418 sq(sk, 0, L"200", prev->name, NULL);
421 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
428 if(data->username != NULL)
430 if(data->userinfo != NULL)
431 sq(sk, 0, L"503", L"Already logged in", NULL);
433 sq(sk, 0, L"503", L"Already logging in", NULL);
436 if((buf = icwcstombs(argv[2], NULL)) == NULL)
438 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
441 data->username = swcsdup(argv[2]);
442 if((pwd = getpwnam(buf)) == NULL)
445 data->uid = pwd->pw_uid;
446 if((data->auth = initauth(argv[1], buf)) == NULL)
449 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
451 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL);
457 switch(authenticate(data->auth, sk, NULL))
460 data->userinfo = finduser(data->username);
461 if(data->userinfo == NULL)
462 data->userinfo = finduser(L"default");
465 sq(sk, 0, L"506", L"Authentication error", NULL);
466 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
468 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
469 sq(sk, 0, L"506", L"Authentication error", NULL);
470 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
473 sq(sk, 0, L"200", L"Welcome", NULL);
474 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
478 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
479 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
483 switch(data->auth->prompt)
501 sq(sk, 0, L"%i", code, data->auth->text, NULL);
504 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
508 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
509 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
515 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
521 if((buf = icwcstombs(argv[1], NULL)) == NULL)
523 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
526 if((data->auth == NULL) || (data->userinfo != NULL))
528 sq(sk, 0, L"507", L"Data not expected", NULL);
531 switch(authenticate(data->auth, sk, buf))
534 data->userinfo = finduser(data->username);
535 if(data->userinfo == NULL)
536 data->userinfo = finduser(L"default");
539 sq(sk, 0, L"506", L"Authentication error", NULL);
540 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
542 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
543 sq(sk, 0, L"506", L"Authentication error", NULL);
544 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
547 sq(sk, 0, L"200", L"Welcome", NULL);
548 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
552 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
553 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
557 switch(data->auth->prompt)
575 sq(sk, 0, L"%i", code, data->auth->text, NULL);
578 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
582 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
583 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
590 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
596 struct wcspair *args;
599 havepriv(PERM_FNETCTL);
600 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
601 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
602 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
605 if((buf = icwcstombs(argv[2], NULL)) == NULL)
607 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
611 for(i = 3; i < argc - 1; i += 2)
612 newwcspair(argv[i], argv[i + 1], &args);
613 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
618 if(errno == EPROTONOSUPPORT)
619 sq(sk, 0, L"511", L"No such network name", NULL);
621 sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL);
625 fnetsetname(fn, argv[2]);
626 sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL);
630 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
634 if(fnetnodes == NULL)
636 sq(sk, 0, L"201", L"No connected nodes", NULL);
639 for(fn = fnetnodes; fn != NULL; fn = fn->next)
641 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);
645 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
651 havepriv(PERM_FNETCTL);
652 /* Note - Programmatical user interfaces must only give one
653 * argument per command, the multiple argument form is only for
654 * convenience when manually controlling the daemon via
655 * eg. telnet. The reason is that the return codes aren't clear
656 * enough for the multiple argument form. */
657 for(i = 1; i < argc; i++)
659 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
661 sq(sk, 0, L"510", L"No such node", NULL);
664 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
666 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
672 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
675 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
678 struct fnetpeerdatum *datum;
681 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
683 sq(sk, 0, L"510", L"No such node", NULL);
686 if(fn->peerdata == NULL)
688 sq(sk, 0, L"201", L"No data available", NULL);
690 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
691 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL);
695 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
699 struct fnetpeer *peer, *npeer;
702 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
704 sq(sk, 0, L"510", L"No such node", NULL);
707 if(fn->peers == NULL)
709 sq(sk, 0, L"201", L"No peers available", NULL);
711 for(peer = btreeiter(fn->peers); peer != NULL; peer = npeer)
713 npeer = btreeiter(NULL);
714 sq(sk, 2 | ((npeer != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL);
715 for(i = 0; i < peer->dinum; i++)
717 if(peer->peerdi[i].datum->datatype == FNPD_INT)
718 sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL);
719 if(peer->peerdi[i].datum->datatype == FNPD_LL)
720 sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL);
721 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
722 sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL);
729 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
734 struct transfer *transfer;
735 struct fnetpeer *peer;
738 if((argc > 5) && ((argc % 2) == 0))
740 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
743 havepriv(PERM_TRANS);
744 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
746 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
748 sq(sk, 0, L"510", L"No such node", NULL);
754 if((net = findfnet(argv[1])) == NULL)
756 sq(sk, 0, L"511", L"No such network name", NULL);
760 transfer = newtransfer();
761 authgethandle(transfer->auth = data->auth);
762 transfer->fnet = net;
763 transfer->peerid = swcsdup(argv[2]);
764 transfer->path = swcsdup(argv[3]);
765 transfer->dir = TRNSD_DOWN;
766 transfer->owner = data->uid;
771 linktransfer(transfer);
772 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
773 transfersetnick(transfer, peer->nick);
775 linktransfer(transfer);
778 transfersetsize(transfer, wcstoll(argv[4], NULL, 0));
781 for(i = 5; i < argc; i += 2)
783 if(!wcscmp(argv[i], L"hash"))
785 transfersethash(transfer, parsehash(argv[i + 1]));
787 newwcspair(argv[i], argv[i + 1], &transfer->args);
791 sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL);
792 transfersetactivity(transfer, L"create");
795 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
797 struct transfer *transfer, *pt;
799 havepriv(PERM_TRANS);
801 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
803 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
806 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir,
807 L"%i", pt->state, pt->peerid,
808 (pt->peernick == NULL)?L"":(pt->peernick),
809 (pt->path == NULL)?L"":(pt->path),
810 L"%oi", pt->size, L"%oi", pt->curpos,
811 (pt->hash == NULL)?L"":unparsehash(pt->hash),
817 sq(sk, 0, L"201", L"No transfers", NULL);
819 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir,
820 L"%i", pt->state, pt->peerid,
821 (pt->peernick == NULL)?L"":(pt->peernick),
822 (pt->path == NULL)?L"":(pt->path),
823 L"%oi", pt->size, L"%oi", pt->curpos,
824 (pt->hash == NULL)?L"":unparsehash(pt->hash),
828 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
830 struct transfer *transfer;
833 havepriv(PERM_TRANS);
834 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
836 sq(sk, 0, L"512", L"No such transfer", NULL);
839 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
841 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
844 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
846 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
850 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
853 static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
855 struct transfer *transfer;
858 havepriv(PERM_TRANS);
859 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
861 sq(sk, 0, L"512", L"No such transfer", NULL);
864 if(transfer->dir == TRNSD_UP)
866 sq(sk, 0, L"512", L"Only applicable to downloads", NULL);
869 resettransfer(transfer);
870 sq(sk, 0, L"200", L"Transfer reset", NULL);
873 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
879 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
882 for(i = 1; i < argc; i += 2)
884 if(!wcscasecmp(argv[i + 1], L"on"))
888 if(!wcscasecmp(argv[i], L"all"))
894 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
895 data->notify.b.fnchat = val;
896 } else if(!wcscasecmp(argv[i], L"fn:act")) {
897 data->notify.b.fnact = val;
898 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
899 data->notify.b.fnpeer = val;
900 } else if(!wcscasecmp(argv[i], L"trans:act")) {
901 data->notify.b.tract = val;
902 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
903 data->notify.b.trprog = val;
904 } else if(!wcscasecmp(argv[i], L"srch:act")) {
905 data->notify.b.srch = val;
906 } else if(!wcscasecmp(argv[i], L"msg")) {
907 data->notify.b.msg = val;
910 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
913 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
920 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
922 sq(sk, 0, L"510", L"No such node", NULL);
925 public = wcstol(argv[2], NULL, 0);
926 if((public != 0) && (public != 1))
928 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
931 if(fn->state != FNN_EST)
933 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
936 if(fnetsendchat(fn, public, argv[3], argv[4]))
939 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
940 else if(errno == EPERM)
941 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
942 else if(errno == EILSEQ)
943 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
945 sq(sk, 0, L"505", L"Could not chat", NULL);
948 sq(sk, 0, L"200", L"Chat string sent", NULL);
951 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
960 srch = newsearch(data->username, NULL);
961 for(i = 1; i < argc; i++)
963 if(!wcscmp(argv[i], L"all"))
965 for(fn = fnetnodes; fn != NULL; fn = fn->next)
967 if(fn->state == FNN_EST)
968 searchaddfn(srch, fn);
972 } else if(!wcscmp(argv[i], L"prio")) {
975 sq(sk, 0, L"501", L"No argument to prio", NULL);
979 srch->prio = wcstol(argv[i], NULL, 0);
980 } else if(iswdigit(*argv[i])) {
981 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
983 sq(sk, 0, L"510", L"No such node", NULL);
987 searchaddfn(srch, fn);
992 if(srch->fnl == NULL)
994 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
1000 sq(sk, 0, L"501", L"No search expression found on line", NULL);
1004 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
1006 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
1011 getsexpr(srch->sexpr = sexpr);
1013 CBREG(srch, search_eta, srcheta, NULL, NULL);
1014 CBREG(srch, search_commit, srchcommit, NULL, NULL);
1015 CBREG(srch, search_result, srchres, NULL, NULL);
1016 sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL);
1020 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1022 struct search *srch, *pt;
1025 havepriv(PERM_SRCH);
1028 for(srch = searches; srch != NULL; srch = srch->next)
1030 if(!wcscmp(srch->owner, data->username))
1033 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1038 sq(sk, 0, L"201", L"No searches", NULL);
1040 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1043 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1045 struct search *srch;
1049 havepriv(PERM_SRCH);
1050 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1052 sq(sk, 0, L"514", L"No such search", NULL);
1055 if(srch->results == NULL)
1057 sq(sk, 0, L"201", L"No results", NULL);
1059 for(sr = srch->results; sr != NULL; sr = sr->next)
1061 sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename,
1062 sr->fnet->name, L"%ls", sr->peerid, L"%oi", sr->size,
1063 L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id),
1065 L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1070 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1072 struct search *srch;
1076 havepriv(PERM_SRCH);
1077 /* Note - Programmatical user interfaces must only give one
1078 * argument per command, the multiple argument form is only for
1079 * convenience when manually controlling the daemon via
1080 * eg. telnet. The reason is that the return codes aren't clear
1081 * enough for the multiple argument form. */
1082 for(i = 1; i < argc; i++)
1084 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1086 sq(sk, 0, L"514", L"No such search", NULL);
1091 sq(sk, 0, L"200", L"Search cancelled", NULL);
1094 static void fcmdread(struct socket *sk, struct uidata *data)
1099 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1101 bufcat(data->fcmdbuf, buf, bufsize);
1105 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1107 wchar_t *wbuf, *p, *p2;
1111 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1112 kill(-data->fcmdpid, SIGHUP);
1113 putsock(data->fcmdsk);
1114 data->fcmdsk = NULL;
1115 if(data->fcmdbuf != NULL)
1117 free(data->fcmdbuf);
1118 data->fcmdbuf = NULL;
1120 data->fcmdbufsize = data->fcmdbufdata = 0;
1121 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL);
1124 putsock(data->fcmdsk);
1125 data->fcmdsk = NULL;
1127 if(data->fcmdbuf == NULL)
1129 wbuf = swcsdup(L"");
1131 addtobuf(data->fcmdbuf, 0);
1132 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1133 free(data->fcmdbuf);
1135 data->fcmdbuf = NULL;
1136 data->fcmdbufsize = data->fcmdbufdata = 0;
1139 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1143 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1146 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL);
1152 sq(data->sk, 0, L"201", L"No data returned", NULL);
1154 sq(data->sk, 0, L"200", L"%ls", p, NULL);
1159 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1165 char *filtercmd, *argbuf;
1166 size_t cargvsize, cargvdata;
1167 struct passwd *pwent;
1170 havepriv(PERM_TRANS);
1171 if((pwent = getpwuid(data->uid)) == NULL)
1173 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1174 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1177 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1178 if(filtercmd == NULL)
1179 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1180 if(filtercmd == NULL)
1182 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1183 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1187 cargvsize = cargvdata = 0;
1188 addtobuf(cargv, filtercmd);
1189 for(i = 1; i < argc; i++)
1191 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1193 for(i = 0; i < cargvdata; i++)
1196 sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL);
1199 addtobuf(cargv, argbuf);
1201 addtobuf(cargv, NULL);
1202 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)
1204 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1205 sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL);
1210 execv(filtercmd, cargv);
1211 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1214 for(pp = cargv; *pp; pp++)
1217 data->fcmdsk = wrapsock(pipe);
1218 data->fcmdpid = pid;
1219 if(data->fcmdbuf != NULL)
1221 free(data->fcmdbuf);
1222 data->fcmdbuf = NULL;
1224 data->fcmdbufsize = data->fcmdbufdata = 0;
1225 data->fcmdsk->data = data;
1226 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1227 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1230 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1232 struct transfer *transfer;
1236 havepriv(PERM_TRANS);
1237 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1239 sq(sk, 0, L"512", L"No such transfer", NULL);
1242 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1244 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1247 if(transfer->args == NULL)
1249 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1251 for(ta = transfer->args; ta != NULL; ta = ta->next)
1252 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1256 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1258 struct sharecache *node;
1262 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1264 if(node->f.b.type == FILE_REG)
1267 if(node->f.b.hastth)
1271 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1274 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1276 havepriv(PERM_TRANS);
1277 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1280 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1285 if(data->userinfo == NULL) {
1286 sq(sk, 0, L"502", L"Must be logged in", NULL);
1289 if(argv[1][0] == L'#') {
1290 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1293 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1294 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1295 sq(sk, 0, L"516", L"Name already in use", NULL);
1299 if(data->regname != NULL)
1300 free(data->regname);
1301 data->regname = swcsdup(argv[1]);
1302 sq(sk, 0, L"200", L"Registered", NULL);
1305 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1308 struct uidata *rcpt;
1310 struct notif *notif;
1313 if(data->userinfo == NULL) {
1314 sq(sk, 0, L"502", L"Must be logged in", NULL);
1317 if(argv[1][0] == L'#') {
1318 rcptid = wcstol(argv[1] + 1, NULL, 0);
1319 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1320 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1324 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1325 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1330 sq(sk, 0, L"517", L"No such recipient", NULL);
1333 if(!rcpt->notify.b.msg) {
1334 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1337 if(data->regname != NULL)
1338 myname = swcsdup(data->regname);
1340 myname = swprintf2(L"#%i", data->id);
1341 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1342 for(i = 2; i < argc; i++)
1343 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1344 sq(sk, 0, L"200", L"Message sent", NULL);
1347 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1349 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1352 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1354 extern volatile int reinit;
1356 havepriv(PERM_ADMIN);
1357 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1359 sq(sk, 0, L"200", L"Will reinit", NULL);
1366 * Reserved command numbers for nameless commands:
1367 * 0: Issued when a client has connected
1368 * 1: Issued when a named command couldn't be found
1371 static struct command commands[] =
1373 {NULL, cmd_connect},
1374 {NULL, cmd_notfound},
1375 {L"shutdown", cmd_shutdown},
1376 {L"quit", cmd_quit},
1377 {L"lsauth", cmd_lsauth},
1378 {L"login", cmd_login},
1379 {L"pass", cmd_pass},
1380 {L"cnct", cmd_fnetconnect},
1381 {L"lsnodes", cmd_lsnodes},
1382 {L"dcnct", cmd_disconnect},
1383 {L"lspa", cmd_lspa},
1384 {L"lspeers", cmd_lspeers},
1385 {L"download", cmd_download},
1386 {L"lstrans", cmd_lstrans},
1387 {L"cancel", cmd_cancel},
1388 {L"reset", cmd_reset},
1389 {L"notify", cmd_notify},
1390 {L"sendchat", cmd_sendchat},
1391 {L"search", cmd_search},
1392 {L"lssrch", cmd_lssrch},
1393 {L"lssr", cmd_lssr},
1394 {L"cansrch", cmd_cansrch},
1395 {L"filtercmd", cmd_filtercmd},
1396 {L"lstrarg", cmd_lstrarg},
1397 {L"hashstatus", cmd_hashstatus},
1398 {L"transstatus", cmd_transstatus},
1399 {L"register", cmd_register},
1400 {L"sendmsg", cmd_sendmsg},
1401 {L"uptime", cmd_uptime},
1406 static void freequeuecmd(struct qcommand *qcmd)
1410 if(qcmd->argv != NULL)
1412 for(i = 0; i < qcmd->argc; i++)
1413 free(qcmd->argv[i]);
1419 static struct qcommand *unlinkqcmd(struct uidata *data)
1421 struct qcommand *qcmd;
1427 data->queue = qcmd->next;
1428 if(qcmd == data->queuelast)
1429 data->queuelast = qcmd->next;
1434 static void notifappendv(struct notif *notif, va_list args)
1438 while((dt = va_arg(args, int)) != NOTIF_END)
1441 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1442 notif->argv[ca].dt = dt;
1447 notif->argv[ca].d.n = va_arg(args, int);
1450 notif->argv[ca].d.o = va_arg(args, off_t);
1453 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1456 notif->argv[ca].d.d = va_arg(args, double);
1462 static void notifappend(struct notif *notif, ...)
1466 va_start(args, notif);
1467 notifappendv(notif, args);
1471 static struct notif *newnotif(struct uidata *data, int code, ...)
1473 struct notif *notif;
1476 notif = smalloc(sizeof(*notif));
1477 memset(notif, 0, sizeof(*notif));
1478 notif->rlimit = 0.0;
1481 va_start(args, code);
1482 notifappendv(notif, args);
1485 notif->prev = data->lnotif;
1486 if(data->lnotif != NULL)
1487 data->lnotif->next = notif;
1489 data->fnotif = notif;
1490 data->lnotif = notif;
1494 static void freenotif(struct notif *notif)
1498 if(notif->next != NULL)
1499 notif->next->prev = notif->prev;
1500 if(notif->prev != NULL)
1501 notif->prev->next = notif->next;
1502 if(notif == notif->ui->fnotif)
1503 notif->ui->fnotif = notif->next;
1504 if(notif == notif->ui->lnotif)
1505 notif->ui->lnotif = notif->prev;
1506 if(notif->exptimer != NULL)
1507 canceltimer(notif->exptimer);
1508 for(i = 0; i < notif->argc; i++)
1510 if(notif->argv[i].dt == NOTIF_STR)
1511 free(notif->argv[i].d.s);
1513 if(notif->argv != NULL)
1518 static void notifexpire(int cancelled, struct notif *notif)
1520 notif->exptimer = NULL;
1525 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1529 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1531 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1536 for(i = 0; i < notif->argc; i++)
1538 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1553 static void freeuidata(struct uidata *data)
1556 struct qcommand *qcmd;
1558 if(data->next != NULL)
1559 data->next->prev = data->prev;
1560 if(data->prev != NULL)
1561 data->prev->next = data->next;
1563 actives = data->next;
1564 data->sk->readcb = NULL;
1565 data->sk->errcb = NULL;
1566 closesock(data->sk);
1568 while((qcmd = unlinkqcmd(data)) != NULL)
1570 iconv_close(data->ichandle);
1571 if(data->cw != NULL)
1573 if(data->cb != NULL)
1575 if(data->argv != NULL)
1577 for(i = 0; i < data->argc; i++)
1578 free(data->argv[i]);
1581 if(data->auth != NULL)
1582 authputhandle(data->auth);
1583 if(data->regname != NULL)
1584 free(data->regname);
1585 if(data->username != NULL)
1587 if(data->userinfo != NULL)
1588 flog(LOG_INFO, "%ls logged out", data->username);
1589 free(data->username);
1592 while(data->fnotif != NULL)
1593 freenotif(data->fnotif);
1594 if(data->fcmdbuf != NULL)
1595 free(data->fcmdbuf);
1596 if(data->fcmdpid != 0)
1597 kill(-data->fcmdpid, SIGHUP);
1598 if(data->fcmdsk != NULL)
1599 putsock(data->fcmdsk);
1603 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1605 struct qcommand *new;
1607 new = smalloc(sizeof(*new));
1612 if(data->queuelast != NULL)
1613 data->queuelast->next = new;
1614 data->queuelast = new;
1615 if(data->queue == NULL)
1620 static struct uidata *newuidata(struct socket *sk)
1622 struct uidata *data;
1623 static int curid = 0;
1625 data = smalloc(sizeof(*data));
1626 memset(data, 0, sizeof(*data));
1630 data->inbuf = smalloc(1024);
1632 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1634 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1637 data->next = actives;
1640 actives->prev = data;
1645 static void uiread(struct socket *sk, struct uidata *data)
1651 size_t datalen, len2;
1652 struct command *cur;
1654 if(data->indata > 1024)
1656 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1658 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1659 memcpy(data->inbuf + data->indata, newbuf, datalen);
1661 data->indata += datalen;
1662 if(data->cb == NULL)
1664 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1666 data->pp = data->cb;
1671 if(data->cbsize == data->cbdata)
1673 len2 = data->pp - data->cb;
1674 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1675 data->pp = data->cb + len2;
1678 p2 = (char *)(porig = (data->cb + data->cbdata));
1679 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1680 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1681 memmove(data->inbuf, p1, data->indata);
1682 /* Just a sanity check */
1683 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1685 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1688 data->cbdata += (((wchar_t *)p2) - porig);
1694 /* XXX: Should this really just ignore it? */
1702 /* Just a sanity check */
1703 if(data->cbsize != data->cbdata)
1705 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1710 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1718 while(!done && (data->pp - data->cb < data->cbdata))
1723 if(iswspace(*data->pp))
1725 if(*data->pp == L'\r')
1727 if(data->pp == data->cb + data->cbdata - 1)
1732 if(*(++data->pp) == L'\n')
1734 if((data->argv != NULL) && (data->argv[0] != NULL))
1736 for(cur = commands; cur->handler != NULL; cur++)
1738 if(cur->name == NULL)
1740 if(!wcscasecmp(cur->name, data->argv[0]))
1742 queuecmd(data, cur, data->argc, data->argv);
1746 if(cur->handler == NULL)
1747 queuecmd(data, &commands[1], data->argc, data->argv);
1749 queuecmd(data, &commands[1], data->argc, data->argv);
1754 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1755 data->pp = data->cb;
1768 if(iswspace(*data->pp))
1770 addtobuf(data->cw, L'\0');
1771 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1772 data->argv[data->argc++] = data->cw;
1777 } else if(*data->pp == L'\"') {
1780 } else if(*data->pp == L'\\') {
1781 if(data->pp == data->cb + data->cbdata - 1)
1786 addtobuf(data->cw, *(++data->pp));
1789 addtobuf(data->cw, *(data->pp++));
1793 if(*data->pp == L'\"')
1796 } else if(*data->pp == L'\\') {
1797 if(data->pp == data->cb + data->cbdata - 1)
1802 addtobuf(data->cw, *(++(data->pp)));
1804 addtobuf(data->cw, *data->pp);
1810 if(data->cbdata > 16384)
1812 /* Kill clients that send us unreasonably long lines */
1817 static void uierror(struct socket *sk, int err, struct uidata *data)
1820 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1824 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1826 struct uidata *uidata;
1828 newsk->data = uidata = newuidata(newsk);
1829 socksettos(newsk, confgetint("ui", "uitos"));
1832 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1833 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1834 queuecmd(uidata, &commands[0], 0, NULL);
1837 static int srcheta(struct search *srch, void *uudata)
1839 struct uidata *data;
1841 for(data = actives; data != NULL; data = data->next)
1843 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1844 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1849 static int srchcommit(struct search *srch, void *uudata)
1851 struct uidata *data;
1853 for(data = actives; data != NULL; data = data->next)
1855 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1856 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1861 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1863 struct uidata *data;
1865 for(data = actives; data != NULL; data = data->next)
1867 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1869 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size,
1870 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);
1876 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1878 struct uidata *data;
1880 for(data = actives; data != NULL; data = data->next)
1882 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1883 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1888 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1890 struct uidata *data;
1891 struct notif *notif;
1893 if(!wcscmp(attrib, L"state"))
1895 for(data = actives; data != NULL; data = data->next)
1897 if(data->notify.b.fnact)
1898 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1900 } else if(!wcscmp(attrib, L"name")) {
1901 for(data = actives; data != NULL; data = data->next)
1903 if(data->notify.b.fnact)
1904 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1906 } else if(!wcscmp(attrib, L"numpeers")) {
1907 for(data = actives; data != NULL; data = data->next)
1909 if(data->notify.b.fnact)
1911 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1912 notif->argv[1].d.n = fn->numpeers;
1914 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1921 static int fnunlink(struct fnetnode *fn, void *uudata)
1923 struct uidata *data;
1925 for(data = actives; data != NULL; data = data->next)
1927 if(data->notify.b.fnact)
1928 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1933 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1935 struct uidata *data;
1937 for(data = actives; data != NULL; data = data->next)
1939 if(data->notify.b.fnpeer)
1940 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1945 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1947 struct uidata *data;
1949 for(data = actives; data != NULL; data = data->next)
1951 if(data->notify.b.fnpeer)
1952 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1957 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1959 struct uidata *data;
1960 struct notif *notif;
1963 for(data = actives; data != NULL; data = data->next)
1965 if(data->notify.b.fnpeer)
1967 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1969 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1973 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1974 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1975 switch(di->datum->datatype)
1978 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1981 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1984 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1985 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1993 static int newfnetnode(struct fnetnode *fn, void *uudata)
1995 struct uidata *data;
1997 for(data = actives; data != NULL; data = data->next)
1999 if(data->notify.b.fnact)
2000 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
2002 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
2003 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
2004 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
2005 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
2006 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
2007 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
2011 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
2013 struct uidata *data;
2015 if(!wcscmp(attrib, L"state"))
2017 for(data = actives; data != NULL; data = data->next)
2019 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2020 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2022 } else if(!wcscmp(attrib, L"nick")) {
2023 for(data = actives; data != NULL; data = data->next)
2025 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2026 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2028 } else if(!wcscmp(attrib, L"size")) {
2029 for(data = actives; data != NULL; data = data->next)
2031 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2032 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END);
2034 } else if(!wcscmp(attrib, L"error")) {
2035 for(data = actives; data != NULL; data = data->next)
2037 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2038 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2040 } else if(!wcscmp(attrib, L"path")) {
2041 for(data = actives; data != NULL; data = data->next)
2043 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2044 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2046 } else if(!wcscmp(attrib, L"hash")) {
2047 for(data = actives; data != NULL; data = data->next)
2049 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2050 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2056 static int transferprog(struct transfer *transfer, void *uudata)
2058 struct uidata *data;
2059 struct notif *notif;
2061 for(data = actives; data != NULL; data = data->next)
2063 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2065 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2066 notif->argv[1].d.o = transfer->curpos;
2068 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2074 static int transferdestroyed(struct transfer *transfer, void *uudata)
2076 struct uidata *data;
2078 for(data = actives; data != NULL; data = data->next)
2080 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2081 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2086 static int newtransfernotify(struct transfer *transfer, void *uudata)
2088 struct uidata *data;
2090 for(data = actives; data != NULL; data = data->next)
2092 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2093 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);
2095 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2096 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2097 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2101 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2105 new = smalloc(sizeof(*new));
2107 new->name = swcsdup(name);
2118 static void freeuser(struct uiuser *user)
2120 if(user->next != NULL)
2121 user->next->prev = user->prev;
2122 if(user->prev != NULL)
2123 user->prev->next = user->next;
2130 static int conf_user(int argc, wchar_t **argv)
2132 int i, perms, permmod;
2133 struct uiuser *user;
2138 flog(LOG_WARNING, "not enough arguments given for user command");
2142 for(i = 2; i < argc; i++)
2144 if(!iswalpha(argv[i][0]))
2148 if(!wcscmp(p, L"disallow"))
2149 permmod = PERM_DISALLOW;
2150 if(!wcscmp(p, L"admin"))
2151 permmod = PERM_ADMIN;
2152 if(!wcscmp(p, L"fnetctl"))
2153 permmod = PERM_FNETCTL;
2154 if(!wcscmp(p, L"trans"))
2155 permmod = PERM_TRANS;
2156 if(!wcscmp(p, L"transcu"))
2157 permmod = PERM_TRANSCU;
2158 if(!wcscmp(p, L"chat"))
2159 permmod = PERM_CHAT;
2160 if(!wcscmp(p, L"srch"))
2161 permmod = PERM_SRCH;
2162 if(!wcscmp(p, L"all"))
2164 if(argv[i][0] == L'-')
2169 if((user = finduser(argv[1])) == NULL)
2171 newuser(argv[1], perms);
2174 user->perms = perms;
2179 static void preinit(int hup)
2181 struct uiuser *user;
2185 newuser(L"default", PERM_DISALLOW);
2187 for(user = users; user != NULL; user = user->next)
2189 if(!wcscmp(user->name, L"default"))
2195 static struct sockaddr_un *makeunixname(void)
2197 static struct sockaddr_un buf;
2202 memset(&buf, 0, sizeof(buf));
2203 buf.sun_family = PF_UNIX;
2204 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2205 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2208 if(!strcmp(val, "none"))
2210 if(!strcmp(val, "default"))
2212 if((uid = getuid()) == 0)
2214 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2217 if((pwd = getpwuid(uid)) == NULL)
2219 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2222 strcpy(buf.sun_path, "/tmp/doldacond-");
2223 strcat(buf.sun_path, pwd->pw_name);
2227 if(strchr(val, '/'))
2229 strcpy(buf.sun_path, val);
2232 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2236 static int tcpportupdate(struct configvar *var, void *uudata)
2238 struct socket *newsock;
2241 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2243 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2246 if(tcpsocket != NULL)
2251 tcpsocket = newsock;
2255 static int unixsockupdate(struct configvar *var, void *uudata)
2257 struct socket *newsock;
2258 struct sockaddr_un *un;
2263 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2266 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2270 if(unixsocket != NULL)
2272 putsock(unixsocket);
2275 unixsocket = newsock;
2279 static int init(int hup)
2281 struct uiuser *user, *next;
2282 struct sockaddr_un *un;
2289 for(user = users; user != NULL; user = next)
2298 starttime = time(NULL);
2299 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2301 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2304 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2306 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2309 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2313 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2314 GCBREG(newfncb, newfnetnode, NULL);
2315 GCBREG(newtransfercb, newtransfernotify, NULL);
2319 for(user = users; user != NULL; user = user->next)
2321 if(wcscmp(user->name, L"default"))
2326 if((pwd = getpwuid(getuid())) == NULL)
2328 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2331 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2333 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2336 newuser(wcsname, ~PERM_DISALLOW);
2343 static int run(void)
2346 struct uidata *data, *next;
2347 struct qcommand *qcmd;
2348 struct notif *notif, *nnotif;
2350 for(data = actives; data != NULL; data = next)
2356 for(data = actives; data != NULL; data = data->next)
2358 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2360 nnotif = notif->next;
2361 if(notif->state == NOTIF_WAIT)
2364 for(i = 0; i < notif->argc; i++)
2366 if(notif->argv[i].dt == NOTIF_ID)
2368 id = notif->argv[i].d.n;
2372 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2374 sq(data->sk, 2, L"%i", notif->code, NULL);
2375 for(i = 0; i < notif->argc; i++)
2377 switch(notif->argv[i].dt)
2381 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2384 sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL);
2387 if(notif->argv[i].d.s[0] == L'%')
2388 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2390 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2393 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2397 sq(data->sk, 0, NULL);
2398 if(notif->rlimit != 0)
2400 notif->state = NOTIF_WAIT;
2401 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2406 if((qcmd = unlinkqcmd(data)) != NULL)
2408 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2412 if(data->queuesize > 10)
2414 /* Clients should not be queue up commands at all, since
2415 * they should not send a new command before receiving a
2416 * reply to the previous command. Therefore, we
2417 * mercilessly massacre clients which are stacking up too
2425 static void terminate(void)
2427 while(users != NULL)
2429 if(tcpsocket != NULL)
2431 if(unixsocket != NULL)
2432 putsock(unixsocket);
2435 static struct configvar myvars[] =
2437 /** If true, UI connections will only be accepted from localhost
2438 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2439 * completely sure that you know what you are doing, never turn
2440 * this off when auth.authless is on. */
2441 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2442 /** The TCP port number on which to accept UI client connections,
2443 * or -1 to not listen on TCP. */
2444 {CONF_VAR_INT, "port", {.num = 1500}},
2446 * Controls the the name to use for the Unix socket on which to
2447 * accept UI client connections. If the name contains a slash, it
2448 * is treated as a file name to bind on. If the name is "default",
2449 * the file name will be "/var/run/doldacond.sock" if doldacond
2450 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2451 * NAME is the user name of the UID which doldacond runs as. If
2452 * the name is "none", no Unix socket will be used. Otherwise, an
2453 * error is signaled.
2455 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2456 /** The TOS value to use for UI connections (see the TOS VALUES
2458 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2459 /** The name of the filtercmd script (see the FILES section for
2460 * lookup information). */
2461 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2465 static struct configcmd mycmds[] =
2467 {"user", conf_user},
2471 static struct module me =
2482 .terminate = terminate