/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: 2009-01-13 04:29:35 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090113042935-ztxe47n42q5z767b
* plugin-runner.c (main): Bug fix; do not accept a "d" character after
                          user ID or group ID numbers.  Bug fix: use
                          "%u" when printing PID of coredumped plugin,
                          not "%d".
* plugins.d/password-prompt.c (main): Remove comment which was copied
                                      from another program by mistake.
* plugins.d/splashy.c: Only comment changes.
* plugins.d/usplash.c: - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  -*- coding: utf-8; mode: c; mode: orgtbl -*- */
 
1
/*  -*- coding: utf-8 -*- */
2
2
/*
3
3
 * Password-prompt - Read a password from the terminal and print it
4
4
 * 
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 <https://www.fukt.bsnet.se/~belorn/> and
 
23
 * <https://www.fukt.bsnet.se/~teddy/>.
23
24
 */
24
25
 
25
26
#define _GNU_SOURCE             /* getline() */
32
33
#include <signal.h>             /* sig_atomic_t, raise(), struct
33
34
                                   sigaction, sigemptyset(),
34
35
                                   sigaction(), sigaddset(), SIGINT,
35
 
                                   SIGQUIT, SIGHUP, SIGTERM,
36
 
                                   raise() */
 
36
                                   SIGQUIT, SIGHUP, SIGTERM */
37
37
#include <stddef.h>             /* NULL, size_t, ssize_t */
38
38
#include <sys/types.h>          /* ssize_t */
39
39
#include <stdlib.h>             /* EXIT_SUCCESS, EXIT_FAILURE,
51
51
                                   ARGP_KEY_ARG, ARGP_KEY_END,
52
52
                                   ARGP_ERR_UNKNOWN */
53
53
 
54
 
volatile sig_atomic_t quit_now = 0;
55
 
int signal_received;
 
54
volatile bool quit_now = false;
56
55
bool debug = false;
57
56
const char *argp_program_version = "password-prompt " VERSION;
58
57
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
59
58
 
60
 
