/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

* plugins.d/mandos-client.c (avahi_loop_with_timeout): Fix warning.
  (main): Disallow "--retry" arguments < 0.  Allow brackets [] around
          IPv6 addresses, as recommended by RFC 5952.  Bug fix: When
          using --connect, really use retry_interval, not 1 second.
* plugins.d/mandos-client.xml (DESCRIPTION): Add retry info.
  (--retry): Remove repeated word.

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, program_invocation_short_name */
 
65
#include <errno.h>              /* perror(), errno,
 
66
                                   program_invocation_short_name */
66
67
#include <time.h>               /* nanosleep(), time(), sleep() */
67
68
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
69
                                   SIOCSIFFLAGS, if_indextoname(),
73
74
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
75
                                   getuid(), getgid(), seteuid(),
75
76
                                   setgid(), pause() */
76
 
#include <arpa/inet.h>          /* inet_pton(), htons */
 
77
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
77
78
#include <iso646.h>             /* not, or, and */
78
79
#include <argp.h>               /* struct argp_option, error_t, struct
79
80
                                   argp_state, struct argp,
130
131
static const char sys_class_net[] = "/sys/class/net";
131
132
char *connect_to = NULL;
132
133
 
133
 
/* Doubly linked list that need to be circular linked when ever used */
 
134
/* Doubly linked list that need to be circularly linked when used */
134
135
typedef struct server{
135
136
  const char *ip;
136
137
  uint16_t port;
156
157
/* global context so signal handler can reach it*/
157
158
mandos_context mc = { .simple_poll = NULL, .server = NULL,
158
159
                      .dh_bits = 1024, .priority = "SECURE256"
159
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
 
160
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
 
161
                      .current_server = NULL };
160
162
 
161
163
sig_atomic_t quit_now = 0;
162
164
int signal_received = 0;
163
165
 
164
166
/* Function to use when printing errors */
165
167
void perror_plus(const char *print_text){
166
 
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
 
168
  fprintf(stderr, "Mandos plugin %s: ",
 
169
          program_invocation_short_name);
167
170
  perror(print_text);
168
171
}
169
172
 
184
187
  return buffer_capacity;
185
188
}
186
189
 
 
190
/* Add server to set of servers to retry periodically */
187
191
int add_server(const char *ip, uint16_t port,
188
192
                 AvahiIfIndex if_index,
189
193
                 int af){
199
203
                         .af = af };
200
204
  if(new_server->ip == NULL){
201
205
    perror_plus("strdup");
202
 
    return -1;    
 
206
    return -1;
203
207
  }
204
 
  /* uniqe case of first server */
 
208
  /* Special case of first server */
205
209
  if (mc.current_server == NULL){
206
210
    new_server->next = new_server;
207
211
    new_server->prev = new_server;
208
212
    mc.current_server = new_server;
209
 
  /* Placing the new server last in the list */
 
213
  /* Place the new server last in the list */
210
214
  } else {
211
215
    new_server->next = mc.current_server;
212
216
    new_server->prev = mc.current_server->prev;
478
482
  }
479
483
  
480
484
  /* OpenPGP credentials */
481
 
  gnutls_certificate_allocate_credentials(&mc.cred);
 
485
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
482
486
  if(ret != GNUTLS_E_SUCCESS){
483
 
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
484
 
                                                    from
485
 
                                                    -Wunreachable-code
486
 
                                                 */
 
487
    fprintf(stderr, "GnuTLS memory error: %s\n",
487
488
            safer_gnutls_strerror(ret));
488
489
    gnutls_global_deinit();
489
490
    return -1;
796
797
    errno = EINTR;
797
798
    goto mandos_end;
798
799
  }
799
 
 
800
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
800
  
 
801
  /* Spurious warning from -Wint-to-pointer-cast */
801
802
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
803
  
803
804
  if(quit_now){
1069
1070
  }
1070
1071
}
1071
1072
 
1072
 
/* Signal handler that stops main loop after sigterm has been called */
 
