/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1
/*  -*- coding: utf-8 -*- */
2
/*
3
 * Mandos plugin runner - Run Mandos plugins
4
 *
28 by Teddy Hogeborn
* server.conf: New file.
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
6
 * 
7
 * This program is free software: you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 * 
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 * 
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see
19
 * <http://www.gnu.org/licenses/>.
20
 * 
28 by Teddy Hogeborn
* server.conf: New file.
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
22
 */
23
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
24
#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY() */
25
24.1.26 by Björn Påhlsson
tally count of used symbols
26
#include <stddef.h>		/* size_t, NULL */
27
#include <stdlib.h>		/* malloc(), exit(), EXIT_FAILURE,
28
				   EXIT_SUCCESS, realloc() */
29
#include <stdbool.h>		/* bool, true, false */
30
#include <stdio.h>		/* perror, popen(), fileno(),
31
				   fprintf(), stderr, STDOUT_FILENO */
32
#include <sys/types.h>	        /* DIR, opendir(), stat(), struct
33
				   stat, waitpid(), WIFEXITED(),
34
				   WEXITSTATUS(), wait(), pid_t,
35
				   uid_t, gid_t, getuid(), getgid(),
36
				   dirfd() */
37
#include <sys/select.h>		/* fd_set, select(), FD_ZERO(),
38
				   FD_SET(), FD_ISSET(), FD_CLR */
39
#include <sys/wait.h>		/* wait(), waitpid(), WIFEXITED(),
40
				   WEXITSTATUS() */
41
#include <sys/stat.h>		/* struct stat, stat(), S_ISREG() */
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
42
#include <iso646.h>		/* and, or, not */
43
#include <dirent.h>		/* DIR, struct dirent, opendir(),
24.1.26 by Björn Påhlsson
tally count of used symbols
44
				   readdir(), closedir(), dirfd() */
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
45
#include <unistd.h>		/* struct stat, stat(), S_ISREG(),
24.1.26 by Björn Påhlsson
tally count of used symbols
46
				   fcntl(), setuid(), setgid(),
47
				   F_GETFD, F_SETFD, FD_CLOEXEC,
48
				   access(), pipe(), fork(), close()
49
				   dup2, STDOUT_FILENO, _exit(),
50
				   execv(), write(), read(),
51
				   close() */
52
#include <fcntl.h>		/* fcntl(), F_GETFD, F_SETFD,
53
				   FD_CLOEXEC */
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
54
#include <string.h>		/* strsep, strlen(), strcpy(),
24.1.26 by Björn Påhlsson
tally count of used symbols
55
				   strcat() */
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
56
#include <errno.h>		/* errno */
24.1.26 by Björn Påhlsson
tally count of used symbols
57
#include <argp.h>		/* struct argp_option, struct
58
				   argp_state, struct argp,
59
				   argp_parse(), ARGP_ERR_UNKNOWN,
60
				   ARGP_KEY_END, ARGP_KEY_ARG, error_t */
61
#include <signal.h> 		/* struct sigaction, sigemptyset(),
62
				   sigaddset(), sigaction(),
63
				   sigprocmask(), SIG_BLOCK, SIGCHLD,
64
				   SIG_UNBLOCK, kill() */
65
#include <errno.h>		/* errno, EBADF */
13 by Björn Påhlsson
Added following support:
66
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
67
#define BUFFER_SIZE 256
68
74 by Teddy Hogeborn
* Makefile (PREFIX, CONFDIR): New.
69
const char *argp_program_version = "plugin-runner 1.0";
24.1.35 by Björn Påhlsson
version 1.0
70
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
71
13 by Björn Påhlsson
Added following support:
72
struct process;
73
74
typedef struct process{
75
  pid_t pid;
76
  int fd;
77
  char *buffer;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
78
  size_t buffer_size;
79
  size_t buffer_length;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
80
  bool eof;
81
  bool completed;
82
  int status;
13 by Björn Påhlsson
Added following support:
83
  struct process *next;
84
} process;
85
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
86
typedef struct plugin{
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
87
  char *name;			/* can be NULL or any plugin name */
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
88
  char **argv;
89
  int argc;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
90
  bool disabled;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
91
  struct plugin *next;
92
} plugin;
93
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
94
static plugin *getplugin(char *name, plugin **plugin_list){
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
95
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
96
    if ((p->name == name)
97
	or (p->name and name and (strcmp(p->name, name) == 0))){
98
      return p;
99
    }
100
  }
