]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Some strings don't need to be shell escaped - for example when sending to
authorRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 20:26:49 +0000 (20:26 +0000)
committerRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 20:26:49 +0000 (20:26 +0000)
syslog(2), so just use vis(3).

compat/svis.c
compat/svis.h
dhcp-common.c
dhcp-common.h
dhcp.c
dhcpcd.c
ipv6nd.c
script.c

index 7bd97ce2ff6cc2c301176335c637a79993ec6afb..37102d743dcb398995e67d95a2287dbebe0a6f86 100644 (file)
@@ -158,3 +158,10 @@ svis(char *dst, int c, int flag, int nextc, const char *extra)
        }
        return dst;
 }
+
+char *
+vis(char *dst, int c, int flag, int nextc)
+{
+
+       return svis(dst, c, flag, nextc, "");
+}
index 982d9f291e700fd21a3305d63888102465df7102..c56306ce503119746691b021a7e1795e7cd6b273 100644 (file)
@@ -4,6 +4,7 @@
 #define        VIS_OCTAL       0x0001  /* use octal \ddd format */
 #define        VIS_CSTYLE      0x0002  /* use \[nrft0..] where appropiate */
 
+char *vis(char *dst, int c, int flag, int nextc);
 char *svis(char *dst, int c, int flag, int nextc, const char *meta);
 
 #endif
index 569766b12e10f91fce7587d05c00193a977ae1ff..c692a1adb2ace5505d346a65490ee58e596b7de0 100644 (file)
@@ -310,7 +310,7 @@ decode_rfc3397(char *out, size_t len, const uint8_t *p, size_t pl)
  * Any non visible characters are escaped as an octal number.
  */
 ssize_t
-print_string(char *s, size_t len, const uint8_t *data, size_t dl)
+print_string(char *s, size_t len, int flags, const uint8_t *data, size_t dl)
 {
        uint8_t c;
        const uint8_t *e, *p;
@@ -329,8 +329,12 @@ print_string(char *s, size_t len, const uint8_t *data, size_t dl)
                        if (p == e)
                                break;
                }
