/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/password-request.c

merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
#include <string.h>             /* memset(), strcmp(), strlen(),
45
45
                                   strerror(), memcpy(), strcpy() */
46
46
#include <sys/ioctl.h>          /* ioctl */
 
47
#include <net/if.h>             /* ifreq, SIOCGIFFLAGS, SIOCSIFFLAGS,
 
48
                                   IFF_UP */
47
49
#include <sys/types.h>          /* socket(), inet_pton(), sockaddr,
48
50
                                   sockaddr_in6, PF_INET6,
49
51
                                   SOCK_STREAM, INET6_ADDRSTRLEN,
50
52
                                   uid_t, gid_t */
51
 
#include <inttypes.h>           /* PRIu16 */
52
53
#include <sys/socket.h>         /* socket(), struct sockaddr_in6,
53
54
                                   struct in6_addr, inet_pton(),
54
55
                                   connect() */
99
100
bool debug = false;
100
101
static const char *keydir = "/conf/conf.d/mandos";
101
102
static const char mandos_protocol_version[] = "1";
102
 
const char *argp_program_version = "password-request 1.0";
 
103
const char *argp_program_version = "mandosclient 0.9";
103
104
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
104
105
 
105
106
/* Used for passing in values through the Avahi callback functions */
227
228
    } else {
228
229
      fprintf(stderr, "Unsupported algorithm: %s\n",
229
230
              result->unsupported_algorithm);
230
 
      fprintf(stderr, "Wrong key usage: %u\n",
 
231
      fprintf(stderr, "Wrong key usage: %d\n",
231
232
              result->wrong_key_usage);
232
233
      if(result->file_name != NULL){
233
234
        fprintf(stderr, "File name: %s\n", result->file_name);
454
455
  }
455
456
  
456
457
  if(debug){
457
 
    fprintf(stderr, "Setting up a tcp connection to %s, port %" PRIu16
458
 
            "\n", ip, port);
 
458
    fprintf(stderr, "Setting up a tcp connection to %s, port %d\n",
 
459
            ip, port);
459
460
  }
460
461
  
461
462
  tcp_sd = socket(PF_INET6, SOCK_STREAM, 0);
485
486
    fprintf(stderr, "Bad address: %s\n", ip);
486
487
    return -1;
487
488
  }
488
 
  to.in6.sin6_port = htons(port); /* Spurious warning */
 
489
  to.in6.sin6_port = htons(port);       /* Spurious warning */
489
490
  
490
491
  to.in6.sin6_scope_id = (uint32_t)if_index;
491
492
  
492
493
  if(debug){
493
 
    fprintf(stderr, "Connection to: %s, port %" PRIu16 "\n", ip,
494
 
            port);
 
494
    fprintf(stderr, "Connection to: %s, port %d\n", ip, port);
495
495
    char addrstr[INET6_ADDRSTRLEN] = "";
496
496
    if(inet_ntop(to.in6.sin6_family, &(to.in6.sin6_addr), addrstr,
497
497
                 sizeof(addrstr)) == NULL){
676
676
      char ip[AVAHI_ADDRESS_STR_MAX];
677
677
      avahi_address_snprint(ip, sizeof(ip), address);
678
678
      if(debug){
679
 
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %"
680
 
                PRIu16 ") on port %d\n", name, host_name, ip,
681
 
                interface, port);
 
679
        fprintf(stderr, "Mandos server \"%s\" found on %s (%s, %d) on"
 
680
                " port %d\n", name, host_name, ip, interface, port);
682
681
      }
683
682
      int ret = start_mandos_communication(ip, port, interface, mc);
684
683
      if (ret == 0){
685
 
        avahi_simple_poll_quit(mc->simple_poll);
 
684
        exit(EXIT_SUCCESS);
686
685
      }
687
686
    }
688
687
  }
850
849
          break;
851
850
        case ARGP_KEY_ARG:
