/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: Björn Påhlsson
  • Date: 2011-10-02 19:18:24 UTC
  • mto: (237.7.53 trunk)
  • mto: This revision was merged to the branch mainline in revision 286.
  • Revision ID: belorn@fukt.bsnet.se-20111002191824-eweh4pvneeg3qzia
transitional stuff actually working
documented change to D-Bus API

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;
279
283
    return false;
280
284
  }
281
285
  
282
 
    /* Set GPGME home directory for the OpenPGP engine only */
 
286
  /* Set GPGME home directory for the OpenPGP engine only */
283
287
  rc = gpgme_get_engine_info(&engine_info);
284
288
  if(rc != GPG_ERR_NO_ERROR){
285
289
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
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
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
 
1474
      int seckey_fd = open(seckey, O_RDONLY);
 
1475
      if(seckey_fd == -1){
 
1476
        perror_plus("open");
 
1477
      } else {
 
1478
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
 
1479
        if(ret == -1){
 
1480
          perror_plus("fstat");
 
1481
        } else {
 
1482
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1483
            ret = fchown(seckey_fd, uid, gid);
 
1484
            if(ret == -1){
 
1485
              perror_plus("fchown");
 
1486
            }
 
1487
          }
 
1488
        }
 
1489
        TEMP_FAILURE_RETRY(close(seckey_fd));
 
1490
      }
 
1491
    }
 
1492
    
 
1493
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
 
1494
      int pubkey_fd = open(pubkey, O_RDONLY);
 
1495
      if(pubkey_fd == -1){
 
1496
        perror_plus("open");
 
1497
      } else {
 
1498
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
 
1499
        if(ret == -1){
 
1500
          perror_plus("fstat");
 
1501
        } else {
 
1502
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
 
1503
            ret = fchown(pubkey_fd, uid, gid);
 
1504
            if(ret == -1){
 
1505
              perror_plus("fchown");
 
1506
            }
 
1507
          }
 
1508
        }
 
1509
        TEMP_FAILURE_RETRY(close(pubkey_fd));
 
1510
      }
 
1511
    }
 
1512
    
 
1513
    /* Lower privileges */
 
1514
    errno = 0;
 
1515
    ret = seteuid(uid);
 
1516
    if(ret == -1){
 
1517
      perror_plus("seteuid");
 
1518
    }
 
1519
  }
1447
1520
  
1448
1521
  if(not debug){
1449
1522
    avahi_set_log_function(empty_log);
1645
1718
        goto end;
1646
1719
      }
1647
1720
    }
1648
 
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
 
1721
    /* Sleep checking until interface is running.
 
1722
       Check every 0.25s, up to total time of delay */
1649
1723
    for(int i=0; i < delay * 4; i++){
1650
1724
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1651
1725
      if(ret == -1){
1761
1835
    
1762
1836
    port = (uint16_t)tmpmax;
1763
1837
    *address = '\0';
1764
 
    address = connect_to;
1765
1838
    /* Colon in address indicates IPv6 */
1766
1839
    int af;
1767
 
    if(strchr(address, ':') != NULL){
 
1840
    if(strchr(connect_to, ':') != NULL){
1768
1841
      af = AF_INET6;
 
1842
      /* Accept [] around IPv6 address - see RFC 5952 */
 
1843
      if(connect_to[0] == '[' and address[-1] == ']')
 
1844
        {
 
1845
          connect_to++;
 
1846
          address[-1] = '\0';
 
1847
        }
1769
1848
    } else {
1770
1849
      af = AF_INET;
1771
1850
    }
 
1851
    address = connect_to;
1772
1852
    
1773
1853
    if(quit_now){
1774
1854
      goto end;
1775
1855
    }
1776
 
 
 
1856
    
1777
1857
    while(not quit_now){
1778
1858
      ret = start_mandos_communication(address, port, if_index, af);
1779
1859
      if(quit_now or ret == 0){
1780
1860
        break;
1781
1861
      }
1782
 
      sleep((int)retry_interval or 1);
1783
 
    };
1784
 
 
 
1862
      if(debug){
 
1863
        fprintf(stderr, "Retrying in %d seconds\n",
 
1864
                (int)retry_interval);
 
1865
      }
 
1866
      sleep((int)retry_interval);
 
1867
    }
 
1868
    
1785
1869
    if (not quit_now){
1786
1870
      exitcode = EXIT_SUCCESS;
1787
1871
    }
1844
1928
    fprintf(stderr, "Starting Avahi loop search\n");
1845
1929
  }
1846
1930
 
1847
 
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
 
1931
  ret = avahi_loop_with_timeout(mc.simple_poll,
 
1932
                                (int)(retry_interval * 1000));
1848
1933
  if(debug){
1849
1934
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
1935
            (ret == 0) ? "successfully" : "with error");
1876
1961
    gpgme_release(mc.ctx);
1877
1962
  }
1878
1963
 
1879
 
  /* cleans up the circular linked list of mandos servers the client has seen */
 
1964
  /* Cleans up the circular linked list of Mandos servers the client
 
1965
     has seen */
1880
1966
  if(mc.current_server != NULL){
1881
1967
    mc.current_server->prev->next = NULL;
1882
1968
    while(mc.current_server != NULL){
1922
2008
  if(tempdir_created){
1923
2009
    struct dirent **direntries = NULL;
1924
2010
    struct dirent *direntry = NULL;
1925
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1926
 
    if (ret > 0){
1927
 
      for(int i = 0; i < ret; i++){
 
2011
    int numentries = scandir(tempdir, &direntries, notdotentries,
 
2012
                             alphasort);
 
2013
    if (numentries > 0){
 
2014
      for(int i = 0; i < numentries; i++){
1928
2015
        direntry = direntries[i];
1929
2016
        char *fullname = NULL;
1930
2017
        ret = asprintf(&fullname, "%s/%s", tempdir,
1942
2029
      }
1943
2030
    }
1944
2031
 
1945
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
 
2032
    /* need to clean even if 0 because man page doesn't specify */
1946
2033
    free(direntries);
1947
 
    if (ret == -1){
 
2034
    if (numentries == -1){
1948
2035
      perror_plus("scandir");
1949
2036
    }
1950
2037
    ret = rmdir(tempdir);