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++)
1218 data->fcmdsk = wrapsock(pipe);
1219 data->fcmdpid = pid;
1220 if(data->fcmdbuf != NULL)
1222 free(data->fcmdbuf);
1223 data->fcmdbuf = NULL;
1225 data->fcmdbufsize = data->fcmdbufdata = 0;
1226 data->fcmdsk->data = data;
1227 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1228 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1231 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1233 struct transfer *transfer;
1237 havepriv(PERM_TRANS);
1238 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1240 sq(sk, 0, L"512", L"No such transfer", NULL);
1243 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1245 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1248 if(transfer->args == NULL)
1250 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1252 for(ta = transfer->args; ta != NULL; ta = ta->next)
1253 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1257 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1259 struct sharecache *node;
1263 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1265 if(node->f.b.type == FILE_REG)
1268 if(node->f.b.hastth)
1272 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1275 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1277 havepriv(PERM_TRANS);
1278 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1281 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1286 if(data->userinfo == NULL) {
1287 sq(sk, 0, L"502", L"Must be logged in", NULL);
1290 if(argv[1][0] == L'#') {
1291 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1294 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1295 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1296 sq(sk, 0, L"516", L"Name already in use", NULL);
1300 if(data->regname != NULL)
1301 free(data->regname);
1302 data->regname = swcsdup(argv[1]);
1303 sq(sk, 0, L"200", L"Registered", NULL);
1306 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1309 struct uidata *rcpt;
1311 struct notif *notif;
1314 if(data->userinfo == NULL) {
1315 sq(sk, 0, L"502", L"Must be logged in", NULL);
1318 if(argv[1][0] == L'#') {
1319 rcptid = wcstol(argv[1] + 1, NULL, 0);
1320 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1321 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1325 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1326 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1331 sq(sk, 0, L"517", L"No such recipient", NULL);
1334 if(!rcpt->notify.b.msg) {
1335 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1338 if(data->regname != NULL)
1339 myname = swcsdup(data->regname);
1341 myname = swprintf2(L"#%i", data->id);
1342 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1343 for(i = 2; i < argc; i++)
1344 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1345 sq(sk, 0, L"200", L"Message sent", NULL);
1348 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1350 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1353 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1355 extern volatile int reinit;
1357 havepriv(PERM_ADMIN);
1358 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1360 sq(sk, 0, L"200", L"Will reinit", NULL);
1367 * Reserved command numbers for nameless commands:
1368 * 0: Issued when a client has connected
1369 * 1: Issued when a named command couldn't be found
1372 static struct command commands[] =
1374 {NULL, cmd_connect},
1375 {NULL, cmd_notfound},
1376 {L"shutdown", cmd_shutdown},
1377 {L"quit", cmd_quit},
1378 {L"lsauth", cmd_lsauth},
1379 {L"login", cmd_login},
1380 {L"pass", cmd_pass},
1381 {L"cnct", cmd_fnetconnect},
1382 {L"lsnodes", cmd_lsnodes},
1383 {L"dcnct", cmd_disconnect},
1384 {L"lspa", cmd_lspa},
1385 {L"lspeers", cmd_lspeers},
1386 {L"download", cmd_download},
1387 {L"lstrans", cmd_lstrans},
1388 {L"cancel", cmd_cancel},
1389 {L"reset", cmd_reset},
1390 {L"notify", cmd_notify},
1391 {L"sendchat", cmd_sendchat},
1392 {L"search", cmd_search},
1393 {L"lssrch", cmd_lssrch},
1394 {L"lssr", cmd_lssr},
1395 {L"cansrch", cmd_cansrch},
1396 {L"filtercmd", cmd_filtercmd},
1397 {L"lstrarg", cmd_lstrarg},
1398 {L"hashstatus", cmd_hashstatus},
1399 {L"transstatus", cmd_transstatus},
1400 {L"register", cmd_register},
1401 {L"sendmsg", cmd_sendmsg},
1402 {L"uptime", cmd_uptime},
1407 static void freequeuecmd(struct qcommand *qcmd)
1411 if(qcmd->argv != NULL)
1413 for(i = 0; i < qcmd->argc; i++)
1414 free(qcmd->argv[i]);
1420 static struct qcommand *unlinkqcmd(struct uidata *data)
1422 struct qcommand *qcmd;
1428 data->queue = qcmd->next;
1429 if(qcmd == data->queuelast)
1430 data->queuelast = qcmd->next;
1435 static void notifappendv(struct notif *notif, va_list args)
1439 while((dt = va_arg(args, int)) != NOTIF_END)
1442 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1443 notif->argv[ca].dt = dt;
1448 notif->argv[ca].d.n = va_arg(args, int);
1451 notif->argv[ca].d.o = va_arg(args, off_t);
1454 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1457 notif->argv[ca].d.d = va_arg(args, double);
1463 static void notifappend(struct notif *notif, ...)
1467 va_start(args, notif);
1468 notifappendv(notif, args);
1472 static struct notif *newnotif(struct uidata *data, int code, ...)
1474 struct notif *notif;
1477 notif = smalloc(sizeof(*notif));
1478 memset(notif, 0, sizeof(*notif));
1479 notif->rlimit = 0.0;
1482 va_start(args, code);
1483 notifappendv(notif, args);
1486 notif->prev = data->lnotif;
1487 if(data->lnotif != NULL)
1488 data->lnotif->next = notif;
1490 data->fnotif = notif;
1491 data->lnotif = notif;
1495 static void freenotif(struct notif *notif)
1499 if(notif->next != NULL)
1500 notif->next->prev = notif->prev;
1501 if(notif->prev != NULL)
1502 notif->prev->next = notif->next;
1503 if(notif == notif->ui->fnotif)
1504 notif->ui->fnotif = notif->next;
1505 if(notif == notif->ui->lnotif)
1506 notif->ui->lnotif = notif->prev;
1507 if(notif->exptimer != NULL)
1508 canceltimer(notif->exptimer);
1509 for(i = 0; i < notif->argc; i++)
1511 if(notif->argv[i].dt == NOTIF_STR)
1512 free(notif->argv[i].d.s);
1514 if(notif->argv != NULL)
1519 static void notifexpire(int cancelled, struct notif *notif)
1521 notif->exptimer = NULL;
1526 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1530 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1532 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1537 for(i = 0; i < notif->argc; i++)
1539 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1554 static void freeuidata(struct uidata *data)
1557 struct qcommand *qcmd;
1559 if(data->next != NULL)
1560 data->next->prev = data->prev;
1561 if(data->prev != NULL)
1562 data->prev->next = data->next;
1564 actives = data->next;
1565 data->sk->readcb = NULL;
1566 data->sk->errcb = NULL;
1567 closesock(data->sk);
1569 while((qcmd = unlinkqcmd(data)) != NULL)
1571 iconv_close(data->ichandle);
1572 if(data->cw != NULL)
1574 if(data->cb != NULL)
1576 if(data->argv != NULL)
1578 for(i = 0; i < data->argc; i++)
1579 free(data->argv[i]);
1582 if(data->auth != NULL)
1583 authputhandle(data->auth);
1584 if(data->regname != NULL)
1585 free(data->regname);
1586 if(data->username != NULL)
1588 if(data->userinfo != NULL)
1589 flog(LOG_INFO, "%ls logged out", data->username);
1590 free(data->username);
1593 while(data->fnotif != NULL)
1594 freenotif(data->fnotif);
1595 if(data->fcmdbuf != NULL)
1596 free(data->fcmdbuf);
1597 if(data->fcmdpid != 0)
1598 kill(-data->fcmdpid, SIGHUP);
1599 if(data->fcmdsk != NULL)
1600 putsock(data->fcmdsk);
1604 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1606 struct qcommand *new;
1608 new = smalloc(sizeof(*new));
1613 if(data->queuelast != NULL)
1614 data->queuelast->next = new;
1615 data->queuelast = new;
1616 if(data->queue == NULL)
1621 static struct uidata *newuidata(struct socket *sk)
1623 struct uidata *data;
1624 static int curid = 0;
1626 data = smalloc(sizeof(*data));
1627 memset(data, 0, sizeof(*data));
1631 data->inbuf = smalloc(1024);
1633 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1635 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1638 data->next = actives;
1641 actives->prev = data;
1646 static void uiread(struct socket *sk, struct uidata *data)
1652 size_t datalen, len2;
1653 struct command *cur;
1655 if(data->indata > 1024)
1657 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1659 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1660 memcpy(data->inbuf + data->indata, newbuf, datalen);
1662 data->indata += datalen;
1663 if(data->cb == NULL)
1665 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1667 data->pp = data->cb;
1672 if(data->cbsize == data->cbdata)
1674 len2 = data->pp - data->cb;
1675 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1676 data->pp = data->cb + len2;
1679 p2 = (char *)(porig = (data->cb + data->cbdata));
1680 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1681 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1682 memmove(data->inbuf, p1, data->indata);
1683 /* Just a sanity check */
1684 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1686 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1689 data->cbdata += (((wchar_t *)p2) - porig);
1695 /* XXX: Should this really just ignore it? */
1703 /* Just a sanity check */
1704 if(data->cbsize != data->cbdata)
1706 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1711 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1719 while(!done && (data->pp - data->cb < data->cbdata))
1724 if(iswspace(*data->pp))
1726 if(*data->pp == L'\r')
1728 if(data->pp == data->cb + data->cbdata - 1)
1733 if(*(++data->pp) == L'\n')
1735 if((data->argv != NULL) && (data->argv[0] != NULL))
1737 for(cur = commands; cur->handler != NULL; cur++)
1739 if(cur->name == NULL)
1741 if(!wcscasecmp(cur->name, data->argv[0]))
1743 queuecmd(data, cur, data->argc, data->argv);
1747 if(cur->handler == NULL)
1748 queuecmd(data, &commands[1], data->argc, data->argv);
1750 queuecmd(data, &commands[1], data->argc, data->argv);
1755 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1756 data->pp = data->cb;
1769 if(iswspace(*data->pp))
1771 addtobuf(data->cw, L'\0');
1772 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1773 data->argv[data->argc++] = data->cw;
1778 } else if(*data->pp == L'\"') {
1781 } else if(*data->pp == L'\\') {
1782 if(data->pp == data->cb + data->cbdata - 1)
1787 addtobuf(data->cw, *(++data->pp));
1790 addtobuf(data->cw, *(data->pp++));
1794 if(*data->pp == L'\"')
1797 } else if(*data->pp == L'\\') {
1798 if(data->pp == data->cb + data->cbdata - 1)
1803 addtobuf(data->cw, *(++(data->pp)));
1805 addtobuf(data->cw, *data->pp);
1811 if(data->cbdata > 16384)
1813 /* Kill clients that send us unreasonably long lines */
1818 static void uierror(struct socket *sk, int err, struct uidata *data)
1821 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1825 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1827 struct uidata *uidata;
1829 newsk->data = uidata = newuidata(newsk);
1830 socksettos(newsk, confgetint("ui", "uitos"));
1833 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1834 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1835 queuecmd(uidata, &commands[0], 0, NULL);
1838 static int srcheta(struct search *srch, void *uudata)
1840 struct uidata *data;
1842 for(data = actives; data != NULL; data = data->next)
1844 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1845 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1850 static int srchcommit(struct search *srch, void *uudata)
1852 struct uidata *data;
1854 for(data = actives; data != NULL; data = data->next)
1856 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1857 newnotif(data, 621, NOTIF_ID, srch->id, NOTIF_END);
1862 static int srchres(struct search *srch, struct srchres *sr, void *uudata)
1864 struct uidata *data;
1866 for(data = actives; data != NULL; data = data->next)
1868 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1870 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size,
1871 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);
1877 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1879 struct uidata *data;
1881 for(data = actives; data != NULL; data = data->next)
1883 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1884 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1889 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1891 struct uidata *data;
1892 struct notif *notif;
1894 if(!wcscmp(attrib, L"state"))
1896 for(data = actives; data != NULL; data = data->next)
1898 if(data->notify.b.fnact)
1899 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1901 } else if(!wcscmp(attrib, L"name")) {
1902 for(data = actives; data != NULL; data = data->next)
1904 if(data->notify.b.fnact)
1905 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1907 } else if(!wcscmp(attrib, L"numpeers")) {
1908 for(data = actives; data != NULL; data = data->next)
1910 if(data->notify.b.fnact)
1912 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1913 notif->argv[1].d.n = fn->numpeers;
1915 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1922 static int fnunlink(struct fnetnode *fn, void *uudata)
1924 struct uidata *data;
1926 for(data = actives; data != NULL; data = data->next)
1928 if(data->notify.b.fnact)
1929 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1934 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1936 struct uidata *data;
1938 for(data = actives; data != NULL; data = data->next)
1940 if(data->notify.b.fnpeer)
1941 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1946 static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1948 struct uidata *data;
1950 for(data = actives; data != NULL; data = data->next)
1952 if(data->notify.b.fnpeer)
1953 newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1958 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1960 struct uidata *data;
1961 struct notif *notif;
1964 for(data = actives; data != NULL; data = data->next)
1966 if(data->notify.b.fnpeer)
1968 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1970 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1974 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1975 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1976 switch(di->datum->datatype)
1979 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1982 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1985 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1986 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1994 static int newfnetnode(struct fnetnode *fn, void *uudata)
1996 struct uidata *data;
1998 for(data = actives; data != NULL; data = data->next)
2000 if(data->notify.b.fnact)
2001 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
2003 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
2004 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
2005 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
2006 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
2007 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
2008 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
2012 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
2014 struct uidata *data;
2016 if(!wcscmp(attrib, L"state"))
2018 for(data = actives; data != NULL; data = data->next)
2020 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2021 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2023 } else if(!wcscmp(attrib, L"nick")) {
2024 for(data = actives; data != NULL; data = data->next)
2026 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2027 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2029 } else if(!wcscmp(attrib, L"size")) {
2030 for(data = actives; data != NULL; data = data->next)
2032 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2033 newnotif(data, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END);
2035 } else if(!wcscmp(attrib, L"error")) {
2036 for(data = actives; data != NULL; data = data->next)
2038 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2039 newnotif(data, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2041 } else if(!wcscmp(attrib, L"path")) {
2042 for(data = actives; data != NULL; data = data->next)
2044 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2045 newnotif(data, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2047 } else if(!wcscmp(attrib, L"hash")) {
2048 for(data = actives; data != NULL; data = data->next)
2050 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2051 newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2057 static int transferprog(struct transfer *transfer, void *uudata)
2059 struct uidata *data;
2060 struct notif *notif;
2062 for(data = actives; data != NULL; data = data->next)
2064 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2066 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2067 notif->argv[1].d.o = transfer->curpos;
2069 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2075 static int transferdestroyed(struct transfer *transfer, void *uudata)
2077 struct uidata *data;
2079 for(data = actives; data != NULL; data = data->next)
2081 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2082 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2087 static int newtransfernotify(struct transfer *transfer, void *uudata)
2089 struct uidata *data;
2091 for(data = actives; data != NULL; data = data->next)
2093 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2094 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);
2096 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2097 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2098 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2102 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2106 new = smalloc(sizeof(*new));
2108 new->name = swcsdup(name);
2119 static void freeuser(struct uiuser *user)
2121 if(user->next != NULL)
2122 user->next->prev = user->prev;
2123 if(user->prev != NULL)
2124 user->prev->next = user->next;
2131 static int conf_user(int argc, wchar_t **argv)
2133 int i, perms, permmod;
2134 struct uiuser *user;
2139 flog(LOG_WARNING, "not enough arguments given for user command");
2143 for(i = 2; i < argc; i++)
2145 if(!iswalpha(argv[i][0]))
2149 if(!wcscmp(p, L"disallow"))
2150 permmod = PERM_DISALLOW;
2151 if(!wcscmp(p, L"admin"))
2152 permmod = PERM_ADMIN;
2153 if(!wcscmp(p, L"fnetctl"))
2154 permmod = PERM_FNETCTL;
2155 if(!wcscmp(p, L"trans"))
2156 permmod = PERM_TRANS;
2157 if(!wcscmp(p, L"transcu"))
2158 permmod = PERM_TRANSCU;
2159 if(!wcscmp(p, L"chat"))
2160 permmod = PERM_CHAT;
2161 if(!wcscmp(p, L"srch"))
2162 permmod = PERM_SRCH;
2163 if(!wcscmp(p, L"all"))
2165 if(argv[i][0] == L'-')
2170 if((user = finduser(argv[1])) == NULL)
2172 newuser(argv[1], perms);
2175 user->perms = perms;
2180 static void preinit(int hup)
2182 struct uiuser *user;
2186 newuser(L"default", PERM_DISALLOW);
2188 for(user = users; user != NULL; user = user->next)
2190 if(!wcscmp(user->name, L"default"))
2196 static struct sockaddr_un *makeunixname(void)
2198 static struct sockaddr_un buf;
2203 memset(&buf, 0, sizeof(buf));
2204 buf.sun_family = PF_UNIX;
2205 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2206 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2209 if(!strcmp(val, "none"))
2211 if(!strcmp(val, "default"))
2213 if((uid = getuid()) == 0)
2215 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2218 if((pwd = getpwuid(uid)) == NULL)
2220 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2223 strcpy(buf.sun_path, "/tmp/doldacond-");
2224 strcat(buf.sun_path, pwd->pw_name);
2228 if(strchr(val, '/'))
2230 strcpy(buf.sun_path, val);
2233 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2237 static int tcpportupdate(struct configvar *var, void *uudata)
2239 struct socket *newsock;
2242 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2244 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2247 if(tcpsocket != NULL)
2252 tcpsocket = newsock;
2256 static int unixsockupdate(struct configvar *var, void *uudata)
2258 struct socket *newsock;
2259 struct sockaddr_un *un;
2264 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2267 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2271 if(unixsocket != NULL)
2273 putsock(unixsocket);
2276 unixsocket = newsock;
2280 static int init(int hup)
2282 struct uiuser *user, *next;
2283 struct sockaddr_un *un;
2290 for(user = users; user != NULL; user = next)
2299 starttime = time(NULL);
2300 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2302 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2305 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2307 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2310 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2314 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2315 GCBREG(newfncb, newfnetnode, NULL);
2316 GCBREG(newtransfercb, newtransfernotify, NULL);
2320 for(user = users; user != NULL; user = user->next)
2322 if(wcscmp(user->name, L"default"))
2327 if((pwd = getpwuid(getuid())) == NULL)
2329 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2332 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2334 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2337 newuser(wcsname, ~PERM_DISALLOW);
2344 static int run(void)
2347 struct uidata *data, *next;
2348 struct qcommand *qcmd;
2349 struct notif *notif, *nnotif;
2351 for(data = actives; data != NULL; data = next)
2357 for(data = actives; data != NULL; data = data->next)
2359 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2361 nnotif = notif->next;
2362 if(notif->state == NOTIF_WAIT)
2365 for(i = 0; i < notif->argc; i++)
2367 if(notif->argv[i].dt == NOTIF_ID)
2369 id = notif->argv[i].d.n;
2373 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2375 sq(data->sk, 2, L"%i", notif->code, NULL);
2376 for(i = 0; i < notif->argc; i++)
2378 switch(notif->argv[i].dt)
2382 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2385 sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL);
2388 if(notif->argv[i].d.s[0] == L'%')
2389 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2391 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2394 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2398 sq(data->sk, 0, NULL);
2399 if(notif->rlimit != 0)
2401 notif->state = NOTIF_WAIT;
2402 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2407 if((qcmd = unlinkqcmd(data)) != NULL)
2409 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2413 if(data->queuesize > 10)
2415 /* Clients should not be queue up commands at all, since
2416 * they should not send a new command before receiving a
2417 * reply to the previous command. Therefore, we
2418 * mercilessly massacre clients which are stacking up too
2426 static void terminate(void)
2428 while(users != NULL)
2430 if(tcpsocket != NULL)
2432 if(unixsocket != NULL)
2433 putsock(unixsocket);
2436 static struct configvar myvars[] =
2438 /** If true, UI connections will only be accepted from localhost
2439 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2440 * completely sure that you know what you are doing, never turn
2441 * this off when auth.authless is on. */
2442 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2443 /** The TCP port number on which to accept UI client connections,
2444 * or -1 to not listen on TCP. */
2445 {CONF_VAR_INT, "port", {.num = 1500}},
2447 * Controls the the name to use for the Unix socket on which to
2448 * accept UI client connections. If the name contains a slash, it
2449 * is treated as a file name to bind on. If the name is "default",
2450 * the file name will be "/var/run/doldacond.sock" if doldacond
2451 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2452 * NAME is the user name of the UID which doldacond runs as. If
2453 * the name is "none", no Unix socket will be used. Otherwise, an
2454 * error is signaled.
2456 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2457 /** The TOS value to use for UI connections (see the TOS VALUES
2459 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2460 /** The name of the filtercmd script (see the FILES section for
2461 * lookup information). */
2462 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2466 static struct configcmd mycmds[] =
2468 {"user", conf_user},
2472 static struct module me =
2483 .terminate = terminate