/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 at bsnet
  • Date: 2010-08-16 04:30:16 UTC
  • mto: This revision was merged to the branch mainline in revision 419.
  • Revision ID: teddy@fukt.bsnet.se-20100816043016-6wzjp3avb84gaejz
* mandos (main): Bug fix: Don't try to bind to an empty string
                 interface.

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 */
88
85
 
89
86
#ifdef __linux__
90
87
#include <sys/klog.h>           /* klogctl() */
131
128
static const char sys_class_net[] = "/sys/class/net";
132
129
char *connect_to = NULL;
133
130
 
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
 
 
145
131
/* Used for passing in values through the Avahi callback functions */
146
132
typedef struct {
147
133
  AvahiSimplePoll *simple_poll;
151
137
  gnutls_dh_params_t dh_params;
152
138
  const char *priority;
153
139
  gpgme_ctx_t ctx;
154
 
  server *current_server;
155
140
} mandos_context;
156
141
 
157
142
/* global context so signal handler can reach it*/
158
143
mandos_context mc = { .simple_poll = NULL, .server = NULL,
159
144
                      .dh_bits = 1024, .priority = "SECURE256"
160
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP",
161
 
                      .current_server = NULL };
 
145
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
162
146
 
163
147
sig_atomic_t quit_now = 0;
164
148
int signal_received = 0;
165
149
 
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
150
/*
174
151
 * Make additional room in "buffer" for at least BUFFER_SIZE more
175
152
 * bytes. "buffer_capacity" is how much is currently allocated,
187
164
  return buffer_capacity;
188
165
}
189
166
 
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
167
/* 
228
168
 * Initialize GPGME.
229
169
 */
243
183
    
244
184
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
245
185
    if(fd == -1){
246
 
      perror_plus("open");
 
186
      perror("open");
247
187
      return false;
248
188
    }
249
189
    
263
203
    
264
204
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
265
205
    if(ret == -1){
266
 
      perror_plus("close");
 
206
      perror("close");
267
207
    }
268
208
    gpgme_data_release(pgp_data);
269
209
    return true;
394
334
  
395
335
  /* Seek back to the beginning of the GPGME plaintext data buffer */
396
336
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
397
 
    perror_plus("gpgme_data_seek");
 
337
    perror("gpgme_data_seek");
398
338
    plaintext_length = -1;
399
339
    goto decrypt_end;
400
340
  }
405
345
                                      (size_t)plaintext_length,
406
346
                                      plaintext_capacity);
407
347
    if(plaintext_capacity == 0){
408
 
        perror_plus("incbuffer");
 
348
        perror("incbuffer");
409
349
        plaintext_length = -1;
410
350
        goto decrypt_end;
411
351
    }
418
358
      break;
419
359
    }
420
360
    if(ret < 0){
421
 
      perror_plus("gpgme_data_read");
 
361
      perror("gpgme_data_read");
422
362
      plaintext_length = -1;
423
363
      goto decrypt_end;
424
364
    }
481
421
  }
482
422
  
483
423
  /* OpenPGP credentials */
484
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
485
425
  if(ret != GNUTLS_E_SUCCESS){
486
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
487
430
            safer_gnutls_strerror(ret));
488
431
    gnutls_global_deinit();
489
432
    return -1;
611
554
  gnutls_session_t session;
612
555
  int pf;                       /* Protocol family */
613
556
  
614
 
  errno = 0;
615
 
  
616
557
  if(quit_now){
617
 
    errno = EINTR;
618
558
    return -1;
619
559
  }
620
560
  
627
567
    break;
628
568
  default:
629
569
    fprintf(stderr, "Bad address family: %d\n", af);
630
 
    errno = EINVAL;
631
570
    return -1;
632
571
  }
633
572
  
643
582
  
644
583
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
584
  if(tcp_sd < 0){
646
 
    int e = errno;
647
 
    perror_plus("socket");
648
 
    errno = e;
 
585
    perror("socket");
649
586
    goto mandos_end;
650
587
  }
651
588
  
652
589
  if(quit_now){
653
 
    errno = EINTR;
654
590
    goto mandos_end;
655
591
  }
656
592
  
663
599
    ret = inet_pton(af, ip, &to.in.sin_addr);
664
600
  }
