/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

* plugins.d/mandos-client.c (main): Add "DELAY" environment variable.
                                    Check return value from setenv().

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-2011 Teddy Hogeborn
 
6
 * Copyright © 2008-2011 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
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 */
200
199
}
201
200
 
202
201
/* Add to a plugin's argument vector */
203
 
__attribute__((nonnull(2)))
204
202
static bool add_argument(plugin *p, const char *arg){
205
203
  if(p == NULL){
206
204
    return false;
209
207
}
210
208
 
211
209
/* Add to a plugin's environment */
212
 
__attribute__((nonnull(2)))
213
210
static bool add_environment(plugin *p, const char *def, bool replace){
214
211
  if(p == NULL){
215
212
    return false;
289
286
}
290
287
 
291
288
/* Prints out a password to stdout */
292
 
__attribute__((nonnull))
293
289
static bool print_out_password(const char *buffer, size_t length){
294
290
  ssize_t ret;
295
291
  for(size_t written = 0; written < length; written += (size_t)ret){
303
299
}
304
300
 
305
301
/* Removes and free a plugin from the plugin list */
306
 
__attribute__((nonnull))
307
302
static void free_plugin(plugin *plugin_node){
308
303
  
309
304
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
421
416
    { .name = NULL }
422
417
  };
423
418
  
424
 
  __attribute__((nonnull(3)))
425
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
426
420
    errno = 0;
427
421
    switch(key){
428
422
      char *tmp;
429
 
      intmax_t tmp_id;
 
423
      intmax_t tmpmax;
430
424
    case 'g':                   /* --global-options */
431
425
      {
432
426
        char *plugin_option;
505
499
      /* This is already done by parse_opt_config_file() */
506
500
      break;
507
501
    case 130:                   /* --userid */
508
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
502
      tmpmax = strtoimax(arg, &tmp, 10);
509
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
510
 
         or tmp_id != (uid_t)tmp_id){
 
504
         or tmpmax != (uid_t)tmpmax){
511
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
512
506
                   PRIdMAX, arg, (intmax_t)uid);
513
507
        break;
514
508
      }
515
 
      uid = (uid_t)tmp_id;
 
509
      uid = (uid_t)tmpmax;
516
510
      break;
517
511
    case 131:                   /* --groupid */
518
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
512
      tmpmax = strtoimax(arg, &tmp, 10);
519
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
520
 
         or tmp_id != (gid_t)tmp_id){
 
514
         or tmpmax != (gid_t)tmpmax){
521
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
522
516
                   PRIdMAX, arg, (intmax_t)gid);
523
517
        break;
524
518
      }
525
 
      gid = (gid_t)tmp_id;
 
519
      gid = (gid_t)tmpmax;
526
520
      break;
527
521
    case 132:                   /* --debug */
528
522
      debug = true;
748
742
    }
749
743
  }
750
744
  
751
 
  if(getuid() == 0){
 
745
  {
752
746
    /* Work around Debian bug #633582:
753
747
       <http://bugs.debian.org/633582> */
754
748
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
771
765
  }
772
766
  
773
767
  /* Lower permissions */
774
 
  ret = setgid(gid);
 
768
  setgid(gid);
775
769
  if(ret == -1){
776
770
    error(0, errno, "setgid");
777
771
  }
850
844
    {
851
845
      bool bad_name = false;
852
846
      
853
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
847
      const char const *bad_prefixes[] = { ".", "#", NULL };
854
848
      
855
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
849
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
856
850
                                           ".dpkg-old",
857
851
                                           ".dpkg-bak",
858
852
                                           ".dpkg-divert", NULL };
859
 
#pragma GCC diagnostic push
860
 
#pragma GCC diagnostic ignored "-Wcast-qual"
861
 
      for(const char **pre = (const char **)bad_prefixes;
862
 
          *pre != NULL; pre++){
863
 
#pragma GCC diagnostic pop
 
853
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
864
854
        size_t pre_len = strlen(*pre);
865
855
        if((d_name_len >= pre_len)
866
856
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
875
865
      if(bad_name){
876
866
        continue;
877
867
      }
878
 
#pragma GCC diagnostic push
879
 
#pragma GCC diagnostic ignored "-Wcast-qual"
880
 
      for(const char **suf = (const char **)bad_suffixes;
881
 
          *suf != NULL; suf++){
882
 
#pragma GCC diagnostic pop
 
868
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
883
869
        size_t suf_len = strlen(*suf);
884
870
        if((d_name_len >= suf_len)
885
871
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1079
1065
      goto fallback;
1080
1066
    }
1081
1067
    
1082
 
#if defined (__GNUC__) and defined (__GLIBC__)
1083
 
#if not __GLIBC_PREREQ(2, 16)
1084
 
#pragma GCC diagnostic push
1085
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1086
 
#endif
1087
 
#endif
1088
1068
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1089
 
                                          -Wconversion in GNU libc
1090
 
                                          before 2.16 */
1091
 
#if defined (__GNUC__) and defined (__GLIBC__)
1092
 
#if not __GLIBC_PREREQ(2, 16)
1093
 
#pragma GCC diagnostic pop
1094
 
#endif
1095
 
#endif
 
1069
                                          -Wconversion */
1096
1070
    
1097
1071
    if(maxfd < new_plugin->fd){
1098
1072
      maxfd = new_plugin->fd;
1152
1126
          }
1153
1127
          
1154
1128
          /* Remove the plugin */
1155
 
#if defined (__GNUC__) and defined (__GLIBC__)
1156
 
#if not __GLIBC_PREREQ(2, 16)
1157
 
#pragma GCC diagnostic push
1158
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1159
 
#endif
1160
 
#endif
1161
1129
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1162
 
                                          -Wconversion in GNU libc
1163
 
                                          before 2.16 */
1164
 
#if defined (__GNUC__) and defined (__GLIBC__)
1165
 
#if not __GLIBC_PREREQ(2, 16)
1166
 
#pragma GCC diagnostic pop
1167
 
#endif
1168
 
#endif
 
1130
                                          -Wconversion */
1169
1131
          
1170
1132
          /* Block signal while modifying process_list */
1171
1133
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1211
1173
      }
1212
1174
      
1213
1175
      /* This process has not completed.  Does it have any output? */
1214
 
#if defined (__GNUC__) and defined (__GLIBC__)
1215
 
#if not __GLIBC_PREREQ(2, 16)
1216
 
#pragma GCC diagnostic push
1217
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1218
 
#endif
1219
 
#endif
1220
1176
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1221
1177
                                                         warning from
1222
 
                                                         -Wconversion
1223
 
                                                         in GNU libc
1224
 
                                                         before
1225
 
                                                         2.16 */
1226
 
#if defined (__GNUC__) and defined (__GLIBC__)
1227
 
#if not __GLIBC_PREREQ(2, 16)
1228
 
#pragma GCC diagnostic pop
1229
 
#endif
1230
 
#endif
 
1178
                                                         -Wconversion */
1231
1179
        /* This process had nothing to say at this time */
1232
1180
        proc = proc->next;
1233
1181
        continue;