/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

Merge from trunk; Better parsing of numbers in command line options,
new "mandos=off" kernel parameter, plus other smaller fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
 
 * Passprompt - Read a password from the terminal and print it
 
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
5
5
 * Copyright © 2008,2009 Teddy Hogeborn
6
6
 * Copyright © 2008,2009 Björn Påhlsson
81
81
    };
82
82
    
83
83
    error_t parse_opt (int key, char *arg, struct argp_state *state) {
84
 
      /* Get the INPUT argument from `argp_parse', which we know is a
85
 
         pointer to our plugin list pointer. */
86
84
      switch (key) {
87
85
      case 'p':
88
86
        prefix = arg;
91
89
        debug = true;
92
90
        break;
93
91
      case ARGP_KEY_ARG:
94
 
        argp_usage (state);
 
92
        argp_usage(state);
95
93
        break;
96
94
      case ARGP_KEY_END:
97
95
        break;
105
103
                         .args_doc = "",
106
104
                         .doc = "Mandos password-prompt -- Read and"
107
105
                         " output a password" };
108
 
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
109
 
    if (ret == ARGP_ERR_UNKNOWN){
 
106
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
107
    if(ret == ARGP_ERR_UNKNOWN){
110
108
      fprintf(stderr, "Unknown error while parsing arguments\n");
111
109
      return EXIT_FAILURE;
112
110
    }
113
111
  }
114
112
  
115
 
  if (debug){
 
113
  if(debug){
116
114
    fprintf(stderr, "Starting %s\n", argv[0]);
117
115
  }
118
 
  if (debug){
 
116
  if(debug){
119
117
    fprintf(stderr, "Storing current terminal attributes\n");
120
118
  }
121
119
  
122
 
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
 
120
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
123
121
    perror("tcgetattr");
124
122
    return EXIT_FAILURE;
125
123
  }
133
131
    perror("sigaction");
134
132
    return EXIT_FAILURE;
135
133
  }
136
 
  if (old_action.sa_handler != SIG_IGN){
 
134
  if(old_action.sa_handler != SIG_IGN){
137
135
    ret = sigaction(SIGINT, &new_action, NULL);
138
136
    if(ret == -1){
139
137
      perror("sigaction");
145
143
    perror("sigaction");
146
144
    return EXIT_FAILURE;
147
145
  }
148
 
  if (old_action.sa_handler != SIG_IGN){
 
146
  if(old_action.sa_handler != SIG_IGN){
149
147
    ret = sigaction(SIGHUP, &new_action, NULL);
150
148
    if(ret == -1){
151
149
      perror("sigaction");
157
155
    perror("sigaction");
158
156
    return EXIT_FAILURE;
159
157
  }
160
 
  if (old_action.sa_handler != SIG_IGN){
 
158
  if(old_action.sa_handler != SIG_IGN){
161
159
    ret = sigaction(SIGTERM, &new_action, NULL);
162
160
    if(ret == -1){
163
161
      perror("sigaction");
166
164
  }
167
165
  
168
166
  
169
 
  if (debug){
 
167
  if(debug){
170
168
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
171
169
  }
172
170
  
173
171
  t_new = t_old;
174
172
  t_new.c_lflag &= ~ECHO;
175
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
173
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
176
174
    perror("tcsetattr-echo");
177
175
    return EXIT_FAILURE;
178
176
  }
179
177
 
180
 
  if (debug){
 
178
  if(debug){
181
179
    fprintf(stderr, "Waiting for input from stdin \n");
182
180
  }
183
181
  while(true){
184
 
    if (quit_now){
 
182
    if(quit_now){
185
183
      if(debug){
186
184
        fprintf(stderr, "Interrupted by signal, exiting.\n");
187
185
      }
213
211
      }
214
212
    }
215
213
    ret = getline(&buffer, &n, stdin);
216
 
    if (ret > 0){
 
214
    if(ret > 0){
217
215
      status = EXIT_SUCCESS;
218
216
      /* Make n = data size instead of allocated buffer size */
219
217
      n = (size_t)ret;
234
232
      }
235
233
      break;
236
234
    }
237
 
    if (ret < 0){
238
 
      if (errno != EINTR and not feof(stdin)){
 
235
    if(ret < 0){
 
236
      if(errno != EINTR and not feof(stdin)){
239
237
        perror("getline");
240
238
        status = EXIT_FAILURE;
241
239
        break;
253
251
  
254
252
  free(buffer);
255
253
  
256
 
  if (debug){
 
254
  if(debug){
257
255
    fprintf(stderr, "Restoring terminal attributes\n");
258
256
  }
259
 
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
257
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
260
258
    perror("tcsetattr+echo");
261
259
  }
262
260
  
263
 
  if (debug){
 
261
  if(debug){
264
262
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
265
263
            status);
266
264
  }