665
601
  if(ret < 0 ){
666
 
    int e = errno;
667
 
    perror_plus("inet_pton");
668
 
    errno = e;
 
602
    perror("inet_pton");
669
603
    goto mandos_end;
670
604
  }
671
605
  if(ret == 0){
672
 
    int e = errno;
673
606
    fprintf(stderr, "Bad address: %s\n", ip);
674
 
    errno = e;
675
607
    goto mandos_end;
676
608
  }
677
609
  if(af == AF_INET6){
685
617
      if(if_index == AVAHI_IF_UNSPEC){
686
618
        fprintf(stderr, "An IPv6 link-local address is incomplete"
687
619
                " without a network interface\n");
688
 
        errno = EINVAL;
689
620
        goto mandos_end;
690
621
      }
691
622
      /* Set the network interface number as scope */
698
629
  }
699
630
  
700
631
  if(quit_now){
701
 
    errno = EINTR;
702
632
    goto mandos_end;
703
633
  }
704
634
  
706
636
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
637
      char interface[IF_NAMESIZE];
708
638
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
639
        perror("if_indextoname");
710
640
      } else {
711
641
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
642
                ip, interface, port);
726
656
                        sizeof(addrstr));
727
657
    }
728
658
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
659
      perror("inet_ntop");
730
660
    } else {
731
661
      if(strcmp(addrstr, ip) != 0){
732
662
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
735
665
  }
736
666
  
737
667
  if(quit_now){
738
 
    errno = EINTR;
739
668
    goto mandos_end;
740
669
  }
741
670
  
745
674
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
746
675
  }
747
676
  if(ret < 0){
748
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
 
      int e = errno;
750
 
      perror_plus("connect");
751
 
      errno = e;
752
 
    }
 
677
    perror("connect");
753
678
    goto mandos_end;
754
679
  }
755
680
  
756
681
  if(quit_now){
757
 
    errno = EINTR;
758
682
    goto mandos_end;
759
683
  }
760
684
  
765
689
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
766
690
                                   out_size - written));
767
691
    if(ret == -1){
768
 
      int e = errno;
769
 
      perror_plus("write");
770
 
      errno = e;
 
692
      perror("write");
771
693
      goto mandos_end;
772
694
    }
773
695
    written += (size_t)ret;
783
705
    }
784
706
  
785
707
    if(quit_now){
786
 
      errno = EINTR;
787
708
      goto mandos_end;
788
709
    }
789
710
  }
793
714
  }
794
715
  
795
716
  if(quit_now){
796
 
    errno = EINTR;
797
717
    goto mandos_end;
798
718
  }
799
719
  
800
 
  /* Spurious warning from -Wint-to-pointer-cast */
801
720
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
721
  
803
722
  if(quit_now){
804
 
    errno = EINTR;
805
723
    goto mandos_end;
806
724
  }
807
725
  
808
726
  do {
809
727
    ret = gnutls_handshake(session);
810
728
    if(quit_now){
811
 
      errno = EINTR;
812
729
      goto mandos_end;
813
730
    }
814
731
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
818
735
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
819
736
      gnutls_perror(ret);
820
737
    }
821
 
    errno = EPROTO;
822
738
    goto mandos_end;
823
739
  }
824
740
  
832
748
  while(true){
833
749
    
834
750
    if(quit_now){
835
 
      errno = EINTR;
836
751
      goto mandos_end;
837
752
    }
838
753
    
839
754
    buffer_capacity = incbuffer(&buffer, buffer_length,
840
755
                                   buffer_capacity);
841
756
    if(buffer_capacity == 0){
842
 
      int e = errno;
843
 
      perror_plus("incbuffer");
844
 
      errno = e;
 
757
      perror("incbuffer");
845
758
      goto mandos_end;
846
759
    }
847
760
    
848
761
    if(quit_now){
849
 
      errno = EINTR;
850
762
      goto mandos_end;
851
763
    }
852
764
    
865
777
          ret = gnutls_handshake(session);
866
778
          
867
779
          if(quit_now){
868
 
            errno = EINTR;
869
780
            goto mandos_end;
870
781
          }
871
782
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
872
783
        if(ret < 0){
873
784
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
874
785
          gnutls_perror(ret);
875
 
          errno = EPROTO;
876
786
          goto mandos_end;
877
787
        }
878
788
        break;
880
790
        fprintf(stderr, "Unknown error while reading data from"
881
791
                " encrypted session with Mandos server\n");
882
792
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
883
 
        errno = EIO;
884
793
        goto mandos_end;
885
794
      }
