/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/plymouth.c

  • Committer: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Plymouth - Read a password from Plymouth and output it
4
4
 * 
5
 
 * Copyright © 2010-2016 Teddy Hogeborn
6
 
 * Copyright © 2010-2016 Björn Påhlsson
 
5
 * Copyright © 2010-2011 Teddy Hogeborn
 
6
 * Copyright © 2010-2011 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* asprintf(), TEMP_FAILURE_RETRY() */
36
36
#include <stddef.h>             /* NULL */
37
37
#include <string.h>             /* strchr(), memcmp() */
38
38
#include <stdio.h>              /* asprintf(), perror(), fopen(),
39
 
                                   fscanf(), vasprintf(), fprintf(),
40
 
                                   vfprintf() */
 
39
                                   fscanf(), vasprintf(), fprintf(), vfprintf() */
41
40
#include <unistd.h>             /* close(), readlink(), read(),
42
41
                                   fork(), setsid(), chdir(), dup2(),
43
42
                                   STDERR_FILENO, execv(), access() */
54
53
#include <stdarg.h>             /* va_list, va_start(), ... */
55
54
 
56
55
sig_atomic_t interrupted_by_signal = 0;
57
 
 
58
 
/* Used by Ubuntu 11.04 (Natty Narwahl) */
59
 
const char plymouth_old_pid[] = "/dev/.initramfs/plymouth.pid";
60
 
/* Used by Ubuntu 11.10 (Oneiric Ocelot) */
61
 
const char plymouth_pid[] = "/run/initramfs/plymouth.pid";
62
 
 
 
56
const char plymouth_pid[] = "/dev/.initramfs/plymouth.pid";
63
57
const char plymouth_path[] = "/bin/plymouth";
64
58
const char plymouthd_path[] = "/sbin/plymouthd";
65
59
const char *plymouthd_default_argv[] = {"/sbin/plymouthd",
66
60
                                        "--mode=boot",
67
61
                                        "--attach-to-session",
 
62
                                        "--pid-file="
 
63
                                        "/dev/.initramfs/"
 
64
                                        "plymouth.pid",
68
65
                                        NULL };
69
66
 
70
67
static void termination_handler(__attribute__((unused))int signum){
75
72
}
76
73
 
77
74
/* Function to use when printing errors */
78
 
__attribute__((format (gnu_printf, 3, 4)))
79
 
