Annotation of src/usr.bin/tcfs/tcfsputkey.c, Revision 1.2
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 <sys/types.h>
15: #include <sys/param.h>
16: #include <ctype.h>
17: #include <pwd.h>
18: #include <unistd.h>
19: #include <sys/param.h>
20: #include <sys/mount.h>
21: #include <des.h>
22:
23: #include <miscfs/tcfs/tcfs.h>
24: #include "tcfslib.h"
25: #include "tcfserrors.h"
26: #include <grp.h>
27:
28: extern char *optarg;
29: extern int optind;
30: char *putkey_usage=
31: "usage: tcfsputkey [-k][-f fliesystem-label][-g group][-p mount-point]\n";
32:
33: int
34: putkey_main(int argc, char *argv[])
35: {
36: u_char *key,*fs,*user,*password,*tcfskey;
37: uid_t uid;
38: gid_t gid;
39: int es, treshold;
40: char x;
41: tcfspwdb *info;
42: tcfsgpwdb *ginfo;
43: char fslabel[MAXPATHLEN], fspath[MAXPATHLEN];
44: int def = TRUE, havempname = FALSE, havefsname = FALSE;
45: int isgroupkey = FALSE;
46: int havefs = FALSE;
47: int havename = FALSE, havefspath = FALSE, havekey = FALSE;
48:
49: while ((x = getopt(argc,argv,"kf:p:g:")) != EOF) {
50: switch(x) {
51: case 'k':
52: def = FALSE;
53: break;
54: case 'p':
55: havempname = TRUE;
56: strlcpy(fspath, optarg, sizeof(fspath));
57: break;
58: case 'f':
59: havefsname = TRUE;
60: strlcpy(fslabel, optarg, sizeof(fslabel));
61: break;
62: case 'g':
63: isgroupkey = TRUE;
64: def = TRUE;
65: gid = atoi(optarg);
66: if (!gid && optarg[0] != 0) {
67: struct group *grp;
68: grp = (struct group *)getgrnam(optarg);
69: if (!grp)
70: tcfs_error(ER_CUSTOM, "Nonexistant group\n");
71: gid = grp->gr_gid;
72: }
73: break;
74: default:
75: tcfs_error(ER_CUSTOM, putkey_usage);
76: exit(ER_UNKOPT);
77: }
78: }
79: if (argc - optind)
80: tcfs_error(ER_UNKOPT,NULL);
81:
82: if (havefsname && havempname) {
83: tcfs_error(ER_CUSTOM, putkey_usage);
84: exit(1);
85: }
86:
87: if (havefsname) {
88: es=tcfs_getfspath(fslabel,fspath);
89: havename = TRUE;
90: }
91:
92: if (havefspath)
93: havename = TRUE;
94:
95: if (!havename)
96: es=tcfs_getfspath("default",fspath);
97:
98: if (!es) {
99: tcfs_error(ER_CUSTOM,"fs-label not found!\n");
100: exit(1);
101: }
102:
103: uid = getuid();
104:
105: if (isgroupkey) {
106: if (!unix_auth(&user,&password,TRUE))
107: tcfs_error(ER_AUTH,user);
108:
109: if (!tcfsgpwdbr_new(&ginfo))
110: tcfs_error(ER_MEM,NULL);
111:
112: if (!tcfs_ggetpwnam(user,gid,&ginfo))
113: tcfs_error(ER_CUSTOM,"Default key non found");
114:
115: if (!strlen(ginfo->gkey))
116: tcfs_error(ER_CUSTOM,"Invalid default key");
117:
118: tcfskey = (char*)malloc(UUKEYSIZE);
119: if (!tcfskey)
120: tcfs_error(ER_MEM,NULL);
121:
122: treshold = ginfo->soglia;
123:
124: tcfs_decrypt_key(user, password, ginfo->gkey, tcfskey,
125: GROUPKEY);
126:
127: es = tcfs_group_enable(fspath,uid,gid,treshold,tcfskey);
128:
129: if(es == -1) {
130: tcfs_error(ER_CUSTOM,"problems updating filesystem");
131: }
132:
133: exit(0);
134: }
135:
136:
137: if(!def) {
138: tcfskey = getpass("Insert tcfs-key:");
139: havekey = TRUE;
140: } else {
141: if(!unix_auth(&user,&password,TRUE))
142: tcfs_error(ER_AUTH,user);
143:
144: if(!tcfspwdbr_new(&info))
145: tcfs_error(ER_MEM,NULL);
146:
147: if(!tcfs_getpwnam(user,&info))
148: tcfs_error(ER_CUSTOM,"Default key non found");
149:
150: if(!strlen(info->upw))
151: tcfs_error(ER_CUSTOM,"Invalid default key");
152:
153: tcfskey = (char*)malloc(UUKEYSIZE);
154: if(!tcfskey)
155: tcfs_error(ER_MEM,NULL);
156:
157: tcfs_decrypt_key (user, password, info->upw, tcfskey, USERKEY);
158: havekey = TRUE;
159: }
160:
161: es = tcfs_user_enable(fspath, uid, tcfskey);
162:
163: if(es == -1)
164: tcfs_error(ER_CUSTOM,"problems updating filesystem");
165:
166: exit(0);
167: }