262
251
rc = gpgme_data_new_from_fd(&pgp_data, fd);
263
252
if(rc != GPG_ERR_NO_ERROR){
264
fprintf_plus(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
253
fprintf(stderr, "bad gpgme_data_new_from_fd: %s: %s\n",
265
254
gpgme_strsource(rc), gpgme_strerror(rc));
269
258
rc = gpgme_op_import(mc.ctx, pgp_data);
270
259
if(rc != GPG_ERR_NO_ERROR){
271
fprintf_plus(stderr, "bad gpgme_op_import: %s: %s\n",
260
fprintf(stderr, "bad gpgme_op_import: %s: %s\n",
272
261
gpgme_strsource(rc), gpgme_strerror(rc));
371
358
rc = gpgme_op_decrypt(mc.ctx, dh_crypto, dh_plain);
372
359
if(rc != GPG_ERR_NO_ERROR){
373
fprintf_plus(stderr, "bad gpgme_op_decrypt: %s: %s\n",
360
fprintf(stderr, "bad gpgme_op_decrypt: %s: %s\n",
374
361
gpgme_strsource(rc), gpgme_strerror(rc));
375
362
plaintext_length = -1;
377
364
gpgme_decrypt_result_t result;
378
365
result = gpgme_op_decrypt_result(mc.ctx);
379
366
if(result == NULL){
380
fprintf_plus(stderr, "gpgme_op_decrypt_result failed\n");
367
fprintf(stderr, "gpgme_op_decrypt_result failed\n");
382
fprintf_plus(stderr, "Unsupported algorithm: %s\n",
369
fprintf(stderr, "Unsupported algorithm: %s\n",
383
370
result->unsupported_algorithm);
384
fprintf_plus(stderr, "Wrong key usage: %u\n",
371
fprintf(stderr, "Wrong key usage: %u\n",
385
372
result->wrong_key_usage);
386
373
if(result->file_name != NULL){
387
fprintf_plus(stderr, "File name: %s\n", result->file_name);
374
fprintf(stderr, "File name: %s\n", result->file_name);
389
376
gpgme_recipient_t recipient;
390
377
recipient = result->recipients;
391
378
while(recipient != NULL){
392
fprintf_plus(stderr, "Public key algorithm: %s\n",
379
fprintf(stderr, "Public key algorithm: %s\n",
393
380
gpgme_pubkey_algo_name(recipient->pubkey_algo));
394
fprintf_plus(stderr, "Key ID: %s\n", recipient->keyid);
395
fprintf_plus(stderr, "Secret key available: %s\n",
381
fprintf(stderr, "Key ID: %s\n", recipient->keyid);
382
fprintf(stderr, "Secret key available: %s\n",
396
383
recipient->status == GPG_ERR_NO_SECKEY
398
385
recipient = recipient->next;
511
500
(mc.cred, pubkeyfilename, seckeyfilename,
512
501
GNUTLS_OPENPGP_FMT_BASE64);
513
502
if(ret != GNUTLS_E_SUCCESS){
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));
504
"Error[%d] while reading the OpenPGP key pair ('%s',"
505
" '%s')\n", ret, pubkeyfilename, seckeyfilename);
506
fprintf(stderr, "The GnuTLS error is: %s\n",
507
safer_gnutls_strerror(ret));
521
511
/* GnuTLS server initialization */
522
512
ret = gnutls_dh_params_init(&mc.dh_params);
523
513
if(ret != GNUTLS_E_SUCCESS){
524
fprintf_plus(stderr, "Error in GnuTLS DH parameter initialization:"
514
fprintf(stderr, "Error in GnuTLS DH parameter initialization:"
525
515
" %s\n", safer_gnutls_strerror(ret));
528
518
ret = gnutls_dh_params_generate2(mc.dh_params, mc.dh_bits);
529
519
if(ret != GNUTLS_E_SUCCESS){
530
fprintf_plus(stderr, "Error in GnuTLS prime generation: %s\n",
520
fprintf(stderr, "Error in GnuTLS prime generation: %s\n",
531
521
safer_gnutls_strerror(ret));
1091
1085
errno = old_errno;
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);
1089
* This function determines if a directory entry in /sys/class/net
1090
* corresponds to an acceptable network device.
1091
* (This function is passed to scandir(3) as a filter function.)
1093
int good_interface(const struct dirent *if_entry){
1095
char *flagname = NULL;
1096
if(if_entry->d_name[0] == '.'){
1099
int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1102
perror_plus("asprintf");
1105
int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1107
perror_plus("open");
1112
typedef short ifreq_flags; /* ifreq.ifr_flags in netdevice(7) */
1113
/* read line from flags_fd */
1114
ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1115
char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
flagstring[(size_t)to_read] = '\0';
1117
if(flagstring == NULL){
1118
perror_plus("malloc");
1123
ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1126
perror_plus("read");
1140
tmpmax = strtoimax(flagstring, &tmp, 0);
1141
if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1142
and not (isspace(*tmp)))
1143
or tmpmax != (ifreq_flags)tmpmax){
1106
perror_plus("ioctl SIOCGIFFLAGS");
1145
fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1146
flagstring, if_entry->d_name);
1113
bool good_flags(const char *ifname, const struct ifreq *ifr){
1152
ifreq_flags flags = (ifreq_flags)tmpmax;
1115
1153
/* Reject the loopback device */
1116
if(ifr->ifr_flags & IFF_LOOPBACK){
1154
if(flags & IFF_LOOPBACK){
1118
fprintf_plus(stderr, "Rejecting loopback interface \"%s\"\n", ifname);
1156
fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1122
1161
/* Accept point-to-point devices only if connect_to is specified */
1123
if(connect_to != NULL and (ifr->ifr_flags & IFF_POINTOPOINT)){
1162
if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1125
fprintf_plus(stderr, "Accepting point-to-point interface \"%s\"\n", ifname);
1164
fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1129
1169
/* Otherwise, reject non-broadcast-capable devices */
1130
if(not (ifr->ifr_flags & IFF_BROADCAST)){
1170
if(not (flags & IFF_BROADCAST)){
1132
fprintf_plus(stderr, "Rejecting non-broadcast interface \"%s\"\n", ifname);
1172
fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1136
1177
/* Reject non-ARP interfaces (including dummy interfaces) */
1137
if(ifr->ifr_flags & IFF_NOARP){
1178
if(flags & IFF_NOARP){
1139
fprintf_plus(stderr, "Rejecting non-ARP interface \"%s\"\n", ifname);
1180
fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1144
1185
/* Accept this device */
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)){
1187
fprintf(stderr, "Interface \"%s\" is acceptable\n",
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
1204
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1289
1206
struct timespec now;
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",
1708
1522
avahi_set_log_function(empty_log);
1711
1525
if(interface[0] == '\0'){
1712
1526
struct dirent **direntries;
1713
/* First look for interfaces that are up */
1714
ret = scandir(sys_class_net, &direntries, up_interface,
1527
ret = scandir(sys_class_net, &direntries, good_interface,
1717
/* No up interfaces, look for any good interfaces */
1719
ret = scandir(sys_class_net, &direntries, good_interface,
1723
/* Pick the first interface returned */
1530
/* Pick the first good interface */
1724
1531
interface = strdup(direntries[0]->d_name);
1726
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1533
fprintf(stderr, "Using interface \"%s\"\n", interface);
1728
1535
if(interface == NULL){
1729
1536
perror_plus("malloc");