1073
/* Signal handler that stops main loop after SIGTERM */
1073
1074
static void handle_sigterm(int sig){
1074
1075
  if(quit_now){
1075
1076
    return;
1110
1111
  free(flagname);
1111
1112
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1113
  /* read line from flags_fd */
1113
 
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
 
1114
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
1115
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1116
  flagstring[(size_t)to_read] = '\0';
1116
1117
  if(flagstring == NULL){
1205
1206
  struct timespec now;
1206
1207
  struct timespec waited_time;
1207
1208
  intmax_t block_time;
1208
 
 
 
1209
  
1209
1210
  while(true){
1210
1211
    if(mc.current_server == NULL){
1211
1212
      if (debug){
1212
 
        fprintf(stderr, "Wait until first server is found. No timeout!\n");     
 
1213
        fprintf(stderr,
 
1214
                "Wait until first server is found. No timeout!\n");
1213
1215
      }
1214
1216
      ret = avahi_simple_poll_iterate(s, -1);
1215
1217
    } else {
1216
1218
      if (debug){
1217
 
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
 
1219
        fprintf(stderr, "Check current_server if we should run it,"
 
1220
                " or wait\n");
1218
1221
      }
1219
1222
      /* the current time */
1220
1223
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1224
1227
      }
1225
1228
      /* Calculating in ms how long time between now and server
1226
1229
         who we visted longest time ago. Now - last seen.  */
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);
1231
 
 
 
1230
      waited_time.tv_sec = (now.tv_sec
 
1231
                            - mc.current_server->last_seen.tv_sec);
 
1232
      waited_time.tv_nsec = (now.tv_nsec
 
1233
                             - mc.current_server->last_seen.tv_nsec);
 
1234
      /* total time is 10s/10,000ms.
 
1235
         Converting to s from ms by dividing by 1,000,
 
1236
         and ns to ms by dividing by 1,000,000. */
 
1237
      block_time = ((retry_interval
 
1238
                     - ((intmax_t)waited_time.tv_sec * 1000))
 
1239
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
 
1240
      
1232
1241
      if (debug){
1233
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
 
1242
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1234
1243
      }
1235
 
 
 
1244
      
1236
1245
      if(block_time <= 0){
1237
1246
        ret = start_mandos_communication(mc.current_server->ip,
1238
 
                                   mc.current_server->port,
1239
 
                                   mc.current_server->if_index,
1240
 
                                   mc.current_server->af);
 
1247
                                         mc.current_server->port,
 
1248
                                         mc.current_server->if_index,
 
1249
                                         mc.current_server->af);
1241
1250
        if(ret == 0){
1242
1251
          avahi_simple_poll_quit(mc.simple_poll);
1243
1252
          return 0;
1244
1253
        }
1245
 
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
 
1254
        ret = clock_gettime(CLOCK_MONOTONIC,
 
1255
                            &mc.current_server->last_seen);
1246
1256
        if(ret == -1){
1247
1257
          perror_plus("clock_gettime");
1248
1258
          return -1;
1249
1259
        }
1250
1260
        mc.current_server = mc.current_server->next;
1251
 
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
 
1261
        block_time = 0;         /* Call avahi to find new Mandos
 
1262
                                   servers, but don't block */
1252
1263
      }
1253
1264
      
1254
1265
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1283
1294
  bool gnutls_initialized = false;
1284
1295
  bool gpgme_initialized = false;
1285
1296
  float delay = 2.5f;
1286
 
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
 
1297
  double retry_interval = 10; /* 10s between trying a server and
 
1298
                                 retrying the same server again */
1287
1299
  
1288
1300
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1289
1301
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1400
1412
        errno = 0;
1401
1413
        retry_interval = strtod(arg, &tmp);
1402
1414
        if(errno != 0 or tmp == arg or *tmp != '\0'
1403
 
           or (retry_interval * 1000) > INT_MAX){
 
1415
           or (retry_interval * 1000) > INT_MAX
 
1416
           or retry_interval < 0){
1404
1417
          argp_error(state, "Bad retry interval");
1405
1418
        }
1406
1419
        break;
1444
1457
      goto end;
1445
1458
    }
1446
1459
  }
 
1460
    
 
1461
  {
 
1462
    /* Work around Debian bug #633582:
 
1463
       <http://bugs.debian.org/633582> */
 
1464
    struct stat st;
 
1465
    
 
1466
    /* Re-raise priviliges */
 
1467
    errno = 0;
 
1468
    ret = seteuid(0);
 
1469
    if(ret == -1){
 
1470
      perror_plus("seteuid");
 
1471
    }
 
1472
    
 
1473
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
 
1474
    if(seckey_fd == -1){
 
1475
      perror_plus("open");
 
1476
    } else {
 
1477
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1478
      if(ret == -1){
 
1479
        perror_plus("fstat");
 
1480
      } else {
 
1481
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1482
          ret = fchown(seckey_fd, uid, gid);
 
1483
          if(ret == -1){
 
1484
            perror_plus("fchown");
 
1485
          }
 
1486
        }
 
1487
      }
 
1488
      TEMP_FAILURE_RETRY(close(seckey_fd));
 
1489
    }
 
1490
    
 
1491
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
 
1492
    if(pubkey_fd == -1){
 
1493
      perror_plus("open");
 
1494
    } else {
 
1495
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1496
      if(ret == -1){
 
1497
        perror_plus("fstat");
 
1498
      } else {
 
1499
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1500
          ret = fchown(pubkey_fd, uid, gid);
 
1501
          if(ret == -1){
 
1502
            perror_plus("fchown");
 
1503
          }
 
1504
        }
 
1505
      }
 
1506
      TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1507
    }
 
