/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
1
/*  -*- coding: utf-8 -*- */
2
/*
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
3
 * Plymouth - Read a password from Plymouth and output it
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
4
 * 
237.7.807 by Teddy Hogeborn
Update copyright year
5
 * Copyright © 2010-2022 Teddy Hogeborn
6
 * Copyright © 2010-2022 Björn Påhlsson
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
7
 * 
237.7.455 by Teddy Hogeborn
Alter copyright notices slightly. Actual license is unchanged!
8
 * This file is part of Mandos.
9
 * 
10
 * Mandos is free software: you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 * 
15
 * Mandos is distributed in the hope that it will be useful, but
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * General Public License for more details.
19
 * 
20
 * You should have received a copy of the GNU General Public License
237.7.455 by Teddy Hogeborn
Alter copyright notices slightly. Actual license is unchanged!
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
22
 * 
237.11.2 by Teddy Hogeborn
Change "fukt.bsnet.se" to "recompile.se" throughout.
23
 * Contact the authors at <mandos@recompile.se>.
237.2.207 by Teddy Hogeborn
Update copyright year to "2010" wherever appropriate.
24
 */
25
237.7.781 by Teddy Hogeborn
Fix #include headers
26
#define _GNU_SOURCE		/* program_invocation_short_name,
27
				   vasprintf(), asprintf(),
28
				   TEMP_FAILURE_RETRY() */
29
#include <sys/types.h>		/* sig_atomic_t, pid_t, setuid(),
30
				   geteuid(), setsid() */
31
#include <argp.h>		/* argp_program_version,
32
				   argp_program_bug_address,
33
				   struct argp_option,
34
				   struct argp_state,
35
				   ARGP_ERR_UNKNOWN, struct argp,
36
				   argp_parse(), ARGP_IN_ORDER */
37
#include <stddef.h>		/* NULL, size_t */
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
38
#include <stdbool.h>		/* bool, false, true */
237.7.781 by Teddy Hogeborn
Fix #include headers
39
#include <stdio.h>		/* FILE, fprintf(), vfprintf(),
40
				   vasprintf(), stderr, asprintf(),
41
				   fopen(), fscanf(), fclose(),
42
				   sscanf() */
43
#include <stdarg.h>		/* va_list, va_start(), vfprintf() */
44
#include <errno.h>		/* program_invocation_short_name,
45
				   errno, ENOMEM, EINTR, ENOENT,
46
				   error_t, EINVAL */
47
#include <string.h>		/* strerror(), strdup(), memcmp() */
48
#include <error.h>		/* error() */
49
#include <stdlib.h>		/* free(), getenv(), malloc(),
50
				   reallocarray(), realloc(),
51
				   EXIT_FAILURE, EXIT_SUCCESS */
52
#include <unistd.h>		/* TEMP_FAILURE_RETRY(), setuid(),
53
				   geteuid(), setsid(), chdir(),
54
				   dup2(), STDERR_FILENO,
55
				   STDOUT_FILENO, fork(), _exit(),
56
				   execv(), ssize_t, readlink(),
57
				   close(), read(), access(), X_OK */
58
#include <signal.h>		/* kill(), SIGTERM, struct sigaction,
59
				   sigemptyset(), SIGINT, SIGHUP,
60
				   sigaddset(), SIG_IGN */
61
#include <sys/wait.h>		/* waitpid(), WIFEXITED(),
62
				   WEXITSTATUS(), WIFSIGNALED(),
63
				   WTERMSIG() */
64
#include <iso646.h>		/* not, and, or */
65
#include <sysexits.h>		/* EX_OSERR, EX_USAGE,
66
				   EX_UNAVAILABLE */
67
#include <stdint.h>		/* SIZE_MAX */
68
#include <dirent.h>		/* struct dirent, scandir(),
69
				   alphasort() */
70
#include <inttypes.h>		/* uintmax_t, strtoumax(), SCNuMAX,
71
				   PRIuMAX */
