/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 */
54
#include <string.h>		/* strtok, strlen(), strcpy(),
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
24.1.37 by Björn Påhlsson
name change for argp
69
const char *argp_program_version = "mandos-client 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[]){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
189
  const char *plugindir = "/conf/conf.d/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
206
  errno = 0;
24.1.8 by Björn Påhlsson
plugbasedclient
207
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
208
  /* Establish a signal handler */
209
  sigemptyset(&sigchld_action.sa_mask);
210
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
211
  if(ret < 0){
212
    perror("sigaddset");
213
    exit(EXIT_FAILURE);
214
  }
215
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
216
  if(ret < 0){
217
    perror("sigaction");
218
    exit(EXIT_FAILURE);
219
  }
220
  
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
221
  /* The options we understand. */
222
  struct argp_option options[] = {
223
    { .name = "global-options", .key = 'g',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
224
      .arg = "OPTION[,OPTION[,...]]",
225
      .doc = "Options passed to all plugins" },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
226
    { .name = "options-for", .key = 'o',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
227
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
228
      .doc = "Options passed only to specified plugin" },
229
    { .name = "disable", .key = 'd',
230
      .arg = "PLUGIN",
231
      .doc = "Disable a specific plugin", .group = 1 },
24.1.5 by Björn Påhlsson
plugbasedclient:
232
    { .name = "plugin-dir", .key = 128,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
233
      .arg = "DIRECTORY",
234
      .doc = "Specify a different plugin directory", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
235
    { .name = "userid", .key = 129,
24.1.7 by Björn Påhlsson
merge
236
      .arg = "ID", .flags = 0,
237
      .doc = "User ID the plugins will run as", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
238
    { .name = "groupid", .key = 130,
24.1.7 by Björn Påhlsson
merge
239
      .arg = "ID", .flags = 0,
240
      .doc = "Group ID the plugins will run as", .group = 2 },
241
    { .name = "debug", .key = 131,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
242
      .doc = "Debug mode", .group = 3 },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
243
    { .name = NULL }
244
  };
245
  
246
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
247
    /* Get the INPUT argument from `argp_parse', which we know is a
248
       pointer to our plugin list pointer. */
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
249
    plugin **plugins = state->input;
250
    switch (key) {
251
    case 'g':
252
      if (arg != NULL){
253
	char *p = strtok(arg, ",");
254
	do{
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
255
	  addargument(getplugin(NULL, plugins), p);
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
256
	  p = strtok(NULL, ",");
24.1.8 by Björn Påhlsson
plugbasedclient
257
	} while (p != NULL);
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){
262
	char *name = strtok(arg, ":");
263
	char *p = strtok(NULL, ":");
24.1.8 by Björn Påhlsson
plugbasedclient
264
	if(p != NULL){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
265
	  p = strtok(p, ",");
266
	  do{
267
	    addargument(getplugin(name, plugins), p);
268
	    p = strtok(NULL, ",");
24.1.8 by Björn Påhlsson
plugbasedclient
269
	  } while (p != NULL);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
270
	}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
271
      }
272
      break;
24.1.5 by Björn Påhlsson
plugbasedclient:
273
    case 'd':
274
      if (arg != NULL){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
275
	getplugin(arg, plugins)->disabled = true;
24.1.5 by Björn Påhlsson
plugbasedclient:
276
      }
277
      break;
278
    case 128:
279
      plugindir = arg;
280
      break;
24.1.6 by Björn Påhlsson
plugbasedclient
281
    case 129:
282
      uid = (uid_t)strtol(arg, NULL, 10);
283
      break;
284
    case 130:
285
      gid = (gid_t)strtol(arg, NULL, 10);
286
      break;
24.1.7 by Björn Påhlsson
merge
287
    case 131:
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
288
      debug = true;
289
      break;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
290
    case ARGP_KEY_ARG:
24.1.8 by Björn Påhlsson
plugbasedclient
291
      if(plus_options != NULL or arg == NULL or arg[0] != '+'){
292
	argp_usage (state);
293
      }
294
      plus_options = arg;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
295
      break;
296
    case ARGP_KEY_END:
297
      break;
298
    default:
299
      return ARGP_ERR_UNKNOWN;
300
    }
301
    return 0;
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
  plugin *plugin_list = NULL;
305
  
306
  struct argp argp = { .options = options, .parser = parse_opt,
24.1.8 by Björn Påhlsson
plugbasedclient
307
		       .args_doc = "[+PLUS_SEPARATED_OPTIONS]",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
308
		       .doc = "Mandos plugin runner -- Run plugins" };
309
  
24.1.26 by Björn Påhlsson
tally count of used symbols
310
  ret = argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
311
  if (ret == ARGP_ERR_UNKNOWN){
64 by Teddy Hogeborn
* mandos-client.c (print_out_password): Strip trailing '\n'.
312
    fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.26 by Björn Påhlsson
tally count of used symbols
313
    exitstatus = EXIT_FAILURE;
314
    goto end;
315
  }
24.1.8 by Björn Påhlsson
plugbasedclient
316
  
317
  if(plus_options){
318
    /* This is a mangled argument in the form of
319
     "+--option+--other-option=parameter+--yet-another-option", etc */
320
    /* Make new argc and argv vars, and call argp_parse() again. */
321
    plus_options++;		/* skip the first '+' character */
322
    const char delims[] = "+";
323
    char *arg;
324
    int new_argc = 1;
325
    plus_argv = malloc(sizeof(char*) * 2);
326
    if(plus_argv == NULL){
327
      perror("malloc");
328
      exitstatus = EXIT_FAILURE;
329
      goto end;
330
    }
331
    plus_argv[0] = argv[0];
332
    plus_argv[1] = NULL;
333
    arg = strtok(plus_options, delims); /* Get first argument */
334
    while(arg != NULL){
335
      new_argc++;
336
      plus_argv = realloc(plus_argv, sizeof(char *)
337
			 * ((unsigned int) new_argc + 1));
338
      if(plus_argv == NULL){
24.1.26 by Björn Påhlsson
tally count of used symbols
339
	perror("realloc");
24.1.8 by Björn Påhlsson
plugbasedclient
340
	exitstatus = EXIT_FAILURE;
341
	goto end;
342
      }
343
      plus_argv[new_argc-1] = arg;
344
      plus_argv[new_argc] = NULL;
345
      arg = strtok(NULL, delims); /* Get next argument */
346
    }
24.1.26 by Björn Påhlsson
tally count of used symbols
347
    ret = argp_parse (&argp, new_argc, plus_argv, 0, 0, &plugin_list);
348
    if (ret == ARGP_ERR_UNKNOWN){
64 by Teddy Hogeborn
* mandos-client.c (print_out_password): Strip trailing '\n'.
349
      fprintf(stderr, "Unknown error while parsing arguments\n");
24.1.26 by Björn Påhlsson
tally count of used symbols
350
      exitstatus = EXIT_FAILURE;
351
      goto end;
352
    }
24.1.8 by Björn Påhlsson
plugbasedclient
353
  }
354
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
355
  if(debug){
356
    for(plugin *p = plugin_list; p != NULL; p=p->next){
357
      fprintf(stderr, "Plugin: %s has %d arguments\n",
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
358
	      p->name ? p->name : "Global", p->argc - 1);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
359
      for(char **a = p->argv; *a != NULL; a++){
360
	fprintf(stderr, "\tArg: %s\n", *a);
361
      }
362
    }
363
  }
24.1.8 by Björn Påhlsson
plugbasedclient
364
  
24.1.6 by Björn Påhlsson
plugbasedclient
365
  ret = setuid(uid);
366
  if (ret == -1){
367
    perror("setuid");
368
  }
24.1.7 by Björn Påhlsson
merge
369
  
24.1.6 by Björn Påhlsson
plugbasedclient
370
  setgid(gid);
371
  if (ret == -1){
39 by Teddy Hogeborn
* plugins.d/mandosclient.c (pgp_packet_decrypt): Renamed variables.
372
    perror("setgid");
24.1.6 by Björn Påhlsson
plugbasedclient
373
  }
374
  
13 by Björn Påhlsson
Added following support:
375
  dir = opendir(plugindir);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
376
  if(dir == NULL){
377
    perror("Could not open plugin dir");
378
    exitstatus = EXIT_FAILURE;
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
379
    goto end;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
380
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
381
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
382
  /* Set the FD_CLOEXEC flag on the directory, if possible */
383
  {
384
    int dir_fd = dirfd(dir);
385
    if(dir_fd >= 0){
386
      ret = set_cloexec_flag(dir_fd);
387
      if(ret < 0){
388
	perror("set_cloexec_flag");
389
	exitstatus = EXIT_FAILURE;
390
	goto end;
391
      }
392
    }
393
  }
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
394
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
395
  FD_ZERO(&rfds_all);
13 by Björn Påhlsson
Added following support:
396
  
397
  while(true){
398
    dirst = readdir(dir);
399
    
400
    // All directory entries have been processed
401
    if(dirst == NULL){
24.1.26 by Björn Påhlsson
tally count of used symbols
402
      if (errno == EBADF){
403
	perror("readdir");
404
	exitstatus = EXIT_FAILURE;
405
	goto end;
406
      }
13 by Björn Påhlsson
Added following support:
407
      break;
408
    }
409
    
410
    d_name_len = strlen(dirst->d_name);
411
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
412
    // Ignore dotfiles, backup files and other junk
413
    {
414
      bool bad_name = false;
415
      
416
      const char const *bad_prefixes[] = { ".", "#", NULL };
417
      
418
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
419
					   ".dpkg-old",
420
					   ".dpkg-divert", NULL };
421
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
422
	size_t pre_len = strlen(*pre);
423
	if((d_name_len >= pre_len)
424
	   and strncmp((dirst->d_name), *pre, pre_len) == 0){
425
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
426
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
427
		    " with bad prefix %s\n", dirst->d_name, *pre);
428
	  }
429
	  bad_name = true;
430
	  break;
431
	}
432
      }
433
      
434
      if(bad_name){
435
	continue;
436
      }
437
      
438
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
439
	size_t suf_len = strlen(*suf);
440
	if((d_name_len >= suf_len)
441
	   and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
442
		== 0)){
443
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
444
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
445
		    " with bad suffix %s\n", dirst->d_name, *suf);
