/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugins.d/mandos-client.c

  • Committer: Teddy Hogeborn
  • Date: 2009-10-26 21:16:16 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091026211616-38e9wynf4428ip2l
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * "browse_callback", and parts of "main".
10
10
 * 
11
11
 * Everything else is
12
 
 * Copyright © 2008-2011 Teddy Hogeborn
13
 
 * Copyright © 2008-2011 Björn Påhlsson
 
12
 * Copyright © 2008,2009 Teddy Hogeborn
 
13
 * Copyright © 2008,2009 Björn Påhlsson
14
14
 * 
15
15
 * This program is free software: you can redistribute it and/or
16
16
 * modify it under the terms of the GNU General Public License as
43
43
                                   stdout, ferror(), remove() */
44
44
#include <stdint.h>             /* uint16_t, uint32_t */
45
45
#include <stddef.h>             /* NULL, size_t, ssize_t */
46
 
#include <stdlib.h>             /* free(), EXIT_SUCCESS, srand(),
47
 
                                   strtof(), abort() */
 
46
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
 
47
                                   srand(), strtof(), abort() */
48
48
#include <stdbool.h>            /* bool, false, true */
49
49
#include <string.h>             /* memset(), strcmp(), strlen(),
50
50
                                   strerror(), asprintf(), strcpy() */
62
62
#include <inttypes.h>           /* PRIu16, PRIdMAX, intmax_t,
63
63
                                   strtoimax() */
64
64
#include <assert.h>             /* assert() */
65
 
#include <errno.h>              /* perror(), errno,
66
 
                                   program_invocation_short_name */
67
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
68
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
69
68
                                   SIOCSIFFLAGS, if_indextoname(),
70
69
                                   if_nametoindex(), IF_NAMESIZE */
74
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
75
74
                                   getuid(), getgid(), seteuid(),
76
75
                                   setgid(), pause() */
77
 
#include <arpa/inet.h>          /* inet_pton(), htons, inet_ntop() */
 
76
#include <arpa/inet.h>          /* inet_pton(), htons */
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,
83
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
84
83
                                   sigaction(), SIGTERM, sig_atomic_t,
85
84
                                   raise() */
86
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
87
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
 
85
#include <sysexits.h>           /* EX_OSERR, EX_USAGE */
88
86
 
89
87
#ifdef __linux__
90
88
#include <sys/klog.h>           /* klogctl() */
128
126
static const char mandos_protocol_version[] = "1";
129
127
const char *argp_program_version = "mandos-client " VERSION;
130
128
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
131
 
static const char sys_class_net[] = "/sys/class/net";
132
 
char *connect_to = NULL;
133
 
 
134
 
/* Doubly linked list that need to be circularly linked when used */
135
 
typedef struct server{
136
 
  const char *ip;
137
 
  uint16_t port;
138
 
  AvahiIfIndex if_index;
139
 
  int af;
140
 
  struct timespec last_seen;
141
 
  struct server *next;
142
 
  struct server *prev;
143
 
} server;
144
129
 
145
130
/* Used for passing in values through the Avahi callback functions */
146
131
typedef struct {
151
136
  gnutls_dh_params_t dh_params;
152
137
  const char *priority;
153
138
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
139
} mandos_context;
156
140
 
157
141
/* global context so signal handler can reach it*/
158
142
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
143
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
144
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
145
 
163
146
sig_atomic_t quit_now = 0;
164
147
int signal_received = 0;
165
148
 
166
 
/* Function to use when printing errors */
167
 
void perror_plus(const char *print_text){
168
 
  fprintf(stderr, "Mandos plugin %s: ",
169
 
          program_invocation_short_name);
170
 
  perror(print_text);
171
 
}
172
 
 
173
149
/*
174
150
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
151
 * bytes. "buffer_capacity" is how much is currently allocated,
187
163
  return buffer_capacity;
188
164
}
189
165
 
190
 
int add_server(const char *ip, uint16_t port,
191
 
                 AvahiIfIndex if_index,
192
 
                 int af){
193
 
  int ret;
194
 
  server *new_server = malloc(sizeof(server));
195
 
  if(new_server == NULL){
196
 
    perror_plus("malloc");
197
 
    return -1;
198
 
  }
199
 
  *new_server = (server){ .ip = strdup(ip),
200
 
                         .port = port,
201
 
                         .if_index = if_index,
202
 
                         .af = af };
203
 
  if(new_server->ip == NULL){
204
 
    perror_plus("strdup");
205
 
    return -1;
206
 
  }
207
 
  /* unique case of first server */
208
 
  if (mc.current_server == NULL){
209
 
    new_server->next = new_server;
210
 
    new_server->prev = new_server;
211
 
    mc.current_server = new_server;
212
 
  /* Placing the new server last in the list */
213
 
  } else {
214
 
    new_server->next = mc.current_server;
215
 
    new_server->prev = mc.current_server->prev;
216
 
    new_server->prev->next = new_server;
217
 
    mc.current_server->prev = new_server;
218
 
  }
219
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
220
 
  if(ret == -1){
221
 
    perror_plus("clock_gettime");
222
 
    return -1;
223
 
  }
224
 
  return 0;
225
 
}
226
 
 
227
166
/* 
228
167
 * Initialize GPGME.
229
168
 */
243
182
    
244
183
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
245
184
    if(fd == -1){
246
 
      perror_plus("open");
 
185
      perror("open");
247
186
      return false;
248
187
    }
249
188
    
263
202
    
264
203
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
265
204
    if(ret == -1){
266
 
      perror_plus("close");
 
205
      perror("close");
267
206
    }
268
207
    gpgme_data_release(pgp_data);
269
208
    return true;
394
333
  
