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() */
60
59
#include <fcntl.h> /* open() */
61
60
#include <dirent.h> /* opendir(), struct dirent, readdir()
74
73
#include <unistd.h> /* close(), SEEK_SET, off_t, write(),
75
74
getuid(), getgid(), seteuid(),
76
75
setgid(), pause(), _exit() */
77
#include <arpa/inet.h> /* inet_pton(), htons() */
76
#include <arpa/inet.h> /* inet_pton(), htons, inet_ntop() */
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,
92
91
argz_delete(), argz_append(),
93
92
argz_stringify(), argz_add(),
95
#include <netdb.h> /* getnameinfo(), NI_NUMERICHOST,
96
EAI_SYSTEM, gai_strerror() */
99
96
#include <sys/klog.h> /* klogctl() */
201
198
size_t incbuffer(char **buffer, size_t buffer_length,
202
199
size_t buffer_capacity){
203
200
if(buffer_length + BUFFER_SIZE > buffer_capacity){
204
char *new_buf = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
206
int old_errno = errno;
201
*buffer = realloc(*buffer, buffer_capacity + BUFFER_SIZE);
213
205
buffer_capacity += BUFFER_SIZE;
215
207
return buffer_capacity;
232
224
perror_plus("strdup");
235
ret = clock_gettime(CLOCK_MONOTONIC, &(new_server->last_seen));
237
perror_plus("clock_gettime");
240
227
/* Special case of first server */
241
228
if(*current_server == NULL){
242
229
new_server->next = new_server;
249
236
new_server->prev->next = new_server;
250
237
(*current_server)->prev = new_server;
239
ret = clock_gettime(CLOCK_MONOTONIC, &(*current_server)->last_seen);
241
perror_plus("clock_gettime");
634
626
int af, mandos_context *mc){
635
627
int ret, tcp_sd = -1;
637
struct sockaddr_storage to;
630
struct sockaddr_in in;
631
struct sockaddr_in6 in6;
638
633
char *buffer = NULL;
639
634
char *decrypted_buffer = NULL;
640
635
size_t buffer_length = 0;
722
717
memset(&to, 0, sizeof(to));
723
718
if(af == AF_INET6){
724
((struct sockaddr_in6 *)&to)->sin6_family = (sa_family_t)af;
725
ret = inet_pton(af, ip, &((struct sockaddr_in6 *)&to)->sin6_addr);
719
to.in6.sin6_family = (sa_family_t)af;
720
ret = inet_pton(af, ip, &to.in6.sin6_addr);
726
721
} else { /* IPv4 */
727
((struct sockaddr_in *)&to)->sin_family = (sa_family_t)af;
728
ret = inet_pton(af, ip, &((struct sockaddr_in *)&to)->sin_addr);
722
to.in.sin_family = (sa_family_t)af;
723
ret = inet_pton(af, ip, &to.in.sin_addr);
742
737
if(af == AF_INET6){
743
((struct sockaddr_in6 *)&to)->sin6_port = htons(port);
744
if(IN6_IS_ADDR_LINKLOCAL
745
(&((struct sockaddr_in6 *)&to)->sin6_addr)){
738
to.in6.sin6_port = htons(port);
740
#pragma GCC diagnostic push
741
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
743
if(IN6_IS_ADDR_LINKLOCAL /* Spurious warnings from */
744
(&to.in6.sin6_addr)){ /* -Wstrict-aliasing=2 or lower */
746
#pragma GCC diagnostic pop
746
748
if(if_index == AVAHI_IF_UNSPEC){
747
749
fprintf_plus(stderr, "An IPv6 link-local address is"
748
750
" incomplete without a network interface\n");
752
754
/* Set the network interface number as scope */
753
((struct sockaddr_in6 *)&to)->sin6_scope_id = (uint32_t)if_index;
755
to.in6.sin6_scope_id = (uint32_t)if_index;
756
((struct sockaddr_in *)&to)->sin_port = htons(port);
758
to.in.sin_port = htons(port);
777
779
char addrstr[(INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ?
778
780
INET_ADDRSTRLEN : INET6_ADDRSTRLEN] = "";
779
782
if(af == AF_INET6){
780
ret = getnameinfo((struct sockaddr *)&to,
781
sizeof(struct sockaddr_in6),
782
addrstr, sizeof(addrstr), NULL, 0,
783
pcret = inet_ntop(af, &(to.in6.sin6_addr), addrstr,
785
ret = getnameinfo((struct sockaddr *)&to,
786
sizeof(struct sockaddr_in),
787
addrstr, sizeof(addrstr), NULL, 0,
786
pcret = inet_ntop(af, &(to.in.sin_addr), 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);
790
perror_plus("inet_ntop");
792
if(strcmp(addrstr, ip) != 0){
793
fprintf_plus(stderr, "Canonical address form: %s\n", addrstr);
804
803
if(af == AF_INET6){
805
ret = connect(tcp_sd, (struct sockaddr *)&to,
806
sizeof(struct sockaddr_in6));
804
ret = connect(tcp_sd, &to.in6, sizeof(to));
808
ret = connect(tcp_sd, (struct sockaddr *)&to, /* IPv4 */
809
sizeof(struct sockaddr_in));
806
ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
812
809
if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
1491
1488
bool run_network_hooks(const char *mode, const char *interface,
1492
1489
const float delay){
1493
1490
struct dirent **direntries;
1491
struct dirent *direntry;
1494
1493
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1496
1495
if(numhooks == -1){
1749
1748
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1751
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1750
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1753
1752
ret_errno = errno;
1754
1753
perror_plus("socket");
1764
1763
/* Raise priviliges */
1765
1764
raise_privileges();
1767
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1766
ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1768
1767
ret_errno = errno;
1770
1769
/* Lower privileges */
1771
1770
lower_privileges();
1773
1772
/* Close the socket */
1774
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1773
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1776
1775
perror_plus("close");
1810
1809
const char *seckey = PATHDIR "/" SECKEY;
1811
1810
const char *pubkey = PATHDIR "/" PUBKEY;
1812
1811
char *interfaces_hooks = NULL;
1812
size_t interfaces_hooks_size = 0;
1814
1814
bool gnutls_initialized = false;
1815
1815
bool gpgme_initialized = false;
2072
2072
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2073
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2073
interfaces_hooks_size = mc.interfaces_size;
2074
argz_stringify(interfaces_hooks, interfaces_hooks_size,
2075
2077
if(not run_network_hooks("start", interfaces_hooks != NULL ?
2076
2078
interfaces_hooks : "", delay)){