/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-09-19 17:41:18 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090919174118-274yt9wptmtx0ykn
* plugins.d/password-prompt.c (main): Fix "-Wconversion" warning.

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, program_invocation_short_name */
66
 
#include <time.h>               /* nanosleep(), time(), sleep() */
 
65
#include <errno.h>              /* perror(), errno */
 
66
#include <time.h>               /* nanosleep(), time() */
67
67
#include <net/if.h>             /* ioctl, ifreq, SIOCGIFFLAGS, IFF_UP,
68
68
                                   SIOCSIFFLAGS, if_indextoname(),
69
69
                                   if_nametoindex(), IF_NAMESIZE */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
83
                                   sigaction(), SIGTERM, sig_atomic_t,
84
84
                                   raise() */
85
 
#include <sysexits.h>           /* EX_OSERR, EX_USAGE, EX_UNAVAILABLE,
86
 
                                   EX_NOHOST, EX_IOERR, EX_PROTOCOL */
87
85
 
88
86
#ifdef __linux__
89
87
#include <sys/klog.h>           /* klogctl() */
127
125
static const char mandos_protocol_version[] = "1";
128
126
const char *argp_program_version = "mandos-client " VERSION;
129
127
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
130
 
static const char sys_class_net[] = "/sys/class/net";
131
 
char *connect_to = NULL;
132
 
 
133
 
/* Doubly linked list that need to be circular linked when ever used */
134
 
typedef struct server{
135
 
  const char *ip;
136
 
  uint16_t port;
137
 
  AvahiIfIndex if_index;
138
 
  int af;
139
 
  struct timespec last_seen;
140
 
  struct server *next;
141
 
  struct server *prev;
142
 
} server;
143
128
 
144
129
/* Used for passing in values through the Avahi callback functions */
145
130
typedef struct {
150
135
  gnutls_dh_params_t dh_params;
151
136
  const char *priority;
152
137
  gpgme_ctx_t ctx;
153
 
  server *current_server;
154
138
} mandos_context;
155
139
 
156
140
/* global context so signal handler can reach it*/
157
141
mandos_context mc = { .simple_poll = NULL, .server = NULL,
158
142
                      .dh_bits = 1024, .priority = "SECURE256"
159
 
                      ":!CTYPE-X.509:+CTYPE-OPENPGP", .current_server = NULL };
 
143
                      ":!CTYPE-X.509:+CTYPE-OPENPGP" };
160
144
 
161
145
sig_atomic_t quit_now = 0;
162
146
int signal_received = 0;
163
147
 
164
 
/* Function to use when printing errors */
165
 
void perror_plus(const char *print_text){
166
 
  fprintf(stderr, "Mandos plugin %s: ", program_invocation_short_name);
167
 
  perror(print_text);
168
 
}
169
 
 
170
148
/*
171
149
 * Make additional room in "buffer" for at least BUFFER_SIZE more
172
150
 * bytes. "buffer_capacity" is how much is currently allocated,
184
162
  return buffer_capacity;
185
163
}
186
164
 
187
 
int add_server(const char *ip, uint16_t port,
188
 
                 AvahiIfIndex if_index,
189
 
                 int af){
190
 
  int ret;
191
 
  server *new_server = malloc(sizeof(server));
192
 
  if(new_server == NULL){
193
 
    perror_plus("malloc");
194
 
    return -1;
195
 
  }
196
 
  *new_server = (server){ .ip = strdup(ip),
197
 
                         .port = port,
198
 
                         .if_index = if_index,
199
 
                         .af = af };
200
 
  if(new_server->ip == NULL){
201
 
    perror_plus("strdup");
202
 
    return -1;    
203
 
  }
204
 
  /* uniqe case of first server */
205
 
  if (mc.current_server == NULL){
206
 
    new_server->next = new_server;
207
 
    new_server->prev = new_server;
208
 
    mc.current_server = new_server;
209
 
  /* Placing the new server last in the list */
210
 
  } else {
211
 
    new_server->next = mc.current_server;
212
 
    new_server->prev = mc.current_server->prev;
213
 
    new_server->prev->next = new_server;
214
 
    mc.current_server->prev = new_server;
215
 
  }
216
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
217
 
  if(ret == -1){
218
 
    perror_plus("clock_gettime");
219
 
    return -1;
220
 
  }
221
 
  return 0;
222
 
}
223
 
 
224
165
/* 
225
166
 * Initialize GPGME.
226
167
 */
240
181
    
241
182
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
242
183
    if(fd == -1){
243
 
      perror_plus("open");
 
184
      perror("open");
244
185
      return false;
245
186
    }
246
187
    
260
201
    
261
202
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
262
203
    if(ret == -1){
263
 
      perror_plus("close");
 
204
      perror("close");
264
205
    }
265
206
    gpgme_data_release(pgp_data);
266
207
    return true;
391
332
  
392
333
  /* Seek back to the beginning of the GPGME plaintext data buffer */
393
334
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
394
 
    perror_plus("gpgme_data_seek");
 
335
    perror("gpgme_data_seek");
395
336
    plaintext_length = -1;
396
337
    goto decrypt_end;
397
338
  }
402
343
                                      (size_t)plaintext_length,
