/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 plugin-runner.c

  • Committer: Teddy Hogeborn
  • Date: 2008-09-05 16:24:33 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080905162433-58fgx91ae9foxlh1
* Makefile (PIDDIR, USER, GROUP): Removed.
  (install-server): Do not create $(PIDDIR).
  (uninstall-server): Do not remove $(PIDDIR).

* init.d-mandos (PIDFILE): Changed to "/var/run/$NAME.pid".

* mandos (IPv6_TCPServer.enabled): New attribute.
  (IPv6_TCPServer.server_activate): Only call method of superclass if
                                    "self.enabled".
  (IPv6_TCPServer.enable): Set "self.enabled" to True.
  (main): Create client Set() early.  Create IPv6_TCPServer object
          early.  Switch to user and group "mandos", "nobody" or
          65534, if possible.  Enable IPv6_TCPServer *after* switching
          user.

* mandos-keygen (KEYDIR): Changed to "/etc/keys/mandos".

* mandos.xml (FILES): Changed PID file.
  (SECURITY): The server does need to be privileged, but switches to a
              non-privileged user.

* plugin-runner.xml (EXAMPLE): Changed long example to something more
                               realistic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
const char *argp_program_version = "plugin-runner 1.0";
73
73
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
74
74
 
 
75
struct plugin;
 
76
 
75
77
typedef struct plugin{
76
78
  char *name;                   /* can be NULL or any plugin name */
77
79
  char **argv;
94
96
 
95
97
static plugin *plugin_list = NULL;
96
98
 
97
 
/* Gets an existing plugin based on name,
 
99
/* Gets a existing plugin based on name,
98
100
   or if none is found, creates a new one */
99
101
static plugin *getplugin(char *name){
100
102
  /* Check for exiting plugin with that name */
116
118
      return NULL;
117
119
    }
118
120
  }
119
 
  
 
121
 
