Enable filter to pass an exit status.
[doldaconnect.git] / daemon / ui.c
index 50f20cf..d2bae9c 100644 (file)
@@ -23,6 +23,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <netinet/ip6.h>
+#include <arpa/inet.h>
 #include <errno.h>
 #include <string.h>
 #include <stdarg.h>
@@ -123,6 +124,7 @@ struct uidata
        {
            int fnact:1;
            int fnchat:1;
+           int fnpeer:1;
            int tract:1;
            int trprog:1;
            int srch:1;
@@ -314,6 +316,7 @@ static int haspriv(struct uidata *data, int perm)
 static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
 {
     int valid;
+    struct in6_addr mv4lo;
     
     if(confgetint("ui", "onlylocal"))
     {
@@ -323,7 +326,12 @@ static void cmd_connect(struct socket *sk, struct uidata *data, int argc, wchar_
            valid = ((struct sockaddr_in *)sk->remote)->sin_addr.s_addr == INADDR_LOOPBACK;
            break;
        case AF_INET6:
-           valid = !memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback));
+           inet_pton(AF_INET6, "::ffff:127.0.0.1", &mv4lo);
+           valid = 0;
+           if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &in6addr_loopback, sizeof(in6addr_loopback)))
+               valid = 1;
+           if(!memcmp(&((struct sockaddr_in6 *)sk->remote)->sin6_addr, &mv4lo, sizeof(in6addr_loopback)))
+               valid = 1;
            break;
        default:
            valid = 0;
@@ -429,19 +437,20 @@ static void cmd_login(struct socket *sk, struct uidata *data, int argc, wchar_t
        if(data->uid == -1)
        {
            sq(sk, 0, L"506", L"Authentication error", NULL);
-           flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
+           flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
            logout(data);
        } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
            sq(sk, 0, L"506", L"Authentication error", NULL);
-           flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
+           flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
            logout(data);
        } else {
            sq(sk, 0, L"200", L"Welcome", NULL);
-           flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
+           flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
        }
        break;
     case AUTH_DENIED:
        sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
+       flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
        logout(data);
        break;
     case AUTH_PASS:
@@ -502,19 +511,20 @@ static void cmd_pass(struct socket *sk, struct uidata *data, int argc, wchar_t *
        if(data->uid == -1)
        {
            sq(sk, 0, L"506", L"Authentication error", NULL);
-           flog(LOG_INFO, "user %ls authenticated successfully, but no account existed", data->username);
+           flog(LOG_INFO, "user %ls authenticated successfully from %s, but no account existed", data->username, formataddress(sk->remote, sk->remotelen));
            logout(data);
        } else if((data->userinfo == NULL) || (data->userinfo->perms & PERM_DISALLOW)) {
            sq(sk, 0, L"506", L"Authentication error", NULL);
-           flog(LOG_INFO, "user %ls authenticated successfully, but was not authorized", data->username);
+           flog(LOG_INFO, "user %ls authenticated successfully from %s, but was not authorized", data->username, formataddress(sk->remote, sk->remotelen));
            logout(data);
        } else {
            sq(sk, 0, L"200", L"Welcome", NULL);
-           flog(LOG_INFO, "%ls (UID %i) logged in", data->username, data->uid);
+           flog(LOG_INFO, "%ls (UID %i) logged in from %s", data->username, data->uid, formataddress(sk->remote, sk->remotelen));
        }
        break;
     case AUTH_DENIED:
        sq(sk, 0, L"506", L"Authentication error", L"%%ls", (data->auth->text == NULL)?L"":(data->auth->text), NULL);
+       flog(LOG_INFO, "authentication failed for %ls from %s", data->username, formataddress(sk->remote, sk->remotelen));
        logout(data);
        break;
     case AUTH_PASS:
@@ -557,6 +567,7 @@ static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wc
     char *buf;
     int err;
     struct fnetnode *fn;
+    struct wcspair *args;
     
     haveargs(3);
     havepriv(PERM_FNETCTL);
@@ -565,7 +576,10 @@ static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wc
        sq(sk, 0, L"504", L"Could not convert data to locale charset", NULL);
        return;
     }
-    fn = fnetinitconnect(argv[1], buf);
+    args = NULL;
+    for(i = 3; i < argc - 1; i += 2)
+       newwcspair(argv[i], argv[i + 1], &args);
+    fn = fnetinitconnect(argv[1], buf, args);
     err = errno;
     free(buf);
     if(fn == NULL)
@@ -576,11 +590,6 @@ static void cmd_fnetconnect(struct socket *sk, struct uidata *data, int argc, wc
            sq(sk, 0, L"509", L"Could not parse the address", L"%%s", strerror(err), NULL);
        return;
     }
