/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: 2012-02-21 21:27:14 UTC
  • mfrom: (237.6.1 nmu)
  • mto: (237.4.31 release)
  • mto: This revision was merged to the branch mainline in revision 560.
  • Revision ID: teddy@recompile.se-20120221212714-40iub281d4yvrk9r
Tags: version-1.5.3-1.1
Merge NMU change.

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-2010 Teddy Hogeborn
6
 
 * Copyright © 2008-2010 Björn Påhlsson
 
5
 * Copyright © 2008-2012 Teddy Hogeborn
 
6
 * Copyright © 2008-2012 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@fukt.bsnet.se>.
 
22
 * Contact the authors at <mandos@recompile.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@fukt.bsnet.se>";
 
82
const char *argp_program_bug_address = "<mandos@recompile.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))
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 */
199
200
}
200
201
 
201
202
/* Add to a plugin's argument vector */
 
203
__attribute__((nonnull(2)))
202
204
static bool add_argument(plugin *p, const char *arg){
203
205
  if(p == NULL){
204
206
    return false;
207
209
}
208
210
 
209
211
/* Add to a plugin's environment */
 
212
__attribute__((nonnull(2)))
210
213
static bool add_environment(plugin *p, const char *def, bool replace){
211
214
  if(p == NULL){
212
215
    return false;
286
289
}
287
290
 
288
291
/* Prints out a password to stdout */
 
292
__attribute__((nonnull))
289
293
static bool print_out_password(const char *buffer, size_t length){
290
294
  ssize_t ret;
291
295
  for(size_t written = 0; written < length; written += (size_t)ret){
299
303
}
300
304
 
301
305
/* Removes and free a plugin from the plugin list */
 
306
__attribute__((nonnull))
302
307
static void free_plugin(plugin *plugin_node){
303
308
  
304
309
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
416
421
    { .name = NULL }
417
422
  };
418
423
  
 
424
  __attribute__((nonnull(3)))
419
425
  error_t parse_opt(int key, char *arg, struct argp_state *state){
420
426
    errno = 0;
421
427
    switch(key){
422
428
      char *tmp;
423
 
      intmax_t tmpmax;
 
429
      intmax_t tmp_id;
424
430
    case 'g':                   /* --global-options */
425
431
      {
426
432
        char *plugin_option;
499
505
      /* This is already done by parse_opt_config_file() */
500
506
      break;
501
507
    case 130:                   /* --userid */
502
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
508
      tmp_id = strtoimax(arg, &tmp, 10);
503
509
      if(errno != 0 or tmp == arg or *tmp != '\0'
504
 
         or tmpmax != (uid_t)tmpmax){
 
510
         or tmp_id != (uid_t)tmp_id){
505
511
        argp_error(state, "Bad user ID number: \"%s\", using %"
506
512
                   PRIdMAX, arg, (intmax_t)uid);
507
513
        break;
508
514
      }
509
 
      uid = (uid_t)tmpmax;
 
515
      uid = (uid_t)tmp_id;
510
516
      break;
511
517
    case 131:                   /* --groupid */
512
 
      tmpmax = strtoimax(arg, &tmp, 10);
 
518
      tmp_id = strtoimax(arg, &tmp, 10);
513
519
      if(errno != 0 or tmp == arg or *tmp != '\0'
514
 
         or tmpmax != (gid_t)tmpmax){
 
520
         or tmp_id != (gid_t)tmp_id){
515
521
        argp_error(state, "Bad group ID number: \"%s\", using %"
516
522
                   PRIdMAX, arg, (intmax_t)gid);
517
523
        break;
518
524
      }
519
 
      gid = (gid_t)tmpmax;
 
525
      gid = (gid_t)tmp_id;
520
526
      break;
521
527
    case 132:                   /* --debug */
522
528
      debug = true;
742
748
    }
743
749
  }
744
750
  
745
 
  /* Strip permissions down to nobody */
 
751
  if(getuid() == 0){
 
752
    /* Work around Debian bug #633582:
 
753
       <http://bugs.debian.org/633582> */
 
754
    int plugindir_fd = open(/* plugindir or */ PDIR, O_RDONLY);
 
755
    if(plugindir_fd == -1){
 
756
      error(0, errno, "open");
 
757
    } else {
 
758
      ret = (int)TEMP_FAILURE_RETRY(fstat(plugindir_fd, &st));
 
759
      if(ret == -1){
 
760
        error(0, errno, "fstat");
 
761
      } else {
 
762
        if(S_ISDIR(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
763
          ret = fchown(plugindir_fd, uid, gid);
 
764
          if(ret == -1){
 
765
            error(0, errno, "fchown");
 
766
          }
 
767
        }
 
768
      }
 
769
      TEMP_FAILURE_RETRY(close(plugindir_fd));
 
770
    }
 
771
  }
 
772
  
 
773
  /* Lower permissions */
746
774
  setgid(gid);
747
775
  if(ret == -1){
748
776
    error(0, errno, "setgid");