/mandos/release

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

« 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: (237.2.182 mandos-local)
  • mto: This revision was merged to the branch mainline in revision 270.
  • 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
 
/* Add server to set of servers to retry periodically */
191
 
int add_server(const char *ip, uint16_t port,
192
 
                 AvahiIfIndex if_index,
193
 
                 int af){
194
 
  int ret;
195
 
  server *new_server = malloc(sizeof(server));
196
 
  if(new_server == NULL){
197
 
    perror_plus("malloc");
198
 
    return -1;
199
 
  }
200
 
  *new_server = (server){ .ip = strdup(ip),
201
 
                         .port = port,
202
 
                         .if_index = if_index,
203
 
                         .af = af };
204
 
  if(new_server->ip == NULL){
205
 
    perror_plus("strdup");
206
 
    return -1;
207
 
  }
208
 
  /* Special case of first server */
209
 
  if (mc.current_server == NULL){
210
 
    new_server->next = new_server;
211
 
    new_server->prev = new_server;
212
 
    mc.current_server = new_server;
213
 
  /* Place the new server last in the list */
214
 
  } else {
215
 
    new_server->next = mc.current_server;
216
 
    new_server->prev = mc.current_server->prev;
217
 
    new_server->prev->next = new_server;
218
 
    mc.current_server->prev = new_server;
219
 
  }
220
 
  ret = clock_gettime(CLOCK_MONOTONIC, &mc.current_server->last_seen);
221
 
  if(ret == -1){
222
 
    perror_plus("clock_gettime");
223
 
    return -1;
224
 
  }
225
 
  return 0;
226
 
}
227
 
 
228
167
/* 
229
168
 * Initialize GPGME.
230
169
 */
244
183
    
245
184
    fd = (int)TEMP_FAILURE_RETRY(open(filename, O_RDONLY));
246
185
    if(fd == -1){
247
 
      perror_plus("open");
 
186
      perror("open");
248
187
      return false;
249
188
    }
250
189
    
264
203
    
265
204
    ret = (int)TEMP_FAILURE_RETRY(close(fd));
266
205
    if(ret == -1){
267
 
      perror_plus("close");
 
206
      perror("close");
268
207
    }
269
208
    gpgme_data_release(pgp_data);
270
209
    return true;
283
222
    return false;
284
223
  }
285
224
  
286
 
  /* Set GPGME home directory for the OpenPGP engine only */
 
225
    /* Set GPGME home directory for the OpenPGP engine only */
287
226
  rc = gpgme_get_engine_info(&engine_info);
288
227
  if(rc != GPG_ERR_NO_ERROR){
289
228
    fprintf(stderr, "bad gpgme_get_engine_info: %s: %s\n",
395
334
  
396
335
  /* Seek back to the beginning of the GPGME plaintext data buffer */
397
336
  if(gpgme_data_seek(dh_plain, (off_t)0, SEEK_SET) == -1){
398
 
    perror_plus("gpgme_data_seek");
 
337
    perror("gpgme_data_seek");
399
338
    plaintext_length = -1;
400
339
    goto decrypt_end;
401
340
  }
406
345
                                      (size_t)plaintext_length,
407
346
                                      plaintext_capacity);
408
347
    if(plaintext_capacity == 0){
409
 
        perror_plus("incbuffer");
 
348
        perror("incbuffer");
410
349
        plaintext_length = -1;
411
350
        goto decrypt_end;
412
351
    }
419
358
      break;
420
359
    }
421
360
    if(ret < 0){
422
 
      perror_plus("gpgme_data_read");
 
361
      perror("gpgme_data_read");
423
362
      plaintext_length = -1;
424
363
      goto decrypt_end;
425
364
    }
482
421
  }
483
422
  
484
423
  /* OpenPGP credentials */
485
 
  ret = gnutls_certificate_allocate_credentials(&mc.cred);
 
424
  gnutls_certificate_allocate_credentials(&mc.cred);
486
425
  if(ret != GNUTLS_E_SUCCESS){
487
 
    fprintf(stderr, "GnuTLS memory error: %s\n",
 
426
    fprintf(stderr, "GnuTLS memory error: %s\n", /* Spurious warning
 
427
                                                    from
 
428
                                                    -Wunreachable-code
 
429
                                                 */
488
430
            safer_gnutls_strerror(ret));
489
431
    gnutls_global_deinit();
490
432
    return -1;
612
554
  gnutls_session_t session;
613
555
  int pf;                       /* Protocol family */
614
556
  
615
 
  errno = 0;
616
 
  
617
557
  if(quit_now){
618
 
    errno = EINTR;
619
558
    return -1;
620
559
  }
621
560
  
628
567
    break;
629
568
  default:
630
569
    fprintf(stderr, "Bad address family: %d\n", af);
631
 
    errno = EINVAL;
632
570
    return -1;
633
571
  }
634
572
  
644
582
  
645
583
  tcp_sd = socket(pf, SOCK_STREAM, 0);
646
584
  if(tcp_sd < 0){
647
 
    int e = errno;
648
 
    perror_plus("socket");
649
 
    errno = e;
 
585
    perror("socket");
650
586
    goto mandos_end;
651
587
  }
652
588
  
653
589
  if(quit_now){
654
 
    errno = EINTR;
655
590
    goto mandos_end;
656
591
  }
657
592
  
664
599
    ret = inet_pton(af, ip, &to.in.sin_addr);
665
600
  }
666
601
  if(ret < 0 ){
667
 
    int e = errno;
668
 
    perror_plus("inet_pton");
669
 
    errno = e;
 
602
    perror("inet_pton");
670
603
    goto mandos_end;
671
604
  }
672
605
  if(ret == 0){
673
 
    int e = errno;
674
606
    fprintf(stderr, "Bad address: %s\n", ip);
675
 
    errno = e;
676
607
    goto mandos_end;
677
608
  }
