/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-09 03:02:43 UTC
  • Revision ID: teddy@recompile.se-20140309030243-0gc6l2yuqbzgyuyt
Use getnameinfo() instead of inet_ntop() in mandos-client.

* plugins.d/mandos-client.c (start_mandos_communication): Use
                                                          getnameinfo()
                                                          instead of
                                                          inet_ntop().

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-2012 Teddy Hogeborn
6
 
 * Copyright © 2008-2012 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
175
175
static bool add_to_char_array(const char *new, char ***array,
176
176
                              int *len){
177
177
  /* Resize the pointed-to array to hold one more pointer */
 
178
  char **new_array = NULL;
178
179
  do {
179
 
    *array = realloc(*array, sizeof(char *)
180
 
                     * (size_t) ((*len) + 2));
181
 
  } while(*array == NULL and errno == EINTR);
 
180
    new_array = realloc(*array, sizeof(char *)
 
181
                        * (size_t) ((*len) + 2));
 
182
  } while(new_array == NULL and errno == EINTR);
182
183
  /* Malloc check */
183
 
  if(*array == NULL){
 
184
  if(new_array == NULL){
184
185
    return false;
185
186
  }
 
187
  *array = new_array;
186
188
  /* Make a copy of the new string */
187
189
  char *copy;
188
190
  do {
217
219
  /* namelen = length of name of environment variable */
218
220
  size_t namelen = (size_t)(strchrnul(def, '=') - def);
219
221
  /* Search for this environment variable */
220
 
  for(char **e = p->environ; *e != NULL; e++){
221
 
    if(strncmp(*e, def, namelen + 1) == 0){
 
222
  for(char **envdef = p->environ; *envdef != NULL; envdef++){
 
223
    if(strncmp(*envdef, def, namelen + 1) == 0){
222
224
      /* It already exists */
223
225
      if(replace){
224
 
        char *new;
 
226
        char *new_envdef;
225
227
        do {
226
 
          new = realloc(*e, strlen(def) + 1);
227
 
        } while(new == NULL and errno == EINTR);
228
 
        if(new == NULL){
 
228
          new_envdef = realloc(*envdef, strlen(def) + 1);
 
229
        } while(new_envdef == NULL and errno == EINTR);
 
230
        if(new_envdef == NULL){
229
231
          return false;
230
232
        }
231
 
        *e = new;
232
 
        strcpy(*e, def);
 
233
        *envdef = new_envdef;
 
234
        strcpy(*envdef, def);
233
235
      }
234
236
      return true;
235
237
    }
664
666
        }
665
667
        
666
668
        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;
 
669
        {
 
670
          char **new_argv = realloc(custom_argv, sizeof(char *)
 
671
                                    * ((unsigned int)
 
672
                                       custom_argc + 1));
 
673
          if(new_argv == NULL){
 
674
            error(0, errno, "realloc");
 
675
            exitstatus = EX_OSERR;
 
676
            free(new_arg);
 
677
            free(org_line);
 
678
            goto fallback;
 
679
          } else {
 
680
            custom_argv = new_argv;
 
681
          }
674
682
        }
675
683
        custom_argv[custom_argc-1] = new_arg;
676
684
        custom_argv[custom_argc] = NULL;
771
779
  }
772
780
  
773
781
  /* Lower permissions */
774
 
  setgid(gid);
 
782
  ret = setgid(gid);
775
783
  if(ret == -1){
776
784
    error(0, errno, "setgid");
777
785
  }
850
858
    {
851
859
      bool bad_name = false;
852
860
      
853
 
      const char const *bad_prefixes[] = { ".", "#", NULL };
 
861
      const char * const bad_prefixes[] = { ".", "#", NULL };
854
862
      
855
 
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
 
863
      const char * const bad_suffixes[] = { "~", "#", ".dpkg-new",
856
864
                                           ".dpkg-old",
857
865
                                           ".dpkg-bak",
858
866
                                           ".dpkg-divert", NULL };
859
 
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
 
867
#ifdef __GNUC__
 
868
#pragma GCC diagnostic push
 
869
#pragma GCC diagnostic ignored "-Wcast-qual"
 
870
#endif
 
871
      for(const char **pre = (const char **)bad_prefixes;
 
872
          *pre != NULL; pre++){
 
873
#ifdef __GNUC__
 
874
#pragma GCC diagnostic pop
 
875
#endif
860
876
        size_t pre_len = strlen(*pre);
861
877
        if((d_name_len >= pre_len)
862
878
           and strncmp((dirst->d_name), *pre, pre_len) == 0){
871
887
      if(bad_name){
872
888
        continue;
873
889
      }
874
 
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
 
890
#ifdef __GNUC__
 
891
#pragma GCC diagnostic push
 
892
#pragma GCC diagnostic ignored "-Wcast-qual"
 
893
#endif
 
894
      for(const char **suf = (const char **)bad_suffixes;
 
895
          *suf != NULL; suf++){
 
896
#ifdef __GNUC__
 
897
#pragma GCC diagnostic pop
 
898
#endif
875
899
        size_t suf_len = strlen(*suf);
876
900
        if((d_name_len >= suf_len)
877
901
           and (strcmp((dirst->d_name) + d_name_len-suf_len, *suf)
1071
1095
      goto fallback;
1072
1096
    }
1073
1097
    
 
1098
#if defined (__GNUC__) and defined (__GLIBC__)
 
1099
#if not __GLIBC_PREREQ(2, 16)
 
1100
#pragma GCC diagnostic push
 
1101
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1102
#endif
 
1103
#endif
1074
1104
    FD_SET(new_plugin->fd, &rfds_all); /* Spurious warning from
1075
 
                                          -Wconversion */
 
1105
                                          -Wconversion in GNU libc
 
1106
                                          before 2.16 */
 
1107
#if defined (__GNUC__) and defined (__GLIBC__)
 
1108
#if not __GLIBC_PREREQ(2, 16)
 
1109
#pragma GCC diagnostic pop
 
1110
#endif
 
1111
#endif
1076
1112
    
1077
1113
    if(maxfd < new_plugin->fd){
1078
1114
      maxfd = new_plugin->fd;
1132
1168
          }
1133
1169
          
1134
1170
          /* Remove the plugin */
 
1171
#if defined (__GNUC__) and defined (__GLIBC__)
 
1172
#if not __GLIBC_PREREQ(2, 16)
 
1173
#pragma GCC diagnostic push
 
1174
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1175
#endif
 
1176
#endif
1135
1177
          FD_CLR(proc->fd, &rfds_all); /* Spurious warning from
1136
 
                                          -Wconversion */
 
1178
                                          -Wconversion in GNU libc
 
1179
                                          before 2.16 */
 
1180
#if defined (__GNUC__) and defined (__GLIBC__)
 
1181
#if not __GLIBC_PREREQ(2, 16)
 
1182
#pragma GCC diagnostic pop
 
1183
#endif
 
1184
#endif
1137
1185
          
1138
1186
          /* Block signal while modifying process_list */
1139
1187
          ret = (int)TEMP_FAILURE_RETRY(sigprocmask
1179
1227
      }
1180
1228
      
1181
1229
      /* This process has not completed.  Does it have any output? */
 
1230
#if defined (__GNUC__) and defined (__GLIBC__)
 
1231
#if not __GLIBC_PREREQ(2, 16)
 
1232
#pragma GCC diagnostic push
 
1233
#pragma GCC diagnostic ignored "-Wsign-conversion"
 
1234
#endif
 
1235
#endif
1182
1236
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){ /* Spurious
1183
1237
                                                         warning from
1184
 
                                                         -Wconversion */
 
1238
                                                         -Wconversion
 
1239
                                                         in GNU libc
 
1240
                                                         before
 
1241
                                                         2.16 */
 
1242
#if defined (__GNUC__) and defined (__GLIBC__)
 
1243
#if not __GLIBC_PREREQ(2, 16)
 
1244
#pragma GCC diagnostic pop
 
1245
#endif
 
1246
#endif
1185
1247
        /* This process had nothing to say at this time */
1186
1248
        proc = proc->next;
1187
1249
        continue;
1188
1250
      }
1189
1251
      /* Before reading, make the process' data buffer large enough */
1190
1252
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
1191
 
        proc->buffer = realloc(proc->buffer, proc->buffer_size
1192
 
                               + (size_t) BUFFER_SIZE);
1193
 
        if(proc->buffer == NULL){
 
1253
        char *new_buffer = realloc(proc->buffer, proc->buffer_size
 
1254
                                   + (size_t) BUFFER_SIZE);
 
1255
        if(new_buffer == NULL){
1194
1256
          error(0, errno, "malloc");
1195
1257
          exitstatus = EX_OSERR;
1196
1258
          goto fallback;
1197
1259
        }
 
1260
        proc->buffer = new_buffer;
1198
1261
        proc->buffer_size += BUFFER_SIZE;
1199
1262
      }
1200
1263
      /* Read from the process */