886
795
    } else {
893
802
  }
894
803
  
895
804
  if(quit_now){
896
 
    errno = EINTR;
897
805
    goto mandos_end;
898
806
  }
899
807
  
900
808
  do {
901
809
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
902
810
    if(quit_now){
903
 
      errno = EINTR;
904
811
      goto mandos_end;
905
812
    }
906
813
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
915
822
      written = 0;
916
823
      while(written < (size_t) decrypted_buffer_size){
917
824
        if(quit_now){
918
 
          errno = EINTR;
919
825
          goto mandos_end;
920
826
        }
921
827
        
923
829
                          (size_t)decrypted_buffer_size - written,
924
830
                          stdout);
925
831
        if(ret == 0 and ferror(stdout)){
926
 
          int e = errno;
927
832
          if(debug){
928
833
            fprintf(stderr, "Error writing encrypted data: %s\n",
929
834
                    strerror(errno));
930
835
          }
931
 
          errno = e;
932
836
          goto mandos_end;
933
837
        }
934
838
        written += (size_t)ret;
940
844
  /* Shutdown procedure */
941
845
  
942
846
 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
 
    }
 
847
  free(decrypted_buffer);
 
848
  free(buffer);
 
849
  if(tcp_sd >= 0){
 
850
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
851
  }
 
852
  if(ret == -1){
 
853
    perror("close");
 
854
  }
 
855
  gnutls_deinit(session);
 
856
  if(quit_now){
 
857
    retval = -1;
962
858
  }
963
859
  return retval;
964
860
}
1007
903
                                           avahi_proto_to_af(proto));
1008
904
      if(ret == 0){
1009
905
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
906
      }
1014
907
    }
1015
908
  }
1069
962
  }
1070
963
}
1071
964
 
1072
 
/* Signal handler that stops main loop after SIGTERM */
 
965
/* stop main loop after sigterm has been called */
1073
966
static void handle_sigterm(int sig){
1074
967
  if(quit_now){
1075
968
    return;
1077
970
  quit_now = 1;
1078
971
  signal_received = sig;
1079
972
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
973
  if(mc.simple_poll != NULL){
1082
974
    avahi_simple_poll_quit(mc.simple_poll);
1083
975
  }
1092
984
int good_interface(const struct dirent *if_entry){
1093
985
  ssize_t ssret;
1094
986
  char *flagname = NULL;
1095
 
  if(if_entry->d_name[0] == '.'){
1096
 
    return 0;
1097
 
  }
1098
987
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1099
988
                     if_entry->d_name);
1100
989
  if(ret < 0){
1101
 
    perror_plus("asprintf");
 
990
    perror("asprintf");
 
991
    return 0;
 
992
  }
 
993
  if(if_entry->d_name[0] == '.'){
1102
994
    return 0;
1103
995
  }
1104
996
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1105
997
  if(flags_fd == -1){
1106
 
    perror_plus("open");
1107
 
    free(flagname);
 
998
    perror("open");
1108
999
    return 0;
1109
1000
  }
1110
 
  free(flagname);
1111
1001
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1112
1002
  /* read line from flags_fd */
1113
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1003
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1114
1004
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1115
1005
  flagstring[(size_t)to_read] = '\0';
1116
1006
  if(flagstring == NULL){
1117
 
    perror_plus("malloc");
 
1007
    perror("malloc");
1118
1008
    close(flags_fd);
1119
1009
    return 0;
1120
1010
  }
1122
1012
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1123
1013
                                             (size_t)to_read));
1124
1014
    if(ssret == -1){
1125
 
      perror_plus("read");
 
1015
      perror("read");
1126
1016
      free(flagstring);
1127
1017
      close(flags_fd);
1128
1018
      return 0;
1173
1063
    }
1174
1064
    return 0;
1175
1065
  }
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
1066
  /* Accept this device */
