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