/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/password-prompt.c

  • Committer: Teddy Hogeborn
  • Date: 2010-11-16 18:10:22 UTC
  • mfrom: (237.5.1 mandos-release)
  • Revision ID: teddy@fukt.bsnet.se-20101116181022-oz0dahg0q4jnyb0y
Merge from Björn

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
 
 * Copyright © 2008,2009 Teddy Hogeborn
6
 
 * Copyright © 2008,2009 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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
22
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
 
#define _GNU_SOURCE             /* getline() */
 
25
#define _GNU_SOURCE             /* getline(), asprintf() */
26
26
 
27
27
#include <termios.h>            /* struct termios, tcsetattr(),
28
28
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
29
#include <unistd.h>             /* struct termios, tcsetattr(),
30
30
                                   STDIN_FILENO, TCSAFLUSH,
31
 
                                   tcgetattr(), ECHO */
 
31
                                   tcgetattr(), ECHO, readlink() */
32
32
#include <signal.h>             /* sig_atomic_t, raise(), struct
33
33
                                   sigaction, sigemptyset(),
34
34
                                   sigaction(), sigaddset(), SIGINT,
35
35
                                   SIGQUIT, SIGHUP, SIGTERM,
36
36
                                   raise() */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
 
#include <sys/types.h>          /* ssize_t */
 
38
#include <sys/types.h>          /* ssize_t, struct dirent, pid_t, ssize_t */
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
40
 
                                   getenv() */
 
40
                                   getenv(), free() */
 
41
#include <dirent.h>             /* scandir(), alphasort() */
41
42
#include <stdio.h>              /* fprintf(), stderr, getline(),
42
 
                                   stdin, feof(), perror(), fputc()
 
43
                                   stdin, feof(), fputc()
43
44
                                */
44
45
#include <errno.h>              /* errno, EBADF, ENOTTY, EINVAL,
45
46
                                   EFAULT, EFBIG, EIO, ENOSPC, EINTR
46
47
                                */
 
48
#include <error.h>              /* error() */
47
49
#include <iso646.h>             /* or, not */
48
50
#include <stdbool.h>            /* bool, false, true */
49
 
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
 
51
#include <inttypes.h>           /* strtoumax() */
 
52
#include <sys/stat.h>           /* struct stat, lstat() */
 
53
#include <string.h>             /* strlen, rindex, memcmp */
50
54
#include <argp.h>               /* struct argp_option, struct
51
55
                                   argp_state, struct argp,
52
56
                                   argp_parse(), error_t,
61
65
const char *argp_program_version = "password-prompt " VERSION;
62
66
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
63
67
 
 
68
/* Needed for conflic resolution */
 
69
const char plymouthd_path[] = "/sbin/plymouth";
 
70
 
 
71
 