72
#include <sys/stat.h>		/* struct stat, lstat(), S_ISLNK() */
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
73
#include <fcntl.h>		/* open(), O_RDONLY */
237.2.194 by teddy at bsnet
* debian/watch: Also watch for "*.tar.xz" files.
74
#include <argz.h>		/* argz_count(), argz_extract() */
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
75
76
sig_atomic_t interrupted_by_signal = 0;
237.7.675 by Teddy Hogeborn
Add dracut(8) support
77
const char *argp_program_version = "plymouth " VERSION;
78
const char *argp_program_bug_address = "<mandos@recompile.se>";
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
79
80
/* Used by Ubuntu 11.04 (Natty Narwahl) */
237.7.463 by Teddy Hogeborn
Plymouth plugin: Try the newest PID file location
81
const char plymouth_old_old_pid[] = "/dev/.initramfs/plymouth.pid";
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
82
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
237.7.463 by Teddy Hogeborn
Plymouth plugin: Try the newest PID file location
83
const char plymouth_old_pid[] = "/run/initramfs/plymouth.pid";
84
/* Used by Debian 9 (stretch) */
85
const char plymouth_pid[] = "/run/plymouth/pid";
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
86
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
87
const char plymouth_path[] = "/bin/plymouth";
88
const char plymouthd_path[] = "/sbin/plymouthd";
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
89
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
90
					"--mode=boot",
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
91
					"--attach-to-session",
92
					NULL };
237.7.675 by Teddy Hogeborn
Add dracut(8) support
93
bool debug = false;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
94
95
static void termination_handler(__attribute__((unused))int signum){
96
  if(interrupted_by_signal){
97
    return;
98
  }
99
  interrupted_by_signal = 1;
100
}
101
237.7.675 by Teddy Hogeborn
Add dracut(8) support
102
__attribute__((format (gnu_printf, 2, 3), nonnull))
103
int fprintf_plus(FILE *stream, const char *format, ...){
104
  va_list ap;
105
  va_start (ap, format);
106
  fprintf(stream, "Mandos plugin %s: ", program_invocation_short_name);
107
  return vfprintf(stream, format, ap);
108
}
109
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
110
/* Function to use when printing errors */
237.7.81 by teddy at bsnet
* plugins.d/splashy.c (error_plus): Check format string.
111
__attribute__((format (gnu_printf, 3, 4)))
237.7.33 by Teddy Hogeborn
Merge from Björn.
112
void error_plus(int status, int errnum, const char *formatstring,
113
		...){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
114
  va_list ap;
115
  char *text;
116
  int ret;
117
  
118
  va_start(ap, formatstring);
119
  ret = vasprintf(&text, formatstring, ap);
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
120
  if(ret == -1){
237.7.33 by Teddy Hogeborn
Merge from Björn.
121
    fprintf(stderr, "Mandos plugin %s: ",
122
	    program_invocation_short_name);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
123
    vfprintf(stderr, formatstring, ap);
124
    fprintf(stderr, ": ");
125
    fprintf(stderr, "%s\n", strerror(errnum));
126
    error(status, errno, "vasprintf while printing error");
127
    return;
128
  }
129
  fprintf(stderr, "Mandos plugin ");
130
  error(status, errnum, "%s", text);
131
  free(text);
132
}
133
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
134
/* Create prompt string */
135
char *makeprompt(void){
136
  int ret = 0;
137
  char *prompt;
138
  const char *const cryptsource = getenv("cryptsource");
139
  const char *const crypttarget = getenv("crypttarget");
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
140
  const char prompt_start[] = "Unlocking the disk";
141
  const char prompt_end[] = "Enter passphrase";
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
142
  
143
  if(cryptsource == NULL){
144
    if(crypttarget == NULL){
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
145
      ret = asprintf(&prompt, "%s\n%s", prompt_start, prompt_end);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
146
    } else {
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
147
      ret = asprintf(&prompt, "%s (%s)\n%s", prompt_start,
148
		     crypttarget, prompt_end);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
149
    }
150
  } else {
151
    if(crypttarget == NULL){
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
152
      ret = asprintf(&prompt, "%s %s\n%s", prompt_start, cryptsource,
153
		     prompt_end);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
154
    } else {
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
155
      ret = asprintf(&prompt, "%s %s (%s)\n%s", prompt_start,
156
		     cryptsource, crypttarget, prompt_end);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
157
    }
158
  }
159
  if(ret == -1){
160
    return NULL;
161
  }
162
  return prompt;
163
}
164
165
void kill_and_wait(pid_t pid){
166
  TEMP_FAILURE_RETRY(kill(pid, SIGTERM));
167
  TEMP_FAILURE_RETRY(waitpid(pid, NULL, 0));
168
}
169
170
bool become_a_daemon(void){
171
  int ret = setuid(geteuid());
172
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
173
    error_plus(0, errno, "setuid");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
174
  }