678
609
  if(af == AF_INET6){
686
617
      if(if_index == AVAHI_IF_UNSPEC){
687
618
        fprintf(stderr, "An IPv6 link-local address is incomplete"
688
619
                " without a network interface\n");
689
 
        errno = EINVAL;
690
620
        goto mandos_end;
691
621
      }
692
622
      /* Set the network interface number as scope */
699
629
  }
700
630
  
701
631
  if(quit_now){
702
 
    errno = EINTR;
703
632
    goto mandos_end;
704
633
  }
705
634
  
707
636
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
708
637
      char interface[IF_NAMESIZE];
709
638
      if(if_indextoname((unsigned int)if_index, interface) == NULL){
710
 
        perror_plus("if_indextoname");
 
639
        perror("if_indextoname");
711
640
      } else {
712
641
        fprintf(stderr, "Connection to: %s%%%s, port %" PRIu16 "\n",
713
642
                ip, interface, port);
727
656
                        sizeof(addrstr));
728
657
    }
729
658
    if(pcret == NULL){
730
 
      perror_plus("inet_ntop");
 
659
      perror("inet_ntop");
731
660
    } else {
732
661
      if(strcmp(addrstr, ip) != 0){
733
662
        fprintf(stderr, "Canonical address form: %s\n", addrstr);
736
665
  }
737
666
  
738
667
  if(quit_now){
739
 
    errno = EINTR;
740
668
    goto mandos_end;
741
669
  }
742
670
  
746
674
    ret = connect(tcp_sd, &to.in, sizeof(to)); /* IPv4 */
747
675
  }
748
676
  if(ret < 0){
749
 
    if ((errno != ECONNREFUSED and errno != ENETUNREACH) or debug){
750
 
      int e = errno;
751
 
      perror_plus("connect");
752
 
      errno = e;
753
 
    }
 
677
    perror("connect");
754
678
    goto mandos_end;
755
679
  }
756
680
  
757
681
  if(quit_now){
758
 
    errno = EINTR;
759
682
    goto mandos_end;
760
683
  }
761
684
  
766
689
    ret = (int)TEMP_FAILURE_RETRY(write(tcp_sd, out + written,
767
690
                                   out_size - written));
768
691
    if(ret == -1){
769
 
      int e = errno;
770
 
      perror_plus("write");
771
 
      errno = e;
 
692
      perror("write");
772
693
      goto mandos_end;
773
694
    }
774
695
    written += (size_t)ret;
784
705
    }
785
706
  
786
707
    if(quit_now){
787
 
      errno = EINTR;
788
708
      goto mandos_end;
789
709
    }
790
710
  }
794
714
  }
795
715
  
796
716
  if(quit_now){
797
 
    errno = EINTR;
798
717
    goto mandos_end;
799
718
  }
800
719
  
801
 
  /* Spurious warning from -Wint-to-pointer-cast */
802
720
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
803
721
  
804
722
  if(quit_now){
805
 
    errno = EINTR;
806
723
    goto mandos_end;
807
724
  }
808
725
  
809
726
  do {
810
727
    ret = gnutls_handshake(session);
811
728
    if(quit_now){
812
 
      errno = EINTR;
813
729
      goto mandos_end;
814
730
    }
815
731
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
819
735
      fprintf(stderr, "*** GnuTLS Handshake failed ***\n");
820
736
      gnutls_perror(ret);
821
737
    }
822
 
    errno = EPROTO;
823
738
    goto mandos_end;
824
739
  }
825
740
  
833
748
  while(true){
834
749
    
835
750
    if(quit_now){
836
 
      errno = EINTR;
837
751
      goto mandos_end;
838
752
    }
839
753
    
840
754
    buffer_capacity = incbuffer(&buffer, buffer_length,
841
755
                                   buffer_capacity);
842
756
    if(buffer_capacity == 0){
843
 
      int e = errno;
844
 
      perror_plus("incbuffer");
845
 
      errno = e;
 
757
      perror("incbuffer");
846
758
      goto mandos_end;
847
759
    }
848
760
    
849
761
    if(quit_now){
850
 
      errno = EINTR;
851
762
      goto mandos_end;
852
763
    }
853
764
    
866
777
          ret = gnutls_handshake(session);
867
778
          
868
779
          if(quit_now){
869
 
            errno = EINTR;
870
780
            goto mandos_end;
871
781
          }
872
782
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
873
783
        if(ret < 0){
874
784
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
875
785
          gnutls_perror(ret);
876
 
          errno = EPROTO;
877
786
          goto mandos_end;
878
787
        }
879
788
        break;
881
790
        fprintf(stderr, "Unknown error while reading data from"
882
791
                " encrypted session with Mandos server\n");
883
792
        gnutls_bye(session, GNUTLS_SHUT_RDWR);
884
 
        errno = EIO;
885
793
        goto mandos_end;
886
794
      }
887
795
    } else {
894
802
  }
895
803
  
896
804
  if(quit_now){
897
 
    errno = EINTR;
898
805
    goto mandos_end;
899
806
  }
900
807
  
901
808
  do {
902
809
    ret = gnutls_bye(session, GNUTLS_SHUT_RDWR);
903
810
    if(quit_now){
904
 
      errno = EINTR;
905
811
      goto mandos_end;
906
812
    }
907
813
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
916
822
      written = 0;
917
823
      while(written < (size_t) decrypted_buffer_size){
918
824
        if(quit_now){
919
 
          errno = EINTR;
920
825
          goto mandos_end;
921
826
        }
922
827
        
924
829
                          (size_t)decrypted_buffer_size - written,
925
830
                          stdout);
926
831
        if(ret == 0 and ferror(stdout)){
927
 
          int e = errno;
928
832
          if(debug){
929
833
            fprintf(stderr, "Error writing encrypted data: %s\n",
930
834
                    strerror(errno));
931
835
          }
932
 
          errno = e;
933
836
          goto mandos_end;
934
837
        }
935
838
        written += (size_t)ret;
941
844
  /* Shutdown procedure */
942
845
  
943
846
 mandos_end:
944
 
  {
945
 
    int e = errno;
946
 
    free(decrypted_buffer);
947
 
    free(buffer);
948
 
    if(tcp_sd >= 0){
949
 
      ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
950
 
    }
951
 
    if(ret == -1){
952
 
      if(e == 0){
953
 
        e = errno;
954
 
      }
955
 
      perror_plus("close");
956
 
    }
957
 
    gnutls_deinit(session);
958
 
    errno = e;
959
 
    if(quit_now){
960
 
      errno = EINTR;
961
 
      retval = -1;
962
 
    }
 
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;
963
858
  }
964
859
  return retval;
965
860
}
1008
903
                                           avahi_proto_to_af(proto));