395
334
  /* Seek back to the beginning of the GPGME plaintext data buffer */
396
335
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
397
 
    perror_plus("gpgme_data_seek");
 
336
    perror("gpgme_data_seek");
398
337
    plaintext_length = -1;
399
338
    goto decrypt_end;
400
339
  }
405
344
                                      (size_t)plaintext_length,
406
345
                                      plaintext_capacity);
407
346
    if(plaintext_capacity == 0){
408
 
        perror_plus("incbuffer");
 
347
        perror("incbuffer");
409
348
        plaintext_length = -1;
410
349
        goto decrypt_end;
411
350
    }
418
357
      break;
419
358
    }
420
359
    if(ret < 0){
421
 
      perror_plus("gpgme_data_read");
 
360
      perror("gpgme_data_read");
422
361
      plaintext_length = -1;
423
362
      goto decrypt_end;
424
363
    }
481
420
  }
482
421
  
483
422
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
423
  gnutls_certificate_allocate_credentials(&mc.cred);
485
424
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
425
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
426
                                                    from
 
427
                                                    -Wunreachable-code
 
428
                                                 */
487
429
            safer_gnutls_strerror(ret));
488
430
    gnutls_global_deinit();
489
431
    return -1;
611
553
  gnutls_session_t session;
612
554
  int pf;                       /* Protocol family */
613
555
  
614
 
  errno = 0;
615
 
  
616
556
  if(quit_now){
617
 
    errno = EINTR;
618
557
    return -1;
619
558
  }
620
559
  
627
566
    break;
628
567
  default:
629
568
    fprintf(stderr, "Bad address family: %d\n", af);
630
 
    errno = EINVAL;
631
569
    return -1;
632
570
  }
633
571
  
643
581
  
644
582
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
583
  if(tcp_sd < 0){
646
 
    int e = errno;
647
 
    perror_plus("socket");
648
 
    errno = e;
 
584
    perror("socket");
649
585
    goto mandos_end;
650
586
  }
651
587
  
652
588
  if(quit_now){
653
 
    errno = EINTR;
654
589
    goto mandos_end;
655
590
  }
656
591
  
663
598
    ret = inet_pton(af, ip, &to.in.sin_addr);
664
599
  }
665
600
  if(ret < 0 ){
666
 
    int e = errno;
667
 
    perror_plus("inet_pton");
668
 
    errno = e;
 
601
    perror("inet_pton");
669
602
    goto mandos_end;
670
603
  }
671
604
  if(ret == 0){
672
 
    int e = errno;
673
605
    fprintf(stderr, "Bad address: %s\n", ip);
674
 
    errno = e;
675
606
    goto mandos_end;
676
607
  }
677
608
  if(af == AF_INET6){
685
616
      if(if_index == AVAHI_IF_UNSPEC){
686
617
        fprintf(stderr, "An IPv6 link-local address is incomplete"
687
618
                " without a network interface\n");
688
 
        errno = EINVAL;
689
619
        goto mandos_end;
690
620
      }
691
621
      /* Set the network interface number as scope */
698
628
  }
699
629
  
700
630
  if(quit_now){
701
 
    errno = EINTR;
702
631
    goto mandos_end;
703
632
  }
704
633
  
706
635
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
636
      char interface[IF_NAMESIZE];
708
637
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
638
        perror("if_indextoname");
710
639
      } else {
711
640
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
641
                ip, interface, port);
726
655
                        sizeof(addrstr));
727
656
    }
728
657
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
658
      perror("inet_ntop");
730
659
    } else {
731
660
      if(strcmp(addrstr, ip) != 0){
732
661
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
735
664
  }
736
665
  
737
666
  if(quit_now){
738
 
    errno = EINTR;
739
667
    goto mandos_end;
740
668
  }
741
669
  
745
673
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
746
674
  }
747
675
  if(ret < 0){
748
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
 
      int e = errno;
750
 
      perror_plus("connect");
751
 
      errno = e;
752
 
    }
 
676
    perror("connect");
753
677
    goto mandos_end;
754
678
  }
755
679
  
756
680
  if(quit_now){
757
 
    errno = EINTR;
758
681
    goto mandos_end;
759
682
  }
760
683
  
765
688
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
766
689
                                   out_size - written));
767
690
    if(ret == -1){
768
 
      int e = errno;
769
 
      perror_plus("write");
770
 
      errno = e;
 
691
      perror("write");
771
692
      goto mandos_end;
772
693
    }
773
694
    written += (size_t)ret;
783
704
    }
784
705
  
785
706
    if(quit_now){
786
 
      errno = EINTR;
787
707
      goto mandos_end;
788
708
    }
789
709
  }
793
713
  }
794
714
  
795
715
  if(quit_now){
796
 
    errno = EINTR;
797
716
    goto mandos_end;
798
717
  }
799
718
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
801
719
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
720
  
803
721
  if(quit_now){
804
 
    errno = EINTR;
805
722
    goto mandos_end;
806
723
  }
807
724
  
808
725
  do {
809
726
    ret = gnutls_handshake(session);
810
727
    if(quit_now){
811
 
      errno = EINTR;
812
728
      goto mandos_end;
813
729
    }
814
730
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
818
734
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
819
735
      gnutls_perror(ret);
820
736
    }
821
 
    errno = EPROTO;
822
737
    goto mandos_end;
823
738
  }
824
739
  
