/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 plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-04 23:38:26 UTC
  • mfrom: (24.1.20 mandos)
  • Revision ID: teddy@fukt.bsnet.se-20080804233826-idy7v8x36llseue0
* network-protocol.txt: New.

* server.py (daemon): Do the double fork thing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
                                   struct argp_state, struct argp,
51
51
                                   argp_parse() */
52
52
 
 
53
#define BUFFER_SIZE 256
 
54
 
53
55
struct process;
54
56
 
55
57
typedef struct process{
72
74
  struct plugin *next;
73
75
} plugin;
74
76
 
75
 
plugin *getplugin(char *name, plugin **plugin_list){
 
77
static plugin *getplugin(char *name, plugin **plugin_list){
76
78
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
79
    if ((p->name == name)
78
80
        or (p->name and name and (strcmp(p->name, name) == 0))){
101
103
  return new_plugin;
102
104
}
103
105
 
104
 
void addargument(plugin *p, char *arg){
 
106
static void addargument(plugin *p, char *arg){
105
107
  p->argv[p->argc] = arg;
106
108
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
109
  if (p->argv == NULL){
117
119
 * Descriptor Flags".
118
120
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
121
 */
120
 
int set_cloexec_flag(int fd)
 
122
static int set_cloexec_flag(int fd)
121
123
{
122
124
  int ret = fcntl(fd, F_GETFD, 0);
123
125
  /* If reading the flags failed, return error indication now. */
128
130
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
131
}
130
132
 
131
 
#define BUFFER_SIZE 256
132
 
 
133
133
const char *argp_program_version = "plugbasedclient 0.9";
134
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
135
 
142
142
  int status;
143
143
  pid_t pid = wait(&status);
144
144
  while(proc != NULL and proc->pid != pid){
145
 
    proc = proc->next;    
 
145
    proc = proc->next;
146
146
  }
147
147
  if(proc == NULL){
148
148
    /* Process not found in process list */
160
160
  struct stat st;
161
161
  fd_set rfds_all;
162
162
  int ret, maxfd = 0;
 
163
  uid_t uid = 65534;
 
164
  gid_t gid = 65534;
163
165
  bool debug = false;
164
166
  int exitstatus = EXIT_SUCCESS;
 
167
  struct sigaction old_sigchld_action;
 
168
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
 
169
                                      .sa_flags = SA_NOCLDSTOP };
 
170
  char *plus_options = NULL;
 
171
  char **plus_argv = NULL;
165
172
  
166
173
  /* Establish a signal handler */
167
 
  struct sigaction old_sigchld_action,
168
 
    sigchld_action = { .sa_handler = handle_sigchld,
169
 
                       .sa_flags = SA_NOCLDSTOP };
170
174
  sigemptyset(&sigchld_action.sa_mask);
171
175
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
172
176
  if(ret < 0){
193
197
    { .name = "plugin-dir", .key = 128,
194
198
      .arg = "DIRECTORY",
195
199
      .doc = "Specify a different plugin directory", .group = 2 },
196
 
    { .name = "debug", .key = 129,
 
200
    { .name = "userid", .key = 129,
 
201
      .arg = "ID", .flags = 0,
 
202
      .doc = "User ID the plugins will run as", .group = 2 },
 
203
    { .name = "groupid", .key = 130,
 
204
      .arg = "ID", .flags = 0,
 
205
      .doc = "Group ID the plugins will run as", .group = 2 },
 
206
    { .name = "debug", .key = 131,
197
207
      .doc = "Debug mode", .group = 3 },
198
208
    { .name = NULL }
199
209
  };
209
219
        do{
210
220
          addargument(getplugin(NULL, plugins), p);
211
221
          p = strtok(NULL, ",");
212
 
        } while (p);
 
222
        } while (p != NULL);
213
223
      }
214
224
      break;
215
225
    case 'o':
216
226
      if (arg != NULL){
217
227
        char *name = strtok(arg, ":");
218
228
        char *p = strtok(NULL, ":");
219
 
        if(p){
 
229
        if(p != NULL){
220
230
          p = strtok(p, ",");
221
231
          do{
222
232
            addargument(getplugin(name, plugins), p);
223
233
            p = strtok(NULL, ",");
224
 
          } while (p);
 
234
          } while (p != NULL);
225
235
        }
226
236
      }
227
237
      break;
234
244
      plugindir = arg;
235
245
      break;
236
246
    case 129:
 
247
      uid = (uid_t)strtol(arg, NULL, 10);
 
248
      break;
 
249
    case 130:
 
250
      gid = (gid_t)strtol(arg, NULL, 10);
 
251
      break;
 
252
    case 131:
237
253
      debug = true;
238
254
      break;
239
255
    case ARGP_KEY_ARG:
240
 
      argp_usage (state);
 
256
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
 
257
        argp_usage (state);
 
258
      }
 
259
      plus_options = arg;
241
260
      break;
242
261
    case ARGP_KEY_END:
243
262
      break;
250
269
  plugin *plugin_list = NULL;
251
270
  
252
271
  struct argp argp = { .options = options, .parser = parse_opt,
253
 
                       .args_doc = "",
 
272
                       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
254
273
                       .doc = "Mandos plugin runner -- Run plugins" };
255
274
  
256
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
 
275
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
 
276
  
 
277
  if(plus_options){
 
278
    /* This is a mangled argument in the form of
 
279
     "+--option+--other-option=parameter+--yet-another-option", etc */
 
280
    /* Make new argc and argv vars, and call argp_parse() again. */
 
281
    plus_options++;             /* skip the first '+' character */
 
282
    const char delims[] = "+";
 
283
    char *arg;
 
284
    int new_argc = 1;
 
285
    plus_argv = malloc(sizeof(char*) * 2);
 
286
    if(plus_argv == NULL){
 
287
      perror("malloc");
 
288
      exitstatus = EXIT_FAILURE;
 
289
      goto end;
 
290
    }
 
291
    plus_argv[0] = argv[0];
 
292
    plus_argv[1] = NULL;
 
293
    arg = strtok(plus_options, delims); /* Get first argument */
 
294
    while(arg != NULL){
 
295
      new_argc++;
 
296
      plus_argv = realloc(plus_argv, sizeof(char *)
 
297
                         * ((unsigned int) new_argc + 1));
 
298
      if(plus_argv == NULL){
 
299
        perror("malloc");
 
300
        exitstatus = EXIT_FAILURE;
 
301
        goto end;
 
302
      }
 
303
      plus_argv[new_argc-1] = arg;
 
304
      plus_argv[new_argc] = NULL;
 
305
      arg = strtok(NULL, delims); /* Get next argument */
 
306
    }
 
307
    argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);  
 
308
  }
257
309
  
258
310
  if(debug){
259
311
    for(plugin *p = plugin_list; p != NULL; p=p->next){
265
317
    }
266
318
  }
267
319
  
 
320
  ret = setuid(uid);
 
321
  if (ret == -1){
 
322
    perror("setuid");
 
323
  }
 
324
  
 
325
  setgid(gid);
 
326
  if (ret == -1){
 
327
    perror("setgid");
 
328
  }
 
329
  
268
330
  dir = opendir(plugindir);
269
331
  if(dir == NULL){
270
332
    perror("Could not open plugin dir");
600
662
  /* Restore old signal handler */
601
663
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
602
664
  
 
665
  free(plus_argv);
 
666
  
603
667
  /* Free the plugin list */
604
668
  for(plugin *next; plugin_list != NULL; plugin_list = next){
605
669
    next = plugin_list->next;