chiark / gitweb /
ask-password: ignore unknown query file fields
[elogind.git] / src / tty-ask-password-agent.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdbool.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/socket.h>
26 #include <sys/un.h>
27 #include <stddef.h>
28 #include <sys/poll.h>
29 #include <sys/inotify.h>
30 #include <unistd.h>
31 #include <getopt.h>
32 #include <sys/signalfd.h>
33
34 #include "util.h"
35 #include "conf-parser.h"
36 #include "utmp-wtmp.h"
37 #include "socket-util.h"
38 #include "ask-password-api.h"
39
40 static enum {
41         ACTION_LIST,
42         ACTION_QUERY,
43         ACTION_WATCH,
44         ACTION_WALL
45 } arg_action = ACTION_QUERY;
46
47 static bool arg_plymouth = false;
48
49 static int ask_password_plymouth(const char *message, usec_t until, const char *flag_file, char **_passphrase) {
50         int fd = -1, notify = -1;
51         union sockaddr_union sa;
52         char *packet = NULL;
53         ssize_t k;
54         int r, n;
55         struct pollfd pollfd[2];
56         char buffer[LINE_MAX];
57         size_t p = 0;
58         enum {
59                 POLL_SOCKET,
60                 POLL_INOTIFY
61         };
62
63         if (flag_file) {
64                 if ((notify = inotify_init1(IN_CLOEXEC|IN_NONBLOCK)) < 0) {
65                         r = -errno;
66                         goto finish;
67                 }
68
69                 if (inotify_add_watch(notify, flag_file, IN_ATTRIB /* for the link count */) < 0) {
70                         r = -errno;
71                         goto finish;
72                 }
73         }
74
75         if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) {
76                 r = -errno;
77                 goto finish;
78         }
79
80         zero(sa);
81         sa.sa.sa_family = AF_UNIX;
82         strncpy(sa.un.sun_path+1, "/ply-boot-protocol", sizeof(sa.un.sun_path)-1);
83
84         if (connect(fd, &sa.sa, sizeof(sa.un)) < 0) {
85                 r = -errno;
86                 goto finish;
87         }
88
89         if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0) {
90                 r = -ENOMEM;
91                 goto finish;
92         }
93
94         if ((k = loop_write(fd, packet, n+1, true)) != n+1) {
95                 r = k < 0 ? (int) k : -EIO;
96                 goto finish;
97         }
98
99         zero(pollfd);
100         pollfd[POLL_SOCKET].fd = fd;
101         pollfd[POLL_SOCKET].events = POLLIN;
102         pollfd[POLL_INOTIFY].fd = notify;
103         pollfd[POLL_INOTIFY].events = POLLIN;
104
105         for (;;) {
106                 int sleep_for = -1, j;
107
108                 if (until > 0) {
109                         usec_t y;
110
111                         y = now(CLOCK_MONOTONIC);
112
113                         if (y > until) {
114                                 r = -ETIMEDOUT;
115                                 goto finish;
116                         }
117
118                         sleep_for = (int) ((until - y) / USEC_PER_MSEC);
119                 }
120
121                 if (flag_file)
122                         if (access(flag_file, F_OK) < 0) {
123                                 r = -errno;
124                                 goto finish;
125                         }
126
127                 if ((j = poll(pollfd, notify > 0 ? 2 : 1, sleep_for)) < 0) {
128
129                         if (errno == EINTR)
130                                 continue;
131
132                         r = -errno;
133                         goto finish;
134                 } else if (j == 0) {
135                         r = -ETIMEDOUT;
136                         goto finish;
137                 }
138
139                 if (notify > 0 && pollfd[POLL_INOTIFY].revents != 0)
140                         flush_fd(notify);
141
142                 if (pollfd[POLL_SOCKET].revents == 0)
143                         continue;
144
145                 if ((k = read(fd, buffer + p, sizeof(buffer) - p)) <= 0) {
146                         r = k < 0 ? -errno : -EIO;
147                         goto finish;
148                 }
149
150                 p += k;
151
152                 if (p < 1)
153                         continue;
154
155                 if (buffer[0] == 5) {
156                         /* No password, because UI not shown */
157                         r = -ENOENT;
158                         goto finish;
159
160                 } else if (buffer[0] == 2) {
161                         uint32_t size;
162                         char *s;
163
164                         /* One answer */
165                         if (p < 5)
166                                 continue;
167
168                         memcpy(&size, buffer+1, sizeof(size));
169                         if (size+5 > sizeof(buffer)) {
170                                 r = -EIO;
171                                 goto finish;
172                         }
173
174                         if (p-5 < size)
175                                 continue;
176
177                         if (!(s = strndup(buffer + 5, size))) {
178                                 r = -ENOMEM;
179                                 goto finish;
180                         }
181
182                         *_passphrase = s;
183                         break;
184                 } else {
185                         /* Unknown packet */
186                         r = -EIO;
187                         goto finish;
188                 }
189         }
190
191         r = 0;
192
193 finish:
194         if (notify >= 0)
195                 close_nointr_nofail(notify);
196
197         if (fd >= 0)
198                 close_nointr_nofail(fd);
199
200         free(packet);
201
202         return r;
203 }
204
205 static int parse_password(const char *filename, char **wall) {
206         char *socket_name = NULL, *message = NULL, *packet = NULL;
207         uint64_t not_after = 0;
208         unsigned pid = 0;
209         int socket_fd = -1;
210
211         const ConfigItem items[] = {
212                 { "Socket",   config_parse_string,   &socket_name, "Ask" },
213                 { "NotAfter", config_parse_uint64,   &not_after,   "Ask" },
214                 { "Message",  config_parse_string,   &message,     "Ask" },
215                 { "PID",      config_parse_unsigned, &pid,         "Ask" },
216                 { NULL, NULL, NULL, NULL }
217         };
218
219         FILE *f;
220         int r;
221         usec_t n;
222
223         assert(filename);
224
225         if (!(f = fopen(filename, "re"))) {
226
227                 if (errno == ENOENT)
228                         return 0;
229
230                 log_error("open(%s): %m", filename);
231                 return -errno;
232         }
233
234         if ((r = config_parse(filename, f, NULL, items, true, NULL)) < 0) {
235                 log_error("Failed to parse password file %s: %s", filename, strerror(-r));
236                 goto finish;
237         }
238
239         if (!socket_name || not_after <= 0) {
240                 log_error("Invalid password file %s", filename);
241                 r = -EBADMSG;
242                 goto finish;
243         }
244
245         n = now(CLOCK_MONOTONIC);
246         if (n > not_after) {
247                 r = 0;
248                 goto finish;
249         }
250
251         if (arg_action == ACTION_LIST)
252                 printf("'%s' (PID %u)\n", message, pid);
253         else if (arg_action == ACTION_WALL) {
254                 char *_wall;
255
256                 if (asprintf(&_wall,
257                              "%s%sPassword entry required for \'%s\' (PID %u).\r\n"
258                              "Please enter password with the systemd-tty-password-agent tool!",
259                              *wall ? *wall : "",
260                              *wall ? "\r\n\r\n" : "",
261                              message,
262                              pid) < 0) {
263                         log_error("Out of memory");
264                         r = -ENOMEM;
265                         goto finish;
266                 }
267
268                 free(*wall);
269                 *wall = _wall;
270         } else {
271                 union {
272                         struct sockaddr sa;
273                         struct sockaddr_un un;
274                 } sa;
275                 char *password;
276
277                 assert(arg_action == ACTION_QUERY ||
278                        arg_action == ACTION_WATCH);
279
280                 if (access(socket_name, W_OK) < 0) {
281
282                         if (arg_action == ACTION_QUERY)
283                                 log_info("Not querying '%s' (PID %u), lacking privileges.", message, pid);
284
285                         r = 0;
286                         goto finish;
287                 }
288
289                 if (arg_plymouth)
290                         r = ask_password_plymouth(message, not_after, filename, &password);
291                 else
292                         r = ask_password_tty(message, not_after, filename, &password);
293
294                 if (r < 0) {
295                         log_error("Failed to query password: %s", strerror(-r));
296                         goto finish;
297                 }
298
299                 asprintf(&packet, "+%s", password);
300                 free(password);
301
302                 if (!packet) {
303                         log_error("Out of memory");
304                         r = -ENOMEM;
305                         goto finish;
306                 }
307
308                 if ((socket_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
309                         log_error("socket(): %m");
310                         r = -errno;
311                         goto finish;
312                 }
313
314                 zero(sa);
315                 sa.un.sun_family = AF_UNIX;
316                 strncpy(sa.un.sun_path, socket_name, sizeof(sa.un.sun_path));
317
318                 if (sendto(socket_fd, packet, strlen(packet), MSG_NOSIGNAL, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(socket_name)) < 0) {
319                         log_error("Failed to send: %m");
320                         r = -errno;
321                         goto finish;
322                 }
323         }
324
325 finish:
326         fclose(f);
327
328         if (socket_fd >= 0)
329                 close_nointr_nofail(socket_fd);
330
331         free(packet);
332         free(socket_name);
333         free(message);
334
335         return r;
336 }
337
338 static int show_passwords(void) {
339         DIR *d;
340         struct dirent *de;
341         int r = 0;
342
343         if (!(d = opendir("/dev/.systemd/ask-password"))) {
344                 if (errno == ENOENT)
345                         return 0;
346
347                 log_error("opendir(): %m");
348                 return -errno;
349         }
350
351         while ((de = readdir(d))) {
352                 char *p;
353                 int q;
354                 char *wall;
355
356                 if (de->d_type != DT_REG)
357                         continue;
358
359                 if (ignore_file(de->d_name))
360                         continue;
361
362                 if (!startswith(de->d_name, "ask."))
363                         continue;
364
365                 if (!(p = strappend("/dev/.systemd/ask-password/", de->d_name))) {
366                         log_error("Out of memory");
367                         r = -ENOMEM;
368                         goto finish;
369                 }
370
371                 wall = NULL;
372                 if ((q = parse_password(p, &wall)) < 0)
373                         r = q;
374
375                 free(p);
376
377                 if (wall) {
378                         utmp_wall(wall);
379                         free(wall);
380                 }
381         }
382
383 finish:
384         if (d)
385                 closedir(d);
386
387         return r;
388 }
389
390 static int watch_passwords(void) {
391         enum {
392                 FD_INOTIFY,
393                 FD_SIGNAL,
394                 _FD_MAX
395         };
396
397         int notify = -1, signal_fd = -1;
398         struct pollfd pollfd[_FD_MAX];
399         sigset_t mask;
400         int r;
401
402         mkdir_p("/dev/.systemd/ask-password", 0755);
403
404         if ((notify = inotify_init1(IN_CLOEXEC)) < 0) {
405                 r = -errno;
406                 goto finish;
407         }
408
409         if (inotify_add_watch(notify, "/dev/.systemd/ask-password", IN_CLOSE_WRITE|IN_MOVED_TO) < 0) {
410                 r = -errno;
411                 goto finish;
412         }
413
414         assert_se(sigemptyset(&mask) == 0);
415         sigset_add_many(&mask, SIGINT, SIGTERM, -1);
416         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
417
418         if ((signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) {
419                 log_error("signalfd(): %m");
420                 r = -errno;
421                 goto finish;
422         }
423
424         zero(pollfd);
425         pollfd[FD_INOTIFY].fd = notify;
426         pollfd[FD_INOTIFY].events = POLLIN;
427         pollfd[FD_SIGNAL].fd = signal_fd;
428         pollfd[FD_SIGNAL].events = POLLIN;
429
430         for (;;) {
431                 if ((r = show_passwords()) < 0)
432                         break;
433
434                 if (poll(pollfd, _FD_MAX, -1) < 0) {
435
436                         if (errno == EINTR)
437                                 continue;
438
439                         r = -errno;
440                         goto finish;
441                 }
442
443                 if (pollfd[FD_INOTIFY].revents != 0)
444                         flush_fd(notify);
445
446                 if (pollfd[FD_SIGNAL].revents != 0)
447                         break;
448         }
449
450         r = 0;
451
452 finish:
453         if (notify >= 0)
454                 close_nointr_nofail(notify);
455
456         if (signal_fd >= 0)
457                 close_nointr_nofail(signal_fd);
458
459         return r;
460 }
461
462 static int help(void) {
463
464         printf("%s [OPTIONS...]\n\n"
465                "Process system password requests.\n\n"
466                "  -h --help     Show this help\n"
467                "     --list     Show pending password requests\n"
468                "     --query    Process pending password requests\n"
469                "     --watch    Continously process password requests\n"
470                "     --wall     Continously forward password requests to wall\n"
471                "     --plymouth Ask question with Plymouth instead of on TTY\n",
472                program_invocation_short_name);
473
474         return 0;
475 }
476
477 static int parse_argv(int argc, char *argv[]) {
478
479         enum {
480                 ARG_LIST = 0x100,
481                 ARG_QUERY,
482                 ARG_WATCH,
483                 ARG_WALL,
484                 ARG_PLYMOUTH
485         };
486
487         static const struct option options[] = {
488                 { "help",     no_argument, NULL, 'h'          },
489                 { "list",     no_argument, NULL, ARG_LIST     },
490                 { "query",    no_argument, NULL, ARG_QUERY    },
491                 { "watch",    no_argument, NULL, ARG_WATCH    },
492                 { "wall",     no_argument, NULL, ARG_WALL     },
493                 { "plymouth", no_argument, NULL, ARG_PLYMOUTH },
494                 { NULL,    0,           NULL, 0               }
495         };
496
497         int c;
498
499         assert(argc >= 0);
500         assert(argv);
501
502         while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
503
504                 switch (c) {
505
506                 case 'h':
507                         help();
508                         return 0;
509
510                 case ARG_LIST:
511                         arg_action = ACTION_LIST;
512                         break;
513
514                 case ARG_QUERY:
515                         arg_action = ACTION_QUERY;
516                         break;
517
518                 case ARG_WATCH:
519                         arg_action = ACTION_WATCH;
520                         break;
521
522                 case ARG_WALL:
523                         arg_action = ACTION_WALL;
524                         break;
525
526                 case ARG_PLYMOUTH:
527                         arg_plymouth = true;
528                         break;
529
530                 case '?':
531                         return -EINVAL;
532
533                 default:
534                         log_error("Unknown option code %c", c);
535                         return -EINVAL;
536                 }
537         }
538
539         if (optind != argc) {
540                 help();
541                 return -EINVAL;
542         }
543
544         return 1;
545 }
546
547 int main(int argc, char *argv[]) {
548         int r;
549
550         log_parse_environment();
551         log_open();
552
553         if ((r = parse_argv(argc, argv)) <= 0)
554                 goto finish;
555
556         if (arg_action == ACTION_WATCH ||
557             arg_action == ACTION_WALL)
558                 r = watch_passwords();
559         else
560                 r = show_passwords();
561
562 finish:
563         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
564 }