403
344
                                      plaintext_capacity);
404
345
    if(plaintext_capacity == 0){
405
 
        perror_plus("incbuffer");
 
346
        perror("incbuffer");
406
347
        plaintext_length = -1;
407
348
        goto decrypt_end;
408
349
    }
415
356
      break;
416
357
    }
417
358
    if(ret < 0){
418
 
      perror_plus("gpgme_data_read");
 
359
      perror("gpgme_data_read");
419
360
      plaintext_length = -1;
420
361
      goto decrypt_end;
421
362
    }
611
552
  gnutls_session_t session;
612
553
  int pf;                       /* Protocol family */
613
554
  
614
 
  errno = 0;
615
 
  
616
555
  if(quit_now){
617
 
    errno = EINTR;
618
556
    return -1;
619
557
  }
620
558
  
627
565
    break;
628
566
  default:
629
567
    fprintf(stderr, "Bad address family: %d\n", af);
630
 
    errno = EINVAL;
631
568
    return -1;
632
569
  }
633
570
  
643
580
  
644
581
  tcp_sd = socket(pf, SOCK_STREAM, 0);
645
582
  if(tcp_sd < 0){
646
 
    int e = errno;
647
 
    perror_plus("socket");
648
 
    errno = e;
 
583
    perror("socket");
649
584
    goto mandos_end;
650
585
  }
651
586
  
652
587
  if(quit_now){
653
 
    errno = EINTR;
654
588
    goto mandos_end;
655
589
  }
656
590
  
663
597
    ret = inet_pton(af, ip, &to.in.sin_addr);
664
598
  }
665
599
  if(ret < 0 ){
666
 
    int e = errno;
667
 
    perror_plus("inet_pton");
668
 
    errno = e;
 
600
    perror("inet_pton");
669
601
    goto mandos_end;
670
602
  }
671
603
  if(ret == 0){
672
 
    int e = errno;
673
604
    fprintf(stderr, "Bad address: %s\n", ip);
674
 
    errno = e;
675
605
    goto mandos_end;
676
606
  }
677
607
  if(af == AF_INET6){
685
615
      if(if_index == AVAHI_IF_UNSPEC){
686
616
        fprintf(stderr, "An IPv6 link-local address is incomplete"
687
617
                " without a network interface\n");
688
 
        errno = EINVAL;
689
618
        goto mandos_end;
690
619
      }
691
620
      /* Set the network interface number as scope */
698
627
  }
699
628
  
700
629
  if(quit_now){
701
 
    errno = EINTR;
702
630
    goto mandos_end;
703
631
  }
704
632
  
706
634
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
707
635
      char interface[IF_NAMESIZE];
708
636
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
709
 
        perror_plus("if_indextoname");
 
637
        perror("if_indextoname");
710
638
      } else {
711
639
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
712
640
                ip, interface, port);
726
654
                        sizeof(addrstr));
727
655
    }
728
656
    if(pcret == NULL){
729
 
      perror_plus("inet_ntop");
 
657
      perror("inet_ntop");
730
658
    } else {
731
659
      if(strcmp(addrstr, ip) != 0){
732
660
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
735
663
  }
736
664
  
737
665
  if(quit_now){
738
 
    errno = EINTR;
739
666
    goto mandos_end;
740
667
  }
741
668
  
745
672
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
746
673
  }
747
674
  if(ret < 0){
748
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
749
 
      int e = errno;
750
 
      perror_plus("connect");
751
 
      errno = e;
752
 
    }
 
675
    perror("connect");
753
676
    goto mandos_end;
754
677
  }
755
678
  
756
679
  if(quit_now){
757
 
    errno = EINTR;
758
680
    goto mandos_end;
759
681
  }
760
682
  
765
687
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
766
688
                                   out_size - written));
767
689
    if(ret == -1){
768
 
      int e = errno;
769
 
      perror_plus("write");
770
 
      errno = e;
 
690
      perror("write");
771
691
      goto mandos_end;
772
692
    }
773
693
    written += (size_t)ret;
783
703
    }
784
704
  
785
705
    if(quit_now){
786
 
      errno = EINTR;
787
706
      goto mandos_end;
788
707
    }
789
708
  }
793
712
  }
794
713
  
795
714
  if(quit_now){
796
 
    errno = EINTR;
797
715
    goto mandos_end;
798
716
  }
799
 
 
800
 
  /* Spurious warnings from -Wint-to-pointer-cast */
 
717
  
801
718
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
802
719
  
803
720
  if(quit_now){
804
 
    errno = EINTR;
805
721
    goto mandos_end;
806
722
  }
807
723
  
808
724
  do {
809
725
    ret = gnutls_handshake(session);
810
726
    if(quit_now){
811
 
      errno = EINTR;
812
727
      goto mandos_end;
813
728
    }
814
729
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
818
733
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
819
734
      gnutls_perror(ret);
820
735
    }
821
 
    errno = EPROTO;
822
736
    goto mandos_end;
823
737
  }
824
738
  