1009
904
      if(ret == 0){
1010
905
        avahi_simple_poll_quit(mc.simple_poll);
1011
 
      } else {
1012
 
        ret = add_server(ip, port, interface,
1013
 
                         avahi_proto_to_af(proto));
1014
906
      }
1015
907
    }
1016
908
  }
1070
962
  }
1071
963
}
1072
964
 
1073
 
/* Signal handler that stops main loop after SIGTERM */
 
965
/* stop main loop after sigterm has been called */
1074
966
static void handle_sigterm(int sig){
1075
967
  if(quit_now){
1076
968
    return;
1078
970
  quit_now = 1;
1079
971
  signal_received = sig;
1080
972
  int old_errno = errno;
1081
 
  /* set main loop to exit */
1082
973
  if(mc.simple_poll != NULL){
1083
974
    avahi_simple_poll_quit(mc.simple_poll);
1084
975
  }
1093
984
int good_interface(const struct dirent *if_entry){
1094
985
  ssize_t ssret;
1095
986
  char *flagname = NULL;
1096
 
  if(if_entry->d_name[0] == '.'){
1097
 
    return 0;
1098
 
  }
1099
987
  int ret = asprintf(&flagname, "%s/%s/flags", sys_class_net,
1100
988
                     if_entry->d_name);
1101
989
  if(ret < 0){
1102
 
    perror_plus("asprintf");
 
990
    perror("asprintf");
 
991
    return 0;
 
992
  }
 
993
  if(if_entry->d_name[0] == '.'){
1103
994
    return 0;
1104
995
  }
1105
996
  int flags_fd = (int)TEMP_FAILURE_RETRY(open(flagname, O_RDONLY));
1106
997
  if(flags_fd == -1){
1107
 
    perror_plus("open");
1108
 
    free(flagname);
 
998
    perror("open");
1109
999
    return 0;
1110
1000
  }
1111
 
  free(flagname);
1112
1001
  typedef short ifreq_flags;    /* ifreq.ifr_flags in netdevice(7) */
1113
1002
  /* read line from flags_fd */
1114
 
  ssize_t to_read = 2+(sizeof(ifreq_flags)*2)+1; /* "0x1003\n" */
 
1003
  ssize_t to_read = (sizeof(ifreq_flags)*2)+3; /* "0x1003\n" */
1115
1004
  char *flagstring = malloc((size_t)to_read+1); /* +1 for final \0 */
1116
1005
  flagstring[(size_t)to_read] = '\0';
1117
1006
  if(flagstring == NULL){
1118
 
    perror_plus("malloc");
 
1007
    perror("malloc");
1119
1008
    close(flags_fd);
1120
1009
    return 0;
1121
1010
  }
1123
1012
    ssret = (ssize_t)TEMP_FAILURE_RETRY(read(flags_fd, flagstring,
1124
1013
                                             (size_t)to_read));
1125
1014
    if(ssret == -1){
1126
 
      perror_plus("read");
 
1015
      perror("read");
1127
1016
      free(flagstring);
1128
1017
      close(flags_fd);
1129
1018
      return 0;
1174
1063
    }
1175
1064
    return 0;
1176
1065
  }
1177
 
  /* Reject non-ARP interfaces (including dummy interfaces) */
1178
 
  if(flags & IFF_NOARP){
1179
 
    if(debug){
1180
 
      fprintf(stderr, "Rejecting non-ARP interface \"%s\"\n",
1181
 
              if_entry->d_name);
1182
 
    }
1183
 
    return 0;
1184
 
  }
1185
1066
  /* Accept this device */
1186
1067
  if(debug){
1187
1068
    fprintf(stderr, "Interface \"%s\" is acceptable\n",
1190
1071
  return 1;
1191
1072
}
1192
1073
 
1193
 
int notdotentries(const struct dirent *direntry){
1194
 
  /* Skip "." and ".." */
1195
 
  if(direntry->d_name[0] == '.'
1196
 
     and (direntry->d_name[1] == '\0'
1197
 
          or (direntry->d_name[1] == '.'
1198
 
              and direntry->d_name[2] == '\0'))){
1199
 
    return 0;
1200
 
  }
1201
 
  return 1;
1202
 
}
1203
 
 
1204
 
int avahi_loop_with_timeout(AvahiSimplePoll *s, int retry_interval){
1205
 
  int ret;
1206
 
  struct timespec now;
1207
 
  struct timespec waited_time;
1208
 
  intmax_t block_time;
1209
 
  
1210
 
  while(true){
1211
 
    if(mc.current_server == NULL){
1212
 
      if (debug){
1213
 
        fprintf(stderr,
1214
 
                "Wait until first server is found. No timeout!\n");
1215
 
      }
1216
 
      ret = avahi_simple_poll_iterate(s, -1);
1217
 
    } else {
1218
 
      if (debug){
1219
 
        fprintf(stderr, "Check current_server if we should run it,"
1220
 
                " or wait\n");
1221
 
      }
1222
 
      /* the current time */
1223
 
      ret = clock_gettime(CLOCK_MONOTONIC, &now);
1224
 
      if(ret == -1){
1225
 
        perror_plus("clock_gettime");
1226
 
        return -1;
1227
 
      }
1228
 
      /* Calculating in ms how long time between now and server
1229
 
         who we visted longest time ago. Now - last seen.  */
1230
 
      waited_time.tv_sec = (now.tv_sec
1231
 
                            - mc.current_server->last_seen.tv_sec);
1232
 
      waited_time.tv_nsec = (now.tv_nsec
1233
 
                             - mc.current_server->last_seen.tv_nsec);
1234
 
      /* total time is 10s/10,000ms.
1235
 
         Converting to s from ms by dividing by 1,000,
1236
 
         and ns to ms by dividing by 1,000,000. */
1237
 
      block_time = ((retry_interval
1238
 
                     - ((intmax_t)waited_time.tv_sec * 1000))
1239
 
                    - ((intmax_t)waited_time.tv_nsec / 1000000));
1240
 
      
1241
 
      if (debug){
1242
 
        fprintf(stderr, "Blocking for %" PRIdMAX " ms\n", block_time);
1243
 
      }
1244
 
      
1245
 
      if(block_time <= 0){
1246
 
        ret = start_mandos_communication(mc.current_server->ip,
1247
 
                                         mc.current_server->port,
1248
 
                                         mc.current_server->if_index,
1249
 
                                         mc.current_server->af);
1250
 
        if(ret == 0){
1251
 
          avahi_simple_poll_quit(mc.simple_poll);
1252
 
          return 0;
1253
 
        }
1254
 
        ret = clock_gettime(CLOCK_MONOTONIC,
1255
 
                            &mc.current_server->last_seen);
1256
 
        if(ret == -1){
1257
 
          perror_plus("clock_gettime");
1258
 
          return -1;
1259
 
        }
1260
 
        mc.current_server = mc.current_server->next;
1261
 
        block_time = 0;         /* Call avahi to find new Mandos
1262
 
                                   servers, but don't block */
1263
 
      }
1264
 
      
1265
 
      ret = avahi_simple_poll_iterate(s, (int)block_time);
1266
 
    }
1267
 
    if(ret != 0){
1268
 
      if (ret > 0 or errno != EINTR) {
1269
 
        return (ret != 1) ? ret : 0;
1270
 
      }
1271
 
    }
1272
 
  }
1273
 
}
1274
 
 
1275
1074
int main(int argc, char *argv[]){
1276
1075
  AvahiSServiceBrowser *sb = NULL;
1277
1076
  int error;
1294
1093
  bool gnutls_initialized = false;
1295
1094
  bool gpgme_initialized = false;
1296
1095
  float delay = 2.5f;
1297
 
  double retry_interval = 10; /* 10s between trying a server and
1298
 
                                 retrying the same server again */
1299
1096
  
1300
1097
  struct sigaction old_sigterm_action = { .sa_handler = SIG_DFL };
1301
1098
  struct sigaction sigterm_action = { .sa_handler = handle_sigterm };
1307
1104
  errno = 0;
1308
1105
  ret = setgid(gid);
1309
1106
  if(ret == -1){
1310
 
    perror_plus("setgid");
 
1107
    perror("setgid");
1311
1108
  }
1312
1109
  
1313
1110
  /* Lower user privileges (temporarily) */
1314
1111
  errno = 0;
1315
1112
  ret = seteuid(uid);
1316
1113
  if(ret == -1){
1317
 
    perror_plus("seteuid");
 
1114
    perror("seteuid");
1318
1115
  }
1319
1116
  
1320
1117
  if(quit_now){
1355
1152
        .arg = "SECONDS",
1356
1153
        .doc = "Maximum delay to wait for interface startup",
1357
1154
        .group = 2 },
1358
 
      { .name = "retry", .key = 132,
1359
 
        .arg = "SECONDS",
1360
 
        .doc = "Retry interval used when denied by the mandos server",
1361
 
        .group = 2 },
1362
 
      /*
1363
 
       * These reproduce what we would get without ARGP_NO_HELP
1364
 
       */
1365
 
      { .name = "help", .key = '?',
1366
 
        .doc = "Give this help list", .group = -1 },
1367
 
      { .name = "usage", .key = -3,
1368
 
        .doc = "Give a short usage message", .group = -1 },
1369
 
      { .name = "version", .key = 'V',
1370
 
        .doc = "Print program version", .group = -1 },
1371
1155
      { .name = NULL }
1372
1156
    };
1373
1157
    
1374
1158
    error_t parse_opt(int key, char *arg,
1375
1159
                      struct argp_state *state){
1376
 
      errno = 0;
1377
1160
      switch(key){
1378
1161
      case 128:                 /* --debug */
1379
1162
        debug = true;
1395
1178
        tmpmax = strtoimax(arg, &tmp, 10);
1396
1179
        if(errno != 0 or tmp == arg or *tmp != '\0'
1397
1180
           or tmpmax != (typeof(mc.dh_bits))tmpmax){
1398
 
          argp_error(state, "Bad number of DH bits");
 
1181
          fprintf(stderr, "Bad number of DH bits\n");
 
1182
          exit(EXIT_FAILURE);
1399
1183
        }
1400
1184
        mc.dh_bits = (typeof(mc.dh_bits))tmpmax;
1401
1185
        break;
1406
1190
        errno = 0;
1407
1191
        delay = strtof(arg, &tmp);
1408
1192
        if(errno != 0 or tmp == arg or *tmp != '\0'){
1409
 
          argp_error(state, "Bad delay");
1410
 
        }
1411
 
      case 132:                 /* --retry */
1412
 
        errno = 0;
1413
 
        retry_interval = strtod(arg, &tmp);
1414
 
        if(errno != 0 or tmp == arg or *tmp != '\0'
1415
 
           or (retry_interval * 1000) > INT_MAX
1416
 
           or retry_interval < 0){
1417
 
          argp_error(state, "Bad retry interval");
 
1193
          fprintf(stderr, "Bad delay\n");
 
1194
          exit(EXIT_FAILURE);
1418
1195
        }
1419
1196
        break;
1420
 
        /*
1421
 
         * These reproduce what we would get without ARGP_NO_HELP
1422
 
         */
1423
 
      case '?':                 /* --help */
1424
 
        argp_state_help(state, state->out_stream,
1425
 
                        (ARGP_HELP_STD_HELP | ARGP_HELP_EXIT_ERR)
1426
 
                        & ~(unsigned int)ARGP_HELP_EXIT_OK);
1427
 
      case -3:                  /* --usage */
1428
 
        argp_state_help(state, state->out_stream,
1429
 
                        ARGP_HELP_USAGE | ARGP_HELP_EXIT_ERR);
1430
 
      case 'V':                 /* --version */
1431
 
        fprintf(state->out_stream, "%s\n", argp_program_version);
1432
 
        exit(argp_err_exit_status);
 
1197
      case ARGP_KEY_ARG:
 
1198
        argp_usage(state);
 
1199
      case ARGP_KEY_END:
1433
1200
        break;
1434
1201
      default:
1435
1202
        return ARGP_ERR_UNKNOWN;
1436
1203
      }
1437
 
      return errno;
 
1204
      return 0;
1438
1205
    }
1439
1206
    
1440
1207
    struct argp argp = { .options = options, .parser = parse_opt,
1441
1208
                         .args_doc = "",
1442
1209
                         .doc = "Mandos client -- Get and decrypt"
1443
1210
                         " passwords from a Mandos server" };
1444
 
    ret = argp_parse(&argp, argc, argv,
1445
 
                     ARGP_IN_ORDER | ARGP_NO_HELP, 0, NULL);
1446
 
    switch(ret){
1447
 
    case 0:
1448
 
      break;
1449
 
    case ENOMEM:
1450
 
    default:
1451
 
      errno = ret;
1452
 
      perror_plus("argp_parse");
1453
 
      exitcode = EX_OSERR;
1454
 
      goto end;
1455
 
    case EINVAL:
1456
 
      exitcode = EX_USAGE;
1457
 
      goto end;
1458
 
    }
1459
 
  }
1460
 
    
1461
 
  {
1462
 
    /* Work around Debian bug #633582:
1463
 
       <http://bugs.debian.org/633582> */
1464
 
    struct stat st;
1465
 
    
1466
 
    /* Re-raise priviliges */
1467
 
    errno = 0;
1468
 
    ret = seteuid(0);
1469
 
    if(ret == -1){
1470
 
      perror_plus("seteuid");
1471
 
    }
1472
 
    
1473
 
    if(strcmp(seckey, PATHDIR "/" SECKEY) == 0){
1474
 
      int seckey_fd = open(seckey, O_RDONLY);
1475
 
      if(seckey_fd == -1){
1476
 
        perror_plus("open");
1477
 
      } else {
1478
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(seckey_fd, &st));
1479
 
        if(ret == -1){
1480
 
          perror_plus("fstat");
1481
 
        } else {
1482
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1483
 
            ret = fchown(seckey_fd, uid, gid);
1484
 
            if(ret == -1){
1485
 
              perror_plus("fchown");
1486
 
            }
1487
 
          }
1488
 
        }
1489
 
        TEMP_FAILURE_RETRY(close(seckey_fd));
1490
 
      }
1491
 
    }
1492
 
    
1493
 
    if(strcmp(pubkey, PATHDIR "/" PUBKEY) == 0){
1494
 
      int pubkey_fd = open(pubkey, O_RDONLY);
1495
 
      if(pubkey_fd == -1){
1496
 
        perror_plus("open");
1497
 
      } else {
1498
 
        ret = (int)TEMP_FAILURE_RETRY(fstat(pubkey_fd, &st));
1499
 
        if(ret == -1){
1500
 
          perror_plus("fstat");
1501
 
        } else {
1502
 
          if(S_ISREG(st.st_mode) and st.st_uid == 0 and st.st_gid == 0){
1503
 
            ret = fchown(pubkey_fd, uid, gid);
1504
 
            if(ret == -1){
1505
 
              perror_plus("fchown");
1506
 
            }
1507
 
          }
1508
 
        }
1509
 
        TEMP_FAILURE_RETRY(close(pubkey_fd));
1510
 
      }
1511
 
    }
1512
 
    
1513
 
    /* Lower privileges */
1514
 
    errno = 0;
1515
 
    ret = seteuid(uid);
1516
 
    if(ret == -1){
1517
 
      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;
1518
1216
    }
1519
1217
  }