1508
    
 
1509
    /* Lower privileges */
 
1510
    errno = 0;
 
1511
    ret = seteuid(uid);
 
1512
    if(ret == -1){
 
1513
      perror_plus("seteuid");
 
1514
    }
 
1515
  }
1447
1516
  
1448
1517
  if(not debug){
1449
1518
    avahi_set_log_function(empty_log);
1645
1714
        goto end;
1646
1715
      }
1647
1716
    }
1648
 
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
 
1717
    /* Sleep checking until interface is running.
 
1718
       Check every 0.25s, up to total time of delay */
1649
1719
    for(int i=0; i < delay * 4; i++){
1650
1720
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1651
1721
      if(ret == -1){
1761
1831
    
1762
1832
    port = (uint16_t)tmpmax;
1763
1833
    *address = '\0';
1764
 
    address = connect_to;
1765
1834
    /* Colon in address indicates IPv6 */
1766
1835
    int af;
1767
 
    if(strchr(address, ':') != NULL){
 
1836
    if(strchr(connect_to, ':') != NULL){
1768
1837
      af = AF_INET6;
 
1838
      /* Accept [] around IPv6 address - see RFC 5952 */
 
1839
      if(connect_to[0] == '[' and address[-1] == ']')
 
1840
        {
 
1841
          connect_to++;
 
1842
          address[-1] = '\0';
 
1843
        }
1769
1844
    } else {
1770
1845
      af = AF_INET;
1771
1846
    }
 
1847
    address = connect_to;
1772
1848
    
1773
1849
    if(quit_now){
1774
1850
      goto end;
1775
1851
    }
1776
 
 
 
1852
    
1777
1853
    while(not quit_now){
1778
1854
      ret = start_mandos_communication(address, port, if_index, af);
1779
1855
      if(quit_now or ret == 0){
1780
1856
        break;
1781
1857
      }
1782
 
      sleep((int)retry_interval or 1);
 
1858
      if(debug){
 
1859
        fprintf(stderr, "Retrying in %d seconds\n",
 
1860
                (int)retry_interval);
 
1861
      }
 
1862
      sleep((int)retry_interval);
1783
1863
    };
1784
1864
 
1785
1865
    if (not quit_now){
1844
1924
    fprintf(stderr, "Starting Avahi loop search\n");
1845
1925
  }
1846
1926
 
1847
 
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
 
1927
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
1928
                                (int)(retry_interval * 1000));
1848
1929
  if(debug){
1849
1930
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
1931
            (ret == 0) ? "successfully" : "with error");
1876
1957
    gpgme_release(mc.ctx);
1877
1958
  }
1878
1959
 
1879
 
  /* cleans up the circular linked list of mandos servers the client has seen */
 
1960
  /* Cleans up the circular linked list of Mandos servers the client
 
1961
     has seen */
1880
1962
  if(mc.current_server != NULL){
1881
1963
    mc.current_server->prev->next = NULL;
1882
1964
    while(mc.current_server != NULL){
1942
2024
      }
1943
2025
    }
1944
2026
 
1945
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
 
2027
    /* need to be cleaned even if ret == 0 because man page doesn't
 
2028
       specify */
1946
2029
    free(direntries);
1947
2030
    if (ret == -1){
1948
2031
      perror_plus("scandir");