832
746
  while(true){
833
747
    
834
748
    if(quit_now){
835
 
      errno = EINTR;
836
749
      goto mandos_end;
837
750
    }
838
751
    
839
752
    buffer_capacity = incbuffer(&buffer, buffer_length,
840
753
                                   buffer_capacity);
841
754
    if(buffer_capacity == 0){
842
 
      int e = errno;
843
 
      perror_plus("incbuffer");
844
 
      errno = e;
 
755
      perror("incbuffer");
845
756
      goto mandos_end;
846
757
    }
847
758
    
848
759
    if(quit_now){
849
 
      errno = EINTR;
850
760
      goto mandos_end;
851
761
    }
852
762
    
865
775
          ret = gnutls_handshake(session);
866
776
          
867
777
          if(quit_now){
868
 
            errno = EINTR;
869
778
            goto mandos_end;
870
779
          }
871
780
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
872
781
        if(ret < 0){
873
782
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
874
783
          gnutls_perror(ret);
875
 
          errno = EPROTO;
876
784
          goto mandos_end;
877
785
        }
878
786
        break;
880
788
        fprintf(stderr, "Unknown error while reading data from"
881
789
                " encrypted session with Mandos server\n");
882
790
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
883
 
        errno = EIO;
884
791
        goto mandos_end;
885
792
      }
886
793
    } else {
893
800
  }
894
801
  
895
802
  if(quit_now){
896
 
    errno = EINTR;
897
803
    goto mandos_end;
898
804
  }
899
805
  
900
806
  do {
901
807
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
902
808
    if(quit_now){
903
 
      errno = EINTR;
904
809
      goto mandos_end;
905
810
    }
906
811
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
915
820
      written = 0;
916
821
      while(written < (size_t) decrypted_buffer_size){
917
822
        if(quit_now){
918
 
          errno = EINTR;
919
823
          goto mandos_end;
920
824
        }
921
825
        
923
827
                          (size_t)decrypted_buffer_size - written,
924
828
                          stdout);
925
829
        if(ret == 0 and ferror(stdout)){
926
 
          int e = errno;
927
830
          if(debug){
928
831
            fprintf(stderr, "Error writing encrypted data: %s\n",
929
832
                    strerror(errno));
930
833
          }
931
 
          errno = e;
932
834
          goto mandos_end;
933
835
        }
934
836
        written += (size_t)ret;
940
842
  /* Shutdown procedure */
941
843
  
942
844
 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
 
    }
 
845
  free(decrypted_buffer);
 
846
  free(buffer);
 
847
  if(tcp_sd >= 0){
 
848
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
849
  }
 
850
  if(ret == -1){
 
851
    perror("close");
 
852
  }
 
853
  gnutls_deinit(session);
 
854
  if(quit_now){
 
855
    retval = -1;
962
856
  }
963
857
  return retval;
964
858
}
1007
901
                                           avahi_proto_to_af(proto));
1008
902
      if(ret == 0){
1009
903
        avahi_simple_poll_quit(mc.simple_poll);
1010
 
      } else {
1011
 
        ret = add_server(ip, port, interface,
1012
 
                         avahi_proto_to_af(proto));
1013
904
      }
1014
905
    }
1015
906
  }
1069
960
  }
1070
961
}
1071
962
 
1072
 
/* Signal handler that stops main loop after sigterm has been called */
 
963
/* stop main loop after sigterm has been called */
1073
964
static void handle_sigterm(int sig){
1074
965
  if(quit_now){
1075
966
    return;
1077
968
  quit_now = 1;
1078
969
  signal_received = sig;
1079
970
  int old_errno = errno;
1080
 
  /* set main loop to exit */
1081
971
  if(mc.simple_poll != NULL){
1082
972
    avahi_simple_poll_quit(mc.simple_poll);
1083
973
  }
1084
974
  errno = old_errno;
1085
975
}
1086
976
 
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 = (sizeof(ifreq_flags)*2)+3; /* "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, "Wait until first server is found. No timeout!\n");     
1213
 
      }
1214
 
      ret = avahi_simple_poll_iterate(s, -1);
1215
 
    } else {
1216
 
      if (debug){
1217
 
        fprintf(stderr, "Check current_server if we should run it, or wait\n"); 
1218
 
      }
1219
 
      /* the current time */
1220
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1221
 
      if(ret == -1){
1222
 
        perror_plus("clock_gettime");
1223
 
        return -1;
1224
 
      }
1225
 
      /* Calculating in ms how long time between now and server
1226
 
         who we visted longest time ago. Now - last seen.  */
1227
 
      waited_time.tv_sec = now.tv_sec - mc.current_server->last_seen.tv_sec;
1228
 
      waited_time.tv_nsec = now.tv_nsec - mc.current_server->last_seen.tv_nsec;
1229
 
      /* total time is 10s/10000ms. Converting to s to ms by 1000/s, and ns to ms by divind by 1000000. */
1230
 
      block_time = (retry_interval - ((intmax_t)waited_time.tv_sec * 1000)) - ((intmax_t)waited_time.tv_nsec / 1000000);
1231
 
 
1232
 
      if (debug){
1233
 
        fprintf(stderr, "Blocking for %ld ms\n", block_time);   
1234
 
      }
1235
 
 
1236
 
      if(block_time <= 0){
1237
 
        ret = start_mandos_communication(mc.current_server->ip,
1238
 
                                   mc.current_server->port,
1239
 
                                   mc.current_server->if_index,
1240
 
                                   mc.current_server->af);
1241
 
        if(ret == 0){
1242
 
          avahi_simple_poll_quit(mc.simple_poll);
1243
 
          return 0;
1244
 
        }
1245
 
        ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
1246
 
        if(ret == -1){
1247
 
          perror_plus("clock_gettime");
1248
 
          return -1;
1249
 
        }
1250
 
        mc.current_server = mc.current_server->next;
1251
 
        block_time = 0;         /* call avahi to find new mandos servers, but dont block */
1252
 
      }
1253
 
      
1254
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1255
 
    }
