/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-01-06 15:56:54 UTC
  • Revision ID: teddy@recompile.se-20140106155654-urx23ytuvy0nxzwp
Update init script to modern standards.

* init.d-mandos (status): Moved to standard location.  Re-added use of
                          -p switch compared to skeleton file.
  (*): Update message to include "status".

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-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
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 */
746
 
  setgid(gid);
 
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 */
 
774
  ret = setgid(gid);
747
775
  if(ret == -1){
748
776
    error(0, errno, "setgid");
749
777
  }
1043
1071
      goto fallback;
1044
1072
    }
1045
1073
    
 
1074
#if defined (__GNUC__) and defined (__GLIBC__)
 
1075
#if not __GLIBC_PREREQ(2, 16)
 
1076
#pragma GCC diagnostic push
 
1077
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1078
#endif
 
1079
#endif
1046
1080
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1047
 
                                          -Wconversion */
 
1081
                                          -Wconversion in GNU libc
 
1082
                                          before 2.16 */
 
1083
#if defined (__GNUC__) and defined (__GLIBC__)
 
1084
#if not __GLIBC_PREREQ(2, 16)
 
1085
#pragma GCC diagnostic pop
 
1086
#endif
 
1087
#endif
1048
1088
    
1049
1089
    if(maxfd < new_plugin->fd){
1050
1090
      maxfd = new_plugin->fd;
1104
1144
          }
1105
1145
          
1106
1146
          /* Remove the plugin */
 
1147
#if defined (__GNUC__) and defined (__GLIBC__)
 
1148
#if not __GLIBC_PREREQ(2, 16)
 
1149
#pragma GCC diagnostic push
 
1150
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1151
#endif
 
1152
#endif
1107
1153
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1108
 
                                          -Wconversion */
 
1154
                                          -Wconversion in GNU libc
 
1155
                                          before 2.16 */
 
1156
#if defined (__GNUC__) and defined (__GLIBC__)
 
1157
#if not __GLIBC_PREREQ(2, 16)
 
1158
#pragma GCC diagnostic pop
 
1159
#endif
 
1160
#endif
1109
1161
          
1110
1162
          /* Block signal while modifying process_list */
1111
1163
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1151
1203
      }
1152
1204
      
1153
1205
      /* This process has not completed.  Does it have any output? */
 
1206
#if defined (__GNUC__) and defined (__GLIBC__)
 
1207
#if not __GLIBC_PREREQ(2, 16)
 
1208
#pragma GCC diagnostic push
 
1209
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1210
#endif
 
1211
#endif
1154
1212
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1155
1213
                                                         warning from
1156
 
                                                         -Wconversion */
 
1214
                                                         -Wconversion
 
1215
                                                         in GNU libc
 
1216
                                                         before
 
1217
                                                         2.16 */
 
1218
#if defined (__GNUC__) and defined (__GLIBC__)
 
1219
#if not __GLIBC_PREREQ(2, 16)
 
1220
#pragma GCC diagnostic pop
 
1221
#endif
 
1222
#endif
1157
1223
        /* This process had nothing to say at this time */
1158
1224
        proc = proc->next;
1159
1225
        continue;