summaryrefslogtreecommitdiff
path: root/usr.bin/tcfs/tcfs_keymaint.c
blob: 188c60a0e76deee15798ab9170eff1bc557ac53d (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*	$OpenBSD: tcfs_keymaint.c,v 1.9 2000/06/19 23:06:25 aaron Exp $	*/

/*
 *	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
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/ucred.h>
#include <blf.h>
#include <ctype.h>
#include <pwd.h>
#include <string.h>
#include <unistd.h>

#include <miscfs/tcfs/tcfs.h>
#include <miscfs/tcfs/tcfs_cmd.h>

#include "tcfsdefines.h"
#include "uuencode.h"

int
tcfs_verify_fs(char *fs)
{
	int ret;
	struct statfs buf;

	ret = statfs(fs, &buf);

	if (ret)
		 return (0);

	if (!strcmp("tcfs", buf.f_fstypename))
		return (1);
	else	
		return (0);
}

int
tcfs_callfunction(char *filesystem, struct tcfs_args *arg)
{
	int i;

	if (tcfs_verify_fs(filesystem))
		i = mount("tcfs", filesystem, MNT_UPDATE, (void *)arg);
	else
		i = -1;
	
	return (i);
}

int 
tcfs_decrypt_key(char *pwd, u_char *t, u_char *tk, int tklen)
{
	char pass[_PASSWORD_LEN];
	char tcfskey[KEYSIZE * 2], iv[8];
	blf_ctx ctx;
	int len;

	if (!tk)
		return (0);

	strlcpy(pass, pwd, sizeof(pass));

	len = uudecode((char *)t, tcfskey, sizeof(tcfskey));
	if (len == -1) {
		fprintf(stderr, "tcfs_decrypt_key: uudecode failed\n");
		return (0);
	} else 	if (len != tklen) {
		fprintf(stderr, "tcfs_decrypt_key: uudecode wrong length\n");
		return (0);
	}

	while (strlen (pass) < 8) {
		char tmp[_PASSWORD_LEN];

		strcpy(tmp, pass);
		strcat(tmp, pass);
		strcat(pass, tmp);
	}

	blf_key(&ctx, pass, strlen(pass));
	memset(iv, 0, sizeof(iv));
	blf_cbc_decrypt(&ctx, iv, tcfskey, tklen);

	memset(pass, 0, strlen(pass));
	memset(&ctx, 0, sizeof(ctx));

	memcpy(tk, tcfskey, tklen);
	return (1);
}

int 
tcfs_encrypt_key(char *pw, u_char *key, int klen, u_char *ek, int eklen)
{
	char pass[_PASSWORD_LEN], iv[8];
	blf_ctx ctx;
	int res;

	if (!ek)
		return (0);

	strlcpy(pass, pw, sizeof(pass));

	while (strlen(pass) < 8) {
		char tmp[_PASSWORD_LEN];
      
		strcpy(tmp, pass);
		strcat(tmp, pass);
		strcat(pass, tmp);
	}
	
	blf_key(&ctx, pass, strlen(pass));
	memset(iv, 0, sizeof(iv));
	blf_cbc_encrypt(&ctx, iv, key, klen);

	memset(&ctx, 0, sizeof(ctx));

	res = uuencode(key, klen, ek, eklen);
	if (res != eklen - 1) {
		fprintf(stderr, "tcfs_encrypt_key: uuencode length wrong\n");
		return (0);
	}

	return (1);
}

int
tcfs_user_enable(char *filesystem, uid_t user, u_char *key)
{
	struct tcfs_args a;

	a.user = user;
	memcpy(a.tcfs_key, key, sizeof(a.tcfs_key));
	a.cmd = TCFS_PUT_UIDKEY;

	return (tcfs_callfunction(filesystem, &a));
}

int
tcfs_user_disable(char *filesystem, uid_t user)
{
	struct tcfs_args a;

	a.user = user;
	a.cmd = TCFS_RM_UIDKEY;

	return (tcfs_callfunction(filesystem, &a));
}

int
tcfs_proc_enable(char *filesystem, uid_t user, pid_t pid, char *key)
{
	struct tcfs_args a;

	a.user = user;
	a.cmd = TCFS_PUT_PIDKEY;
	a.proc = pid;
	memcpy(a.tcfs_key, key, sizeof(a.tcfs_key));

	return (tcfs_callfunction(filesystem, &a));
}

int
tcfs_proc_disable(char *filesystem, uid_t user, pid_t pid)
{
	struct tcfs_args a;

	a.user = user;
	a.cmd = TCFS_RM_PIDKEY;
	a.proc = pid;

	return (tcfs_callfunction(filesystem, &a));
}

int
tcfs_group_enable(char *filesystem, uid_t uid, gid_t gid, 
		  int tre, char *key)
{
	struct tcfs_args a;

	a.cmd = TCFS_PUT_GIDKEY;
	a.user = uid;
	a.group = gid;
	a.treshold = tre;
	memcpy(a.tcfs_key, key, sizeof(a.tcfs_key));

	return (tcfs_callfunction(filesystem, &a));
}

int tcfs_group_disable(char *filesystem, uid_t uid, gid_t gid)
{
	struct tcfs_args a;

	a.cmd = TCFS_RM_GIDKEY;
	a.user = uid;
	a.group = gid;

	return (tcfs_callfunction(filesystem, &a));
}