1256
 
    if(ret != 0){
1257
 
      if (ret > 0 or errno != EINTR) {
1258
 
        return (ret != 1) ? ret : 0;
1259
 
      }
1260
 
    }
1261
 
  }
1262
 
}
1263
 
 
1264
977
int main(int argc, char *argv[]){
1265
978
  AvahiSServiceBrowser *sb = NULL;
1266
979
  int error;
1268
981
  intmax_t tmpmax;
1269
982
  char *tmp;
1270
983
  int exitcode = EXIT_SUCCESS;
1271
 
  const char *interface = "";
 
984
  const char *interface = "eth0";
1272
985
  struct ifreq network;
1273
986
  int sd = -1;
1274
987
  bool take_down_interface = false;
1275
988
  uid_t uid;
1276
989
  gid_t gid;
 
990
  char *connect_to = NULL;
1277
991
  char tempdir[] = "/tmp/mandosXXXXXX";
1278
992
  bool tempdir_created = false;
1279
993
  AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1283
997
  bool gnutls_initialized = false;
1284
998
  bool gpgme_initialized = false;
1285
999
  float delay = 2.5f;
1286
 
  double retry_interval = 10; /* 10s between retrying a server and checking again*/
1287
1000
  
1288
1001
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1289
1002
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1295
1008
  errno = 0;
1296
1009
  ret = setgid(gid);
1297
1010
  if(ret == -1){
1298
 
    perror_plus("setgid");
 
1011
    perror("setgid");
1299
1012
  }
1300
1013
  
1301
1014
  /* Lower user privileges (temporarily) */
1302
1015
  errno = 0;
1303
1016
  ret = seteuid(uid);
1304
1017
  if(ret == -1){
1305
 
    perror_plus("seteuid");
 
1018
    perror("seteuid");
1306
1019
  }
1307
1020
  
1308
1021
  if(quit_now){
1343
1056
        .arg = "SECONDS",
1344
1057
        .doc = "Maximum delay to wait for interface startup",
1345
1058
        .group = 2 },
1346
 
      { .name = "retry", .key = 132,
1347
 
        .arg = "SECONDS",
1348
 
        .doc = "Retry interval used when denied by the mandos server",
1349
 
        .group = 2 },
1350
 
      /*
1351
 
       * These reproduce what we would get without ARGP_NO_HELP
1352
 
       */
1353
 
      { .name = "help", .key = '?',
1354
 
        .doc = "Give this help list", .group = -1 },
1355
 
      { .name = "usage", .key = -3,
1356
 
        .doc = "Give a short usage message", .group = -1 },
1357
 
      { .name = "version", .key = 'V',
1358
 
        .doc = "Print program version", .group = -1 },
1359
1059
      { .name = NULL }
1360
1060
    };
1361
1061
    
1362
1062
    error_t parse_opt(int key, char *arg,
1363
1063
                      struct argp_state *state){
1364
 
      errno = 0;
1365
1064
      switch(key){
1366
1065
      case 128:                 /* --debug */
1367
1066
        debug = true;
1383
1082
        tmpmax = strtoimax(arg, &tmp, 10);
1384
1083
        if(errno != 0 or tmp == arg or *tmp != '\0'
1385
1084
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1386
 
          argp_error(state, "Bad number of DH bits");
 
1085
          fprintf(stderr, "Bad number of DH bits\n");
 
1086
          exit(EXIT_FAILURE);
1387
1087
        }
1388
1088
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1389
1089
        break;
1394
1094
        errno = 0;
1395
1095
        delay = strtof(arg, &tmp);
1396
1096
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1397
 
          argp_error(state, "Bad delay");
1398
 
        }
1399
 
      case 132:                 /* --retry */
1400
 
        errno = 0;
1401
 
        retry_interval = strtod(arg, &tmp);
1402
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1403
 
           or (retry_interval * 1000) > INT_MAX){
1404
 
          argp_error(state, "Bad retry interval");
 
1097
          fprintf(stderr, "Bad delay\n");
 
1098
          exit(EXIT_FAILURE);
1405
1099
        }
1406
1100
        break;
1407
 
        /*
1408
 
         * These reproduce what we would get without ARGP_NO_HELP
1409
 
         */
1410
 
      case '?':                 /* --help */
1411
 
        argp_state_help(state, state->out_stream,
1412
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1413
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1414
 
      case -3:                  /* --usage */
1415
 
        argp_state_help(state, state->out_stream,
1416
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1417
 
      case 'V':                 /* --version */
1418
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1419
 
        exit(argp_err_exit_status);
 
1101
      case ARGP_KEY_ARG:
 
1102
        argp_usage(state);
 
1103
      case ARGP_KEY_END:
1420
1104
        break;
1421
1105
      default:
1422
1106
        return ARGP_ERR_UNKNOWN;
1423
1107
      }
1424
 
      return errno;
 
1108
      return 0;
1425
1109
    }
