Annotation of src/usr.bin/tcfs/tcfsgenkey.c, Revision 1.1.1.1
1.1 provos 1: /*
2: * Transparent Cryptographic File System (TCFS) for NetBSD
3: * Author and mantainer: Luigi Catuogno [luicat@tcfs.unisa.it]
4: *
5: * references: http://tcfs.dia.unisa.it
6: * tcfs-bsd@tcfs.unisa.it
7: */
8:
9: /*
10: * Base utility set v0.1
11: */
12:
13: #include <stdio.h>
14: #include <strings.h>
15:
16: #include <miscfs/tcfs/tcfs.h>
17: #include "tcfslib.h"
18: #include "tcfserrors.h"
19:
20: char *genkey_usage="Usage: %s [OPTION]
21: Generate a TCFS key adding it to the user entry into the TCFS database.
22:
23: -h Shows this help\n";
24:
25: int
26: genkey_main (int argn, char *argv[])
27: {
28: int val;
29: char *user, *passwd;
30: tcfspwdb *userinfo;
31: unsigned char *newkey, *cryptedkey;
32: tcfspwdb *user_info = NULL;
33:
34: /*
35: * Going to check arguments
36: */
37: while ((val = getopt(argn, argv, "h")) != EOF)
38: switch (val) {
39: case 'h':
40: show_usage(genkey_usage, argv[0]);
41: exit (OK);
42: break; /* Useless code */
43: default:
44: fprintf (stderr, "Try %s --help for more information.\n", argv[0]);
45: exit (ER_UNKOPT);
46: break;
47: }
48:
49: if (argn - optind)
50: tcfs_error (ER_UNKOPT, NULL);
51:
52: /*
53: * Must be root to do all this stuff
54: */
55: if (geteuid())
56: tcfs_error (ER_CUSTOM, "I don't have root privileges!");
57:
58: /*
59: * Authenticate user
60: */
61: if (!unix_auth (&user, &passwd, TRUE))
62: tcfs_error (ER_CUSTOM, "Who are you?!");
63:
64: if (!tcfs_getpwnam (user, &user_info))
65: tcfs_error (ER_CUSTOM, "You do not have an entry in the TCFS key database.");
66:
67: if (strlen(user_info->upw))
68: tcfs_error (ER_CUSTOM,"You already have a TCFS key.");
69:
70: /*
71: * Generate a new key for the user.
72: */
73: newkey = gentcfskey ();
74: {
75: int i;
76: printf("gentcfskey: key =");
77: for (i = 0; i < KEYSIZE; i++)
78: printf(" %02x", newkey[i]);
79: printf("\n");
80: }
81:
82: /*
83: * Encrypt the generated key with user password
84: */
85: cryptedkey = (char*)calloc(UUKEYSIZE, sizeof(char));
86: if (!cryptedkey)
87: tcfs_error (ER_MEM, NULL);
88:
89:
90: if (!tcfs_encrypt_key (user, passwd, newkey, cryptedkey, USERKEY))
91: tcfs_error (ER_MEM, NULL);
92:
93: /*
94: * Update TCFS key database
95: */
96: if (!tcfspwdbr_new (&userinfo))
97: tcfs_error (ER_MEM, NULL);
98:
99: if (!tcfspwdbr_edit (&userinfo, F_USR|F_PWD, user, cryptedkey))
100: tcfs_error (ER_MEM, NULL);
101:
102: /* TODO:
103: if (!change && tcfs_getpwnam (user, &userinfo))
104: tcfs_error (ER_CUSTOM, "Use -c to change the key.");
105: */
106:
107: if (!tcfs_putpwnam (user, userinfo, U_CHG))
108: tcfs_error (ER_CUSTOM, "Error: cannot generate key.");
109:
110: tcfs_error (ER_CUSTOM, "\nKey succesfully generated.");
111: }