/mandos/trunk

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

« back to all changes in this revision

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

  • Committer: Teddy Hogeborn
  • Date: 2009-09-08 04:41:37 UTC
  • Revision ID: teddy@fukt.bsnet.se-20090908044137-4cxubotvn4etoxxl
* plugins.d/mandos-client.c (main): Bug fix: Check result of setgid().
                                    Bug fix: If taking down network
                                    interface, do not drop privileges
                                    completely; save them and reassert
                                    privileges when needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
                                   INET_ADDRSTRLEN, INET6_ADDRSTRLEN
72
72
                                */
73
73
#include <unistd.h>             /* close(), SEEK_SET, off_t, write(),
74
 
                                   getuid(), getgid(), setuid(),
 
74
                                   getuid(), getgid(), seteuid(),
75
75
                                   setgid() */
76
76
#include <arpa/inet.h>          /* inet_pton(), htons */
77
77
#include <iso646.h>             /* not, or, and */
80
80
                                   argp_parse(), ARGP_KEY_ARG,
81
81
                                   ARGP_KEY_END, ARGP_ERR_UNKNOWN */
82
82
#include <signal.h>             /* sigemptyset(), sigaddset(),
83
 
                                   sigaction(), SIGTERM, sigaction,
84
 
                                   sig_atomic_t */
 
83
                                   sigaction(), SIGTERM, sig_atomic_t,
 
84
                                   raise() */
85
85
 
86
86
#ifdef __linux__
87
87
#include <sys/klog.h>           /* klogctl() */
164
164
 */
165
165
static bool init_gpgme(const char *seckey,
166
166
                       const char *pubkey, const char *tempdir){
167
 
  int ret;
168
167
  gpgme_error_t rc;
169
168
  gpgme_engine_info_t engine_info;
170
169
  
173
172
   * Helper function to insert pub and seckey to the engine keyring.
174
173
   */
175
174
  bool import_key(const char *filename){
 
175
    int ret;
176
176
    int fd;
177
177
    gpgme_data_t pgp_data;
178
178
    
249
249
    return false;
250
250
  }
251
251
  
252
 
  return true; 
 
252
  return true;
253
253
}
254
254
 
