/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 plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-02 21:06:12 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080802210612-4c1waup4z0f66ya7
Non-tested commit for merge purposes.

* plugbasedclient.c (getplugin, addargument, set_cloexec): Made static.

* plugins.d/passprompt.c (termination_handler): Made static.

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 <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() */
 
26
#include <stdio.h>              /* popen(), fileno(), fprintf(),
 
27
                                   stderr, STDOUT_FILENO */
42
28
#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() */
43
34
#include <dirent.h>             /* DIR, struct dirent, opendir(),
44
 
                                   readdir(), closedir(), dirfd() */
 
35
                                   readdir(), closedir() */
 
36
#include <sys/stat.h>           /* struct stat, stat(), S_ISREG() */
45
37
#include <unistd.h>             /* struct stat, stat(), S_ISREG(),
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() */
 
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() */
56
48
#include <errno.h>              /* errno */
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 */
 
49
#include <argp.h>               /* struct argp_option,
 
50
                                   struct argp_state, struct argp,
 
51
                                   argp_parse() */
66
52
 
67
53
#define BUFFER_SIZE 256
68
54
 
69
 
const char *argp_program_version = "mandos-client 1.0";
70
 
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
 
 
72
55
struct process;
73
56
 
74
57
typedef struct process{
147
130
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
148
131
}
149
132
 
 
133
const char *argp_program_version = "plugbasedclient 0.9";
 
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
 
135
 
150
136
process *process_list = NULL;
151
137
 
152
138
/* Mark a process as completed when it exits, and save its exit
155
141
  process *proc = process_list;
156
142
  int status;
157
143
  pid_t pid = wait(&status);
158
 
  if(pid == -1){
159
 
    perror("wait");
160
 
    return;
161
 
  }
162
144
  while(proc != NULL and proc->pid != pid){
163
 
    proc = proc->next;
 
145
    proc = proc->next;    
164
146
  }
165
147
  if(proc == NULL){
166
148
    /* Process not found in process list */
170
152
  proc->completed = true;
171
153
}
172
154
 
173
 
bool print_out_password(const char *buffer, size_t length){
174
 
  size_t ret;
175
 
  for(size_t written = 0; written < length; written += ret){
176
 
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
177
 
                                   length - written));
178
 
    if(ret < 0){
179
 
      return false;
180
 
    }
181
 
  }
182
 
  return true;
183
 
}
184
 
 
185
155
int main(int argc, char *argv[]){
186
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
187
157
  size_t d_name_len;
190
160
  struct stat st;
191
161
  fd_set rfds_all;
192
162
  int ret, maxfd = 0;
193
 
  uid_t uid = 65534;
194
 
  gid_t gid = 65534;
195
163
  bool debug = false;
196
164
  int exitstatus = EXIT_SUCCESS;
197
 
  struct sigaction old_sigchld_action;
198
 
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
199
 
                                      .sa_flags = SA_NOCLDSTOP };
200
 
  char *plus_options = NULL;
201
 
  char **plus_argv = NULL;
202
165
  
203
166
  /* Establish a signal handler */
 
167
  struct sigaction old_sigchld_action,
 
168
    sigchld_action = { .sa_handler = handle_sigchld,
 
169
                       .sa_flags = SA_NOCLDSTOP };
204
170
  sigemptyset(&sigchld_action.sa_mask);
205
171
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
206
172
  if(ret < 0){
227
193
    { .name = "plugin-dir", .key = 128,
228
194
      .arg = "DIRECTORY",
229
195
      .doc = "Specify a different plugin directory", .group = 2 },
230
 
    { .name = "userid", .key = 129,
231
 
      .arg = "ID", .flags = 0,
232
 
      .doc = "User ID the plugins will run as", .group = 2 },
233
 
    { .name = "groupid", .key = 130,
234
 
      .arg = "ID", .flags = 0,
235
 
      .doc = "Group ID the plugins will run as", .group = 2 },
236
 
    { .name = "debug", .key = 131,
 
196
    { .name = "debug", .key = 129,
237
197
      .doc = "Debug mode", .group = 3 },
238
198
    { .name = NULL }
239
199
  };
249
209
        do{
250
210
          addargument(getplugin(NULL, plugins), p);
251
211
          p = strtok(NULL, ",");
252
 
        } while (p != NULL);
 
212
        } while (p);
253
213
      }
254
214
      break;
255
215
    case 'o':