1520
1218
  
1533
1231
        fprintf(stderr, "Using interface \"%s\"\n", interface);
1534
1232
      }
1535
1233
      if(interface == NULL){
1536
 
        perror_plus("malloc");
 
1234
        perror("malloc");
1537
1235
        free(direntries);
1538
1236
        exitcode = EXIT_FAILURE;
1539
1237
        goto end;
1554
1252
  mc.simple_poll = avahi_simple_poll_new();
1555
1253
  if(mc.simple_poll == NULL){
1556
1254
    fprintf(stderr, "Avahi: Failed to create simple poll object.\n");
1557
 
    exitcode = EX_UNAVAILABLE;
 
1255
    exitcode = EXIT_FAILURE;
1558
1256
    goto end;
1559
1257
  }
1560
1258
  
1561
1259
  sigemptyset(&sigterm_action.sa_mask);
1562
1260
  ret = sigaddset(&sigterm_action.sa_mask, SIGINT);
1563
1261
  if(ret == -1){
1564
 
    perror_plus("sigaddset");
1565
 
    exitcode = EX_OSERR;
 
1262
    perror("sigaddset");
 
1263
    exitcode = EXIT_FAILURE;
1566
1264
    goto end;
1567
1265
  }
1568
1266
  ret = sigaddset(&sigterm_action.sa_mask, SIGHUP);
1569
1267
  if(ret == -1){
1570
 
    perror_plus("sigaddset");
1571
 
    exitcode = EX_OSERR;
 
1268
    perror("sigaddset");
 
1269
    exitcode = EXIT_FAILURE;
1572
1270
    goto end;
1573
1271
  }
1574
1272
  ret = sigaddset(&sigterm_action.sa_mask, SIGTERM);
1575
1273
  if(ret == -1){
1576
 
    perror_plus("sigaddset");
1577
 
    exitcode = EX_OSERR;
 
1274
    perror("sigaddset");
 
1275
    exitcode = EXIT_FAILURE;
1578
1276
    goto end;
1579
1277
  }
1580
1278
  /* Need to check if the handler is SIG_IGN before handling:
1583
1281
  */
1584
1282
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
1585
1283
  if(ret == -1){
1586
 
    perror_plus("sigaction");
1587
 
    return EX_OSERR;
 
1284
    perror("sigaction");
 
1285
    return EXIT_FAILURE;
1588
1286
  }
1589
1287
  if(old_sigterm_action.sa_handler != SIG_IGN){
1590
1288
    ret = sigaction(SIGINT, &sigterm_action, NULL);
1591
1289
    if(ret == -1){
1592
 
      perror_plus("sigaction");
1593
 
      exitcode = EX_OSERR;
 
1290
      perror("sigaction");
 
1291
      exitcode = EXIT_FAILURE;
1594
1292
      goto end;
1595
1293
    }
1596
1294
  }
1597
1295
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
1598
1296
  if(ret == -1){
1599
 
    perror_plus("sigaction");
1600
 
    return EX_OSERR;
 
1297
    perror("sigaction");
 
1298
    return EXIT_FAILURE;
1601
1299
  }
1602
1300
  if(old_sigterm_action.sa_handler != SIG_IGN){
1603
1301
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
1604
1302
    if(ret == -1){
1605
 
      perror_plus("sigaction");
1606
 
      exitcode = EX_OSERR;
 
1303
      perror("sigaction");
 
1304
      exitcode = EXIT_FAILURE;
1607
1305
      goto end;
1608
1306
    }
1609
1307
  }
1610
1308
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
1611
1309
  if(ret == -1){
1612
 
    perror_plus("sigaction");
1613
 
    return EX_OSERR;
 
1310
    perror("sigaction");
 
1311
    return EXIT_FAILURE;
1614
1312
  }
1615
1313
  if(old_sigterm_action.sa_handler != SIG_IGN){
1616
1314
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
1617
1315
    if(ret == -1){
1618
 
      perror_plus("sigaction");
1619
 
      exitcode = EX_OSERR;
 
1316
      perror("sigaction");
 
1317
      exitcode = EXIT_FAILURE;
1620
1318
      goto end;
1621
1319
    }
1622
1320
  }
1626
1324
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1627
1325
    if(if_index == 0){
1628
1326
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1629
 
      exitcode = EX_UNAVAILABLE;
 
1327
      exitcode = EXIT_FAILURE;
1630
1328
      goto end;
1631
1329
    }
1632
1330
    
1638
1336
    errno = 0;
1639
1337
    ret = seteuid(0);
1640
1338
    if(ret == -1){
1641
 
      perror_plus("seteuid");
 
1339
      perror("seteuid");
1642
1340
    }
1643
1341
    
1644
1342
#ifdef __linux__
1645
1343
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1646
 
       messages about the network interface to mess up the prompt */
 
1344
       messages to mess up the prompt */
1647
1345
    ret = klogctl(8, NULL, 5);
1648
1346
    bool restore_loglevel = true;
1649
1347
    if(ret == -1){
1650
1348
      restore_loglevel = false;
1651
 
      perror_plus("klogctl");
 
1349
      perror("klogctl");
1652
1350
    }
1653
1351
#endif  /* __linux__ */
1654
1352
    
1655
1353
    sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1656
1354
    if(sd < 0){
1657
 
      perror_plus("socket");
1658
 
      exitcode = EX_OSERR;
 
1355
      perror("socket");
 
1356
      exitcode = EXIT_FAILURE;
1659
1357
#ifdef __linux__
1660
1358
      if(restore_loglevel){
1661
1359
        ret = klogctl(7, NULL, 0);
1662
1360
        if(ret == -1){
1663
 
          perror_plus("klogctl");
 
1361
          perror("klogctl");
1664
1362
        }
1665
1363
      }
1666
1364
#endif  /* __linux__ */
1668
1366
      errno = 0;
1669
1367
      ret = seteuid(uid);
1670
1368
      if(ret == -1){
1671
 
        perror_plus("seteuid");
 
1369
        perror("seteuid");
1672
1370
      }
1673
1371
      goto end;
1674
1372
    }
1675
1373
    strcpy(network.ifr_name, interface);
1676
1374
    ret = ioctl(sd, SIOCGIFFLAGS, &network);
1677
1375
    if(ret == -1){
1678
 
      perror_plus("ioctl SIOCGIFFLAGS");
 
1376
      perror("ioctl SIOCGIFFLAGS");
1679
1377
#ifdef __linux__
1680
1378
      if(restore_loglevel){
1681
1379
        ret = klogctl(7, NULL, 0);
1682
1380
        if(ret == -1){
1683
 
          perror_plus("klogctl");
 
1381
          perror("klogctl");
1684
1382
        }
1685
1383
      }
1686
1384
#endif  /* __linux__ */
1687
 
      exitcode = EX_OSERR;
 
1385
      exitcode = EXIT_FAILURE;
1688
1386
      /* Lower privileges */
1689
1387
      errno = 0;
1690
1388
      ret = seteuid(uid);
1691
1389
      if(ret == -1){
1692
 
        perror_plus("seteuid");
 
1390
        perror("seteuid");
1693
1391
      }
1694
1392
      goto end;
1695
1393
    }
1699
1397
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1700
1398
      if(ret == -1){
1701
1399
        take_down_interface = false;
1702
 
        perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1703
 
        exitcode = EX_OSERR;
 
1400
        perror("ioctl SIOCSIFFLAGS +IFF_UP");
 
1401
        exitcode = EXIT_FAILURE;
1704
1402
#ifdef __linux__
1705
1403
        if(restore_loglevel){
1706
1404
          ret = klogctl(7, NULL, 0);
1707
1405
          if(ret == -1){
1708
 
            perror_plus("klogctl");
 
1406
            perror("klogctl");
1709
1407
          }
1710
1408
        }
1711
1409
#endif  /* __linux__ */
1713
1411
        errno = 0;
1714
1412
        ret = seteuid(uid);
1715
1413
        if(ret == -1){
1716
 
          perror_plus("seteuid");
 
1414
          perror("seteuid");
1717
1415
        }
1718
1416
        goto end;
1719
1417
      }
1720
1418
    }