static void termination_handler(int signum){
61
 
  if(quit_now){
62
 
    return;
63
 
  }
64
 
  quit_now = 1;
65
 
  signal_received = signum;
 
59
static void termination_handler(__attribute__((unused))int signum){
 
60
  quit_now = true;
66
61
}
67
62
 
68
63
int main(int argc, char **argv){
85
80
      { .name = NULL }
86
81
    };
87
82
    
88
 
    error_t parse_opt (int key, char *arg, struct argp_state *state){
89
 
      switch (key){
 
83
    error_t parse_opt (int key, char *arg, struct argp_state *state) {
 
84
      switch (key) {
90
85
      case 'p':
91
86
        prefix = arg;
92
87
        break;
94
89
        debug = true;
95
90
        break;
96
91
      case ARGP_KEY_ARG:
97
 
        argp_usage(state);
 
92
        argp_usage (state);
98
93
        break;
99
94
      case ARGP_KEY_END:
100
95
        break;
108
103
                         .args_doc = "",
109
104
                         .doc = "Mandos password-prompt -- Read and"
110
105
                         " output a password" };
111
 
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
112
 
    if(ret == ARGP_ERR_UNKNOWN){
 
106
    ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
 
107
    if (ret == ARGP_ERR_UNKNOWN){
113
108
      fprintf(stderr, "Unknown error while parsing arguments\n");
114
109
      return EXIT_FAILURE;
115
110
    }
116
111
  }
117
112
  
118
 
  if(debug){
 
113
  if (debug){
119
114
    fprintf(stderr, "Starting %s\n", argv[0]);
120
115
  }
121
 
  if(debug){
 
116
  if (debug){
122
117
    fprintf(stderr, "Storing current terminal attributes\n");
123
118
  }
124
119
  
125
 
  if(tcgetattr(STDIN_FILENO, &t_old) != 0){
 
120
  if (tcgetattr(STDIN_FILENO, &t_old) != 0){
126
121
    perror("tcgetattr");
127
122
    return EXIT_FAILURE;
128
123
  }
129
124
  
130
125
  sigemptyset(&new_action.sa_mask);
131
 
  ret = sigaddset(&new_action.sa_mask, SIGINT);
132
 
  if(ret == -1){
133
 
    perror("sigaddset");
134
 
    return EXIT_FAILURE;
135
 
  }
136
 
  ret = sigaddset(&new_action.sa_mask, SIGHUP);
137
 
  if(ret == -1){
138
 
    perror("sigaddset");
139
 
    return EXIT_FAILURE;
140
 
  }
141
 
  ret = sigaddset(&new_action.sa_mask, SIGTERM);
142
 
  if(ret == -1){
143
 
    perror("sigaddset");
144
 
    return EXIT_FAILURE;
145
 
  }
146
 
  /* Need to check if the handler is SIG_IGN before handling:
147
 
     | [[info:libc:Initial Signal Actions]] |
148
 
     | [[info:libc:Basic Signal Handling]]  |
149
 
  */
 
126
  sigaddset(&new_action.sa_mask, SIGINT);
 
127
  sigaddset(&new_action.sa_mask, SIGHUP);
 
128
  sigaddset(&new_action.sa_mask, SIGTERM);
150
129
  ret = sigaction(SIGINT, NULL, &old_action);
151
130
  if(ret == -1){
152
131
    perror("sigaction");
153
132
    return EXIT_FAILURE;
154
133
  }
155
 
  if(old_action.sa_handler != SIG_IGN){
 
134
  if (old_action.sa_handler != SIG_IGN){
156
135
    ret = sigaction(SIGINT, &new_action, NULL);
157
136
    if(ret == -1){
158
137
      perror("sigaction");
164
143
    perror("sigaction");
165
144
    return EXIT_FAILURE;
166
145
  }
167
 
  if(old_action.sa_handler != SIG_IGN){
 
146
  if (old_action.sa_handler != SIG_IGN){
168
147
    ret = sigaction(SIGHUP, &new_action, NULL);
169
148
    if(ret == -1){
170
149
      perror("sigaction");
176
155
    perror("sigaction");
177
156
    return EXIT_FAILURE;
178
157
  }
179
 
  if(old_action.sa_handler != SIG_IGN){
 
158
  if (old_action.sa_handler != SIG_IGN){
180
159
    ret = sigaction(SIGTERM, &new_action, NULL);
181
160
    if(ret == -1){
182
161
      perror("sigaction");
185
164
  }
186
165
  
187
166
  
188
 
  if(debug){
 
167
  if (debug){
189
168
    fprintf(stderr, "Removing echo flag from terminal attributes\n");
190
169
  }
191
170
  
192
171
  t_new = t_old;
193
 
  t_new.c_lflag &= ~(tcflag_t)ECHO;
194
 
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
 
172
  t_new.c_lflag &= ~ECHO;
 
173
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_new) != 0){
195
174
    perror("tcsetattr-echo");
196
175
    return EXIT_FAILURE;
197
176
  }
198
177
 
199
 
  if(debug){
 
178
  if (debug){
200
179
    fprintf(stderr, "Waiting for input from stdin \n");
201
180
  }
202
181
  while(true){
203
 
    if(quit_now){
 
182
    if (quit_now){
204
183
      if(debug){
205
184
        fprintf(stderr, "Interrupted by signal, exiting.\n");
206
185
      }
232
211
      }
233
212
    }
234
213
    ret = getline(&buffer, &n, stdin);
235
 
    if(ret > 0){
 
214
    if (ret > 0){
236
215
      status = EXIT_SUCCESS;
237
216
      /* Make n = data size instead of allocated buffer size */
238
217
      n = (size_t)ret;
253
232
      }
254
233
      break;
255
234
    }
256
 
    if(ret < 0){
257
 
      if(errno != EINTR and not feof(stdin)){
 
235
    if (ret < 0){
 
236
      if (errno != EINTR and not feof(stdin)){
258
237
        perror("getline");
259
238
        status = EXIT_FAILURE;
260
239
        break;
264
243
       read from stdin */
265
244
    fputc('\n', stderr);
266
245
    if(debug and not quit_now){
267
 
      /* If quit_now is nonzero, we were interrupted by a signal, and
 
246
      /* If quit_now is true, we were interrupted by a signal, and
268
247
         will print that later, so no need to show this too. */
269
248
      fprintf(stderr, "getline() returned 0, retrying.\n");
270
249
    }
272
251
  
273
252
  free(buffer);
274
253
  
275
 
  if(debug){
 
254
  if (debug){
276
255
    fprintf(stderr, "Restoring terminal attributes\n");
277
256
  }
278
 
  if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
 
257
  if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t_old) != 0){
279
258
    perror("tcsetattr+echo");
280
259
  }
281
260
  
282
 
  if(quit_now){
283
 
    sigemptyset(&old_action.sa_mask);
284
 
    old_action.sa_handler = SIG_DFL;
285
 
    ret = sigaction(signal_received, &old_action, NULL);
286
 
    if(ret == -1){
287
 
      perror("sigaction");
288
 
    }
289
 
    raise(signal_received);
290
 
  }
291
 
  
292
 
  if(debug){
 
261
  if (debug){
293
262
    fprintf(stderr, "%s is exiting with status %d\n", argv[0],
294
263
            status);
295
264
  }