175
    
176
  setsid();
177
  ret = chdir("/");
178
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
179
    error_plus(0, errno, "chdir");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
180
    return false;
181
  }
182
  ret = dup2(STDERR_FILENO, STDOUT_FILENO); /* replace our stdout */
183
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
184
    error_plus(0, errno, "dup2");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
185
    return false;
186
  }
187
  return true;
188
}
189
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
190
__attribute__((nonnull (2, 3)))
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
191
bool exec_and_wait(pid_t *pid_return, const char *path,
237.7.675 by Teddy Hogeborn
Add dracut(8) support
192
		   const char * const * const argv, bool interruptable,
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
193
		   bool daemonize){
194
  int status;
195
  int ret;
196
  pid_t pid;
237.7.675 by Teddy Hogeborn
Add dracut(8) support
197
  if(debug){
198
    for(const char * const *arg = argv; *arg != NULL; arg++){
199
      fprintf_plus(stderr, "exec_and_wait arg: %s\n", *arg);
200
    }
201
    fprintf_plus(stderr, "exec_and_wait end of args\n");
202
  }
203
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
204
  pid = fork();
205
  if(pid == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
206
    error_plus(0, errno, "fork");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
207
    return false;
208
  }
209
  if(pid == 0){
210
    /* Child */
211
    if(daemonize){
212
      if(not become_a_daemon()){
213
	_exit(EX_OSERR);
214
      }
215
    }
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
216
    
237.7.381 by Teddy Hogeborn
Client: Fix plymouth agent; broken since 1.7.2.
217
    char **new_argv = malloc(sizeof(const char *));
218
    if(new_argv == NULL){
219
      error_plus(0, errno, "malloc");
220
      _exit(EX_OSERR);
221
    }
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
222
    char **tmp;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
223
    int i = 0;
237.7.438 by Teddy Hogeborn
Add spaces around all '=' signs in all C code.
224
    for (; argv[i] != NULL; i++){
237.7.753 by teddy at recompile
Use reallocarray() if available, or check for overflow
225
#if defined(__GLIBC_PREREQ) and __GLIBC_PREREQ(2, 26)
226
      tmp = reallocarray(new_argv, ((size_t)i + 2),
227
			 sizeof(const char *));
228
#else
229
      if(((size_t)i + 2) > (SIZE_MAX / sizeof(const char *))){
230
	/* overflow */
231
	tmp = NULL;
232
	errno = ENOMEM;
233
      } else {
234
	tmp = realloc(new_argv, ((size_t)i + 2) * sizeof(const char *));
235
      }
236
#endif
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
237
      if(tmp == NULL){
237.7.753 by teddy at recompile
Use reallocarray() if available, or check for overflow
238
	error_plus(0, errno, "reallocarray");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
239
	free(new_argv);
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
240
	_exit(EX_OSERR);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
241
      }
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
242
      new_argv = tmp;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
243
      new_argv[i] = strdup(argv[i]);
244
    }
237.7.10 by Teddy Hogeborn
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
245
    new_argv[i] = NULL;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
246
    
247
    execv(path, (char *const *)new_argv);
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
248
    error_plus(0, errno, "execv");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
249
    _exit(EXIT_FAILURE);
250
  }
251
  if(pid_return != NULL){
252
    *pid_return = pid;
253
  }
254
  do {
255
    ret = waitpid(pid, &status, 0);
256
  } while(ret == -1 and errno == EINTR
257
	  and ((not interrupted_by_signal)
258
	       or (not interruptable)));
259
  if(interrupted_by_signal and interruptable){
237.7.675 by Teddy Hogeborn
Add dracut(8) support
260
    if(debug){
261
      fprintf_plus(stderr, "Interrupted by signal\n");
262
    }
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
263
    return false;
264
  }
265
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
266
    error_plus(0, errno, "waitpid");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
267
    return false;
268
  }