101
  /* Create a new plugin */
102
  plugin *new_plugin = malloc(sizeof(plugin));
103
  if (new_plugin == NULL){
104
    perror("malloc");
105
    exit(EXIT_FAILURE);
106
  }
107
  new_plugin->name = name;
108
  new_plugin->argv = malloc(sizeof(char *) * 2);
109
  if (new_plugin->argv == NULL){
110
    perror("malloc");
111
    exit(EXIT_FAILURE);
112
  }
113
  new_plugin->argv[0] = name;
114
  new_plugin->argv[1] = NULL;
115
  new_plugin->argc = 1;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
116
  new_plugin->disabled = false;
24.1.5 by Björn Påhlsson
plugbasedclient:
117
  new_plugin->next = *plugin_list;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
118
  /* Append the new plugin to the list */
119
  *plugin_list = new_plugin;
120
  return new_plugin;
121
}
122
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
123
static void addargument(plugin *p, char *arg){
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
124
  p->argv[p->argc] = arg;
125
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
126
  if (p->argv == NULL){
127
    perror("malloc");
128
    exit(EXIT_FAILURE);
129
  }
130
  p->argc++;
131
  p->argv[p->argc] = NULL;
132
}
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
133
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
134
/*
135
 * Based on the example in the GNU LibC manual chapter 13.13 "File
136
 * Descriptor Flags".
137
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
138
 */
37 by Teddy Hogeborn
Non-tested commit for merge purposes.
139
static int set_cloexec_flag(int fd)
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
140
{
141
  int ret = fcntl(fd, F_GETFD, 0);
142
  /* If reading the flags failed, return error indication now. */
143
  if(ret < 0){
144
    return ret;
145
  }
146
  /* Store modified flag word in the descriptor. */
147
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
148
}
149
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
150
process *process_list = NULL;
151
152
/* Mark a process as completed when it exits, and save its exit
153
   status. */
154
void handle_sigchld(__attribute__((unused)) int sig){
155
  process *proc = process_list;
156
  int status;
157
  pid_t pid = wait(&status);
24.1.26 by Björn Påhlsson
tally count of used symbols
158
  if(pid == -1){
159
    perror("wait");
160
    return;
161
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
162
  while(proc != NULL and proc->pid != pid){
24.1.8 by Björn Påhlsson
plugbasedclient
163
    proc = proc->next;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
164
  }
165
  if(proc == NULL){
166
    /* Process not found in process list */
167
    return;
168
  }
169
  proc->status = status;
170
  proc->completed = true;
171
}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
172
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
173
bool print_out_password(const char *buffer, size_t length){
64 by Teddy Hogeborn
* mandos-client.c (print_out_password): Strip trailing '\n'.
174
  ssize_t ret;
175
  if(length>0 and buffer[length-1] == '\n'){
176
    length--;
177
  }
178
  for(size_t written = 0; written < length; written += (size_t)ret){
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
179
    ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO, buffer + written,
180
				   length - written));
181
    if(ret < 0){
182
      return false;
183
    }
184
  }
185
  return true;
186
}
187
13 by Björn Påhlsson
Added following support:
188
int main(int argc, char *argv[]){
74 by Teddy Hogeborn
* Makefile (PREFIX, CONFDIR): New.
189
  const char *plugindir = "/lib/mandos/plugins.d";
24.1.5 by Björn Påhlsson
plugbasedclient:
190
  size_t d_name_len;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
191
  DIR *dir = NULL;
13 by Björn Påhlsson
Added following support:
192
  struct dirent *dirst;
193
  struct stat st;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
194
  fd_set rfds_all;
13 by Björn Påhlsson
Added following support:
195
  int ret, maxfd = 0;
24.1.6 by Björn Påhlsson
plugbasedclient
196
  uid_t uid = 65534;
197
  gid_t gid = 65534;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
198
  bool debug = false;
199
  int exitstatus = EXIT_SUCCESS;
24.1.7 by Björn Påhlsson
merge
200
  struct sigaction old_sigchld_action;
201
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
202
				      .sa_flags = SA_NOCLDSTOP };