1185
1067
  if(debug){
1186
1068
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1189
1071
  return 1;
1190
1072
}
1191
1073
 
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
1074
int main(int argc, char *argv[]){
1275
1075
  AvahiSServiceBrowser *sb = NULL;
1276
1076
  int error;
1293
1093
  bool gnutls_initialized = false;
1294
1094
  bool gpgme_initialized = false;
1295
1095
  float delay = 2.5f;
1296
 
  double retry_interval = 10; /* 10s between trying a server and
1297
 
                                 retrying the same server again */
1298
1096
  
1299
1097
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1300
1098
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1306
1104
  errno = 0;
1307
1105
  ret = setgid(gid);
1308
1106
  if(ret == -1){
1309
 
    perror_plus("setgid");
 
1107
    perror("setgid");
1310
1108
  }
1311
1109
  
1312
1110
  /* Lower user privileges (temporarily) */
1313
1111
  errno = 0;
1314
1112
  ret = seteuid(uid);
1315
1113
  if(ret == -1){
1316
 
    perror_plus("seteuid");
 
1114
    perror("seteuid");
1317
1115
  }
1318
1116
  
1319
1117
  if(quit_now){
1354
1152
        .arg = "SECONDS",
1355
1153
        .doc = "Maximum delay to wait for interface startup",
1356
1154
        .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
 
      /*
1362
 
       * These reproduce what we would get without ARGP_NO_HELP
1363
 
       */
1364
 
      { .name = "help", .key = '?',
1365
 
        .doc = "Give this help list", .group = -1 },
1366
 
      { .name = "usage", .key = -3,
1367
 
        .doc = "Give a short usage message", .group = -1 },
1368
 
      { .name = "version", .key = 'V',
1369
 
        .doc = "Print program version", .group = -1 },
1370
1155
      { .name = NULL }
1371
1156
    };
1372
1157
    
1373
1158
    error_t parse_opt(int key, char *arg,
1374
1159
                      struct argp_state *state){
1375
 
      errno = 0;
1376
1160
      switch(key){
1377
1161
      case 128:                 /* --debug */
1378
1162
        debug = true;
1394
1178
        tmpmax = strtoimax(arg, &tmp, 10);
1395
1179
        if(errno != 0 or tmp == arg or *tmp != '\0'
1396
1180
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1397
 
          argp_error(state, "Bad number of DH bits");
 
1181
          fprintf(stderr, "Bad number of DH bits\n");
 
1182
          exit(EXIT_FAILURE);
1398
1183
        }
1399
1184
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1400
1185
        break;
1405
1190
        errno = 0;
1406
1191
        delay = strtof(arg, &tmp);
1407
1192
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1408
 
          argp_error(state, "Bad delay");
1409
 
        }
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");
 
1193
          fprintf(stderr, "Bad delay\n");
 
1194
          exit(EXIT_FAILURE);
1416
1195
        }
1417
1196
        break;
1418
 
        /*
1419
 
         * These reproduce what we would get without ARGP_NO_HELP
1420
 
         */
1421
 
      case '?':                 /* --help */
1422
 
        argp_state_help(state, state->out_stream,
1423
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1424
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1425
 
      case -3:                  /* --usage */
1426
 
        argp_state_help(state, state->out_stream,
1427
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1428
 
      case 'V':                 /* --version */
1429
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1430
 
        exit(argp_err_exit_status);
 
1197
      case ARGP_KEY_ARG:
 
1198
        argp_usage(state);
 
1199
      case ARGP_KEY_END:
1431
1200
        break;
1432
1201
      default:
1433
1202
        return ARGP_ERR_UNKNOWN;
1434
1203
      }
1435
 
      return errno;
 
1204
      return 0;
1436
1205
    }
1437
1206
    
1438
1207
    struct argp argp = { .options = options, .parser = parse_opt,
1439
1208
                         .args_doc = "",
1440
1209
                         .doc = "Mandos client -- Get and decrypt"
1441
1210
                         " passwords from a Mandos server" };
1442
 
    ret = argp_parse(&argp, argc, argv,
1443
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1444
 
    switch(ret){
1445
 
    case 0:
1446
 
      break;
1447
 
    case ENOMEM:
1448
 
    default:
1449
 
      errno = ret;
1450
 
      perror_plus("argp_parse");
1451
 
      exitcode = EX_OSERR;
1452
 
      goto end;
1453
 
    case EINVAL:
1454
 
      exitcode = EX_USAGE;
1455
 
      goto end;
1456
 
    }
1457
 
  }
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");
 
1211
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1212
    if(ret == ARGP_ERR_UNKNOWN){
 
1213
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1214
      exitcode = EXIT_FAILURE;
 
1215
      goto end;
1512
1216
    }
1513
1217
  }
