/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: Björn Påhlsson
  • Date: 2011-06-23 22:27:15 UTC
  • mto: This revision was merged to the branch mainline in revision 485.
  • Revision ID: belorn@fukt.bsnet.se-20110623222715-q5wro9ma9iyjl367
* Makefile (CFLAGS): Added "-lrt" to include real time library.
* plugins.d/mandos-client.c: use scandir(3) instead of readdir(3)
                             Prefix all debug output with "Mandos plugin " + program_invocation_short_name
                             Retry servers that failed to provide password.
                             New option --retry SECONDS that sets the interval between rechecking.
                             --retry also controls how often it retries a server when using --connect.
* plugins.d/splashy.c:  Prefix all debug output with "Mandos plugin " + program_invocation_short_name
* plugins.d/usplash.c: --||--
* plugins.d/askpass-fifo.c: --||--
* plugins.d/password-prompt.c: --||--
* plugins.d/plymouth.c: --||--
* mandos: Lower logger level from warning to info on failed client requests because client was disabled or unknown fingerprint.
* plugins.d/plymouth.c (get_pid): bug fix. Was not calling free on direntries. 

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