256
216
      if (arg != NULL){
257
217
        char *name = strtok(arg, ":");
258
218
        char *p = strtok(NULL, ":");
259
 
        if(p != NULL){
 
219
        if(p){
260
220
          p = strtok(p, ",");
261
221
          do{
262
222
            addargument(getplugin(name, plugins), p);
263
223
            p = strtok(NULL, ",");
264
 
          } while (p != NULL);
 
224
          } while (p);
265
225
        }
266
226
      }
267
227
      break;
274
234
      plugindir = arg;
275
235
      break;
276
236
    case 129:
277
 
      uid = (uid_t)strtol(arg, NULL, 10);
278
 
      break;
279
 
    case 130:
280
 
      gid = (gid_t)strtol(arg, NULL, 10);
281
 
      break;
282
 
    case 131:
283
237
      debug = true;
284
238
      break;
285
239
    case ARGP_KEY_ARG:
286
 
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
287
 
        argp_usage (state);
288
 
      }
289
 
      plus_options = arg;
 
240
      argp_usage (state);
290
241
      break;
291
242
    case ARGP_KEY_END:
292
243
      break;
299
250
  plugin *plugin_list = NULL;
300
251
  
301
252
  struct argp argp = { .options = options, .parser = parse_opt,
302
 
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
 
253
                       .args_doc = "",
303
254
                       .doc = "Mandos plugin runner -- Run plugins" };
304
255
  
305
 
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
306
 
  if (ret == ARGP_ERR_UNKNOWN){
307
 
    fprintf(stderr, "Unkown error while parsing arguments\n");
308
 
    exitstatus = EXIT_FAILURE;
309
 
    goto end;
310
 
  }
311
 
  
312
 
  if(plus_options){
313
 
    /* This is a mangled argument in the form of
314
 
     "+--option+--other-option=parameter+--yet-another-option", etc */
315
 
    /* Make new argc and argv vars, and call argp_parse() again. */
316
 
    plus_options++;             /* skip the first '+' character */
317
 
    const char delims[] = "+";
318
 
    char *arg;
319
 
    int new_argc = 1;
320
 
    plus_argv = malloc(sizeof(char*) * 2);
321
 
    if(plus_argv == NULL){
322
 
      perror("malloc");
323
 
      exitstatus = EXIT_FAILURE;
324
 
      goto end;
325
 
    }
326
 
    plus_argv[0] = argv[0];
327
 
    plus_argv[1] = NULL;
328
 
    arg = strtok(plus_options, delims); /* Get first argument */
329
 
    while(arg != NULL){
330
 
      new_argc++;
331
 
      plus_argv = realloc(plus_argv, sizeof(char *)
332
 
                         * ((unsigned int) new_argc + 1));
333
 
      if(plus_argv == NULL){
334
 
        perror("realloc");
335
 
        exitstatus = EXIT_FAILURE;
336
 
        goto end;
337
 
      }
338
 
      plus_argv[new_argc-1] = arg;
339
 
      plus_argv[new_argc] = NULL;
340
 
      arg = strtok(NULL, delims); /* Get next argument */
341
 
    }
342
 
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
343
 
    if (ret == ARGP_ERR_UNKNOWN){
344
 
      fprintf(stderr, "Unkown error while parsing arguments\n");
345
 
      exitstatus = EXIT_FAILURE;
346
 
      goto end;
347
 
    }
348
 
  }
 
256
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
349
257
  
350
258
  if(debug){
351
259
    for(plugin *p = plugin_list; p != NULL; p=p->next){
357
265
    }
358
266
  }
359
267
  
360
 
  ret = setuid(uid);
361
 
  if (ret == -1){
362
 
    perror("setuid");
363
 
  }
364
 
  
365
 
  setgid(gid);
366
 
  if (ret == -1){
367
 
    perror("setgid");
368
 
  }
369
 
  
370
268
  dir = opendir(plugindir);
