]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Send a FQDN hostname if it contains dots, strip the domain from a looked up hostname...
authorRoy Marples <roy@marples.name>
Thu, 26 Apr 2007 16:16:56 +0000 (16:16 +0000)
committerRoy Marples <roy@marples.name>
Thu, 26 Apr 2007 16:16:56 +0000 (16:16 +0000)
ChangeLog
Makefile
arp.c
client.c
common.c
common.h
configure.c
dhcpcd.c
interface.c
logger.c
socket.c

index 8dbd7126b99051f8f9f1a51e90edf5552f19da43..53a333af7756f0e41e9bf43ae244e5ea9145f425 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+If the current hostname contains dots then send it as a FQDN.
 Ensure that static routes are always added before routers.
 Use getnameinfo instead of gethostbyaddr.
 Remove gateways from the ROUTES and add to GATEWAYS
index 265bece044c1900900c6c484b20565bbcf001368..93127ca44a356c983beb501b1cd20164b4ee70c8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-VERSION = 3.0.18_pre1
+VERSION = 3.0.18_pre2
 CFLAGS ?= -O2 -pipe
 
 # Should work for both GNU make and BSD make
diff --git a/arp.c b/arp.c
index 9a54453eac3cdca88b4a020dc00713f341105ddf..0828ec1c7c5c3405e686a70fabecbe453efb255f 100644 (file)
--- a/arp.c
+++ b/arp.c
@@ -51,6 +51,9 @@
                                        (ap)->ar_hln + (ap)->ar_pln)
 #define ar_tpa(ap) (((unsigned char *) ((ap) + 1)) + \
                                        2 * (ap)->ar_hln + (ap)->ar_pln)
+#endif
+
+#ifndef arphdr_len
 #define arphdr_len2(ar_hln, ar_pln) (sizeof (struct arphdr) + \
                                                                         2 * (ar_hln) + 2 * (ar_pln))
 #define arphdr_len(ap) (arphdr_len2 ((ap)->ar_hln, (ap)->ar_pln))
