Annotation of src/usr.bin/tcfs/tcfs_getfspath.c, Revision 1.1.1.1
1.1 provos 1: /* $OpenBSD$ */
2: /*
3: * Transparent Cryptographic File System (TCFS) for NetBSD
4: * Author and mantainer: Luigi Catuogno [luicat@tcfs.unisa.it]
5: *
6: * references: http://tcfs.dia.unisa.it
7: * tcfs-bsd@tcfs.unisa.it
8: */
9:
10: /*
11: * Base utility set v0.1
12: */
13:
14: #include <stdio.h>
15: #include <strings.h>
16: #include <stdlib.h>
17: #include <fstab.h>
18:
19: #define WHITESPACE " \t\r\n"
20:
21: int
22: tcfs_label_getcipher (char *label)
23: {
24: int ciphernum;
25:
26: if (tcfs_get_label (label, NULL, &ciphernum))
27: return ciphernum;
28:
29: return (-1);
30: }
31:
32: int
33: tcfs_getfspath (char *label2search, char *path)
34: {
35: return tcfs_get_label (label2search, path, NULL);
36: }
37:
38: int
39: tcfs_get_label(char *label2search, char *path, int *ciphernumber)
40: {
41: FILE *fp;
42: char *label, *line, *p, *tag, *mountpoint, *cipherfield;
43: int found = 0;
44:
45: if ((fp = fopen(_PATH_FSTAB,"r")) == NULL)
46: return (0);
47:
48: if ((line = calloc(1024, sizeof(char))) == NULL)
49: goto out;
50:
51: while (!feof(fp) && !found) {
52: p = line;
53: fgets (p, 1024, fp);
54: p = p + strspn(p, WHITESPACE);
55: while (!found) {
56: strsep(&p, WHITESPACE); /* device */
57: if (p == NULL)
58: break;
59: p = p + strspn(p, WHITESPACE);
60: mountpoint = strsep(&p, WHITESPACE); /* mount point */
61: if (p == NULL)
62: break;
63: tag = strsep(&p, WHITESPACE); /* file system */
64: if (p == NULL || strcmp(tag, "tcfs"))
65: break;
66:
67: /* find the correct label */
68: label = strstr(p, "label=");
69: cipherfield = strstr(p, "cipher=");
70: if (label == NULL)
71: break;
72: p = label + 6;
73: label = strsep(&p, WHITESPACE ",");
74: if (!strlen(label) || strcmp(label, label2search))
75: break;
76:
77: if (path) {
78: strcpy(path, mountpoint);
79: found = 1;
80: }
81:
82: if (ciphernumber) {
83: if (cipherfield == NULL)
84: break;
85: p = cipherfield + 7;
86: cipherfield = strsep(&p, WHITESPACE ",");
87: if (!strlen(cipherfield))
88: break;
89:
90: *ciphernumber = strtol(cipherfield, &p, 0);
91: if (cipherfield != p)
92: found = 1;
93: }
94: }
95: }
96: free(line);
97: out:
98: fclose (fp);
99:
100: return found;
101: }