-               ve = svis(v, c, VIS_CSTYLE | VIS_OCTAL,
-                   data <= e ? *data : 0, ESCAPE_CHARS);
+               if (flags)
+                       ve = svis(v, c, VIS_CSTYLE | VIS_OCTAL,
+                           data <= e ? *data : 0, ESCAPE_CHARS);
+               else
+                       ve = vis(v, c, VIS_CSTYLE | VIS_OCTAL,
+                           data <= e ? *data : 0);
                if (s && len < (size_t)(ve - v) + 1) {
                        errno = ENOBUFS;
                        return -1;
@@ -426,7 +430,7 @@ print_option(char *s, size_t len, int type, const uint8_t *data, size_t dl,
                if (tmp == NULL)
                        return -1;
                decode_rfc3397(tmp, l, data, dl);
-               sl = print_string(s, len, (uint8_t *)tmp, l - 1);
+               sl = print_string(s, len, 1, (uint8_t *)tmp, l - 1);
                free(tmp);
                return sl;
        }
@@ -436,7 +440,7 @@ print_option(char *s, size_t len, int type, const uint8_t *data, size_t dl,
                if ((tmp = decode_rfc3361(data, dl)) == NULL)
                        return -1;
                l = strlen(tmp);
-               sl = print_string(s, len, (uint8_t *)tmp, l);
+               sl = print_string(s, len, 1, (uint8_t *)tmp, l);
                free(tmp);
                return sl;
        }
@@ -452,7 +456,7 @@ print_option(char *s, size_t len, int type, const uint8_t *data, size_t dl,
                /* Some DHCP servers return NULL strings */
                if (*data == '\0')
                        return 0;
-               return print_string(s, len, data, dl);
+               return print_string(s, len, 1, data, dl);
        }
 
        if (type & FLAG) {
index d6b4d5ff285c4d005b959bc339182d86da014972..4b0e0febd29c265e3a22398d929f1786ff4ccd7f 100644 (file)
@@ -93,7 +93,7 @@ int make_option_mask(const struct dhcp_opt *, size_t,
 
 size_t encode_rfc1035(const char *src, uint8_t *dst);
 ssize_t decode_rfc3397(char *, size_t, const uint8_t *, size_t);
-ssize_t print_string(char *, size_t, const uint8_t *, size_t);
+ssize_t print_string(char *, size_t, int, const uint8_t *, size_t);
 ssize_t print_option(char *, size_t, int, const uint8_t *, size_t,
     const char *);
 
diff --git a/dhcp.c b/dhcp.c
index 27cc4b669a09db492b55c52eae9463bf34845e33..a124289d752a82558f5b4247cd2994b387b5ae07 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -1269,12 +1269,12 @@ dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp,
        }
 
        if (*dhcp->bootfile && !(overl & 1)) {
-               print_string(safe, sizeof(safe),
+               print_string(safe, sizeof(safe), 1,
                    dhcp->bootfile, sizeof(dhcp->bootfile));
                setvar(&ep, prefix, "filename", safe);
        }
        if (*dhcp->servername && !(overl & 2)) {
-               print_string(safe, sizeof(safe),
+               print_string(safe, sizeof(safe), 1,
                    dhcp->servername, sizeof(dhcp->servername));
                setvar(&ep, prefix, "server_name", safe);
        }
@@ -2190,9 +2190,25 @@ log_dhcp1(int lvl, const char *msg,
        struct in_addr addr;
        int r;
 
-       if (strcmp(msg, "NAK:") == 0)
+       if (strcmp(msg, "NAK:") == 0) {
                a = get_option_string(iface->ctx, dhcp, DHO_MESSAGE);
-       else if (ad && dhcp->yiaddr != 0) {
+               if (a) {
+                       char *tmp;
+                       size_t al, tmpl;
+
+                       al = strlen(a);
+                       tmpl = (al * 4) + 1;
+                       tmp = malloc(tmpl);
+                       if (tmp == NULL) {
+                               syslog(LOG_ERR, "%s: %m", __func__);
+                               free(a);
+                               return;
+                       }
+                       print_string(tmp, tmpl, 0, (uint8_t *)a, al);
+                       free(a);
+                       a = tmp;
+               }
+       } else if (ad && dhcp->yiaddr != 0) {
                addr.s_addr = dhcp->yiaddr;
                a = strdup(inet_ntoa(addr));
                if (a == NULL) {
@@ -2205,7 +2221,7 @@ log_dhcp1(int lvl, const char *msg,
        tfrom = "from";
        r = get_option_addr(iface->ctx, &addr, dhcp, DHO_SERVERID);
        if (dhcp->servername[0] && r == 0) {
-               print_string(sname, sizeof(sname),
+               print_string(sname, sizeof(sname), 0,
                    dhcp->servername, strlen((const char *)dhcp->servername));
                if (a == NULL)
                        syslog(lvl, "%s: %s %s %s `%s'", iface->name, msg,
index 240bd454d89494e803be4e9b8cd9de77bc8331b4..7e174ccafc8ef9db4d9ffde9a4e0704a6250ca30 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -490,7 +490,9 @@ dhcpcd_selectprofile(struct interface *ifp, const char *profile)
 
        if (ifp->ssid_len) {
                ssize_t r;
-               r =print_string(pssid, sizeof(pssid), ifp->ssid, ifp->ssid_len);
+
+               r = print_string(pssid, sizeof(pssid), 0,
+                   ifp->ssid, ifp->ssid_len);
                if (r == -1) {
                        syslog(LOG_ERR, "%s: %s: %m", ifp->name, __func__);
                        pssid[0] = '\0';
index 794ef9dcc4115880877d7fa620c4af7c70980fce..fbe85275f6284f42bcc57148f7d539edd556ca14 100644 (file)
--- a/ipv6nd.c
+++ b/ipv6nd.c
@@ -1010,11 +1010,11 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp,
                                if (tmp) {
                                        decode_rfc3397(tmp, l, op, n);
                                        l -= 1;
-                                       n = (size_t)print_string(NULL, 0,
+                                       n = (size_t)print_string(NULL, 0, 1,
                                            (const uint8_t *)tmp, l);
                                        opt = malloc(n);
                                        if (opt)
-                                               print_string(opt, n,
+                                               print_string(opt, n, 1,
                                                    (const uint8_t *)tmp, l);
                                        else
                                                syslog(LOG_ERR, "%s: %m",
index c5adb8e5b78d058a929e75a5aa6cf7a511bfcb78..42ffd92cccebfa44c2cde42079e510e3a8f52747 100644 (file)
--- a/script.c
+++ b/script.c
@@ -387,12 +387,12 @@ make_env(const struct interface *ifp, const char *reason, char ***argv)
                        ssize_t psl;
 
                        pfx_len = strlen(pfx);
-                       psl = print_string(NULL, 0,
+                       psl = print_string(NULL, 0, 1,
                            (const uint8_t *)ifp->ssid, ifp->ssid_len);
                        if (psl != -1) {
                                EMALLOC(elen, pfx_len + (size_t)psl + 1);
                                memcpy(env[elen], pfx, pfx_len);
-                               print_string(env[elen] + pfx_len, (size_t)psl,
+                               print_string(env[elen] + pfx_len, (size_t)psl, 1,
                                    (const uint8_t *)ifp->ssid, ifp->ssid_len);
                                elen++;
                        }