1426
1110
    
1427
1111
    struct argp argp = { .options = options, .parser = parse_opt,
1428
1112
                         .args_doc = "",
1429
1113
                         .doc = "Mandos client -- Get and decrypt"
1430
1114
                         " passwords from a Mandos server" };
1431
 
    ret = argp_parse(&argp, argc, argv,
1432
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1433
 
    switch(ret){
1434
 
    case 0:
1435
 
      break;
1436
 
    case ENOMEM:
1437
 
    default:
1438
 
      errno = ret;
1439
 
      perror_plus("argp_parse");
1440
 
      exitcode = EX_OSERR;
1441
 
      goto end;
1442
 
    case EINVAL:
1443
 
      exitcode = EX_USAGE;
 
1115
    ret = argp_parse(&argp, argc, argv, 0, 0, NULL);
 
1116
    if(ret == ARGP_ERR_UNKNOWN){
 
1117
      fprintf(stderr, "Unknown error while parsing arguments\n");
 
1118
      exitcode = EXIT_FAILURE;
1444
1119
      goto end;
1445
1120
    }
1446
1121
  }
1448
1123
  if(not debug){
1449
1124
    avahi_set_log_function(empty_log);
1450
1125
  }
1451
 
 
1452
 
  if(interface[0] == '\0'){
1453
 
    struct dirent **direntries;
1454
 
    ret = scandir(sys_class_net, &direntries, good_interface,
1455
 
                  alphasort);
1456
 
    if(ret >= 1){
1457
 
      /* Pick the first good interface */
1458
 
      interface = strdup(direntries[0]->d_name);
1459
 
      if(debug){
1460
 
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1461
 
      }
1462
 
      if(interface == NULL){
1463
 
        perror_plus("malloc");
1464
 
        free(direntries);
1465
 
        exitcode = EXIT_FAILURE;
1466
 
        goto end;
1467
 
      }
1468
 
      free(direntries);
1469
 
    } else {
1470
 
      free(direntries);
1471
 
      fprintf(stderr, "Could not find a network interface\n");
1472
 
      exitcode = EXIT_FAILURE;
1473
 
      goto end;
1474
 
    }
1475
 
  }
1476
1126
  
1477
1127
  /* Initialize Avahi early so avahi_simple_poll_quit() can be called
1478
1128
     from the signal handler */
1481
1131
  mc.simple_poll = avahi_simple_poll_new();
1482
1132
  if(mc.simple_poll == NULL){
1483
1133
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1484
 
    exitcode = EX_UNAVAILABLE;
 
1134
    exitcode = EXIT_FAILURE;
1485
1135
    goto end;
1486
1136
  }
1487
1137
  
1488
1138
  sigemptyset(&sigterm_action.sa_mask);
1489
1139
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1490
1140
  if(ret == -1){
1491
 
    perror_plus("sigaddset");
1492
 
    exitcode = EX_OSERR;
 
1141
    perror("sigaddset");
 
1142
    exitcode = EXIT_FAILURE;
1493
1143
    goto end;
1494
1144
  }
1495
1145
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1496
1146
  if(ret == -1){
1497
 
    perror_plus("sigaddset");
1498
 
    exitcode = EX_OSERR;
 
1147
    perror("sigaddset");
 
1148
    exitcode = EXIT_FAILURE;
1499
1149
    goto end;
1500
1150
  }
1501
1151
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1502
1152
  if(ret == -1){
1503
 
    perror_plus("sigaddset");
1504
 
    exitcode = EX_OSERR;
 
1153
    perror("sigaddset");
 
1154
    exitcode = EXIT_FAILURE;
1505
1155
    goto end;
1506
1156
  }
1507
1157
  /* Need to check if the handler is SIG_IGN before handling:
1510
1160
  */
1511
1161
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1512
1162
  if(ret == -1){
1513
 
    perror_plus("sigaction");
1514
 
    return EX_OSERR;
 
1163
    perror("sigaction");
 
1164
    return EXIT_FAILURE;
1515
1165
  }
1516
1166
  if(old_sigterm_action.sa_handler != SIG_IGN){
1517
1167
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1518
1168
    if(ret == -1){
1519
 
      perror_plus("sigaction");
1520
 
      exitcode = EX_OSERR;
 
1169
      perror("sigaction");
 
1170
      exitcode = EXIT_FAILURE;
1521
1171
      goto end;
1522
1172
    }
1523
1173
  }
1524
1174
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1525
1175
  if(ret == -1){
1526
 
    perror_plus("sigaction");
1527
 
    return EX_OSERR;
 
1176
    perror("sigaction");
 
1177
    return EXIT_FAILURE;
1528
1178
  }
1529
1179
  if(old_sigterm_action.sa_handler != SIG_IGN){
1530
1180
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1531
1181
    if(ret == -1){
1532
 
      perror_plus("sigaction");
1533
 
      exitcode = EX_OSERR;
 
1182
      perror("sigaction");
 
1183
      exitcode = EXIT_FAILURE;
1534
1184
      goto end;
1535
1185
    }
1536
1186
  }
1537
1187
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1538
1188
  if(ret == -1){
1539
 
    perror_plus("sigaction");
1540
 
    return EX_OSERR;
 
1189
    perror("sigaction");
 
1190
    return EXIT_FAILURE;
1541
1191
  }
1542
1192
  if(old_sigterm_action.sa_handler != SIG_IGN){
1543
1193
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1544
1194
    if(ret == -1){
1545
 
      perror_plus("sigaction");
1546
 
      exitcode = EX_OSERR;
 
1195
      perror("sigaction");
 
1196
      exitcode = EXIT_FAILURE;
1547
1197
      goto end;
1548
1198
    }
1549
1199
  }
1550
1200
  
1551
1201
  /* If the interface is down, bring it up */
1552
 
  if(strcmp(interface, "none") != 0){
 
1202
  if(interface[0] != '\0'){
1553
1203
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1554
1204
    if(if_index == 0){
1555
1205
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1556
 
      exitcode = EX_UNAVAILABLE;
 
1206
      exitcode = EXIT_FAILURE;
1557
1207
      goto end;
1558
1208
    }
1559
1209
    
1565
1215
    errno = 0;
1566
1216
    ret = seteuid(0);
1567
1217
    if(ret == -1){
1568
 
      perror_plus("seteuid");
 
1218
      perror("seteuid");
1569
1219
    }
1570
1220
    
1571
1221
#ifdef __linux__
1572
1222
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1573
 
       messages about the network interface to mess up the prompt */
 
1223
       messages to mess up the prompt */
1574
1224
    ret = klogctl(8, NULL, 5);
1575
1225
    bool restore_loglevel = true;
1576
1226
    if(ret == -1){
1577
1227
      restore_loglevel = false;
1578
 
      perror_plus("klogctl");
 
1228
      perror("klogctl");
1579
1229
    }
1580
1230
#endif  /* __linux__ */
1581
1231
    
1582
1232
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1583
1233
    if(sd < 0){
1584
 
      perror_plus("socket");
1585
 
      exitcode = EX_OSERR;
 
1234
      perror("socket");
 
1235
      exitcode = EXIT_FAILURE;
1586
1236
#ifdef __linux__
1587
1237
      if(restore_loglevel){
1588
1238
        ret = klogctl(7, NULL, 0);
1589
1239
        if(ret == -1){
1590
 
          perror_plus("klogctl");
 
1240
          perror("klogctl");
1591
1241
        }
1592
1242
      }
1593
1243
#endif  /* __linux__ */
1595
1245
      errno = 0;
1596
1246
      ret = seteuid(uid);
1597
1247
      if(ret == -1){
1598
 
        perror_plus("seteuid");
 
1248
        perror("seteuid");
1599
1249
      }
1600
1250
      goto end;
1601
1251
    }
1602
1252
    strcpy(network.ifr_name, interface);
1603
1253
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1604
1254
    if(ret == -1){
1605
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1255
      perror("ioctl SIOCGIFFLAGS");
1606
1256
#ifdef __linux__
1607
1257
      if(restore_loglevel){
1608
1258
        ret = klogctl(7, NULL, 0);
1609
1259
        if(ret == -1){
1610
 
          perror_plus("klogctl");
 
1260
          perror("klogctl");
1611
1261
        }
1612
1262
      }
1613
1263
#endif  /* __linux__ */
1614
 
      exitcode = EX_OSERR;
 
1264
      exitcode = EXIT_FAILURE;
1615
1265
      /* Lower privileges */
1616
1266
      errno = 0;
1617
1267
      ret = seteuid(uid);
1618
1268
      if(ret == -1){
1619
 
        perror_plus("seteuid");
 
1269
        perror("seteuid");
1620
1270
      }
1621
1271
      goto end;
1622
1272
    }
1626
1276
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1627
1277
      if(ret == -1){
1628
1278
        take_down_interface = false;
1629
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1630
 
        exitcode = EX_OSERR;
 
1279
        perror("ioctl SIOCSIFFLAGS");
 
1280
        exitcode = EXIT_FAILURE;
1631
1281
#ifdef __linux__
1632
1282
        if(restore_loglevel){
1633
1283
          ret = klogctl(7, NULL, 0);
1634
1284
          if(ret == -1){
1635
 
            perror_plus("klogctl");
 
1285
            perror("klogctl");
1636
1286
          }
1637
1287
        }
1638
1288
#endif  /* __linux__ */
1640
1290
        errno = 0;
1641
1291
        ret = seteuid(uid);
1642
1292
        if(ret == -1){
1643
 
          perror_plus("seteuid");
 
1293
          perror("seteuid");
1644
1294
        }
1645
1295
        goto end;
1646
1296
      }
1647
1297
    }
1648
 
    /* sleep checking until interface is running. Check every 0.25s, up to total time of delay */
 
1298
    /* sleep checking until interface is running */
1649
1299
    for(int i=0; i < delay * 4; i++){
1650
1300
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1651
1301
      if(ret == -1){
1652
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1302
        perror("ioctl SIOCGIFFLAGS");
1653
1303
      } else if(network.ifr_flags & IFF_RUNNING){
1654
1304
        break;
1655
1305
      }
1656
1306
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1657
1307
      ret = nanosleep(&sleeptime, NULL);
1658
1308
      if(ret == -1 and errno != EINTR){
1659
 
        perror_plus("nanosleep");
 
1309
        perror("nanosleep");
1660
1310
      }
1661
1311
    }
1662
1312
    if(not take_down_interface){
1663
1313
      /* We won't need the socket anymore */
1664
1314
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1665
1315
      if(ret == -1){
1666
 
        perror_plus("close");
 
1316
        perror("close");
1667
1317
      }
1668
1318
    }
1669
1319
#ifdef __linux__
1671
1321
      /* Restores kernel loglevel to default */
1672
1322
      ret = klogctl(7, NULL, 0);
1673
1323
      if(ret == -1){
1674
 
        perror_plus("klogctl");
 
1324
        perror("klogctl");
1675
1325
      }
1676
1326
    }
1677
1327
#endif  /* __linux__ */
1681
1331
      /* Lower privileges */
1682
1332
      ret = seteuid(uid);
1683
1333
      if(ret == -1){
1684
 
        perror_plus("seteuid");
 
1334
        perror("seteuid");
1685
1335
      }
1686
1336
    } else {
1687
1337
      /* Lower privileges permanently */
1688
1338
      ret = setuid(uid);
1689
1339
      if(ret == -1){
1690
 
        perror_plus("setuid");
 
1340
        perror("setuid");
1691
1341
      }
1692
1342
    }
