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;
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 = fn->peers; peer != NULL; peer = peer->next)
711 sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%ls", peer->id, L"%ls", peer->nick, NULL);
712 for(i = 0; i < peer->dinum; i++)
714 if(peer->peerdi[i].datum->datatype == FNPD_INT)
715 sq(sk, 2, peer->peerdi[i].datum->id, L"%i", peer->peerdi[i].data.num, NULL);
716 if(peer->peerdi[i].datum->datatype == FNPD_LL)
717 sq(sk, 2, peer->peerdi[i].datum->id, L"%ll", peer->peerdi[i].data.lnum, NULL);
718 if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL))
719 sq(sk, 2, peer->peerdi[i].datum->id, L"%ls", peer->peerdi[i].data.str, NULL);
726 static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
731 struct transfer *transfer;
732 struct fnetpeer *peer;
735 if((argc > 5) && ((argc % 2) == 0))
737 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
740 havepriv(PERM_TRANS);
741 if((*(argv[1]) >= L'0') && (*(argv[1]) <= L'9'))
743 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
745 sq(sk, 0, L"510", L"No such node", NULL);
751 if((net = findfnet(argv[1])) == NULL)
753 sq(sk, 0, L"511", L"No such network name", NULL);
757 transfer = newtransfer();
758 authgethandle(transfer->auth = data->auth);
759 transfer->fnet = net;
760 transfer->peerid = swcsdup(argv[2]);
761 transfer->path = swcsdup(argv[3]);
762 transfer->dir = TRNSD_DOWN;
763 transfer->owner = data->uid;
768 linktransfer(transfer);
769 if(((peer = fnetfindpeer(fn, transfer->peerid)) != NULL) && (peer->nick != NULL))
770 transfersetnick(transfer, peer->nick);
772 linktransfer(transfer);
775 transfersetsize(transfer, wcstoll(argv[4], NULL, 0));
778 for(i = 5; i < argc; i += 2)
780 if(!wcscmp(argv[i], L"hash"))
782 transfersethash(transfer, parsehash(argv[i + 1]));
784 newwcspair(argv[i], argv[i + 1], &transfer->args);
788 sq(sk, 0, L"200", L"%i", transfer->id, L"Download queued", NULL);
789 transfersetactivity(transfer, L"create");
792 static void cmd_lstrans(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
794 struct transfer *transfer, *pt;
796 havepriv(PERM_TRANS);
798 for(transfer = transfers; transfer != NULL; transfer = transfer->next)
800 if((transfer->dir != TRNSD_DOWN) || (transfer->owner == data->uid))
803 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->dir,
804 L"%i", pt->state, pt->peerid,
805 (pt->peernick == NULL)?L"":(pt->peernick),
806 (pt->path == NULL)?L"":(pt->path),
807 L"%oi", pt->size, L"%oi", pt->curpos,
808 (pt->hash == NULL)?L"":unparsehash(pt->hash),
814 sq(sk, 0, L"201", L"No transfers", NULL);
816 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->dir,
817 L"%i", pt->state, pt->peerid,
818 (pt->peernick == NULL)?L"":(pt->peernick),
819 (pt->path == NULL)?L"":(pt->path),
820 L"%oi", pt->size, L"%oi", pt->curpos,
821 (pt->hash == NULL)?L"":unparsehash(pt->hash),
825 static void cmd_cancel(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
827 struct transfer *transfer;
830 havepriv(PERM_TRANS);
831 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
833 sq(sk, 0, L"512", L"No such transfer", NULL);
836 if((transfer->dir == TRNSD_UP) && !(data->userinfo->perms & PERM_TRANSCU))
838 sq(sk, 0, L"502", L"You are not allowed to cancel uploads", NULL);
841 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
843 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
847 sq(sk, 0, L"200", L"Transfer cancelled", NULL);
850 static void cmd_reset(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
852 struct transfer *transfer;
855 havepriv(PERM_TRANS);
856 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
858 sq(sk, 0, L"512", L"No such transfer", NULL);
861 if(transfer->dir == TRNSD_UP)
863 sq(sk, 0, L"512", L"Only applicable to downloads", NULL);
866 resettransfer(transfer);
867 sq(sk, 0, L"200", L"Transfer reset", NULL);
870 static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
876 sq(sk, 0, L"501", L"Must have an even number of arguments", NULL);
879 for(i = 1; i < argc; i += 2)
881 if(!wcscasecmp(argv[i + 1], L"on"))
885 if(!wcscasecmp(argv[i], L"all"))
891 } else if(!wcscasecmp(argv[i], L"fn:chat")) {
892 data->notify.b.fnchat = val;
893 } else if(!wcscasecmp(argv[i], L"fn:act")) {
894 data->notify.b.fnact = val;
895 } else if(!wcscasecmp(argv[i], L"fn:peer")) {
896 data->notify.b.fnpeer = val;
897 } else if(!wcscasecmp(argv[i], L"trans:act")) {
898 data->notify.b.tract = val;
899 } else if(!wcscasecmp(argv[i], L"trans:prog")) {
900 data->notify.b.trprog = val;
901 } else if(!wcscasecmp(argv[i], L"srch:act")) {
902 data->notify.b.srch = val;
903 } else if(!wcscasecmp(argv[i], L"msg")) {
904 data->notify.b.msg = val;
907 sq(sk, 0, L"200", L"Notification alteration succeeded", NULL);
910 static void cmd_sendchat(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
917 if((fn = findfnetnode(wcstol(argv[1], NULL, 0))) == NULL)
919 sq(sk, 0, L"510", L"No such node", NULL);
922 public = wcstol(argv[2], NULL, 0);
923 if((public != 0) && (public != 1))
925 sq(sk, 0, L"509", L"Second argument must be 0 or 1", NULL);
928 if(fn->state != FNN_EST)
930 sq(sk, 0, L"513", L"Hub is in state FNN_EST", NULL);
933 if(fnetsendchat(fn, public, argv[3], argv[4]))
936 sq(sk, 0, L"513", L"This network does not support chatting", NULL);
937 else if(errno == EPERM)
938 sq(sk, 0, L"502", L"This node does not allow you to chat", NULL);
939 else if(errno == EILSEQ)
940 sq(sk, 0, L"504", L"This network could not support all the characters in that message", NULL);
942 sq(sk, 0, L"505", L"Could not chat", NULL);
945 sq(sk, 0, L"200", L"Chat string sent", NULL);
948 static void cmd_search(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
957 srch = newsearch(data->username, NULL);
958 for(i = 1; i < argc; i++)
960 if(!wcscmp(argv[i], L"all"))
962 for(fn = fnetnodes; fn != NULL; fn = fn->next)
964 if(fn->state == FNN_EST)
965 searchaddfn(srch, fn);
969 } else if(!wcscmp(argv[i], L"prio")) {
972 sq(sk, 0, L"501", L"No argument to prio", NULL);
976 srch->prio = wcstol(argv[i], NULL, 0);
977 } else if(iswdigit(*argv[i])) {
978 if((fn = findfnetnode(wcstol(argv[i], NULL, 0))) == NULL)
980 sq(sk, 0, L"510", L"No such node", NULL);
984 searchaddfn(srch, fn);
989 if(srch->fnl == NULL)
991 sq(sk, 0, L"501", L"No fnetnodes to search found on line", NULL);
997 sq(sk, 0, L"501", L"No search expression found on line", NULL);
1001 if((sexpr = parsesexpr(argc - i, argv + i)) == NULL)
1003 sq(sk, 0, L"509", L"Could not parse search expression", NULL);
1008 getsexpr(srch->sexpr = sexpr);
1010 CBREG(srch, search_eta, srcheta, NULL, NULL);
1011 CBREG(srch, search_commit, srchcommit, NULL, NULL);
1012 CBREG(srch, search_result, srchres, NULL, NULL);
1013 sq(sk, 0, L"200", L"%i", srch->id, L"%i", srch->eta - time(NULL), NULL);
1017 static void cmd_lssrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1019 struct search *srch, *pt;
1022 havepriv(PERM_SRCH);
1025 for(srch = searches; srch != NULL; srch = srch->next)
1027 if(!wcscmp(srch->owner, data->username))
1030 sq(sk, 1, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1035 sq(sk, 0, L"201", L"No searches", NULL);
1037 sq(sk, 0, L"200", L"%i", pt->id, L"%i", pt->state, L"%i", pt->eta - now, L"%i", pt->numres, NULL);
1040 static void cmd_lssr(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1042 struct search *srch;
1046 havepriv(PERM_SRCH);
1047 if((srch = findsearch(wcstol(argv[1], NULL, 0))) == NULL)
1049 sq(sk, 0, L"514", L"No such search", NULL);
1052 if(srch->results == NULL)
1054 sq(sk, 0, L"201", L"No results", NULL);
1056 for(sr = srch->results; sr != NULL; sr = sr->next)
1058 sq(sk, (sr->next != NULL)?1:0, L"200", L"%ls", sr->filename,
1059 sr->fnet->name, L"%ls", sr->peerid, L"%oi", sr->size,
1060 L"%i", sr->slots, L"%i", (sr->fn == NULL)?-1:(sr->fn->id),
1062 L"%ls", (sr->hash == NULL)?L"":unparsehash(sr->hash), NULL);
1067 static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1069 struct search *srch;
1073 havepriv(PERM_SRCH);
1074 /* Note - Programmatical user interfaces must only give one
1075 * argument per command, the multiple argument form is only for
1076 * convenience when manually controlling the daemon via
1077 * eg. telnet. The reason is that the return codes aren't clear
1078 * enough for the multiple argument form. */
1079 for(i = 1; i < argc; i++)
1081 if((srch = findsearch(wcstol(argv[i], NULL, 0))) == NULL)
1083 sq(sk, 0, L"514", L"No such search", NULL);
1088 sq(sk, 0, L"200", L"Search cancelled", NULL);
1091 static void fcmdread(struct socket *sk, struct uidata *data)
1096 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
1098 bufcat(data->fcmdbuf, buf, bufsize);
1102 static void fcmderr(struct socket *sk, int err, struct uidata *data)
1104 wchar_t *wbuf, *p, *p2;
1108 flog(LOG_WARNING, "error occurred on filtercmd pipe socket: %s", strerror(err));
1109 kill(-data->fcmdpid, SIGHUP);
1110 putsock(data->fcmdsk);
1111 data->fcmdsk = NULL;
1112 if(data->fcmdbuf != NULL)
1114 free(data->fcmdbuf);
1115 data->fcmdbuf = NULL;
1117 data->fcmdbufsize = data->fcmdbufdata = 0;
1118 sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%s", strerror(err), NULL);
1121 putsock(data->fcmdsk);
1122 data->fcmdsk = NULL;
1124 if(data->fcmdbuf == NULL)
1126 wbuf = swcsdup(L"");
1128 addtobuf(data->fcmdbuf, 0);
1129 wbuf = icmbstowcs(data->fcmdbuf, NULL);
1130 free(data->fcmdbuf);
1132 data->fcmdbuf = NULL;
1133 data->fcmdbufsize = data->fcmdbufdata = 0;
1136 sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL);
1140 for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n'))
1143 sq(data->sk, (*p2 == L'\0')?0:1, L"200", L"%ls", p, NULL);
1149 sq(data->sk, 0, L"201", L"No data returned", NULL);
1151 sq(data->sk, 0, L"200", L"%ls", p, NULL);
1156 static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1162 char *filtercmd, *argbuf;
1163 size_t cargvsize, cargvdata;
1164 struct passwd *pwent;
1167 havepriv(PERM_TRANS);
1168 if((pwent = getpwuid(data->uid)) == NULL)
1170 flog(LOG_WARNING, "no passwd entry for UI user %i", data->uid);
1171 sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL);
1174 filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0);
1175 if(filtercmd == NULL)
1176 filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0);
1177 if(filtercmd == NULL)
1179 flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name);
1180 sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL);
1184 cargvsize = cargvdata = 0;
1185 addtobuf(cargv, filtercmd);
1186 for(i = 1; i < argc; i++)
1188 if((argbuf = icwcstombs(argv[i], NULL)) == NULL)
1190 for(i = 0; i < cargvdata; i++)
1193 sq(sk, 0, L"504", L"Could not convert argument into local character set", L"%i", i, L"%s", strerror(errno), NULL);
1196 addtobuf(cargv, argbuf);
1198 addtobuf(cargv, NULL);
1199 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)
1201 flog(LOG_WARNING, "could not fork session in filtercmd: %s", strerror(errno));
1202 sq(sk, 0, L"505", L"System error - Could not fork session", L"%s", strerror(errno), NULL);
1207 execv(filtercmd, cargv);
1208 flog(LOG_WARNING, "could not exec filtercmd %s: %s", filtercmd, strerror(errno));
1211 for(pp = cargv; *pp; pp++)
1214 data->fcmdsk = wrapsock(pipe);
1215 data->fcmdpid = pid;
1216 if(data->fcmdbuf != NULL)
1218 free(data->fcmdbuf);
1219 data->fcmdbuf = NULL;
1221 data->fcmdbufsize = data->fcmdbufdata = 0;
1222 data->fcmdsk->data = data;
1223 data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread;
1224 data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr;
1227 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1229 struct transfer *transfer;
1233 havepriv(PERM_TRANS);
1234 if((transfer = findtransfer(wcstol(argv[1], NULL, 0))) == NULL)
1236 sq(sk, 0, L"512", L"No such transfer", NULL);
1239 if((transfer->dir == TRNSD_DOWN) && (transfer->owner != data->uid))
1241 sq(sk, 0, L"502", L"You do not own that transfer", NULL);
1244 if(transfer->args == NULL)
1246 sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
1248 for(ta = transfer->args; ta != NULL; ta = ta->next)
1249 sq(sk, ta->next != NULL, L"200", L"%ls", ta->key, L"%ls", ta->val, NULL);
1253 static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1255 struct sharecache *node;
1259 for(node = shareroot->child; node != NULL; node = nextscnode(node))
1261 if(node->f.b.type == FILE_REG)
1264 if(node->f.b.hastth)
1268 sq(sk, 0, L"200", L"%i", total, L"tth", L"%i", hashed, NULL);
1271 static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1273 havepriv(PERM_TRANS);
1274 sq(sk, 0, L"200", L"down", L"%ll", bytesdownload, L"up", L"%ll", bytesupload, NULL);
1277 static void cmd_register(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1282 if(data->userinfo == NULL) {
1283 sq(sk, 0, L"502", L"Must be logged in", NULL);
1286 if(argv[1][0] == L'#') {
1287 sq(sk, 0, L"509", L"Name must not begin with a hash sign", NULL);
1290 for(d2 = actives; d2 != NULL; d2 = d2->next) {
1291 if((d2 != data) && (d2->userinfo == data->userinfo) && d2->regname && !wcscmp(d2->regname, argv[1])) {
1292 sq(sk, 0, L"516", L"Name already in use", NULL);
1296 if(data->regname != NULL)
1297 free(data->regname);
1298 data->regname = swcsdup(argv[1]);
1299 sq(sk, 0, L"200", L"Registered", NULL);
1302 static void cmd_sendmsg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1305 struct uidata *rcpt;
1307 struct notif *notif;
1310 if(data->userinfo == NULL) {
1311 sq(sk, 0, L"502", L"Must be logged in", NULL);
1314 if(argv[1][0] == L'#') {
1315 rcptid = wcstol(argv[1] + 1, NULL, 0);
1316 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1317 if((rcpt->userinfo == data->userinfo) && (rcpt->id == rcptid))
1321 for(rcpt = actives; rcpt != NULL; rcpt = rcpt->next) {
1322 if((rcpt->userinfo == data->userinfo) && rcpt->regname && !wcscmp(rcpt->regname, argv[1]))
1327 sq(sk, 0, L"517", L"No such recipient", NULL);
1330 if(!rcpt->notify.b.msg) {
1331 sq(sk, 0, L"518", L"Recipient not listening for messages", NULL);
1334 if(data->regname != NULL)
1335 myname = swcsdup(data->regname);
1337 myname = swprintf2(L"#%i", data->id);
1338 notif = newnotif(rcpt, 640, NOTIF_STR, myname, NOTIF_END);
1339 for(i = 2; i < argc; i++)
1340 notifappend(notif, NOTIF_STR, argv[i], NOTIF_END);
1341 sq(sk, 0, L"200", L"Message sent", NULL);
1344 static void cmd_uptime(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1346 sq(sk, 0, L"200", L"%i", time(NULL) - starttime, NULL);
1349 static void cmd_hup(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
1351 extern volatile int reinit;
1353 havepriv(PERM_ADMIN);
1354 flog(LOG_NOTICE, "UI HUP request from %ls", data->username);
1356 sq(sk, 0, L"200", L"Will reinit", NULL);
1363 * Reserved command numbers for nameless commands:
1364 * 0: Issued when a client has connected
1365 * 1: Issued when a named command couldn't be found
1368 static struct command commands[] =
1370 {NULL, cmd_connect},
1371 {NULL, cmd_notfound},
1372 {L"shutdown", cmd_shutdown},
1373 {L"quit", cmd_quit},
1374 {L"lsauth", cmd_lsauth},
1375 {L"login", cmd_login},
1376 {L"pass", cmd_pass},
1377 {L"cnct", cmd_fnetconnect},
1378 {L"lsnodes", cmd_lsnodes},
1379 {L"dcnct", cmd_disconnect},
1380 {L"lspa", cmd_lspa},
1381 {L"lspeers", cmd_lspeers},
1382 {L"download", cmd_download},
1383 {L"lstrans", cmd_lstrans},
1384 {L"cancel", cmd_cancel},
1385 {L"reset", cmd_reset},
1386 {L"notify", cmd_notify},
1387 {L"sendchat", cmd_sendchat},
1388 {L"search", cmd_search},
1389 {L"lssrch", cmd_lssrch},
1390 {L"lssr", cmd_lssr},
1391 {L"cansrch", cmd_cansrch},
1392 {L"filtercmd", cmd_filtercmd},
1393 {L"lstrarg", cmd_lstrarg},
1394 {L"hashstatus", cmd_hashstatus},
1395 {L"transstatus", cmd_transstatus},
1396 {L"register", cmd_register},
1397 {L"sendmsg", cmd_sendmsg},
1398 {L"uptime", cmd_uptime},
1403 static void freequeuecmd(struct qcommand *qcmd)
1407 if(qcmd->argv != NULL)
1409 for(i = 0; i < qcmd->argc; i++)
1410 free(qcmd->argv[i]);
1416 static struct qcommand *unlinkqcmd(struct uidata *data)
1418 struct qcommand *qcmd;
1423 data->queue = qcmd->next;
1424 if(qcmd == data->queuelast)
1425 data->queuelast = qcmd->next;
1430 static void notifappendv(struct notif *notif, va_list args)
1434 while((dt = va_arg(args, int)) != NOTIF_END)
1437 notif->argv = realloc(notif->argv, sizeof(*notif->argv) * ++notif->argc);
1438 notif->argv[ca].dt = dt;
1443 notif->argv[ca].d.n = va_arg(args, int);
1446 notif->argv[ca].d.o = va_arg(args, off_t);
1449 notif->argv[ca].d.s = swcsdup(va_arg(args, wchar_t *));
1452 notif->argv[ca].d.d = va_arg(args, double);
1458 static void notifappend(struct notif *notif, ...)
1462 va_start(args, notif);
1463 notifappendv(notif, args);
1467 static struct notif *newnotif(struct uidata *data, int code, ...)
1469 struct notif *notif;
1472 notif = smalloc(sizeof(*notif));
1473 memset(notif, 0, sizeof(*notif));
1474 notif->rlimit = 0.0;
1477 va_start(args, code);
1478 notifappendv(notif, args);
1481 notif->prev = data->lnotif;
1482 if(data->lnotif != NULL)
1483 data->lnotif->next = notif;
1485 data->fnotif = notif;
1486 data->lnotif = notif;
1490 static void freenotif(struct notif *notif)
1494 if(notif->next != NULL)
1495 notif->next->prev = notif->prev;
1496 if(notif->prev != NULL)
1497 notif->prev->next = notif->next;
1498 if(notif == notif->ui->fnotif)
1499 notif->ui->fnotif = notif->next;
1500 if(notif == notif->ui->lnotif)
1501 notif->ui->lnotif = notif->prev;
1502 if(notif->exptimer != NULL)
1503 canceltimer(notif->exptimer);
1504 for(i = 0; i < notif->argc; i++)
1506 if(notif->argv[i].dt == NOTIF_STR)
1507 free(notif->argv[i].d.s);
1509 if(notif->argv != NULL)
1514 static void notifexpire(int cancelled, struct notif *notif)
1516 notif->exptimer = NULL;
1521 static struct notif *findnotif(struct notif *notif, int dir, int state, int code, int id)
1525 for(; notif != NULL; notif = (dir?notif->next:notif->prev))
1527 if((notif->code == code) && ((state < 0) || (state == notif->state)))
1532 for(i = 0; i < notif->argc; i++)
1534 if((notif->argv[i].dt == NOTIF_ID) && (notif->argv[i].d.n != id))
1549 static void freeuidata(struct uidata *data)
1552 struct qcommand *qcmd;
1554 if(data->next != NULL)
1555 data->next->prev = data->prev;
1556 if(data->prev != NULL)
1557 data->prev->next = data->next;
1559 actives = data->next;
1560 data->sk->readcb = NULL;
1561 data->sk->errcb = NULL;
1563 while((qcmd = unlinkqcmd(data)) != NULL)
1565 iconv_close(data->ichandle);
1566 if(data->cw != NULL)
1568 if(data->cb != NULL)
1570 if(data->argv != NULL)
1572 for(i = 0; i < data->argc; i++)
1573 free(data->argv[i]);
1576 if(data->auth != NULL)
1577 authputhandle(data->auth);
1578 if(data->regname != NULL)
1579 free(data->regname);
1580 if(data->username != NULL)
1582 if(data->userinfo != NULL)
1583 flog(LOG_INFO, "%ls logged out", data->username);
1584 free(data->username);
1587 while(data->fnotif != NULL)
1588 freenotif(data->fnotif);
1589 if(data->fcmdbuf != NULL)
1590 free(data->fcmdbuf);
1591 if(data->fcmdpid != 0)
1592 kill(-data->fcmdpid, SIGHUP);
1593 if(data->fcmdsk != NULL)
1594 putsock(data->fcmdsk);
1598 static void queuecmd(struct uidata *data, struct command *cmd, int argc, wchar_t **argv)
1600 struct qcommand *new;
1602 new = smalloc(sizeof(*new));
1607 if(data->queuelast != NULL)
1608 data->queuelast->next = new;
1609 data->queuelast = new;
1610 if(data->queue == NULL)
1614 static struct uidata *newuidata(struct socket *sk)
1616 struct uidata *data;
1617 static int curid = 0;
1619 data = smalloc(sizeof(*data));
1620 memset(data, 0, sizeof(*data));
1624 data->inbuf = smalloc(1024);
1626 if((data->ichandle = iconv_open("wchar_t", "utf-8")) == (iconv_t)-1)
1628 flog(LOG_CRIT, "iconv cannot handle UTF-8: %s", strerror(errno));
1631 data->next = actives;
1634 actives->prev = data;
1639 static void uiread(struct socket *sk, struct uidata *data)
1645 size_t datalen, len2;
1646 struct command *cur;
1648 if(data->indata > 1024)
1650 if((newbuf = sockgetinbuf(sk, &datalen)) == NULL)
1652 sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1);
1653 memcpy(data->inbuf + data->indata, newbuf, datalen);
1655 data->indata += datalen;
1656 if(data->cb == NULL)
1658 data->cb = smalloc(sizeof(wchar_t) * (data->cbsize = 64));
1660 data->pp = data->cb;
1665 if(data->cbsize == data->cbdata)
1667 len2 = data->pp - data->cb;
1668 data->cb = srealloc(data->cb, sizeof(wchar_t) * (data->cbsize *= 2));
1669 data->pp = data->cb + len2;
1672 p2 = (char *)(porig = (data->cb + data->cbdata));
1673 len2 = sizeof(wchar_t) * (data->cbsize - data->cbdata);
1674 ret = iconv(data->ichandle, &p1, &data->indata, &p2, &len2);
1675 memmove(data->inbuf, p1, data->indata);
1676 /* Just a sanity check */
1677 if(((p2 - ((char *)data->cb)) % sizeof(wchar_t)) != 0)
1679 flog(LOG_CRIT, "Aiya! iconv does strange things to our wchar_t's!");
1682 data->cbdata += (((wchar_t *)p2) - porig);
1688 /* XXX: Should this really just ignore it? */
1696 /* Just a sanity check */
1697 if(data->cbsize != data->cbdata)
1699 flog(LOG_CRIT, "Aiya! iconv doesn't give us wchar_t's!");
1704 flog(LOG_WARNING, "bug: strange error from iconv in uiread: %s", strerror(errno));
1712 while(!done && (data->pp - data->cb < data->cbdata))
1717 if(iswspace(*data->pp))
1719 if(*data->pp == L'\r')
1721 if(data->pp == data->cb + data->cbdata - 1)
1726 if(*(++data->pp) == L'\n')
1728 if((data->argv != NULL) && (data->argv[0] != NULL))
1730 for(cur = commands; cur->handler != NULL; cur++)
1732 if(cur->name == NULL)
1734 if(!wcscasecmp(cur->name, data->argv[0]))
1736 queuecmd(data, cur, data->argc, data->argv);
1740 if(cur->handler == NULL)
1741 queuecmd(data, &commands[1], data->argc, data->argv);
1743 queuecmd(data, &commands[1], data->argc, data->argv);
1748 wmemmove(data->cb, data->pp, data->cbdata -= (data->pp - data->cb));
1749 data->pp = data->cb;
1762 if(iswspace(*data->pp))
1764 addtobuf(data->cw, L'\0');
1765 sizebuf(&data->argv, &data->args, data->argc + 1, sizeof(*data->argv), 1);
1766 data->argv[data->argc++] = data->cw;
1771 } else if(*data->pp == L'\"') {
1774 } else if(*data->pp == L'\\') {
1775 if(data->pp == data->cb + data->cbdata - 1)
1780 addtobuf(data->cw, *(++data->pp));
1783 addtobuf(data->cw, *(data->pp++));
1787 if(*data->pp == L'\"')
1790 } else if(*data->pp == L'\\') {
1791 if(data->pp == data->cb + data->cbdata - 1)
1796 addtobuf(data->cw, *(++(data->pp)));
1798 addtobuf(data->cw, *data->pp);
1806 static void uierror(struct socket *sk, int err, struct uidata *data)
1809 flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err));
1813 static void uiaccept(struct socket *sk, struct socket *newsk, void *data)
1815 struct uidata *uidata;
1817 newsk->data = uidata = newuidata(newsk);
1818 socksettos(newsk, confgetint("ui", "uitos"));
1821 newsk->errcb = (void (*)(struct socket *, int, void *))uierror;
1822 newsk->readcb = (void (*)(struct socket *, void *))uiread;
1823 queuecmd(uidata, &commands[0], 0, NULL);
1826 static int srcheta(struct search *srch, void *uudata)
1828 struct uidata *data;
1830 for(data = actives; data != NULL; data = data->next)
1832 if(haspriv(data, PERM_SRCH) && data->notify.b.srch && !wcscmp(srch->owner, data->username))
1833 newnotif(data, 620, NOTIF_ID, srch->id, NOTIF_INT, srch->eta - time(NULL), NOTIF_END);
1838 static int srchcommit(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, 621, NOTIF_ID, srch->id, NOTIF_END);
1850 static int srchres(struct search *srch, struct srchres *sr, 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))
1858 newnotif(data, 622, NOTIF_ID, srch->id, NOTIF_STR, sr->filename, NOTIF_STR, sr->fnet->name, NOTIF_STR, sr->peerid, NOTIF_OFF, sr->size,
1859 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);
1865 static int recvchat(struct fnetnode *fn, int public, wchar_t *name, wchar_t *peer, wchar_t *string, void *uudata)
1867 struct uidata *data;
1869 for(data = actives; data != NULL; data = data->next)
1871 if(haspriv(data, PERM_CHAT) && data->notify.b.fnchat)
1872 newnotif(data, 600, NOTIF_ID, fn->id, NOTIF_INT, public, NOTIF_STR, name, NOTIF_STR, peer, NOTIF_STR, string, NOTIF_END);
1877 static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
1879 struct uidata *data;
1880 struct notif *notif;
1882 if(!wcscmp(attrib, L"state"))
1884 for(data = actives; data != NULL; data = data->next)
1886 if(data->notify.b.fnact)
1887 newnotif(data, 601, NOTIF_ID, fn->id, NOTIF_INT, fn->state, NOTIF_END);
1889 } else if(!wcscmp(attrib, L"name")) {
1890 for(data = actives; data != NULL; data = data->next)
1892 if(data->notify.b.fnact)
1893 newnotif(data, 602, NOTIF_ID, fn->id, NOTIF_STR, fn->name, NOTIF_END);
1895 } else if(!wcscmp(attrib, L"numpeers")) {
1896 for(data = actives; data != NULL; data = data->next)
1898 if(data->notify.b.fnact)
1900 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
1901 notif->argv[1].d.n = fn->numpeers;
1903 newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
1910 static int fnunlink(struct fnetnode *fn, void *uudata)
1912 struct uidata *data;
1914 for(data = actives; data != NULL; data = data->next)
1916 if(data->notify.b.fnact)
1917 newnotif(data, 603, NOTIF_ID, fn->id, NOTIF_END);
1922 static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
1924 struct uidata *data;
1926 for(data = actives; data != NULL; data = data->next)
1928 if(data->notify.b.fnpeer)
1929 newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1934 static int peerdel(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, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
1946 static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
1948 struct uidata *data;
1949 struct notif *notif;
1952 for(data = actives; data != NULL; data = data->next)
1954 if(data->notify.b.fnpeer)
1956 for(notif = data->fnotif; notif != NULL; notif = notif->next)
1958 if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
1962 notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
1963 notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
1964 switch(di->datum->datatype)
1967 notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
1970 notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
1973 swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
1974 notifappend(notif, NOTIF_STR, buf, NOTIF_END);
1982 static int newfnetnode(struct fnetnode *fn, void *uudata)
1984 struct uidata *data;
1986 for(data = actives; data != NULL; data = data->next)
1988 if(data->notify.b.fnact)
1989 newnotif(data, 604, NOTIF_ID, fn->id, NOTIF_STR, fn->fnet->name, NOTIF_END);
1991 CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
1992 CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
1993 CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
1994 CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
1995 CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
1996 CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
2000 static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uudata)
2002 struct uidata *data;
2004 if(!wcscmp(attrib, L"state"))
2006 for(data = actives; data != NULL; data = data->next)
2008 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2009 newnotif(data, 611, NOTIF_ID, transfer->id, NOTIF_INT, transfer->state, NOTIF_END);
2011 } else if(!wcscmp(attrib, L"nick")) {
2012 for(data = actives; data != NULL; data = data->next)
2014 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2015 newnotif(data, 612, NOTIF_ID, transfer->id, NOTIF_STR, transfer->peernick, NOTIF_END);
2017 } else if(!wcscmp(attrib, L"size")) {
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, 613, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->size, NOTIF_END);
2023 } else if(!wcscmp(attrib, L"error")) {
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, 614, NOTIF_ID, transfer->id, NOTIF_INT, transfer->error, NOTIF_END);
2029 } else if(!wcscmp(attrib, L"path")) {
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, 616, NOTIF_ID, transfer->id, NOTIF_STR, transfer->path, NOTIF_END);
2035 } else if(!wcscmp(attrib, L"hash")) {
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, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
2045 static int transferprog(struct transfer *transfer, void *uudata)
2047 struct uidata *data;
2048 struct notif *notif;
2050 for(data = actives; data != NULL; data = data->next)
2052 if(haspriv(data, PERM_TRANS) && data->notify.b.trprog && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2054 if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 615, transfer->id)) != NULL)
2055 notif->argv[1].d.o = transfer->curpos;
2057 newnotif(data, 615, NOTIF_ID, transfer->id, NOTIF_OFF, transfer->curpos, NOTIF_END)->rlimit = 0.5;
2063 static int transferdestroyed(struct transfer *transfer, void *uudata)
2065 struct uidata *data;
2067 for(data = actives; data != NULL; data = data->next)
2069 if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
2070 newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
2075 static int newtransfernotify(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, 610, NOTIF_ID, transfer->id, NOTIF_INT, transfer->dir, NOTIF_STR, transfer->peerid, NOTIF_STR, (transfer->path == NULL)?L"":transfer->path, NOTIF_END);
2084 CBREG(transfer, trans_ac, transferchattr, NULL, NULL);
2085 CBREG(transfer, trans_p, transferprog, NULL, NULL);
2086 CBREG(transfer, trans_destroy, transferdestroyed, NULL, NULL);
2090 static struct uiuser *newuser(wchar_t *name, unsigned long perms)
2094 new = smalloc(sizeof(*new));
2096 new->name = swcsdup(name);
2107 static void freeuser(struct uiuser *user)
2109 if(user->next != NULL)
2110 user->next->prev = user->prev;
2111 if(user->prev != NULL)
2112 user->prev->next = user->next;
2119 static int conf_user(int argc, wchar_t **argv)
2121 int i, perms, permmod;
2122 struct uiuser *user;
2127 flog(LOG_WARNING, "not enough arguments given for user command");
2131 for(i = 2; i < argc; i++)
2133 if(!iswalpha(argv[i][0]))
2137 if(!wcscmp(p, L"disallow"))
2138 permmod = PERM_DISALLOW;
2139 if(!wcscmp(p, L"admin"))
2140 permmod = PERM_ADMIN;
2141 if(!wcscmp(p, L"fnetctl"))
2142 permmod = PERM_FNETCTL;
2143 if(!wcscmp(p, L"trans"))
2144 permmod = PERM_TRANS;
2145 if(!wcscmp(p, L"transcu"))
2146 permmod = PERM_TRANSCU;
2147 if(!wcscmp(p, L"chat"))
2148 permmod = PERM_CHAT;
2149 if(!wcscmp(p, L"srch"))
2150 permmod = PERM_SRCH;
2151 if(!wcscmp(p, L"all"))
2153 if(argv[i][0] == L'-')
2158 if((user = finduser(argv[1])) == NULL)
2160 newuser(argv[1], perms);
2163 user->perms = perms;
2168 static void preinit(int hup)
2170 struct uiuser *user;
2174 newuser(L"default", PERM_DISALLOW);
2176 for(user = users; user != NULL; user = user->next)
2178 if(!wcscmp(user->name, L"default"))
2184 static struct sockaddr_un *makeunixname(void)
2186 static struct sockaddr_un buf;
2191 memset(&buf, 0, sizeof(buf));
2192 buf.sun_family = PF_UNIX;
2193 if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) {
2194 flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno));
2197 if(!strcmp(val, "none"))
2199 if(!strcmp(val, "default"))
2201 if((uid = getuid()) == 0)
2203 strcpy(buf.sun_path, "/var/run/doldacond.sock");
2206 if((pwd = getpwuid(uid)) == NULL)
2208 flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno));
2211 strcpy(buf.sun_path, "/tmp/doldacond-");
2212 strcat(buf.sun_path, pwd->pw_name);
2216 if(strchr(val, '/'))
2218 strcpy(buf.sun_path, val);
2221 flog(LOG_WARNING, "invalid Unix socket name: %s", val);
2225 static int tcpportupdate(struct configvar *var, void *uudata)
2227 struct socket *newsock;
2230 if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL))
2232 flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno));
2235 if(tcpsocket != NULL)
2240 tcpsocket = newsock;
2244 static int unixsockupdate(struct configvar *var, void *uudata)
2246 struct socket *newsock;
2247 struct sockaddr_un *un;
2252 if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2255 flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno));
2259 if(unixsocket != NULL)
2261 putsock(unixsocket);
2264 unixsocket = newsock;
2268 static int init(int hup)
2270 struct uiuser *user, *next;
2271 struct sockaddr_un *un;
2278 for(user = users; user != NULL; user = next)
2287 starttime = time(NULL);
2288 if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL))
2290 flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno));
2293 CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL);
2295 if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL))
2298 flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno));
2302 CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL);
2303 GCBREG(newfncb, newfnetnode, NULL);
2304 GCBREG(newtransfercb, newtransfernotify, NULL);
2308 for(user = users; user != NULL; user = user->next)
2310 if(wcscmp(user->name, L"default"))
2315 if((pwd = getpwuid(getuid())) == NULL)
2317 flog(LOG_CRIT, "could not get login info: %s", strerror(errno));
2320 if((wcsname = icmbstowcs(pwd->pw_name, NULL)) == NULL)
2322 flog(LOG_CRIT, "could not convert user name into wcs: %s", strerror(errno));
2325 newuser(wcsname, ~PERM_DISALLOW);
2332 static int run(void)
2335 struct uidata *data, *next;
2336 struct qcommand *qcmd;
2337 struct notif *notif, *nnotif;
2339 for(data = actives; data != NULL; data = next)
2345 for(data = actives; data != NULL; data = data->next)
2347 for(notif = data->fnotif; notif != NULL; notif = nnotif)
2349 nnotif = notif->next;
2350 if(notif->state == NOTIF_WAIT)
2353 for(i = 0; i < notif->argc; i++)
2355 if(notif->argv[i].dt == NOTIF_ID)
2357 id = notif->argv[i].d.n;
2361 if(findnotif(notif->prev, 0, -1, notif->code, id) != NULL)
2363 sq(data->sk, 2, L"%i", notif->code, NULL);
2364 for(i = 0; i < notif->argc; i++)
2366 switch(notif->argv[i].dt)
2370 sq(data->sk, 2, L"%i", notif->argv[i].d.n, NULL);
2373 sq(data->sk, 2, L"%oi", notif->argv[i].d.o, NULL);
2376 if(notif->argv[i].d.s[0] == L'%')
2377 sq(data->sk, 2, L"%ls", notif->argv[i].d.s, NULL);
2379 sq(data->sk, 2, notif->argv[i].d.s, NULL);
2382 sq(data->sk, 2, L"%f", notif->argv[i].d.d, NULL);
2386 sq(data->sk, 0, NULL);
2387 if(notif->rlimit != 0)
2389 notif->state = NOTIF_WAIT;
2390 notif->exptimer = timercallback(ntime() + notif->rlimit, (void (*)(int, void *))notifexpire, notif);
2395 if((qcmd = unlinkqcmd(data)) != NULL)
2397 qcmd->cmd->handler(data->sk, data, qcmd->argc, qcmd->argv);
2405 static void terminate(void)
2407 while(users != NULL)
2409 if(tcpsocket != NULL)
2411 if(unixsocket != NULL)
2412 putsock(unixsocket);
2415 static struct configvar myvars[] =
2417 /** If true, UI connections will only be accepted from localhost
2418 * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are
2419 * completely sure that you know what you are doing, never turn
2420 * this off when auth.authless is on. */
2421 {CONF_VAR_BOOL, "onlylocal", {.num = 1}},
2422 /** The TCP port number on which to accept UI client connections,
2423 * or -1 to not listen on TCP. */
2424 {CONF_VAR_INT, "port", {.num = 1500}},
2426 * Controls the the name to use for the Unix socket on which to
2427 * accept UI client connections. If the name contains a slash, it
2428 * is treated as a file name to bind on. If the name is "default",
2429 * the file name will be "/var/run/doldacond.sock" if doldacond
2430 * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where
2431 * NAME is the user name of the UID which doldacond runs as. If
2432 * the name is "none", no Unix socket will be used. Otherwise, an
2433 * error is signaled.
2435 {CONF_VAR_STRING, "unixsock", {.str = L"default"}},
2436 /** The TOS value to use for UI connections (see the TOS VALUES
2438 {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}},
2439 /** The name of the filtercmd script (see the FILES section for
2440 * lookup information). */
2441 {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}},
2445 static struct configcmd mycmds[] =
2447 {"user", conf_user},
2451 static struct module me =
2462 .terminate = terminate