Merge branch 'master' of git.dolda2000.com:/srv/git/r/ashd
authorFredrik Tolf <fredrik@dolda2000.com>
Fri, 25 Apr 2014 15:33:22 +0000 (17:33 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Fri, 25 Apr 2014 15:33:22 +0000 (17:33 +0200)
src/plaintcp.c
src/ssl-gnutls.c

index 7d36a1d..6524955 100644 (file)
@@ -168,20 +168,28 @@ void servetcp(struct muth *muth, va_list args)
 static void listenloop(struct muth *muth, va_list args)
 {
     vavar(struct tcpport *, tcp);
-    int i, ns;
+    int i, ns, n;
     struct sockaddr_storage name;
     socklen_t namelen;
     
+    fcntl(tcp->fd, F_SETFL, fcntl(tcp->fd, F_GETFL) | O_NONBLOCK);
     while(1) {
        namelen = sizeof(name);
        if(block(tcp->fd, EV_READ, 0) == 0)
            goto out;
-       ns = accept(tcp->fd, (struct sockaddr *)&name, &namelen);
-       if(ns < 0) {
-           flog(LOG_ERR, "accept: %s", strerror(errno));
-           goto out;
+       n = 0;
+       while(1) {
+           ns = accept(tcp->fd, (struct sockaddr *)&name, &namelen);
+           if(ns < 0) {
+               if(errno == EAGAIN)
+                   break;
+               flog(LOG_ERR, "accept: %s", strerror(errno));
+               goto out;
+           }
+           mustart(servetcp, ns, name, tcp);
+           if(++n >= 100)
+               break;
        }
-       mustart(servetcp, ns, name, tcp);
     }
     
 out:
index 0d4dd22..459556e 100644 (file)
@@ -56,6 +56,7 @@ struct sslport {
     int fd;
     int sport;
     gnutls_certificate_credentials_t creds;
+    gnutls_priority_t ciphers;
     struct namedcreds **ncreds;
 };
 
@@ -325,7 +326,7 @@ static void servessl(struct muth *muth, va_list args)
     numconn++;
     fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
     gnutls_init(&sess, GNUTLS_SERVER);
-    gnutls_set_default_priority(sess);
+    gnutls_priority_set(sess, pd->ciphers);
     gnutls_db_set_retrieve_function(sess, sessdbfetch);
     gnutls_db_set_store_function(sess, sessdbstore);
     gnutls_db_set_remove_function(sess, sessdbdel);
@@ -359,20 +360,28 @@ out:
 static void listenloop(struct muth *muth, va_list args)
 {
     vavar(struct sslport *, pd);
-    int i, ns;
+    int i, ns, n;
     struct sockaddr_storage name;
     socklen_t namelen;
     
+    fcntl(pd->fd, F_SETFL, fcntl(pd->fd, F_GETFL) | O_NONBLOCK);
     while(1) {
        namelen = sizeof(name);
        if(block(pd->fd, EV_READ, 0) == 0)
            goto out;
-       ns = accept(pd->fd, (struct sockaddr *)&name, &namelen);
-       if(ns < 0) {
-           flog(LOG_ERR, "accept: %s", strerror(errno));
-           goto out;
+       n = 0;
+       while(1) {
+           ns = accept(pd->fd, (struct sockaddr *)&name, &namelen);
+           if(ns < 0) {
+               if(errno == EAGAIN)
+                   break;
+               flog(LOG_ERR, "accept: %s", strerror(errno));
+               goto out;
+           }
+           mustart(servessl, ns, name, pd);
+           if(++n >= 100)
+               break;
        }
-       mustart(servessl, ns, name, pd);
     }
     
 out:
@@ -512,15 +521,17 @@ void handlegnussl(int argc, char **argp, char **argv)
 {
     int i, ret, port, fd;
     gnutls_certificate_credentials_t creds;
+    gnutls_priority_t ciphers;
     struct ncredbuf ncreds;
     struct sslport *pd;
-    char *crtfile, *keyfile;
+    char *crtfile, *keyfile, *perr;
     
     init();
     port = 443;
     bufinit(ncreds);
     gnutls_certificate_allocate_credentials(&creds);
     keyfile = crtfile = NULL;
+    ciphers = NULL;
     for(i = 0; i < argc; i++) {
        if(!strcmp(argp[i], "help")) {
            printf("ssl handler parameters:\n");
@@ -528,6 +539,8 @@ void handlegnussl(int argc, char **argp, char **argv)
            printf("\t\tThe name of the file to read the certificate from.\n");
            printf("\tkey=KEY-FILE    [same as CERT-FILE]\n");
            printf("\t\tThe name of the file to read the private key from.\n");
+           printf("\tprio=PRIORITIES [NORMAL]\n");
+           printf("\t\tCiphersuite priorities, as a GnuTLS priority string.\n");
            printf("\ttrust=CA-FILE   [no default]\n");
            printf("\t\tThe name of a file to read trusted certificates from.\n");
            printf("\t\tMay be given multiple times.\n");
@@ -557,6 +570,17 @@ void handlegnussl(int argc, char **argp, char **argv)
            crtfile = argv[i];
        } else if(!strcmp(argp[i], "key")) {
            keyfile = argv[i];
+       } else if(!strcmp(argp[i], "prio")) {
+           if(ciphers != NULL)
+               gnutls_priority_deinit(ciphers);
+           ret = gnutls_priority_init(&ciphers, argv[i], (const char **)&perr);
+           if(ret == GNUTLS_E_INVALID_REQUEST) {
+               flog(LOG_ERR, "ssl: invalid cipher priority string, at `%s'", perr);
+               exit(1);
+           } else if(ret != 0) {
+               flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret));
+               exit(1);
+           }
        } else if(!strcmp(argp[i], "trust")) {
            if((ret = gnutls_certificate_set_x509_trust_file(creds, argv[i], GNUTLS_X509_FMT_PEM)) != 0) {
                flog(LOG_ERR, "ssl: could not load trust file `%s': %s", argv[i], gnutls_strerror(ret));
@@ -604,6 +628,10 @@ void handlegnussl(int argc, char **argp, char **argv)
        flog(LOG_ERR, "ssl: could not load certificate or key: %s", gnutls_strerror(ret));
        exit(1);
     }
+    if((ciphers == NULL) && ((ret = gnutls_priority_init(&ciphers, "NORMAL", NULL)) != 0)) {
+       flog(LOG_ERR, "ssl: could not initialize cipher priorities: %s", gnutls_strerror(ret));
+       exit(1);
+    }
     gnutls_certificate_set_dh_params(creds, dhparams());
     bufadd(ncreds, NULL);
     omalloc(pd);
@@ -611,6 +639,7 @@ void handlegnussl(int argc, char **argp, char **argv)
     pd->sport = port;
     pd->creds = creds;
     pd->ncreds = ncreds.b;
+    pd->ciphers = ciphers;
     bufadd(listeners, mustart(listenloop, pd));
     if((fd = listensock4(port)) < 0) {
        if(errno != EADDRINUSE) {
@@ -622,6 +651,8 @@ void handlegnussl(int argc, char **argp, char **argv)
        pd->fd = fd;
        pd->sport = port;
        pd->creds = creds;
+       pd->ncreds = ncreds.b;
+       pd->ciphers = ciphers;
        bufadd(listeners, mustart(listenloop, pd));
     }
 }