/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/passprompt.c

  • Committer: Teddy Hogeborn
  • Date: 2008-07-21 01:52:54 UTC
  • mfrom: (15.1.2 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080721015254-5ut0m7j1r4t5x7hj
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8 -*- */
2
 
/*
3
 
 * Passprompt - Read a password from the terminal and print it
4
 
 *
5
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
6
 
 * 
7
 
 * This program is free software: you can redistribute it and/or
8
 
 * modify it under the terms of the GNU General Public License as
9
 
 * published by the Free Software Foundation, either version 3 of the
10
 
 * License, or (at your option) any later version.
11
 
 * 
12
 
 * This program is distributed in the hope that it will be useful, but
13
 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * General Public License for more details.
16
 
 * 
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program.  If not, see
19
 
 * <http://www.gnu.org/licenses/>.
20
 
 * 
21
 
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
22
 
 * <https://www.fukt.bsnet.se/~teddy/>.
23
 
 */
24
 
 
25
1
#define _GNU_SOURCE             /* getline() */
26
 
 
 
2
#define _FORTIFY_SOURCE 2
27
3
#include <termios.h>            /* struct termios, tcsetattr(),
28
4
                                   TCSAFLUSH, tcgetattr(), ECHO */
29
5
#include <unistd.h>             /* struct termios, tcsetattr(),
35
11
                                   SIGQUIT, SIGHUP, SIGTERM */
36
12
#include <stddef.h>             /* NULL, size_t */
37
13
#include <sys/types.h>          /* ssize_t */
38
 
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
39
 
                                   getopt_long */
 
14
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE */
40
15
#include <stdio.h>              /* fprintf(), stderr, getline(),
41
16
                                   stdin, feof(), perror(), fputc(),
42
 
                                   stdout, getopt_long */
 
17
                                   stdout */
43
18
#include <errno.h>              /* errno, EINVAL */
44
19
#include <iso646.h>             /* or, not */
45
20
#include <stdbool.h>            /* bool, false, true */
46
21
#include <string.h>             /* strlen, rindex, strncmp, strcmp */
47
 
#include <getopt.h>             /* getopt_long */
48
22
 
49
23
volatile bool quit_now = false;
50
24
bool debug = false;
51
25
 
52
 
void termination_handler(__attribute__((unused))int signum){
 
26
void termination_handler(int signum){
53
27
  quit_now = true;
54
28
}
55
29
 
56
30
int main(int argc, char **argv){
57
 
  ssize_t ret;
 
31
  ssize_t ret = -1;
58
32
  size_t n;
59
33
  struct termios t_new, t_old;
60
34
  char *buffer = NULL;
61
 
  char *prefix = NULL;
62
35
  int status = EXIT_SUCCESS;
63
36
  struct sigaction old_action,
64
37
    new_action = { .sa_handler = termination_handler,
65
38
                   .sa_flags = 0 };
66
 
 
67
 
  while (true){
68
 
    static struct option long_options[] = {
69
 
      {"debug", no_argument, (int *)&debug, 1},
70
 
      {"prefix", required_argument, 0, 'p'},
71
 
      {0, 0, 0, 0} };
72
 
 
73
 
    int option_index = 0;
74
 
    ret = getopt_long (argc, argv, "p:", long_options, &option_index);
75
 
 
76
 
    if (ret == -1){
77
 
      break;
78
 
    }
79
 
      
80
 
    switch(ret){
81
 
    case 0:
82
 
      break;
83
 
    case 'p':
84
 
      prefix = optarg;
85
 
      break;
86
 
    default:
87
 
      fprintf(stderr, "bad arguments\n");
88
 
      exit(EXIT_FAILURE);
89
 
    }
90
 
  }
91
 
      
 
39
  const char db[] = "--debug";
 
40
  char *basename = rindex(argv[0], '/');
 
41
  if(basename == NULL){
 
42
    basename = argv[0];
 
43
  } else {
 
44
    basename++;
 
45
  }
 
46
 
 
47
  char *program_name = malloc(strlen(basename) + sizeof(db));
 
48
  if (program_name == NULL){
 
49
    perror("argv[0]");
 
50
    return EXIT_FAILURE;
 
51
  }
 
52
    
 
53
  program_name[0] = '\0';
 
54
    
 
55
  for (int i = 1; i < argc; i++){
 
56
    if (not strncmp(argv[i], db, 5)){
 
57
      strcat(strcat(strcat(program_name, db ), "="), basename);
 
58
      if(not strcmp(argv[i], db) or not strcmp(argv[i], program_name)){
 
59
        debug = true;
 
60
      }
 
61
    }
 
62
  }
 
63
  free(program_name);
 
64
 
92
65
  if (debug){
93
66
    fprintf(stderr, "Starting %s\n", argv[0]);
94
67
  }
102
75
  
103
76
  sigemptyset(&new_action.sa_mask);
104
77
  sigaddset(&new_action.sa_mask, SIGINT);
 
78
  sigaddset(&new_action.sa_mask, SIGQUIT);
105
79
  sigaddset(&new_action.sa_mask, SIGHUP);
106
80
  sigaddset(&new_action.sa_mask, SIGTERM);
107
81
  sigaction(SIGINT, NULL, &old_action);
108
82
  if (old_action.sa_handler != SIG_IGN)
109
83
    sigaction(SIGINT, &new_action, NULL);
 
84
  sigaction(SIGQUIT, NULL, &old_action);
 
85
  if (old_action.sa_handler != SIG_IGN)
 
86
    sigaction(SIGQUIT, &new_action, NULL);
110
87
  sigaction(SIGHUP, NULL, &old_action);
111
88
  if (old_action.sa_handler != SIG_IGN)
112
89
    sigaction(SIGHUP, &new_action, NULL);
134
111
      status = EXIT_FAILURE;
135
112
      break;
136
113
    }
137
 
 
138
 
    if(prefix){
139
 
      fprintf(stderr, "%s Password: ", prefix);
140
 
    } else {
141
 
      fprintf(stderr, "Password: ");
142
 
    }      
 
114
    fprintf(stderr, "Password: ");
143
115
    ret = getline(&buffer, &n, stdin);
144
116
    if (ret > 0){
145
117
      fprintf(stdout, "%s", buffer);