186
256
fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
192
262
rc = gpgme_data_new_from_fd(&pgp_data, fd);
193
263
if(rc != GPG_ERR_NO_ERROR){
194
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
195
265
gpgme_strsource(rc), gpgme_strerror(rc));
199
269
rc = gpgme_op_import(mc.ctx, pgp_data);
200
270
if(rc != GPG_ERR_NO_ERROR){
201
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
202
272
gpgme_strsource(rc), gpgme_strerror(rc));
206
276
ret = (int)TEMP_FAILURE_RETRY(close(fd));
278
perror_plus("close");
210
280
gpgme_data_release(pgp_data);
215
fprintf(stderr, "Initializing GPGME\n");
285
fprintf_plus(stderr, "Initializing GPGME\n");
219
289
gpgme_check_version(NULL);
220
290
rc = gpgme_engine_check_version(GPGME_PROTOCOL_OpenPGP);
221
291
if(rc != GPG_ERR_NO_ERROR){
222
fprintf(stderr, "bad gpgme_engine_check_version: %s: %s\n",
292
fprintf_plus(stderr, "bad gpgme_engine_check_version: %s: %s\n",
223
293
gpgme_strsource(rc), gpgme_strerror(rc));
227
/* Set GPGME home directory for the OpenPGP engine only */
297
/* Set GPGME home directory for the OpenPGP engine only */
228
298
rc = gpgme_get_engine_info(&engine_info);
229
299
if(rc != GPG_ERR_NO_ERROR){
230
fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
300
fprintf_plus(stderr, "bad gpgme_get_engine_info: %s: %s\n",
231
301
gpgme_strsource(rc), gpgme_strerror(rc));
299
371
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
300
372
if(rc != GPG_ERR_NO_ERROR){
301
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
302
374
gpgme_strsource(rc), gpgme_strerror(rc));
303
375
plaintext_length = -1;
305
377
gpgme_decrypt_result_t result;
306
378
result = gpgme_op_decrypt_result(mc.ctx);
307
379
if(result == NULL){
308
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
310
fprintf(stderr, "Unsupported algorithm: %s\n",
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
311
383
result->unsupported_algorithm);
312
fprintf(stderr, "Wrong key usage: %u\n",
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
313
385
result->wrong_key_usage);
314
386
if(result->file_name != NULL){
315
fprintf(stderr, "File name: %s\n", result->file_name);
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
317
389
gpgme_recipient_t recipient;
318
390
recipient = result->recipients;
319
391
while(recipient != NULL){
320
fprintf(stderr, "Public key algorithm: %s\n",
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
321
393
gpgme_pubkey_algo_name(recipient->pubkey_algo));
322
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
323
fprintf(stderr, "Secret key available: %s\n",
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
324
396
recipient->status == GPG_ERR_NO_SECKEY
326
398
recipient = recipient->next;
444
511
(mc.cred, pubkeyfilename, seckeyfilename,
445
512
GNUTLS_OPENPGP_FMT_BASE64);
446
513
if(ret != GNUTLS_E_SUCCESS){
448
"Error[%d] while reading the OpenPGP key pair ('%s',"
449
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
450
fprintf(stderr, "The GnuTLS error is: %s\n",
451
safer_gnutls_strerror(ret));
515
"Error[%d] while reading the OpenPGP key pair ('%s',"
516
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
517
fprintf_plus(stderr, "The GnuTLS error is: %s\n", safer_gnutls_strerror(ret));
455
521
/* GnuTLS server initialization */
456
522
ret = gnutls_dh_params_init(&mc.dh_params);
457
523
if(ret != GNUTLS_E_SUCCESS){
458
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
459
525
" %s\n", safer_gnutls_strerror(ret));
462
528
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
463
529
if(ret != GNUTLS_E_SUCCESS){
464
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
465
531
safer_gnutls_strerror(ret));
1019
1085
signal_received = sig;
1020
1086
int old_errno = errno;
1087
/* set main loop to exit */
1021
1088
if(mc.simple_poll != NULL){
1022
1089
avahi_simple_poll_quit(mc.simple_poll);
1024
1091
errno = old_errno;
1028
* This function determines if a directory entry in /sys/class/net
1029
* corresponds to an acceptable network device.
1030
* (This function is passed to scandir(3) as a filter function.)
1032
int good_interface(const struct dirent *if_entry){
1034
char *flagname = NULL;
1035
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1041
if(if_entry->d_name[0] == '.'){
1044
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1049
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1050
/* read line from flags_fd */
1051
ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1052
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1053
flagstring[(size_t)to_read] = '\0';
1054
if(flagstring == NULL){
1060
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1077
tmpmax = strtoimax(flagstring, &tmp, 0);
1078
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1079
and not (isspace(*tmp)))
1080
or tmpmax != (ifreq_flags)tmpmax){
1094
bool get_flags(const char *ifname, struct ifreq *ifr){
1097
int s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1099
perror_plus("socket");
1102
strcpy(ifr->ifr_name, ifname);
1103
ret = ioctl(s, SIOCGIFFLAGS, ifr);
1082
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1083
flagstring, if_entry->d_name);
1106
perror_plus("ioctl SIOCGIFFLAGS");
1089
ifreq_flags flags = (ifreq_flags)tmpmax;
1113
bool good_flags(const char *ifname, const struct ifreq *ifr){
1090
1115
/* Reject the loopback device */
1091
if(flags & IFF_LOOPBACK){
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1093
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1098
1122
/* Accept point-to-point devices only if connect_to is specified */
1099
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1101
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1106
1129
/* Otherwise, reject non-broadcast-capable devices */
1107
if(not (flags & IFF_BROADCAST)){
1109
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1136
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
if(ifr->ifr_flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1114
1144
/* Accept this device */
1116
fprintf(stderr, "Interface \"%s\" is acceptable\n",
1146
fprintf_plus(stderr, "Interface \"%s\" is good\n", ifname);
1152
* This function determines if a directory entry in /sys/class/net
1153
* corresponds to an acceptable network device.
1154
* (This function is passed to scandir(3) as a filter function.)
1156
int good_interface(const struct dirent *if_entry){
1157
if(if_entry->d_name[0] == '.'){
1162
if(not get_flags(if_entry->d_name, &ifr)){
1164
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1170
if(not good_flags(if_entry->d_name, &ifr)){
1177
* This function determines if a directory entry in /sys/class/net
1178
* corresponds to an acceptable network device which is up.
1179
* (This function is passed to scandir(3) as a filter function.)
1181
int up_interface(const struct dirent *if_entry){
1182
if(if_entry->d_name[0] == '.'){
1187
if(not get_flags(if_entry->d_name, &ifr)){
1189
fprintf_plus(stderr, "Failed to get flags for interface \"%s\"\n",
1195
/* Reject down interfaces */
1196
if(not (ifr.ifr_flags & IFF_UP)){
1198
fprintf_plus(stderr, "Rejecting down interface \"%s\"\n",
1204
/* Reject non-running interfaces */
1205
if(not (ifr.ifr_flags & IFF_RUNNING)){
1207
fprintf_plus(stderr, "Rejecting non-running interface \"%s\"\n",
1213
if(not good_flags(if_entry->d_name, &ifr)){
1219
int notdotentries(const struct dirent *direntry){
1220
/* Skip "." and ".." */
1221
if(direntry->d_name[0] == '.'
1222
and (direntry->d_name[1] == '\0'
1223
or (direntry->d_name[1] == '.'
1224
and direntry->d_name[2] == '\0'))){
1230
/* Is this directory entry a runnable program? */
1231
int runnable_hook(const struct dirent *direntry){
1236
if((direntry->d_name)[0] == '\0'){
1241
sret = strspn(direntry->d_name, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1242
"abcdefghijklmnopqrstuvwxyz"
1245
if((direntry->d_name)[sret] != '\0'){
1246
/* Contains non-allowed characters */
1248
fprintf_plus(stderr, "Ignoring hook \"%s\" with bad name\n",
1254
char *fullname = NULL;
1255
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1257
perror_plus("asprintf");
1261
ret = stat(fullname, &st);
1264
perror_plus("Could not stat hook");
1268
if(not (S_ISREG(st.st_mode))){
1269
/* Not a regular file */
1271
fprintf_plus(stderr, "Ignoring hook \"%s\" - not a file\n",
1276
if(not (st.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))){
1277
/* Not executable */
1279
fprintf_plus(stderr, "Ignoring hook \"%s\" - not executable\n",
1287
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1289
struct timespec now;
1290
struct timespec waited_time;
1291
intmax_t block_time;
1294
if(mc.current_server == NULL){
1296
fprintf_plus(stderr, "Wait until first server is found. No timeout!\n");
1298
ret = avahi_simple_poll_iterate(s, -1);
1301
fprintf_plus(stderr, "Check current_server if we should run it,"
1304
/* the current time */
1305
ret = clock_gettime(CLOCK_MONOTONIC, &now);
1307
perror_plus("clock_gettime");
1310
/* Calculating in ms how long time between now and server
1311
who we visted longest time ago. Now - last seen. */
1312
waited_time.tv_sec = (now.tv_sec
1313
- mc.current_server->last_seen.tv_sec);
1314
waited_time.tv_nsec = (now.tv_nsec
1315
- mc.current_server->last_seen.tv_nsec);
1316
/* total time is 10s/10,000ms.
1317
Converting to s from ms by dividing by 1,000,
1318
and ns to ms by dividing by 1,000,000. */
1319
block_time = ((retry_interval
1320
- ((intmax_t)waited_time.tv_sec * 1000))
1321
- ((intmax_t)waited_time.tv_nsec / 1000000));
1324
fprintf_plus(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1327
if(block_time <= 0){
1328
ret = start_mandos_communication(mc.current_server->ip,
1329
mc.current_server->port,
1330
mc.current_server->if_index,
1331
mc.current_server->af);
1333
avahi_simple_poll_quit(mc.simple_poll);
1336
ret = clock_gettime(CLOCK_MONOTONIC,
1337
&mc.current_server->last_seen);
1339
perror_plus("clock_gettime");
1342
mc.current_server = mc.current_server->next;
1343
block_time = 0; /* Call avahi to find new Mandos
1344
servers, but don't block */
1347
ret = avahi_simple_poll_iterate(s, (int)block_time);
1350
if (ret > 0 or errno != EINTR){
1351
return (ret != 1) ? ret : 0;
1122
1357
int main(int argc, char *argv[]){
1552
/* Work around Debian bug #633582:
1553
<http://bugs.debian.org/633582> */
1556
/* Re-raise priviliges */
1560
perror_plus("seteuid");
1563
if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1564
int seckey_fd = open(seckey, O_RDONLY);
1565
if(seckey_fd == -1){
1566
perror_plus("open");
1568
ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1570
perror_plus("fstat");
1572
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1573
ret = fchown(seckey_fd, uid, gid);
1575
perror_plus("fchown");
1579
TEMP_FAILURE_RETRY(close(seckey_fd));
1583
if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1584
int pubkey_fd = open(pubkey, O_RDONLY);
1585
if(pubkey_fd == -1){
1586
perror_plus("open");
1588
ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1590
perror_plus("fstat");
1592
if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1593
ret = fchown(pubkey_fd, uid, gid);
1595
perror_plus("fchown");
1599
TEMP_FAILURE_RETRY(close(pubkey_fd));
1603
/* Lower privileges */
1607
perror_plus("seteuid");
1611
/* Find network hooks and run them */
1613
struct dirent **direntries;
1614
struct dirent *direntry;
1615
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1618
perror_plus("scandir");
1620
int devnull = open("/dev/null", O_RDONLY);
1621
for(int i = 0; i < numhooks; i++){
1622
direntry = direntries[0];
1623
char *fullname = NULL;
1624
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1626
perror_plus("asprintf");
1629
pid_t hook_pid = fork();
1632
dup2(devnull, STDIN_FILENO);
1634
dup2(STDERR_FILENO, STDOUT_FILENO);
1635
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1637
perror_plus("setenv");
1640
ret = setenv("DEVICE", interface, 1);
1642
perror_plus("setenv");
1645
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1647
perror_plus("setenv");
1650
ret = setenv("MODE", "start", 1);
1652
perror_plus("setenv");
1656
ret = asprintf(&delaystring, "%f", delay);
1658
perror_plus("asprintf");
1661
ret = setenv("DELAY", delaystring, 1);
1664
perror_plus("setenv");
1668
ret = execl(fullname, direntry->d_name, "start", NULL);
1669
perror_plus("execl");
1672
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1673
perror_plus("waitpid");
1677
if(WIFEXITED(status)){
1678
if(WEXITSTATUS(status) != 0){
1679
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1680
" with status %d\n", direntry->d_name,
1681
WEXITSTATUS(status));
1685
} else if(WIFSIGNALED(status)){
1686
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1687
" signal %d\n", direntry->d_name,
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" crashed\n",
1295
1708
avahi_set_log_function(empty_log);
1298
1711
if(interface[0] == '\0'){
1299
1712
struct dirent **direntries;
1300
ret = scandir(sys_class_net, &direntries, good_interface,
1713
/* First look for interfaces that are up */
1714
ret = scandir(sys_class_net, &direntries, up_interface,
1717
/* No up interfaces, look for any good interfaces */
1719
ret = scandir(sys_class_net, &direntries, good_interface,
1303
/* Pick the first good interface */
1723
/* Pick the first interface returned */
1304
1724
interface = strdup(direntries[0]->d_name);
1306
fprintf(stderr, "Using interface \"%s\"\n", interface);
1726
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1308
1728
if(interface == NULL){
1729
perror_plus("malloc");
1310
1730
free(direntries);
1311
1731
exitcode = EXIT_FAILURE;
1726
2173
ret = seteuid(0);
2175
perror_plus("seteuid");
1730
2177
if(geteuid() == 0){
1731
2178
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1733
perror("ioctl SIOCGIFFLAGS");
1734
} else if(network.ifr_flags & IFF_UP) {
2180
perror_plus("ioctl SIOCGIFFLAGS");
2181
} else if(network.ifr_flags & IFF_UP){
1735
2182
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1736
2183
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1738
perror("ioctl SIOCSIFFLAGS -IFF_UP");
2185
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1741
2188
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2190
perror_plus("close");
1745
2192
/* Lower privileges permanently */
1747
2194
ret = setuid(uid);
2196
perror_plus("setuid");
1754
/* Removes the temp directory used by GPGME */
2201
/* Removes the GPGME temp directory and all files inside */
1755
2202
if(tempdir_created){
1757
struct dirent *direntry;
1758
d = opendir(tempdir);
1760
if(errno != ENOENT){
1765
direntry = readdir(d);
1766
if(direntry == NULL){
1769
/* Skip "." and ".." */
1770
if(direntry->d_name[0] == '.'
1771
and (direntry->d_name[1] == '\0'
1772
or (direntry->d_name[1] == '.'
1773
and direntry->d_name[2] == '\0'))){
2203
struct dirent **direntries = NULL;
2204
struct dirent *direntry = NULL;
2205
int numentries = scandir(tempdir, &direntries, notdotentries,
2207
if (numentries > 0){
2208
for(int i = 0; i < numentries; i++){
2209
direntry = direntries[i];
1776
2210
char *fullname = NULL;
1777
2211
ret = asprintf(&fullname, "%s/%s", tempdir,
1778
2212
direntry->d_name);
2214
perror_plus("asprintf");
1783
2217
ret = remove(fullname);
1785
fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
2219
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname, strerror(errno));
1788
2221
free(fullname);
2225
/* need to clean even if 0 because man page doesn't specify */
2227
if (numentries == -1){
2228
perror_plus("scandir");
1792
2230
ret = rmdir(tempdir);
1793
2231
if(ret == -1 and errno != ENOENT){
2232
perror_plus("rmdir");