-    for(i = 3; i < argc - 1; i += 2)
-    {
-       if(!wcscmp(argv[i], L"nick"))
-           fnetsetnick(fn, argv[i + 1]);
-    }
     linkfnetnode(fn);
     fnetsetname(fn, argv[2]);
     putfnetnode(fn);
@@ -598,7 +607,7 @@ static void cmd_lsnodes(struct socket *sk, struct uidata *data, int argc, wchar_
     }
     for(fn = fnetnodes; fn != NULL; fn = fn->next)
     {
-       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, NULL);
+       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);
     }
 }
 
@@ -744,7 +753,7 @@ static void cmd_download(struct socket *sk, struct uidata *data, int argc, wchar
            {
                transfersethash(transfer, parsehash(argv[i + 1]));
            } else {
-               transferaddarg(transfer, argv[i], argv[i + 1]);
+               newwcspair(argv[i], argv[i + 1], &transfer->args);
            }
        }
     }
@@ -835,6 +844,8 @@ static void cmd_notify(struct socket *sk, struct uidata *data, int argc, wchar_t
            data->notify.b.fnchat = val;
        } else if(!wcscasecmp(argv[i], L"fn:act")) {
            data->notify.b.fnact = val;
+       } else if(!wcscasecmp(argv[i], L"fn:peer")) {
+           data->notify.b.fnpeer = val;
        } else if(!wcscasecmp(argv[i], L"trans:act")) {
            data->notify.b.tract = val;
        } else if(!wcscasecmp(argv[i], L"trans:prog")) {
@@ -1161,7 +1172,7 @@ static void cmd_filtercmd(struct socket *sk, struct uidata *data, int argc, wcha
 static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
 {
     struct transfer *transfer;
-    struct transarg *ta;
+    struct wcspair *ta;
     
     haveargs(2);
     havepriv(PERM_TRANS);
@@ -1180,7 +1191,7 @@ static void cmd_lstrarg(struct socket *sk, struct uidata *data, int argc, wchar_
        sq(sk, 0, L"201", L"Transfer has no arguments", NULL);
     } else {
        for(ta = transfer->args; ta != NULL; ta = ta->next)
-           sq(sk, ta->next != NULL, L"200", L"%%ls", ta->rec, L"%%ls", ta->val, NULL);
+           sq(sk, ta->next != NULL, L"200", L"%%ls", ta->key, L"%%ls", ta->val, NULL);
     }
 }
 
@@ -1202,6 +1213,18 @@ static void cmd_hashstatus(struct socket *sk, struct uidata *data, int argc, wch
     sq(sk, 0, L"200", L"%%i", total, L"tth", L"%%i", hashed, NULL);
 }
 
+static void cmd_transstatus(struct socket *sk, struct uidata *data, int argc, wchar_t **argv)
+{
+    wchar_t *buf1, *buf2;
+    
+    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);
+    free(buf1);
+    free(buf2);
+}
+
 #undef haveargs
 #undef havepriv
 
@@ -1237,6 +1260,7 @@ static struct command commands[] =
     {L"filtercmd", cmd_filtercmd},
     {L"lstrarg", cmd_lstrarg},
     {L"hashstatus", cmd_hashstatus},
+    {L"transstatus", cmd_transstatus},
     {NULL, NULL}
 };
 
@@ -1267,18 +1291,10 @@ static struct qcommand *unlinkqcmd(struct uidata *data)
     return(qcmd);
 }
 
-static struct notif *newnotif(struct uidata *data, int code, ...)
+static void notifappendv(struct notif *notif, va_list args)
 {
-    struct notif *notif;
-    va_list args;
     int dt, ca;
     
-    notif = smalloc(sizeof(*notif));
-    memset(notif, 0, sizeof(*notif));
-    notif->rlimit = 0.0;
-    notif->ui = data;
-    notif->code = code;
-    va_start(args, code);
     while((dt = va_arg(args, int)) != NOTIF_END)
     {
        ca = notif->argc;
@@ -1298,6 +1314,29 @@ static struct notif *newnotif(struct uidata *data, int code, ...)
            break;
        }
     }
