mirror of
https://github.com/systemd/systemd.git
synced 2025-02-04 21:47:31 +03:00
ask-password: modernizations
This commit is contained in:
parent
036eeac5a1
commit
981e4cd325
@ -56,9 +56,10 @@ int ask_password_tty(
|
|||||||
char **_passphrase) {
|
char **_passphrase) {
|
||||||
|
|
||||||
struct termios old_termios, new_termios;
|
struct termios old_termios, new_termios;
|
||||||
char passphrase[LINE_MAX];
|
char passphrase[LINE_MAX], *x;
|
||||||
size_t p = 0;
|
size_t p = 0;
|
||||||
int r, ttyfd = -1, notify = -1;
|
int r;
|
||||||
|
_cleanup_close_ int ttyfd = -1, notify = -1;
|
||||||
struct pollfd pollfd[2];
|
struct pollfd pollfd[2];
|
||||||
bool reset_tty = false;
|
bool reset_tty = false;
|
||||||
bool silent_mode = false;
|
bool silent_mode = false;
|
||||||
@ -72,7 +73,8 @@ int ask_password_tty(
|
|||||||
assert(_passphrase);
|
assert(_passphrase);
|
||||||
|
|
||||||
if (flag_file) {
|
if (flag_file) {
|
||||||
if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
|
notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK);
|
||||||
|
if (notify < 0) {
|
||||||
r = -errno;
|
r = -errno;
|
||||||
goto finish;
|
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) {
|
if (tcgetattr(ttyfd, &old_termios) < 0) {
|
||||||
r = -errno;
|
r = -errno;
|
||||||
@ -138,8 +141,8 @@ int ask_password_tty(
|
|||||||
goto finish;
|
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)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -156,8 +159,8 @@ int ask_password_tty(
|
|||||||
if (pollfd[POLL_TTY].revents == 0)
|
if (pollfd[POLL_TTY].revents == 0)
|
||||||
continue;
|
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)
|
if (errno == EINTR || errno == EAGAIN)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -221,26 +224,19 @@ int ask_password_tty(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
passphrase[p] = 0;
|
x = strndup(passphrase, p);
|
||||||
|
if (!x) {
|
||||||
if (!(*_passphrase = strdup(passphrase))) {
|
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*_passphrase = x;
|
||||||
r = 0;
|
r = 0;
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
safe_close(notify);
|
if (ttyfd >= 0 && reset_tty) {
|
||||||
|
loop_write(ttyfd, "\n", 1, false);
|
||||||
if (ttyfd >= 0) {
|
tcsetattr(ttyfd, TCSADRAIN, &old_termios);
|
||||||
|
|
||||||
if (reset_tty) {
|
|
||||||
loop_write(ttyfd, "\n", 1, false);
|
|
||||||
tcsetattr(ttyfd, TCSADRAIN, &old_termios);
|
|
||||||
}
|
|
||||||
|
|
||||||
safe_close(ttyfd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
@ -314,12 +310,12 @@ int ask_password_agent(
|
|||||||
|
|
||||||
char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
|
char temp[] = "/run/systemd/ask-password/tmp.XXXXXX";
|
||||||
char final[sizeof(temp)] = "";
|
char final[sizeof(temp)] = "";
|
||||||
int fd = -1, r;
|
_cleanup_fclose_ FILE *f = NULL;
|
||||||
FILE *f = NULL;
|
_cleanup_free_ char *socket_name = NULL;
|
||||||
char *socket_name = NULL;
|
_cleanup_close_ int socket_fd = -1, signal_fd = -1, fd = -1;
|
||||||
int socket_fd = -1, signal_fd = -1;
|
|
||||||
sigset_t mask, oldmask;
|
sigset_t mask, oldmask;
|
||||||
struct pollfd pollfd[_FD_MAX];
|
struct pollfd pollfd[_FD_MAX];
|
||||||
|
int r;
|
||||||
|
|
||||||
assert(_passphrases);
|
assert(_passphrases);
|
||||||
|
|
||||||
@ -338,7 +334,8 @@ int ask_password_agent(
|
|||||||
|
|
||||||
fchmod(fd, 0644);
|
fchmod(fd, 0644);
|
||||||
|
|
||||||
if (!(f = fdopen(fd, "w"))) {
|
f = fdopen(fd, "w");
|
||||||
|
if (!f) {
|
||||||
log_error("Failed to allocate FILE: %m");
|
log_error("Failed to allocate FILE: %m");
|
||||||
r = -errno;
|
r = -errno;
|
||||||
goto finish;
|
goto finish;
|
||||||
@ -346,13 +343,15 @@ int ask_password_agent(
|
|||||||
|
|
||||||
fd = -1;
|
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");
|
log_error("signalfd(): %m");
|
||||||
r = -errno;
|
r = -errno;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((socket_fd = create_socket(&socket_name)) < 0) {
|
socket_fd = create_socket(&socket_name);
|
||||||
|
if (socket_fd < 0) {
|
||||||
r = socket_fd;
|
r = socket_fd;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
@ -421,8 +420,8 @@ int ask_password_agent(
|
|||||||
goto finish;
|
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)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -459,8 +458,8 @@ int ask_password_agent(
|
|||||||
msghdr.msg_control = &control;
|
msghdr.msg_control = &control;
|
||||||
msghdr.msg_controllen = sizeof(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 ||
|
if (errno == EAGAIN ||
|
||||||
errno == EINTR)
|
errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
@ -525,18 +524,8 @@ int ask_password_agent(
|
|||||||
r = 0;
|
r = 0;
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
safe_close(fd);
|
if (socket_name)
|
||||||
|
|
||||||
if (socket_name) {
|
|
||||||
unlink(socket_name);
|
unlink(socket_name);
|
||||||
free(socket_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
safe_close(socket_fd);
|
|
||||||
safe_close(signal_fd);
|
|
||||||
|
|
||||||
if (f)
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
unlink(temp);
|
unlink(temp);
|
||||||
|
|
||||||
@ -556,18 +545,16 @@ int ask_password_auto(const char *message, const char *icon, usec_t until, bool
|
|||||||
int r;
|
int r;
|
||||||
char *s = NULL, **l = NULL;
|
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;
|
return r;
|
||||||
|
|
||||||
l = strv_new(s, NULL);
|
r = strv_consume(&l, s);
|
||||||
free(s);
|
if (r < 0)
|
||||||
|
return r;
|
||||||
if (!l)
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
*_passphrases = l;
|
*_passphrases = l;
|
||||||
return r;
|
return r;
|
||||||
|
|
||||||
} else
|
} else
|
||||||
return ask_password_agent(message, icon, until, accept_cached, _passphrases);
|
return ask_password_agent(message, icon, until, accept_cached, _passphrases);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user