Module Name:    src
Committed By:   christos
Date:           Sat Dec 12 18:06:58 UTC 2015

Modified Files:
        src/libexec/httpd: bozohttpd.c bozohttpd.h ssl-bozo.c

Log Message:
Introduce bozo_strdup and bozo_asprintf to add error checking and reduce
code duplication.

Note that bozo_strdup is different that bozostrdup; the _ routines exit
loging error to syslog or stderr, whereas the non _ routines send error
responses to the http client.


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/libexec/httpd/bozohttpd.c
cvs rdiff -u -r1.39 -r1.40 src/libexec/httpd/bozohttpd.h
cvs rdiff -u -r1.19 -r1.20 src/libexec/httpd/ssl-bozo.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/libexec/httpd/bozohttpd.c
diff -u src/libexec/httpd/bozohttpd.c:1.71 src/libexec/httpd/bozohttpd.c:1.72
--- src/libexec/httpd/bozohttpd.c:1.71	Fri Oct 30 20:55:17 2015
+++ src/libexec/httpd/bozohttpd.c	Sat Dec 12 13:06:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: bozohttpd.c,v 1.71 2015/10/31 00:55:17 christos Exp $	*/
+/*	$NetBSD: bozohttpd.c,v 1.72 2015/12/12 18:06:58 christos Exp $	*/
 
 /*	$eterna: bozohttpd.c,v 1.178 2011/11/18 09:21:15 mrg Exp $	*/
 
@@ -936,16 +936,14 @@ handle_redirect(bozo_httpreq_t *request,
 			   * eg. https:// */
 
 	if (url == NULL) {
-		if (asprintf(&urlbuf, "/%s/", request->hr_file) == -1)
-			bozo_err(httpd, 1, "asprintf");
+		bozo_asprintf(httpd, &urlbuf, "/%s/", request->hr_file);
 		url = urlbuf;
 	} else
 		urlbuf = NULL;
 
 #ifndef NO_USER_SUPPORT
 	if (request->hr_user && !absolute) {
-		if (asprintf(&userbuf, "/~%s%s", request->hr_user, url) == -1)
-			bozo_err(httpd, 1, "asprintf");
+		bozo_asprintf(httpd, &userbuf, "/~%s%s", request->hr_user, url);
 		url = userbuf;
 	} else
 		userbuf = NULL;
@@ -1143,9 +1141,9 @@ check_virtual(bozo_httpreq_t *request)
 					debug((httpd, DEBUG_OBESE, "found it punch it"));
 					request->hr_virthostname =
 					    bozostrdup(httpd, d->d_name);
-					if (asprintf(&s, "%s/%s", httpd->virtbase,
-					    request->hr_virthostname) == -1)
-						bozo_err(httpd, 1, "asprintf");
+					bozo_asprintf(httpd, &s, "%s/%s",
+					    httpd->virtbase,
+					    request->hr_virthostname);
 					break;
 				}
 			}