64
72
static void termination_handler(int signum){
65
73
  if(quit_now){
66
74
    return;
69
77
  signal_received = signum;
70
78
}
71
79
 
 
80
bool conflict_detection(void){
 
81
 
 
82
  /* plymouth conflicts with password-promt since both want to control the
 
83
     associated terminal. Password-prompt exit since plymouth perferms the same
 
84
     functionallity.
 
85
   */
 
86
  int is_plymouth(const struct dirent *proc_entry){
 
87
    int ret;
 
88
    {
 
89
      uintmax_t maxvalue;
 
90
      char *tmp;
 
91
      errno = 0;
 
92
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
93
      
 
94
      if(errno != 0 or *tmp != '\0'
 
95
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
96
        return 0;
 
97
      }
 
98
    }
 
99
    char exe_target[sizeof(plymouthd_path)];
 
100
    char *exe_link;
 
101
    ret = asprintf(&exe_link, "/proc/%s/exe", proc_entry->d_name);
 
102
    if(ret == -1){
 
103
      error(0, errno, "asprintf");
 
104
      return 0;
 
105
    }
 
106
  
 
107
    struct stat exe_stat;
 
108
    ret = lstat(exe_link, &exe_stat);
 
109
    if(ret == -1){
 
110
      free(exe_link);
 
111
      if(errno != ENOENT){
 
112
        error(0, errno, "lstat");
 
113
      }
 
114
      return 0;
 
115
    }
 
116
  
 
117
    if(not S_ISLNK(exe_stat.st_mode)
 
118
       or exe_stat.st_uid != 0
 
119
       or exe_stat.st_gid != 0){
 
120
      free(exe_link);
 
121
      return 0;
 
122
    }
 
123
  
 
124
    ssize_t sret = readlink(exe_link, exe_target, sizeof(exe_target));
 
125
    free(exe_link);
 
126
    if((sret != (ssize_t)sizeof(plymouthd_path)-1) or
 
127
       (memcmp(plymouthd_path, exe_target,
 
128
               sizeof(plymouthd_path)-1) != 0)){
 
129
      return 0;
 
130
    }
 
131
    return 1;
 
132
  }
 
133
 
 
134
  struct dirent **direntries;
 
135
  int ret;
 
136
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
 
137
  if (ret == -1){
 
138
    error(1, errno, "scandir");
 
139
  }
 
140
  if (ret < 0){
 
141
    return 1;
 
142
  } else {
 
143
    return 0;
 
144
  }
 
145
}
 
146
 
 
147
 
72
148
int main(int argc, char **argv){
73
 
  ssize_t ret;
 
149
  ssize_t sret;
 
150
  int ret;
74
151
  size_t n;
75
152
  struct termios t_new, t_old;
76
153
  char *buffer = NULL;
139
216
    case ENOMEM:
140
217
    default:
141
218
      errno = ret;
142
 
      perror("argp_parse");
 
219
      error(0, errno, "argp_parse");
143
220
      return EX_OSERR;
144
221
    case EINVAL:
145
222
      return EX_USAGE;
149
226
  if(debug){
150
227
    fprintf(stderr, "Starting %s\n", argv[0]);
151
228
  }
 
229
 
 
230
  if (conflict_detection()){
 
231
    if(debug){
 
232
      fprintf(stderr, "Stopping %s because of conflict", argv[0]);
 
233
    }
 
234
    return EXIT_FAILURE;
 
235
  }
 
236
  
152
237
  if(debug){
153
238
    fprintf(stderr, "Storing current terminal attributes\n");
154
239
  }
155
240
  
156
241
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
157
242
    int e = errno;
158
 
    perror("tcgetattr");
 
243
    error(0, errno, "tcgetattr");
159
244
    switch(e){
160
245
    case EBADF:
161
246
    case ENOTTY:
168
253
  sigemptyset(&new_action.sa_mask);
169
254
  ret = sigaddset(&new_action.sa_mask, SIGINT);
170
255
  if(ret == -1){
171
 
    perror("sigaddset");
 
256
    error(0, errno, "sigaddset");
172
257
    return EX_OSERR;
173
258
  }
174
259
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
175
260
  if(ret == -1){
176
 
    perror("sigaddset");
 
261
    error(0, errno, "sigaddset");
177
262
    return EX_OSERR;
178
263
  }
179
264
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
180
265
  if(ret == -1){
181
 
    perror("sigaddset");
 
266
    error(0, errno, "sigaddset");
182
267
    return EX_OSERR;
183
268
  }
184
269
  /* Need to check if the handler is SIG_IGN before handling:
187
272
  */
188
273
  ret = sigaction(SIGINT, NULL, &old_action);
189
274
  if(ret == -1){
190
 
    perror("sigaction");
 
275
    error(0, errno, "sigaction");
191
276
    return EX_OSERR;
192
277
  }
193
278
  if(old_action.sa_handler != SIG_IGN){
194
279
    ret = sigaction(SIGINT, &new_action, NULL);
195
280
    if(ret == -1){
196
 
      perror("sigaction");
 
281
      error(0, errno, "sigaction");
197
282
      return EX_OSERR;
198
283
    }
199
284
  }
200
285
  ret = sigaction(SIGHUP, NULL, &old_action);
201
286
  if(ret == -1){
202
 
    perror("sigaction");
 
287
    error(0, errno, "sigaction");
203
288
    return EX_OSERR;
204
289
  }
205
290
  if(old_action.sa_handler != SIG_IGN){
206
291
    ret = sigaction(SIGHUP, &new_action, NULL);
207
292
    if(ret == -1){
208
 
      perror("sigaction");
 
293
      error(0, errno, "sigaction");
209
294
      return EX_OSERR;
210
295
    }
211
296
  }
212
297
  ret = sigaction(SIGTERM, NULL, &old_action);
213
298
  if(ret == -1){
214
 
    perror("sigaction");
 
299
    error(0, errno, "sigaction");
215
300
    return EX_OSERR;
216
301
  }
217
302
  if(old_action.sa_handler != SIG_IGN){
218
303
    ret = sigaction(SIGTERM, &new_action, NULL);
219
304
    if(ret == -1){
220
 
      perror("sigaction");
 
305
      error(0, errno, "sigaction");
221
306
      return EX_OSERR;
222
307
    }
223
308
  }
231
316
  t_new.c_lflag &= ~(tcflag_t)ECHO;
232
317
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
233
318
    int e = errno;
234
 
    perror("tcsetattr-echo");
 
319
    error(0, errno, "tcsetattr-echo");
235
320
    switch(e){
236
321
    case EBADF:
237
322
    case ENOTTY:
286
371
        }
287
372
      }
288
373
    }
289
 
    ret = getline(&buffer, &n, stdin);
290
 
    if(ret > 0){
 
374
    sret = getline(&buffer, &n, stdin);
 
375
    if(sret > 0){
291
376
      status = EXIT_SUCCESS;
292
377
      /* Make n = data size instead of allocated buffer size */
293
 
      n = (size_t)ret;
 
378
      n = (size_t)sret;
294
379
      /* Strip final newline */
295
380
      if(n > 0 and buffer[n-1] == '\n'){
296
381
        buffer[n-1] = '\0';     /* not strictly necessary */
298
383
      }
299
384
      size_t written = 0;
300
385
      while(written < n){
301
 
        ret = write(STDOUT_FILENO, buffer + written, n - written);
302
 
        if(ret < 0){
 
386
        sret = write(STDOUT_FILENO, buffer + written, n - written);
 
387
        if(sret < 0){
303
388
          int e = errno;
304
 
          perror("write");
 
389
          error(0, errno, "write");
305
390
          switch(e){
306
391
          case EBADF:
307
392
          case EFAULT:
318
403
          }
319
404
          break;
320
405
        }
321
 
        written += (size_t)ret;
 
406
        written += (size_t)sret;
322
407
      }
323
 
      ret = close(STDOUT_FILENO);
324
 
      if(ret == -1){
 
408
      sret = close(STDOUT_FILENO);
 
409
      if(sret == -1){
325
410
        int e = errno;
326
 
        perror("close");
 
411
        error(0, errno, "close");
327
412
        switch(e){
328
413
        case EBADF:
329
414
          status = EX_OSFILE;
336
421
      }
337
422
      break;
338
423
    }
339
 
    if(ret < 0){
 
424
    if(sret < 0){
340
425
      int e = errno;
341
426
      if(errno != EINTR and not feof(stdin)){
342
 
        perror("getline");
 
427
        error(0, errno, "getline");
343
428
        switch(e){
344
429
        case EBADF:
345
430
          status = EX_UNAVAILABLE;
352
437
        break;
353
438
      }
354
439
    }
355
 
    /* if(ret == 0), then the only sensible thing to do is to retry to
 
440
    /* if(sret == 0), then the only sensible thing to do is to retry to
356
441
       read from stdin */
357
442
    fputc('\n', stderr);
358
443
    if(debug and not quit_now){
368
453
    fprintf(stderr, "Restoring terminal attributes\n");
369
454
  }
370
455
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
371
 
    perror("tcsetattr+echo");
 
456
    error(0, errno, "tcsetattr+echo");
372
457
  }
373
458
  
374
459
  if(quit_now){
376
461
    old_action.sa_handler = SIG_DFL;
377
462
    ret = sigaction(signal_received, &old_action, NULL);
378
463
    if(ret == -1){
379
 
      perror("sigaction");
 
464
      error(0, errno, "sigaction");
380
465
    }
381
466
    raise(signal_received);
382
467
  }