/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 mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2008-08-11 16:35:32 UTC
  • mto: (237.7.1 mandos) (24.1.154 mandos)
  • mto: This revision was merged to the branch mainline in revision 66.
  • Revision ID: belorn@braxen-20080811163532-er1zd4q2j4ykfv11
Fixed fallback on error in mandos-client
fixed a bug in password-request

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY() */
25
25
 
26
 
#include <stdio.h>              /* popen(), fileno(), fprintf(),
27
 
                                   stderr, STDOUT_FILENO */
 
26
#include <stddef.h>             /* size_t, NULL */
 
27
#include <stdlib.h>             /* malloc(), exit(), EXIT_FAILURE,
 
28
                                   EXIT_SUCCESS, realloc() */
 
29
#include <stdbool.h>            /* bool, true, false */
 
30
#include <stdio.h>              /* perror, popen(), fileno(),
 
31
                                   fprintf(), stderr, STDOUT_FILENO */
 
32
#include <sys/types.h>          /* DIR, opendir(), stat(), struct
 
33
                                   stat, waitpid(), WIFEXITED(),
 
34
                                   WEXITSTATUS(), wait(), pid_t,
 
35
                                   uid_t, gid_t, getuid(), getgid(),
 
36
                                   dirfd() */
 
37
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
 
38
                                   FD_SET(), FD_ISSET(), FD_CLR */
 
39
#include <sys/wait.h>           /* wait(), waitpid(), WIFEXITED(),
 
40
                                   WEXITSTATUS() */
 
41
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
28
42
#include <iso646.h>             /* and, or, not */
29
 
#include <sys/types.h>          /* DIR, opendir(), stat(),
30
 
                                   struct stat, waitpid(),
31
 
                                   WIFEXITED(), WEXITSTATUS(),
32
 
                                   wait() */
33
 
#include <sys/wait.h>           /* wait() */
34
43
#include <dirent.h>             /* DIR, struct dirent, opendir(),
35
 
                                   readdir(), closedir() */
36
 
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
 
44
                                   readdir(), closedir(), dirfd() */
37
45
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
38
 
                                   fcntl() */
39
 
#include <fcntl.h>              /* fcntl() */
40
 
#include <stddef.h>             /* NULL */
41
 
#include <stdlib.h>             /* EXIT_FAILURE */
42
 
#include <sys/select.h>         /* fd_set, select(), FD_ZERO(),
43
 
                                   FD_SET(), FD_ISSET() */
44
 
#include <string.h>             /* strlen(), strcpy(), strcat() */
45
 
#include <stdbool.h>            /* true */
46
 
#include <sys/wait.h>           /* waitpid(), WIFEXITED(),
47
 
                                   WEXITSTATUS() */
 
46
                                   fcntl(), setuid(), setgid(),
 
47
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
 
48
                                   access(), pipe(), fork(), close()
 
49
                                   dup2, STDOUT_FILENO, _exit(),
 
50
                                   execv(), write(), read(),
 
51
                                   close() */
 
52
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
 
53
                                   FD_CLOEXEC */
 
54
#include <string.h>             /* strtok, strlen(), strcpy(),
 
55
                                   strcat() */
48
56
#include <errno.h>              /* errno */
49
 
#include <argp.h>               /* struct argp_option,
50
 
                                   struct argp_state, struct argp,
51
 
                                   argp_parse() */
 
57
#include <argp.h>               /* struct argp_option, struct
 
58
                                   argp_state, struct argp,
 
59
                                   argp_parse(), ARGP_ERR_UNKNOWN,
 
60
                                   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
 
61
#include <signal.h>             /* struct sigaction, sigemptyset(),
 
62
                                   sigaddset(), sigaction(),
 
63
                                   sigprocmask(), SIG_BLOCK, SIGCHLD,
 
64
                                   SIG_UNBLOCK, kill() */
 
65
#include <errno.h>              /* errno, EBADF */
 
66
 
 
67
#define BUFFER_SIZE 256
 
68
 
 
69
const char *argp_program_version = "mandos-client 1.0";
 
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
52
71
 
53
72
struct process;
54
73
 
72
91
  struct plugin *next;
73
92
} plugin;
74
93
 
75
 
