/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: 2017-08-20 14:14:14 UTC
  • Revision ID: teddy@recompile.se-20170820141414-m034xuebg7ccaeui
Add some more restrictions to the systemd service file.

* mandos.service ([Service]/ProtectKernelTunables): New; set to "yes".
  ([Service]/ProtectControlGroups): - '' -

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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 Björn Påhlsson
 
5
 * Copyright © 2008-2017 Teddy Hogeborn
 
6
 * Copyright © 2008-2017 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@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* getline(), asprintf() */
67
67
int signal_received;
68
68
bool debug = false;
69
69
const char *argp_program_version = "password-prompt " VERSION;
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
70
const char *argp_program_bug_address = "<mandos@recompile.se>";
71
71
 
72
72
/* Needed for conflict resolution */
73
73
const char plymouth_name[] = "plymouthd";
74
74
 
75
75
/* Function to use when printing errors */
 
76
__attribute__((format (gnu_printf, 3, 4)))
76
77
void error_plus(int status, int errnum, const char *formatstring,
77
78
                ...){
78
79
  va_list ap;
81
82
  
82
83
  va_start(ap, formatstring);
83
84
  ret = vasprintf(&text, formatstring, ap);
84
 
  if (ret == -1){
 
85
  if(ret == -1){
85
86
    fprintf(stderr, "Mandos plugin %s: ",
86
87
            program_invocation_short_name);
87
88
    vfprintf(stderr, formatstring, ap);
88
 
    fprintf(stderr, ": ");
89
 
    fprintf(stderr, "%s\n", strerror(errnum));
 
89
    fprintf(stderr, ": %s\n", strerror(errnum));
90
90
    error(status, errno, "vasprintf while printing error");
91
91
    return;
92
92
  }
109
109
     from the terminal.  Password-prompt will exit if it detects
110
110
     plymouth since plymouth performs the same functionality.
111
111
   */
 
112
  __attribute__((nonnull))
112
113
  int is_plymouth(const struct dirent *proc_entry){
113
114
    int ret;
114
115
    int cl_fd;
115
116
    {
116
 
      uintmax_t maxvalue;
 
117
      uintmax_t proc_id;
117
118
      char *tmp;
118
119
      errno = 0;
119
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
120
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
120
121
      
121
122
      if(errno != 0 or *tmp != '\0'
122
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
123
         or proc_id != (uintmax_t)((pid_t)proc_id)){
123
124
        return 0;
124
125
      }
125
126
    }
128
129
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
129
130
                   proc_entry->d_name);
130
131
    if(ret == -1){
131
 
      error(0, errno, "asprintf");
 
132
      error_plus(0, errno, "asprintf");
132
133
      return 0;
133
134
    }
134
135
    
137
138
    free(cmdline_filename);
138
139
    if(cl_fd == -1){
139
140
      if(errno != ENOENT){
140
 
        error(0, errno, "open");
 
141
        error_plus(0, errno, "open");
141
142
      }
142
143
      return 0;
143
144
    }
154
155
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
155
156
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
156
157
          if(tmp == NULL){
157
 
            error(0, errno, "realloc");
 
158
            error_plus(0, errno, "realloc");
158
159
            free(cmdline);
159
160
            close(cl_fd);
160
161
            return 0;
167
168
        sret = read(cl_fd, cmdline + cmdline_len,
168
169
                    cmdline_allocated - cmdline_len);
169
170
        if(sret == -1){
170
 
          error(0, errno, "read");
 
171
          error_plus(0, errno, "read");
171
172
          free(cmdline);
172
173
          close(cl_fd);
173
174
          return 0;
176
177
      } while(sret != 0);
177
178
      ret = close(cl_fd);
178
179
      if(ret == -1){
179
 
        error(0, errno, "close");
 
180
        error_plus(0, errno, "close");
180
181
        free(cmdline);
181
182
        return 0;
182
183
      }
211
212
  struct dirent **direntries = NULL;
212
213
  int ret;
213
214
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
214
 
  if (ret == -1){
215
 
    error(1, errno, "scandir");
 
215
  if(ret == -1){
 
216
    error_plus(1, errno, "scandir");
 
217
  }
 
218
  {
 
219
    int i = ret;
 
220
    while(i--){
 
221
      free(direntries[i]);
 
222
    }
216
223
  }
217
224
  free(direntries);
218
225
  return ret > 0;
249
256
      { .name = NULL }
250
257
    };
251
258
    
 
259
    __attribute__((nonnull(3)))
252
260
    error_t parse_opt (int key, char *arg, struct argp_state *state){
253
261
      errno = 0;
254
262
      switch (key){
290
298
    case ENOMEM:
291
299
    default:
292
300
      errno = ret;
293
 
      error(0, errno, "argp_parse");
 
301
      error_plus(0, errno, "argp_parse");
294
302
      return EX_OSERR;
295
303
    case EINVAL:
296
304
      return EX_USAGE;
301
309
    fprintf(stderr, "Starting %s\n", argv[0]);
302
310
  }
303
311
 
304
 
  if (conflict_detection()){
 
312
  if(conflict_detection()){
305
313
    if(debug){
306
314
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
307
315
    }
314
322
  
315
323
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
316
324
    int e = errno;
317
 
    error(0, errno, "tcgetattr");
 
325
    error_plus(0, errno, "tcgetattr");
318
326
    switch(e){
319
327
    case EBADF:
320
328
    case ENOTTY:
327
335
  sigemptyset(&new_action.sa_mask);
328
336
  ret = sigaddset(&new_action.sa_mask, SIGINT);
329
337
  if(ret == -1){
330
 
    error(0, errno, "sigaddset");
 
338
    error_plus(0, errno, "sigaddset");
331
339
    return EX_OSERR;
332
340
  }
333
341
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
334
342
  if(ret == -1){
335
 
    error(0, errno, "sigaddset");
 
343
    error_plus(0, errno, "sigaddset");
336
344
    return EX_OSERR;
337
345
  }
338
346
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
339
347
  if(ret == -1){
340
 
    error(0, errno, "sigaddset");
 
348
    error_plus(0, errno, "sigaddset");
341
349
    return EX_OSERR;
342
350
  }
343
351
  /* Need to check if the handler is SIG_IGN before handling:
346
354
  */
347
355
  ret = sigaction(SIGINT, NULL, &old_action);
348
356
  if(ret == -1){
349
 
    error(0, errno, "sigaction");
 
357
    error_plus(0, errno, "sigaction");
350
358
    return EX_OSERR;
351
359
  }
352
360
  if(old_action.sa_handler != SIG_IGN){
353
361
    ret = sigaction(SIGINT, &new_action, NULL);
354
362
    if(ret == -1){
355
 
      error(0, errno, "sigaction");
 
363
      error_plus(0, errno, "sigaction");
356
364
      return EX_OSERR;
357
365
    }
358
366
  }
359
367
  ret = sigaction(SIGHUP, NULL, &old_action);
360
368
  if(ret == -1){
361
 
    error(0, errno, "sigaction");
 
369
    error_plus(0, errno, "sigaction");
362
370
    return EX_OSERR;
363
371
  }
364
372
  if(old_action.sa_handler != SIG_IGN){
365
373
    ret = sigaction(SIGHUP, &new_action, NULL);
366
374
    if(ret == -1){
367
 
      error(0, errno, "sigaction");
 
375
      error_plus(0, errno, "sigaction");
368
376
      return EX_OSERR;
369
377
    }
370
378
  }
371
379
  ret = sigaction(SIGTERM, NULL, &old_action);
372
380
  if(ret == -1){
373
 
    error(0, errno, "sigaction");
 
381
    error_plus(0, errno, "sigaction");
374
382
    return EX_OSERR;
375
383
  }
376
384
  if(old_action.sa_handler != SIG_IGN){
377
385
    ret = sigaction(SIGTERM, &new_action, NULL);
378
386
    if(ret == -1){
379
 
      error(0, errno, "sigaction");
 
387
      error_plus(0, errno, "sigaction");
380
388
      return EX_OSERR;
381
389
    }
382
390
  }
390
398
  t_new.c_lflag &= ~(tcflag_t)ECHO;
391
399
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
392
400
    int e = errno;
393
 
    error(0, errno, "tcsetattr-echo");
 
401
    error_plus(0, errno, "tcsetattr-echo");
394
402
    switch(e){
395
403
    case EBADF:
396
404
    case ENOTTY:
460
468
        sret = write(STDOUT_FILENO, buffer + written, n - written);
461
469
        if(sret < 0){
462
470
          int e = errno;
463
 
          error(0, errno, "write");
 
471
          error_plus(0, errno, "write");
464
472
          switch(e){
465
473
          case EBADF:
466
474
          case EFAULT:
482
490
      sret = close(STDOUT_FILENO);
483
491
      if(sret == -1){
484
492
        int e = errno;
485
 
        error(0, errno, "close");
 
493
        error_plus(0, errno, "close");
486
494
        switch(e){
487
495
        case EBADF:
488
496
          status = EX_OSFILE;
498
506
    if(sret < 0){
499
507
      int e = errno;
500
508
      if(errno != EINTR and not feof(stdin)){
501
 
        error(0, errno, "getline");
 
509
        error_plus(0, errno, "getline");
502
510
        switch(e){
503
511
        case EBADF:
504
512
          status = EX_UNAVAILABLE;
 
513
          break;
505
514
        case EIO:
506
515
        case EINVAL:
507
516
        default:
527
536
    fprintf(stderr, "Restoring terminal attributes\n");
528
537
  }
529
538
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
530
 
    error(0, errno, "tcsetattr+echo");
 
539
    error_plus(0, errno, "tcsetattr+echo");
531
540
  }
532
541
  
533
542
  if(quit_now){
535
544
    old_action.sa_handler = SIG_DFL;
536
545
    ret = sigaction(signal_received, &old_action, NULL);
537
546
    if(ret == -1){
538
 
      error(0, errno, "sigaction");
 
547
      error_plus(0, errno, "sigaction");
539
548
    }
540
549
    raise(signal_received);
541
550
  }