237.7.675 by Teddy Hogeborn
Add dracut(8) support
269
  if(debug){
270
    if(WIFEXITED(status)){
271
      fprintf_plus(stderr, "exec_and_wait exited: %d\n",
272
		   WEXITSTATUS(status));
273
    } else if(WIFSIGNALED(status)) {
274
      fprintf_plus(stderr, "exec_and_wait signaled: %d\n",
275
		   WTERMSIG(status));
276
    }
277
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
278
  if(WIFEXITED(status) and (WEXITSTATUS(status) == 0)){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
279
    return true;
280
  }
281
  return false;
282
}
283
237.7.82 by teddy at bsnet
* plugin-runner.c (add_to_char_array): Added "nonnull" attribute.
284
__attribute__((nonnull))
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
285
int is_plymouth(const struct dirent *proc_entry){
286
  int ret;
287
  {
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
288
    uintmax_t proc_id;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
289
    char *tmp;
290
    errno = 0;
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
291
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
292
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
293
    if(errno != 0 or *tmp != '\0'
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
294
       or proc_id != (uintmax_t)((pid_t)proc_id)){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
295
      return 0;
296
    }
297
  }
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
298
  char exe_target[sizeof(plymouthd_path)];
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
299
  char *exe_link;
300
  ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
301
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
302
    error_plus(0, errno, "asprintf");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
303
    return 0;
304
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
305
  
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
306
  struct stat exe_stat;
307
  ret = lstat(exe_link, &exe_stat);
308
  if(ret == -1){
309
    free(exe_link);
310
    if(errno != ENOENT){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
311
      error_plus(0, errno, "lstat");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
312
    }
313
    return 0;
314
  }
315
  
316
  if(not S_ISLNK(exe_stat.st_mode)
317
     or exe_stat.st_uid != 0
318
     or exe_stat.st_gid != 0){
319
    free(exe_link);
320
    return 0;
321
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
322
  
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
323
  ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
324
  free(exe_link);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
325
  if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
326
      (memcmp(plymouthd_path, exe_target,
327
	      sizeof(plymouthd_path)-1) != 0)){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
328
    return 0;
329
  }
330
  return 1;
331
}
332
333
pid_t get_pid(void){
334
  int ret;
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
335
  uintmax_t proc_id = 0;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
336
  FILE *pidfile = fopen(plymouth_pid, "r");
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
337
  /* Try the new pid file location */
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
338
  if(pidfile != NULL){
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
339
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
340
    if(ret != 1){
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
341
      proc_id = 0;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
342
    }
343
    fclose(pidfile);
344
  }
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
345
  /* Try the old pid file location */
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
346
  if(proc_id == 0){
237.7.462 by Teddy Hogeborn
Plymouth plugin bug fix: Actually try the old PID file location
347
    pidfile = fopen(plymouth_old_pid, "r");
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
348
    if(pidfile != NULL){
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
349
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
350
      if(ret != 1){
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
351
	proc_id = 0;
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
352
      }
353
      fclose(pidfile);
354
    }
355
  }
237.7.463 by Teddy Hogeborn
Plymouth plugin: Try the newest PID file location
356
  /* Try the old old pid file location */
357
  if(proc_id == 0){
358
    pidfile = fopen(plymouth_old_old_pid, "r");
359
    if(pidfile != NULL){
360
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
361
      if(ret != 1){
362
	proc_id = 0;
363
      }
364
      fclose(pidfile);
365
    }
366
  }
237.7.52 by Teddy Hogeborn
* plugins.d/plymouth.c (plymouth_pid): Changed to
367
  /* Look for a plymouth process */
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
368
  if(proc_id == 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
369
    struct dirent **direntries = NULL;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
370
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
371
    if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
372
      error_plus(0, errno, "scandir");
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
373
    }
237.7.218 by Teddy Hogeborn
White space fix: change "if (" to "if(" in C code.
374
    if(ret > 0){
237.7.846 by Teddy Hogeborn
Eliminate compiler warnings
375
      const int num_entries = ret;
376
      for(int i = 0; i < num_entries; i++){
237.7.461 by Teddy Hogeborn
Fix another memory leak in plymouth plugin
377
	if(proc_id == 0){
378
	  ret = sscanf(direntries[i]->d_name, "%" SCNuMAX, &proc_id);
379
	  if(ret < 0){
380
	    error_plus(0, errno, "sscanf");
381
	  }
382
	}
383
	free(direntries[i]);
275.1.1 by Björn Påhlsson
password-prompt: added conflic handling with plymouth daemon.
384
      }
385
    }
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
386
    /* scandir might preallocate for this variable (man page unclear).
237.7.33 by Teddy Hogeborn
Merge from Björn.
387
       even if ret == 0, therefore we need to free it. */
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
388
    free(direntries);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
389
  }