832
747
  while(true){
833
748
    
834
749
    if(quit_now){
835
 
      errno = EINTR;
836
750
      goto mandos_end;
837
751
    }
838
752
    
839
753
    buffer_capacity = incbuffer(&buffer, buffer_length,
840
754
                                   buffer_capacity);
841
755
    if(buffer_capacity == 0){
842
 
      int e = errno;
843
 
      perror_plus("incbuffer");
844
 
      errno = e;
 
756
      perror("incbuffer");
845
757
      goto mandos_end;
846
758
    }
847
759
    
848
760
    if(quit_now){
849
 
      errno = EINTR;
850
761
      goto mandos_end;
851
762
    }
852
763
    
865
776
          ret = gnutls_handshake(session);
866
777
          
867
778
          if(quit_now){
868
 
            errno = EINTR;
869
779
            goto mandos_end;
870
780
          }
871
781
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
872
782
        if(ret < 0){
873
783
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
874
784
          gnutls_perror(ret);
875
 
          errno = EPROTO;
876
785
          goto mandos_end;
877
786
        }
878
787
        break;
880
789
        fprintf(stderr, "Unknown error while reading data from"
881
790
                " encrypted session with Mandos server\n");
882
791
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
883
 
        errno = EIO;
884
792
        goto mandos_end;
885
793
      }
886
794
    } else {
893
801
  }
894
802
  
895
803
  if(quit_now){
896
 
    errno = EINTR;
897
804
    goto mandos_end;
898
805
  }
899
806
  
900
807
  do {
901
808
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
902
809
    if(quit_now){
903
 
      errno = EINTR;
904
810
      goto mandos_end;
905
811
    }
906
812
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
915
821
      written = 0;
916
822
      while(written < (size_t) decrypted_buffer_size){
917
823
        if(quit_now){
918
 
          errno = EINTR;
919
824
          goto mandos_end;
920
825
        }
921
826
        
923
828
                          (size_t)decrypted_buffer_size - written,
924
829
                          stdout);
925
830
        if(ret == 0 and ferror(stdout)){
926
 
          int e = errno;
927
831
          if(debug){
928
832
            fprintf(stderr, "Error writing encrypted data: %s\n",
929
833
                    strerror(errno));
930
834
          }
931
 
          errno = e;
932
835
          goto mandos_end;
933
836
        }
934
837
        written += (size_t)ret;
940
843
  /* Shutdown procedure */
941
844
  
942
845
 mandos_end:
943
 
  {
944
 
    int e = errno;
945
 
    free(decrypted_buffer);
946
 
    free(buffer);
947
 
    if(tcp_sd >= 0){
948
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
949
 
    }
950
 
    if(ret == -1){
951
 
      if(e == 0){
952
 
        e = errno;
953
 
      }
954
 
      perror_plus("close");
955
 
    }
956
 
    gnutls_deinit(session);
957
 
    errno = e;
958
 
    if(quit_now){
959
 
      errno = EINTR;
960
 
      retval = -1;
961
 
    }
 
846
  free(decrypted_buffer);
 
847
  free(buffer);
 
848
  if(tcp_sd >= 0){
 
849
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
850
  }
 
851
  if(ret == -1){
 
852
    perror("close");
 
853
  }
 
854
  gnutls_deinit(session);
 
855
  if(quit_now){
 
856
    retval = -1;
962
857
  }
963
858
  return retval;
964
859
}
1007
902
                                           avahi_proto_to_af(proto));
1008
903
      if(ret == 0){
1009
904
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
905
      }
1014
906
    }
1015
907
  }
1069
961
  }
1070
962
}
1071
963
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
964
/* stop main loop after sigterm has been called */
1073
965
static void handle_sigterm(int sig){
1074
966
  if(quit_now){
1075
967
    return;
1077
969
  quit_now = 1;
1078
970
  signal_received = sig;
1079
971
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
972
  if(mc.simple_poll != NULL){
1082
973
    avahi_simple_poll_quit(mc.simple_poll);
1083
974
  }
1084
975
  errno = old_errno;
1085
976
}
1086
977
 
1087
 
/* 
1088
 
 * This function determines if a directory entry in /sys/class/net
1089
 
 * corresponds to an acceptable network device.
1090
 
 * (This function is passed to scandir(3) as a filter function.)
1091
 
 */
1092
 
int good_interface(const struct dirent *if_entry){
1093
 
  ssize_t ssret;
1094
 
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
 
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
 
                     if_entry->d_name);
1100
 
  if(ret < 0){
1101
 
    perror_plus("asprintf");
1102
 
    return 0;
1103
 
  }
1104
 
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
 
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
1108
 
    return 0;
1109
 
  }
1110
 
  free(flagname);
1111
 
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
 
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
1114
 
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
 
  flagstring[(size_t)to_read] = '\0';
1116
 
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
1118
 
    close(flags_fd);
1119
 
    return 0;
1120
 
  }
1121
 
  while(to_read > 0){
1122
 
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
 
                                             (size_t)to_read));
1124
 
    if(ssret == -1){
1125
 
      perror_plus("read");
1126
 
      free(flagstring);
1127
 
      close(flags_fd);
1128
 
      return 0;
1129
 
    }
1130
 
    to_read -= ssret;
1131
 
    if(ssret == 0){
1132
 
      break;
1133
 
    }
1134
 
  }
1135
 
  close(flags_fd);
1136
 
  intmax_t tmpmax;
1137
 
  char *tmp;
1138
 
  errno = 0;
1139
 
  tmpmax = strtoimax(flagstring, &tmp, 0);
1140
 
  if(errno != 0 or tmp == flagstring or (*tmp != '\0'
1141
 
                                         and not (isspace(*tmp)))
1142
 
     or tmpmax != (ifreq_flags)tmpmax){
1143
 
    if(debug){
1144
 
      fprintf(stderr, "Invalid flags \"%s\" for interface \"%s\"\n",
1145
 
              flagstring, if_entry->d_name);
1146
 
    }
1147
 
    free(flagstring);
1148
 
    return 0;
1149
 
  }