852
851
          argp_usage (state);
853
 
        case ARGP_KEY_END:
854
852
          break;
 
853
          case ARGP_KEY_END:
 
854
            break;
855
855
        default:
856
856
          return ARGP_ERR_UNKNOWN;
857
857
        }
864
864
                           " passwords from mandos server" };
865
865
      ret = argp_parse (&argp, argc, argv, 0, 0, NULL);
866
866
      if (ret == ARGP_ERR_UNKNOWN){
867
 
        fprintf(stderr, "Unknown error while parsing arguments\n");
 
867
        fprintf(stderr, "Unkown error while parsing arguments\n");
868
868
        exitcode = EXIT_FAILURE;
869
869
        goto end;
870
870
      }
880
880
    seckeyfile = combinepath(keydir, seckeyfile);
881
881
    if (seckeyfile == NULL){
882
882
      perror("combinepath");
883
 
      exitcode = EXIT_FAILURE;
884
883
      goto end;
885
884
    }
886
885
 
887
886
    ret = init_gnutls_global(&mc, pubkeyfile, seckeyfile);
888
887
    if (ret == -1){
889
 
      fprintf(stderr, "init_gnutls_global failed\n");
890
 
      exitcode = EXIT_FAILURE;
 
888
      fprintf(stderr, "init_gnutls_global\n");
891
889
      goto end;
892
890
    } else {
893
891
      gnutls_initalized = true;
894
892
    }
895
 
    
896
 
    /* If the interface is down, bring it up */
897
 
    {
898
 
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
899
 
      if(sd < 0) {
900
 
        perror("socket");
901
 
        exitcode = EXIT_FAILURE;
902
 
        goto end;
903
 
      }
904
 
      strcpy(network.ifr_name, interface); /* Spurious warning */
905
 
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
906
 
      if(ret == -1){
907
 
        perror("ioctl SIOCGIFFLAGS");
908
 
        exitcode = EXIT_FAILURE;
909
 
        goto end;
910
 
      }
911
 
      if((network.ifr_flags & IFF_UP) == 0){
912
 
        network.ifr_flags |= IFF_UP;
913
 
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
914
 
        if(ret == -1){
915
 
          perror("ioctl SIOCSIFFLAGS");
916
 
          exitcode = EXIT_FAILURE;
917
 
          goto end;
918
 
        }
919
 
      }
920
 
      close(sd);
921
 
    }
922
 
    
 
893
 
923
894
    uid = getuid();
924
895
    gid = getgid();
925
 
    
 
896
 
926
897
    ret = setuid(uid);
927
898
    if (ret == -1){
928
899
      perror("setuid");
966
937
      goto end;
967
938
    }
968
939
    
 
940
    /* If the interface is down, bring it up */
 
941
    {
 
942
      sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
 
943
      if(sd < 0) {
 
944
        perror("socket");
 
945
        exitcode = EXIT_FAILURE;
 
946
        goto end;
 
947
      }
 
948
      strcpy(network.ifr_name, interface); /* Spurious warning */
 
949
      ret = ioctl(sd, SIOCGIFFLAGS, &network);
 
950
      if(ret == -1){
 
951
        perror("ioctl SIOCGIFFLAGS");
 
952
        exitcode = EXIT_FAILURE;
 
953
        goto end;
 
954
      }
 
955
      if((network.ifr_flags & IFF_UP) == 0){
 
956
        network.ifr_flags |= IFF_UP;
 
957
        ret = ioctl(sd, SIOCSIFFLAGS, &network);
 
958
        if(ret == -1){
 
959
          perror("ioctl SIOCSIFFLAGS");
 
960
          exitcode = EXIT_FAILURE;
 
961
          goto end;
 
962
        }
 
963
      }
 
964
      close(sd);
 
965
    }
 
966
    
969
967
    if (not debug){
970
968
      avahi_set_log_function(empty_log);
971
969
    }