255
255
/* 
309
309
        }
310
310
        gpgme_recipient_t recipient;
311
311
        recipient = result->recipients;
312
 
        if(recipient){
313
 
          while(recipient != NULL){
314
 
            fprintf(stderr, "Public key algorithm: %s\n",
315
 
                    gpgme_pubkey_algo_name(recipient->pubkey_algo));
316
 
            fprintf(stderr, "Key ID: %s\n", recipient->keyid);
317
 
            fprintf(stderr, "Secret key available: %s\n",
318
 
                    recipient->status == GPG_ERR_NO_SECKEY
319
 
                    ? "No" : "Yes");
320
 
            recipient = recipient->next;
321
 
          }
 
312
        while(recipient != NULL){
 
313
          fprintf(stderr, "Public key algorithm: %s\n",
 
314
                  gpgme_pubkey_algo_name(recipient->pubkey_algo));
 
315
          fprintf(stderr, "Key ID: %s\n", recipient->keyid);
 
316
          fprintf(stderr, "Secret key available: %s\n",
 
317
                  recipient->status == GPG_ERR_NO_SECKEY
 
318
                  ? "No" : "Yes");
 
319
          recipient = recipient->next;
322
320
        }
323
321
      }
324
322
    }
516
514
static void empty_log(__attribute__((unused)) AvahiLogLevel level,
517
515
                      __attribute__((unused)) const char *txt){}
518
516
 
 
517
sig_atomic_t quit_now = 0;
 
518
int signal_received = 0;
 
519
 
519
520
/* Called when a Mandos server is found */
520
521
static int start_mandos_communication(const char *ip, uint16_t port,
521
522
                                      AvahiIfIndex if_index,
522
523
                                      int af){
523
 
  int ret, tcp_sd;
 
524
  int ret, tcp_sd = -1;
524
525
  ssize_t sret;
525
526
  union {
526
527
    struct sockaddr_in in;
530
531
  char *decrypted_buffer;
531
532
  size_t buffer_length = 0;
532
533
  size_t buffer_capacity = 0;
533
 
  ssize_t decrypted_buffer_size;
534
534
  size_t written;
535
535
  int retval = 0;
536
536
  gnutls_session_t session;
537
537
  int pf;                       /* Protocol family */
538
538
  
 
539
  if(quit_now){
 
540
    return -1;
 
541
  }
 
542
  
539
543
  switch(af){
540
544
  case AF_INET6:
541
545
    pf = PF_INET6;
561
565
  tcp_sd = socket(pf, SOCK_STREAM, 0);
562
566
  if(tcp_sd < 0){
563
567
    perror("socket");
564
 
    return -1;
 
568
    retval = -1;
 
569
    goto mandos_end;
 
570
  }
 
571
  
 
572
  if(quit_now){
 
573
    goto mandos_end;
565
574
  }
566
575
  
567
576
  memset(&to, 0, sizeof(to));
568
577
  if(af == AF_INET6){
569
 
    to.in6.sin6_family = (uint16_t)af;
 
578
    to.in6.sin6_family = (sa_family_t)af;
570
579
    ret = inet_pton(af, ip, &to.in6.sin6_addr);
571
580
  } else {                      /* IPv4 */
572
581
    to.in.sin_family = (sa_family_t)af;
574
583
  }
575
584
  if(ret < 0 ){
576
585
    perror("inet_pton");
577
 
    return -1;
 
586
    retval = -1;
 
587
    goto mandos_end;
578
588
  }
579
589
  if(ret == 0){
580
590
    fprintf(stderr, "Bad address: %s\n", ip);
581
 
    return -1;
 
591
    retval = -1;
 
592
    goto mandos_end;
582
593
  }
583
594
  if(af == AF_INET6){
584
595
    to.in6.sin6_port = htons(port); /* Spurious warnings from
591
602
      if(if_index == AVAHI_IF_UNSPEC){
592
603
        fprintf(stderr, "An IPv6 link-local address is incomplete"
593
604
                " without a network interface\n");
594
 
        return -1;
 
605
        retval = -1;
 
606
        goto mandos_end;
595
607
      }
596
608
      /* Set the network interface number as scope */
597
609
      to.in6.sin6_scope_id = (uint32_t)if_index;
602
614
                                     -Wunreachable-code */
603
615
  }
604
616
  
 
617
  if(quit_now){
 
618
    goto mandos_end;
 
619
  }
 
620
  
605
621
  if(debug){
606
622
    if(af == AF_INET6 and if_index != AVAHI_IF_UNSPEC){
607
623
      char interface[IF_NAMESIZE];
634
650
    }
635
651
  }
636
652
  
 
653
  if(quit_now){
 
654
    goto mandos_end;
 
655
  }
 
656
  
637
657
  if(af == AF_INET6){
638
658
    ret = connect(tcp_sd, &to.in6, sizeof(to));
639
659
  } else {
641
661
  }
642
662
  if(ret < 0){
643
663
    perror("connect");
644
 
    return -1;
 
664
    retval = -1;
 
665
    goto mandos_end;
 
666
  }
 
667
  
 
668
  if(quit_now){
 
669
    goto mandos_end;
645
670
  }
646
671
  
647
672
  const char *out = mandos_protocol_version;
666
691
        break;
667
692
      }
668
693
    }
 
694
  
 
695
    if(quit_now){
 
696
      goto mandos_end;
 
697
    }
669
698
  }
670
699
  
671
700
  if(debug){
672
701
    fprintf(stderr, "Establishing TLS session with %s\n", ip);
673
702
  }
674
703
  
 
704
  if(quit_now){
 
705
    goto mandos_end;
 
706
  }
 
707
  
