/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-23 19:55:18 UTC
  • Revision ID: teddy@recompile.se-20140323195518-amvoytwi12iheypo
Make mandos-client prefer /run/tmp over /tmp.

* plugins.d/mandos-client.c (init_gpgme): Add more const.
  (main): Make "tempdir" a pointer to the temporary directory, if one
          has been created, or NULL.  This also eliminates the need
          for the "tempdir_created" variable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
105
105
 
106
106
/* Gets an existing plugin based on name,
107
107
   or if none is found, creates a new one */
 
108
__attribute__((warn_unused_result))
108
109
static plugin *getplugin(char *name){
109
110
  /* Check for existing plugin with that name */
110
111
  for(plugin *p = plugin_list; p != NULL; p = p->next){
171
172
}
172
173
 
173
174
/* Helper function for add_argument and add_environment */
174
 
__attribute__((nonnull))
 
175
__attribute__((nonnull, warn_unused_result))
175
176
static bool add_to_char_array(const char *new, char ***array,
176
177
                              int *len){
177
178
  /* Resize the pointed-to array to hold one more pointer */
 
179
  char **new_array = NULL;
178
180
  do {
179
 
    *array = realloc(*array, sizeof(char *)
180
 
                     * (size_t) ((*len) + 2));
181
 
  } while(*array == NULL and errno == EINTR);
 
181
    new_array = realloc(*array, sizeof(char *)
 
182
                        * (size_t) ((*len) + 2));
 
183
  } while(new_array == NULL and errno == EINTR);
182
184
  /* Malloc check */
183
 
  if(*array == NULL){
 
185
  if(new_array == NULL){
184
186
    return false;
185
187
  }
 
188
  *array = new_array;
186
189
  /* Make a copy of the new string */
187
190
  char *copy;
188
191
  do {
200
203
}
201
204
 
202
205
/* Add to a plugin's argument vector */
203
 
__attribute__((nonnull(2)))
 
206
__attribute__((nonnull(2), warn_unused_result))
204
207
static bool add_argument(plugin *p, const char *arg){
205
208
  if(p == NULL){
206
209
    return false;
209
212
}
210
213
 
211
214
/* Add to a plugin's environment */
212
 
__attribute__((nonnull(2)))
 
215
__attribute__((nonnull(2), warn_unused_result))
213
216
static bool add_environment(plugin *p, const char *def, bool replace){
214
217
  if(p == NULL){
215
218
    return false;
217
220
  /* namelen = length of name of environment variable */
218
221
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
219
222
  /* Search for this environment variable */
220
 
  for(char **e = p->environ; *e != NULL; e++){
221
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
223
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
224
    if(strncmp(*envdef, def, namelen + 1) == 0){
222
225
      /* It already exists */
223
226
      if(replace){
224
 
        char *new;
 
227
        char *new_envdef;
225
228
        do {
226
 
          new = realloc(*e, strlen(def) + 1);
227
 
        } while(new == NULL and errno == EINTR);
228
 
        if(new == NULL){
 
229
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
230
        } while(new_envdef == NULL and errno == EINTR);
 
231
        if(new_envdef == NULL){
229
232
          return false;
230
233
        }
231
 
        *e = new;
232
 
        strcpy(*e, def);
 
234
        *envdef = new_envdef;
 
235
        strcpy(*envdef, def);
233
236
      }
234
237
      return true;
235
238
    }
242
245
 * Descriptor Flags".
243
246
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
244
247
 */
 
248
__attribute__((warn_unused_result))
245
249
static int set_cloexec_flag(int fd){
246
250
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
247
251
  /* If reading the flags failed, return error indication now. */
289
293
}
290
294
 
291
295
/* Prints out a password to stdout */
292
 
__attribute__((nonnull))
 
296
__attribute__((nonnull, warn_unused_result))
293
297
static bool print_out_password(const char *buffer, size_t length){
294
298
  ssize_t ret;
295
299
  for(size_t written = 0; written < length; written += (size_t)ret){
435
439
            break;
436
440
          }
437
441
        }
 
442
        errno = 0;
438
443
      }
439
444
      break;
440
445
    case 'G':                   /* --global-env */
441
 
      add_environment(getplugin(NULL), arg, true);
 
446
      if(add_environment(getplugin(NULL), arg, true)){
 
447
        errno = 0;
 
448
      }
442
449
      break;
443
450
    case 'o':                   /* --options-for */
444
451
      {
461
468
            break;
462
469
          }
463
470
        }
 
471
        errno = 0;
464
472
      }
465
473
      break;
466
474
    case 'E':                   /* --env-for */
478
486
          errno = EINVAL;
479
487
          break;
480
488
        }
481
 
        add_environment(getplugin(arg), envdef, true);
 
489
        if(add_environment(getplugin(arg), envdef, true)){
 
490
          errno = 0;
 
491
        }
482
492
      }
483
493
      break;
484
494
    case 'd':                   /* --disable */
486
496
        plugin *p = getplugin(arg);
487
497
        if(p != NULL){
488
498
          p->disabled = true;
 
499
          errno = 0;
489
500
        }
490
501
      }
