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