plugin *getplugin(char *name, plugin **plugin_list){
 
94
static plugin *getplugin(char *name, plugin **plugin_list){
76
95
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
96
    if ((p->name == name)
78
97
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
120
  return new_plugin;
102
121
}
103
122
 
104
 
void addargument(plugin *p, char *arg){
 
123
static void addargument(plugin *p, char *arg){
105
124
  p->argv[p->argc] = arg;
106
125
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
126
  if (p->argv == NULL){
117
136
 * Descriptor Flags".
118
137
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
138
 */
120
 
int set_cloexec_flag(int fd)
 
139
static int set_cloexec_flag(int fd)
121
140
{
122
141
  int ret = fcntl(fd, F_GETFD, 0);
123
142
  /* If reading the flags failed, return error indication now. */
128
147
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
148
}
130
149
 
131
 
#define BUFFER_SIZE 256
132
 
 
133
 
const char *argp_program_version = "plugbasedclient 0.9";
134
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
 
 
136
150
process *process_list = NULL;
137
151
 
138
152
/* Mark a process as completed when it exits, and save its exit
141
155
  process *proc = process_list;
142
156
  int status;
143
157
  pid_t pid = wait(&status);
 
158
  if(pid == -1){
 
159
    perror("wait");
 
160
    return;
 
161
  }
144
162
  while(proc != NULL and proc->pid != pid){
145
 
    proc = proc->next;    
 
163
    proc = proc->next;
146
164
  }
147
165
  if(proc == NULL){
148
166
    /* Process not found in process list */
152
170
  proc->completed = true;
153
171
}
154
172
 
 
173
bool print_out_password(const char *buffer, size_t length){
 
174
  ssize_t ret;
 
175
  if(length>0 and buffer[length-1] == '\n'){
 
176
    length--;
 
177
  }
 
178
  for(size_t written = 0; written < length; written += (size_t)ret){
 
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
 
180
                                   length - written));
 
181
    if(ret < 0){
 
182
      return false;
 
183
    }
 
184
  }
 
185
  return true;
 
186
}
 
187
 
155
188
int main(int argc, char *argv[]){
156
189
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
157
190
  size_t d_name_len;
160
193
  struct stat st;
161
194
  fd_set rfds_all;
162
195
  int ret, maxfd = 0;
 
196
  uid_t uid = 65534;
 
197
  gid_t gid = 65534;
163
198
  bool debug = false;
164
199
  int exitstatus = EXIT_SUCCESS;
 
200
  struct sigaction old_sigchld_action;
 
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
202
                                      .sa_flags = SA_NOCLDSTOP };
 
203
  char *plus_options = NULL;
 
204
  char **plus_argv = NULL;
 
205
 
 
206
  errno = 0;
165
207
  
166
208
  /* Establish a signal handler */
167
 
  struct sigaction old_sigchld_action,
168
 
    sigchld_action = { .sa_handler = handle_sigchld,
169
 
                       .sa_flags = SA_NOCLDSTOP };
170
209
  sigemptyset(&sigchld_action.sa_mask);
171
210
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
172
211
  if(ret < 0){
193
232
    { .name = "plugin-dir", .key = 128,
194
233
      .arg = "DIRECTORY",
195
234
      .doc = "Specify a different plugin directory", .group = 2 },
196
 
    { .name = "debug", .key = 129,
 
235
    { .name = "userid", .key = 129,
 
236
      .arg = "ID", .flags = 0,
 
237
      .doc = "User ID the plugins will run as", .group = 2 },
 
238
    { .name = "groupid", .key = 130,
 
239
      .arg = "ID", .flags = 0,
 
240
      .doc = "Group ID the plugins will run as", .group = 2 },
 
241
    { .name = "debug", .key = 131,
197
242
      .doc = "Debug mode", .group = 3 },
198
243
    { .name = NULL }
199
244
  };
209
254
        do{
210
255
          addargument(getplugin(NULL, plugins), p);
211
256
          p = strtok(NULL, ",");
212
 
        } while (p);
 
257
        } while (p != NULL);
213
258
      }
214
259
      break;
215
260
    case 'o':
216
261
      if (arg != NULL){
217
262
        char *name = strtok(arg, ":");
218
263
        char *p = strtok(NULL, ":");
219
 
        if(p){
 
264
        if(p != NULL){
220
265
          p = strtok(p, ",");
221
266
          do{
222
267
            addargument(getplugin(name, plugins), p);
223
268
            p = strtok(NULL, ",");
224
 
          } while (p);
 
269
          } while (p != NULL);
225
270
        }
226
271
      }
227
272
      break;
234
279
      plugindir = arg;
235
280
      break;
236
281
    case 129:
 
282
      uid = (uid_t)strtol(arg, NULL, 10);
 
283
      break;
 
284
    case 130:
 
285
      gid = (gid_t)strtol(arg, NULL, 10);
 
286
      break;
 
287
    case 131:
237
288
      debug = true;
238
289
      break;
239
290
    case ARGP_KEY_ARG:
240
 
      argp_usage (state);
 
291
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
292
        argp_usage (state);
 
293
      }
 
