d11a0bf7 |
1 | /* -*-c-*- |
d11a0bf7 |
2 | * |
3 | * Reading and writing key flag strings |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
d11a0bf7 |
9 | * |
10 | * This file is part of Catacomb. |
11 | * |
12 | * Catacomb is free software; you can redistribute it and/or modify |
13 | * it under the terms of the GNU Library General Public License as |
14 | * published by the Free Software Foundation; either version 2 of the |
15 | * License, or (at your option) any later version. |
45c0fd36 |
16 | * |
d11a0bf7 |
17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 | * GNU Library General Public License for more details. |
45c0fd36 |
21 | * |
d11a0bf7 |
22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
25 | * MA 02111-1307, USA. |
26 | */ |
27 | |
d11a0bf7 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <stdlib.h> |
31 | #include <string.h> |
32 | |
33 | #include <mLib/bits.h> |
34 | #include <mLib/dstr.h> |
35 | |
0f9bd85a |
36 | #include "key-data.h" |
d11a0bf7 |
37 | |
38 | /*----- Data structures ---------------------------------------------------*/ |
39 | |
40 | typedef struct key_flags { |
41 | unsigned f; |
42 | unsigned m; |
43 | } key_flags; |
44 | |
45 | /*----- Flags table -------------------------------------------------------*/ |
46 | |
47 | typedef struct flagent { |
48 | const char *name; |
49 | unsigned f; |
50 | unsigned m; |
51 | } flagent; |
52 | |
4e66da02 |
53 | static const flagent flagtab[] = { |
d11a0bf7 |
54 | |
55 | /* --- Encoding types --- */ |
56 | |
57 | { "binary", KENC_BINARY, KF_ENCMASK }, |
58 | { "integer", KENC_MP, KF_ENCMASK }, |
59 | { "struct", KENC_STRUCT, KF_ENCMASK }, |
60 | { "encrypt", KENC_ENCRYPT, KF_ENCMASK }, |
1ba83484 |
61 | { "string", KENC_STRING, KF_ENCMASK }, |
62 | { "ec", KENC_EC, KF_ENCMASK }, |
d11a0bf7 |
63 | |
64 | /* --- Classes of keys --- */ |
65 | |
66 | { "shared", KCAT_SHARE, KF_CATMASK }, |
67 | { "public", KCAT_PUB, KF_CATMASK }, |
68 | { "private", KCAT_PRIV, KF_CATMASK }, |
69 | { "symmetric", KCAT_SYMM, KF_CATMASK }, |
70 | { "secret", 0, KF_NONSECRET }, |
71 | { "-secret", KF_NONSECRET, KF_NONSECRET }, |
72 | |
73 | /* --- Other flags --- */ |
74 | |
75 | { "burn", KF_BURN, KF_BURN }, |
76 | { "-burn", 0, KF_BURN }, |
77 | |
78 | /* --- End marker --- */ |
79 | |
80 | { 0, 0, 0 } |
81 | }; |
82 | |
83 | /*----- Main code ---------------------------------------------------------*/ |
84 | |
85 | /* --- @key_readflags@ --- * |
86 | * |
87 | * Arguments: @const char *p@ = pointer to string to read |
88 | * @char **pp@ = where to store the end pointer |
89 | * @unsigned *ff@ = where to store the flags |
90 | * @unsigned *mm@ = where to store the mask |
91 | * |
92 | * Returns: Zero if all went well, nonzero if there was an error. |
93 | * |
94 | * Use: Reads a flag string. |
95 | */ |
96 | |
97 | int key_readflags(const char *p, char **pp, unsigned *ff, unsigned *mm) |
98 | { |
99 | unsigned f = 0, m = 0; |
100 | |
101 | for (;;) { |
102 | size_t sz = strcspn(p, ",:"); |
4e66da02 |
103 | const flagent *e, *ee = 0; |
d11a0bf7 |
104 | |
105 | /* --- Look up the string in the flags table --- */ |
106 | |
b817bfc6 |
107 | if (sz == 4 && strncmp(p, "none", 4) == 0) |
108 | goto next; |
d11a0bf7 |
109 | for (e = flagtab; e->name; e++) { |
110 | if (strncmp(e->name, p, sz) == 0) { |
111 | if (e->name[sz] == 0) { |
112 | ee = e; |
113 | break; |
114 | } else if (ee) |
115 | return (KERR_BADFLAGS); |
116 | else |
117 | ee = e; |
118 | } |
119 | } |
120 | if (!ee) |
121 | return (KERR_BADFLAGS); |
122 | |
123 | /* --- Adjust the flag words --- * |
124 | * |
125 | * Ensure that the flags set are disjoint. |
126 | */ |
127 | |
128 | if (m & ee->m) |
129 | return (KERR_BADFLAGS); |
130 | m |= ee->m; |
131 | f |= ee->f; |
b817bfc6 |
132 | next: |
d11a0bf7 |
133 | p += sz; |
134 | if (*p == 0 || *p == ':') |
135 | break; |
136 | p++; |
137 | } |
138 | |
139 | /* --- Report the results --- */ |
140 | |
141 | if (ff) *ff = f; |
142 | if (mm) *mm = m; |
143 | if (pp) *pp = (char *)p; |
144 | return (0); |
145 | } |
146 | |
147 | /* --- @key_writeflags@ --- * |
148 | * |
149 | * Arguments: @unsigned f@ = flags to write |
150 | * @dstr *d@ = pointer to destination string |
151 | * |
152 | * Returns: --- |
153 | * |
154 | * Use: Emits a flags word as a string representation. |
155 | */ |
156 | |
157 | void key_writeflags(unsigned f, dstr *d) |
158 | { |
159 | int del = 0; |
4e66da02 |
160 | const flagent *e; |
d11a0bf7 |
161 | unsigned m = 0; |
162 | |
163 | for (e = flagtab; e->name; e++) { |
164 | if (m & e->m || e->name[0] == '-' || (f & e->m) != e->f) |
165 | continue; |
166 | if (del) |
167 | DPUTC(d, ','); |
168 | DPUTS(d, e->name); |
169 | m |= e->m; |
170 | del = 1; |
171 | } |
172 | } |
173 | |
052b36d0 |
174 | /* --- @key_match@ --- * |
175 | * |
176 | * Arguments: @key_data *k@ = pointer to key data block |
177 | * @const key_filter *kf@ = pointer to filter block |
178 | * |
179 | * Returns: Nonzero if the key matches the filter. |
180 | * |
181 | * Use: Checks whether a key matches a filter. |
182 | */ |
183 | |
184 | int key_match(key_data *k, const key_filter *kf) |
185 | { |
3384672a |
186 | key_subkeyiter i; |
187 | const char *tag; |
188 | key_data *kd; |
052b36d0 |
189 | |
190 | if (!kf) |
191 | return (1); |
192 | if ((k->e & KF_ENCMASK) != KENC_STRUCT) |
193 | return ((k->e & kf->m) == kf->f); |
194 | |
3384672a |
195 | for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &kd); ) { |
196 | if (key_match(kd, kf)) |
052b36d0 |
197 | return (1); |
198 | } |
199 | return (0); |
200 | } |
201 | |
d11a0bf7 |
202 | /*----- That's all, folks -------------------------------------------------*/ |