390
  pid_t pid;
237.7.84 by Björn Påhlsson
renamed some foomax values to what they actually represent
391
  pid = (pid_t)proc_id;
392
  if((uintmax_t)pid == proc_id){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
393
    return pid;
394
  }
395
  
396
  return 0;
397
}
398
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
399
char **getargv(pid_t pid){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
400
  int cl_fd;
401
  char *cmdline_filename;
402
  ssize_t sret;
403
  int ret;
404
  
405
  ret = asprintf(&cmdline_filename, "/proc/%" PRIuMAX "/cmdline",
406
		 (uintmax_t)pid);
407
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
408
    error_plus(0, errno, "asprintf");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
409
    return NULL;
410
  }
411
  
412
  /* Open /proc/<pid>/cmdline  */
413
  cl_fd = open(cmdline_filename, O_RDONLY);
414
  free(cmdline_filename);
415
  if(cl_fd == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
416
    error_plus(0, errno, "open");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
417
    return NULL;
418
  }
419
  
420
  size_t cmdline_allocated = 0;
421
  size_t cmdline_len = 0;
422
  char *cmdline = NULL;
423
  char *tmp;
424
  const size_t blocksize = 1024;
425
  do {
426
    /* Allocate more space? */
427
    if(cmdline_len + blocksize > cmdline_allocated){
428
      tmp = realloc(cmdline, cmdline_allocated + blocksize);
429
      if(tmp == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
430
	error_plus(0, errno, "realloc");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
431
	free(cmdline);
432
	close(cl_fd);
433
	return NULL;
434
      }
435
      cmdline = tmp;
436
      cmdline_allocated += blocksize;
437
    }
438
    
439
    /* Read data */
440
    sret = read(cl_fd, cmdline + cmdline_len,
441
		cmdline_allocated - cmdline_len);
442
    if(sret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
443
      error_plus(0, errno, "read");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
444
      free(cmdline);
445
      close(cl_fd);
446
      return NULL;
447
    }
448
    cmdline_len += (size_t)sret;
449
  } while(sret != 0);
450
  ret = close(cl_fd);
451
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
452
    error_plus(0, errno, "close");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
453
    free(cmdline);
454
    return NULL;
455
  }
456
  
457
  /* we got cmdline and cmdline_len, ignore rest... */