1514
1218
  
1527
1231
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1528
1232
      }
1529
1233
      if(interface == NULL){
1530
 
        perror_plus("malloc");
 
1234
        perror("malloc");
1531
1235
        free(direntries);
1532
1236
        exitcode = EXIT_FAILURE;
1533
1237
        goto end;
1548
1252
  mc.simple_poll = avahi_simple_poll_new();
1549
1253
  if(mc.simple_poll == NULL){
1550
1254
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1551
 
    exitcode = EX_UNAVAILABLE;
 
1255
    exitcode = EXIT_FAILURE;
1552
1256
    goto end;
1553
1257
  }
1554
1258
  
1555
1259
  sigemptyset(&sigterm_action.sa_mask);
1556
1260
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1557
1261
  if(ret == -1){
1558
 
    perror_plus("sigaddset");
1559
 
    exitcode = EX_OSERR;
 
1262
    perror("sigaddset");
 
1263
    exitcode = EXIT_FAILURE;
1560
1264
    goto end;
1561
1265
  }
1562
1266
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1563
1267
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
1565
 
    exitcode = EX_OSERR;
 
1268
    perror("sigaddset");
 
1269
    exitcode = EXIT_FAILURE;
1566
1270
    goto end;
1567
1271
  }
1568
1272
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1569
1273
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
1571
 
    exitcode = EX_OSERR;
 
1274
    perror("sigaddset");
 
1275
    exitcode = EXIT_FAILURE;
1572
1276
    goto end;
1573
1277
  }
1574
1278
  /* Need to check if the handler is SIG_IGN before handling:
1577
1281
  */
1578
1282
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1579
1283
  if(ret == -1){
1580
 
    perror_plus("sigaction");
1581
 
    return EX_OSERR;
 
1284
    perror("sigaction");
 
1285
    return EXIT_FAILURE;
1582
1286
  }
1583
1287
  if(old_sigterm_action.sa_handler != SIG_IGN){
1584
1288
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1585
1289
    if(ret == -1){
1586
 
      perror_plus("sigaction");
1587
 
      exitcode = EX_OSERR;
 
1290
      perror("sigaction");
 
1291
      exitcode = EXIT_FAILURE;
1588
1292
      goto end;
1589
1293
    }
1590
1294
  }
1591
1295
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1592
1296
  if(ret == -1){
1593
 
    perror_plus("sigaction");
1594
 
    return EX_OSERR;
 
1297
    perror("sigaction");
 
1298
    return EXIT_FAILURE;
1595
1299
  }
1596
1300
  if(old_sigterm_action.sa_handler != SIG_IGN){
1597
1301
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1598
1302
    if(ret == -1){
1599
 
      perror_plus("sigaction");
1600
 
      exitcode = EX_OSERR;
 
1303
      perror("sigaction");
 
1304
      exitcode = EXIT_FAILURE;
1601
1305
      goto end;
1602
1306
    }
1603
1307
  }
1604
1308
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1605
1309
  if(ret == -1){
1606
 
    perror_plus("sigaction");
1607
 
    return EX_OSERR;
 
1310
    perror("sigaction");
 
1311
    return EXIT_FAILURE;
1608
1312
  }
1609
1313
  if(old_sigterm_action.sa_handler != SIG_IGN){
1610
1314
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1611
1315
    if(ret == -1){
1612
 
      perror_plus("sigaction");
1613
 
      exitcode = EX_OSERR;
 
1316
      perror("sigaction");
 
1317
      exitcode = EXIT_FAILURE;
1614
1318
      goto end;
1615
1319
    }
1616
1320
  }
1620
1324
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1621
1325
    if(if_index == 0){
1622
1326
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1623
 
      exitcode = EX_UNAVAILABLE;
 
1327
      exitcode = EXIT_FAILURE;
1624
1328
      goto end;
1625
1329
    }