@@ -1397,8 +1395,7 @@ transform_request(bozo_httpreq_t *reques
 		}
 		if (strchr(file + 2, '/') == NULL) {
 			char *userredirecturl;
-			if (asprintf(&userredirecturl, "%s/", file) == -1)
-				bozo_err(httpd, 1, "asprintf");
+			bozo_asprintf(httpd, &userredirecturl, "%s/", file);
 			handle_redirect(request, userredirecturl, 0);
 			free(userredirecturl);
 			return 0;
@@ -1559,8 +1556,7 @@ bozo_process_request(bozo_httpreq_t *req
 	fd = -1;
 	encoding = NULL;
 	if (can_gzip(request)) {
-		if (asprintf(&file, "%s.gz", request->hr_file) == -1)
-			bozo_err(httpd, 1, "asprintf");
+		bozo_asprintf(httpd, &file, "%s.gz", request->hr_file);
 		fd = open(file, O_RDONLY);
 		if (fd >= 0)
 			encoding = "gzip";
@@ -1794,6 +1790,32 @@ bozo_err(bozohttpd_t *httpd, int code, c
 	exit(code);
 }
 
+char *
+bozo_strdup(bozohttpd_t *httpd, const char *str)
+{
+	char	*p;
+
+	p = strdup(str);
+	if (p == NULL)
+		bozo_err(httpd, EXIT_FAILURE, "strdup");
+
+	return p;
+}
+
+void
+bozo_asprintf(bozohttpd_t *httpd, char **str, const char *fmt, ...)
+{
+	va_list ap;
+	int e;
+
+	va_start(ap, fmt);
+	e = vasprintf(str, fmt, ap);
+	va_end(ap);
+
+	if (e < 0)
+		bozo_err(httpd, EXIT_FAILURE, "asprintf");
+}
+
 /*
  * this escapes HTML tags.  returns allocated escaped
  * string if needed, or NULL on allocation failure or
@@ -1945,8 +1967,7 @@ bozo_http_error(bozohttpd_t *httpd, int 
 			if (user_escaped == NULL)
 				user_escaped = request->hr_user;
 			/* expand username to ~user/ */
-			if (asprintf(&user, "~%s/", user_escaped) == -1)
-				bozo_err(httpd, 1, "asprintf");
+			bozo_asprintf(httpd, &user, "~%s/", user_escaped);
 			if (user_escaped != request->hr_user)
 				free(user_escaped);
 		}
@@ -2125,7 +2146,7 @@ bozomalloc(bozohttpd_t *httpd, size_t si
 	if (p == NULL) {
 		(void)bozo_http_error(httpd, 500, NULL,
 				"memory allocation failure");
-		exit(1);
+		exit(EXIT_FAILURE);
 	}
 	return (p);
 }
@@ -2138,8 +2159,8 @@ bozostrdup(bozohttpd_t *httpd, const cha
 	p = strdup(str);
 	if (p == NULL) {
 		(void)bozo_http_error(httpd, 500, NULL,
-					"memory allocation failure");
-		exit(1);
+		    "memory allocation failure");
+		exit(EXIT_FAILURE);
 	}
 	return (p);
 }
@@ -2218,11 +2239,11 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 			bozo_err(httpd, 1, "gethostname");
 		httpd->virthostname[MAXHOSTNAMELEN] = '\0';
 	} else {
-		httpd->virthostname = strdup(vhost);
+		httpd->virthostname = bozo_strdup(httpd, vhost);
 	}
-	httpd->slashdir = strdup(root);
+	httpd->slashdir = bozo_strdup(httpd, root);
 	if ((portnum = bozo_get_pref(prefs, "port number")) != NULL) {
-		httpd->bindport = strdup(portnum);
+		httpd->bindport = bozo_strdup(httpd, portnum);
 	}
 
 	/* go over preferences now */
@@ -2235,7 +2256,7 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 		httpd->logstderr = 1;
 	}
 	if ((cp = bozo_get_pref(prefs, "bind address")) != NULL) {
-		httpd->bindaddress = strdup(cp);
+		httpd->bindaddress = bozo_strdup(httpd, cp);
 	}
 	if ((cp = bozo_get_pref(prefs, "background")) != NULL) {
 		httpd->background = atoi(cp);
@@ -2245,14 +2266,14 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 		httpd->foreground = 1;
 	}
 	if ((cp = bozo_get_pref(prefs, "pid file")) != NULL) {
-		httpd->pidfile = strdup(cp);
+		httpd->pidfile = bozo_strdup(httpd, cp);
 	}
 	if ((cp = bozo_get_pref(prefs, "unknown slash")) != NULL &&
 	    strcmp(cp, "true") == 0) {
 		httpd->unknown_slash = 1;
 	}
 	if ((cp = bozo_get_pref(prefs, "virtual base")) != NULL) {
-		httpd->virtbase = strdup(cp);
+		httpd->virtbase = bozo_strdup(httpd, cp);
 	}
 	if ((cp = bozo_get_pref(prefs, "enable users")) != NULL &&
 	    strcmp(cp, "true") == 0) {
@@ -2275,11 +2296,12 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 		httpd->dir_indexing = 1;
 	}
 	if ((cp = bozo_get_pref(prefs, "public_html")) != NULL) {
-		httpd->public_html = strdup(cp);
+		httpd->public_html = bozo_strdup(httpd, cp);
 	}
 	httpd->server_software =