1150
 
  free(flagstring);
1151
 
  ifreq_flags flags = (ifreq_flags)tmpmax;
1152
 
  /* Reject the loopback device */
1153
 
  if(flags & IFF_LOOPBACK){
1154
 
    if(debug){
1155
 
      fprintf(stderr, "Rejecting loopback interface \"%s\"\n",
1156
 
              if_entry->d_name);
1157
 
    }
1158
 
    return 0;
1159
 
  }
1160
 
  /* Accept point-to-point devices only if connect_to is specified */
1161
 
  if(connect_to != NULL and (flags & IFF_POINTOPOINT)){
1162
 
    if(debug){
1163
 
      fprintf(stderr, "Accepting point-to-point interface \"%s\"\n",
1164
 
              if_entry->d_name);
1165
 
    }
1166
 
    return 1;
1167
 
  }
1168
 
  /* Otherwise, reject non-broadcast-capable devices */
1169
 
  if(not (flags & IFF_BROADCAST)){
1170
 
    if(debug){
1171
 
      fprintf(stderr, "Rejecting non-broadcast interface \"%s\"\n",
1172
 
              if_entry->d_name);
1173
 
    }
1174
 
    return 0;
1175
 
  }
1176
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1177
 
  if(flags & IFF_NOARP){
1178
 
    if(debug){
1179
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1180
 
              if_entry->d_name);
1181
 
    }
1182
 
    return 0;
1183
 
  }
1184
 
  /* Accept this device */
1185
 
  if(debug){
1186
 
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1187
 
            if_entry->d_name);
1188
 
  }
1189
 
  return 1;
1190
 
}
1191
 
 
1192
 
int notdotentries(const struct dirent *direntry){
1193
 
  /* Skip "." and ".." */
1194
 
  if(direntry->d_name[0] == '.'
1195
 
     and (direntry->d_name[1] == '\0'
1196
 
          or (direntry->d_name[1] == '.'
1197
 
              and direntry->d_name[2] == '\0'))){
1198
 
    return 0;
1199
 
  }
1200
 
  return 1;
1201
 
}
1202
 
 
1203
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1204
 
  int ret;
1205
 
  struct timespec now;
1206
 
  struct timespec waited_time;
1207
 
  intmax_t block_time;
1208
 
 
1209
 
  while(true){
1210
 
    if(mc.current_server == NULL){
1211
 
      if (debug){
1212
 
        fprintf(stderr,
1213
 
                "Wait until first server is found. No timeout!\n");
1214
 
      }
1215
 
      ret = avahi_simple_poll_iterate(s, -1);
1216
 
    } else {
1217
 
      if (debug){
1218
 
        fprintf(stderr, "Check current_server if we should run it,"
1219
 
                " or wait\n");
1220
 
      }
1221
 
      /* the current time */
1222
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1223
 
      if(ret == -1){
1224
 
        perror_plus("clock_gettime");
1225
 
        return -1;
1226
 
      }
1227
 
      /* Calculating in ms how long time between now and server
1228
 
         who we visted longest time ago. Now - last seen.  */
1229
 
      waited_time.tv_sec = (now.tv_sec
1230
 
                            - mc.current_server->last_seen.tv_sec);
1231
 
      waited_time.tv_nsec = (now.tv_nsec
1232
 
                             - mc.current_server->last_seen.tv_nsec);
1233
 
      /* total time is 10s/10,000ms.
1234
 
         Converting to s from ms by dividing by 1,000,
1235
 
         and ns to ms by dividing by 1,000,000. */
1236
 
      block_time = ((retry_interval
1237
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1238
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1239
 
 
1240
 
      if (debug){
1241
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);
1242
 
      }
1243
 
 
1244
 
      if(block_time <= 0){
1245
 
        ret = start_mandos_communication(mc.current_server->ip,
1246
 
                                         mc.current_server->port,
1247
 
                                         mc.current_server->if_index,
1248
 
                                         mc.current_server->af);
1249
 
        if(ret == 0){
1250
 
          avahi_simple_poll_quit(mc.simple_poll);
1251
 
          return 0;
1252
 
        }
1253
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1254
 
                            &mc.current_server->last_seen);
1255
 
        if(ret == -1){
1256
 
          perror_plus("clock_gettime");
1257
 
          return -1;
1258
 
        }
1259
 
        mc.current_server = mc.current_server->next;
1260
 
        block_time = 0;         /* Call avahi to find new Mandos
1261
 
                                   servers, but don't block */
1262
 
      }
1263
 
      
1264
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1265
 
    }
1266
 
    if(ret != 0){
1267
 
      if (ret > 0 or errno != EINTR) {
1268
 
        return (ret != 1) ? ret : 0;
1269
 
      }
1270
 
    }
1271
 
  }