237.2.195 by teddy at bsnet
* plugins.d/usplash.c (main): BUG FIX: allocate space for the final
458
  char **argv = malloc((argz_count(cmdline, cmdline_len) + 1)
237.2.194 by teddy at bsnet
* debian/watch: Also watch for "*.tar.xz" files.
459
		       * sizeof(char *)); /* Get number of args */
460
  if(argv == NULL){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
461
    error_plus(0, errno, "argv = malloc()");
237.2.194 by teddy at bsnet
* debian/watch: Also watch for "*.tar.xz" files.
462
    free(cmdline);
463
    return NULL;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
464
  }
237.2.194 by teddy at bsnet
* debian/watch: Also watch for "*.tar.xz" files.
465
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
466
  return argv;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
467
}
468
469
int main(__attribute__((unused))int argc,
470
	 __attribute__((unused))char **argv){
237.7.675 by Teddy Hogeborn
Add dracut(8) support
471
  char *prompt = NULL;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
472
  char *prompt_arg;
473
  pid_t plymouth_command_pid;
474
  int ret;
475
  bool bret;
476
237.7.675 by Teddy Hogeborn
Add dracut(8) support
477
  {
478
    struct argp_option options[] = {
479
      { .name = "prompt", .key = 128, .arg = "PROMPT",
480
	.doc = "The prompt to show" },
481
      { .name = "debug", .key = 129,
482
	.doc = "Debug mode" },
483
      { .name = NULL }
484
    };
485
    
486
    __attribute__((nonnull(3)))
487
    error_t parse_opt (int key, char *arg, __attribute__((unused))
488
		       struct argp_state *state){
489
      errno = 0;
490
      switch (key){
491
      case 128:			/* --prompt */
492
	prompt = arg;
493
	if(debug){
494
	  fprintf_plus(stderr, "Custom prompt \"%s\"\n", prompt);
495
	}
496
	break;
497
      case 129:			/* --debug */
498
	debug = true;
499
	break;
500
      default:
501
	return ARGP_ERR_UNKNOWN;
502
      }
503
      return errno;
504
    }
505
    
506
    struct argp argp = { .options = options, .parser = parse_opt,
507
			 .args_doc = "",
508
			 .doc = "Mandos plymouth -- Read and"
509
			 " output a password" };
510
    ret = argp_parse(&argp, argc, argv, ARGP_IN_ORDER, NULL, NULL);
511
    switch(ret){
512
    case 0:
513
      break;
514
    case ENOMEM:
515
    default:
516
      errno = ret;
517
      error_plus(0, errno, "argp_parse");
518
      return EX_OSERR;
519
    case EINVAL:
520
      error_plus(0, errno, "argp_parse");
521
      return EX_USAGE;
522
    }
523
  }
524
  
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
525
  /* test -x /bin/plymouth */
526
  ret = access(plymouth_path, X_OK);
527
  if(ret == -1){
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
528
    /* Plymouth is probably not installed.  Don't print an error
529
       message, just exit. */
237.7.675 by Teddy Hogeborn
Add dracut(8) support
530
    if(debug){
531
      fprintf_plus(stderr, "Plymouth (%s) not found\n",
532
		   plymouth_path);
533
    }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
534
    exit(EX_UNAVAILABLE);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
535
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
536
  
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
537
  { /* Add signal handlers */
538
    struct sigaction old_action,
539
      new_action = { .sa_handler = termination_handler,
540
		     .sa_flags = 0 };
541
    sigemptyset(&new_action.sa_mask);
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
542
    for(int *sig = (int[]){ SIGINT, SIGHUP, SIGTERM, 0 };
543
	*sig != 0; sig++){
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
544
      ret = sigaddset(&new_action.sa_mask, *sig);
545
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
546
	error_plus(EX_OSERR, errno, "sigaddset");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
547
      }
548
      ret = sigaction(*sig, NULL, &old_action);
549
      if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
550
	error_plus(EX_OSERR, errno, "sigaction");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
551
      }
552
      if(old_action.sa_handler != SIG_IGN){
553
	ret = sigaction(*sig, &new_action, NULL);
554
	if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
555
	  error_plus(EX_OSERR, errno, "sigaction");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
556
	}
557
      }
558
    }
