/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-07-21 15:34:44 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080721153444-lugbjkj1oq65ugq3
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
                     $(LANGUAGE).
  (WARN, DEBUG, COVERAGE, LANGUAGE): New.
  (LDFLAGS): New; use $(COVERAGE)

* plugbasedclient.c: Added copyright header.
  (process.buffer_size, process.buffer_length): Changed to "size_t".
  (main): Cast arguments to malloc and realloc.  Detect read errors
          from processes.

* mandosclient.c: Added copyright header.
  (interface): Moved to inside "main".
  (gpg_packet_decrypt): Renamed to "pgp_packet_decrypt"; all callers
                        changed.  Changed "new_packet_capacity" and
                        "new_packet_length" to be ssize_t.  Cast
                        arguments to realloc.
  (debuggnutls): Attribute "level" argument as unused.
  (empty_log): Attribute "level" and "txt" arguments as unused.
  (start_mandos_communication): New argument "if_index".  Bug fix:
                                check ret, no tcp_sd, for errors from
                                setsockopt.  Use "if_index" directly
                                instead of looking up the index.  Loop
                                around fwrite until all data is written.
  (resolve_callback): Attribute "txt", and "flags" as usused.  Added
                      default case to switch.  Also show server host
                      name.  Call start_mandos_communication with
                      "interface".
  (browse_callback): Added default case to switch.
  (main): Variable "interface" moved here.  Cast "srand" argument.
          Bug fix: Call avahi_s_service_browser_new with index of
          "interface", not "eth0".

* passprompt.c: Added copyright header.
  (termination_handler): Attribute "signum" argument as unused.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
/*
3
3
 * Mandos plugin runner - Run Mandos plugins
4
4
 *
5
 
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
 
5
 * Copyright © 2007-2008 Teddy Hogeborn and Björn Påhlsson.
6
6
 * 
7
7
 * This program is free software: you can redistribute it and/or
8
8
 * modify it under the terms of the GNU General Public License as
18
18
 * along with this program.  If not, see
19
19
 * <http://www.gnu.org/licenses/>.
20
20
 * 
21
 
 * Contact the authors at <mandos@fukt.bsnet.se>.
 
21
 * Contact the authors at <https://www.fukt.bsnet.se/~belorn/> and
 
22
 * <https://www.fukt.bsnet.se/~teddy/>.
22
23
 */
23
24
 
 
25
#define _FORTIFY_SOURCE 2
 
26
 
24
27
#include <stdio.h>      /* popen, fileno */
25
28
#include <iso646.h>     /* and, or, not */
26
29
#include <sys/types.h>  /* DIR, opendir, stat, struct stat, waitpid,
36
39
#include <stdbool.h>    /* true */
37
40
#include <sys/wait.h>   /* waitpid, WIFEXITED, WEXITSTATUS */
38
41
#include <errno.h>      /* errno */
39
 
#include <argp.h>       /* argp */
40
42
 
41
43
struct process;
42
44
 
49
51
  struct process *next;
50
52
} process;
51
53
 
52
 
typedef struct plugin{
53
 
  char *name;           /* can be "global" and any plugin name */
54
 
  char **argv;
55
 
  int argc;
56
 
  struct plugin *next;
57
 
} plugin;
58
 
 
59
 
plugin *getplugin(char *name, plugin **plugin_list){
60
 
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
61
 
    if ((p->name == name)
62
 
        or (p->name and name and (strcmp(p->name, name) == 0))){
63
 
      return p;
64
 
    }
65
 
  }
66
 
  /* Create a new plugin */
67
 
  plugin *new_plugin = malloc(sizeof(plugin));
68
 
  if (new_plugin == NULL){
69
 
    perror("malloc");
70
 
    exit(EXIT_FAILURE);
71
 
  }
72
 
  new_plugin->name = name;
73
 
  new_plugin->argv = malloc(sizeof(char *) * 2);
74
 
  if (new_plugin->argv == NULL){
75
 
    perror("malloc");
76
 
    exit(EXIT_FAILURE);
77
 
  }
78
 
  new_plugin->argv[0] = name;
79
 
  new_plugin->argv[1] = NULL;
80
 
  new_plugin->argc = 1;
81
 
  /* Append the new plugin to the list */
82
 
  new_plugin->next = *plugin_list;
83
 
  *plugin_list = new_plugin;
84
 
  return new_plugin;
85
 
}
86
 
 
87
 
void addarguments(plugin *p, char *arg){
88
 
  p->argv[p->argc] = arg;
89
 
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
90
 
  if (p->argv == NULL){
91
 
    perror("malloc");
92
 
    exit(EXIT_FAILURE);
93
 
  }
94
 
  p->argc++;
95
 
  p->argv[p->argc] = NULL;
96
 
}
97
 
        
98
54
#define BUFFER_SIZE 256
99
55
 
100
 
const char *argp_program_version =
101
 
  "plugbasedclient 0.9";
102
 
const char *argp_program_bug_address =
103
 
  "<mandos@fukt.bsnet.se>";
104
 
static char doc[] =
105
 
  "Mandos plugin runner -- Run Mandos plugins";
106
 
/* A description of the arguments we accept. */
107
 
