/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: 2010-11-16 17:58:49 UTC
  • Revision ID: teddy@fukt.bsnet.se-20101116175849-m0fttdoskhahgyup
* plugins.d/plymouth.c: Fixed comment to "Plymouth" instead of "Usplash".
 (makeprompt): Mimic the default prompt.
 (exec_and_wait): Correct type of "tmp"; remove cast.  Also remove
                  unnecessary cast of NULL.

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-2013 Teddy Hogeborn
6
 
 * Copyright © 2008-2013 Björn Påhlsson
 
5
 * Copyright © 2008-2010 Teddy Hogeborn
 
6
 * Copyright © 2008-2010 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@recompile.se>.
 
22
 * Contact the authors at <mandos@fukt.bsnet.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@recompile.se>";
 
82
const char *argp_program_bug_address = "<mandos@fukt.bsnet.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))
175
174
static bool add_to_char_array(const char *new, char ***array,
176
175
                              int *len){
177
176
  /* Resize the pointed-to array to hold one more pointer */
200
199
}
201
200
 
202
201
/* Add to a plugin's argument vector */
203
 
__attribute__((nonnull(2)))
204
202
static bool add_argument(plugin *p, const char *arg){
205
203
  if(p == NULL){
206
204
    return false;
209
207
}
210
208
 
211
209
/* Add to a plugin's environment */
212
 
__attribute__((nonnull(2)))
213
210
static bool add_environment(plugin *p, const char *def, bool replace){
214
211
  if(p == NULL){
215
212
    return false;
289
286
}
290
287
 
291
288
/* Prints out a password to stdout */
292
 
__attribute__((nonnull))
293
289
static bool print_out_password(const char *buffer, size_t length){
294
290
  ssize_t ret;
295
291
  for(size_t written = 0; written < length; written += (size_t)ret){
303
299
}
304
300
 
305
301
/* Removes and free a plugin from the plugin list */
306
 
__attribute__((nonnull))
307
302
static void free_plugin(plugin *plugin_node){
308
303
  
309
304
  for(char **arg = plugin_node->argv; *arg != NULL; arg++){
421
416
    { .name = NULL }
422
417
  };
423
418
  
424
 
  __attribute__((nonnull(3)))
425
419
  error_t parse_opt(int key, char *arg, struct argp_state *state){
426
420
    errno = 0;
427
421
    switch(key){
428
422
      char *tmp;
429
 
      intmax_t tmp_id;
 
423
      intmax_t tmpmax;
430
424
    case 'g':                   /* --global-options */
431
425
      {
432
426
        char *plugin_option;
505
499
      /* This is already done by parse_opt_config_file() */
506
500
      break;
507
501
    case 130:                   /* --userid */
508
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
502
      tmpmax = strtoimax(arg, &tmp, 10);
509
503
      if(errno != 0 or tmp == arg or *tmp != '\0'
510
 
         or tmp_id != (uid_t)tmp_id){
 
504
         or tmpmax != (uid_t)tmpmax){
511
505
        argp_error(state, "Bad user ID number: \"%s\", using %"
512
506
                   PRIdMAX, arg, (intmax_t)uid);
513
507
        break;
514
508
      }
515
 
      uid = (uid_t)tmp_id;
 
509
      uid = (uid_t)tmpmax;
516
510
      break;
517
511
    case 131:                   /* --groupid */
518
 
      tmp_id = strtoimax(arg, &tmp, 10);
 
512
      tmpmax = strtoimax(arg, &tmp, 10);
519
513
      if(errno != 0 or tmp == arg or *tmp != '\0'
520
 
         or tmp_id != (gid_t)tmp_id){
 
514
         or tmpmax != (gid_t)tmpmax){
521
515
        argp_error(state, "Bad group ID number: \"%s\", using %"
522
516
                   PRIdMAX, arg, (intmax_t)gid);
523
517
        break;
524
518
      }
525
 
      gid = (gid_t)tmp_id;
 
519
      gid = (gid_t)tmpmax;
526
520
      break;
527
521
    case 132:                   /* --debug */
528
522
      debug = true;
748
742
    }
749
743
  }
750
744
  
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);
 
745
  /* Strip permissions down to nobody */
 
746
  setgid(gid);
775
747
  if(ret == -1){
776
748
    error(0, errno, "setgid");
777
749
  }
850
822
    {
851
823
      bool bad_name = false;
852
824
      
853
 
      const char * const bad_prefixes[] = { ".", "#", NULL };
 
825
      const char const *bad_prefixes[] = { ".", "#", NULL };
854
826
      
855
 
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
 
827
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
856
828
                                           ".dpkg-old",
857
829
                                           ".dpkg-bak",
858
830
                                           ".dpkg-divert", NULL };
859
 
#pragma GCC diagnostic push
860
 
#pragma GCC diagnostic ignored "-Wcast-qual"
861
 
      for(const char **pre = (const char **)bad_prefixes;
862
 
          *pre != NULL; pre++){
863
 
#pragma GCC diagnostic pop
 
831
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
864
832
        size_t pre_len = strlen(*pre);
865
833
        if((d_name_len >= pre_len)
866
834
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
875
843
      if(bad_name){
876
844
        continue;
877
845
      }
878
 
#pragma GCC diagnostic push
879
 
#pragma GCC diagnostic ignored "-Wcast-qual"
880
 
      for(const char **suf = (const char **)bad_suffixes;
881
 
          *suf != NULL; suf++){
882
 
#pragma GCC diagnostic pop
 
846
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
883
847
        size_t suf_len = strlen(*suf);
884
848
        if((d_name_len >= suf_len)
885
849
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1079
1043
      goto fallback;
1080
1044
    }
1081
1045
    
1082
 
#if defined (__GNUC__) and defined (__GLIBC__)
1083
 
#if not __GLIBC_PREREQ(2, 16)
1084
 
#pragma GCC diagnostic push
1085
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1086
 
#endif
1087
 
#endif
1088
1046
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1089
 
                                          -Wconversion in GNU libc
1090
 
                                          before 2.16 */
1091
 
#if defined (__GNUC__) and defined (__GLIBC__)
1092
 
#if not __GLIBC_PREREQ(2, 16)
1093
 
#pragma GCC diagnostic pop
1094
 
#endif
1095
 
#endif
 
1047
                                          -Wconversion */
1096
1048
    
1097
1049
    if(maxfd < new_plugin->fd){
1098
1050
      maxfd = new_plugin->fd;
1152
1104
          }
1153
1105
          
1154
1106
          /* Remove the plugin */
1155
 
#if defined (__GNUC__) and defined (__GLIBC__)
1156
 
#if not __GLIBC_PREREQ(2, 16)
1157
 
#pragma GCC diagnostic push
1158
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1159
 
#endif
1160
 
#endif
1161
1107
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1162
 
                                          -Wconversion in GNU libc
1163
 
                                          before 2.16 */
1164
 
#if defined (__GNUC__) and defined (__GLIBC__)
1165
 
#if not __GLIBC_PREREQ(2, 16)
1166
 
#pragma GCC diagnostic pop
1167
 
#endif
1168
 
#endif
 
1108
                                          -Wconversion */
1169
1109
          
1170
1110
          /* Block signal while modifying process_list */
1171
1111
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1211
1151
      }
1212
1152
      
1213
1153
      /* This process has not completed.  Does it have any output? */
1214
 
#if defined (__GNUC__) and defined (__GLIBC__)
1215
 
#if not __GLIBC_PREREQ(2, 16)
1216
 
#pragma GCC diagnostic push
1217
 
#pragma GCC diagnostic ignored "-Wsign-conversion"
1218
 
#endif
1219
 
#endif
1220
1154
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1221
1155
                                                         warning from
1222
 
                                                         -Wconversion
1223
 
                                                         in GNU libc
1224
 
                                                         before
1225
 
                                                         2.16 */
1226
 
#if defined (__GNUC__) and defined (__GLIBC__)
1227
 
#if not __GLIBC_PREREQ(2, 16)
1228
 
#pragma GCC diagnostic pop
1229
 
#endif
1230
 
#endif
 
1156
                                                         -Wconversion */
1231
1157
        /* This process had nothing to say at this time */
1232
1158
        proc = proc->next;
1233
1159
        continue;