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