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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/**
* Copyright (C) 2006 International Business Machines Corp.
* Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
* Trevor Highland <trevor.highland@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "config.h"
#ifndef S_SPLINT_S
#include <sys/types.h>
#include <sys/stat.h>
#endif /* S_SPLINT_S */
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#ifndef S_SPLINT_S
#include <stdio.h>
#include <syslog.h>
#endif /* S_SPLINT_S */
#include <errno.h>
#include <stdlib.h>
#include <pwd.h>
#include "../include/ecryptfs.h"
#define MAX_TOK_LEN 128
#define MAX_FILE_SIZE 0xa000
int print_nvp_list(struct ecryptfs_name_val_pair *dst)
{
syslog(LOG_ERR, "Printing nvp list\n");
while (dst) {
syslog(LOG_ERR, "name=%s\n", dst->name);
syslog(LOG_ERR, "val=%s\n", dst->value);
dst = dst->next;
}
return 0;
}
static int copy_nv_pair(struct ecryptfs_name_val_pair *dst,
struct ecryptfs_name_val_pair *src)
{
int rc;
dst->flags = src->flags;
if ((rc = asprintf(&dst->name, "%s", src->name)) == -1) {
rc = -ENOMEM;
goto out;
}
if ((rc = asprintf(&dst->value, "%s", src->value)) == -1) {
rc = -ENOMEM;
goto out;
}
rc = 0;
out:
return rc;
}
/**
* For each name in dst that is also in src, set the value in dst to
* that which is in src.
*
* For each name in src that is not in dst, append a copy of the node
* onto dst.
*
* TODO: This function sucks. Partially because the nv data structure
* sucks. Overhaul it sometime.
*/
int ecryptfs_nvp_list_union(struct ecryptfs_name_val_pair *dst,
struct ecryptfs_name_val_pair *src,
struct ecryptfs_name_val_pair *allowed_duplicates)
{
int rc = 0;
struct ecryptfs_name_val_pair *dst_cursor;
struct ecryptfs_name_val_pair *src_cursor;
src_cursor = src->next;
while (src_cursor) {
int found_match;
struct ecryptfs_name_val_pair *prev_dst_cursor;
struct ecryptfs_name_val_pair *ad_cursor;
if (!src_cursor->name)
goto next_src_cursor;
found_match = 0;
prev_dst_cursor = dst;
dst_cursor = dst->next;
ad_cursor = allowed_duplicates->next;
while (ad_cursor) {
if (strcmp(src_cursor->name, ad_cursor->name) == 0) {
if (ecryptfs_verbosity)
syslog(LOG_INFO,
"Duplicates allowed for [%s]\n",
src_cursor->name);
while (dst_cursor) {
prev_dst_cursor = dst_cursor;
dst_cursor = dst_cursor->next;
}
goto do_insert;
}
ad_cursor = ad_cursor->next;
}
while (dst_cursor) {
if (!dst_cursor->name)
goto next_dst_cursor;
if (strcmp(src_cursor->name, dst_cursor->name) == 0) {
found_match = 1;
free(dst_cursor->value);
rc = asprintf(&dst_cursor->value, "%s",
src_cursor->value);
if (rc == -1) {
rc = -ENOMEM;
goto out;
}
}
next_dst_cursor:
prev_dst_cursor = dst_cursor;
dst_cursor = dst_cursor->next;
}
do_insert:
if (!found_match) {
struct ecryptfs_name_val_pair *dst_tmp;
struct ecryptfs_name_val_pair *src_tmp;
int i;
prev_dst_cursor->next = dst_cursor =
malloc(sizeof(struct ecryptfs_name_val_pair));
memset(dst_cursor, 0,
sizeof(struct ecryptfs_name_val_pair));
if (!dst_cursor) {
rc = -ENOMEM;
goto out;
}
dst_cursor->next = NULL;
if ((rc = copy_nv_pair(dst_cursor, src_cursor))) {
goto out;
}
dst_tmp = dst_cursor;
src_tmp = src_cursor;
/* TODO: Okay; this has officially become a
* hack. It's time to switch to a real tree
* structure for the name/value pair list. */
for (i = 0; i < NV_MAX_CHILDREN; i++) {
if (src_cursor->children[i]) {
if ((dst_cursor->children[i]
= malloc(sizeof(struct ecryptfs_name_val_pair)))
== NULL) {
rc = -ENOMEM;
goto out;
}
memset(dst_cursor->children[i], 0,
sizeof(struct ecryptfs_name_val_pair));
copy_nv_pair(dst_cursor->children[i],
src_cursor->children[i]);
dst_tmp->next = dst_cursor->children[i];
prev_dst_cursor = dst_tmp;
dst_tmp = dst_tmp->next;
prev_dst_cursor->next = dst_tmp;
src_tmp = src_tmp->next;
if (src_tmp != src_cursor->children[i]) {
rc = -EINVAL;
syslog(LOG_ERR,
"Internal error: src_tmp"
"->next != src_cursor->c"
"hildren[%d]\n", i);
goto out;
}
}
}
dst_cursor = dst_tmp;
src_cursor = src_tmp;
}
next_src_cursor:
src_cursor = src_cursor->next;
}
out:
return rc;
}
int
ecryptfs_parse_rc_file_fullpath(struct ecryptfs_name_val_pair *nvp_list_head,
char *fullpath)
{
int rc;
int fd;
fd = open(fullpath, O_RDONLY);
if (fd == -1) {
rc = -EIO;
goto out;
}
rc = parse_options_file(fd, nvp_list_head);
close(fd);
out:
return rc;
}
int ecryptfs_parse_rc_file(struct ecryptfs_name_val_pair *nvp_list_head)
{
char *home;
uid_t uid;
struct passwd *pw;
char *rcfile_fullpath;
int rc;
uid = getuid();
pw = getpwuid(uid);
if (!pw) {
rc = -EIO;
goto out;
}
home = pw->pw_dir;
rc = asprintf(&rcfile_fullpath, "%s/.ecryptfsrc", home);
if (rc == -1) {
rc = -ENOMEM;
goto out;
}
rc = ecryptfs_parse_rc_file_fullpath(nvp_list_head, rcfile_fullpath);
free(rcfile_fullpath);
out:
return rc;
}
int process_comma_tok(struct ecryptfs_name_val_pair **current, char *tok,
/*@null@*/ char *prefix)
{
int tok_len = (int)strlen(tok);
char new_prefix[MAX_TOK_LEN];
char sub_token[MAX_TOK_LEN];
char *name = NULL;
char *value = NULL;
int i, j, st_len;
int rc = 0;
if(tok && tok[0] == '\0') {
goto out;
}
if (tok_len < 0 || tok_len > MAX_TOK_LEN) {
rc = -EINVAL;
goto out;
}
if (tok[0] == '=' || tok[0] == ':') {
rc = -EINVAL;
goto out;
}
j = 0;
if (tok_len > 4 && !memcmp(tok, "key=", 4))
for (i = 4; i < tok_len; i++) {
if (tok[i] == ':')
goto process_colon_list;
}
goto process_nv_pair;
process_colon_list:
new_prefix[j] = '\0';
i = 0;
j = 0;
while (i < tok_len) {
if (tok[i] == ':') {
sub_token[j] = '\0';
if ((rc = process_comma_tok(current, sub_token, NULL)))
goto out;
j = 0;
} else
sub_token[j++] = tok[i];
i++;
}
sub_token[j] = '\0';
rc = process_comma_tok(current, sub_token, new_prefix);
goto out;
process_nv_pair:
st_len = snprintf(sub_token, MAX_TOK_LEN, "%s%s",
(prefix ? prefix : ""), tok);
j = 0;
for (i = 0; i < st_len; i++)
if (sub_token[i] == '=') {
if (!(name = malloc(i + 1))) {
rc = -ENOMEM;
goto out;
}
memcpy(name, sub_token, i);
name[i] = '\0';
j = i;
}
if (!name) {
if (!(name = malloc(i+1))) {
rc = -ENOMEM;
goto out;
}
memcpy(name, sub_token, i);
name[i] = '\0';
} else {
if((i-j) > 1) {
if (!(value = malloc(i - j + 1))) {
rc = -ENOMEM;
goto out;
}
memcpy(value, &sub_token[j+1], (i - j));
value[(i - j)] = '\0';
}
}
if (!((*current)->next =
malloc(sizeof(struct ecryptfs_name_val_pair)))) {
rc = -ENOMEM;
goto out;
}
memset((*current)->next, 0, sizeof(struct ecryptfs_name_val_pair));
if (strlen(name) == 0) {
free(name);
free(value);
} else {
*current = (*current)->next;
(*current)->name = name;
(*current)->value = value;
(*current)->next = NULL;
}
out:
return rc;
}
/**
* name=val,key=PKI:name1=val1:name2=val2,name=val...
*/
int generate_nv_list(struct ecryptfs_name_val_pair *head, char *buf)
{
struct ecryptfs_name_val_pair *current = head;
char tok_str[MAX_TOK_LEN];
if (!buf)
return 0;
int buf_len = strlen(buf);
int i, j = 0;
int rc = 0;
for (i = 0; i < buf_len; i++) {
if (buf[i] == ',' || buf[i] == '\n') {
tok_str[j] = '\0';
if ((rc = process_comma_tok(¤t, tok_str, NULL)))
goto out;
j = 0;
} else
tok_str[j++] = buf[i];
if (j == MAX_TOK_LEN)
goto out;
}
tok_str[j] = '\0';
if ((rc = process_comma_tok(¤t, tok_str, NULL)))
goto out;
out:
return rc;
}
int ecryptfs_parse_options(char *opts, struct ecryptfs_name_val_pair *head)
{
return generate_nv_list(head, opts);
}
int parse_options_file(int fd, struct ecryptfs_name_val_pair *head)
{
int rc = 0;
int pagesize;
char *data;
off_t file_size;
struct stat filestat;
rc = fstat(fd, &filestat);
if (rc) {
syslog(LOG_ERR, "%s: fstat returned [%d] on fd [%d]\n",
__FUNCTION__, rc, fd);
goto out;
}
pagesize = getpagesize();
file_size = filestat.st_size;
if (file_size > MAX_FILE_SIZE) {
syslog(LOG_ERR, "File size too large\n");
rc = -1;
goto out;
}
if (file_size % pagesize) {
file_size -= file_size % pagesize;
file_size += pagesize;
}
data = mmap((caddr_t)0, file_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (data == MAP_FAILED) {
rc = errno;
syslog(LOG_ERR, "%s: mmap failed on fd [%d]; rc = [%d]\n",
__FUNCTION__, fd, rc);
goto out;
}
rc = generate_nv_list(head, data);
munmap(data, file_size);
out:
return rc;
}
|