56
56
#include <sys/stat.h> /* open(), S_ISREG */
57
57
#include <sys/socket.h> /* socket(), struct sockaddr_in6,
58
inet_pton(), connect() */
58
inet_pton(), connect(),
59
60
#include <fcntl.h> /* open() */
60
61
#include <dirent.h> /* opendir(), struct dirent, readdir()
73
74
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
74
75
getuid(), getgid(), seteuid(),
75
76
setgid(), pause(), _exit() */
76
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
77
#include <arpa/inet.h> /* inet_pton(), htons() */
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,
91
92
argz_delete(), argz_append(),
92
93
argz_stringify(), argz_add(),
95
#include <netdb.h> /* getnameinfo(), NI_NUMERICHOST,
96
EAI_SYSTEM, gai_strerror() */
96
99
#include <sys/klog.h> /* klogctl() */
198
201
size_t incbuffer(char **buffer, size_t buffer_length,
199
202
size_t buffer_capacity){
200
203
if(buffer_length + BUFFER_SIZE > buffer_capacity){
201
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
204
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
206
int old_errno = errno;
205
213
buffer_capacity += BUFFER_SIZE;
207
215
return buffer_capacity;
224
232
perror_plus("strdup");
235
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
237
perror_plus("clock_gettime");
227
240
/* Special case of first server */
228
241
if(*current_server == NULL){
229
242
new_server->next = new_server;
236
249
new_server->prev->next = new_server;
237
250
(*current_server)->prev = new_server;
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
241
perror_plus("clock_gettime");
626
634
int af, mandos_context *mc){
627
635
int ret, tcp_sd = -1;
630
struct sockaddr_in in;
631
struct sockaddr_in6 in6;
637
struct sockaddr_storage to;
633
638
char *buffer = NULL;
634
639
char *decrypted_buffer = NULL;
635
640
size_t buffer_length = 0;
717
722
memset(&to, 0, sizeof(to));
718
723
if(af == AF_INET6){
719
to.in6.sin6_family = (sa_family_t)af;
720
ret = inet_pton(af, ip, &to.in6.sin6_addr);
724
((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
725
ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
721
726
} else { /* IPv4 */
722
to.in.sin_family = (sa_family_t)af;
723
ret = inet_pton(af, ip, &to.in.sin_addr);
727
((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
728
ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
737
742
if(af == AF_INET6){
738
to.in6.sin6_port = htons(port);
739
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
740
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower and
743
((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
744
if(IN6_IS_ADDR_LINKLOCAL
745
(&((struct sockaddr_in6 *)&to)->sin6_addr)){
742
746
if(if_index == AVAHI_IF_UNSPEC){
743
747
fprintf_plus(stderr, "An IPv6 link-local address is"
744
748
" incomplete without a network interface\n");
748
752
/* Set the network interface number as scope */
749
to.in6.sin6_scope_id = (uint32_t)if_index;
753
((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
752
to.in.sin_port = htons(port); /* Spurious warnings from
754
-Wunreachable-code */
756
((struct sockaddr_in *)&to)->sin_port = htons(port);
775
777
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
776
778
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
778
779
if(af == AF_INET6){
779
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
780
ret = getnameinfo((struct sockaddr *)&to,
781
sizeof(struct sockaddr_in6),
782
addrstr, sizeof(addrstr), NULL, 0,
782
pcret = inet_ntop(af, &(to.in.sin_addr), addrstr,
785
ret = getnameinfo((struct sockaddr *)&to,
786
sizeof(struct sockaddr_in),
787
addrstr, sizeof(addrstr), NULL, 0,
786
perror_plus("inet_ntop");
788
if(strcmp(addrstr, ip) != 0){
789
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
790
if(ret == EAI_SYSTEM){
791
perror_plus("getnameinfo");
792
} else if(ret != 0) {
793
fprintf_plus(stderr, "getnameinfo: %s", gai_strerror(ret));
794
} else if(strcmp(addrstr, ip) != 0){
795
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
799
804
if(af == AF_INET6){
800
ret = connect(tcp_sd, &to.in6, sizeof(to));
805
ret = connect(tcp_sd, (struct sockaddr *)&to,
806
sizeof(struct sockaddr_in6));
802
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
808
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
809
sizeof(struct sockaddr_in));
805
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
812
if((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
807
814
perror_plus("connect");
1367
1374
if(mc->current_server == NULL){
1369
1376
fprintf_plus(stderr, "Wait until first server is found."
1370
1377
" No timeout!\n");
1372
1379
ret = avahi_simple_poll_iterate(s, -1);
1375
1382
fprintf_plus(stderr, "Check current_server if we should run"
1376
1383
" it, or wait\n");
1422
1429
ret = avahi_simple_poll_iterate(s, (int)block_time);
1425
if (ret > 0 or errno != EINTR){
1432
if(ret > 0 or errno != EINTR){
1426
1433
return (ret != 1) ? ret : 0;
1484
1491
bool run_network_hooks(const char *mode, const char *interface,
1485
1492
const float delay){
1486
1493
struct dirent **direntries;
1487
struct dirent *direntry;
1489
1494
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1491
1496
if(numhooks == -1){
1744
1749
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1746
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1751
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1748
1753
ret_errno = errno;
1749
1754
perror_plus("socket");
1759
1764
/* Raise priviliges */
1760
1765
raise_privileges();
1762
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1767
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1763
1768
ret_errno = errno;
1765
1770
/* Lower privileges */
1766
1771
lower_privileges();
1768
1773
/* Close the socket */
1769
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1774
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1771
1776
perror_plus("close");
1805
1810
const char *seckey = PATHDIR "/" SECKEY;
1806
1811
const char *pubkey = PATHDIR "/" PUBKEY;
1807
1812
char *interfaces_hooks = NULL;
1808
size_t interfaces_hooks_size = 0;
1810
1814
bool gnutls_initialized = false;
1811
1815
bool gpgme_initialized = false;
2068
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2069
interfaces_hooks_size = mc.interfaces_size;
2070
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2073
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2073
2075
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2074
2076
interfaces_hooks : "", delay)){
2480
2487
struct dirent *direntry = NULL;
2481
2488
int numentries = scandir(tempdir, &direntries, notdotentries,
2483
if (numentries > 0){
2484
2491
for(int i = 0; i < numentries; i++){
2485
2492
direntry = direntries[i];
2486
2493
char *fullname = NULL;