1272
 
}
1273
 
 
1274
978
int main(int argc, char *argv[]){
1275
979
  AvahiSServiceBrowser *sb = NULL;
1276
980
  int error;
1278
982
  intmax_t tmpmax;
1279
983
  char *tmp;
1280
984
  int exitcode = EXIT_SUCCESS;
1281
 
  const char *interface = "";
 
985
  const char *interface = "eth0";
1282
986
  struct ifreq network;
1283
987
  int sd = -1;
1284
988
  bool take_down_interface = false;
1285
989
  uid_t uid;
1286
990
  gid_t gid;
 
991
  char *connect_to = NULL;
1287
992
  char tempdir[] = "/tmp/mandosXXXXXX";
1288
993
  bool tempdir_created = false;
1289
994
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1293
998
  bool gnutls_initialized = false;
1294
999
  bool gpgme_initialized = false;
1295
1000
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
1298
1001
  
1299
1002
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1003
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1306
1009
  errno = 0;
1307
1010
  ret = setgid(gid);
1308
1011
  if(ret == -1){
1309
 
    perror_plus("setgid");
 
1012
    perror("setgid");
1310
1013
  }
1311
1014
  
1312
1015
  /* Lower user privileges (temporarily) */
1313
1016
  errno = 0;
1314
1017
  ret = seteuid(uid);
1315
1018
  if(ret == -1){
1316
 
    perror_plus("seteuid");
 
1019
    perror("seteuid");
1317
1020
  }
1318
1021
  
1319
1022
  if(quit_now){
1354
1057
        .arg = "SECONDS",
1355
1058
        .doc = "Maximum delay to wait for interface startup",
1356
1059
        .group = 2 },
1357
 
      { .name = "retry", .key = 132,
1358
 
        .arg = "SECONDS",
1359
 
        .doc = "Retry interval used when denied by the mandos server",
1360
 
        .group = 2 },
1361
1060
      /*
1362
1061
       * These reproduce what we would get without ARGP_NO_HELP
1363
1062
       */
1407
1106
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
1107
          argp_error(state, "Bad delay");
1409
1108
        }
1410
 
      case 132:                 /* --retry */
1411
 
        errno = 0;
1412
 
        retry_interval = strtod(arg, &tmp);
1413
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1414
 
           or (retry_interval * 1000) > INT_MAX){
1415
 
          argp_error(state, "Bad retry interval");
1416
 
        }
1417
1109
        break;
1418
1110
        /*
1419
1111
         * These reproduce what we would get without ARGP_NO_HELP
1447
1139
    case ENOMEM:
1448
1140
    default:
1449
1141
      errno = ret;
1450
 
      perror_plus("argp_parse");
 
1142
      perror("argp_parse");
1451
1143
      exitcode = EX_OSERR;
1452
1144
      goto end;
1453
1145
    case EINVAL:
1455
1147
      goto end;
1456
1148
    }
1457
1149
  }
1458
 
    
1459
 
  {
1460
 
    /* Work around Debian bug #633582:
1461
 
       <http://bugs.debian.org/633582> */
1462
 
    struct stat st;
1463
 
    
1464
 
    /* Re-raise priviliges */
1465
 
    errno = 0;
1466
 
    ret = seteuid(0);
1467
 
    if(ret == -1){
1468
 
      perror_plus("seteuid");
1469
 
    }
1470
 
    
1471
 
    int seckey_fd = open(PATHDIR "/" SECKEY, O_RDONLY);
1472
 
    if(seckey_fd == -1){
1473
 
      perror_plus("open");
1474
 
    } else {
1475
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1476
 
      if(ret == -1){
1477
 
        perror_plus("fstat");
1478
 
      } else {
1479
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1480
 
          ret = fchown(seckey_fd, uid, gid);
1481
 
          if(ret == -1){
1482
 
            perror_plus("fchown");
1483
 
          }
1484
 
        }
1485
 
      }
1486
 
      TEMP_FAILURE_RETRY(close(seckey_fd));
1487
 
    }
1488
 
    
1489
 
    int pubkey_fd = open(PATHDIR "/" PUBKEY, O_RDONLY);
1490
 
    if(pubkey_fd == -1){
1491
 
      perror_plus("open");
1492
 
    } else {
1493
 
      ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1494
 
      if(ret == -1){
1495
 
        perror_plus("fstat");
1496
 
      } else {
1497
 
        if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1498
 
          ret = fchown(pubkey_fd, uid, gid);
1499
 
          if(ret == -1){
1500
 
            perror_plus("fchown");
1501
 
          }
1502
 
        }
1503
 
      }
1504
 
      TEMP_FAILURE_RETRY(close(pubkey_fd));
1505
 
    }
1506
 
    
1507
 
    /* Lower privileges */
1508
 
    errno = 0;
1509
 
    ret = seteuid(uid);
1510
 
    if(ret == -1){
1511
 
      perror_plus("seteuid");
1512
 
    }
1513
 
  }
1514
1150
  
1515
1151
  if(not debug){
1516
1152
    avahi_set_log_function(empty_log);
1517
1153
  }
1518
 
 
1519
 
  if(interface[0] == '\0'){
1520
 
    struct dirent **direntries;
1521
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1522
 
                  alphasort);
1523
 
    if(ret >= 1){
1524
 
      /* Pick the first good interface */
1525
 
      interface = strdup(direntries[0]->d_name);
1526
 
      if(debug){
1527
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1528
 
      }
1529
 
      if(interface == NULL){
1530
 
        perror_plus("malloc");
1531
 
        free(direntries);
1532
 
        exitcode = EXIT_FAILURE;
1533
 
        goto end;
1534
 
      }
1535
 
      free(direntries);
1536
 
    } else {
1537
 
      free(direntries);
1538
 
      fprintf(stderr, "Could not find a network interface\n");
1539
 
      exitcode = EXIT_FAILURE;
1540
 
      goto end;
1541
 
    }
1542
 
  }
1543
1154
  
1544
1155
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1545
1156
     from the signal handler */
1548
1159
  mc.simple_poll = avahi_simple_poll_new();
1549
1160
  if(mc.simple_poll == NULL){
1550
1161
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1551
 
    exitcode = EX_UNAVAILABLE;
 
1162
    exitcode = EXIT_FAILURE;
1552
1163
    goto end;
1553
1164
  }
1554
1165
  
1555
1166
  sigemptyset(&sigterm_action.sa_mask);
1556
1167
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1557
1168
  if(ret == -1){
1558
 
    perror_plus("sigaddset");
1559
 
    exitcode = EX_OSERR;
 
1169
    perror("sigaddset");
 
1170
    exitcode = EXIT_FAILURE;
1560
1171
    goto end;
1561
1172
  }
