Source-Changes-HG archive

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

[src/trunk]: src/lib/libwrap Use a utility function to expand %m correctly...



details:   https://anonhg.NetBSD.org/src/rev/46b0df6e77e8
branches:  trunk
changeset: 447455:46b0df6e77e8
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Jan 11 20:37:30 2019 +0000

description:
Use a utility function to expand %m correctly...

diffstat:

 lib/libwrap/Makefile  |   4 +-
 lib/libwrap/diag.c    |  26 +++++++---------
 lib/libwrap/expandm.c |  77 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/libwrap/expandm.h |  35 +++++++++++++++++++++++
 4 files changed, 126 insertions(+), 16 deletions(-)

diffs (199 lines):

diff -r 9e120b123dbf -r 46b0df6e77e8 lib/libwrap/Makefile
--- a/lib/libwrap/Makefile      Fri Jan 11 16:15:20 2019 +0000
+++ b/lib/libwrap/Makefile      Fri Jan 11 20:37:30 2019 +0000
@@ -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 @@
 
 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
diff -r 9e120b123dbf -r 46b0df6e77e8 lib/libwrap/diag.c
--- a/lib/libwrap/diag.c        Fri Jan 11 16:15:20 2019 +0000
+++ b/lib/libwrap/diag.c        Fri Jan 11 20:37:30 2019 +0000
@@ -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 @@
 /* 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 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 */
diff -r 9e120b123dbf -r 46b0df6e77e8 lib/libwrap/expandm.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libwrap/expandm.c     Fri Jan 11 20:37:30 2019 +0000
@@ -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
diff -r 9e120b123dbf -r 46b0df6e77e8 lib/libwrap/expandm.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/libwrap/expandm.h     Fri Jan 11 20:37:30 2019 +0000
@@ -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



Home | Main Index | Thread Index | Old Index