1626
1330
    
1632
1336
    errno = 0;
1633
1337
    ret = seteuid(0);
1634
1338
    if(ret == -1){
1635
 
      perror_plus("seteuid");
 
1339
      perror("seteuid");
1636
1340
    }
1637
1341
    
1638
1342
#ifdef __linux__
1639
1343
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1640
 
       messages about the network interface to mess up the prompt */
 
1344
       messages to mess up the prompt */
1641
1345
    ret = klogctl(8, NULL, 5);
1642
1346
    bool restore_loglevel = true;
1643
1347
    if(ret == -1){
1644
1348
      restore_loglevel = false;
1645
 
      perror_plus("klogctl");
 
1349
      perror("klogctl");
1646
1350
    }
1647
1351
#endif  /* __linux__ */
1648
1352
    
1649
1353
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1650
1354
    if(sd < 0){
1651
 
      perror_plus("socket");
1652
 
      exitcode = EX_OSERR;
 
1355
      perror("socket");
 
1356
      exitcode = EXIT_FAILURE;
1653
1357
#ifdef __linux__
1654
1358
      if(restore_loglevel){
1655
1359
        ret = klogctl(7, NULL, 0);
1656
1360
        if(ret == -1){
1657
 
          perror_plus("klogctl");
 
1361
          perror("klogctl");
1658
1362
        }
1659
1363
      }
1660
1364
#endif  /* __linux__ */
1662
1366
      errno = 0;
1663
1367
      ret = seteuid(uid);
1664
1368
      if(ret == -1){
1665
 
        perror_plus("seteuid");
 
1369
        perror("seteuid");
1666
1370
      }
1667
1371
      goto end;
1668
1372
    }
1669
1373
    strcpy(network.ifr_name, interface);
1670
1374
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1671
1375
    if(ret == -1){
1672
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1376
      perror("ioctl SIOCGIFFLAGS");
1673
1377
#ifdef __linux__
1674
1378
      if(restore_loglevel){
1675
1379
        ret = klogctl(7, NULL, 0);
1676
1380
        if(ret == -1){
1677
 
          perror_plus("klogctl");
 
1381
          perror("klogctl");
1678
1382
        }
1679
1383
      }
1680
1384
#endif  /* __linux__ */
1681
 
      exitcode = EX_OSERR;
 
1385
      exitcode = EXIT_FAILURE;
1682
1386
      /* Lower privileges */
1683
1387
      errno = 0;
1684
1388
      ret = seteuid(uid);
1685
1389
      if(ret == -1){
1686
 
        perror_plus("seteuid");
 
1390
        perror("seteuid");
1687
1391
      }
1688
1392
      goto end;
1689
1393
    }
1693
1397
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1694
1398
      if(ret == -1){
1695
1399
        take_down_interface = false;
1696
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1697
 
        exitcode = EX_OSERR;
 
1400
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1401
        exitcode = EXIT_FAILURE;
1698
1402
#ifdef __linux__
1699
1403
        if(restore_loglevel){
1700
1404
          ret = klogctl(7, NULL, 0);
1701
1405
          if(ret == -1){
1702
 
            perror_plus("klogctl");
 
1406
            perror("klogctl");
1703
1407
          }
1704
1408
        }
1705
1409
#endif  /* __linux__ */
1707
1411
        errno = 0;
1708
1412
        ret = seteuid(uid);
1709
1413
        if(ret == -1){
1710
 
          perror_plus("seteuid");
 
1414
          perror("seteuid");
1711
1415
        }
1712
1416
        goto end;
1713
1417
      }
1714
1418
    }
1715
 
    /* Sleep checking until interface is running.
1716
 
       Check every 0.25s, up to total time of delay */
 
1419
    /* sleep checking until interface is running */
1717
1420
    for(int i=0; i < delay * 4; i++){
1718
1421
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1719
1422
      if(ret == -1){
1720
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1423
        perror("ioctl SIOCGIFFLAGS");
1721
1424
      } else if(network.ifr_flags & IFF_RUNNING){
1722
1425
        break;
1723
1426
      }
1724
1427
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1725
1428
      ret = nanosleep(&sleeptime, NULL);
1726
1429
      if(ret == -1 and errno != EINTR){
1727
 
        perror_plus("nanosleep");
 
1430
        perror("nanosleep");
1728
1431
      }
1729
1432
    }