1693
1343
  }
1699
1349
  ret = init_gnutls_global(pubkey, seckey);
1700
1350
  if(ret == -1){
1701
1351
    fprintf(stderr, "init_gnutls_global failed\n");
1702
 
    exitcode = EX_UNAVAILABLE;
 
1352
    exitcode = EXIT_FAILURE;
1703
1353
    goto end;
1704
1354
  } else {
1705
1355
    gnutls_initialized = true;
1709
1359
    goto end;
1710
1360
  }
1711
1361
  
 
1362
  tempdir_created = true;
1712
1363
  if(mkdtemp(tempdir) == NULL){
1713
 
    perror_plus("mkdtemp");
 
1364
    tempdir_created = false;
 
1365
    perror("mkdtemp");
1714
1366
    goto end;
1715
1367
  }
1716
 
  tempdir_created = true;
1717
1368
  
1718
1369
  if(quit_now){
1719
1370
    goto end;
1721
1372
  
1722
1373
  if(not init_gpgme(pubkey, seckey, tempdir)){
1723
1374
    fprintf(stderr, "init_gpgme failed\n");
1724
 
    exitcode = EX_UNAVAILABLE;
 
1375
    exitcode = EXIT_FAILURE;
1725
1376
    goto end;
1726
1377
  } else {
1727
1378
    gpgme_initialized = true;
1737
1388
    char *address = strrchr(connect_to, ':');
1738
1389
    if(address == NULL){
1739
1390
      fprintf(stderr, "No colon in address\n");
1740
 
      exitcode = EX_USAGE;
 
1391
      exitcode = EXIT_FAILURE;
1741
1392
      goto end;
1742
1393
    }
1743
1394
    
1751
1402
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1752
1403
       or tmpmax != (uint16_t)tmpmax){
1753
1404
      fprintf(stderr, "Bad port number\n");
1754
 
      exitcode = EX_USAGE;
 
1405
      exitcode = EXIT_FAILURE;
1755
1406
      goto end;
1756
1407
    }
1757
1408
  
1773
1424
    if(quit_now){
1774
1425
      goto end;
1775
1426
    }
1776
 
 
1777
 
    while(not quit_now){
1778
 
      ret = start_mandos_communication(address, port, if_index, af);
1779
 
      if(quit_now or ret == 0){
1780
 
        break;
1781
 
      }
1782
 
      sleep((int)retry_interval or 1);
1783
 
    };
1784
 
 
1785
 
    if (not quit_now){
 
1427
    
 
1428
    ret = start_mandos_communication(address, port, if_index, af);
 
1429
    if(ret < 0){
 
1430
      exitcode = EXIT_FAILURE;
 
1431
    } else {
1786
1432
      exitcode = EXIT_SUCCESS;
1787
1433
    }
1788
 
 
1789
1434
    goto end;
1790
1435
  }
1791
1436
  
1815
1460
  if(mc.server == NULL){
1816
1461
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1817
1462
            avahi_strerror(error));
1818
 
    exitcode = EX_UNAVAILABLE;
 
1463
    exitcode = EXIT_FAILURE;
1819
1464
    goto end;
1820
1465
  }