24.1.8 by Björn Påhlsson
plugbasedclient
203
  char *plus_options = NULL;
204
  char **plus_argv = NULL;
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
205
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
206
  /* Establish a signal handler */
207
  sigemptyset(&sigchld_action.sa_mask);
208
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
209
  if(ret < 0){
210
    perror("sigaddset");
211
    exit(EXIT_FAILURE);
212
  }
213
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
214
  if(ret < 0){
215
    perror("sigaction");
216
    exit(EXIT_FAILURE);
217
  }
218
  
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
219
  /* The options we understand. */
220
  struct argp_option options[] = {
221
    { .name = "global-options", .key = 'g',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
222
      .arg = "OPTION[,OPTION[,...]]",
223
      .doc = "Options passed to all plugins" },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
224
    { .name = "options-for", .key = 'o',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
225
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
226
      .doc = "Options passed only to specified plugin" },
227
    { .name = "disable", .key = 'd',
228
      .arg = "PLUGIN",
229
      .doc = "Disable a specific plugin", .group = 1 },
24.1.5 by Björn Påhlsson
plugbasedclient:
230
    { .name = "plugin-dir", .key = 128,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
231
      .arg = "DIRECTORY",
232
      .doc = "Specify a different plugin directory", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
233
    { .name = "userid", .key = 129,
24.1.7 by Björn Påhlsson
merge
234
      .arg = "ID", .flags = 0,
235
      .doc = "User ID the plugins will run as", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
236
    { .name = "groupid", .key = 130,
24.1.7 by Björn Påhlsson
merge
237
      .arg = "ID", .flags = 0,
238
      .doc = "Group ID the plugins will run as", .group = 2 },
239
    { .name = "debug", .key = 131,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
240
      .doc = "Debug mode", .group = 3 },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
241
    { .name = NULL }
242
  };
243
  
244
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
245
    /* Get the INPUT argument from `argp_parse', which we know is a
246
       pointer to our plugin list pointer. */
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
247
    plugin **plugins = state->input;
248
    switch (key) {
249
    case 'g':
250
      if (arg != NULL){
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
251
	char *p;
252
	while((p = strsep(&arg, ",")) != NULL){
253
	  if(strcmp(p, "") == 0){
254
	    continue;
255
	  }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
256
	  addargument(getplugin(NULL, plugins), p);
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
257
	}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
258
      }
259
      break;
260
    case 'o':
261
      if (arg != NULL){
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
262
	char *name = strsep(&arg, ":");
263
	if(strcmp(name, "") == 0){
264
	  break;
265
	}
266
	char *opt = strsep(&arg, ":");
267
	if(strcmp(opt, "") == 0){
268
	  break;
269
	}
270
	if(opt != NULL){
271
	  char *p;
272
	  while((p = strsep(&opt, ",")) != NULL){
273
	    if(strcmp(p, "") == 0){
274
	      continue;
275
	    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
276
	    addargument(getplugin(name, plugins), p);
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
277
	  }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
278
	}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
279
      }
280
      break;
24.1.5 by Björn Påhlsson
plugbasedclient:
281
    case 'd':
282
      if (arg != NULL){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
283
	getplugin(arg, plugins)->disabled = true;
24.1.5 by Björn Påhlsson
plugbasedclient:
284
      }
285
      break;
286
    case 128:
287
      plugindir = arg;
288
      break;
24.1.6 by Björn Påhlsson
plugbasedclient
289
    case 129:
290
      uid = (uid_t)strtol(arg, NULL, 10);
291
      break;
292
    case 130:
293
      gid = (gid_t)strtol(arg, NULL, 10);
294
      break;
24.1.7 by Björn Påhlsson
merge
295
    case 131:
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
296
      debug = true;
297
      break;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
298
    case ARGP_KEY_ARG:
24.1.8 by Björn Påhlsson
plugbasedclient
299
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
300
	argp_usage (state);
301
      }
302
      plus_options = arg;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
303
      break;
304
    case ARGP_KEY_END:
305
      break;
306
    default:
307
      return ARGP_ERR_UNKNOWN;
308
    }
309
    return 0;
310
  }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
311
  
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
312
  plugin *plugin_list = NULL;
313
  
314
  struct argp argp = { .options = options, .parser = parse_opt,
24.1.8 by Björn Påhlsson
plugbasedclient
315
		       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
316
		       .doc = "Mandos plugin runner -- Run plugins" };
317
  
24.1.26 by Björn Påhlsson
tally count of used symbols
318
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
319
  if (ret == ARGP_ERR_UNKNOWN){
64 by Teddy Hogeborn
* mandos-client.c (print_out_password): Strip trailing '\n'.
320
    fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.26 by Björn Påhlsson
tally count of used symbols
321
    exitstatus = EXIT_FAILURE;
322
    goto end;
323
  }
24.1.8 by Björn Påhlsson
plugbasedclient
324
  
325
  if(plus_options){
326
    /* This is a mangled argument in the form of
327
     "+--option+--other-option=parameter+--yet-another-option", etc */
328
    /* Make new argc and argv vars, and call argp_parse() again. */
329
    plus_options++;		/* skip the first '+' character */
330
    const char delims[] = "+";
331
    char *arg;
332
    int new_argc = 1;
333
    plus_argv = malloc(sizeof(char*) * 2);
334
    if(plus_argv == NULL){
335
      perror("malloc");
336
      exitstatus = EXIT_FAILURE;
337
      goto end;
338
    }
339
    plus_argv[0] = argv[0];
340
    plus_argv[1] = NULL;
24.1.50 by Björn Påhlsson
changed from using strtok to strsep
341
342
    while((arg = strsep(&plus_options, delims)) != NULL){
24.1.8 by Björn Påhlsson
plugbasedclient
343
      new_argc++;
344
      plus_argv = realloc(plus_argv, sizeof(char *)
345
			 * ((unsigned int) new_argc + 1));
346
      if(plus_argv == NULL){
24.1.26 by Björn Påhlsson
tally count of used symbols
347
	perror("realloc");
24.1.8 by Björn Påhlsson
plugbasedclient
348
	exitstatus = EXIT_FAILURE;
349
	goto end;
350
      }
351
      plus_argv[new_argc-1] = arg;
352
      plus_argv[new_argc] = NULL;
353
    }
75 by Teddy Hogeborn
Merge.
354
    
24.1.26 by Björn Påhlsson
tally count of used symbols
355
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
356
    if (ret == ARGP_ERR_UNKNOWN){
64 by Teddy Hogeborn
* mandos-client.c (print_out_password): Strip trailing '\n'.
357
      fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.26 by Björn Påhlsson
tally count of used symbols
358
      exitstatus = EXIT_FAILURE;
359
      goto end;
360
    }
24.1.8 by Björn Påhlsson
plugbasedclient
361
  }
362
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
363
  if(debug){
364
    for(plugin *p = plugin_list; p != NULL; p=p->next){
365
      fprintf(stderr, "Plugin: %s has %d arguments\n",
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
366
	      p->name ? p->name : "Global", p->argc - 1);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
367
      for(char **a = p->argv; *a != NULL; a++){
368
	fprintf(stderr, "\tArg: %s\n", *a);
369
      }
370
    }
371
  }
24.1.8 by Björn Påhlsson
plugbasedclient
372
  
24.1.6 by Björn Påhlsson
plugbasedclient
373
  ret = setuid(uid);
374
  if (ret == -1){
375
    perror("setuid");
376
  }
24.1.7 by Björn Påhlsson
merge
377
  
24.1.6 by Björn Påhlsson
plugbasedclient
378
  setgid(gid);
379
  if (ret == -1){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
380
    perror("setgid");
24.1.6 by Björn Påhlsson
plugbasedclient
381
  }
382
  
13 by Björn Påhlsson
Added following support:
383
  dir = opendir(plugindir);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
384
  if(dir == NULL){
385
    perror("Could not open plugin dir");
386
    exitstatus = EXIT_FAILURE;
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
387
    goto end;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
388
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
389
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
390
  /* Set the FD_CLOEXEC flag on the directory, if possible */
391
  {
392
    int dir_fd = dirfd(dir);
393
    if(dir_fd >= 0){
394
      ret = set_cloexec_flag(dir_fd);
395
      if(ret < 0){
396
	perror("set_cloexec_flag");
397
	exitstatus = EXIT_FAILURE;
398
	goto end;
399
      }
400
    }
401
  }
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
402
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
403
  FD_ZERO(&rfds_all);
13 by Björn Påhlsson
Added following support:
404
  
405
  while(true){
406
    dirst = readdir(dir);
407
    
408
    // All directory entries have been processed
409
    if(dirst == NULL){
24.1.26 by Björn Påhlsson
tally count of used symbols
410
      if (errno == EBADF){
411
	perror("readdir");
412
	exitstatus = EXIT_FAILURE;
413
	goto end;
414
      }
13 by Björn Påhlsson
Added following support:
415
      break;
416
    }
417
    
418
    d_name_len = strlen(dirst->d_name);
419
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
420
    // Ignore dotfiles, backup files and other junk
421
    {
422
      bool bad_name = false;
423
      
424
      const char const *bad_prefixes[] = { ".", "#", NULL };
425
      
426
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
427
					   ".dpkg-old",
428
					   ".dpkg-divert", NULL };
429
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
430
	size_t pre_len = strlen(*pre);
431
	if((d_name_len >= pre_len)
432
	   and strncmp((dirst->d_name), *pre, pre_len) == 0){
433
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
434
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
435
		    " with bad prefix %s\n", dirst->d_name, *pre);
436
	  }
437
	  bad_name = true;
438
	  break;
439
	}
440
      }