559
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
560
  
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
561
  /* plymouth --ping */
562
  bret = exec_and_wait(&plymouth_command_pid, plymouth_path,
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
563
		       (const char *[])
564
		       { plymouth_path, "--ping", NULL },
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
565
		       true, false);
566
  if(not bret){
567
    if(interrupted_by_signal){
568
      kill_and_wait(plymouth_command_pid);
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
569
      exit(EXIT_FAILURE);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
570
    }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
571
    /* Plymouth is probably not running.  Don't print an error
572
       message, just exit. */
237.7.675 by Teddy Hogeborn
Add dracut(8) support
573
    if(debug){
574
      fprintf_plus(stderr, "Plymouth not running\n");
575
    }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
576
    exit(EX_UNAVAILABLE);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
577
  }
578
  
237.7.675 by Teddy Hogeborn
Add dracut(8) support
579
  if(prompt != NULL){
580
    ret = asprintf(&prompt_arg, "--prompt=%s", prompt);
581
  } else {
582
    char *made_prompt = makeprompt();
583
    ret = asprintf(&prompt_arg, "--prompt=%s", made_prompt);
584
    free(made_prompt);
585
  }
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
586
  if(ret == -1){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
587
    error_plus(EX_OSERR, errno, "asprintf");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
588
  }
589
  
590
  /* plymouth ask-for-password --prompt="$prompt" */
237.7.675 by Teddy Hogeborn
Add dracut(8) support
591
  if(debug){
592
    fprintf_plus(stderr, "Prompting for password via Plymouth\n");
593
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
594
  bret = exec_and_wait(&plymouth_command_pid,
595
		       plymouth_path, (const char *[])
596
		       { plymouth_path, "ask-for-password",
597
			   prompt_arg, NULL },
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
598
		       true, false);
599
  free(prompt_arg);
600
  if(bret){
601
    exit(EXIT_SUCCESS);
602
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
603
  if(not interrupted_by_signal){
604
    /* exec_and_wait failed for some other reason */
605
    exit(EXIT_FAILURE);
606
  }
607
  kill_and_wait(plymouth_command_pid);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
608
  
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
609
  char **plymouthd_argv = NULL;
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
610
  pid_t pid = get_pid();
611
  if(pid == 0){
24.1.174 by Björn Påhlsson
* Makefile (CFLAGS): Added "-lrt" to include real time library.
612
    error_plus(0, 0, "plymouthd pid not found");
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
613
  } else {
614
    plymouthd_argv = getargv(pid);
615
  }
616
  
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
617
  bret = exec_and_wait(NULL, plymouth_path, (const char *[])
618
		       { plymouth_path, "quit", NULL },
619
		       false, false);
620
  if(not bret){
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
621
    if(plymouthd_argv != NULL){
622
      free(*plymouthd_argv);
623
      free(plymouthd_argv);
624
    }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
625
    exit(EXIT_FAILURE);
626
  }
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
627
  bret = exec_and_wait(NULL, plymouthd_path,
628
		       (plymouthd_argv != NULL)
629
		       ? (const char * const *)plymouthd_argv
630
		       : plymouthd_default_argv,
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
631
		       false, true);
237.7.460 by Teddy Hogeborn
Fix memory leak in plymouth plugin
632
  if(plymouthd_argv != NULL){
633
    free(*plymouthd_argv);
634
    free(plymouthd_argv);
635
  }
237.2.199 by teddy at bsnet
* plugins.d/plymouth.c: Break lines, fix whitespace. Remove
636
  if(not bret){
637
    exit(EXIT_FAILURE);
638
  }
639
  exec_and_wait(NULL, plymouth_path, (const char *[])
640
		{ plymouth_path, "show-splash", NULL },
641
		false, false);
24.1.157 by Björn Påhlsson
added the actually plugin file for plymouth
642
  exit(EXIT_FAILURE);
643
}