summaryrefslogtreecommitdiff
path: root/usr.bin/tcfs/tcfs_getfspath.c
blob: 5fee07598f9fd6ac9801a0637cdf9c2c5d946bd4 (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
/*	$OpenBSD: tcfs_getfspath.c,v 1.7 2000/06/20 10:46:51 fgsch 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 <fstab.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

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

#define WHITESPACE " \t\r\n"

int
tcfs_label_getcipher(char *label)
{
	int ciphernum;

	if (tcfs_get_label(label, NULL, &ciphernum))
		return (ciphernum);

	return (-1);
}

int
tcfs_getfspath(char *label2search, char *path)
{
	return (tcfs_get_label(label2search, path, NULL));
}

int
tcfs_get_label(char *label2search, char *path, int *ciphernumber)
{
	FILE *fp;
	char *label, *line, *p, *tag, *mountpoint, *cipherfield;
	int found = 0;

	if ((fp = fopen(_PATH_FSTAB, "r")) == NULL)
		return (0);

	if ((line = (char *)malloc(1024)) == NULL)
		goto out;

	while (!feof(fp) && !found) {
		p = line;
		fgets(p, 1024, fp);
		p = p + strspn(p, WHITESPACE);
		while (!found) {
			strsep(&p, WHITESPACE);  /* device */
			if (p == NULL)
				break;
			p = p + strspn(p, WHITESPACE);
			mountpoint = strsep(&p, WHITESPACE); /* mount point */
			if (p == NULL)
				break;
			tag = strsep(&p, WHITESPACE); /* file system */
			if (p == NULL || strcmp(tag, "tcfs"))
				break;

			/* find the correct label */
			label = strstr(p, "label=");
			cipherfield = strstr(p, "cipher=");
			if (label == NULL)
				break;
			p = label + 6;
			label = strsep(&p, WHITESPACE ",");
			if (!strlen(label) || strcmp(label, label2search))
				break;

			if (path) {
				strcpy(path, mountpoint);
				found = 1;
			}
			
			if (ciphernumber) {
				if (cipherfield == NULL)
					break;
				p = cipherfield + 7;
				cipherfield = strsep(&p, WHITESPACE ",");
				if (!strlen(cipherfield))
					break;

				*ciphernumber = strtol(cipherfield, &p, 0);
				if (cipherfield != p) 
					found = 1;
			}
		}
	}
	free(line);
 out:
	fclose(fp);

	return (found);
}