summaryrefslogtreecommitdiff
path: root/usr.bin/tcfs/tcfsputkey.c
blob: 362355ee3d0e9e3a7bfcabb12f4a4e7efa6ca7c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 *	Transparent Cryptographic File System (TCFS) for NetBSD 
 *	Author and mantainer: 	Luigi Catuogno [luicat@tcfs.unisa.it]
 *	
 *	references:		http://tcfs.dia.unisa.it
 *				tcfs-bsd@tcfs.unisa.it
 */

/*
 *	Base utility set v0.1
 *
 *	  $Source: /cvs/OpenBSD/src/usr.bin/tcfs/Attic/tcfsputkey.c,v $
 *	   $State: Exp $
 *	$Revision: 1.1 $
 *	  $Author: provos $
 *	    $Date: 2000/06/18 22:07:24 $
 *
 */

static const char *RCSid="$id: $";

/* RCS_HEADER_ENDS_HERE */



#include <stdio.h>
#include <sys/types.h>
#include <sys/param.h>
#include <ctype.h>
#include <pwd.h>
#include <unistd.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <des.h>

#include <miscfs/tcfs/tcfs.h>
#include "tcfslib.h"
#include "tcfserrors.h"
#include <grp.h>

extern char *optarg;
extern int optind;
char *putkey_usage=
"usage: tcfsputkey [-k][-f fliesystem-label][-g group][-p mount-point]\n";

int
putkey_main(int argc, char *argv[])
{
	u_char *key,*fs,*user,*password,*tcfskey;
	uid_t uid;
	gid_t gid;
	int es, treshold;
	char x;
	tcfspwdb *info;
	tcfsgpwdb *ginfo;
	char fslabel[MAXPATHLEN], fspath[MAXPATHLEN];
	int def = TRUE, havempname = FALSE, havefsname = FALSE;
	int isgroupkey = FALSE;
	int havefs = FALSE;
	int havename = FALSE, havefspath = FALSE, havekey = FALSE;

	while ((x = getopt(argc,argv,"kf:p:g:")) != EOF) {
		switch(x) {
		case 'k':
			def = FALSE;
			break;
		case 'p':
			havempname = TRUE;
			strlcpy(fspath, optarg, sizeof(fspath));
			break;
		case 'f':
			havefsname = TRUE;
			strlcpy(fslabel, optarg, sizeof(fslabel));
			break;
		case 'g':
			isgroupkey = TRUE;
			def = TRUE;
			gid = atoi(optarg);
			if (!gid && optarg[0] != 0) {
				struct group *grp;
				grp = (struct group *)getgrnam(optarg);
				if (!grp)
					tcfs_error(ER_CUSTOM, "Nonexistant group\n");
				gid = grp->gr_gid;
			}
			break;
		default: 
			tcfs_error(ER_CUSTOM, putkey_usage);
			exit(ER_UNKOPT);
		}
	}
	if (argc - optind)
		tcfs_error(ER_UNKOPT,NULL);

	if (havefsname && havempname) {
		tcfs_error(ER_CUSTOM, putkey_usage);
		exit(1);
	}
			 
	if (havefsname) {
		es=tcfs_getfspath(fslabel,fspath);
		havename = TRUE;
	}

	if (havefspath)
		havename = TRUE;

	if (!havename)
		es=tcfs_getfspath("default",fspath);

	if (!es) {
		tcfs_error(ER_CUSTOM,"fs-label not found!\n");
		exit(1);
	}

	uid = getuid();
		
	if (isgroupkey) {
		if (!unix_auth(&user,&password,TRUE))
			tcfs_error(ER_AUTH,user);

		if (!tcfsgpwdbr_new(&ginfo))
			tcfs_error(ER_MEM,NULL);

		if (!tcfs_ggetpwnam(user,gid,&ginfo))
			tcfs_error(ER_CUSTOM,"Default key non found");

		if (!strlen(ginfo->gkey))
			tcfs_error(ER_CUSTOM,"Invalid default key");

		tcfskey = (char*)malloc(UUKEYSIZE);
		if (!tcfskey)
			tcfs_error(ER_MEM,NULL);	

		treshold = ginfo->soglia;

		tcfs_decrypt_key(user, password, ginfo->gkey, tcfskey,
				 GROUPKEY);

		es = tcfs_group_enable(fspath,uid,gid,treshold,tcfskey);

		if(es == -1) {
			tcfs_error(ER_CUSTOM,"problems updating filesystem");
		}

		exit(0);
	}


	if(!def) {
		tcfskey = getpass("Insert tcfs-key:");
		havekey = TRUE;
	} else {
		if(!unix_auth(&user,&password,TRUE))
			tcfs_error(ER_AUTH,user);
				
		if(!tcfspwdbr_new(&info))
			tcfs_error(ER_MEM,NULL);	

		if(!tcfs_getpwnam(user,&info))
			tcfs_error(ER_CUSTOM,"Default key non found");
	
		if(!strlen(info->upw))
			tcfs_error(ER_CUSTOM,"Invalid default key");

		tcfskey = (char*)malloc(UUKEYSIZE);
		if(!tcfskey)
			tcfs_error(ER_MEM,NULL);	
		
		tcfs_decrypt_key (user, password, info->upw, tcfskey, USERKEY);
		havekey = TRUE;
	}

	es = tcfs_user_enable(fspath, uid, tcfskey);

	if(es == -1)
		tcfs_error(ER_CUSTOM,"problems updating filesystem");

	exit(0);
}