446
	  }
447
	  bad_name = true;
448
	  break;
449
	}
450
      }
451
      
452
      if(bad_name){
453
	continue;
454
      }
13 by Björn Påhlsson
Added following support:
455
    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
456
    
24.1.5 by Björn Påhlsson
plugbasedclient:
457
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
458
    if (filename == NULL){
459
      perror("malloc");
24.1.46 by Björn Påhlsson
mandos-client
460
      continue;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
461
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
462
    strcpy(filename, plugindir); /* Spurious warning */
463
    strcat(filename, "/");	/* Spurious warning */
464
    strcat(filename, dirst->d_name); /* Spurious warning */
465
    
24.1.26 by Björn Påhlsson
tally count of used symbols
466
    ret = stat(filename, &st);
467
    if (ret == -1){
468
      perror("stat");
24.1.46 by Björn Påhlsson
mandos-client
469
      free(filename);
470
      continue;
24.1.26 by Björn Påhlsson
tally count of used symbols
471
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
472
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
473
    if (not S_ISREG(st.st_mode)	or (access(filename, X_OK) != 0)){
474
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
475
	fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
476
		" with bad type or mode\n", filename);
477
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
478
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
479
      continue;
480
    }
481
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
482
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
483
	fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
484
		dirst->d_name);
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
    }
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
489
    plugin *p = getplugin(dirst->d_name, &plugin_list);