1721
 
    /* Sleep checking until interface is running.
1722
 
       Check every 0.25s, up to total time of delay */
 
1419
    /* sleep checking until interface is running */
1723
1420
    for(int i=0; i < delay * 4; i++){
1724
1421
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1725
1422
      if(ret == -1){
1726
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1423
        perror("ioctl SIOCGIFFLAGS");
1727
1424
      } else if(network.ifr_flags & IFF_RUNNING){
1728
1425
        break;
1729
1426
      }
1730
1427
      struct timespec sleeptime = { .tv_nsec = 250000000 };
1731
1428
      ret = nanosleep(&sleeptime, NULL);
1732
1429
      if(ret == -1 and errno != EINTR){
1733
 
        perror_plus("nanosleep");
 
1430
        perror("nanosleep");
1734
1431
      }
1735
1432
    }
1736
1433
    if(not take_down_interface){
1737
1434
      /* We won't need the socket anymore */
1738
1435
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1739
1436
      if(ret == -1){
1740
 
        perror_plus("close");
 
1437
        perror("close");
1741
1438
      }
1742
1439
    }
1743
1440
#ifdef __linux__
1745
1442
      /* Restores kernel loglevel to default */
1746
1443
      ret = klogctl(7, NULL, 0);
1747
1444
      if(ret == -1){
1748
 
        perror_plus("klogctl");
 
1445
        perror("klogctl");
1749
1446
      }