294
      plus_options = arg;
241
295
      break;
242
296
    case ARGP_KEY_END:
243
297
      break;
250
304
  plugin *plugin_list = NULL;
251
305
  
252
306
  struct argp argp = { .options = options, .parser = parse_opt,
253
 
                       .args_doc = "",
 
307
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
254
308
                       .doc = "Mandos plugin runner -- Run plugins" };
255
309
  
256
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
310
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
311
  if (ret == ARGP_ERR_UNKNOWN){
 
312
    fprintf(stderr, "Unknown error while parsing arguments\n");
 
313
    exitstatus = EXIT_FAILURE;
 
314
    goto end;
 
315
  }
 
316
  
 
317
  if(plus_options){
 
318
    /* This is a mangled argument in the form of
 
319
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
320
    /* Make new argc and argv vars, and call argp_parse() again. */
 
321
    plus_options++;             /* skip the first '+' character */
 
322
    const char delims[] = "+";
 
323
    char *arg;
 
324
    int new_argc = 1;
 
325
    plus_argv = malloc(sizeof(char*) * 2);
 
326
    if(plus_argv == NULL){
 
327
      perror("malloc");
 
328
      exitstatus = EXIT_FAILURE;
 
329
      goto end;
 
330
    }
 
331
    plus_argv[0] = argv[0];
 
332
    plus_argv[1] = NULL;
 
333
    arg = strtok(plus_options, delims); /* Get first argument */
 
334
    while(arg != NULL){
 
335
      new_argc++;
 
336
      plus_argv = realloc(plus_argv, sizeof(char *)
 
337
                         * ((unsigned int) new_argc + 1));
 
338
      if(plus_argv == NULL){
 
339
        perror("realloc");
 
340
        exitstatus = EXIT_FAILURE;
 
341
        goto end;
 
342
      }
 
343
      plus_argv[new_argc-1] = arg;
 
344
      plus_argv[new_argc] = NULL;
 
345
      arg = strtok(NULL, delims); /* Get next argument */
 
346
    }
 
347
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
 
348
    if (ret == ARGP_ERR_UNKNOWN){
 
349
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
350
      exitstatus = EXIT_FAILURE;
 
351
      goto end;
 
352
    }
 
353
  }
257
354
  
258
355
  if(debug){
259
356
    for(plugin *p = plugin_list; p != NULL; p=p->next){
265
362
    }
266
363
  }
267
364
  
 
365
  ret = setuid(uid);
 
366
  if (ret == -1){
 
367
    perror("setuid");
 
368
  }
 
369
  
 
370
  setgid(gid);
 
371
  if (ret == -1){
 
372
    perror("setgid");
 
373
  }
 
374
  
268
375
  dir = opendir(plugindir);