-			strdup(bozo_get_pref(prefs, "server software"));
-	httpd->index_html = strdup(bozo_get_pref(prefs, "index.html"));
+	    bozo_strdup(httpd, bozo_get_pref(prefs, "server software"));
+	httpd->index_html =
+	    bozo_strdup(httpd, bozo_get_pref(prefs, "index.html"));
 
 	/*
 	 * initialise ssl and daemon mode if necessary.
@@ -2290,9 +2312,9 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 	if ((username = bozo_get_pref(prefs, "username")) == NULL) {
 		if ((pw = getpwuid(uid = 0)) == NULL)
 			bozo_err(httpd, 1, "getpwuid(0): %s", strerror(errno));
-		httpd->username = strdup(pw->pw_name);
+		httpd->username = bozo_strdup(httpd, pw->pw_name);
 	} else {
-		httpd->username = strdup(username);
+		httpd->username = bozo_strdup(httpd, username);
 		if ((pw = getpwnam(httpd->username)) == NULL)
 			bozo_err(httpd, 1, "getpwnam(%s): %s", httpd->username,
 					strerror(errno));
@@ -2307,7 +2329,7 @@ bozo_setup(bozohttpd_t *httpd, bozoprefs
 	 * handle chroot.
 	 */
 	if ((chrootdir = bozo_get_pref(prefs, "chroot dir")) != NULL) {
-		httpd->rootdir = strdup(chrootdir);
+		httpd->rootdir = bozo_strdup(httpd, chrootdir);
 		if (chdir(httpd->rootdir) == -1)
 			bozo_err(httpd, 1, "chdir(%s): %s", httpd->rootdir,
 				strerror(errno));

Index: src/libexec/httpd/bozohttpd.h
diff -u src/libexec/httpd/bozohttpd.h:1.39 src/libexec/httpd/bozohttpd.h:1.40
--- src/libexec/httpd/bozohttpd.h:1.39	Sat Dec 12 11:57:53 2015
+++ src/libexec/httpd/bozohttpd.h	Sat Dec 12 13:06:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: bozohttpd.h,v 1.39 2015/12/12 16:57:53 christos Exp $	*/
+/*	$NetBSD: bozohttpd.h,v 1.40 2015/12/12 18:06:58 christos Exp $	*/
 
 /*	$eterna: bozohttpd.h,v 1.39 2011/11/18 09:21:15 mrg Exp $	*/
 
@@ -223,6 +223,10 @@ void	bozo_warn(bozohttpd_t *, const char
 void	bozo_err(bozohttpd_t *, int, const char *, ...)
 		BOZO_PRINTFLIKE(3, 4)
 		BOZO_DEAD;
+void	bozo_asprintf(bozohttpd_t *, char **, const char *, ...)
+		BOZO_PRINTFLIKE(3, 4);
+char	*bozo_strdup(bozohttpd_t *, const char *);
+
 int	bozo_http_error(bozohttpd_t *, int, bozo_httpreq_t *, const char *);
 
 int	bozo_check_special_files(bozo_httpreq_t *, const char *);

Index: src/libexec/httpd/ssl-bozo.c
diff -u src/libexec/httpd/ssl-bozo.c:1.19 src/libexec/httpd/ssl-bozo.c:1.20
--- src/libexec/httpd/ssl-bozo.c:1.19	Sat Dec 12 11:57:53 2015
+++ src/libexec/httpd/ssl-bozo.c	Sat Dec 12 13:06:58 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: ssl-bozo.c,v 1.19 2015/12/12 16:57:53 christos Exp $	*/
+/*	$NetBSD: ssl-bozo.c,v 1.20 2015/12/12 18:06:58 christos Exp $	*/
 
 /*	$eterna: ssl-bozo.c,v 1.15 2011/11/18 09:21:15 mrg Exp $	*/
 
@@ -302,13 +302,13 @@ bozo_ssl_set_opts(bozohttpd_t *httpd, co
 {
 	sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
 
-	sslinfo->certificate_file = bozostrdup(httpd, cert);
-	sslinfo->privatekey_file = bozostrdup(httpd, priv);
+	sslinfo->certificate_file = bozo_strdup(httpd, cert);
+	sslinfo->privatekey_file = bozo_strdup(httpd, priv);
 	debug((httpd, DEBUG_NORMAL, "using cert/priv files: %s & %s",
 	    sslinfo->certificate_file,
 	    sslinfo->privatekey_file));
 	if (!httpd->bindport)
-		httpd->bindport = bozostrdup(httpd, "https");
+		httpd->bindport = bozo_strdup(httpd, "https");
 }
 
 void
@@ -316,7 +316,7 @@ bozo_ssl_set_ciphers(bozohttpd_t *httpd,
 {
 	sslinfo_t *sslinfo = bozo_get_sslinfo(httpd);
 
-	sslinfo->ciphers = bozostrdup(httpd, ciphers);
+	sslinfo->ciphers = bozo_strdup(httpd, ciphers);
 	debug((httpd, DEBUG_NORMAL, "using ciphers: %s", sslinfo->ciphers));
 }
 

Reply via email to