/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: 2019-03-03 16:20:02 UTC
  • Revision ID: teddy@recompile.se-20190303162002-y0ezr98f71d0jfro
mandos-ctl: Refactor; test PrintTableCmd instead of TableOfClients

* mandos-ctl (Test_TableOfClients): Removed.
  (TestCmd): New abstract class for tests for command classes.
  (TestPrintTableCmd): New.

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
7
 
 * 
8
 
 * This program is free software: you can redistribute it and/or
9
 
 * modify it under the terms of the GNU General Public License as
10
 
 * published by the Free Software Foundation, either version 3 of the
11
 
 * License, or (at your option) any later version.
12
 
 * 
13
 
 * This program is distributed in the hope that it will be useful, but
 
5
 * Copyright © 2008-2019 Teddy Hogeborn
 
6
 * Copyright © 2008-2019 Björn Påhlsson
 
7
 * 
 
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
14
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
15
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
18
 * General Public License for more details.
17
19
 * 
18
20
 * You should have received a copy of the GNU General Public License
19
 
 * along with this program.  If not, see
20
 
 * <http://www.gnu.org/licenses/>.
 
21
 * along with Mandos.  If not, see <http://www.gnu.org/licenses/>.
21
22
 * 
22
23
 * Contact the authors at <mandos@recompile.se>.
23
24
 */
82
83
  
83
84
  va_start(ap, formatstring);
84
85
  ret = vasprintf(&text, formatstring, ap);