491
502
      break;
494
505
        plugin *p = getplugin(arg);
495
506
        if(p != NULL){
496
507
          p->disabled = false;
 
508
          errno = 0;
497
509
        }
498
510
      }
499
511
      break;
500
512
    case 128:                   /* --plugin-dir */
501
513
      free(plugindir);
502
514
      plugindir = strdup(arg);
 
515
      if(plugindir != NULL){
 
516
        errno = 0;
 
517
      }
503
518
      break;
504
519
    case 129:                   /* --config-file */
505
520
      /* This is already done by parse_opt_config_file() */
513
528
        break;
514
529
      }
515
530
      uid = (uid_t)tmp_id;
 
531
      errno = 0;
516
532
      break;
517
533
    case 131:                   /* --groupid */
518
534
      tmp_id = strtoimax(arg, &tmp, 10);
523
539
        break;
524
540
      }
525
541
      gid = (gid_t)tmp_id;
 
542
      errno = 0;
526
543
      break;
527
544
    case 132:                   /* --debug */
528
545
      debug = true;
576
593
    case 129:                   /* --config-file */
577
594
      free(argfile);
578
595
      argfile = strdup(arg);
 
596
      if(argfile != NULL){
 
597
        errno = 0;
 
598
      }
579
599
      break;
580
600
    case 130:                   /* --userid */
581
601
    case 131:                   /* --groupid */
664
684
        }
665
685
        
666
686
        custom_argc += 1;
667
 
        custom_argv = realloc(custom_argv, sizeof(char *)
668
 
                              * ((unsigned int) custom_argc + 1));
669
 
        if(custom_argv == NULL){
670
 
          error(0, errno, "realloc");
671
 
          exitstatus = EX_OSERR;
672
 
          free(org_line);
673
 
          goto fallback;
 
687
        {
 
688
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
689
                                    * ((unsigned int)
 
690
                                       custom_argc + 1));
 
691
          if(new_argv == NULL){
 
692
            error(0, errno, "realloc");
 
693
            exitstatus = EX_OSERR;
 
694
            free(new_arg);
 
695
            free(org_line);
 
696
            goto fallback;
 
697
          } else {
 
698
            custom_argv = new_argv;
 
699
          }
674
700
        }
675
701
        custom_argv[custom_argc-1] = new_arg;
676
702
        custom_argv[custom_argc] = NULL;
850
876
    {
851
877
      bool bad_name = false;
852
878
      
853
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
879
      const char * const bad_prefixes[] = { ".", "#", NULL };
854
880
      
855
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
881
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
856
882
                                           ".dpkg-old",
857
883
                                           ".dpkg-bak",
858
884
                                           ".dpkg-divert", NULL };
859
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
885
#ifdef __GNUC__
 
886
#pragma GCC diagnostic push
 
887
#pragma GCC diagnostic ignored "-Wcast-qual"
 
888
#endif
 
889
      for(const char **pre = (const char **)bad_prefixes;
 
890
          *pre != NULL; pre++){
 
891
#ifdef __GNUC__
 
892
#pragma GCC diagnostic pop
 
893
#endif
860
894
        size_t pre_len = strlen(*pre);
861
895
        if((d_name_len >= pre_len)
862
896
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
871
905
      if(bad_name){
872
906
        continue;
873
907
      }
874
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
908
#ifdef __GNUC__
 
909
#pragma GCC diagnostic push
 
910
#pragma GCC diagnostic ignored "-Wcast-qual"
 
911
#endif
 
912
      for(const char **suf = (const char **)bad_suffixes;
 
913
          *suf != NULL; suf++){
 
914
#ifdef __GNUC__
 
915
#pragma GCC diagnostic pop
 
916
#endif
875
917
        size_t suf_len = strlen(*suf);
876
918
        if((d_name_len >= suf_len)
877
919
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1226
1268
      }
1227
1269
      /* Before reading, make the process' data buffer large enough */
1228
1270
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1229
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1230
 
                               + (size_t) BUFFER_SIZE);
1231
 
        if(proc->buffer == NULL){
 
1271
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1272
                                   + (size_t) BUFFER_SIZE);
 
1273
        if(new_buffer == NULL){
1232
1274
          error(0, errno, "malloc");
1233
1275
          exitstatus = EX_OSERR;
1234
1276
          goto fallback;
1235
1277
        }
 
1278
        proc->buffer = new_buffer;
1236
1279
        proc->buffer_size += BUFFER_SIZE;
1237
1280
      }
1238
1281
      /* Read from the process */