1750
1447
    }
1751
1448
#endif  /* __linux__ */
1755
1452
      /* Lower privileges */
1756
1453
      ret = seteuid(uid);
1757
1454
      if(ret == -1){
1758
 
        perror_plus("seteuid");
 
1455
        perror("seteuid");
1759
1456
      }
1760
1457
    } else {
1761
1458
      /* Lower privileges permanently */
1762
1459
      ret = setuid(uid);
1763
1460
      if(ret == -1){
1764
 
        perror_plus("setuid");
 
1461
        perror("setuid");
1765
1462
      }
1766
1463
    }
1767
1464
  }
1773
1470
  ret = init_gnutls_global(pubkey, seckey);
1774
1471
  if(ret == -1){
1775
1472
    fprintf(stderr, "init_gnutls_global failed\n");
1776
 
    exitcode = EX_UNAVAILABLE;
 
1473
    exitcode = EXIT_FAILURE;
1777
1474
    goto end;
1778
1475
  } else {
1779
1476
    gnutls_initialized = true;
1783
1480
    goto end;
1784
1481
  }
1785
1482
  
 
1483
  tempdir_created = true;
1786
1484
  if(mkdtemp(tempdir) == NULL){
1787
 
    perror_plus("mkdtemp");
 
1485
    tempdir_created = false;
 
1486
    perror("mkdtemp");
1788
1487
    goto end;
1789
1488
  }
