/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: 2014-03-06 02:26:04 UTC
  • Revision ID: teddy@recompile.se-20140306022604-4uc43taz25cflgi3
Bug fix: Free all memory and give better messages when memory is full.

* plugin-runner.c (add_to_char_array): Bug fix: If realloc fails, do
                                       not change old array pointer.
  (add_environment): Bug fix: If realloc fails, do not change old
                     environment pointer.  Also rename "e" to "envdef"
                     for clarity.
  (main): Bug fix: If realloc fails, do not change old pointers.  Also
          wrap "#pragma GCC" with "#ifdef ___GNUC___".
* plugins.d/mandos-client.c (incbuffer): Bug fix: if realloc fails,
                                         free old buffer.
  (run_network_hooks): Moved variables "directory" and "ret" to their
                       innermost possible scope.
  (take_down_interface): Moved variables "sd", "ret_errno", and
                         "ret_setflags" to their innermost possible
                         scope.
  (main): Removed variable "interfaces_hooks_size".  Also, if argz_add
          fails when adding all found interfaces, the error message
          will now be correct.  Also print error message if, after
          having taken up an interface, argz_add fails to add
          interface to list of interfaces to be taken down.
* plugins.d/mandos-client.xml (OPTIONS): Explain better what "none"
                                         means as argument to
                                         "--interface" by negating
                                         sense.
* plugins.d/password-prompt.c (fprintf_plus): Removed (unused).

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-2011 Teddy Hogeborn
6
 
 * Copyright © 2008-2011 Björn Påhlsson
 
5
 * Copyright © 2008-2013 Teddy Hogeborn
 
6
 * Copyright © 2008-2013 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))
174
175
static bool add_to_char_array(const char *new, char ***array,
175
176
                              int *len){
176
177
  /* Resize the pointed-to array to hold one more pointer */
 
178
  char **new_array = NULL;
177
179
  do {
178
 
    *array = realloc(*array, sizeof(char *)
179
 
                     * (size_t) ((*len) + 2));
180
 
  } while(*array == NULL and errno == EINTR);
 
180
    new_array = realloc(*array, sizeof(char *)
 
181
                        * (size_t) ((*len) + 2));
 
182
  } while(new_array == NULL and errno == EINTR);
181
183
  /* Malloc check */