1730
1433
    if(not take_down_interface){
1731
1434
      /* We won't need the socket anymore */
1732
1435
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1733
1436
      if(ret == -1){
1734
 
        perror_plus("close");
 
1437
        perror("close");
1735
1438
      }
1736
1439
    }
1737
1440
#ifdef __linux__
1739
1442
      /* Restores kernel loglevel to default */
1740
1443
      ret = klogctl(7, NULL, 0);
1741
1444
      if(ret == -1){
1742
 
        perror_plus("klogctl");
 
1445
        perror("klogctl");
1743
1446
      }
1744
1447
    }
1745
1448
#endif  /* __linux__ */
1749
1452
      /* Lower privileges */
1750
1453
      ret = seteuid(uid);
1751
1454
      if(ret == -1){
1752
 
        perror_plus("seteuid");
 
1455
        perror("seteuid");
1753
1456
      }
1754
1457
    } else {
1755
1458
      /* Lower privileges permanently */
1756
1459
      ret = setuid(uid);
1757
1460
      if(ret == -1){
1758
 
        perror_plus("setuid");
 
1461
        perror("setuid");
1759
1462
      }
1760
1463
    }
1761
1464
  }
1767
1470
  ret = init_gnutls_global(pubkey, seckey);
1768
1471
  if(ret == -1){
1769
1472
    fprintf(stderr, "init_gnutls_global failed\n");
1770
 
    exitcode = EX_UNAVAILABLE;
 
1473
    exitcode = EXIT_FAILURE;
1771
1474
    goto end;
1772
1475
  } else {
1773
1476
    gnutls_initialized = true;
1777
1480
    goto end;
1778
1481
  }
1779
1482
  
 
1483
  tempdir_created = true;
1780
1484
  if(mkdtemp(tempdir) == NULL){
1781
 
    perror_plus("mkdtemp");
 
1485
    tempdir_created = false;
 
1486
    perror("mkdtemp");
1782
1487
    goto end;
1783
1488
  }
1784
 
  tempdir_created = true;
1785
1489
  
1786
1490
  if(quit_now){
1787
1491
    goto end;
1789
1493
  
1790
1494
  if(not init_gpgme(pubkey, seckey, tempdir)){
1791
1495
    fprintf(stderr, "init_gpgme failed\n");
1792
 
    exitcode = EX_UNAVAILABLE;
 
1496
    exitcode = EXIT_FAILURE;
1793
1497
    goto end;
1794
1498
  } else {
1795
1499
    gpgme_initialized = true;
1805
1509
    char *address = strrchr(connect_to, ':');
1806
1510
    if(address == NULL){
1807
1511
      fprintf(stderr, "No colon in address\n");
1808
 
      exitcode = EX_USAGE;
 
1512
      exitcode = EXIT_FAILURE;
1809
1513
      goto end;
1810
1514
    }
1811
1515
    
1819
1523
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1820
1524
       or tmpmax != (uint16_t)tmpmax){
1821
1525
      fprintf(stderr, "Bad port number\n");
1822
 
      exitcode = EX_USAGE;
 
1526
      exitcode = EXIT_FAILURE;
1823
1527
      goto end;
1824
1528
    }
1825
1529
  
1841
1545
    if(quit_now){
1842
1546
      goto end;
1843
1547
    }
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){
 
1548
    
 
1549
    ret = start_mandos_communication(address, port, if_index, af);
 
1550
    if(ret < 0){
 
1551
      exitcode = EXIT_FAILURE;
 
1552
    } else {
1854
1553
      exitcode = EXIT_SUCCESS;
1855
1554
    }
1856
 
 
1857
1555
    goto end;
1858
1556
  }
1859
1557
  
1883
1581
  if(mc.server == NULL){
1884
1582
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1885
1583
            avahi_strerror(error));
1886
 
    exitcode = EX_UNAVAILABLE;
 
1584
    exitcode = EXIT_FAILURE;
1887
1585
    goto end;
1888
1586
  }
1889
1587
  