371
269
  if(dir == NULL){
372
270
    perror("Could not open plugin dir");
394
292
    
395
293
    // All directory entries have been processed
396
294
    if(dirst == NULL){
397
 
      if (errno == EBADF){
398
 
        perror("readdir");
399
 
        exitstatus = EXIT_FAILURE;
400
 
        goto end;
401
 
      }
402
295
      break;
403
296
    }
404
297
    
459
352
    strcat(filename, "/");      /* Spurious warning */
460
353
    strcat(filename, dirst->d_name); /* Spurious warning */
461
354
    
462
 
    ret = stat(filename, &st);
463
 
    if (ret == -1){
464
 
      perror("stat");
465
 
      exitstatus = EXIT_FAILURE;
466
 
      goto end;
467
 
    }
 
355
    stat(filename, &st);
468
356
    
469
357
    if (not S_ISREG(st.st_mode) or (access(filename, X_OK) != 0)){
470
358
      if(debug){
530
418
        perror("sigprocmask");
531
419
        _exit(EXIT_FAILURE);
532
420
      }
533
 
 
534
 
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
535
 
      if(ret == -1){
536
 
        perror("dup2");
537
 
        _exit(EXIT_FAILURE);
538
 
      }
 
421
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
539
422
      
540
423
      if(dirfd(dir) < 0){
541
424
        /* If dir has no file descriptor, we could not set FD_CLOEXEC
593
476
  
594
477
  closedir(dir);
595
478
  dir = NULL;
596
 
    
 
479
  
597
480
  if (process_list == NULL){
598
 
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
599
 
            " directory?\n");
600
 
    process_list = NULL;
 
481
    fprintf(stderr, "No plugin processes started, exiting\n");
 
482
    exitstatus = EXIT_FAILURE;
 
483
    goto end;
601
484
  }
602
485
  while(process_list){
603
486
    fd_set rfds = rfds_all;
618
501
          /* Bad exit by plugin */
619
502
          if(debug){
620
503
            if(WIFEXITED(proc->status)){
621
 
              fprintf(stderr, "Plugin %u exited with status %d\n",
622
 
                      (unsigned int) (proc->pid),
623
 
                      WEXITSTATUS(proc->status));
 
504
              fprintf(stderr, "Plugin %d exited with status %d\n",
 
505
                      proc->pid, WEXITSTATUS(proc->status));
624
506
            } else if(WIFSIGNALED(proc->status)) {
625
 
              fprintf(stderr, "Plugin %u killed by signal %d\n",
626
 
                      (unsigned int) (proc->pid),
627
 
                      WTERMSIG(proc->status));
 
507
              fprintf(stderr, "Plugin %d killed by signal %d\n",
 
508
                      proc->pid, WTERMSIG(proc->status));
628
509
            } else if(WCOREDUMP(proc->status)){
629
 
              fprintf(stderr, "Plugin %d dumped core\n",
630
 
                      (unsigned int) (proc->pid));
 
510
              fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
631
511
            }
632
512
          }
633
513
          /* Remove the plugin */
666
546
          break;
667
547
        }
668
548
        /* This process exited nicely, so print its buffer */
669
 
 
670
 
        bool bret = print_out_password(proc->buffer, proc->buffer_length);
671
 
        if(not bret){
672
 
          perror("print_out_password");
673
 
          exitstatus = EXIT_FAILURE;
 
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
560
        }
675
561
        goto end;
676
562
      }
705
591
      }
706
592
    }
707
593
  }
708
 
  
709
594
  if(process_list == NULL){
710
 
    bool bret;
711
 
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
712
 
    char *passwordbuffer = getpass("Password: ");
713
 
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
714
 
    if(not bret){
715
 
      perror("print_out_password");
716
 
      exitstatus = EXIT_FAILURE;
717
 
      goto end;
718
 
    }
719
 
    bret = print_out_password("\n", 1);
720
 
    if(not bret){
721
 
      perror("print_out_password");
722
 
      exitstatus = EXIT_FAILURE;
723
 
    }
 
595
    fprintf(stderr, "All plugin processes failed, exiting\n");
 
596
    exitstatus = EXIT_FAILURE;
724
597
  }
725
 
 
 
598
  
726
599
 end:
727
 
  
728
600
  /* Restore old signal handler */
729
601
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
730
602
  
731
 
  free(plus_argv);
732
 
  
733
603
  /* Free the plugin list */
734
604
  for(plugin *next; plugin_list != NULL; plugin_list = next){
735
605
    next = plugin_list->next;
745
615
  for(process *next; process_list != NULL; process_list = next){
746
616
    next = process_list->next;
747
617
    close(process_list->fd);
748
 
    ret = kill(process_list->pid, SIGTERM);
749
 
    if(ret == -1 and errno != ESRCH){
750
 
      /* set-uid proccesses migth not get closed */
751
 
      perror("kill");
752
 
    }
 
618
    kill(process_list->pid, SIGTERM);
753
619
    free(process_list->buffer);
754
620
    free(process_list);
755
621
  }