Module Name:    src
Committed By:   christos
Date:           Fri Jan 11 20:37:30 UTC 2019

Modified Files:
        src/lib/libwrap: Makefile diag.c
Added Files:
        src/lib/libwrap: expandm.c expandm.h

Log Message:
Use a utility function to expand %m correctly...


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/lib/libwrap/Makefile
cvs rdiff -u -r1.15 -r1.16 src/lib/libwrap/diag.c
cvs rdiff -u -r0 -r1.1 src/lib/libwrap/expandm.c src/lib/libwrap/expandm.h

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

Modified files:

Index: src/lib/libwrap/Makefile
diff -u src/lib/libwrap/Makefile:1.10 src/lib/libwrap/Makefile:1.11
--- src/lib/libwrap/Makefile:1.10	Mon May 28 08:06:22 2007
+++ src/lib/libwrap/Makefile	Fri Jan 11 15:37:30 2019
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.10 2007/05/28 12:06:22 tls Exp $
+#	$NetBSD: Makefile,v 1.11 2019/01/11 20:37:30 christos Exp $
 
 USE_FORT?= yes	# network server
 
@@ -6,7 +6,7 @@ LIB=	wrap
 
 SRCS=	hosts_access.c options.c shell_cmd.c rfc931.c eval.c hosts_ctl.c \
 	refuse.c percent_x.c clean_exit.c fix_options.c socket.c \
-	update.c misc.c diag.c
+	update.c misc.c diag.c expandm.c
 MAN=	hosts_access.3 hosts_access.5 hosts_options.5
 MLINKS+=hosts_access.5 hosts.allow.5
 MLINKS+=hosts_access.5 hosts.deny.5

Index: src/lib/libwrap/diag.c
diff -u src/lib/libwrap/diag.c:1.15 src/lib/libwrap/diag.c:1.16
--- src/lib/libwrap/diag.c:1.15	Fri Jan 11 11:15:20 2019
+++ src/lib/libwrap/diag.c	Fri Jan 11 15:37:30 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: diag.c,v 1.15 2019/01/11 16:15:20 christos Exp $	*/
+/*	$NetBSD: diag.c,v 1.16 2019/01/11 20:37:30 christos Exp $	*/
 
  /*
   * Routines to report various classes of problems. Each report is decorated
@@ -16,7 +16,7 @@
 #if 0
 static char sccsid[] = "@(#) diag.c 1.1 94/12/28 17:42:20";
 #else
-__RCSID("$NetBSD: diag.c,v 1.15 2019/01/11 16:15:20 christos Exp $");
+__RCSID("$NetBSD: diag.c,v 1.16 2019/01/11 20:37:30 christos Exp $");
 #endif
 #endif
 
@@ -33,6 +33,7 @@ __RCSID("$NetBSD: diag.c,v 1.15 2019/01/
 /* Local stuff */
 
 #include "tcpd.h"
+#include "expandm.h"
 
 struct tcpd_context tcpd_context;
 jmp_buf tcpd_buf;
@@ -45,25 +46,22 @@ static void tcpd_diag(int, const char *,
 static void
 tcpd_diag(int severity, const char *tag, const char *fmt, va_list ap)
 {
-    char *buf;
-    int e, oerrno = errno;
+    char *buf2, *buf = expandm(fmt, NULL);
+
+    if (vasprintf(&buf2, buf, ap) == -1)
+	buf2 = buf;
 
     /* contruct the tag for the log entry */
     if (tcpd_context.file)
-	e = asprintf(&buf, "%s: %s, line %d: %s",
-	    tag, tcpd_context.file, tcpd_context.line, fmt);
+	syslog(severity, "%s: %s, line %d: %s",
+	    tag, tcpd_context.file, tcpd_context.line, buf2);
     else
-	e = asprintf(&buf, "%s: %s", tag, fmt);
-
-    if (e == -1)
-	buf = __UNCONST(fmt);
-
-    errno = oerrno;
-
-    vsyslog(severity, buf, ap);
+	syslog(severity, "%s: %s", tag, buf2);
 
     if (buf != fmt)
         free(buf);
+    if (buf2 != buf)
+	free(buf2);
 }
 
 /* tcpd_warn - report problem of some sort and proceed */

Added files:

Index: src/lib/libwrap/expandm.c
diff -u /dev/null src/lib/libwrap/expandm.c:1.1
--- /dev/null	Fri Jan 11 15:37:30 2019
+++ src/lib/libwrap/expandm.c	Fri Jan 11 15:37:30 2019
@@ -0,0 +1,77 @@
+/*	$NetBSD: expandm.c,v 1.1 2019/01/11 20:37:30 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__RCSID("$NetBSD: expandm.c,v 1.1 2019/01/11 20:37:30 christos Exp $");
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include "expandm.h"
+
+char * __attribute__((__format_arg__(1)))
+expandm(const char *fmt, const char *sf)
+{
+	const char *e = strerror(errno);
+	char *buf, *m, *nbuf;
+	const char *ptr;
+
+	for (ptr = fmt, buf = NULL; (m = strstr(ptr, "%m")); ptr = m + 2) {
+		size_t cnt = 0;
+		for (char *p = m; p >= ptr && *p == '%'; p--)
+			cnt++;
+		if (asprintf(&nbuf, "%s%.*s%s", buf ? buf : "",
+		    (int)(m - ptr), ptr, (cnt & 1) ? e : "%%m") == -1)
+			goto out;
+		free(buf);
+		buf = nbuf;
+	}
+
+	if (asprintf(&nbuf, "%s%s%s", buf ? buf : "", ptr, sf ? sf : "") == -1)
+		goto out;
+
+	free(buf);
+	return nbuf;
+out:
+	free(buf);
+	return __UNCONST(fmt);
+}
+
+#ifdef TEST
+int
+main(int argc, char *argv[])
+{
+	errno = ERANGE;
+	printf("%s\n", expandm(argv[1]));
+	return 0;
+}
+#endif
Index: src/lib/libwrap/expandm.h
diff -u /dev/null src/lib/libwrap/expandm.h:1.1
--- /dev/null	Fri Jan 11 15:37:30 2019
+++ src/lib/libwrap/expandm.h	Fri Jan 11 15:37:30 2019
@@ -0,0 +1,35 @@
+/*	$NetBSD: expandm.h,v 1.1 2019/01/11 20:37:30 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2018 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+
+__BEGIN_DECLS
+char * __attribute__((__format_arg__(1))) expandm(const char *, const char *);
+__END_DECLS

Reply via email to