Fix permissions of newly created /etc/resolv.conf
authorMarcel Holtmann <marcel@holtmann.org>
Thu, 24 Dec 2009 15:05:53 +0000 (07:05 -0800)
committerMarcel Holtmann <marcel@holtmann.org>
Thu, 24 Dec 2009 15:05:53 +0000 (07:05 -0800)
src/resolver.c

index fa6880c..59adeab 100644 (file)
@@ -338,30 +338,46 @@ static int resolvfile_append(const char *interface, const char *domain,
 {
        char *cmd;
        int fd, len, err;
+       mode_t old_umask;
 
        DBG("interface %s server %s", interface, server);
 
        if (interface == NULL)
                return -ENOENT;
 
+       old_umask = umask(022);
+
        fd = open("/etc/resolv.conf", O_RDWR | O_CREAT,
                                        S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
-       if (fd < 0)
-               return -errno;
+       if (fd < 0) {
+               err = -errno;
+               goto done;
+       }
+
+       if (ftruncate(fd, 0) < 0) {
+               err = -errno;
+               goto failed;
+       }
 
-       err = ftruncate(fd, 0);
+       err = 0;
 
        cmd = g_strdup_printf("# Generated by Connection Manager\n"
                                                "options edns0\n"
                                                "nameserver %s\n", server);
 
        len = write(fd, cmd, strlen(cmd));
+       if (len < 0)
+               err = -errno;
 
        g_free(cmd);
 
+failed:
        close(fd);
 
-       return 0;
+done:
+       umask(old_umask);
+
+       return err;
 }
 
 static int resolvfile_remove(const char *interface, const char *domain,