1562
1173
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1563
1174
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
1565
 
    exitcode = EX_OSERR;
 
1175
    perror("sigaddset");
 
1176
    exitcode = EXIT_FAILURE;
1566
1177
    goto end;
1567
1178
  }
1568
1179
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1569
1180
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
1571
 
    exitcode = EX_OSERR;
 
1181
    perror("sigaddset");
 
1182
    exitcode = EXIT_FAILURE;
1572
1183
    goto end;
1573
1184
  }
1574
1185
  /* Need to check if the handler is SIG_IGN before handling:
1577
1188
  */
1578
1189
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1579
1190
  if(ret == -1){
1580
 
    perror_plus("sigaction");
1581
 
    return EX_OSERR;
 
1191
    perror("sigaction");
 
1192
    return EXIT_FAILURE;
1582
1193
  }
1583
1194
  if(old_sigterm_action.sa_handler != SIG_IGN){
1584
1195
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1585
1196
    if(ret == -1){
1586
 
      perror_plus("sigaction");
1587
 
      exitcode = EX_OSERR;
 
1197
      perror("sigaction");
 
1198
      exitcode = EXIT_FAILURE;
1588
1199
      goto end;
1589
1200
    }
1590
1201
  }
1591
1202
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1592
1203
  if(ret == -1){
1593
 
    perror_plus("sigaction");
1594
 
    return EX_OSERR;
 
1204
    perror("sigaction");
 
1205
    return EXIT_FAILURE;
1595
1206
  }
1596
1207
  if(old_sigterm_action.sa_handler != SIG_IGN){
1597
1208
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1598
1209
    if(ret == -1){
1599
 
      perror_plus("sigaction");
1600
 
      exitcode = EX_OSERR;
 
1210
      perror("sigaction");
 
1211
      exitcode = EXIT_FAILURE;
1601
1212
      goto end;
1602
1213
    }
1603
1214
  }
1604
1215
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1605
1216
  if(ret == -1){
1606
 
    perror_plus("sigaction");
1607
 
    return EX_OSERR;
 
1217
    perror("sigaction");
 
1218
    return EXIT_FAILURE;
1608
1219
  }
1609
1220
  if(old_sigterm_action.sa_handler != SIG_IGN){
1610
1221
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1611
1222
    if(ret == -1){
1612
 
      perror_plus("sigaction");
1613
 
      exitcode = EX_OSERR;
 
1223
      perror("sigaction");
 
1224
      exitcode = EXIT_FAILURE;
1614
1225
      goto end;
1615
1226
    }
1616
1227
  }
1617
1228
  
1618
1229
  /* If the interface is down, bring it up */
1619
 
  if(strcmp(interface, "none") != 0){
 
1230
  if(interface[0] != '\0'){
1620
1231
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1621
1232
    if(if_index == 0){
1622
1233
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1623
 
      exitcode = EX_UNAVAILABLE;
 
1234
      exitcode = EXIT_FAILURE;
1624
1235
      goto end;
1625
1236
    }
1626
1237
    
1632
1243
    errno = 0;
1633
1244
    ret = seteuid(0);
1634
1245
    if(ret == -1){
1635
 
      perror_plus("seteuid");
 
1246
      perror("seteuid");
1636
1247
    }
1637
1248
    
1638
1249
#ifdef __linux__
1642
1253
    bool restore_loglevel = true;
1643
1254
    if(ret == -1){
1644
1255
      restore_loglevel = false;
1645
 
      perror_plus("klogctl");
 
1256
      perror("klogctl");
1646
1257
    }
1647
1258
#endif  /* __linux__ */
1648
1259
    
1649
1260
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1650
1261
    if(sd < 0){
1651
 
      perror_plus("socket");
1652
 
      exitcode = EX_OSERR;
 
1262
      perror("socket");
 
1263
      exitcode = EXIT_FAILURE;
1653
1264
#ifdef __linux__
1654
1265
      if(restore_loglevel){
1655
1266
        ret = klogctl(7, NULL, 0);
1656
1267
        if(ret == -1){
1657
 
          perror_plus("klogctl");
 
1268
          perror("klogctl");
1658
1269
        }
1659
1270
      }
1660
1271
#endif  /* __linux__ */
1662
1273
      errno = 0;
1663
1274
      ret = seteuid(uid);
1664
1275
      if(ret == -1){
1665
 
        perror_plus("seteuid");
 
1276
        perror("seteuid");
1666
1277
      }
1667
1278
      goto end;
1668
1279
    }
1669
1280
    strcpy(network.ifr_name, interface);
1670
1281
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1671
1282
    if(ret == -1){
1672
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1283
      perror("ioctl SIOCGIFFLAGS");
1673
1284
#ifdef __linux__
1674
1285
      if(restore_loglevel){
1675
1286
        ret = klogctl(7, NULL, 0);
1676
1287
        if(ret == -1){
1677
 
          perror_plus("klogctl");
 
1288
          perror("klogctl");
1678
1289
        }
1679
1290
      }
1680
1291
#endif  /* __linux__ */
1681
 
      exitcode = EX_OSERR;
 
1292
      exitcode = EXIT_FAILURE;
1682
1293
      /* Lower privileges */
1683
1294
      errno = 0;
1684
1295
      ret = seteuid(uid);
1685
1296
      if(ret == -1){
1686
 
        perror_plus("seteuid");
 
1297
        perror("seteuid");
1687
1298
      }
1688
1299
      goto end;
1689
1300
    }
1693
1304
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1694
1305
      if(ret == -1){
1695
1306
        take_down_interface = false;
1696
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1697
 
        exitcode = EX_OSERR;
 
1307
        perror("ioctl SIOCSIFFLAGS");
 
1308
        exitcode = EXIT_FAILURE;
1698
1309
#ifdef __linux__
1699
1310
        if(restore_loglevel){
1700
1311
          ret = klogctl(7, NULL, 0);
1701
1312
          if(ret == -1){
1702
 
            perror_plus("klogctl");
 
1313
            perror("klogctl");
1703
1314
          }
1704
1315
        }
1705
1316
#endif  /* __linux__ */
1707
1318
        errno = 0;
1708
1319
        ret = seteuid(uid);
1709
1320
        if(ret == -1){
1710
 
          perror_plus("seteuid");
 
1321
          perror("seteuid");
1711
1322
        }
1712
1323
        goto end;
1713
1324
      }
1714
1325
    }
1715
 
    /* Sleep checking until interface is running.
1716
 
       Check every 0.25s, up to total time of delay */
 
1326
    /* sleep checking until interface is running */
1717
1327
    for(int i=0; i < delay * 4; i++){
1718
1328
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1719
1329
      if(ret == -1){
1720
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1330
        perror("ioctl SIOCGIFFLAGS");
1721
1331
      } else if(network.ifr_flags & IFF_RUNNING){
1722
1332
        break;
1723
1333
      }
1724
1334
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1725
1335
      ret = nanosleep(&sleeptime, NULL);
1726
1336
      if(ret == -1 and errno != EINTR){
1727
 
        perror_plus("nanosleep");
 
1337
        perror("nanosleep");
1728
1338
      }
1729
1339
    }
1730
1340
    if(not take_down_interface){
1731
1341
      /* We won't need the socket anymore */
1732
1342
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1733
1343
      if(ret == -1){
1734
 
        perror_plus("close");
 
1344
        perror("close");
1735
1345
      }
1736
1346
    }
1737
1347
#ifdef __linux__
1739
1349
      /* Restores kernel loglevel to default */
1740
1350
      ret = klogctl(7, NULL, 0);
1741
1351
      if(ret == -1){
1742
 
        perror_plus("klogctl");
 
1352
        perror("klogctl");
1743
1353
      }
1744
1354
    }
