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>
46 #include "sysevents.h"
52 #define PERM_DISALLOW 1
54 #define PERM_FNETCTL 4
56 #define PERM_TRANSCU 16
74 void (*handler)(struct socket *sk, struct uidata *data, int argc, wchar_t **argv);
79 struct qcommand *next;
87 struct uiuser *next, *prev;
96 struct notif *next, *prev;
102 struct timer *exptimer;
118 struct uidata *next, *prev;
120 struct qcommand *queue, *queuelast;
121 struct authhandle *auth;
138 struct uiuser *userinfo;
142 struct notif *fnotif, *lnotif;
144 size_t fcmdbufdata, fcmdbufsize;
146 struct socket *fcmdsk;
149 size_t inbufsize, indata;
150 /* Wordset storage */
153 /* WCS conversation stuff */
154 wchar_t *cb; /* Conversation buffer */
155 size_t cbsize, cbdata;
158 int ps; /* Parser state */
159 wchar_t *pp; /* Current parse pointer */
160 wchar_t *cw; /* Current word (building storage) */
161 size_t cwsize, cwdata;
164 static int srcheta(struct search *srch, void *uudata);
165 static int srchcommit(struct search *srch, void *uudata);
166 static int srchres(struct search *srch, struct srchres *sr, void *uudata);
167 static struct notif *newnotif(struct uidata *data, int code, ...);
168 static void notifappend(struct notif *notif, ...);
170 struct uiuser *users = NULL;
171 struct uidata *actives = NULL;
172 struct socket *tcpsocket = NULL;
173 struct socket *unixsocket = NULL;
174 static time_t starttime;
176 static wchar_t *quoteword(wchar_t *word)
178 wchar_t *wp, *buf, *bp;
188 for(wp = word; *wp != L'\0'; wp++)
190 if(!dq && iswspace(*wp))
192 if((*wp == L'\\') || (*wp == L'\"'))
199 bp = buf = smalloc(sizeof(wchar_t) * (numc + numbs + (dq?2:0) + 1));
202 for(wp = word; *wp != L'\0'; wp++)
204 if((*wp == L'\\') || (*wp == L'\"'))
214 static void sq(struct socket *sk, int cont, ...)
220 wchar_t *part, *tpart;
221 size_t bufsize, bufdata;
224 bufsize = bufdata = 0;
227 while((part = va_arg(al, wchar_t *)) != NULL)
233 if(!wcscmp(tpart, L"i"))
236 part = swprintf2(L"%i", va_arg(al, int));
237 } else if(!wcscmp(tpart, L"zi")) {
239 part = swprintf2(L"%zi", va_arg(al, size_t));
240 } else if(!wcscmp(tpart, L"oi")) {
242 part = swprintf2(L"%ji", (intmax_t)va_arg(al, off_t));
243 } else if(!wcscmp(tpart, L"s")) {
245 part = icmbstowcs(sarg = va_arg(al, char *), NULL);
250 flog(LOG_ERR, "could not convert local string to wcs: %s", sarg);
252 } else if(!wcscmp(tpart, L"ls")) {
253 part = va_arg(al, wchar_t *);
254 } else if(!wcscmp(tpart, L"ll")) {
256 part = swprintf2(L"%lli", va_arg(al, long long));
257 } else if(!wcscmp(tpart, L"f")) {
259 part = swprintf2(L"%f", va_arg(al, double));
260 } else if(!wcscmp(tpart, L"x")) {
262 part = swprintf2(L"%x", va_arg(al, int));
264 flog(LOG_CRIT, "BUG: unknown type code in sq: %ls", tpart);
268 if((tpart = quoteword(part)) != NULL)
275 if((num > 1) || ((num == 1) && !(cont & 1)))
277 bufcat(buf, part, wcslen(part));
278 if((num == 0) && (cont & 1))
285 bufcat(buf, L" \0", 2);
287 bufcat(buf, L"\r\n\0", 3);
288 if((final = icwcstombs(buf, "utf-8")) == NULL)
290 flog(LOG_CRIT, "could not convert \"%ls\" into utf-8: %s", buf, strerror(errno));
296 sockqueue(sk, final, strlen(final));
300 struct uiuser *finduser(wchar_t *name)
304 for(user = users; user != NULL; user = user->next)
306 if(!wcscmp(user->name, name))
312 static void logout(struct uidata *data)
314 data->userinfo = NULL;
315 if(data->username != NULL)
316 free(data->username);
317 data->username = NULL;
318 if(data->auth != NULL)
319 authputhandle(data->auth);
323 static int haspriv(struct uidata *data, int perm)
325 if(data->userinfo == NULL)
327 if(data->userinfo->perms & perm)
333 /* Useful macros for the command functions: */
334 #define haveargs(n) do { if(argc < n) { sq(sk, 0, L"501", L"Wrong number of arguments", NULL); return; } } while(0)
335 #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)
337 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
340 struct in6_addr mv4lo;
342 if(confgetint("ui", "onlylocal"))
344 switch(sk->remote->sa_family)
347 valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
350 inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
352 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
354 if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
366 sq(sk, 0, L"502", L"Only localhost connections allowed to this host", NULL);
372 sq(sk, 0, L"201", L"1", L"2", L"Dolda Connect daemon v" VERSION, NULL);
375 static void cmd_notfound(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
377 if((argv != NULL) && (argv[0] != NULL))
378 sq(sk, 0, L"500", L"Command not found", NULL);
380 sq(sk, 0, L"500", L"No command", NULL);
383 static void cmd_shutdown(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
385 extern volatile int running;
387 havepriv(PERM_ADMIN);
388 flog(LOG_NOTICE, "UI shutdown request from %ls, shutting down", data->username);
390 sq(sk, 0, L"200", L"Daemon shutting down", NULL);
393 static void cmd_quit(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
395 sq(sk, 0, L"200", L"Closing connection", NULL);
399 static void cmd_lsauth(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
401 struct authmech *mech, *prev;
404 for(mech = mechs; mech != NULL; mech = mech->next)
406 if(mech->enabled && authavailable(mech, sk))
409 sq(sk, 1, L"200", prev->name, NULL);
414 sq(sk, 0, L"201", L"No authentication methods supported", NULL);
416 sq(sk, 0, L"200", prev->name, NULL);
419 static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
426 if(data->username != NULL)
428 if(data->userinfo != NULL)
429 sq(sk, 0, L"503", L"Already logged in", NULL);
431 sq(sk, 0, L"503", L"Already logging in", NULL);
434 if((buf = icwcstombs(argv[2], NULL)) == NULL)
436 sq(sk, 0, L"504", L"Could not convert username to locale charset", NULL);
439 data->username = swcsdup(argv[2]);
440 if((pwd = getpwnam(buf)) == NULL)
443 data->uid = pwd->pw_uid;
444 if((data->auth = initauth(argv[1], buf)) == NULL)
447 sq(sk, 0, L"508", L"No such authentication mechanism", NULL);
449 sq(sk, 0, L"505", L"Could not initialize authentication system", L"%s", strerror(errno), NULL);
455 switch(authenticate(data->auth, sk, NULL))
458 data->userinfo = finduser(data->username);
459 if(data->userinfo == NULL)
460 data->userinfo = finduser(L"default");
463 sq(sk, 0, L"506", L"Authentication error", NULL);
464 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
466 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
467 sq(sk, 0, L"506", L"Authentication error", NULL);
468 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
471 sq(sk, 0, L"200", L"Welcome", NULL);
472 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
476 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
477 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
481 switch(data->auth->prompt)
499 sq(sk, 0, L"%i", code, data->auth->text, NULL);
502 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
506 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_login");
507 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
513 static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
519 if((buf = icwcstombs(argv[1], NULL)) == NULL)
521 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
524 if((data->auth == NULL) || (data->userinfo != NULL))
526 sq(sk, 0, L"507", L"Data not expected", NULL);
529 switch(authenticate(data->auth, sk, buf))
532 data->userinfo = finduser(data->username);
533 if(data->userinfo == NULL)
534 data->userinfo = finduser(L"default");
537 sq(sk, 0, L"506", L"Authentication error", NULL);
538 flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
540 } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
541 sq(sk, 0, L"506", L"Authentication error", NULL);
542 flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
545 sq(sk, 0, L"200", L"Welcome", NULL);
546 flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
550 sq(sk, 0, L"506", L"Authentication error", L"%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
551 flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
555 switch(data->auth->prompt)
573 sq(sk, 0, L"%i", code, data->auth->text, NULL);
576 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
580 flog(LOG_WARNING, "BUG? Non-caught return from authenticate in cmd_pass");
581 sq(sk, 0, L"505", L"System error", L"%s", strerror(errno), NULL);
588 static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
594 struct wcspair *args;
597 havepriv(PERM_FNETCTL);
598 for(i = 0, fn = fnetnodes; fn != NULL; i++, fn = fn->next);
599 if((confgetint("fnet", "maxnodes") > 0) && (i >= confgetint("fnet", "maxnodes"))) {
600 sq(sk, 0, L"515", L"Too many fnetnodes connected already", NULL);
603 if((buf = icwcstombs(argv[2], NULL)) == NULL)
605 sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
609 for(i = 3; i < argc - 1; i += 2)
610 newwcspair(argv[i], argv[i + 1], &args);
611 fn = fnetinitconnect(argv[1], data->userinfo->name, buf, args);
616 if(errno == EPROTONOSUPPORT)
617 sq(sk, 0, L"511", L"No such network name", NULL);
619 sq(sk, 0, L"509", L"Could not parse the address", L"%s", strerror(err), NULL);
623 fnetsetname(fn, argv[2]);
624 sq(sk, 0, L"200", L"%i", fn->id, L"Connection under way", NULL);
628 static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
632 if(fnetnodes == NULL)
634 sq(sk, 0, L"201", L"No connected nodes", NULL);
637 for(fn = fnetnodes; fn != NULL; fn = fn->next)
639 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);
643 static void cmd_disconnect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
649 havepriv(PERM_FNETCTL);
650 /* Note - Programmatical user interfaces must only give one
651 * argument per command, the multiple argument form is only for
652 * convenience when manually controlling the daemon via
653 * eg. telnet. The reason is that the return codes aren't clear
654 * enough for the multiple argument form. */
655 for(i = 1; i < argc; i++)
657 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
659 sq(sk, 0, L"510", L"No such node", NULL);
662 if(wpfind(fn->args, L"locked") && !((data->userinfo->perms & PERM_ADMIN) || !wcscmp(data->userinfo->name, fn->owner)))
664 sq(sk, 0, L"502", L"This node is locked and you are neither administrator nor its owner", NULL);
670 sq(sk, 0, L"200", L"Node flagged for disconnection", NULL);
673 static void cmd_lspa(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
676 struct fnetpeerdatum *datum;
679 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
681 sq(sk, 0, L"510", L"No such node", NULL);
684 if(fn->peerdata == NULL)
686 sq(sk, 0, L"201", L"No data available", NULL);
688 for(datum = fn->peerdata; datum != NULL; datum = datum->next)
689 sq(sk, (datum->next != NULL)?1:0, L"200", datum->id, L"%i", datum->datatype, NULL);
693 static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
697 struct fnetpeer *peer, *npeer;
700 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
702 sq(sk, 0, L"510", L"No such node", NULL);
705 if(fn->peers == NULL)
707 sq(sk, 0, L"201", L"No peers avaiable", NULL);
709 for(peer = btreeiter(fn->peers); peer != NULL; peer = npeer)
711 npeer = btreeiter(NULL);
712 sq(sk, 2 | ((npeer != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL);
713 for(i = 0; i < peer->dinum; i++)
715 if(peer->peerdi[i].datum->datatype == FNPD_INT)
716 sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL);
717 if(peer->peerdi[i].datum->datatype == FNPD_LL)
718 sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL);
719 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
720 sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL);
727 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
732 struct transfer *transfer;
733 struct fnetpeer *peer;
736 if((argc > 5) && ((argc % 2) == 0))
738 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
741 havepriv(PERM_TRANS);
742 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
744 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
746 sq(sk, 0, L"510", L"No such node", NULL);
752 if((net = findfnet(argv[1])) == NULL)
754 sq(sk, 0, L"511", L"No such network name", NULL);
758 transfer = newtransfer();
759 authgethandle(transfer->auth = data->auth);
760 transfer->fnet = net;
761 transfer->peerid = swcsdup(argv[2]);
762 transfer->path = swcsdup(argv[3]);
763 transfer->dir = TRNSD_DOWN;
764 transfer->owner = data->uid;
769 linktransfer(transfer);
770 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
771 transfersetnick(transfer, peer->nick);
773 linktransfer(transfer);
776 transfersetsize(transfer, wcstoll(argv[4], NULL, 0));
779 for(i = 5; i < argc; i += 2)
781 if(!wcscmp(argv[i], L"hash"))
783 transfersethash(transfer, parsehash(argv[i + 1]));
785 newwcspair(argv[i], argv[i + 1], &transfer->args);
789 sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL);
790 transfersetactivity(transfer, L"create");
793 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
795 struct transfer *transfer, *pt;
797 havepriv(PERM_TRANS);
799 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
801 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
804 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir,
805 L"%i", pt->state, pt->peerid,
806 (pt->peernick == NULL)?L"":(pt->peernick),
807 (pt->path == NULL)?L"":(pt->path),
808 L"%oi", pt->size, L"%oi", pt->curpos,
809 (pt->hash == NULL)?L"":unparsehash(pt->hash),
815 sq(sk, 0, L"201", L"No transfers", NULL);
817 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir,
818 L"%i", pt->state, pt->peerid,
819 (pt->peernick == NULL)?L"":(pt->peernick),
820 (pt->path == NULL)?L"":(pt->path),
821 L"%oi", pt->size, L"%oi", pt->curpos,
822 (pt->hash == NULL)?L"":unparsehash(pt->hash),
826 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
828 struct transfer *transfer;
831 havepriv(PERM_TRANS);
832 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
834 sq(sk, 0, L"512", L"No such transfer", NULL);
837 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
839 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
842 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
844 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
848 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
851 static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
853 struct transfer *transfer;
856 havepriv(PERM_TRANS);
857 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
859 sq(sk, 0, L"512", L"No such transfer", NULL);
862 if(transfer->dir == TRNSD_UP)
864 sq(sk, 0, L"512", L"Only applicable to downloads", NULL);
867 resettransfer(transfer);
868 sq(sk, 0, L"200", L"Transfer reset", NULL);
871 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
877 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
880 for(i = 1; i < argc; i += 2)
882 if(!wcscasecmp(argv[i + 1], L"on"))
886 if(!wcscasecmp(argv[i], L"all"))
892 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
893 data->notify.b.fnchat = val;
894 } else if(!wcscasecmp(argv[i], L"fn:act")) {
895 data->notify.b.fnact = val;
896 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
897 data->notify.b.fnpeer = val;
898 } else if(!wcscasecmp(argv[i], L"trans:act")) {
899 data->notify.b.tract = val;
900 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
901 data->notify.b.trprog = val;
902 } else if(!wcscasecmp(argv[i], L"srch:act")) {
903 data->notify.b.srch = val;
904 } else if(!wcscasecmp(argv[i], L"msg")) {
905 data->notify.b.msg = val;
908 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
911 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
918 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
920 sq(sk, 0, L"510", L"No such node", NULL);
923 public = wcstol(argv[2], NULL, 0);
924 if((public != 0) && (public != 1))
926 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
929 if(fn->state != FNN_EST)
931 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
934 if(fnetsendchat(fn, public, argv[3], argv[4]))
937 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
938 else if(errno == EPERM)
939 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
940 else if(errno == EILSEQ)
941 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
943 sq(sk, 0, L"505", L"Could not chat", NULL);
946 sq(sk, 0, L"200", L"Chat string sent", NULL);
949 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
958 srch = newsearch(data->username, NULL);
959 for(i = 1; i < argc; i++)
961 if(!wcscmp(argv[i], L"all"))
963 for(fn = fnetnodes; fn != NULL; fn = fn->next)
965 if(fn->state == FNN_EST)
966 searchaddfn(srch, fn);
970 } else if(!wcscmp(argv[i], L"prio")) {
973 sq(sk, 0, L"501", L"No argument to prio", NULL);
977 srch->prio = wcstol(argv[i], NULL, 0);
978 } else if(iswdigit(*argv[i])) {
979 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
981 sq(sk, 0, L"510", L"No such node", NULL);
985 searchaddfn(srch, fn);
990 if(srch->fnl == NULL)
992 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
998 sq(sk, 0, L"501", L"No search expression found on line", NULL);
1002 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
1004 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
1009 getsexpr(srch->sexpr = sexpr);
1011 CBREG(srch, search_eta, srcheta, NULL, NULL);
1012 CBREG(srch, search_commit, srchcommit, NULL, NULL);
1013 CBREG(srch, search_result, srchres, NULL, NULL);
1014 sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL);
1018 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1020 struct search *srch, *pt;
1023 havepriv(PERM_SRCH);
1026 for(srch = searches; srch != NULL; srch = srch->next)
1028 if(!wcscmp(srch->owner, data->username))
1031 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1036 sq(sk, 0, L"201", L"No searches", NULL);
1038 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1041 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1043 struct search *srch;
1047 havepriv(PERM_SRCH);
1048 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1050 sq(sk, 0, L"514", L"No such search", NULL);
1053 if(srch->results == NULL)
1055 sq(sk, 0, L"201", L"No results", NULL);
1057 for(sr = srch->results; sr != NULL; sr = sr->next)
1059 sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename,
1060 sr->fnet->name, L"%ls", sr->peerid, L"%oi", sr->size,
1061 L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id),
1063 L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1068 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1070 struct search *srch;
1074 havepriv(PERM_SRCH);
1075 /* Note - Programmatical user interfaces must only give one
1076 * argument per command, the multiple argument form is only for
1077 * convenience when manually controlling the daemon via
1078 * eg. telnet. The reason is that the return codes aren't clear
1079 * enough for the multiple argument form. */
1080 for(i = 1; i < argc; i++)
1082 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1084 sq(sk, 0, L"514", L"No such search", NULL);
1089 sq(sk, 0, L"200", L"Search cancelled", NULL);
1092 static void fcmdread(struct socket *sk, struct uidata *data)
1097 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1099 bufcat(data->fcmdbuf, buf, bufsize);
1103 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1105 wchar_t *wbuf, *p, *p2;
1109 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1110 kill(-data->fcmdpid, SIGHUP);
1111 putsock(data->fcmdsk);
1112 data->fcmdsk = NULL;
1113 if(data->fcmdbuf != NULL)
1115 free(data->fcmdbuf);
1116 data->fcmdbuf = NULL;
1118 data->fcmdbufsize = data->fcmdbufdata = 0;
1119 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL);
1122 putsock(data->fcmdsk);
1123 data->fcmdsk = NULL;
1125 if(data->fcmdbuf == NULL)
1127 wbuf = swcsdup(L"");
1129 addtobuf(data->fcmdbuf, 0);
1130 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1131 free(data->fcmdbuf);
1133 data->fcmdbuf = NULL;
1134 data->fcmdbufsize = data->fcmdbufdata = 0;
1137 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1141 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1144 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL);
1150 sq(data->sk, 0, L"201", L"No data returned", NULL);
1152 sq(data->sk, 0, L"200", L"%ls", p, NULL);
1157 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1163 char *filtercmd, *argbuf;
1164 size_t cargvsize, cargvdata;
1165 struct passwd *pwent;
1168 havepriv(PERM_TRANS);
1169 if((pwent = getpwuid(data->uid)) == NULL)
1171 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1172 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1175 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1176 if(filtercmd == NULL)
1177 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1178 if(filtercmd == NULL)
1180 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1181 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1185 cargvsize = cargvdata = 0;
1186 addtobuf(cargv, filtercmd);
1187 for(i = 1; i < argc; i++)
1189 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1191 for(i = 0; i < cargvdata; i++)
1194 sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL);
1197 addtobuf(cargv, argbuf);
1199 addtobuf(cargv, NULL);
1200 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)
1202 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1203 sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL);
1208 execv(filtercmd, cargv);
1209 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1212 for(pp = cargv; *pp; pp++)
1215 data->fcmdsk = wrapsock(pipe);
1216 data->fcmdpid = pid;
1217 if(data->fcmdbuf != NULL)
1219 free(data->fcmdbuf);
1220 data->fcmdbuf = NULL;
1222 data->fcmdbufsize = data->fcmdbufdata = 0;
1223 data->fcmdsk->data = data;
1224 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1225 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1228 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1230 struct transfer *transfer;
1234 havepriv(PERM_TRANS);
1235 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1237 sq(sk, 0, L"512", L"No such transfer", NULL);
1240 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1242 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1245 if(transfer->args == NULL)
1247 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1249 for(ta = transfer->args; ta != NULL; ta = ta->next)
1250 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1254 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1256 struct sharecache *node;
1260 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1262 if(node->f.b.type == FILE_REG)
1265 if(node->f.b.hastth)
1269 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1272 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1274 havepriv(PERM_TRANS);
1275 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1278 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1283 if(data->userinfo == NULL) {
1284 sq(sk, 0, L"502", L"Must be logged in", NULL);
1287 if(argv[1][0] == L'#') {
1288 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1291 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1292 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1293 sq(sk, 0, L"516", L"Name already in use", NULL);
1297 if(data->regname != NULL)
1298 free(data->regname);
1299 data->regname = swcsdup(argv[1]);
1300 sq(sk, 0, L"200", L"Registered", NULL);
1303 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1306 struct uidata *rcpt;
1308 struct notif *notif;
1311 if(data->userinfo == NULL) {
1312 sq(sk, 0, L"502", L"Must be logged in", NULL);
1315 if(argv[1][0] == L'#') {
1316 rcptid = wcstol(argv[1] + 1, NULL, 0);
1317 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1318 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1322 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1323 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1328 sq(sk, 0, L"517", L"No such recipient", NULL);
1331 if(!rcpt->notify.b.msg) {
1332 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1335 if(data->regname != NULL)
1336 myname = swcsdup(data->regname);
1338 myname = swprintf2(L"#%i", data->id);
1339 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1340 for(i = 2; i < argc; i++)
1341 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1342 sq(sk, 0, L"200", L"Message sent", NULL);
1345 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1347 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1350 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1352 extern volatile int reinit;
1354 havepriv(PERM_ADMIN);
1355 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1357 sq(sk, 0, L"200", L"Will reinit", NULL);
1364 * Reserved command numbers for nameless commands:
1365 * 0: Issued when a client has connected
1366 * 1: Issued when a named command couldn't be found
1369 static struct command commands[] =
1371 {NULL, cmd_connect},
1372 {NULL, cmd_notfound},
1373 {L"shutdown", cmd_shutdown},
1374 {L"quit", cmd_quit},
1375 {L"lsauth", cmd_lsauth},
1376 {L"login", cmd_login},
1377 {L"pass", cmd_pass},
1378 {L"cnct", cmd_fnetconnect},
1379 {L"lsnodes", cmd_lsnodes},
1380 {L"dcnct", cmd_disconnect},
1381 {L"lspa", cmd_lspa},
1382 {L"lspeers", cmd_lspeers},
1383 {L"download", cmd_download},
1384 {L"lstrans", cmd_lstrans},
1385 {L"cancel", cmd_cancel},
1386 {L"reset", cmd_reset},
1387 {L"notify", cmd_notify},
1388 {L"sendchat", cmd_sendchat},
1389 {L"search", cmd_search},
1390 {L"lssrch", cmd_lssrch},
1391 {L"lssr", cmd_lssr},
1392 {L"cansrch", cmd_cansrch},
1393 {L"filtercmd", cmd_filtercmd},
1394 {L"lstrarg", cmd_lstrarg},
1395 {L"hashstatus", cmd_hashstatus},
1396 {L"transstatus", cmd_transstatus},
1397 {L"register", cmd_register},
1398 {L"sendmsg", cmd_sendmsg},
1399 {L"uptime", cmd_uptime},
1404 static void freequeuecmd(struct qcommand *qcmd)
1408 if(qcmd->argv != NULL)
1410 for(i = 0; i < qcmd->argc; i++)
1411 free(qcmd->argv[i]);
1417 static struct qcommand *unlinkqcmd(struct uidata *data)
1419 struct qcommand *qcmd;
1424 data->queue = qcmd->next;
1425 if(qcmd == data->queuelast)
1426 data->queuelast = qcmd->next;
1431 static void notifappendv(struct notif *notif, va_list args)
1435 while((dt = va_arg(args, int)) != NOTIF_END)
1438 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1439 notif->argv[ca].dt = dt;
1444 notif->argv[ca].d.n = va_arg(args, int);
1447 notif->argv[ca].d.o = va_arg(args, off_t);
1450 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1453 notif->argv[ca].d.d = va_arg(args, double);
1459 static void notifappend(struct notif *notif, ...)
1463 va_start(args, notif);
1464 notifappendv(notif, args);
1468 static struct notif *newnotif(struct uidata *data, int code, ...)
1470 struct notif *notif;
1473 notif = smalloc(sizeof(*notif));
1474 memset(notif, 0, sizeof(*notif));
1475 notif->rlimit = 0.0;
1478 va_start(args, code);
1479 notifappendv(notif, args);
1482 notif->prev = data->lnotif;
1483 if(data->lnotif != NULL)
1484 data->lnotif->next = notif;
1486 data->fnotif = notif;
1487 data->lnotif = notif;
1491 static void freenotif(struct notif *notif)
1495 if(notif->next != NULL)
1496 notif->next->prev = notif->prev;
1497 if(notif->prev != NULL)
1498 notif->prev->next = notif->next;
1499 if(notif == notif->ui->fnotif)
1500 notif->ui->fnotif = notif->next;
1501 if(notif == notif->ui->lnotif)
1502 notif->ui->lnotif = notif->prev;
1503 if(notif->exptimer != NULL)
1504 canceltimer(notif->exptimer);
1505 for(i = 0; i < notif->argc; i++)
1507 if(notif->argv[i].dt == NOTIF_STR)
1508 free(notif->argv[i].d.s);
1510 if(notif->argv != NULL)
1515 static void notifexpire(int cancelled, struct notif *notif)
1517 notif->exptimer = NULL;
1522 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1526 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1528 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1533 for(i = 0; i < notif->argc; i++)
1535 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1550 static void freeuidata(struct uidata *data)
1553 struct qcommand *qcmd;
1555 if(data->next != NULL)
1556 data->next->prev = data->prev;
1557 if(data->prev != NULL)
1558 data->prev->next = data->next;
1560 actives = data->next;
1561 data->sk->readcb = NULL;
1562 data->sk->errcb = NULL;
1563 closesock(data->sk);
1565 while((qcmd = unlinkqcmd(data)) != NULL)
1567 iconv_close(data->ichandle);
1568 if(data->cw != NULL)
1570 if(data->cb != NULL)
1572 if(data->argv != NULL)
1574 for(i = 0; i < data->argc; i++)
1575 free(data->argv[i]);
1578 if(data->auth != NULL)
1579 authputhandle(data->auth);
1580 if(data->regname != NULL)
1581 free(data->regname);
1582 if(data->username != NULL)
1584 if(data->userinfo != NULL)
1585 flog(LOG_INFO, "%ls logged out", data->username);
1586 free(data->username);
1589 while(data->fnotif != NULL)
1590 freenotif(data->fnotif);
1591 if(data->fcmdbuf != NULL)
1592 free(data->fcmdbuf);
1593 if(data->fcmdpid != 0)
1594 kill(-data->fcmdpid, SIGHUP);
1595 if(data->fcmdsk != NULL)
1596 putsock(data->fcmdsk);
1600 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1602 struct qcommand *new;
1604 new = smalloc(sizeof(*new));
1609 if(data->queuelast != NULL)
1610 data->queuelast->next = new;
1611 data->queuelast = new;
1612 if(data->queue == NULL)
1616 static struct uidata *newuidata(struct socket *sk)
1618 struct uidata *data;
1619 static int curid = 0;
1621 data = smalloc(sizeof(*data));
1622 memset(data, 0, sizeof(*data));
1626 data->inbuf = smalloc(1024);
1628 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1630 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1633 data->next = actives;
1636 actives->prev = data;
1641 static void uiread(struct socket *sk, struct uidata *data)
1647 size_t datalen, len2;
1648 struct command *cur;
1650 if(data->indata > 1024)
1652 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1654 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1655 memcpy(data->inbuf + data->indata, newbuf, datalen);
1657 data->indata += datalen;
1658 if(data->cb == NULL)
1660 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1662 data->pp = data->cb;
1667 if(data->cbsize == data->cbdata)
1669 len2 = data->pp - data->cb;
1670 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1671 data->pp = data->cb + len2;
1674 p2 = (char *)(porig = (data->cb + data->cbdata));
1675 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1676 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1677 memmove(data->inbuf, p1, data->indata);
1678 /* Just a sanity check */
1679 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1681 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1684 data->cbdata += (((wchar_t *)p2) - porig);
1690 /* XXX: Should this really just ignore it? */
1698 /* Just a sanity check */
1699 if(data->cbsize != data->cbdata)
1701 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1706 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1714 while(!done && (data->pp - data->cb < data->cbdata))
1719 if(iswspace(*data->pp))
1721 if(*data->pp == L'\r')
1723 if(data->pp == data->cb + data->cbdata - 1)
1728 if(*(++data->pp) == L'\n')
1730 if((data->argv != NULL) && (data->argv[0] != NULL))
1732 for(cur = commands; cur->handler != NULL; cur++)
1734 if(cur->name == NULL)
1736 if(!wcscasecmp(cur->name, data->argv[0]))
1738 queuecmd(data, cur, data->argc, data->argv);
1742 if(cur->handler == NULL)
1743 queuecmd(data, &commands[1], data->argc, data->argv);
1745 queuecmd(data, &commands[1], data->argc, data->argv);
1750 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1751 data->pp = data->cb;
1764 if(iswspace(*data->pp))
1766 addtobuf(data->cw, L'\0');
1767 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1768 data->argv[data->argc++] = data->cw;
1773 } else if(*data->pp == L'\"') {
1776 } else if(*data->pp == L'\\') {
1777 if(data->pp == data->cb + data->cbdata - 1)
1782 addtobuf(data->cw, *(++data->pp));
1785 addtobuf(data->cw, *(data->pp++));
1789 if(*data->pp == L'\"')
1792 } else if(*data->pp == L'\\') {
1793 if(data->pp == data->cb + data->cbdata - 1)
1798 addtobuf(data->cw, *(++(data->pp)));
1800 addtobuf(data->cw, *data->pp);
1808 static void uierror(struct socket *sk, int err, struct uidata *data)
1811 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1815 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1817 struct uidata *uidata;
1819 newsk->data = uidata = newuidata(newsk);
1820 socksettos(newsk, confgetint("ui", "uitos"));
1823 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1824 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1825 queuecmd(uidata, &commands[0], 0, NULL);
1828 static int srcheta(struct search *srch, void *uudata)
1830 struct uidata *data;
1832 for(data = actives; data != NULL; data = data->next)
1834 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1835 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1840 static int srchcommit(struct search *srch, void *uudata)
1842 struct uidata *data;
1844 for(data = actives; data != NULL; data = data->next)
1846 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1847 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1852 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1854 struct uidata *data;
1856 for(data = actives; data != NULL; data = data->next)
1858 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1860 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size,
1861 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);
1867 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1869 struct uidata *data;
1871 for(data = actives; data != NULL; data = data->next)
1873 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1874 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1879 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1881 struct uidata *data;
1882 struct notif *notif;
1884 if(!wcscmp(attrib, L"state"))
1886 for(data = actives; data != NULL; data = data->next)
1888 if(data->notify.b.fnact)
1889 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1891 } else if(!wcscmp(attrib, L"name")) {
1892 for(data = actives; data != NULL; data = data->next)
1894 if(data->notify.b.fnact)
1895 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1897 } else if(!wcscmp(attrib, L"numpeers")) {
1898 for(data = actives; data != NULL; data = data->next)
1900 if(data->notify.b.fnact)
1902 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1903 notif->argv[1].d.n = fn->numpeers;
1905 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1912 static int fnunlink(struct fnetnode *fn, void *uudata)
1914 struct uidata *data;
1916 for(data = actives; data != NULL; data = data->next)
1918 if(data->notify.b.fnact)
1919 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1924 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1926 struct uidata *data;
1928 for(data = actives; data != NULL; data = data->next)
1930 if(data->notify.b.fnpeer)
1931 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1936 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1938 struct uidata *data;
1940 for(data = actives; data != NULL; data = data->next)
1942 if(data->notify.b.fnpeer)
1943 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1948 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1950 struct uidata *data;
1951 struct notif *notif;
1954 for(data = actives; data != NULL; data = data->next)
1956 if(data->notify.b.fnpeer)
1958 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1960 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1964 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1965 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1966 switch(di->datum->datatype)
1969 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1972 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1975 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1976 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1984 static int newfnetnode(struct fnetnode *fn, void *uudata)
1986 struct uidata *data;
1988 for(data = actives; data != NULL; data = data->next)
1990 if(data->notify.b.fnact)
1991 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1993 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1994 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1995 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1996 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1997 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1998 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
2002 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
2004 struct uidata *data;
2006 if(!wcscmp(attrib, L"state"))
2008 for(data = actives; data != NULL; data = data->next)
2010 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2011 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2013 } else if(!wcscmp(attrib, L"nick")) {
2014 for(data = actives; data != NULL; data = data->next)
2016 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2017 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2019 } else if(!wcscmp(attrib, L"size")) {
2020 for(data = actives; data != NULL; data = data->next)
2022 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2023 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END);
2025 } else if(!wcscmp(attrib, L"error")) {
2026 for(data = actives; data != NULL; data = data->next)
2028 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2029 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2031 } else if(!wcscmp(attrib, L"path")) {
2032 for(data = actives; data != NULL; data = data->next)
2034 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2035 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2037 } else if(!wcscmp(attrib, L"hash")) {
2038 for(data = actives; data != NULL; data = data->next)
2040 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2041 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2047 static int transferprog(struct transfer *transfer, void *uudata)
2049 struct uidata *data;
2050 struct notif *notif;
2052 for(data = actives; data != NULL; data = data->next)
2054 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2056 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2057 notif->argv[1].d.o = transfer->curpos;
2059 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2065 static int transferdestroyed(struct transfer *transfer, void *uudata)
2067 struct uidata *data;
2069 for(data = actives; data != NULL; data = data->next)
2071 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2072 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2077 static int newtransfernotify(struct transfer *transfer, void *uudata)
2079 struct uidata *data;
2081 for(data = actives; data != NULL; data = data->next)
2083 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2084 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);
2086 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2087 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2088 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2092 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2096 new = smalloc(sizeof(*new));
2098 new->name = swcsdup(name);
2109 static void freeuser(struct uiuser *user)
2111 if(user->next != NULL)
2112 user->next->prev = user->prev;
2113 if(user->prev != NULL)
2114 user->prev->next = user->next;
2121 static int conf_user(int argc, wchar_t **argv)
2123 int i, perms, permmod;
2124 struct uiuser *user;
2129 flog(LOG_WARNING, "not enough arguments given for user command");
2133 for(i = 2; i < argc; i++)
2135 if(!iswalpha(argv[i][0]))
2139 if(!wcscmp(p, L"disallow"))
2140 permmod = PERM_DISALLOW;
2141 if(!wcscmp(p, L"admin"))
2142 permmod = PERM_ADMIN;
2143 if(!wcscmp(p, L"fnetctl"))
2144 permmod = PERM_FNETCTL;
2145 if(!wcscmp(p, L"trans"))
2146 permmod = PERM_TRANS;
2147 if(!wcscmp(p, L"transcu"))
2148 permmod = PERM_TRANSCU;
2149 if(!wcscmp(p, L"chat"))
2150 permmod = PERM_CHAT;
2151 if(!wcscmp(p, L"srch"))
2152 permmod = PERM_SRCH;
2153 if(!wcscmp(p, L"all"))
2155 if(argv[i][0] == L'-')
2160 if((user = finduser(argv[1])) == NULL)
2162 newuser(argv[1], perms);
2165 user->perms = perms;
2170 static void preinit(int hup)
2172 struct uiuser *user;
2176 newuser(L"default", PERM_DISALLOW);
2178 for(user = users; user != NULL; user = user->next)
2180 if(!wcscmp(user->name, L"default"))
2186 static struct sockaddr_un *makeunixname(void)
2188 static struct sockaddr_un buf;
2193 memset(&buf, 0, sizeof(buf));
2194 buf.sun_family = PF_UNIX;
2195 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2196 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2199 if(!strcmp(val, "none"))
2201 if(!strcmp(val, "default"))
2203 if((uid = getuid()) == 0)
2205 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2208 if((pwd = getpwuid(uid)) == NULL)
2210 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2213 strcpy(buf.sun_path, "/tmp/doldacond-");
2214 strcat(buf.sun_path, pwd->pw_name);
2218 if(strchr(val, '/'))
2220 strcpy(buf.sun_path, val);
2223 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2227 static int tcpportupdate(struct configvar *var, void *uudata)
2229 struct socket *newsock;
2232 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2234 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2237 if(tcpsocket != NULL)
2242 tcpsocket = newsock;
2246 static int unixsockupdate(struct configvar *var, void *uudata)
2248 struct socket *newsock;
2249 struct sockaddr_un *un;
2254 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2257 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2261 if(unixsocket != NULL)
2263 putsock(unixsocket);
2266 unixsocket = newsock;
2270 static int init(int hup)
2272 struct uiuser *user, *next;
2273 struct sockaddr_un *un;
2280 for(user = users; user != NULL; user = next)
2289 starttime = time(NULL);
2290 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2292 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2295 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2297 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2300 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2304 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2305 GCBREG(newfncb, newfnetnode, NULL);
2306 GCBREG(newtransfercb, newtransfernotify, NULL);
2310 for(user = users; user != NULL; user = user->next)
2312 if(wcscmp(user->name, L"default"))
2317 if((pwd = getpwuid(getuid())) == NULL)
2319 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2322 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2324 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2327 newuser(wcsname, ~PERM_DISALLOW);
2334 static int run(void)
2337 struct uidata *data, *next;
2338 struct qcommand *qcmd;
2339 struct notif *notif, *nnotif;
2341 for(data = actives; data != NULL; data = next)
2347 for(data = actives; data != NULL; data = data->next)
2349 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2351 nnotif = notif->next;
2352 if(notif->state == NOTIF_WAIT)
2355 for(i = 0; i < notif->argc; i++)
2357 if(notif->argv[i].dt == NOTIF_ID)
2359 id = notif->argv[i].d.n;
2363 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2365 sq(data->sk, 2, L"%i", notif->code, NULL);
2366 for(i = 0; i < notif->argc; i++)
2368 switch(notif->argv[i].dt)
2372 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2375 sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL);
2378 if(notif->argv[i].d.s[0] == L'%')
2379 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2381 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2384 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2388 sq(data->sk, 0, NULL);
2389 if(notif->rlimit != 0)
2391 notif->state = NOTIF_WAIT;
2392 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2397 if((qcmd = unlinkqcmd(data)) != NULL)
2399 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2407 static void terminate(void)
2409 while(users != NULL)
2411 if(tcpsocket != NULL)
2413 if(unixsocket != NULL)
2414 putsock(unixsocket);
2417 static struct configvar myvars[] =
2419 /** If true, UI connections will only be accepted from localhost
2420 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2421 * completely sure that you know what you are doing, never turn
2422 * this off when auth.authless is on. */
2423 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2424 /** The TCP port number on which to accept UI client connections,
2425 * or -1 to not listen on TCP. */
2426 {CONF_VAR_INT, "port", {.num = 1500}},
2428 * Controls the the name to use for the Unix socket on which to
2429 * accept UI client connections. If the name contains a slash, it
2430 * is treated as a file name to bind on. If the name is "default",
2431 * the file name will be "/var/run/doldacond.sock" if doldacond
2432 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2433 * NAME is the user name of the UID which doldacond runs as. If
2434 * the name is "none", no Unix socket will be used. Otherwise, an
2435 * error is signaled.
2437 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2438 /** The TOS value to use for UI connections (see the TOS VALUES
2440 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2441 /** The name of the filtercmd script (see the FILES section for
2442 * lookup information). */
2443 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2447 static struct configcmd mycmds[] =
2449 {"user", conf_user},
2453 static struct module me =
2464 .terminate = terminate