269
376
  if(dir == NULL){
270
377
    perror("Could not open plugin dir");
292
399
    
293
400
    // All directory entries have been processed
294
401
    if(dirst == NULL){
 
402
      if (errno == EBADF){
 
403
        perror("readdir");
 
404
        exitstatus = EXIT_FAILURE;
 
405
        goto end;
 
406
      }
295
407
      break;
296
408
    }
297
409
    
352
464
    strcat(filename, "/");      /* Spurious warning */
353
465
    strcat(filename, dirst->d_name); /* Spurious warning */
354
466
    
355
 
    stat(filename, &st);
 
467
    ret = stat(filename, &st);
 
468
    if (ret == -1){
 
469
      perror("stat");
 
470
      exitstatus = EXIT_FAILURE;
 
471
      goto end;
 
472
    }
356
473
    
357
474
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
358
475
      if(debug){
418
535
        perror("sigprocmask");
419
536
        _exit(EXIT_FAILURE);
420
537
      }
421
 
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
538
 
 
539
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
 
540
      if(ret == -1){
 
541
        perror("dup2");
 
542
        _exit(EXIT_FAILURE);
 
543
      }
422
544
      
423
545
      if(dirfd(dir) < 0){
424
546
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
476
598
  
477
599
  closedir(dir);
478
600
  dir = NULL;
479
 
  
 
601
    
480
602
  if (process_list == NULL){
481
 
    fprintf(stderr, "No plugin processes started, exiting\n");
482
 
    exitstatus = EXIT_FAILURE;
483
 
    goto end;
 
603
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
 
604
            " directory?\n");
 
605
    process_list = NULL;
484
606
  }
485
607
  while(process_list){
486
608
    fd_set rfds = rfds_all;
501
623
          /* Bad exit by plugin */
502
624
          if(debug){
503
625
            if(WIFEXITED(proc->status)){
504
 
              fprintf(stderr, "Plugin %d exited with status %d\n",
505
 
                      proc->pid, WEXITSTATUS(proc->status));
 
626
              fprintf(stderr, "Plugin %u exited with status %d\n",
 
627
                      (unsigned int) (proc->pid),
 
628
                      WEXITSTATUS(proc->status));
506
629
            } else if(WIFSIGNALED(proc->status)) {
507
 
              fprintf(stderr, "Plugin %d killed by signal %d\n",
508
 
                      proc->pid, WTERMSIG(proc->status));
 
630
              fprintf(stderr, "Plugin %u killed by signal %d\n",
 
631
                      (unsigned int) (proc->pid),
 
632
                      WTERMSIG(proc->status));
509
633
            } else if(WCOREDUMP(proc->status)){
510
 
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
 
634
              fprintf(stderr, "Plugin %d dumped core\n",
 
635
                      (unsigned int) (proc->pid));
511
636
            }
512
637
          }
513
638
          /* Remove the plugin */
546
671
          break;
547
672
        }
548
673
        /* This process exited nicely, so print its buffer */
549
 
        for(size_t written = 0; written < proc->buffer_length;
550
 
            written += (size_t)ret){
551
 
          ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
552
 
                                         proc->buffer + written,
553
 
                                         proc->buffer_length
554
 
                                         - written));
555
 
          if(ret < 0){
556
 
            perror("write");
557
 
            exitstatus = EXIT_FAILURE;
558
 
            goto end;
559
 
          }
 
674
 
 
675
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
 
676
        if(not bret){
 
677
          perror("print_out_password");
 
678
          exitstatus = EXIT_FAILURE;
560
679
        }
561
680
        goto end;
562
681
      }
591
710
      }
592
711
    }
593
712
  }
594
 
  if(process_list == NULL){
595
 
    fprintf(stderr, "All plugin processes failed, exiting\n");
596
 
    exitstatus = EXIT_FAILURE;
597
 
  }
598
 
  
 
713
 
 
714
 
599
715
 end:
 
716
  
 
717
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
 
718
    /* Fallback if all plugins failed or an error occured */
 
719
    bool bret;
 
720
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
 
721
    char *passwordbuffer = getpass("Password: ");
 
722
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
 
723
    if(not bret){
 
724
      perror("print_out_password");
 
725
      exitstatus = EXIT_FAILURE;
 
726
      goto end;
 
727
    }
 
728
  }
 
729
  
600
730
  /* Restore old signal handler */
601
731
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
602
732
  
 
733
  free(plus_argv);
 
734
  
603
735
  /* Free the plugin list */
604
736
  for(plugin *next; plugin_list != NULL; plugin_list = next){
605
737
    next = plugin_list->next;
615
747
  for(process *next; process_list != NULL; process_list = next){
616
748
    next = process_list->next;
617
749
    close(process_list->fd);
618
 
    kill(process_list->pid, SIGTERM);
 
750
    ret = kill(process_list->pid, SIGTERM);
 
751
    if(ret == -1 and errno != ESRCH){
 
752
      /* set-uid proccesses migth not get closed */
 
753
      perror("kill");
 
754
    }
619
755
    free(process_list->buffer);
620
756
    free(process_list);
621
757
  }