120
122
  *new_plugin = (plugin) { .name = copy_name,
121
123
                           .argc = 1,
122
124
                           .disabled = false,
185
187
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
186
188
  /* Search for this environment variable */
187
189
  for(char **e = p->environ; *e != NULL; e++){
188
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
190
    if(strncmp(*e, def, namelen+1) == 0){
189
191
      /* It already exists */
190
192
      if(replace){
191
 
        char *new = realloc(*e, strlen(def) + 1);
 
193
        char *new = realloc(*e, strlen(def));
192
194
        if(new == NULL){
193
195
          return false;
194
196
        }
206
208
 * Descriptor Flags".
207
209
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
208
210
 */
209
 
static int set_cloexec_flag(int fd){
 
211
static int set_cloexec_flag(int fd)
 
212
{
210
213
  int ret = fcntl(fd, F_GETFD, 0);
211
214
  /* If reading the flags failed, return error indication now. */
212
215
  if(ret < 0){
219
222
 
220
223
/* Mark processes as completed when they exit, and save their exit
221
224
   status. */
222
 
static void handle_sigchld(__attribute__((unused)) int sig){
 
225
void handle_sigchld(__attribute__((unused)) int sig){
223
226
  while(true){
224
227
    plugin *proc = plugin_list;
225
228
    int status;
235
238
      /* No child processes */
236
239
      break;
237
240
    }
238
 
    
 
241
 
239
242
    /* A child exited, find it in process_list */
240
243
    while(proc != NULL and proc->pid != pid){
241
244
      proc = proc->next;
250
253
}
251
254
 
252
255
/* Prints out a password to stdout */
253
 
static bool print_out_password(const char *buffer, size_t length){
 
256
bool print_out_password(const char *buffer, size_t length){
254
257
  ssize_t ret;
 
258
  if(length>0 and buffer[length-1] == '\n'){
 
259
    length--;
 
260
  }
255
261
  for(size_t written = 0; written < length; written += (size_t)ret){
256
262
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
257
263
                                   length - written));
391
397
      if(arg == NULL){
392
398
        break;
393
399
      }
394
 
      if(not add_environment(getplugin(NULL), arg, true)){
395
 
        perror("add_environment");
 
400
      {
 
401
        char *envdef = strdup(arg);
 
402
        if(envdef == NULL){
 
403
          break;
 
404
        }
 
405
        if(not add_environment(getplugin(NULL), envdef, true)){
 
406
          perror("add_environment");
 
407
        }
396
408
      }
397
409
      break;
398
410
    case 'o':                   /* --options-for */
399
411
      if (arg != NULL){
400
412
        char *p_name = strsep(&arg, ":");
401
 
        if(p_name[0] == '\0' or arg == NULL){
 
413
        if(p_name[0] == '\0'){
402
414
          break;
403
415
        }
404
416
        char *opt = strsep(&arg, ":");
405
 
        if(opt[0] == '\0' or opt == NULL){
 
417
        if(opt[0] == '\0'){
406
418
          break;
407
419
        }
408
 
        char *p;
409
 
        while((p = strsep(&opt, ",")) != NULL){
410
 
          if(p[0] == '\0'){
411
 
            continue;
412
 
          }
413
 
          if(not add_argument(getplugin(p_name), p)){
414
 
            perror("add_argument");
415
 
            return ARGP_ERR_UNKNOWN;
 
420
        if(opt != NULL){
 
421
          char *p;
 
422
          while((p = strsep(&opt, ",")) != NULL){
 
423
            if(p[0] == '\0'){
 
424
              continue;
 
425
            }
 
426
            if(not add_argument(getplugin(p_name), p)){
 
427
              perror("add_argument");
 
428
              return ARGP_ERR_UNKNOWN;
 
429
            }
416
430
          }
417
431
        }
418
432
      }
426
440
        if(envdef == NULL){
427
441
          break;
428
442
        }
429
 
        *envdef = '\0';
430
 
        if(not add_environment(getplugin(arg), envdef+1, true)){
 
443
        char *p_name = strndup(arg, (size_t) (envdef-arg));
 
444
        if(p_name == NULL){
 
445
          break;
 
446
        }
 
447
        envdef++;
 
448
        if(not add_environment(getplugin(p_name), envdef, true)){
431
449
          perror("add_environment");
432
450
        }
433
451
      }
451
469
      }
452
470
      break;
453
471
    case 128:                   /* --plugin-dir */
454
 
      free(plugindir);
455
472
      plugindir = strdup(arg);
456
473
      if(plugindir == NULL){
457
474
        perror("strdup");
470
487
      debug = true;
471
488
      break;
472
489
    case ARGP_KEY_ARG:
473
 
      /* Cryptsetup always passes an argument, which is an empty
474
 
         string if "none" was specified in /etc/crypttab.  So if
475
 
         argument was empty, we ignore it silently. */
476
 
      if(arg[0] != '\0'){
477
 
        fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
478
 
      }
 
490
      fprintf(stderr, "Ignoring unknown argument \"%s\"\n", arg);
479
491
      break;
480
492
    case ARGP_KEY_END:
481
493
      break;
500
512
    case 128:                   /* --plugin-dir */
501
513
      break;
502
514
    case 129:                   /* --config-file */
503
 
      free(argfile);
504
515
      argfile = strdup(arg);
505
516
      if(argfile == NULL){
506
517
        perror("strdup");
736
747
    }
737
748
 
738
749
    char *filename;
739
 
    if(plugindir == NULL){
740
 
      ret = asprintf(&filename, PDIR "/%s", dirst->d_name);
741
 
    } else {
742
 
      ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
743
 
    }
 
750
    ret = asprintf(&filename, "%s/%s", plugindir, dirst->d_name);
744
751
    if(ret < 0){
745
752
      perror("asprintf");
746
753
      continue;
935
942
    }
936
943
    /* OK, now either a process completed, or something can be read
937
944
       from one of them */
938
 
    for(plugin *proc = plugin_list; proc != NULL;){
 
945
    for(plugin *proc = plugin_list; proc != NULL; proc = proc->next){
939
946
      /* Is this process completely done? */
940
947
      if(proc->eof and proc->completed){
941
948
        /* Only accept the plugin output if it exited cleanly */
968
975
            exitstatus = EXIT_FAILURE;
969
976
            goto fallback;
970
977
          }
971
 
          
 
978
          free_plugin(proc);
972
979
          /* We are done modifying process list, so unblock signal */
973
980
          ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
974
981
                             NULL);
981
988
          if(plugin_list == NULL){
982
989
            break;
983
990
          }
984
 
          
985
 
          plugin *next_plugin = proc->next;
986
 
          free_plugin(proc);
987
 
          proc = next_plugin;
988
991
          continue;
989
992
        }
990
993
        
1002
1005
      /* This process has not completed.  Does it have any output? */
1003
1006
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
1004
1007
        /* This process had nothing to say at this time */
1005
 
        proc = proc->next;
1006
1008
        continue;
1007
1009
      }
1008
1010
      /* Before reading, make the process' data buffer large enough */
1021
1023
                 BUFFER_SIZE);
1022
1024
      if(ret < 0){
1023
1025
        /* Read error from this process; ignore the error */
1024
 
        proc = proc->next;
1025
1026
        continue;
1026
1027
      }
1027
1028
      if(ret == 0){
1042
1043
    bool bret;
1043
1044
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
1044
1045
    char *passwordbuffer = getpass("Password: ");
1045
 
    size_t len = strlen(passwordbuffer);
1046
 
    /* Strip trailing newline */
1047
 
    if(len > 0 and passwordbuffer[len-1] == '\n'){
1048
 
      passwordbuffer[len-1] = '\0'; /* not strictly necessary */
1049
 
      len--;
1050
 
    }
1051
 
    bret = print_out_password(passwordbuffer, len);
 
1046
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
1052
1047
    if(not bret){
1053
1048
      perror("print_out_password");
1054
1049
      exitstatus = EXIT_FAILURE;