/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: 2011-07-16 00:29:19 UTC
  • Revision ID: teddy@fukt.bsnet.se-20110716002919-r77yikuiulj42o40
* initramfs-tools-script: Abort if plugin-runner is missing.  Removed
                          workaround for Debian bug #633582; the
                          workaround required getopt, which can not be
                          guaranteed.
* plugin-runner.c (main): Work around Debian bug #633582.
* plugins.d/mandos-client.c (main): - '' -

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2008-2013 Teddy Hogeborn
6
 
 * Copyright © 2008-2013 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 Björn Påhlsson
7
7
 * 
8
8
 * This program is free software: you can redistribute it and/or
9
9
 * modify it under the terms of the GNU General Public License as
19
19
 * along with this program.  If not, see
20
20
 * <http://www.gnu.org/licenses/>.
21
21
 * 
22
 
 * Contact the authors at <mandos@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
23
 */
24
24
 
25
25
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), getline(),
79
79
#define AFILE "/conf/conf.d/mandos/plugin-runner.conf"
80
80
 
81
81
const char *argp_program_version = "plugin-runner " VERSION;
82
 
const char *argp_program_bug_address = "<mandos@recompile.se>";
 
82
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
83
83
 
84
84
typedef struct plugin{
85
85
  char *name;                   /* can be NULL or any plugin name */
171
171
}
172
172
 
173
173
/* Helper function for add_argument and add_environment */
174
 
__attribute__((nonnull))
175
174
static bool add_to_char_array(const char *new, char ***array,
176
175
                              int *len){
177
176
  /* Resize the pointed-to array to hold one more pointer */
178
 
  char **new_array = NULL;
179
177
  do {
180
 
    new_array = realloc(*array, sizeof(char *)
181
 
                        * (size_t) ((*len) + 2));
182
 
  } while(new_array == NULL and errno == EINTR);
 
178
    *array = realloc(*array, sizeof(char *)
 
179
                     * (size_t) ((*len) + 2));
 
180
  } while(*array == NULL and errno == EINTR);
183
181
  /* Malloc check */
