ccninitkeystore.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <unistd.h>
00024 #include <errno.h>
00025 #include <pwd.h>
00026 #include <sys/stat.h>
00027 #include <ccn/ccn.h>
00028 #include <ccn/charbuf.h>
00029 #include <ccn/keystore.h>
00030
00031 #define CCN_KEYSTORE_PASS "Th1s1sn0t8g00dp8ssw0rd."
00032
00033 static void
00034 usage(const char *progname)
00035 {
00036 fprintf(stderr,
00037 "%s [-h] [-f] [-u username] [-l keylength] [-v validity] [directory]\n"
00038 " Initialize a CCNx keystore with given parameters\n", progname);
00039 fprintf(stderr,
00040 " -h Display this help message.\n"
00041 " -f Force overwriting an existing keystore. Default no overwrite permitted.\n"
00042 " -u username Username for this keystore. Default username of effective uid.\n"
00043 " -l keylength Length of RSA key to be generated. Default 1024 bits.\n"
00044 " -v validity Number of days that certificate should be valid. Default 30.\n"
00045 " directory Directory in which to create .ccnx/.ccnx_keystore. Default $HOME.\n"
00046 );
00047 }
00048
00049 int
00050 main(int argc, char **argv)
00051 {
00052 int res;
00053 int opt;
00054 struct stat statbuf;
00055 char *dir;
00056 struct ccn_charbuf *keystore = NULL;
00057 int force = 0;
00058 char *user = NULL;
00059 char useruid[32];
00060 struct passwd *pwd = NULL;
00061 int keylength = 0;
00062 int validity = 0;
00063
00064 while ((opt = getopt(argc, argv, "hfu:p:l:v:")) != -1) {
00065 switch (opt) {
00066 case 'f':
00067 force = 1;
00068 break;
00069 case 'u':
00070 user = optarg;
00071 break;
00072 case 'l':
00073 keylength = atoi(optarg);
00074 if (keylength < 512) {
00075 fprintf(stderr, "%d: Key length too short for signing CCNx objects.\n", keylength);
00076 exit(1);
00077 }
00078 break;
00079 case 'v':
00080 validity = atoi(optarg);
00081 if (validity < 0) {
00082 fprintf(stderr, "%d: Certificate validity must be > 0.\n", validity);
00083 exit(1);
00084 }
00085 break;
00086 case 'h':
00087 default:
00088 usage(argv[0]);
00089 exit(1);
00090 }
00091 }
00092 dir = argv[optind];
00093 if (dir == NULL){
00094 dir = getenv("HOME");
00095 }
00096 res = stat(dir, &statbuf);
00097 if (res == -1) {
00098 perror(dir);
00099 exit(1);
00100 } else if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
00101 errno = ENOTDIR;
00102 perror(dir);
00103 exit(1);
00104 }
00105 keystore = ccn_charbuf_create();
00106 ccn_charbuf_putf(keystore, "%s/.ccnx", dir);
00107 res = stat(ccn_charbuf_as_string(keystore), &statbuf);
00108 if (res == -1) {
00109 res = mkdir(ccn_charbuf_as_string(keystore), 0700);
00110 if (res != 0) {
00111 perror(ccn_charbuf_as_string(keystore));
00112 exit(1);
00113 }
00114 }
00115 ccn_charbuf_append_string(keystore, "/.ccnx_keystore");
00116 res = stat(ccn_charbuf_as_string(keystore), &statbuf);
00117 if (res != -1 && !force) {
00118 errno=EEXIST;
00119 perror(ccn_charbuf_as_string(keystore));
00120 exit(1);
00121 }
00122 if (user == NULL) {
00123 errno = 0;
00124 pwd = getpwuid(geteuid());
00125 if (pwd != NULL)
00126 user = pwd->pw_name;
00127 else {
00128 if (errno != 0) {
00129 perror("getpwuid");
00130 exit(1);
00131 }
00132 snprintf(useruid, sizeof(useruid), "uid%d", geteuid());
00133 user = useruid;
00134 }
00135 }
00136 res = ccn_keystore_file_init(ccn_charbuf_as_string(keystore), CCN_KEYSTORE_PASS,
00137 user, keylength, validity);
00138 if (res != 0) {
00139 if (errno != 0)
00140 perror(ccn_charbuf_as_string(keystore));
00141 else
00142 fprintf(stderr, "ccn_keystore_file_init: invalid argument\n");
00143 exit(1);
00144 }
00145 return(0);
00146 }