1745
1355
#endif  /* __linux__ */
1749
1359
      /* Lower privileges */
1750
1360
      ret = seteuid(uid);
1751
1361
      if(ret == -1){
1752
 
        perror_plus("seteuid");
 
1362
        perror("seteuid");
1753
1363
      }
1754
1364
    } else {
1755
1365
      /* Lower privileges permanently */
1756
1366
      ret = setuid(uid);
1757
1367
      if(ret == -1){
1758
 
        perror_plus("setuid");
 
1368
        perror("setuid");
1759
1369
      }
1760
1370
    }
1761
1371
  }
1767
1377
  ret = init_gnutls_global(pubkey, seckey);
1768
1378
  if(ret == -1){
1769
1379
    fprintf(stderr, "init_gnutls_global failed\n");
1770
 
    exitcode = EX_UNAVAILABLE;
 
1380
    exitcode = EXIT_FAILURE;
1771
1381
    goto end;
1772
1382
  } else {
1773
1383
    gnutls_initialized = true;
1777
1387
    goto end;
1778
1388
  }
1779
1389
  
 
1390
  tempdir_created = true;
1780
1391
  if(mkdtemp(tempdir) == NULL){
1781
 
    perror_plus("mkdtemp");
 
1392
    tempdir_created = false;
 
1393
    perror("mkdtemp");
1782
1394
    goto end;
1783
1395
  }
1784
 
  tempdir_created = true;
1785
1396
  
1786
1397
  if(quit_now){
1787
1398
    goto end;
1789
1400
  
1790
1401
  if(not init_gpgme(pubkey, seckey, tempdir)){
1791
1402
    fprintf(stderr, "init_gpgme failed\n");
1792
 
    exitcode = EX_UNAVAILABLE;
 
1403
    exitcode = EXIT_FAILURE;
1793
1404
    goto end;
1794
1405
  } else {
1795
1406
    gpgme_initialized = true;
1805
1416
    char *address = strrchr(connect_to, ':');
1806
1417
    if(address == NULL){
1807
1418
      fprintf(stderr, "No colon in address\n");
1808
 
      exitcode = EX_USAGE;
 
1419
      exitcode = EXIT_FAILURE;
1809
1420
      goto end;
1810
1421
    }
1811
1422
    
1819
1430
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1820
1431
       or tmpmax != (uint16_t)tmpmax){
1821
1432
      fprintf(stderr, "Bad port number\n");
1822
 
      exitcode = EX_USAGE;
 
1433
      exitcode = EXIT_FAILURE;
1823
1434
      goto end;
1824
1435
    }
1825
1436
  
1841
1452
    if(quit_now){
1842
1453
      goto end;
1843
1454
    }
1844
 
 
1845
 
    while(not quit_now){
1846
 
      ret = start_mandos_communication(address, port, if_index, af);
1847
 
      if(quit_now or ret == 0){
1848
 
        break;
1849
 
      }
1850
 
      sleep((int)retry_interval or 1);
1851
 
    };
1852
 
 
1853
 
    if (not quit_now){
 
1455
    
 
1456
    ret = start_mandos_communication(address, port, if_index, af);
 
1457
    if(ret < 0){
 
1458
      exitcode = EXIT_FAILURE;
 
1459
    } else {
1854
1460
      exitcode = EXIT_SUCCESS;
1855
1461
    }
1856
 
 
1857
1462
    goto end;
1858
1463
  }
1859
1464
  
1883
1488
  if(mc.server == NULL){
1884
1489
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1885
1490
            avahi_strerror(error));
1886
 
    exitcode = EX_UNAVAILABLE;
 
1491
    exitcode = EXIT_FAILURE;
1887
1492
    goto end;
1888
1493
  }