184
 
  if(new_array == NULL){
 
182
  if(*array == NULL){
185
183
    return false;
186
184
  }
187
 
  *array = new_array;
188
185
  /* Make a copy of the new string */
189
186
  char *copy;
190
187
  do {
202
199
}
203
200
 
204
201
/* Add to a plugin's argument vector */
205
 
__attribute__((nonnull(2)))
206
202
static bool add_argument(plugin *p, const char *arg){
207
203
  if(p == NULL){
208
204
    return false;
211
207
}
212
208
 
213
209
/* Add to a plugin's environment */
214
 
__attribute__((nonnull(2)))
215
210
static bool add_environment(plugin *p, const char *def, bool replace){
216
211
  if(p == NULL){
217
212
    return false;
219
214
  /* namelen = length of name of environment variable */
220
215
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
221
216
  /* Search for this environment variable */
222
 
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
223
 
    if(strncmp(*envdef, def, namelen + 1) == 0){
 
217
  for(char **e = p->environ; *e != NULL; e++){
 
218
    if(strncmp(*e, def, namelen + 1) == 0){
224
219
      /* It already exists */
225
220
      if(replace){
226
 
        char *new_envdef;
 
221
        char *new;
227
222
        do {
228
 
          new_envdef = realloc(*envdef, strlen(def) + 1);
229
 
        } while(new_envdef == NULL and errno == EINTR);
230
 
        if(new_envdef == NULL){
 
223
          new = realloc(*e, strlen(def) + 1);
 
224
        } while(new == NULL and errno == EINTR);
 
225
        if(new == NULL){
231
226
          return false;
232
227
        }
233
 
        *envdef = new_envdef;
234
 
        strcpy(*envdef, def);
 
228
        *e = new;
 
229
        strcpy(*e, def);
235
230
      }
236
231
      return true;
237
232
    }
291
286
}
292
287
 
293
288
/* Prints out a password to stdout */
294
 
__attribute__((nonnull))
295
289
static bool print_out_password(const char *buffer, size_t length){
296
290
  ssize_t ret;
297
291
  for(size_t written = 0; written < length; written += (size_t)ret){
305
299
}
306
300
 
307
301
/* Removes and free a plugin from the plugin list */
308
 
__attribute__((nonnull))
309
302
static void free_plugin(plugin *plugin_node){
310
303
  
311
304
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
423
416
    { .name = NULL }
424
417
  };
425
418
  
426
 
  __attribute__((nonnull(3)))
427
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
428
420
    errno = 0;
429
421
    switch(key){
430
422
      char *tmp;
431
 
      intmax_t tmp_id;
 
423
      intmax_t tmpmax;
432
424
    case 'g':                   /* --global-options */
433
425
      {
434
426
        char *plugin_option;
507
499
      /* This is already done by parse_opt_config_file() */
508
500
      break;
509
501
    case 130:                   /* --userid */
510
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
502
      tmpmax = strtoimax(arg, &tmp, 10);
511
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
512
 
         or tmp_id != (uid_t)tmp_id){
 
504
         or tmpmax != (uid_t)tmpmax){
513
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
514
506
                   PRIdMAX, arg, (intmax_t)uid);
515
507
        break;
516
508
      }
517
 
      uid = (uid_t)tmp_id;
 
509
      uid = (uid_t)tmpmax;
518
510
      break;
519
511
    case 131:                   /* --groupid */
520
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
512
      tmpmax = strtoimax(arg, &tmp, 10);
521
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
522
 
         or tmp_id != (gid_t)tmp_id){
 
514
         or tmpmax != (gid_t)tmpmax){
523
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
524
516
                   PRIdMAX, arg, (intmax_t)gid);
525
517
        break;
526
518
      }
527
 
      gid = (gid_t)tmp_id;
 
519
      gid = (gid_t)tmpmax;
528
520
      break;
529
521
    case 132:                   /* --debug */
530
522
      debug = true;
666
658
        }
667
659
        
668
660
        custom_argc += 1;
669
 
        {
670
 
          char **new_argv = realloc(custom_argv, sizeof(char *)
671
 
                                    * ((unsigned int)
672
 
                                       custom_argc + 1));
673
 
          if(new_argv == NULL){
674
 
            error(0, errno, "realloc");
675
 
            exitstatus = EX_OSERR;
676
 
            free(new_arg);
677
 
            free(org_line);
678
 
            goto fallback;
679
 
          } else {
680
 
            custom_argv = new_argv;
681
 
          }
 
661
        custom_argv = realloc(custom_argv, sizeof(char *)
 
662
                              * ((unsigned int) custom_argc + 1));
 
663
        if(custom_argv == NULL){
 
664
          error(0, errno, "realloc");
 
665
          exitstatus = EX_OSERR;
 
666
          free(org_line);
 
667
          goto fallback;
682
668
        }
683
669
        custom_argv[custom_argc-1] = new_arg;
684
670
        custom_argv[custom_argc] = NULL;
756
742
    }
757
743
  }
758
744
  
759
 
  if(getuid() == 0){
 
745
  {
760
746
    /* Work around Debian bug #633582:
761
747
       <http://bugs.debian.org/633582> */
762
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
779
765
  }
780
766
  
781
767
  /* Lower permissions */
782
 
  ret = setgid(gid);
 
768
  setgid(gid);
783
769
  if(ret == -1){
784
770
    error(0, errno, "setgid");
785
771
  }
858
844
    {
859
845
      bool bad_name = false;
860
846
      
861
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
847
      const char const *bad_prefixes[] = { ".", "#", NULL };
862
848
      
863
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
849
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
864
850
                                           ".dpkg-old",
865
851
                                           ".dpkg-bak",
866
852
                                           ".dpkg-divert", NULL };
867
 
#ifdef __GNUC__
868
 
#pragma GCC diagnostic push
869
 
#pragma GCC diagnostic ignored "-Wcast-qual"
870
 
#endif
871
 
      for(const char **pre = (const char **)bad_prefixes;
872
 
          *pre != NULL; pre++){
873
 
#ifdef __GNUC__
874
 
#pragma GCC diagnostic pop
875
 
#endif
 
853
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
876
854
        size_t pre_len = strlen(*pre);
877
855
        if((d_name_len >= pre_len)
878
856
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
887
865
      if(bad_name){
888
866
        continue;
889
867
      }
890
 
#ifdef __GNUC__
891
 
#pragma GCC diagnostic push
892
 
#pragma GCC diagnostic ignored "-Wcast-qual"
893
 
#endif
894
 
      for(const char **suf = (const char **)bad_suffixes;
895
 
          *suf != NULL; suf++){
896
 
#ifdef __GNUC__
897
 
#pragma GCC diagnostic pop
898
 
#endif
 
868
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
899
869
        size_t suf_len = strlen(*suf);
900
870
        if((d_name_len >= suf_len)
901
871
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1095
1065
      goto fallback;
1096
1066
    }
1097
1067
    
1098
 
#if defined (__GNUC__) and defined (__GLIBC__)
1099
 
#if not __GLIBC_PREREQ(2, 16)
1100
 
#pragma GCC diagnostic push
1101
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1102
 
#endif
1103
 
#endif
1104
1068
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1105
 
                                          -Wconversion in GNU libc
1106
 
                                          before 2.16 */
1107
 
#if defined (__GNUC__) and defined (__GLIBC__)
1108
 
#if not __GLIBC_PREREQ(2, 16)
1109
 
#pragma GCC diagnostic pop
1110
 
#endif
1111
 
#endif
 
1069
                                          -Wconversion */
1112
1070
    
1113
1071
    if(maxfd < new_plugin->fd){
1114
1072
      maxfd = new_plugin->fd;
1168
1126
          }
1169
1127
          
1170
1128
          /* Remove the plugin */
1171
 
#if defined (__GNUC__) and defined (__GLIBC__)
1172
 
#if not __GLIBC_PREREQ(2, 16)
1173
 
#pragma GCC diagnostic push
1174
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1175
 
#endif
1176
 
#endif
1177
1129
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1178
 
                                          -Wconversion in GNU libc
1179
 
                                          before 2.16 */
1180
 
#if defined (__GNUC__) and defined (__GLIBC__)
1181
 
#if not __GLIBC_PREREQ(2, 16)
1182
 
#pragma GCC diagnostic pop
1183
 
#endif
1184
 
#endif
 
1130
                                          -Wconversion */
1185
1131
          
1186
1132
          /* Block signal while modifying process_list */
1187
1133
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1227
1173
      }
1228
1174
      
1229
1175
      /* This process has not completed.  Does it have any output? */
1230
 
#if defined (__GNUC__) and defined (__GLIBC__)
1231
 
#if not __GLIBC_PREREQ(2, 16)
1232
 
#pragma GCC diagnostic push
1233
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1234
 
#endif
1235
 
#endif
1236
1176
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1237
1177
                                                         warning from
1238
 
                                                         -Wconversion
1239
 
                                                         in GNU libc
1240
 
                                                         before
1241
 
                                                         2.16 */
1242
 
#if defined (__GNUC__) and defined (__GLIBC__)
1243
 
#if not __GLIBC_PREREQ(2, 16)
1244
 
#pragma GCC diagnostic pop
1245
 
#endif
1246
 
#endif
 
1178
                                                         -Wconversion */
1247
1179
        /* This process had nothing to say at this time */
1248
1180
        proc = proc->next;
1249
1181
        continue;
1250
1182
      }
1251
1183
      /* Before reading, make the process' data buffer large enough */
1252
1184
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1253
 
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
1254
 
                                   + (size_t) BUFFER_SIZE);
1255
 
        if(new_buffer == NULL){
 
1185
        proc->buffer = realloc(proc->buffer, proc->buffer_size
 
1186
                               + (size_t) BUFFER_SIZE);
 
1187
        if(proc->buffer == NULL){
1256
1188
          error(0, errno, "malloc");
1257
1189
          exitstatus = EX_OSERR;
1258
1190
          goto fallback;
1259
1191
        }
1260
 
        proc->buffer = new_buffer;
1261
1192
        proc->buffer_size += BUFFER_SIZE;
1262
1193
      }
1263
1194
      /* Read from the process */