static char args_doc[] = "";
108
 
 
109
56
int main(int argc, char *argv[]){
110
57
  char plugindir[] = "plugins.d";
111
58
  size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
116
63
  int ret, maxfd = 0;
117
64
  process *process_list = NULL;
118
65
  
119
 
  /* The options we understand. */
120
 
  struct argp_option options[] = {
121
 
    { .name = "global-options", .key = 'g',
122
 
      .arg = "option[,option[,...]]", .flags = 0,
123
 
      .doc = "Options effecting all plugins" },
124
 
    { .name = "options-for", .key = 'o',
125
 
      .arg = "plugin:option[,option[,...]]", .flags = 0,
126
 
      .doc = "Options effecting only specified plugins" },
127
 
    { .name = NULL }
128
 
  };
129
 
  
130
 
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
131
 
       /* Get the INPUT argument from `argp_parse', which we
132
 
          know is a pointer to our arguments structure. */
133
 
    plugin **plugins = state->input;
134
 
    switch (key) {
135
 
    case 'g':
136
 
      if (arg != NULL){
137
 
        char *p = strtok(arg, ",");
138
 
        do{
139
 
          addarguments(getplugin(NULL, plugins), p);
140
 
          p = strtok(NULL, ",");
141
 
        } while (p);
142
 
      }
143
 
      break;
144
 
    case 'o':
145
 
      if (arg != NULL){
146
 
        char *name = strtok(arg, ":");
147
 
        char *p = strtok(NULL, ":");
148
 
        p = strtok(p, ",");
149
 
        do{
150
 
          addarguments(getplugin(name, plugins), p);
151
 
          p = strtok(NULL, ",");
152
 
        } while (p);
153
 
      }
154
 
      break;
155
 
    case ARGP_KEY_ARG:
156
 
      argp_usage (state);
157
 
      break;
158
 
    case ARGP_KEY_END:
159
 
      break;
160
 
    default:
161
 
      return ARGP_ERR_UNKNOWN;
162
 
    }
163
 
    return 0;
164
 
  }
165
 
 
166
 
  plugin *plugin_list = NULL;
167
 
  
168
 
  struct argp argp = { .options = options, .parser = parse_opt,
169
 
                       .args_doc = args_doc, .doc = doc };
170
 
 
171
 
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);
172
 
 
173
 
/*   for(plugin *p = plugin_list; p != NULL; p=p->next){ */
174
 
/*     fprintf(stderr, "Plugin: %s has %d arguments\n", p->name ? p->name : "Global", p->argc); */
175
 
/*     for(char **a = p->argv + 1; *a != NULL; a++){ */
176
 
/*       fprintf(stderr, "\tArg: %s\n", *a); */
177
 
/*     } */
178
 
/*   } */
179
 
  
180
 
/*   return 0; */
181
 
  
182
66
  dir = opendir(plugindir);
183
 
  
 
67
 
184
68
  if(dir == NULL){
185
69
    fprintf(stderr, "Can not open directory\n");
186
70
    return EXIT_FAILURE;
215
99
      // Starting a new process to be watched
216
100
      process *new_process = malloc(sizeof(process));
217
101
      int pipefd[2];
218
 
      ret = pipe(pipefd);
219
 
      if (ret == -1){
220
 
        perror(argv[0]);
221
 
        goto end;
222
 
      }
 
102
      pipe(pipefd);
223
103
      new_process->pid = fork();
224
104
      if(new_process->pid == 0){
225
105
        /* this is the child process */
226
106
        closedir(dir);
227
107
        close(pipefd[0]);       /* close unused read end of pipe */
228
108
        dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
229
 
        char *basename;
230
 
        basename = strrchr(filename, '/');
231
 
        if (basename == NULL){
232
 
          basename = filename;
233
 
        } else {
234
 
          basename++;
235
 
        }
236
 
        plugin *p = getplugin(basename, &plugin_list);
237
 
 
238
 
        plugin *g = getplugin(NULL, &plugin_list);
239
 
        for(char **a = g->argv + 1; *a != NULL; a++){
240
 
          addarguments(p, *a);
241
 
        }
242
 
        if(execv(filename, p->argv) < 0){
 
109
        /* create a new modified argument list */
 
110
        char **new_argv = malloc(sizeof(char *)
 
111
                                 * ((unsigned int) argc + 1));
 
112
        new_argv[0] = filename;
 
113
        for(int i = 1; i < argc; i++){
 
114
          new_argv[i] = argv[i];
 
115
        }
 
116
        new_argv[argc] = NULL;
 
117
        if(execv(filename, new_argv) < 0){
243
118
          perror(argv[0]);
244
119
          close(pipefd[1]);
245
120
          exit(EXIT_FAILURE);
304
179
              int status;
305
180
              waitpid(process_itr->pid, &status, 0);
306
181
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
307
 
                for(size_t written = 0;
308
 
                    written < process_itr->buffer_length;){
309
 
                  ret = write(STDOUT_FILENO,
310
 
                              process_itr->buffer + written,
311
 
                              process_itr->buffer_length - written);
312
 
                  if(ret < 0){
313
 
                    perror(argv[0]);
314
 
                    goto end;
315
 
                  }
316
 
                  written += (size_t)ret;
317
 
                }
 
182
                write(STDOUT_FILENO, process_itr->buffer,
 
183
                      process_itr->buffer_length);
318
184
                goto end;
319
185
              } else {
320
186
                FD_CLR(process_itr->fd, &rfds_orig);