/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 plugins.d/mandos-client.c

  • Committer: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno,
66
 
                                   program_invocation_short_name */
 
65
#include <errno.h>              /* perror(), errno, program_invocation_short_name */
67
66
#include <time.h>               /* nanosleep(), time(), sleep() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
78
77
#include <iso646.h>             /* not, or, and */
79
78
#include <argp.h>               /* struct argp_option, error_t, struct
80
79
                                   argp_state, struct argp,
131
130
static const char sys_class_net[] = "/sys/class/net";
132
131
char *connect_to = NULL;
133
132
 
134
 
/* Doubly linked list that need to be circularly linked when used */
 
133
/* Doubly linked list that need to be circular linked when ever used */
135
134
typedef struct server{
136
135
  const char *ip;
137
136
  uint16_t port;
157
156
/* global context so signal handler can reach it*/
158
157
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
158
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
159
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
162
160
 
163
161
sig_atomic_t quit_now = 0;
164
162
int signal_received = 0;
165
163
 
166
164
/* Function to use when printing errors */
167
165
void perror_plus(const char *print_text){
168
 
  fprintf(stderr, "Mandos plugin %s: ",
169
 
          program_invocation_short_name);
 
166
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
170
167
  perror(print_text);
171
168
}
172
169
 
202
199
                         .af = af };
203
200
  if(new_server->ip == NULL){
204
201
    perror_plus("strdup");
205
 
    return -1;
 
202
    return -1;    
206
203
  }
207
 
  /* unique case of first server */
 
204
  /* uniqe case of first server */
208
205
  if (mc.current_server == NULL){
209
206
    new_server->next = new_server;
210
207
    new_server->prev = new_server;
481
478
  }
482
479
  
483
480
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
481
  gnutls_certificate_allocate_credentials(&mc.cred);
485
482
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
483
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
484
                                                    from
 
485
                                                    -Wunreachable-code
 
486
                                                 */
487
487
            safer_gnutls_strerror(ret));
488
488
    gnutls_global_deinit();
489
489
    return -1;
796
796
    errno = EINTR;
797
797
    goto mandos_end;
798
798
  }
799
 
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
 
799
 
 
800
  /* Spurious warnings from -Wint-to-pointer-cast */
801
801
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
802
  
803
803
  if(quit_now){
1069
1069
  }
1070
1070
}
1071
1071
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
1072
/* Signal handler that stops main loop after sigterm has been called */
1073
1073
static void handle_sigterm(int sig){
1074
1074
  if(quit_now){
1075
1075
    return;
1110
1110
  free(flagname);
1111
1111
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1112
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1113
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1114
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1115
  flagstring[(size_t)to_read] = '\0';
1116
1116
  if(flagstring == NULL){
1209
1209
  while(true){
1210
1210
    if(mc.current_server == NULL){
1211
1211
      if (debug){
1212
 
        fprintf(stderr,
1213
 
                "Wait until first server is found. No timeout!\n");
 
1212
        fprintf(stderr, "Wait until first server is found. No timeout!\n");     
1214
1213
      }
1215
1214
      ret = avahi_simple_poll_iterate(s, -1);
1216
1215
    } else {
1217
1216
      if (debug){
1218
 
        fprintf(stderr, "Check current_server if we should run it,"
1219
 
                " or wait\n");
 
1217
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
1220
1218
      }
1221
1219
      /* the current time */
1222
1220
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1226
1224
      }
1227
1225
      /* Calculating in ms how long time between now and server
1228
1226
         who we visted longest time ago. Now - last seen.  */
1229
 
      waited_time.tv_sec = (now.tv_sec
1230
 
                            - mc.current_server->last_seen.tv_sec);
1231
 
      waited_time.tv_nsec = (now.tv_nsec
1232
 
                             - mc.current_server->last_seen.tv_nsec);
1233
 
      /* total time is 10s/10,000ms.
1234
 
         Converting to s from ms by dividing by 1,000,
1235
 
         and ns to ms by dividing by 1,000,000. */
1236
 
      block_time = ((retry_interval
1237
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1227
      waited_time.tv_sec = now.tv_sec - mc.current_server->last_seen.tv_sec;
 
1228
      waited_time.tv_nsec = now.tv_nsec - mc.current_server->last_seen.tv_nsec;
 
1229
      /* total time is 10s/10000ms. Converting to s to ms by 1000/s, and ns to ms by divind by 1000000. */
 
1230
      block_time = (retry_interval - ((intmax_t)waited_time.tv_sec * 1000)) - ((intmax_t)waited_time.tv_nsec / 1000000);
1239
1231
 
1240
1232
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
 
1233
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
1242
1234
      }
1243
1235
 
1244
1236
      if(block_time <= 0){
1245
1237
        ret = start_mandos_communication(mc.current_server->ip,
1246
 
                                         mc.current_server->port,
1247
 
                                         mc.current_server->if_index,
1248
 
                                         mc.current_server->af);
 
1238
                                   mc.current_server->port,
 
1239
                                   mc.current_server->if_index,
 
1240
                                   mc.current_server->af);
1249
1241
        if(ret == 0){
1250
1242
          avahi_simple_poll_quit(mc.simple_poll);
1251
1243
          return 0;
1252
1244
        }
1253
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1254
 
                            &mc.current_server->last_seen);
 
1245
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1255
1246
        if(ret == -1){
1256
1247
          perror_plus("clock_gettime");
1257
1248
          return -1;
1258
1249
        }
1259
1250
        mc.current_server = mc.current_server->next;
1260
 
        block_time = 0;         /* Call avahi to find new Mandos
1261
 
                                   servers, but don't block */
 
1251
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
1262
1252
      }
1263
1253
      
1264
1254
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1293
1283
  bool gnutls_initialized = false;
1294
1284
  bool gpgme_initialized = false;
1295
1285
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
 
1286
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
1298
1287
  
1299
1288
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1289
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1656
1645
        goto end;
1657
1646
      }
1658
1647
    }
1659
 
    /* Sleep checking until interface is running.
1660
 
       Check every 0.25s, up to total time of delay */
 
1648
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
1661
1649
    for(int i=0; i < delay * 4; i++){
1662
1650
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1663
1651
      if(ret == -1){
1856
1844
    fprintf(stderr, "Starting Avahi loop search\n");
1857
1845
  }
1858
1846
 
1859
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1860
 
                                (int)(retry_interval * 1000));
 
1847
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1861
1848
  if(debug){
1862
1849
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1863
1850
            (ret == 0) ? "successfully" : "with error");
1889
1876
    gpgme_release(mc.ctx);
1890
1877
  }
1891
1878
 
1892
 
  /* Cleans up the circular linked list of Mandos servers the client
1893
 
     has seen */
 
1879
  /* cleans up the circular linked list of mandos servers the client has seen */
1894
1880
  if(mc.current_server != NULL){
1895
1881
    mc.current_server->prev->next = NULL;
1896
1882
    while(mc.current_server != NULL){
1956
1942
      }
1957
1943
    }
1958
1944
 
1959
 
    /* need to be cleaned even if ret == 0 because man page doesn't
1960
 
       specify */
 
1945
    /* need to be cleaned even if ret == 0 because man page dont specify */
1961
1946
    free(direntries);
1962
1947
    if (ret == -1){
1963
1948
      perror_plus("scandir");