+}
+
+static void notifappend(struct notif *notif, ...)
+{
+    va_list args;
+    
+    va_start(args, notif);
+    notifappendv(notif, args);
+    va_end(args);
+}
+
+static struct notif *newnotif(struct uidata *data, int code, ...)
+{
+    struct notif *notif;
+    va_list args;
+    
+    notif = smalloc(sizeof(*notif));
+    memset(notif, 0, sizeof(*notif));
+    notif->rlimit = 0.0;
+    notif->ui = data;
+    notif->code = code;
+    va_start(args, code);
+    notifappendv(notif, args);
     va_end(args);
     notif->next = NULL;
     notif->prev = data->lnotif;
@@ -1718,7 +1757,7 @@ static int fnactive(struct fnetnode *fn, wchar_t *attrib, void *uudata)
                if((notif = findnotif(data->fnotif, 1, NOTIF_PEND, 605, fn->id)) != NULL)
                    notif->argv[1].d.n = fn->numpeers;
                else
-                   newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END);
+                   newnotif(data, 605, NOTIF_ID, fn->id, NOTIF_INT, fn->numpeers, NOTIF_END)->rlimit = 0.5;
            }
        }
     }
@@ -1737,6 +1776,66 @@ static int fnunlink(struct fnetnode *fn, void *uudata)
     return(0);
 }
 
+static int peernew(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
+{
+    struct uidata *data;
+    
+    for(data = actives; data != NULL; data = data->next)
+    {
+       if(data->notify.b.fnpeer)
+           newnotif(data, 630, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
+    }
+    return(0);
+}
+
+static int peerdel(struct fnetnode *fn, struct fnetpeer *peer, void *uudata)
+{
+    struct uidata *data;
+    
+    for(data = actives; data != NULL; data = data->next)
+    {
+       if(data->notify.b.fnpeer)
+           newnotif(data, 631, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_END);
+    }
+    return(0);
+}
+
+static int peerchange(struct fnetnode *fn, struct fnetpeer *peer, struct fnetpeerdi *di, void *uudata)
+{
+    struct uidata *data;
+    struct notif *notif;
+    wchar_t buf[32];
+    
+    for(data = actives; data != NULL; data = data->next)
+    {
+       if(data->notify.b.fnpeer)
+       {
+           for(notif = data->fnotif; notif != NULL; notif = notif->next)
+           {
+               if((notif->code == 632) && (notif->state == NOTIF_PEND) && (notif->argv[0].d.n == fn->id) && !wcscmp(notif->argv[1].d.s, peer->id))
+                   break;
+           }
+           if(notif == NULL)
+               notif = newnotif(data, 632, NOTIF_INT, fn->id, NOTIF_STR, peer->id, NOTIF_STR, peer->nick, NOTIF_END);
+           notifappend(notif, NOTIF_STR, di->datum->id, NOTIF_INT, di->datum->datatype, NOTIF_END);
+           switch(di->datum->datatype)
+           {
+           case FNPD_INT:
+               notifappend(notif, NOTIF_INT, di->data.num, NOTIF_END);
+               break;
+           case FNPD_STR:
+               notifappend(notif, NOTIF_STR, di->data.str, NOTIF_END);
+               break;
+           case FNPD_LL:
+               swprintf(buf, sizeof(buf) / sizeof(*buf), L"%lli", di->data.lnum);
+               notifappend(notif, NOTIF_STR, buf, NOTIF_END);
+               break;
+           }
+       }
+    }
+    return(0);
+}
+
 static int newfnetnode(struct fnetnode *fn, void *uudata)
 {
     struct uidata *data;
@@ -1749,6 +1848,9 @@ static int newfnetnode(struct fnetnode *fn, void *uudata)
     CBREG(fn, fnetnode_ac, fnactive, NULL, NULL);
     CBREG(fn, fnetnode_chat, recvchat, NULL, NULL);
     CBREG(fn, fnetnode_unlink, fnunlink, NULL, NULL);
+    CBREG(fn, fnetpeer_new, peernew, NULL, NULL);
+    CBREG(fn, fnetpeer_del, peerdel, NULL, NULL);
+    CBREG(fn, fnetpeer_chdi, peerchange, NULL, NULL);
     return(0);
 }
 
@@ -1791,7 +1893,7 @@ static int transferchattr(struct transfer *transfer, wchar_t *attrib, void *uuda
        for(data = actives; data != NULL; data = data->next)
        {
            if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
-               newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
+               newnotif(data, 618, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->hash == NULL)?L"":unparsehash(transfer->hash), NOTIF_END);
        }
     }
     return(0);
@@ -1822,7 +1924,7 @@ static int transferdestroyed(struct transfer *transfer, void *uudata)
     for(data = actives; data != NULL; data = data->next)
     {
        if(haspriv(data, PERM_TRANS) && data->notify.b.tract && ((transfer->owner == 0) || (transfer->owner == data->uid)))
-           newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_END);
+           newnotif(data, 617, NOTIF_ID, transfer->id, NOTIF_STR, (transfer->exitstatus == NULL)?L"":(transfer->exitstatus), NOTIF_END);
     }
     return(0);
 }