Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/httpd - convert most asprintf() calls to bozoasprint...



details:   https://anonhg.NetBSD.org/src/rev/57cb8f5558e8
branches:  trunk
changeset: 342570:57cb8f5558e8
user:      mrg <mrg%NetBSD.org@localhost>
date:      Tue Dec 29 04:21:46 2015 +0000

description:
- convert most asprintf() calls to bozoasprintf().
- don't call getpwuid(0) if we don't need to, or fail it it fails,
  and remove the 'username' member of bozohttpd_t since it is not
  used outside of bozo_setup().

diffstat:

 libexec/httpd/bozohttpd.c      |  41 +++++++++++++++--------------------------
 libexec/httpd/bozohttpd.h      |   3 +--
 libexec/httpd/cgi-bozo.c       |  13 ++++++++-----
 libexec/httpd/dir-index-bozo.c |   7 +++----
 4 files changed, 27 insertions(+), 37 deletions(-)

diffs (173 lines):

diff -r fb777e40c4de -r 57cb8f5558e8 libexec/httpd/bozohttpd.c
--- a/libexec/httpd/bozohttpd.c Tue Dec 29 01:58:26 2015 +0000
+++ b/libexec/httpd/bozohttpd.c Tue Dec 29 04:21:46 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.c,v 1.74 2015/12/28 07:37:59 mrg Exp $       */
+/*     $NetBSD: bozohttpd.c,v 1.75 2015/12/29 04:21:46 mrg Exp $       */
 
 /*     $eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $      */
 
@@ -383,11 +383,7 @@
                /* yup, merge it in */
                char *nval;
 
