/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-01-17 02:34:57 UTC
  • mfrom: (237.2.11 mandos-release)
  • Revision ID: teddy@fukt.bsnet.se-20090117023457-1ll0f30sr9dzwto7
Merge from release branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#define _GNU_SOURCE             /* TEMP_FAILURE_RETRY(), asprintf() */
37
37
 
38
38
#include <stdio.h>              /* fprintf(), stderr, fwrite(),
39
 
                                   stdout, ferror(), sscanf(),
40
 
                                   remove() */
 
39
                                   stdout, ferror(), sscanf */
41
40
#include <stdint.h>             /* uint16_t, uint32_t */
42
41
#include <stddef.h>             /* NULL, size_t, ssize_t */
43
42
#include <stdlib.h>             /* free(), EXIT_SUCCESS, EXIT_FAILURE,
154
153
  
155
154
  
156
155
  /*
157
 
   * Helper function to insert pub and seckey to the engine keyring.
 
156
   * Helper function to insert pub and seckey to the enigne keyring.
158
157
   */
159
158
  bool import_key(const char *filename){
160
159
    int fd;
832
831
    gid_t gid;
833
832
    char *connect_to = NULL;
834
833
    char tempdir[] = "/tmp/mandosXXXXXX";
835
 
    bool tempdir_created = false;
836
834
    AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
837
835
    const char *seckey = PATHDIR "/" SECKEY;
838
836
    const char *pubkey = PATHDIR "/" PUBKEY;
840
838
    mandos_context mc = { .simple_poll = NULL, .server = NULL,
841
839
                          .dh_bits = 1024, .priority = "SECURE256"
842
840
                          ":!CTYPE-X.509:+CTYPE-OPENPGP" };
843
 
    bool gnutls_initialized = false;
844
 
    bool gpgme_initialized = false;
 
841
    bool gnutls_initalized = false;
 
842
    bool gpgme_initalized = false;
845
843
    
846
844
    {
847
845
      struct argp_option options[] = {
961
959
    uid = getuid();
962
960
    gid = getgid();
963
961
    
 
962
    ret = setuid(uid);
 
963
    if(ret == -1){
 
964
      perror("setuid");
 
965
    }
 
966
    
964
967
    setgid(gid);
965
968
    if(ret == -1){
966
969
      perror("setgid");
967
970
    }
968
971
    
969
 
    ret = setuid(uid);
970
 
    if(ret == -1){
971
 
      perror("setuid");
972
 
    }
973
 
    
974
972
    ret = init_gnutls_global(&mc, pubkey, seckey);
975
973
    if(ret == -1){
976
974
      fprintf(stderr, "init_gnutls_global failed\n");
977
975
      exitcode = EXIT_FAILURE;
978
976
      goto end;
979
977
    } else {
980
 
      gnutls_initialized = true;
 
978
      gnutls_initalized = true;
981
979
    }
982
980
    
983
981
    if(mkdtemp(tempdir) == NULL){
984
982
      perror("mkdtemp");
 
983
      tempdir[0] = '\0';
985
984
      goto end;
986
985
    }
987
 
    tempdir_created = true;
988
986
    
989
987
    if(not init_gpgme(&mc, pubkey, seckey, tempdir)){
990
 
      fprintf(stderr, "init_gpgme failed\n");
 
988
      fprintf(stderr, "gpgme_initalized failed\n");
991
989
      exitcode = EXIT_FAILURE;
992
990
      goto end;
993
991
    } else {
994
 
      gpgme_initialized = true;
 
992
      gpgme_initalized = true;
995
993
    }
996
994
    
997
995
    if_index = (AvahiIfIndex) if_nametoindex(interface);
998
996
    if(if_index == 0){
999
997
      fprintf(stderr, "No such interface: \"%s\"\n", interface);
1000
 
      exitcode = EXIT_FAILURE;
1001
 
      goto end;
 
998
      exit(EXIT_FAILURE);
1002
999
    }
1003
1000
    
1004
1001
    if(connect_to != NULL){
1108
1105
    if(mc.simple_poll != NULL)
1109
1106
        avahi_simple_poll_free(mc.simple_poll);
1110
1107
    
1111
 
    if(gnutls_initialized){
 
1108
    if(gnutls_initalized){
1112
1109
      gnutls_certificate_free_credentials(mc.cred);
1113
1110
      gnutls_global_deinit();
1114
1111
      gnutls_dh_params_deinit(mc.dh_params);
1115
1112
    }
1116
1113
    
1117
 
    if(gpgme_initialized){
 
1114
    if(gpgme_initalized){
1118
1115
      gpgme_release(mc.ctx);
1119
1116
    }
1120
1117
    
1121
1118
    /* Removes the temp directory used by GPGME */
1122
 
    if(tempdir_created){
 
1119
    if(tempdir[0] != '\0'){
1123
1120
      DIR *d;
1124
1121
      struct dirent *direntry;
1125
1122
      d = opendir(tempdir);
1133
1130
          if(direntry == NULL){
1134
1131
            break;
1135
1132
          }
1136
 
          /* Skip "." and ".." */
1137
 
          if(direntry->d_name[0] == '.'
1138
 
             and (direntry->d_name[1] == '\0'
1139
 
                  or (direntry->d_name[1] == '.'
1140
 
                      and direntry->d_name[2] == '\0'))){
1141
 
            continue;
1142
 
          }
1143
 
          char *fullname = NULL;
1144
 
          ret = asprintf(&fullname, "%s/%s", tempdir,
1145
 
                         direntry->d_name);
1146
 
          if(ret < 0){
1147
 
            perror("asprintf");
1148
 
            continue;
1149
 
          }
1150
 
          ret = remove(fullname);
1151
 
          if(ret == -1){
1152
 
            fprintf(stderr, "remove(\"%s\"): %s\n", fullname,
1153
 
                    strerror(errno));
1154
 
          }
1155
 
          free(fullname);
 
1133
          if(direntry->d_type == DT_REG){
 
1134
            char *fullname = NULL;
 
1135
            ret = asprintf(&fullname, "%s/%s", tempdir,
 
1136
                           direntry->d_name);
 
1137
            if(ret < 0){
 
1138
              perror("asprintf");
 
1139
              continue;
 
1140
            }
 
1141
            ret = unlink(fullname);
 
1142
            if(ret == -1){
 
1143
              fprintf(stderr, "unlink(\"%s\"): %s",
 
1144
                      fullname, strerror(errno));
 
1145
            }
 
1146
            free(fullname);
 
1147
          }
1156
1148
        }
1157
1149
        closedir(d);
1158
1150
      }
1161
1153
        perror("rmdir");
1162
1154
      }
1163
1155
    }
1164
 
    
 
1156
          
1165
1157
    return exitcode;
1166
1158
}