675
708
  gnutls_transport_set_ptr(session, (gnutls_transport_ptr_t) tcp_sd);
676
709
  
677
 
  do{
 
710
  if(quit_now){
 
711
    goto mandos_end;
 
712
  }
 
713
  
 
714
  do {
678
715
    ret = gnutls_handshake(session);
 
716
    if(quit_now){
 
717
      goto mandos_end;
 
718
    }
679
719
  } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
680
720
  
681
721
  if(ret != GNUTLS_E_SUCCESS){
695
735
  }
696
736
  
697
737
  while(true){
 
738
    
 
739
    if(quit_now){
 
740
      goto mandos_end;
 
741
    }
 
742
    
698
743
    buffer_capacity = incbuffer(&buffer, buffer_length,
699
744
                                   buffer_capacity);
700
745
    if(buffer_capacity == 0){
703
748
      goto mandos_end;
704
749
    }
705
750
    
 
751
    if(quit_now){
 
752
      goto mandos_end;
 
753
    }
 
754
    
706
755
    sret = gnutls_record_recv(session, buffer+buffer_length,
707
756
                              BUFFER_SIZE);
708
757
    if(sret == 0){
714
763
      case GNUTLS_E_AGAIN:
715
764
        break;
716
765
      case GNUTLS_E_REHANDSHAKE:
717
 
        do{
 
766
        do {
718
767
          ret = gnutls_handshake(session);
 
768
          
 
769
          if(quit_now){
 
770
            goto mandos_end;
 
771
          }
719
772
        } while(ret == GNUTLS_E_AGAIN or ret == GNUTLS_E_INTERRUPTED);
720
773
        if(ret < 0){
721
774
          fprintf(stderr, "*** GnuTLS Re-handshake failed ***\n");
740
793
    fprintf(stderr, "Closing TLS session\n");
741
794
  }
742
795
  
 
796
  if(quit_now){
 
797
    goto mandos_end;
 
798
  }
 
799
  
743
800
  gnutls_bye(session, GNUTLS_SHUT_RDWR);
744
801
  
 
802
  if(quit_now){
 
803
    goto mandos_end;
 
804
  }
 
805
  
745
806
  if(buffer_length > 0){
 
807
    ssize_t decrypted_buffer_size;
746
808
    decrypted_buffer_size = pgp_packet_decrypt(buffer,
747
809
                                               buffer_length,
748
810
                                               &decrypted_buffer);
749
811
    if(decrypted_buffer_size >= 0){
 
812
      
750
813
      written = 0;
751
814
      while(written < (size_t) decrypted_buffer_size){
 
815
        if(quit_now){
 
816
          goto mandos_end;
 
817
        }
 
818
        
752
819
        ret = (int)fwrite(decrypted_buffer + written, 1,
753
820
                          (size_t)decrypted_buffer_size - written,
754
821
                          stdout);
774
841
  
775
842
 mandos_end:
776
843
  free(buffer);
777
 
  ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
844
  if(tcp_sd >= 0){
 
845
    ret = (int)TEMP_FAILURE_RETRY(close(tcp_sd));
 
846
  }
778
847
  if(ret == -1){
779
848
    perror("close");
780
849
  }
781
850
  gnutls_deinit(session);
 
851
  if(quit_now){
 
852
    retval = -1;
 
853
  }
782
854
  return retval;
783
855
}
784
856
 
801
873
  /* Called whenever a service has been resolved successfully or
802
874
     timed out */
803
875
  
 
876
  if(quit_now){
 
877
    return;
 
878
  }
 
879
  
804
880
  switch(event){
805
881
  default:
806
882
  case AVAHI_RESOLVER_FAILURE:
843
919
  /* Called whenever a new services becomes available on the LAN or
844
920
     is removed from the LAN */
845
921
  
 
922
  if(quit_now){
 
923
    return;
 
924
  }
 
925
  
846
926
  switch(event){
847
927
  default:
848
928
  case AVAHI_BROWSER_FAILURE:
877
957
  }
878
958
}
879
959
 
880
 
sig_atomic_t quit_now = 0;
881
 
 
882
960
/* stop main loop after sigterm has been called */
883
 
static void handle_sigterm(__attribute__((unused)) int sig){
 
961
static void handle_sigterm(int sig){
884
962
  if(quit_now){
885
963
    return;
886
964
  }
887
965
  quit_now = 1;
 
966
  signal_received = sig;
888
967
  int old_errno = errno;
889
968
  if(mc.simple_poll != NULL){
890
969
    avahi_simple_poll_quit(mc.simple_poll);
901
980
  int exitcode = EXIT_SUCCESS;
902
981
  const char *interface = "eth0";
903
982
  struct ifreq network;
904
 
  int sd;
 
983
  int sd = -1;
 
984
  bool take_down_interface = false;
905
985
  uid_t uid;
906
986
  gid_t gid;
907
987
  char *connect_to = NULL;
1050
1130
    exitcode = EXIT_FAILURE;
1051
1131
    goto end;
1052
1132
  }
1053
 
  ret = sigaction(SIGTERM, &sigterm_action, &old_sigterm_action);
1054
 
  if(ret == -1){
1055
 
    perror("sigaction");
1056
 
    exitcode = EXIT_FAILURE;
1057
 
    goto end;
1058
 
  }  
 
1133
  /* Need to check if the handler is SIG_IGN before handling:
 
1134
     | [[info:libc:Initial Signal Actions]] |
 
1135
     | [[info:libc:Basic Signal Handling]]  |
 
1136
  */
 
1137
  ret = sigaction(SIGINT, NULL, &old_sigterm_action);
 
1138
  if(ret == -1){
 
1139
    perror("sigaction");
 
1140
    return EXIT_FAILURE;
 
1141
  }
 
1142
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1143
    ret = sigaction(SIGINT, &sigterm_action, NULL);
 
1144
    if(ret == -1){
 
1145
      perror("sigaction");
 
1146
      exitcode = EXIT_FAILURE;
 
1147
      goto end;
 
1148
    }
 
1149
  }
 
1150
  ret = sigaction(SIGHUP, NULL, &old_sigterm_action);
 
1151
  if(ret == -1){
 
1152
    perror("sigaction");
 
1153
    return EXIT_FAILURE;
 
1154
  }
 
1155
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1156
    ret = sigaction(SIGHUP, &sigterm_action, NULL);
 
1157
    if(ret == -1){
 
1158
      perror("sigaction");
 
1159
      exitcode = EXIT_FAILURE;
 
1160
      goto end;
 
1161
    }
 
1162
  }
 
1163
  ret = sigaction(SIGTERM, NULL, &old_sigterm_action);
 
1164
  if(ret == -1){
 
1165
    perror("sigaction");
 
1166
    return EXIT_FAILURE;
 
1167
  }
 
1168
  if(old_sigterm_action.sa_handler != SIG_IGN){
 
1169
    ret = sigaction(SIGTERM, &sigterm_action, NULL);
 
1170
    if(ret == -1){
 
1171
      perror("sigaction");
 
1172
      exitcode = EXIT_FAILURE;
 
1173
      goto end;
 
1174
    }
 
1175
  }
1059
1176
  
1060
1177
  /* If the interface is down, bring it up */
1061
1178
  if(interface[0] != '\0'){
 
1179
    if_index = (AvahiIfIndex) if_nametoindex(interface);
 
1180
    if(if_index == 0){
 
1181
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
 
1182
      exitcode = EXIT_FAILURE;
 
1183
      goto end;
 
1184
    }
 
1185
    
 
1186
    if(quit_now){
 
1187
      goto end;
 
1188
    }
 
1189
    
1062
1190
#ifdef __linux__
1063
1191
    /* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1064
1192
       messages to mess up the prompt */
1101
1229
    }
1102
1230
    if((network.ifr_flags & IFF_UP) == 0){
1103
1231
      network.ifr_flags |= IFF_UP;
 
1232
      take_down_interface = true;
1104
1233
      ret = ioctl(sd, SIOCSIFFLAGS, &network);
1105
1234
      if(ret == -1){
 
1235
        take_down_interface = false;
1106
1236
        perror("ioctl SIOCSIFFLAGS");
1107
1237
        exitcode = EXIT_FAILURE;
1108
1238
#ifdef __linux__
1130
1260
        perror("nanosleep");
1131
1261
      }
1132
1262
    }
1133
 
    ret = (int)TEMP_FAILURE_RETRY(close(sd));
1134
 
    if(ret == -1){
1135
 
      perror("close");
 
1263
    if(not take_down_interface){
 
1264
      /* We won't need the socket anymore */
 
1265
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1266
      if(ret == -1){
 
1267
        perror("close");
 
1268
      }
1136
1269
    }
1137
1270
#ifdef __linux__
1138
1271
    if(restore_loglevel){
1145
1278
#endif  /* __linux__ */
1146
1279
  }
1147
1280
  
 
1281
  if(quit_now){
 
1282
    goto end;
 
1283
  }
 
1284
  
1148
1285
  uid = getuid();
1149
1286
  gid = getgid();
1150
1287
  
 
1288
  /* Drop any group privileges we might have, just to be safe */
1151
1289
  errno = 0;
1152
 
  setgid(gid);
 
1290
  ret = setgid(gid);
1153
1291
  if(ret == -1){
1154
1292
    perror("setgid");
1155
1293
  }
1156
1294
  
1157
 
  ret = setuid(uid);
1158
 
  if(ret == -1){
1159
 
    perror("setuid");
 
1295
  /* Drop user privileges */
 
1296
  errno = 0;
 
1297
  /* Will we need privileges later? */
 
1298
  if(take_down_interface){
 
1299
    /* Drop user privileges temporarily */
 
1300
    ret = seteuid(uid);
 
1301
    if(ret == -1){
 
1302
      perror("seteuid");
 
1303
    }
 
1304
  } else {
 
1305
    /* Drop user privileges permanently */
 
1306
    ret = setuid(uid);
 
1307
    if(ret == -1){
 
1308
      perror("setuid");
 
1309
    }
 
1310
  }
 
1311
  
 
1312
  if(quit_now){
 
1313
    goto end;
1160
1314
  }
1161
1315
  
1162
1316
  ret = init_gnutls_global(pubkey, seckey);
1168
1322
    gnutls_initialized = true;
1169
1323
  }
1170
1324
  
 
1325
  if(quit_now){
 
1326
    goto end;
 
1327
  }
 
1328
  
 
1329
  tempdir_created = true;
1171
1330
  if(mkdtemp(tempdir) == NULL){
 
1331
    tempdir_created = false;
1172
1332
    perror("mkdtemp");
1173
1333
    goto end;
1174
1334
  }
1175
 
  tempdir_created = true;
 
1335
  
 
1336
  if(quit_now){
 
1337
    goto end;
 
1338
  }
1176
1339
  
1177
1340
  if(not init_gpgme(pubkey, seckey, tempdir)){
1178
1341
    fprintf(stderr, "init_gpgme failed\n");
1182
1345
    gpgme_initialized = true;
1183
1346
  }
1184
1347
  
1185
 
  if(interface[0] != '\0'){
1186
 
    if_index = (AvahiIfIndex) if_nametoindex(interface);
1187
 
    if(if_index == 0){
1188
 
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1189
 
      exitcode = EXIT_FAILURE;
1190
 
      goto end;
1191
 
    }
 
1348
  if(quit_now){
 
1349
    goto end;
1192
1350
  }
1193
1351
  
1194
1352
  if(connect_to != NULL){
1200
1358
      exitcode = EXIT_FAILURE;
1201
1359
      goto end;
1202
1360
    }
 
1361
    
 
1362
    if(quit_now){
 
1363
      goto end;
 
1364
    }
 
1365
    
1203
1366
    uint16_t port;
1204
1367
    errno = 0;
1205
1368
    tmpmax = strtoimax(address+1, &tmp, 10);
1209
1372
      exitcode = EXIT_FAILURE;
1210
1373
      goto end;
1211
1374
    }
 
1375
  
 
1376
    if(quit_now){
 
1377
      goto end;
 
1378
    }
 
1379
    
1212
1380
    port = (uint16_t)tmpmax;
1213
1381
    *address = '\0';
1214
1382
    address = connect_to;
1219
1387
    } else {
1220
1388
      af = AF_INET;
1221
1389
    }
 
1390
    
 
1391
    if(quit_now){
 
1392
      goto end;
 
1393
    }
 
1394
    
1222
1395
    ret = start_mandos_communication(address, port, if_index, af);
1223
1396
    if(ret < 0){
1224
1397
      exitcode = EXIT_FAILURE;
1227
1400
    }
1228
1401
    goto end;
1229
1402
  }
1230
 
    
 
1403
  
 
1404
  if(quit_now){
 
1405
    goto end;
 
1406
  }
 
1407
  
1231
1408
  {
1232
1409
    AvahiServerConfig config;
1233
1410
    /* Do not publish any local Zeroconf records */
1254
1431
    goto end;
1255
1432
  }
1256
1433
  
 
1434
  if(quit_now){
 
1435
    goto end;
 
1436
  }
 
1437
  
1257
1438
  /* Create the Avahi service browser */
1258
1439
  sb = avahi_s_service_browser_new(mc.server, if_index,
1259
1440
                                   AVAHI_PROTO_UNSPEC, "_mandos._tcp",
1265
1446
    goto end;
1266
1447
  }
1267
1448
  
 
1449
  if(quit_now){
 
1450
    goto end;
 
1451
  }
 
1452
  
1268
1453
  /* Run the main loop */
1269
1454
  
1270
1455
  if(debug){
1299
1484
    gpgme_release(mc.ctx);
1300
1485
  }
1301
1486
  
 
1487
  /* Take down the network interface */
 
1488
  if(take_down_interface){
 
1489
    /* Re-raise priviliges */
 
1490
    errno = 0;
 
1491
    ret = seteuid(0);
 
1492
    if(ret == -1){
 
1493
      perror("seteuid");
 
1494
    }
 
1495
    if(geteuid() == 0){
 
1496
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
1497
      if(ret == -1){
 
1498
        perror("ioctl SIOCGIFFLAGS");
 
1499
      } else if(network.ifr_flags & IFF_UP) {
 
1500
        network.ifr_flags &= ~IFF_UP; /* clear flag */
 
1501
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
1502
        if(ret == -1){
 
1503
          perror("ioctl SIOCSIFFLAGS");
 
1504
        }
 
1505
      }
 
1506
      ret = (int)TEMP_FAILURE_RETRY(close(sd));
 
1507
      if(ret == -1){
 
1508
        perror("close");
 
1509
      }
 
1510
      /* Lower privileges, permanently this time */
 
1511
      errno = 0;
 
1512
      ret = setuid(uid);
 
1513
      if(ret == -1){
 
1514
        perror("setuid");
 
1515
      }
 
1516
    }
 
1517
  }
 
1518
  
1302
1519
  /* Removes the temp directory used by GPGME */
1303
1520
  if(tempdir_created){
1304
1521
    DIR *d;
1343
1560
    }
1344
1561
  }
1345
1562
  
 
1563
  if(quit_now){
 
1564
    sigemptyset(&old_sigterm_action.sa_mask);
 
1565
    old_sigterm_action.sa_handler = SIG_DFL;
 
1566
    ret = sigaction(signal_received, &old_sigterm_action, NULL);
 
1567
    if(ret == -1){
 
1568
      perror("sigaction");
 
1569
    }
 
1570
    raise(signal_received);
 
1571
  }
 
1572
  
1346
1573
  return exitcode;
1347
1574
}