X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=daemon%2Fui.c;h=d77efa79146b67eac62588d857e30e0127b2eff7;hb=9e5f2b29cf819c9f79113bf3ae7edcb484d8ee14;hp=9c89b504de0699380daf431d0518e4f813cd4253;hpb=f5dbbe62975d51f5096e1b38c3d982e6af093d8b;p=doldaconnect.git diff --git a/daemon/ui.c b/daemon/ui.c index 9c89b50..d77efa7 100644 --- a/daemon/ui.c +++ b/daemon/ui.c @@ -19,11 +19,11 @@ #define _GNU_SOURCE #include #include -#include #include #include #include #include +#include #include #include #include @@ -159,8 +159,6 @@ struct uidata size_t cwsize, cwdata; }; -static int uiread(struct socket *sk, struct uidata *data); -static int uierror(struct socket *sk, int err, struct uidata *data); static int srcheta(struct search *srch, void *uudata); static int srchcommit(struct search *srch, void *uudata); static int srchres(struct search *srch, struct srchres *sr, void *uudata); @@ -169,7 +167,8 @@ static void notifappend(struct notif *notif, ...); struct uiuser *users = NULL; struct uidata *actives = NULL; -struct socket *uisocket = NULL; +struct socket *tcpsocket = NULL; +struct socket *unixsocket = NULL; static time_t starttime; static wchar_t *quoteword(wchar_t *word) @@ -341,6 +340,9 @@ static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_ if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback))) valid = 1; break; + case AF_UNIX: + valid = 1; + break; default: valid = 0; break; @@ -693,7 +695,7 @@ static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_ } else { for(peer = fn->peers; peer != NULL; peer = peer->next) { - sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%%s", peer->id, L"%%s", peer->nick, NULL); + sq(sk, 2 | ((peer->next != NULL)?1:0), L"200", L"%%ls", peer->id, L"%%ls", peer->nick, NULL); for(i = 0; i < peer->dinum; i++) { if(peer->peerdi[i].datum->datatype == FNPD_INT) @@ -706,7 +708,7 @@ static void cmd_lspeers(struct socket *sk, struct uidata *data, int argc, wchar_ sq(sk, 2, peer->peerdi[i].datum->id, buf, NULL); } if((peer->peerdi[i].datum->datatype == FNPD_STR) && (peer->peerdi[i].data.str != NULL)) - sq(sk, 2, peer->peerdi[i].datum->id, L"%%s", peer->peerdi[i].data.str, NULL); + sq(sk, 2, peer->peerdi[i].datum->id, L"%%ls", peer->peerdi[i].data.str, NULL); } sq(sk, 0, NULL); } @@ -1056,19 +1058,18 @@ static void cmd_cansrch(struct socket *sk, struct uidata *data, int argc, wchar_ sq(sk, 0, L"200", L"Search cancelled", NULL); } -static int fcmdread(struct socket *sk, struct uidata *data) +static void fcmdread(struct socket *sk, struct uidata *data) { char *buf; size_t bufsize; if((buf = sockgetinbuf(sk, &bufsize)) == NULL) - return(0); + return; bufcat(data->fcmdbuf, buf, bufsize); free(buf); - return(0); } -static int fcmderr(struct socket *sk, int err, struct uidata *data) +static void fcmderr(struct socket *sk, int err, struct uidata *data) { wchar_t *wbuf, *p, *p2; @@ -1085,7 +1086,7 @@ static int fcmderr(struct socket *sk, int err, struct uidata *data) } data->fcmdbufsize = data->fcmdbufdata = 0; sq(data->sk, 0, L"505", L"An error occurred on the pipe to the filtercmd", L"%%s", strerror(err), NULL); - return(0); + return; } putsock(data->fcmdsk); data->fcmdsk = NULL; @@ -1103,7 +1104,7 @@ static int fcmderr(struct socket *sk, int err, struct uidata *data) if(wbuf == NULL) { sq(data->sk, 0, L"504", L"Filtercmd sent data which could not be converted from the local charset", NULL); - return(0); + return; } p = wbuf; for(p2 = wcschr(p, L'\n'); p2 != NULL; p2 = wcschr(p, L'\n')) @@ -1120,7 +1121,6 @@ static int fcmderr(struct socket *sk, int err, struct uidata *data) sq(data->sk, 0, L"200", L"%%ls", p, NULL); } free(wbuf); - return(0); } static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) @@ -1141,7 +1141,10 @@ static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wcha sq(sk, 0, L"505", L"System error - Could not fork session", "Internal error", NULL); return; } - if((filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), "dcdl-filtercmd", pwent->pw_dir, 0)) == NULL) + filtercmd = findfile(icswcstombs(confgetstr("ui", "filtercmd"), NULL, NULL), NULL, 0); + if(filtercmd == NULL) + filtercmd = findfile("dc-filtercmd", pwent->pw_dir, 0); + if(filtercmd == NULL) { flog(LOG_WARNING, "could not find filtercmd executable for user %s", pwent->pw_name); sq(sk, 0, L"505", L"System error - Could not fork session", L"Could not find filtercmd executable", NULL); @@ -1186,8 +1189,9 @@ static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wcha data->fcmdbuf = NULL; } data->fcmdbufsize = data->fcmdbufdata = 0; - CBREG(data->fcmdsk, socket_read, (int (*)(struct socket *, void *))fcmdread, NULL, data); - CBREG(data->fcmdsk, socket_err, (int (*)(struct socket *, int, void *))fcmderr, NULL, data); + data->fcmdsk->data = data; + data->fcmdsk->readcb = (void (*)(struct socket *, void *))fcmdread; + data->fcmdsk->errcb = (void (*)(struct socket *, int, void *))fcmderr; } static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv) @@ -1241,7 +1245,7 @@ static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wc havepriv(PERM_TRANS); buf1 = swprintf2(L"%lli", bytesdownload); buf2 = swprintf2(L"%lli", bytesupload); - sq(sk, 0, L"200", L"%%ls", buf1, L"%%ls", buf2, NULL); + sq(sk, 0, L"200", L"down", L"%%ls", buf1, L"up", L"%%ls", buf2, NULL); free(buf1); free(buf2); } @@ -1514,8 +1518,8 @@ static void freeuidata(struct uidata *data) data->prev->next = data->next; if(data == actives) actives = data->next; - CBUNREG(data->sk, socket_read, uiread, data); - CBUNREG(data->sk, socket_err, uierror, data); + data->sk->readcb = NULL; + data->sk->errcb = NULL; putsock(data->sk); while((qcmd = unlinkqcmd(data)) != NULL) freequeuecmd(qcmd); @@ -1593,7 +1597,7 @@ static struct uidata *newuidata(struct socket *sk) return(data); } -static int uiread(struct socket *sk, struct uidata *data) +static void uiread(struct socket *sk, struct uidata *data) { int ret, done; char *newbuf; @@ -1605,7 +1609,7 @@ static int uiread(struct socket *sk, struct uidata *data) if(data->indata > 1024) data->indata = 0; if((newbuf = sockgetinbuf(sk, &datalen)) == NULL) - return(0); + return; sizebuf(&data->inbuf, &data->inbufsize, data->indata + datalen, 1, 1); memcpy(data->inbuf + data->indata, newbuf, datalen); free(newbuf); @@ -1758,29 +1762,26 @@ static int uiread(struct socket *sk, struct uidata *data) break; } } - return(0); } -static int uierror(struct socket *sk, int err, struct uidata *data) +static void uierror(struct socket *sk, int err, struct uidata *data) { if(err) flog(LOG_WARNING, "error occurred on UI socket: %s", strerror(err)); freeuidata(data); - return(0); } -static int uiaccept(struct socket *sk, struct socket *newsk, void *data) +static void uiaccept(struct socket *sk, struct socket *newsk, void *data) { struct uidata *uidata; - uidata = newuidata(newsk); + newsk->data = uidata = newuidata(newsk); socksettos(newsk, confgetint("ui", "uitos")); if(uidata == NULL) - return(0); - CBREG(newsk, socket_err, (int (*)(struct socket *, int, void *))uierror, NULL, uidata); - CBREG(newsk, socket_read, (int (*)(struct socket *, void *))uiread, NULL, uidata); + return; + newsk->errcb = (void (*)(struct socket *, int, void *))uierror; + newsk->readcb = (void (*)(struct socket *, void *))uiread; queuecmd(uidata, &commands[0], 0, NULL); - return(0); } static int srcheta(struct search *srch, void *uudata) @@ -2141,24 +2142,90 @@ static void preinit(int hup) } } -static int portupdate(struct configvar *var, void *uudata) +static struct sockaddr_un *makeunixname(void) +{ + static struct sockaddr_un buf; + char *val; + struct passwd *pwd; + uid_t uid; + + memset(&buf, 0, sizeof(buf)); + buf.sun_family = PF_UNIX; + if((val = icswcstombs(confgetstr("ui", "unixsock"), NULL, NULL)) == NULL) { + flog(LOG_WARNING, "could not map Unix socket name into local charset: %s", strerror(errno)); + return(NULL); + } + if(!strcmp(val, "none")) + return(NULL); + if(!strcmp(val, "default")) + { + if((uid = getuid()) == 0) + { + strcpy(buf.sun_path, "/var/run/doldacond.sock"); + return(&buf); + } else { + if((pwd = getpwuid(uid)) == NULL) + { + flog(LOG_ERR, "could not get passwd entry for current user: %s", strerror(errno)); + return(NULL); + } + strcpy(buf.sun_path, "/tmp/doldacond-"); + strcat(buf.sun_path, pwd->pw_name); + return(&buf); + } + } + if(strchr(val, '/')) + { + strcpy(buf.sun_path, val); + return(&buf); + } + flog(LOG_WARNING, "invalid Unix socket name: %s", val); + return(NULL); +} + +static int tcpportupdate(struct configvar *var, void *uudata) { struct socket *newsock; - if((uisocket = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL) + newsock = NULL; + if((var->val.num != -1) && ((newsock = netcstcplisten(var->val.num, 1, uiaccept, NULL)) == NULL)) { - flog(LOG_WARNING, "could not create new UI socket, reverting to old: %s", strerror(errno)); + flog(LOG_WARNING, "could not create new TCP UI socket, reverting to old: %s", strerror(errno)); return(0); } - if(uisocket != NULL) - putsock(uisocket); - uisocket = newsock; + if(tcpsocket != NULL) + { + putsock(tcpsocket); + tcpsocket = NULL; + } + tcpsocket = newsock; + return(0); +} + +static int unixsockupdate(struct configvar *var, void *uudata) +{ + struct socket *newsock; + struct sockaddr_un *un; + + newsock = NULL; + if(((un = makeunixname()) != NULL) && ((newsock = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) + { + flog(LOG_WARNING, "could not create new Unix UI socket, reverting to old: %s", strerror(errno)); + return(0); + } + if(unixsocket != NULL) + { + putsock(unixsocket); + unixsocket = NULL; + } + unixsocket = newsock; return(0); } static int init(int hup) { struct uiuser *user, *next; + struct sockaddr_un *un; if(hup) { @@ -2172,14 +2239,18 @@ static int init(int hup) if(!hup) { starttime = time(NULL); - if(uisocket != NULL) - putsock(uisocket); - if((uisocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL) + if((confgetint("ui", "port") != -1) && ((tcpsocket = netcstcplisten(confgetint("ui", "port"), 1, uiaccept, NULL)) == NULL)) + { + flog(LOG_CRIT, "could not create TCP UI socket: %s", strerror(errno)); + return(1); + } + CBREG(confgetvar("ui", "port"), conf_update, tcpportupdate, NULL, NULL); + if(((un = makeunixname()) != NULL) && ((unixsocket = netcslistenlocal(SOCK_STREAM, (struct sockaddr *)un, sizeof(*un), uiaccept, NULL)) == NULL)) { - flog(LOG_CRIT, "could not create UI socket: %s", strerror(errno)); + flog(LOG_CRIT, "could not create Unix UI socket: %s", strerror(errno)); return(1); } - CBREG(confgetvar("ui", "port"), conf_update, portupdate, NULL, NULL); + CBREG(confgetvar("ui", "unixsock"), conf_update, unixsockupdate, NULL, NULL); GCBREG(newfncb, newfnetnode, NULL); GCBREG(newtransfercb, newtransfernotify, NULL); } @@ -2262,13 +2333,38 @@ static void terminate(void) { while(users != NULL) freeuser(users); + if(tcpsocket != NULL) + putsock(tcpsocket); + if(unixsocket != NULL) + putsock(unixsocket); } static struct configvar myvars[] = { + /** If true, UI connections will only be accepted from localhost + * addresses (127.0.0.1, ::1 or ::ffff:127.0.0.1). Unless you are + * completely sure that you know what you are doing, never turn + * this off when auth.authless is on. */ {CONF_VAR_BOOL, "onlylocal", {.num = 1}}, + /** The TCP port number on which to accept UI client connections, + * or -1 to not listen on TCP. */ {CONF_VAR_INT, "port", {.num = 1500}}, + /** + * Controls the the name to use for the Unix socket on which to + * accept UI client connections. If the name contains a slash, it + * is treated as a file name to bind on. If the name is "default", + * the file name will be "/var/run/doldacond.sock" if doldacond + * runs with UID == 0, or "/tmp/doldacond-NAME" otherwise, where + * NAME is the user name of the UID which doldacond runs as. If + * the name is "none", no Unix socket will be used. Otherwise, an + * error is signaled. + */ + {CONF_VAR_STRING, "unixsock", {.str = L"default"}}, + /** The TOS value to use for UI connections (see the TOS VALUES + * section). */ {CONF_VAR_INT, "uitos", {.num = SOCK_TOS_MINDELAY}}, + /** The name of the filtercmd script (see the FILES section for + * lookup information). */ {CONF_VAR_STRING, "filtercmd", {.str = L"dc-filtercmd"}}, {CONF_VAR_END} };