/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release
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");
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
460
      exitstatus = EXIT_FAILURE;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
461
      goto end;
462
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
463
    strcpy(filename, plugindir); /* Spurious warning */
464
    strcat(filename, "/");	/* Spurious warning */
465
    strcat(filename, dirst->d_name); /* Spurious warning */
466
    
24.1.26 by Björn Påhlsson
tally count of used symbols
467
    ret = stat(filename, &st);
468
    if (ret == -1){
469
      perror("stat");
470
      exitstatus = EXIT_FAILURE;
471
      goto end;
472
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
473
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
474
    if (not S_ISREG(st.st_mode)	or (access(filename, X_OK) != 0)){
475
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
476
	fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
477
		" with bad type or mode\n", filename);
478
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
479
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
480
      continue;
481
    }
482
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
483
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
484
	fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
485
		dirst->d_name);
486
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
487
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
488
      continue;
489
    }
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
490
    plugin *p = getplugin(dirst->d_name, &plugin_list);
491
    {
492
      /* Add global arguments to argument list for this plugin */
493
      plugin *g = getplugin(NULL, &plugin_list);
494
      for(char **a = g->argv + 1; *a != NULL; a++){
495
	addargument(p, *a);
496
      }
497
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
498
    int pipefd[2]; 
499
    ret = pipe(pipefd);
500
    if (ret == -1){
501
      perror("pipe");
502
      exitstatus = EXIT_FAILURE;
503
      goto end;
504
    }
505
    ret = set_cloexec_flag(pipefd[0]);
506
    if(ret < 0){
507
      perror("set_cloexec_flag");
508
      exitstatus = EXIT_FAILURE;
509
      goto end;
510
    }
511
    ret = set_cloexec_flag(pipefd[1]);
512
    if(ret < 0){
513
      perror("set_cloexec_flag");
514
      exitstatus = EXIT_FAILURE;
515
      goto end;
516
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
517
    /* Block SIGCHLD until process is safely in process list */
518
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
519
    if(ret < 0){
520
      perror("sigprocmask");
521
      exitstatus = EXIT_FAILURE;
522
      goto end;
523
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
524
    // Starting a new process to be watched
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
525
    pid_t pid = fork();
526
    if(pid == 0){
527
      /* this is the child process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
528
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
529
      if(ret < 0){
530
	perror("sigaction");
531
	_exit(EXIT_FAILURE);
532
      }
533
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
534
      if(ret < 0){
535
	perror("sigprocmask");
536
	_exit(EXIT_FAILURE);
537
      }
24.1.26 by Björn Påhlsson
tally count of used symbols
538
539
      ret = dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
540
      if(ret == -1){
541
	perror("dup2");
542
	_exit(EXIT_FAILURE);
543
      }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
544
      
545
      if(dirfd(dir) < 0){
546
	/* If dir has no file descriptor, we could not set FD_CLOEXEC
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
547
	   above and must now close it manually here. */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
548
	closedir(dir);
33 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (main): Close the pipe fd after dup2:ing
549
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
550
      if(execv(filename, p->argv) < 0){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
551
	perror("execv");
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
552
	_exit(EXIT_FAILURE);
553
      }
554
      /* no return */
555
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
556
    /* parent process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
557
    free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
558
    close(pipefd[1]);		/* close unused write end of pipe */
559
    process *new_process = malloc(sizeof(process));
560
    if (new_process == NULL){
561
      perror("malloc");
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
562
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
563
      if(ret < 0){
564
	perror("sigprocmask");
565
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
566
      exitstatus = EXIT_FAILURE;
567
      goto end;
568
    }
569
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
570
    *new_process = (struct process){ .pid = pid,
571
				     .fd = pipefd[0],
572
				     .next = process_list };
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
573
    // List handling
574
    process_list = new_process;
575
    /* Unblock SIGCHLD so signal handler can be run if this process
576
       has already completed */
577
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
578
    if(ret < 0){
579
      perror("sigprocmask");
580
      exitstatus = EXIT_FAILURE;
581
      goto end;
582
    }
583
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
584
    FD_SET(new_process->fd, &rfds_all);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
585
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
586
    if (maxfd < new_process->fd){
587
      maxfd = new_process->fd;
588
    }
589
    
13 by Björn Påhlsson
Added following support:
590
  }
591
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
592
  /* Free the plugin list */
593
  for(plugin *next; plugin_list != NULL; plugin_list = next){
594
    next = plugin_list->next;
595
    free(plugin_list->argv);
596
    free(plugin_list);
597
  }
598
  
13 by Björn Påhlsson
Added following support:
599
  closedir(dir);
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
600
  dir = NULL;
24.1.26 by Björn Påhlsson
tally count of used symbols
601
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
602
  if (process_list == NULL){
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
603
    fprintf(stderr, "No plugin processes started. Incorrect plugin"
604
	    " directory?\n");
605
    process_list = NULL;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
606
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
607
  while(process_list){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
608
    fd_set rfds = rfds_all;
609
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
610
    if (select_ret == -1){
611
      perror("select");
612
      exitstatus = EXIT_FAILURE;
613
      goto end;
614
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
615
    /* OK, now either a process completed, or something can be read
616
       from one of them */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
617
    for(process *proc = process_list; proc ; proc = proc->next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
618
      /* Is this process completely done? */
619
      if(proc->eof and proc->completed){
620
	/* Only accept the plugin output if it exited cleanly */
621
	if(not WIFEXITED(proc->status)
622
	   or WEXITSTATUS(proc->status) != 0){
623
	  /* Bad exit by plugin */
624
	  if(debug){
625
	    if(WIFEXITED(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
626
	      fprintf(stderr, "Plugin %u exited with status %d\n",
627
		      (unsigned int) (proc->pid),
628
		      WEXITSTATUS(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
629
	    } else if(WIFSIGNALED(proc->status)) {
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
630
	      fprintf(stderr, "Plugin %u killed by signal %d\n",
631
		      (unsigned int) (proc->pid),
632
		      WTERMSIG(proc->status));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
633
	    } else if(WCOREDUMP(proc->status)){
60 by Teddy Hogeborn
* mandos-client.c (main): Cast pid_t to unsigned int before printing.
634
	      fprintf(stderr, "Plugin %d dumped core\n",
635
		      (unsigned int) (proc->pid));
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
636
	    }
637
	  }
638
	  /* Remove the plugin */
639
	  FD_CLR(proc->fd, &rfds_all);
640
	  /* Block signal while modifying process_list */
641
	  ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
642
	  if(ret < 0){
643
	    perror("sigprocmask");
644
	    exitstatus = EXIT_FAILURE;
645
	    goto end;
646
	  }
647
	  /* Delete this process entry from the list */
648
	  if(process_list == proc){
649
	    /* First one - simple */
650
	    process_list = proc->next;
651
	  } else {
652
	    /* Second one or later */
653
	    for(process *p = process_list; p != NULL; p = p->next){
654
	      if(p->next == proc){
655
		p->next = proc->next;
656
		break;
657
	      }
658
	    }
659
	  }
660
	  /* We are done modifying process list, so unblock signal */
661
	  ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
662
			     NULL);
663
	  if(ret < 0){
664
	    perror("sigprocmask");
665
	  }
666
	  free(proc->buffer);
667
	  free(proc);
668
	  /* We deleted this process from the list, so we can't go
669
	     proc->next.  Therefore, start over from the beginning of
670
	     the process list */
671
	  break;
672
	}
673
	/* This process exited nicely, so print its buffer */
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
674
675
	bool bret = print_out_password(proc->buffer, proc->buffer_length);
676
	if(not bret){
677
	  perror("print_out_password");
678
	  exitstatus = EXIT_FAILURE;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
679
	}
680
	goto end;
681
      }
682
      /* This process has not completed.  Does it have any output? */
683
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
684
	/* This process had nothing to say at this time */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
685
	continue;
686
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
687
      /* Before reading, make the process' data buffer large enough */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
688
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
689
	proc->buffer = realloc(proc->buffer, proc->buffer_size
690
			       + (size_t) BUFFER_SIZE);
691
	if (proc->buffer == NULL){
692
	  perror("malloc");
693
	  exitstatus = EXIT_FAILURE;
694
	  goto end;
695
	}
696
	proc->buffer_size += BUFFER_SIZE;
697
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
698
      /* Read from the process */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
699
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
700
		 BUFFER_SIZE);
701
      if(ret < 0){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
702
	/* Read error from this process; ignore the error */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
703
	continue;
704
      }
705
      if(ret == 0){
706
	/* got EOF */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
707
	proc->eof = true;
708
      } else {
709
	proc->buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
710
      }
711
    }
712
  }
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
713
714
715
 end:
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
716
  
24.1.45 by Björn Påhlsson
Fixed fallback on error in mandos-client
717
  if(process_list == NULL or exitstatus != EXIT_SUCCESS){
718
    /* Fallback if all plugins failed or an error occured */
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
719
    bool bret;
720
    fprintf(stderr, "Going to fallback mode using getpass(3)\n");
721
    char *passwordbuffer = getpass("Password: ");
722
    bret = print_out_password(passwordbuffer, strlen(passwordbuffer));
723
    if(not bret){
724
      perror("print_out_password");
725
      exitstatus = EXIT_FAILURE;
726
      goto end;
727
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
728
  }
24.1.42 by Björn Påhlsson
Added fallback to mandos-client
729
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
730
  /* Restore old signal handler */
731
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
732
  
24.1.8 by Björn Påhlsson
plugbasedclient
733
  free(plus_argv);
734
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
735
  /* Free the plugin list */
736
  for(plugin *next; plugin_list != NULL; plugin_list = next){
737
    next = plugin_list->next;
738
    free(plugin_list->argv);
739
    free(plugin_list);
740
  }
741
  
742
  if(dir != NULL){
743
    closedir(dir);
744
  }
745
  
746
  /* Free the process list and kill the processes */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
747
  for(process *next; process_list != NULL; process_list = next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
748
    next = process_list->next;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
749
    close(process_list->fd);
24.1.26 by Björn Påhlsson
tally count of used symbols
750
    ret = kill(process_list->pid, SIGTERM);
751
    if(ret == -1 and errno != ESRCH){
752
      /* set-uid proccesses migth not get closed */
753
      perror("kill");
754
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
755
    free(process_list->buffer);
756
    free(process_list);
13 by Björn Påhlsson
Added following support:
757
  }
758
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
759
  /* Wait for any remaining child processes to terminate */
760
  do{
761
    ret = wait(NULL);
762
  } while(ret >= 0);
763
  if(errno != ECHILD){
764
    perror("wait");
765
  }
766
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
767
  return exitstatus;
13 by Björn Påhlsson
Added following support:
768
}