1889
1494
  
1898
1503
  if(sb == NULL){
1899
1504
    fprintf(stderr, "Failed to create service browser: %s\n",
1900
1505
            avahi_strerror(avahi_server_errno(mc.server)));
1901
 
    exitcode = EX_UNAVAILABLE;
 
1506
    exitcode = EXIT_FAILURE;
1902
1507
    goto end;
1903
1508
  }
1904
1509
  
1911
1516
  if(debug){
1912
1517
    fprintf(stderr, "Starting Avahi loop search\n");
1913
1518
  }
1914
 
 
1915
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1916
 
                                (int)(retry_interval * 1000));
1917
 
  if(debug){
1918
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1919
 
            (ret == 0) ? "successfully" : "with error");
1920
 
  }
 
1519
  
 
1520
  avahi_simple_poll_loop(mc.simple_poll);
1921
1521
  
1922
1522
 end:
1923
1523
  
1944
1544
  if(gpgme_initialized){
1945
1545
    gpgme_release(mc.ctx);
1946
1546
  }
1947
 
 
1948
 
  /* Cleans up the circular linked list of Mandos servers the client
1949
 
     has seen */
1950
 
  if(mc.current_server != NULL){
1951
 
    mc.current_server->prev->next = NULL;
1952
 
    while(mc.current_server != NULL){
1953
 
      server *next = mc.current_server->next;
1954
 
      free(mc.current_server);
1955
 
      mc.current_server = next;
1956
 
    }
1957
 
  }
1958
1547
  
1959
1548
  /* Take down the network interface */
1960
1549
  if(take_down_interface){
1962
1551
    errno = 0;
1963
1552
    ret = seteuid(0);
1964
1553
    if(ret == -1){
1965
 
      perror_plus("seteuid");
 
1554
      perror("seteuid");
1966
1555
    }
1967
1556
    if(geteuid() == 0){
1968
1557
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1969
1558
      if(ret == -1){
1970
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1559
        perror("ioctl SIOCGIFFLAGS");
1971
1560
      } else if(network.ifr_flags & IFF_UP) {
1972
1561
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1973
1562
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
1563
        if(ret == -1){
1975
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1564
          perror("ioctl SIOCSIFFLAGS");
1976
1565
        }
1977
1566
      }
1978
1567
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1979
1568
      if(ret == -1){
1980
 
        perror_plus("close");
 
1569
        perror("close");
1981
1570
      }
1982
1571
      /* Lower privileges permanently */
1983
1572
      errno = 0;
1984
1573
      ret = setuid(uid);
1985
1574
      if(ret == -1){
1986
 
        perror_plus("setuid");
 
1575
        perror("setuid");
1987
1576
      }
1988
1577
    }
1989
1578
  }
1990
1579
  
1991
 
  /* Removes the GPGME temp directory and all files inside */
 
1580
  /* Removes the temp directory used by GPGME */
1992
1581
  if(tempdir_created){
1993
 
    struct dirent **direntries = NULL;
1994
 
    struct dirent *direntry = NULL;
1995
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1996
 
    if (ret > 0){
1997
 
      for(int i = 0; i < ret; i++){
1998
 
        direntry = direntries[i];
 
1582
    DIR *d;
 
1583
    struct dirent *direntry;
 
1584
    d = opendir(tempdir);
 
1585
    if(d == NULL){
 
1586
      if(errno != ENOENT){
 
1587
        perror("opendir");
 
1588
      }
 
1589
    } else {
 
1590
      while(true){
 
1591
        direntry = readdir(d);
 
1592
        if(direntry == NULL){
 
1593
          break;
 
1594
        }
 
1595
        /* Skip "." and ".." */
 
1596
        if(direntry->d_name[0] == '.'
 
1597
           and (direntry->d_name[1] == '\0'
 
1598
                or (direntry->d_name[1] == '.'
 
1599
                    and direntry->d_name[2] == '\0'))){
 
1600
          continue;
 
1601
        }
1999
1602
        char *fullname = NULL;
2000
1603
        ret = asprintf(&fullname, "%s/%s", tempdir,
2001
1604
                       direntry->d_name);
2002
1605
        if(ret < 0){
2003
 
          perror_plus("asprintf");
 
1606
          perror("asprintf");
2004
1607
          continue;
2005
1608
        }
2006
1609
        ret = remove(fullname);
2010
1613
        }
2011
1614
        free(fullname);
2012
1615
      }
2013
 
    }
2014
 
 
2015
 
    /* need to be cleaned even if ret == 0 because man page doesn't
2016
 
       specify */
2017
 
    free(direntries);
2018
 
    if (ret == -1){
2019
 
      perror_plus("scandir");
 
1616
      closedir(d);
2020
1617
    }
2021
1618
    ret = rmdir(tempdir);
2022
1619
    if(ret == -1 and errno != ENOENT){
2023
 
      perror_plus("rmdir");
 
1620
      perror("rmdir");
2024
1621
    }
2025
1622
  }
2026
1623
  
2031
1628
                                            &old_sigterm_action,
2032
1629
                                            NULL));
2033
1630
    if(ret == -1){
2034
 
      perror_plus("sigaction");
 
1631
      perror("sigaction");
2035
1632
    }
2036
1633
    do {
2037
1634
      ret = raise(signal_received);
2038
1635
    } while(ret != 0 and errno == EINTR);
2039
1636
    if(ret != 0){
2040
 
      perror_plus("raise");
 
1637
      perror("raise");
2041
1638
      abort();
2042
1639
    }
2043
1640
    TEMP_FAILURE_RETRY(pause());