85
 
  if (ret == -1){
 
86
  if(ret == -1){
86
87
    fprintf(stderr, "Mandos plugin %s: ",
87
88
            program_invocation_short_name);
88
89
    vfprintf(stderr, formatstring, ap);
89
 
    fprintf(stderr, ": ");
90
 
    fprintf(stderr, "%s\n", strerror(errnum));
 
90
    fprintf(stderr, ": %s\n", strerror(errnum));
91
91
    error(status, errno, "vasprintf while printing error");
92
92
    return;
93
93
  }
110
110
     from the terminal.  Password-prompt will exit if it detects
111
111
     plymouth since plymouth performs the same functionality.
112
112
   */
 
113
  __attribute__((nonnull))
113
114
  int is_plymouth(const struct dirent *proc_entry){
114
115
    int ret;
115
116
    int cl_fd;
116
117
    {
117
 
      uintmax_t maxvalue;
 
118
      uintmax_t proc_id;
118
119
      char *tmp;
119
120
      errno = 0;
120
 
      maxvalue = strtoumax(proc_entry->d_name, &tmp, 10);
 
121
      proc_id = strtoumax(proc_entry->d_name, &tmp, 10);
121
122
      
122
123
      if(errno != 0 or *tmp != '\0'
123
 
         or maxvalue != (uintmax_t)((pid_t)maxvalue)){
 
124
         or proc_id != (uintmax_t)((pid_t)proc_id)){
124
125
        return 0;
125
126
      }
126
127
    }
129
130
    ret = asprintf(&cmdline_filename, "/proc/%s/cmdline",
130
131
                   proc_entry->d_name);
131
132
    if(ret == -1){
132
 
      error(0, errno, "asprintf");
 
133
      error_plus(0, errno, "asprintf");
133
134
      return 0;
134
135
    }
135
136
    
138
139
    free(cmdline_filename);
139
140
    if(cl_fd == -1){
140
141
      if(errno != ENOENT){
141
 
        error(0, errno, "open");
 
142
        error_plus(0, errno, "open");
142
143
      }
143
144
      return 0;
144
145
    }
155
156
        if(cmdline_len + blocksize + 1 > cmdline_allocated){
156
157
          tmp = realloc(cmdline, cmdline_allocated + blocksize + 1);
157
158
          if(tmp == NULL){
158
 
            error(0, errno, "realloc");
 
159
            error_plus(0, errno, "realloc");
159
160
            free(cmdline);
160
161
            close(cl_fd);
161
162
            return 0;
168
169
        sret = read(cl_fd, cmdline + cmdline_len,
169
170
                    cmdline_allocated - cmdline_len);
170
171
        if(sret == -1){
171
 
          error(0, errno, "read");
 
172
          error_plus(0, errno, "read");
172
173
          free(cmdline);
173
174
          close(cl_fd);
174
175
          return 0;
177
178
      } while(sret != 0);
178
179
      ret = close(cl_fd);
179
180
      if(ret == -1){
180
 
        error(0, errno, "close");
 
181
        error_plus(0, errno, "close");
181
182
        free(cmdline);
182
183
        return 0;
183
184
      }
212
213
  struct dirent **direntries = NULL;
213
214
  int ret;
214
215
  ret = scandir("/proc", &direntries, is_plymouth, alphasort);
215
 
  if (ret == -1){
216
 
    error(1, errno, "scandir");
 
216
  if(ret == -1){
 
217
    error_plus(1, errno, "scandir");
 
218
  }
 
219
  {
 
220
    int i = ret;
 
221
    while(i--){
 
222
      free(direntries[i]);
 
223
    }
217
224
  }
218
225
  free(direntries);
219
226
  return ret > 0;
250
257
      { .name = NULL }
251
258
    };
252
259
    
 
260
    __attribute__((nonnull(3)))
253
261
    error_t parse_opt (int key, char *arg, struct argp_state *state){
254
262
      errno = 0;
255
263
      switch (key){
266
274
        argp_state_help(state, state->out_stream,
267
275
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
268
276
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
 
277
        __builtin_unreachable();
269
278
      case -3:                  /* --usage */
270
279
        argp_state_help(state, state->out_stream,
271
280
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
 
281
        __builtin_unreachable();
272
282
      case 'V':                 /* --version */
273
283
        fprintf(state->out_stream, "%s\n", argp_program_version);
274
284
        exit(argp_err_exit_status);
291
301
    case ENOMEM:
292
302
    default:
293
303
      errno = ret;
294
 
      error(0, errno, "argp_parse");
 
304
      error_plus(0, errno, "argp_parse");
295
305
      return EX_OSERR;
296
306
    case EINVAL:
297
307
      return EX_USAGE;
302
312
    fprintf(stderr, "Starting %s\n", argv[0]);
303
313
  }
304
314
 
305
 
  if (conflict_detection()){
 
315
  if(conflict_detection()){
306
316
    if(debug){
307
317
      fprintf(stderr, "Stopping %s because of conflict\n", argv[0]);
308
318
    }
315
325
  
316
326
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
317
327
    int e = errno;
318
 
    error(0, errno, "tcgetattr");
 
328
    error_plus(0, errno, "tcgetattr");
319
329
    switch(e){
320
330
    case EBADF:
321
331
    case ENOTTY:
328
338
  sigemptyset(&new_action.sa_mask);
329
339
  ret = sigaddset(&new_action.sa_mask, SIGINT);
330
340
  if(ret == -1){
331
 
    error(0, errno, "sigaddset");
 
341
    error_plus(0, errno, "sigaddset");
332
342
    return EX_OSERR;
333
343
  }
334
344
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
335
345
  if(ret == -1){
336
 
    error(0, errno, "sigaddset");
 
346
    error_plus(0, errno, "sigaddset");
337
347
    return EX_OSERR;
338
348
  }
339
349
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
340
350
  if(ret == -1){
341
 
    error(0, errno, "sigaddset");
 
351
    error_plus(0, errno, "sigaddset");
342
352
    return EX_OSERR;
343
353
  }
344
354
  /* Need to check if the handler is SIG_IGN before handling:
347
357
  */
348
358
  ret = sigaction(SIGINT, NULL, &old_action);
349
359
  if(ret == -1){
350
 
    error(0, errno, "sigaction");
 
360
    error_plus(0, errno, "sigaction");
351
361
    return EX_OSERR;
352
362
  }
353
363
  if(old_action.sa_handler != SIG_IGN){
354
364
    ret = sigaction(SIGINT, &new_action, NULL);
355
365
    if(ret == -1){
356
 
      error(0, errno, "sigaction");
 
366
      error_plus(0, errno, "sigaction");
357
367
      return EX_OSERR;
358
368
    }
359
369
  }
360
370
  ret = sigaction(SIGHUP, NULL, &old_action);
361
371
  if(ret == -1){
362
 
    error(0, errno, "sigaction");
 
372
    error_plus(0, errno, "sigaction");
363
373
    return EX_OSERR;
364
374
  }
365
375
  if(old_action.sa_handler != SIG_IGN){
366
376
    ret = sigaction(SIGHUP, &new_action, NULL);
367
377
    if(ret == -1){
368
 
      error(0, errno, "sigaction");
 
378
      error_plus(0, errno, "sigaction");
369
379
      return EX_OSERR;
370
380
    }
371
381
  }
372
382
  ret = sigaction(SIGTERM, NULL, &old_action);
373
383
  if(ret == -1){
374
 
    error(0, errno, "sigaction");
 
384
    error_plus(0, errno, "sigaction");
375
385
    return EX_OSERR;
376
386
  }
377
387
  if(old_action.sa_handler != SIG_IGN){
378
388
    ret = sigaction(SIGTERM, &new_action, NULL);
379
389
    if(ret == -1){
380
 
      error(0, errno, "sigaction");
 
390
      error_plus(0, errno, "sigaction");
381
391
      return EX_OSERR;
382
392
    }
383
393
  }
391
401
  t_new.c_lflag &= ~(tcflag_t)ECHO;
392
402
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
393
403
    int e = errno;
394
 
    error(0, errno, "tcsetattr-echo");
 
404
    error_plus(0, errno, "tcsetattr-echo");
395
405
    switch(e){
396
406
    case EBADF:
397
407
    case ENOTTY:
461
471
        sret = write(STDOUT_FILENO, buffer + written, n - written);
462
472
        if(sret < 0){
463
473
          int e = errno;
464
 
          error(0, errno, "write");
 
474
          error_plus(0, errno, "write");
465
475
          switch(e){
466
476
          case EBADF:
467
477
          case EFAULT:
483
493
      sret = close(STDOUT_FILENO);
484
494
      if(sret == -1){
485
495
        int e = errno;
486
 
        error(0, errno, "close");
 
496
        error_plus(0, errno, "close");
487
497
        switch(e){
488
498
        case EBADF:
489
499
          status = EX_OSFILE;
499
509
    if(sret < 0){
500
510
      int e = errno;
501
511
      if(errno != EINTR and not feof(stdin)){
502
 
        error(0, errno, "getline");
 
512
        error_plus(0, errno, "getline");
503
513
        switch(e){
504
514
        case EBADF:
505
515
          status = EX_UNAVAILABLE;
 
516
          break;
506
517
        case EIO:
507
518
        case EINVAL:
508
519
        default:
528
539
    fprintf(stderr, "Restoring terminal attributes\n");
529
540
  }
530
541
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
531
 
    error(0, errno, "tcsetattr+echo");
 
542
    error_plus(0, errno, "tcsetattr+echo");
532
543
  }
533
544
  
534
545
  if(quit_now){
536
547
    old_action.sa_handler = SIG_DFL;
537
548
    ret = sigaction(signal_received, &old_action, NULL);
538
549
    if(ret == -1){
539
 
      error(0, errno, "sigaction");
 
550
      error_plus(0, errno, "sigaction");
540
551
    }
541
552
    raise(signal_received);
542
553
  }