void error_plus(int status, int errnum, const char *formatstring,
80
 
                ...){
 
75
void error_plus(int status, int errnum, const char *formatstring, ...){
81
76
  va_list ap;
82
77
  char *text;
83
78
  int ret;
84
79
  
85
80
  va_start(ap, formatstring);
86
81
  ret = vasprintf(&text, formatstring, ap);
87
 
  if(ret == -1){
88
 
    fprintf(stderr, "Mandos plugin %s: ",
89
 
            program_invocation_short_name);
 
82
  if (ret == -1){
 
83
    fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
90
84
    vfprintf(stderr, formatstring, ap);
91
85
    fprintf(stderr, ": ");
92
86
    fprintf(stderr, "%s\n", strerror(errnum));
154
148
  return true;
155
149
}
156
150
 
157
 
__attribute__((nonnull (2, 3)))
158
151
bool exec_and_wait(pid_t *pid_return, const char *path,
159
 
                   const char * const *argv, bool interruptable,
 
152
                   const char **argv, bool interruptable,
160
153
                   bool daemonize){
161
154
  int status;
162
155
  int ret;
174
167
      }
175
168
    }
176
169
    
177
 
    char **new_argv = malloc(sizeof(const char *));
178
 
    if(new_argv == NULL){
179
 
      error_plus(0, errno, "malloc");
180
 
      _exit(EX_OSERR);
181
 
    }
 
170
    char **new_argv = NULL;
182
171
    char **tmp;
183
172
    int i = 0;
184
173
    for (; argv[i]!=NULL; i++){
185
 
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 2));
186
 
      if(tmp == NULL){
 
174
      tmp = realloc(new_argv, sizeof(const char *) * ((size_t)i + 1));
 
175
      if (tmp == NULL){
187
176
        error_plus(0, errno, "realloc");
188
177
        free(new_argv);
189
178
        _exit(EX_OSERR);
218
207
  return false;
219
208
}
220
209
 
221
 
__attribute__((nonnull))
222
210
int is_plymouth(const struct dirent *proc_entry){
223
211
  int ret;
224
212
  {
225
 
    uintmax_t proc_id;
 
213
    uintmax_t maxvalue;
226
214
    char *tmp;
227
215
    errno = 0;
228
 
    proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
 
216
    maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
229
217
 
230
218
    if(errno != 0 or *tmp != '\0'
231
 
       or proc_id != (uintmax_t)((pid_t)proc_id)){
 
219
       or maxvalue != (uintmax_t)((pid_t)maxvalue)){
232
220
      return 0;
233
221
    }
234
222
  }
269
257
 
270
258
pid_t get_pid(void){
271
259
  int ret;
272
 
  uintmax_t proc_id = 0;
273
260
  FILE *pidfile = fopen(plymouth_pid, "r");
274
 
  /* Try the new pid file location */
 
261
  uintmax_t maxvalue = 0;
275
262
  if(pidfile != NULL){
276
 
    ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
 
263
    ret = fscanf(pidfile, "%" SCNuMAX, &maxvalue);
277
264
    if(ret != 1){
278
 
      proc_id = 0;
 
265
      maxvalue = 0;
279
266
    }
280
267
    fclose(pidfile);
281
268
  }
282
 
  /* Try the old pid file location */
283
 
  if(proc_id == 0){
284
 
    pidfile = fopen(plymouth_pid, "r");
285
 
    if(pidfile != NULL){
286
 
      ret = fscanf(pidfile, "%" SCNuMAX, &proc_id);
287
 
      if(ret != 1){
288
 
        proc_id = 0;
289
 
      }
290
 
      fclose(pidfile);
291
 
    }
292
 
  }
293
 
  /* Look for a plymouth process */
294
 
  if(proc_id == 0){
 
269
  if(maxvalue == 0){
295
270
    struct dirent **direntries = NULL;
296
271
    ret = scandir("/proc", &direntries, is_plymouth, alphasort);
297
 
    if(ret == -1){
 
272
    if (ret == -1){
298
273
      error_plus(0, errno, "scandir");
299
274
    }
300
 
    if(ret > 0){
301
 
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &proc_id);
302
 
      if(ret < 0){
 
275
    if (ret > 0){
 
276
      ret = sscanf(direntries[0]->d_name, "%" SCNuMAX, &maxvalue);
 
277
      if (ret < 0){
303
278
        error_plus(0, errno, "sscanf");
304
279
      }
305
280
    }
306
281
    /* scandir might preallocate for this variable (man page unclear).
307
 
       even if ret == 0, therefore we need to free it. */
 
282
       even if ret == 0, we need to free it. */
308
283
    free(direntries);
309
284
  }
310
285
  pid_t pid;
311
 
  pid = (pid_t)proc_id;
312
 
  if((uintmax_t)pid == proc_id){
 
286
  pid = (pid_t)maxvalue;
 
287
  if((uintmax_t)pid == maxvalue){
313
288
    return pid;
314
289
  }
315
290
  
316
291
  return 0;
317
292
}
318
293
 
319
 
const char * const * getargv(pid_t pid){
 
294
const char **getargv(pid_t pid){
320
295
  int cl_fd;
321
296
  char *cmdline_filename;
322
297
  ssize_t sret;
383
358
    return NULL;
384
359
  }
385
360
  argz_extract(cmdline, cmdline_len, argv); /* Create argv */
386
 
  return (const char * const *)argv;
 
361
  return (const char **)argv;
387
362
}
388
363
 
389
364
int main(__attribute__((unused))int argc,
464
439
  }
465
440
  kill_and_wait(plymouth_command_pid);
466
441
  
467
 
  const char * const *plymouthd_argv;
 
442
  const char **plymouthd_argv;
468
443
  pid_t pid = get_pid();
469
444
  if(pid == 0){
470
445
    error_plus(0, 0, "plymouthd pid not found");