1821
1466
  
1830
1475
  if(sb == NULL){
1831
1476
    fprintf(stderr, "Failed to create service browser: %s\n",
1832
1477
            avahi_strerror(avahi_server_errno(mc.server)));
1833
 
    exitcode = EX_UNAVAILABLE;
 
1478
    exitcode = EXIT_FAILURE;
1834
1479
    goto end;
1835
1480
  }
1836
1481
  
1843
1488
  if(debug){
1844
1489
    fprintf(stderr, "Starting Avahi loop search\n");
1845
1490
  }
1846
 
 
1847
 
  ret = avahi_loop_with_timeout(mc.simple_poll, (int)(retry_interval * 1000));
1848
 
  if(debug){
1849
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1850
 
            (ret == 0) ? "successfully" : "with error");
1851
 
  }
 
1491
  
 
1492
  avahi_simple_poll_loop(mc.simple_poll);
1852
1493
  
1853
1494
 end:
1854
1495
  
1875
1516
  if(gpgme_initialized){
1876
1517
    gpgme_release(mc.ctx);
1877
1518
  }
1878
 
 
1879
 
  /* cleans up the circular linked list of mandos servers the client has seen */
1880
 
  if(mc.current_server != NULL){
1881
 
    mc.current_server->prev->next = NULL;
1882
 
    while(mc.current_server != NULL){
1883
 
      server *next = mc.current_server->next;
1884
 
      free(mc.current_server);
1885
 
      mc.current_server = next;
1886
 
    }
1887
 
  }
