/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-29 03:35:39 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080729033539-08zecoj3jwlkpjhw
* server.conf: New file.

* mandos-clients.conf: Renamed to clients.conf.

* Makefile (FORTIFY): New.
  (CFLAGS): Include $(FORTIFY).

* plugins.d/mandosclient.c (main): New "if_index" variable.  Bug fix:
                                   check if interface exists.  New
                                   "--connect" option.

* server.py (serviceInterface): Removed; replaced by
                                "AvahiService.interface".  All users
                                changed.
  (AvahiError, AvahiServiceError, AvahiGroupError): New exception
                                                    classes.
  (AvahiService): New class.
  (serviceName): Removed; replaced by "AvahiService.name".  All users
                 changed.
  (serviceType): Removed; replaced by "AvahiService.type".  All users
                 changed.
  (servicePort): Removed; replaced by "AvahiService.port".  All users
                 changed.
  (serviceTXT): Removed; replaced by "AvahiService.TXT".  All users
                changed.
  (domain): Removed; replaced by "AvahiService.domain".  All users
            changed.
  (host): Removed; replaced by "AvahiService.host".  All users
          changed.
  (rename_count): Removed; replaced by "AvahiService.rename_count" and
                 "AvahiService.max_renames".  All users changed.
  (Client.__init__): If no secret or secfile, raise TypeError instead
                     of RuntimeError.
  (Client.last_seen): Renamed to "Client.last_checked_ok".  All users
                      changed.
  (Client.stop, Client.stop_checker): Use "getattr" with default value
                                      instead of "hasattr".
  (Client.still_valid): Removed "now" argument.
  (Client.handle): Separate the "no client found" and "client invalid"
                   cases for clearer code.
  (IPv6_TCPServer.__init__): "options" argument replaced by
                             "settings".  All callers changed.
  (IPv6_TCPServer.options): Replaced by "IPv6_TCPServer.settings".
                            All users changed.
  (IPv6_TCPServer.server_bind): Use getattr instead of hasattr.
  (add_service): Removed; replaced by "AvahiService.add".  All callers
                 changed.
  (remove_service): Removed; replaced by "AvahiService.remove".  All
                    callers changed.
  (entry_group_state_changed): On entry group collision, call the new
                               AvahiService.rename method.  Raise
                               AvahiGroupError on group error.
  (if_nametoindex): Use ctypes.utils.find_library to locate the C
                    library.  Cache the result.  Loop on EINTR.
  (daemon): Use os.path.devnull to locate "/dev/null".
  (killme): Removed.  All callers changed to do "sys.exit()" instead,
            except where stated otherwise.
  (main): Removed "exitstatus".  Removed all default values from all
          non-bool options.  New option "--configdir".  New variables
          "server_defaults" and "server_settings", read from
          "%(configdir)s/server.conf".  Let any supplied command line
          options override server settings.   Variable "defaults"
          renamed to "client_defaults", which is read from
          "clients.conf" instead of "mandos-clients.conf".  New global
          AvahiService object "service" replaces old global variables.
          Catch AvahiError and exit with error if caught.

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 and Björn Påhlsson.
 
5
 * Copyright © 2007-2008 Teddy Hogeborn & 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 <https://www.fukt.bsnet.se/~belorn/> and
22
 
 * <https://www.fukt.bsnet.se/~teddy/>.
 
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
23
22
 */
24
23
 
25
 
#define _FORTIFY_SOURCE 2
26
 
 
27
24
#include <stdio.h>      /* popen, fileno */
28
25
#include <iso646.h>     /* and, or, not */
29
26
#include <sys/types.h>  /* DIR, opendir, stat, struct stat, waitpid,
39
36
#include <stdbool.h>    /* true */
40
37
#include <sys/wait.h>   /* waitpid, WIFEXITED, WEXITSTATUS */
41
38
#include <errno.h>      /* errno */
 
39
#include <argp.h>       /* argp */
42
40
 
43
41
struct process;
44
42
 
51
49
  struct process *next;
52
50
} process;
53
51
 
 
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
        
54
98
#define BUFFER_SIZE 256
55
99
 
 
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
 
56
109
int main(int argc, char *argv[]){
57
110
  char plugindir[] = "plugins.d";
58
111
  size_t d_name_len, plugindir_len = sizeof(plugindir)-1;
63
116
  int ret, maxfd = 0;
64
117
  process *process_list = NULL;
65
118
  
 
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
  
66
182
  dir = opendir(plugindir);
67
 
 
 
183
  
68
184
  if(dir == NULL){
69
185
    fprintf(stderr, "Can not open directory\n");
70
186
    return EXIT_FAILURE;
99
215
      // Starting a new process to be watched
100
216
      process *new_process = malloc(sizeof(process));
101
217
      int pipefd[2];
102
 
      pipe(pipefd);
 
218
      ret = pipe(pipefd);
 
219
      if (ret == -1){
 
220
        perror(argv[0]);
 
221
        goto end;
 
222
      }
103
223
      new_process->pid = fork();
104
224
      if(new_process->pid == 0){
105
225
        /* this is the child process */
106
226
        closedir(dir);
107
227
        close(pipefd[0]);       /* close unused read end of pipe */
108
228
        dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
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){
 
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){
118
243
          perror(argv[0]);
119
244
          close(pipefd[1]);
120
245
          exit(EXIT_FAILURE);
179
304
              int status;
180
305
              waitpid(process_itr->pid, &status, 0);
181
306
              if(WIFEXITED(status) and WEXITSTATUS(status) == 0){
182
 
                write(STDOUT_FILENO, process_itr->buffer,
183
 
                      process_itr->buffer_length);
 
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
                }
184
318
                goto end;
185
319
              } else {
186
320
                FD_CLR(process_itr->fd, &rfds_orig);