1898
1596
  if(sb == NULL){
1899
1597
    fprintf(stderr, "Failed to create service browser: %s\n",
1900
1598
            avahi_strerror(avahi_server_errno(mc.server)));
1901
 
    exitcode = EX_UNAVAILABLE;
 
1599
    exitcode = EXIT_FAILURE;
1902
1600
    goto end;
1903
1601
  }
1904
1602
  
1911
1609
  if(debug){
1912
1610
    fprintf(stderr, "Starting Avahi loop search\n");
1913
1611
  }
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
 
  }
 
1612
  
 
1613
  avahi_simple_poll_loop(mc.simple_poll);
1921
1614
  
1922
1615
 end:
1923
1616
  
1944
1637
  if(gpgme_initialized){
1945
1638
    gpgme_release(mc.ctx);
1946
1639
  }
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
1640
  
1959
1641
  /* Take down the network interface */
1960
1642
  if(take_down_interface){
1962
1644
    errno = 0;
1963
1645
    ret = seteuid(0);
1964
1646
    if(ret == -1){
1965
 
      perror_plus("seteuid");
 
1647
      perror("seteuid");
1966
1648
    }
1967
1649
    if(geteuid() == 0){
1968
1650
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1969
1651
      if(ret == -1){
1970
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1652
        perror("ioctl SIOCGIFFLAGS");
1971
1653
      } else if(network.ifr_flags & IFF_UP) {
1972
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1654
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1973
1655
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1974
1656
        if(ret == -1){
1975
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1657
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1976
1658
        }
1977
1659
      }
1978
1660
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1979
1661
      if(ret == -1){
1980
 
        perror_plus("close");
 
1662
        perror("close");
1981
1663
      }
1982
1664
      /* Lower privileges permanently */
1983
1665
      errno = 0;
1984
1666
      ret = setuid(uid);
1985
1667
      if(ret == -1){
1986
 
        perror_plus("setuid");
 
1668
        perror("setuid");
1987
1669
      }
1988
1670
    }
1989
1671
  }
1990
1672
  
1991
 
  /* Removes the GPGME temp directory and all files inside */
 
1673
  /* Removes the temp directory used by GPGME */
1992
1674
  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];
 
1675
    DIR *d;
 
1676
    struct dirent *direntry;
 
1677
    d = opendir(tempdir);
 
1678
    if(d == NULL){
 
1679
      if(errno != ENOENT){
 
1680
        perror("opendir");
 
1681
      }
 
1682
    } else {
 
1683
      while(true){
 
1684
        direntry = readdir(d);
 
1685
        if(direntry == NULL){
 
1686
          break;
 
1687
        }
 
1688
        /* Skip "." and ".." */
 
1689
        if(direntry->d_name[0] == '.'
 
1690
           and (direntry->d_name[1] == '\0'
 
1691
                or (direntry->d_name[1] == '.'
 
1692
                    and direntry->d_name[2] == '\0'))){
 
1693
          continue;
 
1694
        }
1999
1695
        char *fullname = NULL;
2000
1696
        ret = asprintf(&fullname, "%s/%s", tempdir,
2001
1697
                       direntry->d_name);
2002
1698
        if(ret < 0){
2003
 
          perror_plus("asprintf");
 
1699
          perror("asprintf");
2004
1700
          continue;
2005
1701
        }
2006
1702
        ret = remove(fullname);
2010
1706
        }
2011
1707
        free(fullname);
2012
1708
      }
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");
 
1709
      closedir(d);
2020
1710
    }
2021
1711
    ret = rmdir(tempdir);
2022
1712
    if(ret == -1 and errno != ENOENT){
2023
 
      perror_plus("rmdir");
 
1713
      perror("rmdir");
2024
1714
    }
2025
1715
  }
2026
1716
  
2031
1721
                                            &old_sigterm_action,
2032
1722
                                            NULL));
2033
1723
    if(ret == -1){
2034
 
      perror_plus("sigaction");
 
1724
      perror("sigaction");
2035
1725
    }
2036
1726
    do {
2037
1727
      ret = raise(signal_received);
2038
1728
    } while(ret != 0 and errno == EINTR);
2039
1729
    if(ret != 0){
2040
 
      perror_plus("raise");
 
1730
      perror("raise");
2041
1731
      abort();
2042
1732
    }
2043
1733
    TEMP_FAILURE_RETRY(pause());