1888
1519
  
1889
1520
  /* Take down the network interface */
1890
1521
  if(take_down_interface){
1892
1523
    errno = 0;
1893
1524
    ret = seteuid(0);
1894
1525
    if(ret == -1){
1895
 
      perror_plus("seteuid");
 
1526
      perror("seteuid");
1896
1527
    }
1897
1528
    if(geteuid() == 0){
1898
1529
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1899
1530
      if(ret == -1){
1900
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1531
        perror("ioctl SIOCGIFFLAGS");
1901
1532
      } else if(network.ifr_flags & IFF_UP) {
1902
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1533
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1903
1534
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1904
1535
        if(ret == -1){
1905
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1536
          perror("ioctl SIOCSIFFLAGS");
1906
1537
        }
1907
1538
      }
1908
1539
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1909
1540
      if(ret == -1){
1910
 
        perror_plus("close");
 
1541
        perror("close");
1911
1542
      }
1912
1543
      /* Lower privileges permanently */
1913
1544
      errno = 0;
1914
1545
      ret = setuid(uid);
1915
1546
      if(ret == -1){
1916
 
        perror_plus("setuid");
 
1547
        perror("setuid");
1917
1548
      }
1918
1549
    }
1919
1550
  }
1920
1551
  
1921
 
  /* Removes the GPGME temp directory and all files inside */
 
1552
  /* Removes the temp directory used by GPGME */
1922
1553
  if(tempdir_created){
1923
 
    struct dirent **direntries = NULL;
1924
 
    struct dirent *direntry = NULL;
1925
 
    ret = scandir(tempdir, &direntries, notdotentries, alphasort);
1926
 
    if (ret > 0){
1927
 
      for(int i = 0; i < ret; i++){
1928
 
        direntry = direntries[i];
 
1554
    DIR *d;
 
1555
    struct dirent *direntry;
 
1556
    d = opendir(tempdir);
 
1557
    if(d == NULL){
 
1558
      if(errno != ENOENT){
 
1559
        perror("opendir");
 
1560
      }
 
1561
    } else {
 
1562
      while(true){
 
1563
        direntry = readdir(d);
 
1564
        if(direntry == NULL){
 
1565
          break;
 
1566
        }
 
1567
        /* Skip "." and ".." */
 
1568
        if(direntry->d_name[0] == '.'
 
1569
           and (direntry->d_name[1] == '\0'
 
1570
                or (direntry->d_name[1] == '.'
 
1571
                    and direntry->d_name[2] == '\0'))){
 
1572
          continue;
 
1573
        }
1929
1574
        char *fullname = NULL;
1930
1575
        ret = asprintf(&fullname, "%s/%s", tempdir,
1931
1576
                       direntry->d_name);
1932
1577
        if(ret < 0){
1933
 
          perror_plus("asprintf");
 
1578
          perror("asprintf");
1934
1579
          continue;
1935
1580
        }
1936
1581
        ret = remove(fullname);
1940
1585
        }
1941
1586
        free(fullname);
1942
1587
      }
1943
 
    }
1944
 
 
1945
 
    /* need to be cleaned even if ret == 0 because man page dont specify */
1946
 
    free(direntries);
1947
 
    if (ret == -1){
1948
 
      perror_plus("scandir");
 
1588
      closedir(d);
1949
1589
    }
1950
1590
    ret = rmdir(tempdir);
1951
1591
    if(ret == -1 and errno != ENOENT){
1952
 
      perror_plus("rmdir");
 
1592
      perror("rmdir");
1953
1593
    }
1954
1594
  }
1955
1595
  
1960
1600
                                            &old_sigterm_action,
1961
1601
                                            NULL));
1962
1602
    if(ret == -1){
1963
 
      perror_plus("sigaction");
 
1603
      perror("sigaction");
1964
1604
    }
1965
1605
    do {
1966
1606
      ret = raise(signal_received);
1967
1607
    } while(ret != 0 and errno == EINTR);
1968
1608
    if(ret != 0){
1969
 
      perror_plus("raise");
 
1609
      perror("raise");
1970
1610
      abort();
1971
1611
    }
1972
1612
    TEMP_FAILURE_RETRY(pause());