441
      
442
      if(bad_name){
443
	continue;
444
      }
445
      
446
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
447
	size_t suf_len = strlen(*suf);
448
	if((d_name_len >= suf_len)
449
	   and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
450
		== 0)){
451
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
452
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
453
		    " with bad suffix %s\n", dirst->d_name, *suf);
454
	  }
455
	  bad_name = true;
456
	  break;
457
	}
458
      }
459
      
460
      if(bad_name){
461
	continue;
462
      }
13 by Björn Påhlsson
Added following support:
463
    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
464
    
24.1.5 by Björn Påhlsson
plugbasedclient:
465
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
466
    if (filename == NULL){
467
      perror("malloc");
24.1.46 by Björn Påhlsson
mandos-client
468
      continue;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
469
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
470
    strcpy(filename, plugindir); /* Spurious warning */
471
    strcat(filename, "/");	/* Spurious warning */
472
    strcat(filename, dirst->d_name); /* Spurious warning */
473
    
24.1.26 by Björn Påhlsson
tally count of used symbols
474
    ret = stat(filename, &st);
475
    if (ret == -1){
476
      perror("stat");
24.1.46 by Björn Påhlsson
mandos-client
477
      free(filename);
478
      continue;
24.1.26 by Björn Påhlsson
tally count of used symbols
479
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
480
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
481
    if (not S_ISREG(st.st_mode)	or (access(filename, X_OK) != 0)){
482
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
483
	fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
484
		" with bad type or mode\n", filename);
485
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
486
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
487
      continue;
488
    }
489
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
490
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
491
	fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
492
		dirst->d_name);