182
 
  if(*array == NULL){
 
184
  if(new_array == NULL){
183
185
    return false;
184
186
  }
 
187
  *array = new_array;
185
188
  /* Make a copy of the new string */
186
189
  char *copy;
187
190
  do {
199
202
}
200
203
 
201
204
/* Add to a plugin's argument vector */
 
205
__attribute__((nonnull(2)))
202
206
static bool add_argument(plugin *p, const char *arg){
203
207
  if(p == NULL){
204
208
    return false;
207
211
}
208
212
 
209
213
/* Add to a plugin's environment */
 
214
__attribute__((nonnull(2)))
210
215
static bool add_environment(plugin *p, const char *def, bool replace){
211
216
  if(p == NULL){
212
217
    return false;
214
219
  /* namelen = length of name of environment variable */
215
220
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
216
221
  /* Search for this environment variable */
217
 
  for(char **e = p->environ; *e != NULL; e++){
218
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
222
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
223
    if(strncmp(*envdef, def, namelen + 1) == 0){
219
224
      /* It already exists */
220
225
      if(replace){
221
 
        char *new;
 
226
        char *new_envdef;
222
227
        do {
223
 
          new = realloc(*e, strlen(def) + 1);
224
 
        } while(new == NULL and errno == EINTR);
225
 
        if(new == NULL){
 
228
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
229
        } while(new_envdef == NULL and errno == EINTR);
 
230
        if(new_envdef == NULL){
226
231
          return false;
227
232
        }
228
 
        *e = new;
229
 
        strcpy(*e, def);
 
233
        *envdef = new_envdef;
 
234
        strcpy(*envdef, def);
230
235
      }
231
236
      return true;
232
237
    }
286
291
}
287
292
 
288
293
/* Prints out a password to stdout */
 
294
__attribute__((nonnull))
289
295
static bool print_out_password(const char *buffer, size_t length){
290
296
  ssize_t ret;
291
297
  for(size_t written = 0; written < length; written += (size_t)ret){
299
305
}
300
306
 
301
307
/* Removes and free a plugin from the plugin list */
 
308
__attribute__((nonnull))
302
309
static void free_plugin(plugin *plugin_node){
303
310
  
304
311
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
416
423
    { .name = NULL }
417
424
  };
418
425
  
 
426
  __attribute__((nonnull(3)))
419
427
  error_t parse_opt(int key, char *arg, struct argp_state *state){
420
428
    errno = 0;
421
429
    switch(key){
422
430
      char *tmp;
423
 
      intmax_t tmpmax;
 
431
      intmax_t tmp_id;
424
432
    case 'g':                   /* --global-options */
425
433
      {
426
434
        char *plugin_option;
499
507
      /* This is already done by parse_opt_config_file() */
500
508
      break;
501
509
    case 130:                   /* --userid */
502
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
510
      tmp_id = strtoimax(arg, &tmp, 10);
503
511
      if(errno != 0 or tmp == arg or *tmp != '\0'
504
 
         or tmpmax != (uid_t)tmpmax){
 
512
         or tmp_id != (uid_t)tmp_id){
505
513
        argp_error(state, "Bad user ID number: \"%s\", using %"
506
514
                   PRIdMAX, arg, (intmax_t)uid);
507
515
        break;
508
516
      }
509
 
      uid = (uid_t)tmpmax;
 
517
      uid = (uid_t)tmp_id;
510
518
      break;
511
519
    case 131:                   /* --groupid */
512
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
520
      tmp_id = strtoimax(arg, &tmp, 10);
513
521
      if(errno != 0 or tmp == arg or *tmp != '\0'
514
 
         or tmpmax != (gid_t)tmpmax){
 
522
         or tmp_id != (gid_t)tmp_id){
515
523
        argp_error(state, "Bad group ID number: \"%s\", using %"
516
524
                   PRIdMAX, arg, (intmax_t)gid);
517
525
        break;
518
526
      }
519
 
      gid = (gid_t)tmpmax;
 
527
      gid = (gid_t)tmp_id;
520
528
      break;
521
529
    case 132:                   /* --debug */
522
530
      debug = true;
658
666
        }
659
667
        
660
668
        custom_argc += 1;
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;
 
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
          }
668
682
        }
669
683
        custom_argv[custom_argc-1] = new_arg;
670
684
        custom_argv[custom_argc] = NULL;
742
756
    }
743
757
  }
744
758
  
745
 
  {
 
759
  if(getuid() == 0){
746
760
    /* Work around Debian bug #633582:
747
761
       <http://bugs.debian.org/633582> */
748
762
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
765
779
  }
766
780
  
767
781
  /* Lower permissions */
768
 
  setgid(gid);
 
782
  ret = setgid(gid);
769
783
  if(ret == -1){
770
784
    error(0, errno, "setgid");
771
785
  }
844
858
    {
845
859
      bool bad_name = false;
846
860
      
847
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
861
      const char * const bad_prefixes[] = { ".", "#", NULL };
848
862
      
849
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
863
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
850
864
                                           ".dpkg-old",
851
865
                                           ".dpkg-bak",
852
866
                                           ".dpkg-divert", NULL };
853
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
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
854
876
        size_t pre_len = strlen(*pre);
855
877
        if((d_name_len >= pre_len)
856
878
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
865
887
      if(bad_name){
866
888
        continue;
867
889
      }
868
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
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
869
899
        size_t suf_len = strlen(*suf);
870
900
        if((d_name_len >= suf_len)
871
901
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1065
1095
      goto fallback;
1066
1096
    }
1067
1097
    
 
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
1068
1104
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1069
 
                                          -Wconversion */
 
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
1070
1112
    
1071
1113
    if(maxfd < new_plugin->fd){
1072
1114
      maxfd = new_plugin->fd;
1126
1168
          }
1127
1169
          
1128
1170
          /* 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
1129
1177
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1130
 
                                          -Wconversion */
 
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
1131
1185
          
1132
1186
          /* Block signal while modifying process_list */
1133
1187
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1173
1227
      }
1174
1228
      
1175
1229
      /* 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
1176
1236
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1177
1237
                                                         warning from
1178
 
                                                         -Wconversion */
 
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
1179
1247
        /* This process had nothing to say at this time */
1180
1248
        proc = proc->next;
1181
1249
        continue;
1182
1250
      }
1183
1251
      /* Before reading, make the process' data buffer large enough */
1184
1252
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1185
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1186
 
                               + (size_t) BUFFER_SIZE);
1187
 
        if(proc->buffer == NULL){
 
1253
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1254
                                   + (size_t) BUFFER_SIZE);
 
1255
        if(new_buffer == NULL){
1188
1256
          error(0, errno, "malloc");
1189
1257
          exitstatus = EX_OSERR;
1190
1258
          goto fallback;
1191
1259
        }
 
1260
        proc->buffer = new_buffer;
1192
1261
        proc->buffer_size += BUFFER_SIZE;
1193
1262
      }
1194
1263
      /* Read from the process */