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