index 1aa8a88e9b0a143122b6316cd0010d5b6a5b9860..adf4b3d32d47b11b2d0949ca561e6668919bd0bd 100644 (file)
--- a/client.c
+++ b/client.c
@@ -94,6 +94,7 @@
 static int daemonise (const char *pidfile)
 {
        logger (LOG_DEBUG, "forking to background");
+
        if (daemon (0, 0) < 0) {
                logger (LOG_ERR, "daemon: %s", strerror (errno));
                return -1;
index bbddf3cd968586c5591726de85d5dca025f2d853..c609abaa0dd42a20e367fa5c379349db69832840 100644 (file)
--- a/common.c
+++ b/common.c
@@ -104,6 +104,20 @@ void *xmalloc (size_t size)
                return value;
 
        logger (LOG_ERR, "memory exhausted");
-       exit (1);
+       exit (EXIT_FAILURE);
+}
+
+char *xstrdup (const char *str)
+{
+       char *value;
+
+       if (! str)
+               return (NULL);
+
+       if ((value = strdup (str)))
+               return (value);
+
+       logger (LOG_ERR, "memory exhausted");
+       exit (EXIT_FAILURE);
 }
 
index 30a2781d002f4b33279be5cb6ef2f501b111514f..da2962cb27930e1f091d5c69e2d15695f39999f3 100644 (file)
--- a/common.h
+++ b/common.h
@@ -34,6 +34,6 @@ size_t strlcpy (char *dst, const char *src, size_t size);
 
 long uptime (void);
 void *xmalloc (size_t size);
-
+char *xstrdup (const char *str);
 
 #endif
index 20794b2cf2c97564e14442f8791e018709e67823..1904a2423841e417e305d11030d2a1b5e0fea527 100644 (file)
@@ -661,8 +661,8 @@ int configure (const options_t *options, interface_t *iface,
                make_nis(iface->name, dhcp);
 #endif
 
-       /* Now we have made a resolv.conf we can obtain a hostname if we need one */
-       if (options->dohostname && ! dhcp->hostname) {
+       /* Now we have made a resolv.conf we can obtain a hostname if we need it */
+       if (options->dohostname && ! dhcp->hostname) { 
                union {
                        struct sockaddr sa;
                        struct sockaddr_in sin;
@@ -670,33 +670,55 @@ int configure (const options_t *options, interface_t *iface,
                socklen_t salen;
                char addr[NI_MAXHOST];
                struct addrinfo hints, *res;
+               int result;
 
                salen = sizeof (struct sockaddr);
                memset (&su.sa, 0, salen);
                su.sin.sin_family = AF_INET;
                memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (struct in_addr));
 
-               if (getnameinfo (&su.sa, salen, addr, sizeof (addr),
-                                                NULL, 0, NI_NAMEREQD) == 0) {
+               logger (LOG_DEBUG, "Looking up hostname via DNS");
+               if ((result = getnameinfo (&su.sa, salen, addr, sizeof (addr),
+                                                NULL, 0, NI_NAMEREQD)) != 0)
+                       logger (LOG_ERR, "Failed to lookup hostname via DNS: %s", gai_strerror (result));
+               else {
+                       /* Check for a malicious PTR record */
                        memset (&hints, 0, sizeof (hints));
                        hints.ai_socktype = SOCK_DGRAM;
                        hints.ai_flags = AI_NUMERICHOST;
-                       /* Check for a malicious PTR record */
                        if (getaddrinfo (addr, "0", &hints, &res) == 0) {
                                freeaddrinfo (res);
                                addr[0] = '\0';
                                logger (LOG_ERR, "malicious PTR record detected");
+                       } else if (*addr) {
+                               /* Strip out the domain if it matches */
+                               char *p = addr;
+                               char *token = strsep (&p, ".");
+                               bool match_domain = false;
+
+                               if (p && *p) {
+                                       if (dhcp->dnssearch) {
+                                               char *s = xstrdup (dhcp->dnssearch);
+                                               char *sp = s;
+                                               char *t;
+
+                                               while ((t = strsep (&sp, " ")))
+                                                       if (strcmp (t, p) == 0) {
+                                                               match_domain = true;
+                                                               break;
+                                                       }
+                                               free (s);
+                                       } else if (dhcp->dnsdomain) {
+                                               if (strcmp (dhcp->dnsdomain, p) == 0)
+                                                       match_domain = true;
+                                       }
+                               }
+                               if (match_domain)
+                                       strlcpy (newhostname, token, sizeof (newhostname));
+                               else
+                                       snprintf (newhostname, sizeof (newhostname), "%s.%s", token, p);
                        }
                }
-               
-               /* Split the hostname from the domain */
-               if (addr[0]) {
-                       char *p = addr;
-                       char *token = strsep (&p, ".");
-                       if (token)
-                               strlcpy (newhostname, token, sizeof (newhostname));
-               } else
-                       logger (LOG_ERR, "failed to lookup the hostname");
        }
 
        gethostname (curhostname, sizeof (curhostname));
index 7c87d54e73a10423ba9db49fac60304d7df7a886..2a952e6b41817e006af227c65d17ba45ac64b21d 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -109,6 +109,7 @@ int main(int argc, char **argv)
        char prefix[IF_NAMESIZE + 3];
        pid_t pid;
        int debug = 0;
+       int i;
 
        const struct option longopts[] = {
         {"arp",         no_argument,        NULL, 'a'},
@@ -137,13 +138,9 @@ int main(int argc, char **argv)
         {NULL,          0,                  NULL, 0}
        };
 
-       /* Sanitize our fd's */
-       int zero;
-       if ((zero = open (_PATH_DEVNULL, O_RDWR, 0)) >= 0) {
-               while (zero < 3)
-                       zero = dup (zero);
-               close(zero);
-       }
+       /* Close any un-needed fd's */
+       for (i = getdtablesize() - 1; i >= 3; --i)
+               close (i);
 
        openlog (PACKAGE, LOG_PID, LOG_LOCAL0);
 
@@ -158,10 +155,6 @@ int main(int argc, char **argv)
        options.dontp = true;
        options.dogateway = true;
        options.daemonise = true;
-       gethostname (options.hostname, sizeof (options.hostname));
-       if (strcmp (options.hostname, "(none)") == 0 ||
-               strcmp (options.hostname, "localhost") == 0)
-               memset (options.hostname, 0, sizeof (options.hostname));
        options.timeout = DEFAULT_TIMEOUT;
 
        while ((ch = getopt_long(argc, argv, "ac:dh:i:kl:m:nps:t:u:F:GHI:MNRY", longopts,
@@ -242,7 +235,6 @@ int main(int argc, char **argv)
                                break;
                        case 'u':
                                {
-                                       int i;
                                        int offset = 0;
                                        for (i = 0; i < userclasses; i++)
                                                offset += (int) options.userclass[offset] + 1;
@@ -258,11 +250,11 @@ int main(int argc, char **argv)
                                }
                                break;
                        case 'F':
-                               if (strcmp (optarg, "none") == 0)
+                               if (strncmp (optarg, "none", strlen (optarg)) == 0)
                                        options.fqdn = FQDN_NONE;
-                               else if (strcmp (optarg, "ptr") == 0)
+                               else if (strncmp (optarg, "ptr", strlen (optarg)) == 0)
                                        options.fqdn = FQDN_PTR;
-                               else if (strcmp (optarg, "both") == 0)
+                               else if (strncmp (optarg, "both", strlen (optarg)) == 0)
                                        options.fqdn = FQDN_BOTH;
                                else {
                                        logger (LOG_ERR, "invalid value `%s' for FQDN", optarg);
@@ -326,6 +318,19 @@ int main(int argc, char **argv)
                exit (EXIT_FAILURE);
        }
 
+       /* If we are given a hostname use it and set FQDN if it contains a . */
+       if (! options.hostname[0]) {
+               gethostname (options.hostname, sizeof (options.hostname));
+               if (strcmp (options.hostname, "(none)") == 0 ||
+                       strcmp (options.hostname, "localhost") == 0)
+                       memset (options.hostname, 0, sizeof (options.hostname));
+       }
+       if (strchr (options.hostname, '.')) {
+               if (options.fqdn == FQDN_DISABLE)
+                       options.fqdn = FQDN_BOTH;
+       } else
+               options.fqdn = FQDN_DISABLE;
+       
        if (geteuid ()) {
                logger (LOG_ERR, "you need to be root to run "PACKAGE);
                exit (EXIT_FAILURE);
index 9494c715bfa707f46c7dc08a4abd7337a3014cc3..ffee74448bacd44491e6368cca71f760cf4f94c7 100644 (file)
@@ -407,7 +407,7 @@ static int do_route (const char *ifname,
        /* Do something with metric to satisfy compiler warnings */
        metric = 0;
 
-       dstd = strdup (inet_ntoa (destination));
+       dstd = xstrdup (inet_ntoa (destination));
        if (gateway.s_addr == destination.s_addr)
                logger (LOG_INFO, "%s route to %s/%d",
                                change ? "changing" : del ? "removing" : "adding",
@@ -738,8 +738,8 @@ static int do_route (const char *ifname,
        if (! ifname)
                return -1;
 
-       dstd = strdup (inet_ntoa (destination));
-       gend = strdup (inet_ntoa (netmask));
+       dstd = xstrdup (inet_ntoa (destination));
+       gend = xstrdup (inet_ntoa (netmask));
        if (gateway.s_addr == destination.s_addr)
                logger (LOG_INFO, "%s route to %s (%s) metric %d",
                                change ? "changing" : del ? "removing" : "adding",
index 0fe75174617f76da4c4c43f8c97403e391c6c985..2663443198976d0db55bd7c03755ea6c968c93c0 100644 (file)
--- a/logger.c
+++ b/logger.c
@@ -91,6 +91,10 @@ void logger(int level, const char *fmt, ...)
                fprintf (f, "%s, %s", syslog_level_msg[level], logprefix);
                vfprintf (f, fmt, p);
                fputc ('\n', f);
+
+               /* stdout, stderr may be re-directed to some kind of buffer.
+                * So we always flush to ensure it's written. */
+               fflush (f);
        }
 
        if (level < LOG_DEBUG || level <= loglevel) {
index 1428e3bd19d24b6204dedbf1557f7cdc97a8b1b5..860a98b43927261f250859131fe71c8af2843aaa 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -24,6 +24,7 @@
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
+#include <net/if.h>
 #include <netinet/in_systm.h>
 #include <netinet/in.h>
 #ifndef __OpenBSD__
 #include <net/ethernet.h>
 #endif
 #include <netinet/udp.h>
-#ifndef __linux__
-#include <net/if_types.h>
-#endif
-#include <net/if.h>
 #include <arpa/inet.h>
 #include <errno.h>
 #include <fcntl.h>