chiark / gitweb /
Be more verbose when bind or listen fails
[elogind.git] / src / shared / ask-password-api.c
index d091a22deafcccc04830c4954f9ba79cda65574f..8d03f4ad0994f001a5c1a6a246ac01f56e350064 100644 (file)
@@ -56,9 +56,10 @@ int ask_password_tty(
                 char **_passphrase) {
 
         struct termios old_termios, new_termios;
-        char passphrase[LINE_MAX];
+        char passphrase[LINE_MAX], *x;
         size_t p = 0;
-        int r, ttyfd = -1, notify = -1;
+        int r;
+        _cleanup_close_ int ttyfd = -1, notify = -1;
         struct pollfd pollfd[2];
         bool reset_tty = false;
         bool silent_mode = false;
@@ -72,7 +73,8 @@ int ask_password_tty(
         assert(_passphrase);
 
         if (flag_file) {
-                if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
+                notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
+                if (notify < 0) {
                         r = -errno;
                         goto finish;
                 }
@@ -83,7 +85,8 @@ int ask_password_tty(
                 }
         }
 
-        if ((ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC)) >= 0) {
+        ttyfd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC);
+        if (ttyfd >= 0) {
 
                 if (tcgetattr(ttyfd, &old_termios) < 0) {
                         r = -errno;
@@ -109,7 +112,6 @@ int ask_password_tty(
         }
 
         zero(pollfd);
-
         pollfd[POLL_TTY].fd = ttyfd >= 0 ? ttyfd : STDIN_FILENO;
         pollfd[POLL_TTY].events = POLLIN;
         pollfd[POLL_INOTIFY].fd = notify;
@@ -139,8 +141,8 @@ int ask_password_tty(
                                 goto finish;
                         }
 
-                if ((k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
-
+                k = poll(pollfd, notify > 0 ? 2 : 1, sleep_for);
+                if (k < 0) {
                         if (errno == EINTR)
                                 continue;
 
@@ -157,8 +159,8 @@ int ask_password_tty(
                 if (pollfd[POLL_TTY].revents == 0)
                         continue;
 
-                if ((n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1)) < 0) {
-
+                n = read(ttyfd >= 0 ? ttyfd : STDIN_FILENO, &c, 1);
+                if (n < 0) {
                         if (errno == EINTR || errno == EAGAIN)
                                 continue;
 
@@ -208,6 +210,11 @@ int ask_password_tty(
                         if (ttyfd >= 0)
                                 loop_write(ttyfd, "(no echo) ", 10, false);
                 } else {
+                        if (p >= sizeof(passphrase)-1) {
+                                loop_write(ttyfd, "\a", 1, false);
+                                continue;
+                        }
+
                         passphrase[p++] = c;
 
                         if (!silent_mode && ttyfd >= 0)
@@ -217,27 +224,19 @@ int ask_password_tty(
                 }
         }
 
-        passphrase[p] = 0;
-
-        if (!(*_passphrase = strdup(passphrase))) {
+        x = strndup(passphrase, p);
+        if (!x) {
                 r = -ENOMEM;
                 goto finish;
         }
 
+        *_passphrase = x;
         r = 0;
 
 finish:
-        if (notify >= 0)
-                close_nointr_nofail(notify);
-
-        if (ttyfd >= 0) {
-
-                if (reset_tty) {
-                        loop_write(ttyfd, "\n", 1, false);
-                        tcsetattr(ttyfd, TCSADRAIN, &old_termios);
-                }
-
-                close_nointr_nofail(ttyfd);
+        if (ttyfd >= 0 && reset_tty) {
+                loop_write(ttyfd, "\n", 1, false);
+                tcsetattr(ttyfd, TCSADRAIN, &old_termios);
         }
 
         return r;
@@ -248,10 +247,12 @@ static int create_socket(char **name) {
         union {
                 struct sockaddr sa;
                 struct sockaddr_un un;
-        } sa;
-        int one = 1, r;
+        } sa = {
+                .un.sun_family = AF_UNIX,
+        };
+        int one = 1;
+        int r = 0;
         char *c;
-        mode_t u;
 
         assert(name);
 
@@ -261,18 +262,15 @@ static int create_socket(char **name) {
                 return -errno;
         }
 
-        zero(sa);
-        sa.un.sun_family = AF_UNIX;
-        snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1,
-                 "/run/systemd/ask-password/sck.%llu", random_ull());
+        snprintf(sa.un.sun_path, sizeof(sa.un.sun_path)-1, "/run/systemd/ask-password/sck.%" PRIx64, random_u64());
 
-        u = umask(0177);
-        r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
-        umask(u);
+        RUN_WITH_UMASK(0177) {
+                r = bind(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path));
+        }
 
         if (r < 0) {
                 r = -errno;
-                log_error("bind() failed: %m");
+                log_error("bind(%s) failed: %m", sa.un.sun_path);
                 goto fail;
         }
 
@@ -292,7 +290,7 @@ static int create_socket(char **name) {
         return fd;
 
 fail:
-        close_nointr_nofail(fd);
+        safe_close(fd);
 
         return r;
 }
@@ -300,6 +298,7 @@ fail:
 int ask_password_agent(
                 const char *message,
                 const char *icon,
+                const char *id,
                 usec_t until,
                 bool accept_cached,
                 char ***_passphrases) {
@@ -312,13 +311,12 @@ int ask_password_agent(
 
         char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
         char final[sizeof(temp)] = "";
-        int fd = -1, r;
-        FILE *f = NULL;
-        char *socket_name = NULL;
-        int socket_fd = -1, signal_fd = -1;
+        _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_free_ char *socket_name = NULL;
+        _cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1;
         sigset_t mask, oldmask;
         struct pollfd pollfd[_FD_MAX];
-        mode_t u;
+        int r;
 
         assert(_passphrases);
 
@@ -328,10 +326,7 @@ int ask_password_agent(
 
         mkdir_p_label("/run/systemd/ask-password", 0755);
 
-        u = umask(0022);
-        fd = mkostemp(temp, O_CLOEXEC|O_CREAT|O_WRONLY);
-        umask(u);
-
+        fd = mkostemp_safe(temp, O_WRONLY|O_CLOEXEC);
         if (fd < 0) {
                 log_error("Failed to create password file: %m");
                 r = -errno;
@@ -340,7 +335,8 @@ int ask_password_agent(
 
         fchmod(fd, 0644);
 
-        if (!(f = fdopen(fd, "w"))) {
+        f = fdopen(fd, "w");
+        if (!f) {
                 log_error("Failed to allocate FILE: %m");
                 r = -errno;
                 goto finish;
@@ -348,27 +344,29 @@ int ask_password_agent(
 
         fd = -1;
 
-        if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
+        signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
+        if (signal_fd < 0) {
                 log_error("signalfd(): %m");
                 r = -errno;
                 goto finish;
         }
 
-        if ((socket_fd = create_socket(&socket_name)) < 0) {
+        socket_fd = create_socket(&socket_name);
+        if (socket_fd < 0) {
                 r = socket_fd;
                 goto finish;
         }
 
         fprintf(f,
                 "[Ask]\n"
-                "PID=%lu\n"
+                "PID="PID_FMT"\n"
                 "Socket=%s\n"
                 "AcceptCached=%i\n"
-                "NotAfter=%llu\n",
-                (unsigned long) getpid(),
+                "NotAfter="USEC_FMT"\n",
+                getpid(),
                 socket_name,
                 accept_cached ? 1 : 0,
-                (unsigned long long) until);
+                until);
 
         if (message)
                 fprintf(f, "Message=%s\n", message);
@@ -376,6 +374,9 @@ int ask_password_agent(
         if (icon)
                 fprintf(f, "Icon=%s\n", icon);
 
+        if (id)
+                fprintf(f, "Id=%s\n", id);
+
         fflush(f);
 
         if (ferror(f)) {
@@ -423,8 +424,8 @@ int ask_password_agent(
                         goto finish;
                 }
 
-                if ((k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1)) < 0) {
-
+                k = poll(pollfd, _FD_MAX, until > 0 ? (int) ((until-t)/USEC_PER_MSEC) : -1);
+                if (k < 0) {
                         if (errno == EINTR)
                                 continue;
 
@@ -461,8 +462,8 @@ int ask_password_agent(
                 msghdr.msg_control = &control;
                 msghdr.msg_controllen = sizeof(control);
 
-                if ((n = recvmsg(socket_fd, &msghdr, 0)) < 0) {
-
+                n = recvmsg(socket_fd, &msghdr, 0);
+                if (n < 0) {
                         if (errno == EAGAIN ||
                             errno == EINTR)
                                 continue;
@@ -527,22 +528,8 @@ int ask_password_agent(
         r = 0;
 
 finish:
-        if (fd >= 0)
-                close_nointr_nofail(fd);
-
-        if (socket_name) {
+        if (socket_name)
                 unlink(socket_name);
-                free(socket_name);
-        }
-
-        if (socket_fd >= 0)
-                close_nointr_nofail(socket_fd);
-
-        if (signal_fd >= 0)
-                close_nointr_nofail(signal_fd);
-
-        if (f)
-                fclose(f);
 
         unlink(temp);
 
@@ -554,7 +541,8 @@ finish:
         return r;
 }
 
-int ask_password_auto(const char *message, const char *icon, usec_t until, bool accept_cached, char ***_passphrases) {
+int ask_password_auto(const char *message, const char *icon, const char *id,
+                      usec_t until, bool accept_cached, char ***_passphrases) {
         assert(message);
         assert(_passphrases);
 
@@ -562,18 +550,16 @@ int ask_password_auto(const char *message, const char *icon, usec_t until, bool
                 int r;
                 char *s = NULL, **l = NULL;
 
-                if ((r = ask_password_tty(message, until, NULL, &s)) < 0)
+                r = ask_password_tty(message, until, NULL, &s);
+                if (r < 0)
                         return r;
 
-                l = strv_new(s, NULL);
-                free(s);
-
-                if (!l)
-                        return -ENOMEM;
+                r = strv_consume(&l, s);
+                if (r < 0)
+                        return r;
 
                 *_passphrases = l;
                 return r;
-
         } else
-                return ask_password_agent(message, icon, until, accept_cached, _passphrases);
+                return ask_password_agent(message, icon, id, until, accept_cached, _passphrases);
 }