-               if (asprintf(&nval, "%s, %s", hdr->h_value, str) == -1) {
-                       (void)bozo_http_error(httpd, 500, NULL,
-                            "memory allocation failure");
-                       return NULL;
-               }
+               bozoasprintf(httpd, &nval, "%s, %s", hdr->h_value, str);
                free(hdr->h_value);
                hdr->h_value = nval;
        } else {
@@ -955,9 +951,9 @@
                const char *s;
 
                /*
-                * absolute redirect may specify own protocol i.e. to redirect to
-                * another schema like https:// or ftp://. Details: RFC 3986, section
-                * 3.
+                * absolute redirect may specify own protocol i.e. to redirect
+                * to another schema like https:// or ftp://.
+                * Details: RFC 3986, section 3.
                 */
 
                /* 1. check if url contains :// */
@@ -969,8 +965,8 @@
                 */
                if (sep) {
                        for (s = url; s != sep;) {
-                               if (!isalnum((int)*s) && *s != '+' && *s != '-' &&
-                                       *s != '.')
+                               if (!isalnum((int)*s) &&
+                                   *s != '+' && *s != '-' && *s != '.')
                                        break;
                                if (++s == sep) {
                                        absproto = 1;
@@ -2223,7 +2219,6 @@
 
        if (vhost == NULL) {
                httpd->virthostname = bozomalloc(httpd, MAXHOSTNAMELEN+1);
-               /* XXX we do not check for FQDN here */
                if (gethostname(httpd->virthostname, MAXHOSTNAMELEN+1) < 0)
                        bozoerr(httpd, 1, "gethostname");
                httpd->virthostname[MAXHOSTNAMELEN] = '\0';
@@ -2298,20 +2293,16 @@
        bozo_ssl_init(httpd);
        bozo_daemon_init(httpd);
 
-       if ((username = bozo_get_pref(prefs, "username")) == NULL) {
-               if ((pw = getpwuid(uid = 0)) == NULL)
-                       bozoerr(httpd, 1, "getpwuid(0): %s", strerror(errno));
-               httpd->username = bozostrdup(httpd, NULL, pw->pw_name);
-       } else {
-               httpd->username = bozostrdup(httpd, NULL, username);
-               if ((pw = getpwnam(httpd->username)) == NULL)
-                       bozoerr(httpd, 1, "getpwnam(%s): %s", httpd->username,
-                                       strerror(errno));
+       username = bozo_get_pref(prefs, "username");
+       if (username != NULL) {
+               if ((pw = getpwnam(username)) == NULL)
+                       bozoerr(httpd, 1, "getpwnam(%s): %s", username,
+                               strerror(errno));
                if (initgroups(pw->pw_name, pw->pw_gid) == -1)
                        bozoerr(httpd, 1, "initgroups: %s", strerror(errno));
                if (setgid(pw->pw_gid) == -1)
                        bozoerr(httpd, 1, "setgid(%u): %s", pw->pw_gid,
-                                       strerror(errno));
+                               strerror(errno));
                uid = pw->pw_uid;
        }
        /*
@@ -2327,10 +2318,8 @@
                                strerror(errno));
        }
 
-       if (username != NULL)
-               if (setuid(uid) == -1)
-                       bozoerr(httpd, 1, "setuid(%d): %s", uid,
-                                       strerror(errno));
+       if (username != NULL && setuid(uid) == -1)
+               bozoerr(httpd, 1, "setuid(%d): %s", uid, strerror(errno));
 
        /*
         * prevent info leakage between different compartments.
diff -r fb777e40c4de -r 57cb8f5558e8 libexec/httpd/bozohttpd.h
--- a/libexec/httpd/bozohttpd.h Tue Dec 29 01:58:26 2015 +0000
+++ b/libexec/httpd/bozohttpd.h Tue Dec 29 04:21:46 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bozohttpd.h,v 1.42 2015/12/28 07:37:59 mrg Exp $       */
+/*     $NetBSD: bozohttpd.h,v 1.43 2015/12/29 04:21:46 mrg Exp $       */
 
 /*     $eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $       */
 
@@ -90,7 +90,6 @@
 /* this structure encapsulates all the bozo flags and control vars */
 typedef struct bozohttpd_t {
        char            *rootdir;       /* root directory */
-       char            *username;      /* username to switch to */
        int              numeric;       /* avoid gethostby*() */
        char            *virtbase;      /* virtual directory base */
        int              unknown_slash; /* unknown vhosts go to normal slashdir */
diff -r fb777e40c4de -r 57cb8f5558e8 libexec/httpd/cgi-bozo.c
--- a/libexec/httpd/cgi-bozo.c  Tue Dec 29 01:58:26 2015 +0000
+++ b/libexec/httpd/cgi-bozo.c  Tue Dec 29 04:21:46 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cgi-bozo.c,v 1.30 2015/12/28 07:37:59 mrg Exp $        */
+/*     $NetBSD: cgi-bozo.c,v 1.31 2015/12/29 04:21:46 mrg Exp $        */
 
 /*     $eterna: cgi-bozo.c,v 1.40 2011/11/18 09:21:15 mrg Exp $        */
 
@@ -274,7 +274,7 @@
        if (uri[0] == '/')
                file = bozostrdup(httpd, request, uri);
        else
-               asprintf(&file, "/%s", uri);
+               bozoasprintf(httpd, &file, "/%s", uri);
        if (file == NULL)
                return 0;
 
@@ -283,7 +283,10 @@
        else
                query = NULL;
 
-       asprintf(&url, "%s%s%s", file, query ? "?" : "", query ? query : "");
+       bozoasprintf(httpd, &url, "%s%s%s",
+                    file,
+                    query ? "?" : "",
+                    query ? query : "");
        if (url == NULL)
                goto out;
        debug((httpd, DEBUG_NORMAL, "bozo_process_cgi: url `%s'", url));
@@ -420,8 +423,8 @@
                bozo_setenv(httpd, "REMOTE_ADDR", request->hr_remoteaddr,
                                curenvp++);
        /*
-        * XXX Apache does this when invoking content handlers, and PHP
-        * XXX 5.3 requires it as a "security" measure.
+        * Apache does this when invoking content handlers, and PHP
+        * 5.3 requires it as a "security" measure.
         */
        if (cgihandler)
                bozo_setenv(httpd, "REDIRECT_STATUS", "200", curenvp++);
diff -r fb777e40c4de -r 57cb8f5558e8 libexec/httpd/dir-index-bozo.c
--- a/libexec/httpd/dir-index-bozo.c    Tue Dec 29 01:58:26 2015 +0000
+++ b/libexec/httpd/dir-index-bozo.c    Tue Dec 29 04:21:46 2015 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: dir-index-bozo.c,v 1.24 2015/12/28 07:37:59 mrg Exp $  */
+/*     $NetBSD: dir-index-bozo.c,v 1.25 2015/12/29 04:21:46 mrg Exp $  */
 
 /*     $eterna: dir-index-bozo.c,v 1.20 2011/11/18 09:21:15 mrg Exp $  */
 
@@ -110,9 +110,8 @@
 
 #ifndef NO_USER_SUPPORT
        if (request->hr_user) {
-               if (asprintf(&printname, "~%s/%s", request->hr_user,
-                 request->hr_file) < 0)
-                       bozoerr(httpd, 1, "asprintf");
+               bozoasprintf(httpd, &printname, "~%s/%s",
+                            request->hr_user, request->hr_file);
        } else
                printname = bozostrdup(httpd, request, request->hr_file);
 #else



Home | Main Index | Thread Index | Old Index