493
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
494
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
495
      continue;
496
    }
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
497
    plugin *p = getplugin(dirst->d_name, &plugin_list);
498
    {
499
      /* Add global arguments to argument list for this plugin */
500
      plugin *g = getplugin(NULL, &plugin_list);
501
      for(char **a = g->argv + 1; *a != NULL; a++){
502
	addargument(p, *a);
503
      }
504
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
505
    int pipefd[2]; 
506
    ret = pipe(pipefd);
507
    if (ret == -1){
508
      perror("pipe");
509
      exitstatus = EXIT_FAILURE;
510
      goto end;
511
    }
512
    ret = set_cloexec_flag(pipefd[0]);
513
    if(ret < 0){
514
      perror("set_cloexec_flag");
515
      exitstatus = EXIT_FAILURE;
516
      goto end;
517
    }
518
    ret = set_cloexec_flag(pipefd[1]);
519
    if(ret < 0){
520
      perror("set_cloexec_flag");
521
      exitstatus = EXIT_FAILURE;
522
      goto end;
523
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
524
    /* Block SIGCHLD until process is safely in process list */
525
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
526
    if(ret < 0){
527
      perror("sigprocmask");
528
      exitstatus = EXIT_FAILURE;
529
      goto end;
530
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
531
    // Starting a new process to be watched
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
532
    pid_t pid = fork();
24.1.46 by Björn Påhlsson
mandos-client
533
    if(pid == -1){
534
      perror("fork");
535
      exitstatus = EXIT_FAILURE;
536
      goto end;
537
    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
538
    if(pid == 0){
539
      /* this is the child process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
540
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
541
      if(ret < 0){
542
	perror("sigaction");
543
	_exit(EXIT_FAILURE);
544
      }
545
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
546
      if(ret < 0){
547
	perror("sigprocmask");
548
	_exit(EXIT_FAILURE);
549
      }
24.1.26 by Björn Påhlsson
tally count of used symbols
550
551
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
552
      if(ret == -1){
553
	perror("dup2");
554
	_exit(EXIT_FAILURE);
555
      }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
556
      
557
      if(dirfd(dir) < 0){
558
	/* If dir has no file descriptor, we could not set FD_CLOEXEC
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
559
	   above and must now close it manually here. */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
560
	closedir(dir);
33 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (main): Close the pipe fd after dup2:ing
561
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
562
      if(execv(filename, p->argv) < 0){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
563
	perror("execv");
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
564
	_exit(EXIT_FAILURE);
565
      }
566
      /* no return */
567
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
568
    /* parent process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
569
    free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
570
    close(pipefd[1]);		/* close unused write end of pipe */
571
    process *new_process = malloc(sizeof(process));
572
    if (new_process == NULL){
573
      perror("malloc");
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
574
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
575
      if(ret < 0){
576
	perror("sigprocmask");
577
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
578
      exitstatus = EXIT_FAILURE;
579
      goto end;
580
    }
581
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
582
    *new_process = (struct process){ .pid = pid,
583
				     .fd = pipefd[0],
584
				     .next = process_list };
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
585
    // List handling
586
    process_list = new_process;
587
    /* Unblock SIGCHLD so signal handler can be run if this process
588
       has already completed */
589
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
590
    if(ret < 0){
591
      perror("sigprocmask");
592
      exitstatus = EXIT_FAILURE;
593
      goto end;
594
    }
595
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
596
    FD_SET(new_process->fd, &rfds_all);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
597
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
598
    if (maxfd < new_process->fd){
599
      maxfd = new_process->fd;
600
    }
601
    
13 by Björn Påhlsson
Added following support:
602
  }
603
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
604
  /* Free the plugin list */
605
  for(plugin *next; plugin_list != NULL; plugin_list = next){
606
    next = plugin_list->next;
607
    free(plugin_list->argv);
608
    free(plugin_list);
609
  }
610
  
13 by Björn Påhlsson
Added following support:
611
  closedir(dir);
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
612
  dir = NULL;
24.1.26 by Björn Påhlsson
tally count of used symbols
613
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
614
  if (process_list == NULL){
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
615
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
616
	    " directory?\n");
617
    process_list = NULL;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
618
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
619
  while(process_list){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
620
    fd_set rfds = rfds_all;
621
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
622
    if (select_ret == -1){
623
      perror("select");
624
      exitstatus = EXIT_FAILURE;
625
      goto end;
626
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
627
    /* OK, now either a process completed, or something can be read
628
       from one of them */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
629
    for(process *proc = process_list; proc ; proc = proc->next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
630
      /* Is this process completely done? */
631
      if(proc->eof and proc->completed){
632
	/* Only accept the plugin output if it exited cleanly */
633
	if(not WIFEXITED(proc->status)
634
	   or WEXITSTATUS(proc->status) != 0){
635
	  /* Bad exit by plugin */
636
	  if(debug){
637
	    if(WIFEXITED(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
638
	      fprintf(stderr, "Plugin %u exited with status %d\n",
639
		      (unsigned int) (proc->pid),
640
		      WEXITSTATUS(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
641
	    } else if(WIFSIGNALED(proc->status)) {
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
642
	      fprintf(stderr, "Plugin %u killed by signal %d\n",
643
		      (unsigned int) (proc->pid),
644
		      WTERMSIG(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
645
	    } else if(WCOREDUMP(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
646
	      fprintf(stderr, "Plugin %d dumped core\n",
647
		      (unsigned int) (proc->pid));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
648
	    }
649
	  }
650
	  /* Remove the plugin */
651
	  FD_CLR(proc->fd, &rfds_all);
652
	  /* Block signal while modifying process_list */
653
	  ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
654
	  if(ret < 0){
655
	    perror("sigprocmask");
656
	    exitstatus = EXIT_FAILURE;
657
	    goto end;
658
	  }
659
	  /* Delete this process entry from the list */
660
	  if(process_list == proc){
661
	    /* First one - simple */
662
	    process_list = proc->next;
663
	  } else {
664
	    /* Second one or later */
665
	    for(process *p = process_list; p != NULL; p = p->next){
666
	      if(p->next == proc){
667
		p->next = proc->next;
668
		break;
669
	      }
670
	    }
671
	  }
672
	  /* We are done modifying process list, so unblock signal */
673
	  ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
674
			     NULL);
675
	  if(ret < 0){
676
	    perror("sigprocmask");
677
	  }
678
	  free(proc->buffer);
679
	  free(proc);
680
	  /* We deleted this process from the list, so we can't go
681
	     proc->next.  Therefore, start over from the beginning of
682
	     the process list */
683
	  break;
684
	}
685
	/* This process exited nicely, so print its buffer */
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
686
687
	bool bret = print_out_password(proc->buffer, proc->buffer_length);
688
	if(not bret){
689
	  perror("print_out_password");
690
	  exitstatus = EXIT_FAILURE;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
691
	}
692
	goto end;
693
      }
694
      /* This process has not completed.  Does it have any output? */
695
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
696
	/* This process had nothing to say at this time */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
697
	continue;
698
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
699
      /* Before reading, make the process' data buffer large enough */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
700
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
701
	proc->buffer = realloc(proc->buffer, proc->buffer_size
702
			       + (size_t) BUFFER_SIZE);
703
	if (proc->buffer == NULL){
704
	  perror("malloc");
705
	  exitstatus = EXIT_FAILURE;
706
	  goto end;
707
	}
708
	proc->buffer_size += BUFFER_SIZE;
709
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
710
      /* Read from the process */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
711
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
712
		 BUFFER_SIZE);
713
      if(ret < 0){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
714
	/* Read error from this process; ignore the error */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
715
	continue;
716
      }
717
      if(ret == 0){
718
	/* got EOF */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
719
	proc->eof = true;
720
      } else {
721
	proc->buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
722
      }
723
    }
724
  }
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
725
726
727
 end:
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
728
  
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
729
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
24.1.46 by Björn Påhlsson
mandos-client
730
    /* Fallback if all plugins failed, none are found or an error occured */
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
731
    bool bret;
732
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
733
    char *passwordbuffer = getpass("Password: ");
734
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
735
    if(not bret){
736
      perror("print_out_password");
737
      exitstatus = EXIT_FAILURE;
738
      goto end;
739
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
740
  }
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
741
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
742
  /* Restore old signal handler */
743
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
744
  
24.1.8 by Björn Påhlsson
plugbasedclient
745
  free(plus_argv);
746
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
747
  /* Free the plugin list */
748
  for(plugin *next; plugin_list != NULL; plugin_list = next){
749
    next = plugin_list->next;
750
    free(plugin_list->argv);
751
    free(plugin_list);
752
  }
753
  
754
  if(dir != NULL){
755
    closedir(dir);
756
  }
757
  
758
  /* Free the process list and kill the processes */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
759
  for(process *next; process_list != NULL; process_list = next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
760
    next = process_list->next;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
761
    close(process_list->fd);
24.1.26 by Björn Påhlsson
tally count of used symbols
762
    ret = kill(process_list->pid, SIGTERM);
763
    if(ret == -1 and errno != ESRCH){
764
      /* set-uid proccesses migth not get closed */
765
      perror("kill");
766
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
767
    free(process_list->buffer);
768
    free(process_list);
13 by Björn Påhlsson
Added following support:
769
  }
770
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
771
  /* Wait for any remaining child processes to terminate */
772
  do{
773
    ret = wait(NULL);
774
  } while(ret >= 0);
775
  if(errno != ECHILD){
776
    perror("wait");
777
  }
778
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
779
  return exitstatus;
13 by Björn Påhlsson
Added following support:
780
}