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