/mandos/release

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

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2012-06-01 16:07:20 UTC
  • mfrom: (237.7.140 trunk)
  • mto: This revision was merged to the branch mainline in revision 301.
  • Revision ID: teddy@recompile.se-20120601160720-vsivlnue8qqkcsla
Merge from trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008-2012 Teddy Hogeborn
 
13
 * Copyright © 2008-2012 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
41
41
 
42
42
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
43
43
                                   stdout, ferror(), remove() */
44
 
#include <stdint.h>             /* uint16_t, uint32_t */
 
44
#include <stdint.h>             /* uint16_t, uint32_t, intptr_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
47
                                   strtof(), abort() */
170
170
 
171
171
/* Function to use when printing errors */
172
172
void perror_plus(const char *print_text){
 
173
  int e = errno;
173
174
  fprintf(stderr, "Mandos plugin %s: ",
174
175
          program_invocation_short_name);
 
176
  errno = e;
175
177
  perror(print_text);
176
178
}
177
179
 
 
180
__attribute__((format (gnu_printf, 2, 3)))
178
181
int fprintf_plus(FILE *stream, const char *format, ...){
179
182
  va_list ap;
180
183
  va_start (ap, format);
202
205
}
203
206
 
204
207
/* Add server to set of servers to retry periodically */
205
 
int add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
206
 
               int af){
 
208
bool add_server(const char *ip, uint16_t port, AvahiIfIndex if_index,
 
209
                int af){
207
210
  int ret;
208
211
  server *new_server = malloc(sizeof(server));
209
212
  if(new_server == NULL){
210
213
    perror_plus("malloc");
211
 
    return -1;
 
214
    return false;
212
215
  }
213
216
  *new_server = (server){ .ip = strdup(ip),
214
217
                          .port = port,
216
219
                          .af = af };
217
220
  if(new_server->ip == NULL){
218
221
    perror_plus("strdup");
219
 
    return -1;
 
222
    return false;
220
223
  }
221
224
  /* Special case of first server */
222
225
  if (mc.current_server == NULL){
233
236
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
234
237
  if(ret == -1){
235
238
    perror_plus("clock_gettime");
236
 
    return -1;
 
239
    return false;
237
240
  }
238
 
  return 0;
 
241
  return true;
239
242
}
240
243
 
241
244
/* 
818
821
    goto mandos_end;
819
822
  }
820
823
  
821
 
  /* Spurious warning from -Wint-to-pointer-cast */
822
 
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
 
824
  /* This casting via intptr_t is to eliminate warning about casting
 
825
     an int to a pointer type.  This is exactly how the GnuTLS Guile
 
826
     function "set-session-transport-fd!" does it. */
 
827
  gnutls_transport_set_ptr(session,
 
828
                           (gnutls_transport_ptr_t)(intptr_t)tcp_sd);
823
829
  
824
830
  if(quit_now){
825
831
    errno = EINTR;
1030
1036
      if(ret == 0){
1031
1037
        avahi_simple_poll_quit(mc.simple_poll);
1032
1038
      } else {
1033
 
        ret = add_server(ip, port, interface,
1034
 
                         avahi_proto_to_af(proto));
 
1039
        if(not add_server(ip, port, interface,
 
1040
                          avahi_proto_to_af(proto))){
 
1041
          fprintf_plus(stderr, "Failed to add server \"%s\" to server"
 
1042
                       " list\n", name);
 
1043
        }
1035
1044
      }
1036
1045
    }
1037
1046
  }
1444
1453
          perror_plus("setenv");
1445
1454
          _exit(EX_OSERR);
1446
1455
        }
1447
 
        ret = setenv("VERBOSE", debug ? "1" : "0", 1);
 
1456
        ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1448
1457
        if(ret == -1){
1449
1458
          perror_plus("setenv");
1450
1459
          _exit(EX_OSERR);
1467
1476
          _exit(EX_OSERR);
1468
1477
        }
1469
1478
        free(delaystring);
1470
 
        ret = execl(fullname, direntry->d_name, mode, NULL);
1471
 
        perror_plus("execl");
 
1479
        if(connect_to != NULL){
 
1480
          ret = setenv("CONNECT", connect_to, 1);
 
1481
          if(ret == -1){
 
1482
            perror_plus("setenv");
 
1483
            _exit(EX_OSERR);
 
1484
          }
 
1485
        }
 
1486
        if(execl(fullname, direntry->d_name, mode, NULL) == -1){
 
1487
          perror_plus("execl");
 
1488
          _exit(EXIT_FAILURE);
 
1489
        }
1472
1490
      } else {
1473
1491
        int status;
1474
1492
        if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1593
1611
        .group = 2 },
1594
1612
      { .name = "retry", .key = 132,
1595
1613
        .arg = "SECONDS",
1596
 
        .doc = "Retry interval used when denied by the mandos server",
 
1614
        .doc = "Retry interval used when denied by the Mandos server",
1597
1615
        .group = 2 },
1598
1616
      { .name = "network-hook-dir", .key = 133,
1599
1617
        .arg = "DIR",
1671
1689
        argp_state_help(state, state->out_stream,
1672
1690
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1673
1691
      case 'V':                 /* --version */
1674
 
        fprintf_plus(state->out_stream,
1675
 
                     "Mandos plugin mandos-client: ");
1676
1692
        fprintf_plus(state->out_stream, "%s\n", argp_program_version);
1677
1693
        exit(argp_err_exit_status);
1678
1694
        break;