490
    {
491
      /* Add global arguments to argument list for this plugin */
492
      plugin *g = getplugin(NULL, &plugin_list);
493
      for(char **a = g->argv + 1; *a != NULL; a++){
494
	addargument(p, *a);
495
      }
496
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
497
    int pipefd[2]; 
498
    ret = pipe(pipefd);
499
    if (ret == -1){
500
      perror("pipe");
501
      exitstatus = EXIT_FAILURE;
502
      goto end;
503
    }
504
    ret = set_cloexec_flag(pipefd[0]);
505
    if(ret < 0){
506
      perror("set_cloexec_flag");
507
      exitstatus = EXIT_FAILURE;
508
      goto end;
509
    }
510
    ret = set_cloexec_flag(pipefd[1]);
511
    if(ret < 0){
512
      perror("set_cloexec_flag");
513
      exitstatus = EXIT_FAILURE;
514
      goto end;
515
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
516
    /* Block SIGCHLD until process is safely in process list */
517
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
518
    if(ret < 0){
519
      perror("sigprocmask");
520
      exitstatus = EXIT_FAILURE;
521
      goto end;
522
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
523
    // Starting a new process to be watched
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
524
    pid_t pid = fork();
24.1.46 by Björn Påhlsson
mandos-client
525
    if(pid == -1){
526
      perror("fork");
527
      exitstatus = EXIT_FAILURE;
528
      goto end;
529
    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
530
    if(pid == 0){
531
      /* this is the child process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
532
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
533
      if(ret < 0){
534
	perror("sigaction");
535
	_exit(EXIT_FAILURE);
536
      }
537
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
538
      if(ret < 0){
539
	perror("sigprocmask");
540
	_exit(EXIT_FAILURE);
541
      }
24.1.26 by Björn Påhlsson
tally count of used symbols
542
543
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
544
      if(ret == -1){
545
	perror("dup2");
546
	_exit(EXIT_FAILURE);
547
      }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
548
      
549
      if(dirfd(dir) < 0){
550
	/* If dir has no file descriptor, we could not set FD_CLOEXEC
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
551
	   above and must now close it manually here. */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
552
	closedir(dir);
33 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (main): Close the pipe fd after dup2:ing
553
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
554
      if(execv(filename, p->argv) < 0){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
555
	perror("execv");
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
556
	_exit(EXIT_FAILURE);
557
      }
558
      /* no return */
559
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
560
    /* parent process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
561
    free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
562
    close(pipefd[1]);		/* close unused write end of pipe */
563
    process *new_process = malloc(sizeof(process));
564
    if (new_process == NULL){
565
      perror("malloc");
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
566
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
567
      if(ret < 0){
568
	perror("sigprocmask");
569
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
570
      exitstatus = EXIT_FAILURE;
571
      goto end;
572
    }
573
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
574
    *new_process = (struct process){ .pid = pid,
575
				     .fd = pipefd[0],
576
				     .next = process_list };
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
577
    // List handling
578
    process_list = new_process;
579
    /* Unblock SIGCHLD so signal handler can be run if this process
580
       has already completed */
581
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
582
    if(ret < 0){
583
      perror("sigprocmask");
584
      exitstatus = EXIT_FAILURE;
585
      goto end;
586
    }
587
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
588
    FD_SET(new_process->fd, &rfds_all);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
589
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
590
    if (maxfd < new_process->fd){
591
      maxfd = new_process->fd;
592
    }
593
    
13 by Björn Påhlsson
Added following support:
594
  }
595
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
596
  /* Free the plugin list */
597
  for(plugin *next; plugin_list != NULL; plugin_list = next){
598
    next = plugin_list->next;
599
    free(plugin_list->argv);
600
    free(plugin_list);
601
  }
602
  
13 by Björn Påhlsson
Added following support:
603
  closedir(dir);
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
604
  dir = NULL;
24.1.26 by Björn Påhlsson
tally count of used symbols
605
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
606
  if (process_list == NULL){
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
607
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
608
	    " directory?\n");
609
    process_list = NULL;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
610
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
611
  while(process_list){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
612
    fd_set rfds = rfds_all;
613
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
614
    if (select_ret == -1){
615
      perror("select");
616
      exitstatus = EXIT_FAILURE;
617
      goto end;
618
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
619
    /* OK, now either a process completed, or something can be read
620
       from one of them */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
621
    for(process *proc = process_list; proc ; proc = proc->next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
622
      /* Is this process completely done? */
623
      if(proc->eof and proc->completed){
624
	/* Only accept the plugin output if it exited cleanly */
625
	if(not WIFEXITED(proc->status)
626
	   or WEXITSTATUS(proc->status) != 0){
627
	  /* Bad exit by plugin */
628
	  if(debug){
629
	    if(WIFEXITED(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
630
	      fprintf(stderr, "Plugin %u exited with status %d\n",
631
		      (unsigned int) (proc->pid),
632
		      WEXITSTATUS(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
633
	    } else if(WIFSIGNALED(proc->status)) {
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
634
	      fprintf(stderr, "Plugin %u killed by signal %d\n",
635
		      (unsigned int) (proc->pid),
636
		      WTERMSIG(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
637
	    } else if(WCOREDUMP(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
638
	      fprintf(stderr, "Plugin %d dumped core\n",
639
		      (unsigned int) (proc->pid));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
640
	    }
641
	  }
642
	  /* Remove the plugin */
643
	  FD_CLR(proc->fd, &rfds_all);
644
	  /* Block signal while modifying process_list */
645
	  ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
646
	  if(ret < 0){
647
	    perror("sigprocmask");
648
	    exitstatus = EXIT_FAILURE;
649
	    goto end;
650
	  }
651
	  /* Delete this process entry from the list */
652
	  if(process_list == proc){
653
	    /* First one - simple */
654
	    process_list = proc->next;
655
	  } else {
656
	    /* Second one or later */
657
	    for(process *p = process_list; p != NULL; p = p->next){
658
	      if(p->next == proc){
659
		p->next = proc->next;
660
		break;
661
	      }
662
	    }
663
	  }
664
	  /* We are done modifying process list, so unblock signal */
665
	  ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
666
			     NULL);
667
	  if(ret < 0){
668
	    perror("sigprocmask");
669
	  }
670
	  free(proc->buffer);
671
	  free(proc);
672
	  /* We deleted this process from the list, so we can't go
673
	     proc->next.  Therefore, start over from the beginning of
674
	     the process list */
675
	  break;
676
	}
677
	/* This process exited nicely, so print its buffer */
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
678
679
	bool bret = print_out_password(proc->buffer, proc->buffer_length);
680
	if(not bret){
681
	  perror("print_out_password");
682
	  exitstatus = EXIT_FAILURE;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
683
	}
684
	goto end;
685
      }
686
      /* This process has not completed.  Does it have any output? */
687
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
688
	/* This process had nothing to say at this time */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
689
	continue;
690
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
691
      /* Before reading, make the process' data buffer large enough */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
692
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
693
	proc->buffer = realloc(proc->buffer, proc->buffer_size
694
			       + (size_t) BUFFER_SIZE);
695
	if (proc->buffer == NULL){
696
	  perror("malloc");
697
	  exitstatus = EXIT_FAILURE;
698
	  goto end;
699
	}
700
	proc->buffer_size += BUFFER_SIZE;
701
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
702
      /* Read from the process */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
703
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
704
		 BUFFER_SIZE);
705
      if(ret < 0){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
706
	/* Read error from this process; ignore the error */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
707
	continue;
708
      }
709
      if(ret == 0){
710
	/* got EOF */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
711
	proc->eof = true;
712
      } else {
713
	proc->buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
714
      }
715
    }
716
  }
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
717
718
719
 end:
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
720
  
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
721
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
24.1.46 by Björn Påhlsson
mandos-client
722
    /* 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
723
    bool bret;
724
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
725
    char *passwordbuffer = getpass("Password: ");
726
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
727
    if(not bret){
728
      perror("print_out_password");
729
      exitstatus = EXIT_FAILURE;
730
      goto end;
731
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
732
  }
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
733
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
734
  /* Restore old signal handler */
735
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
736
  
24.1.8 by Björn Påhlsson
plugbasedclient
737
  free(plus_argv);
738
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
739
  /* Free the plugin list */
740
  for(plugin *next; plugin_list != NULL; plugin_list = next){
741
    next = plugin_list->next;
742
    free(plugin_list->argv);
743
    free(plugin_list);
744
  }
745
  
746
  if(dir != NULL){
747
    closedir(dir);
748
  }
749
  
750
  /* Free the process list and kill the processes */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
751
  for(process *next; process_list != NULL; process_list = next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
752
    next = process_list->next;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
753
    close(process_list->fd);
24.1.26 by Björn Påhlsson
tally count of used symbols
754
    ret = kill(process_list->pid, SIGTERM);
755
    if(ret == -1 and errno != ESRCH){
756
      /* set-uid proccesses migth not get closed */
757
      perror("kill");
758
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
759
    free(process_list->buffer);
760
    free(process_list);
13 by Björn Påhlsson
Added following support:
761
  }
762
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
763
  /* Wait for any remaining child processes to terminate */
764
  do{
765
    ret = wait(NULL);
766
  } while(ret >= 0);
767
  if(errno != ECHILD){
768
    perror("wait");
769
  }
770
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
771
  return exitstatus;
13 by Björn Påhlsson
Added following support:
772
}