/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 20:48:58 UTC
  • Revision ID: teddy@recompile.se-20140323204858-l1x3aiick21oog4y
Improve error message when working around Debian bug #633582.

* plugins-runner.c (main): Improve error message when failing to work
                           around Debian bug #633582.  Also simplify
                           code to always use execve().

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
                                   F_GETFD, F_SETFD, FD_CLOEXEC,
50
50
                                   access(), pipe(), fork(), close()
51
51
                                   dup2(), STDOUT_FILENO, _exit(),
52
 
                                   execv(), write(), read(),
 
52
                                   execve(), write(), read(),
53
53
                                   close() */
54
54
#include <fcntl.h>              /* fcntl(), F_GETFD, F_SETFD,
55
55
                                   FD_CLOEXEC */
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 */
202
203
}
203
204
 
204
205
/* Add to a plugin's argument vector */
205
 
__attribute__((nonnull(2)))
 
206
__attribute__((nonnull(2), warn_unused_result))
206
207
static bool add_argument(plugin *p, const char *arg){
207
208
  if(p == NULL){
208
209
    return false;
211
212
}
212
213
 
213
214
/* Add to a plugin's environment */
214
 
__attribute__((nonnull(2)))
 
215
__attribute__((nonnull(2), warn_unused_result))
215
216
static bool add_environment(plugin *p, const char *def, bool replace){
216
217
  if(p == NULL){
217
218
    return false;
244
245
 * Descriptor Flags".
245
246
 | [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
246
247
 */
 
248
__attribute__((warn_unused_result))
247
249
static int set_cloexec_flag(int fd){
248
250
  int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
249
251
  /* If reading the flags failed, return error indication now. */
291
293
}
292
294
 
293
295
/* Prints out a password to stdout */
294
 
__attribute__((nonnull))
 
296
__attribute__((nonnull, warn_unused_result))
295
297
static bool print_out_password(const char *buffer, size_t length){
296
298
  ssize_t ret;
297
299
  for(size_t written = 0; written < length; written += (size_t)ret){
437
439
            break;
438
440
          }
439
441
        }
 
442
        errno = 0;
440
443
      }
441
444
      break;
442
445
    case 'G':                   /* --global-env */
443
 
      add_environment(getplugin(NULL), arg, true);
 
446
      if(add_environment(getplugin(NULL), arg, true)){
 
447
        errno = 0;
 
448
      }
444
449
      break;
445
450
    case 'o':                   /* --options-for */
446
451
      {
463
468
            break;
464
469
          }
465
470
        }
 
471
        errno = 0;
466
472
      }
467
473
      break;
468
474
    case 'E':                   /* --env-for */
480
486
          errno = EINVAL;
481
487
          break;
482
488
        }
483
 
        add_environment(getplugin(arg), envdef, true);
 
489
        if(add_environment(getplugin(arg), envdef, true)){
 
490
          errno = 0;
 
491
        }
484
492
      }
485
493
      break;
486
494
    case 'd':                   /* --disable */
488
496
        plugin *p = getplugin(arg);
489
497
        if(p != NULL){
490
498
          p->disabled = true;
 
499
          errno = 0;
491
500
        }
492
501
      }
493
502
      break;
496
505
        plugin *p = getplugin(arg);
497
506
        if(p != NULL){
498
507
          p->disabled = false;
 
508
          errno = 0;
499
509
        }
500
510
      }
501
511
      break;
502
512
    case 128:                   /* --plugin-dir */
503
513
      free(plugindir);
504
514
      plugindir = strdup(arg);
 
515
      if(plugindir != NULL){
 
516
        errno = 0;
 
517
      }
505
518
      break;
506
519
    case 129:                   /* --config-file */
507
520
      /* This is already done by parse_opt_config_file() */
515
528
        break;
516
529
      }
517
530
      uid = (uid_t)tmp_id;
 
531
      errno = 0;
518
532
      break;
519
533
    case 131:                   /* --groupid */
520
534
      tmp_id = strtoimax(arg, &tmp, 10);
525
539
        break;
526
540
      }
527
541
      gid = (gid_t)tmp_id;
 
542
      errno = 0;
528
543
      break;
529
544
    case 132:                   /* --debug */
530
545
      debug = true;
578
593
    case 129:                   /* --config-file */
579
594
      free(argfile);
580
595
      argfile = strdup(arg);
 
596
      if(argfile != NULL){
 
597
        errno = 0;
 
598
      }
581
599
      break;
582
600
    case 130:                   /* --userid */
583
601
    case 131:                   /* --groupid */
761
779
       <http://bugs.debian.org/633582> */
762
780
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
763
781
    if(plugindir_fd == -1){
764
 
      error(0, errno, "open");
 
782
      error(0, errno, "open(\"" PDIR "\")");
765
783
    } else {
766
784
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
767
785
      if(ret == -1){
1051
1069
           above and must now close it manually here. */
1052
1070
        closedir(dir);
1053
1071
      }
1054
 
      if(p->environ[0] == NULL){
1055
 
        if(execv(filename, p->argv) < 0){
1056
 
          error(0, errno, "execv for %s", filename);
1057
 
          _exit(EX_OSERR);
1058
 
        }
1059
 
      } else {
1060
 
        if(execve(filename, p->argv, p->environ) < 0){
1061
 
          error(0, errno, "execve for %s", filename);
1062
 
          _exit(EX_OSERR);
1063
 
        }
 
1072
      if(execve(filename, p->argv,
 
1073
                (p->environ[0] != NULL) ? p->environ : environ) < 0){
 
1074
        error(0, errno, "execve for %s", filename);
 
1075
        _exit(EX_OSERR);
1064
1076
      }
1065
1077
      /* no return */
1066
1078
    }