1790
 
  tempdir_created = true;
1791
1489
  
1792
1490
  if(quit_now){
1793
1491
    goto end;
1795
1493
  
1796
1494
  if(not init_gpgme(pubkey, seckey, tempdir)){
1797
1495
    fprintf(stderr, "init_gpgme failed\n");
1798
 
    exitcode = EX_UNAVAILABLE;
 
1496
    exitcode = EXIT_FAILURE;
1799
1497
    goto end;
1800
1498
  } else {
1801
1499
    gpgme_initialized = true;
1811
1509
    char *address = strrchr(connect_to, ':');
1812
1510
    if(address == NULL){
1813
1511
      fprintf(stderr, "No colon in address\n");
1814
 
      exitcode = EX_USAGE;
 
1512
      exitcode = EXIT_FAILURE;
1815
1513
      goto end;
1816
1514
    }
1817
1515
    
1825
1523
    if(errno != 0 or tmp == address+1 or *tmp != '\0'
1826
1524
       or tmpmax != (uint16_t)tmpmax){
1827
1525
      fprintf(stderr, "Bad port number\n");
1828
 
      exitcode = EX_USAGE;
 
1526
      exitcode = EXIT_FAILURE;
1829
1527
      goto end;
1830
1528
    }
1831
1529
  
1835
1533
    
1836
1534
    port = (uint16_t)tmpmax;
1837
1535
    *address = '\0';
 
1536
    address = connect_to;
1838
1537
    /* Colon in address indicates IPv6 */
1839
1538
    int af;
1840
 
    if(strchr(connect_to, ':') != NULL){
 
1539
    if(strchr(address, ':') != NULL){
1841
1540
      af = AF_INET6;
1842
 
      /* Accept [] around IPv6 address - see RFC 5952 */
1843
 
      if(connect_to[0] == '[' and address[-1] == ']')
1844
 
        {
1845
 
          connect_to++;
1846
 
          address[-1] = '\0';
1847
 
        }
1848
1541
    } else {
1849
1542
      af = AF_INET;
1850
1543
    }
1851
 
    address = connect_to;
1852
1544
    
1853
1545
    if(quit_now){
1854
1546
      goto end;
1855
1547
    }
1856
1548
    
1857
 
    while(not quit_now){
1858
 
      ret = start_mandos_communication(address, port, if_index, af);
1859
 
      if(quit_now or ret == 0){
1860
 
        break;
1861
 
      }
1862
 
      if(debug){
1863
 
        fprintf(stderr, "Retrying in %d seconds\n",
1864
 
                (int)retry_interval);
1865
 
      }
1866
 
      sleep((int)retry_interval);
1867
 
    }
1868
 
    
1869
 
    if (not quit_now){
 
1549
    ret = start_mandos_communication(address, port, if_index, af);
 
1550
    if(ret < 0){
 
1551
      exitcode = EXIT_FAILURE;
 
1552
    } else {
1870
1553
      exitcode = EXIT_SUCCESS;
1871
1554
    }
1872
 
 
1873
1555
    goto end;
1874
1556
  }
1875
1557
  
1899
1581
  if(mc.server == NULL){
1900
1582
    fprintf(stderr, "Failed to create Avahi server: %s\n",
1901
1583
            avahi_strerror(error));
1902
 
    exitcode = EX_UNAVAILABLE;
 
1584
    exitcode = EXIT_FAILURE;
1903
1585
    goto end;
1904
1586
  }
1905
1587
  
1914
1596
  if(sb == NULL){
1915
1597
    fprintf(stderr, "Failed to create service browser: %s\n",
1916
1598
            avahi_strerror(avahi_server_errno(mc.server)));
1917
 
    exitcode = EX_UNAVAILABLE;
 
1599
    exitcode = EXIT_FAILURE;
1918
1600
    goto end;
1919
1601
  }
1920
1602
  
1927
1609
  if(debug){
1928
1610
    fprintf(stderr, "Starting Avahi loop search\n");
1929
1611
  }
1930
 
 
1931
 
  ret = avahi_loop_with_timeout(mc.simple_poll,
1932
 
                                (int)(retry_interval * 1000));
1933
 
  if(debug){
1934
 
    fprintf(stderr, "avahi_loop_with_timeout exited %s\n",
1935
 
            (ret == 0) ? "successfully" : "with error");
1936
 
  }
 
1612
  
 
1613
  avahi_simple_poll_loop(mc.simple_poll);
1937
1614
  
1938
1615
 end:
1939
1616
  
1960
1637
  if(gpgme_initialized){
1961
1638
    gpgme_release(mc.ctx);
1962
1639
  }
1963
 
 
1964
 
  /* Cleans up the circular linked list of Mandos servers the client
1965
 
     has seen */
1966
 
  if(mc.current_server != NULL){
1967
 
    mc.current_server->prev->next = NULL;
1968
 
    while(mc.current_server != NULL){
1969
 
      server *next = mc.current_server->next;
1970
 
      free(mc.current_server);
1971
 
      mc.current_server = next;
1972
 
    }
1973
 
  }
1974
1640
  
1975
1641
  /* Take down the network interface */
1976
1642
  if(take_down_interface){
1978
1644
    errno = 0;
1979
1645
    ret = seteuid(0);
1980
1646
    if(ret == -1){
1981
 
      perror_plus("seteuid");
 
1647
      perror("seteuid");
1982
1648
    }
1983
1649
    if(geteuid() == 0){
1984
1650
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
1985
1651
      if(ret == -1){
1986
 
        perror_plus("ioctl SIOCGIFFLAGS");
 
1652
        perror("ioctl SIOCGIFFLAGS");
1987
1653
      } else if(network.ifr_flags & IFF_UP) {
1988
 
        network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
 
1654
        network.ifr_flags &= ~IFF_UP; /* clear flag */
1989
1655
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
1990
1656
        if(ret == -1){
1991
 
          perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
 
1657
          perror("ioctl SIOCSIFFLAGS -IFF_UP");
1992
1658
        }
1993
1659
      }
1994
1660
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
1995
1661
      if(ret == -1){
1996
 
        perror_plus("close");
 
1662
        perror("close");
1997
1663
      }
1998
1664
      /* Lower privileges permanently */
1999
1665
      errno = 0;
2000
1666
      ret = setuid(uid);
2001
1667
      if(ret == -1){
2002
 
        perror_plus("setuid");
 
1668
        perror("setuid");
2003
1669
      }
2004
1670
    }
2005
1671
  }
2006
1672
  
2007
 
  /* Removes the GPGME temp directory and all files inside */
 
1673
  /* Removes the temp directory used by GPGME */
2008
1674
  if(tempdir_created){
2009
 
    struct dirent **direntries = NULL;
2010
 
    struct dirent *direntry = NULL;
2011
 
    int numentries = scandir(tempdir, &direntries, notdotentries,
2012
 
                             alphasort);
2013
 
    if (numentries > 0){
2014
 
      for(int i = 0; i < numentries; i++){
2015
 
        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
        }
2016
1695
        char *fullname = NULL;
2017
1696
        ret = asprintf(&fullname, "%s/%s", tempdir,
2018
1697
                       direntry->d_name);
2019
1698
        if(ret < 0){
2020
 
          perror_plus("asprintf");
 
1699
          perror("asprintf");
2021
1700
          continue;
2022
1701
        }
2023
1702
        ret = remove(fullname);
2027
1706
        }
2028
1707
        free(fullname);
2029
1708
      }
2030
 
    }
2031
 
 
2032
 
    /* need to clean even if 0 because man page doesn't specify */
2033
 
    free(direntries);
2034
 
    if (numentries == -1){
2035
 
      perror_plus("scandir");
 
1709
      closedir(d);
2036
1710
    }
2037
1711
    ret = rmdir(tempdir);
2038
1712
    if(ret == -1 and errno != ENOENT){
2039
 
      perror_plus("rmdir");
 
1713
      perror("rmdir");
2040
1714
    }
2041
1715
  }
2042
1716
  
2047
1721
                                            &old_sigterm_action,
2048
1722
                                            NULL));
2049
1723
    if(ret == -1){
2050
 
      perror_plus("sigaction");
 
1724
      perror("sigaction");
2051
1725
    }
2052
1726
    do {
2053
1727
      ret = raise(signal_received);
2054
1728
    } while(ret != 0 and errno == EINTR);
2055
1729
    if(ret != 0){
2056
 
      perror_plus("raise");
 
1730
      perror("raise");
2057
1731
      abort();
2058
1732
    }
2059
1733
    TEMP_FAILURE_RETRY(pause());