1 | |
---|
2 | /** |
---|
3 | ** abac_util.c |
---|
4 | **/ |
---|
5 | |
---|
6 | #include <err.h> |
---|
7 | #include <stdlib.h> |
---|
8 | #include <string.h> |
---|
9 | #include <stdio.h> |
---|
10 | #include <assert.h> |
---|
11 | #include <stdarg.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | |
---|
14 | #include "abac_util.h" |
---|
15 | |
---|
16 | static int debug=0; |
---|
17 | |
---|
18 | #define SHA1_LENGTH 40 |
---|
19 | #define ABAC_VERSION "2.1.0" |
---|
20 | |
---|
21 | char *abac_version() |
---|
22 | { |
---|
23 | return ABAC_VERSION; |
---|
24 | } |
---|
25 | |
---|
26 | void abac_errx(int eval, const char *msg) |
---|
27 | { |
---|
28 | errx(eval,"%s",msg); |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * Malloc, fatal on error. |
---|
33 | */ |
---|
34 | void *abac_xmalloc(size_t size) { |
---|
35 | void *ret; |
---|
36 | |
---|
37 | ret = malloc(size); |
---|
38 | if (ret == NULL) |
---|
39 | err(1, "malloc"); |
---|
40 | |
---|
41 | return ret; |
---|
42 | } |
---|
43 | |
---|
44 | void *abac_xrealloc(void *ptr, size_t size) { |
---|
45 | void *ret = realloc(ptr, size); |
---|
46 | if (ret == NULL) |
---|
47 | err(1, "couldn't realloc %d bytes\n", size); |
---|
48 | return ret; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | /** |
---|
53 | * strdup fatal on error |
---|
54 | */ |
---|
55 | char *abac_xstrdup(char *source) { |
---|
56 | char *ret; |
---|
57 | |
---|
58 | if (source == NULL) { |
---|
59 | return NULL; |
---|
60 | } |
---|
61 | |
---|
62 | ret = strdup(source); |
---|
63 | if (ret == NULL) |
---|
64 | err(1, "strdup"); |
---|
65 | |
---|
66 | return ret; |
---|
67 | } |
---|
68 | |
---|
69 | /* make a brand new string with or without being trimmed */ |
---|
70 | char *abac_trim_quotes(char *string) { |
---|
71 | char *tmp=abac_xstrdup(string); |
---|
72 | int len=strlen(tmp); |
---|
73 | if( (tmp[len-1] == '\'' || tmp[len-1] == '"') && |
---|
74 | (tmp[0] == '\'' || tmp[0] == '"')) { |
---|
75 | tmp[len-1]='\0'; |
---|
76 | char *ntmp=abac_xstrdup(&tmp[1]); |
---|
77 | free(tmp); |
---|
78 | return ntmp; |
---|
79 | } |
---|
80 | return tmp; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Split a string based on the given delimiter. |
---|
85 | */ |
---|
86 | void abac_split(char *string, char *delim, char **ret, int *num) { |
---|
87 | int len = strlen(delim); |
---|
88 | char *start = string; |
---|
89 | int count = 0; |
---|
90 | |
---|
91 | // split the string by the delim |
---|
92 | while ((start = strstr(string, delim)) != NULL) { |
---|
93 | *start = 0; |
---|
94 | ret[count++] = string; |
---|
95 | string = start + len; |
---|
96 | } |
---|
97 | ret[count++] = string; |
---|
98 | |
---|
99 | *num = count; |
---|
100 | } |
---|
101 | |
---|
102 | int abac_validate_clean_name(char *string) { |
---|
103 | int i; |
---|
104 | |
---|
105 | assert(string != NULL); |
---|
106 | |
---|
107 | // must start with a letter/number |
---|
108 | if (!isalnum(string[0])) return 0; |
---|
109 | |
---|
110 | // Name must be alphanumeric or - |
---|
111 | for (i = 1; string[i] != '\0'; ++i) |
---|
112 | if (!isalnum(string[i]) && string[i] != '-') |
---|
113 | return 0; |
---|
114 | |
---|
115 | return 1; |
---|
116 | } |
---|
117 | |
---|
118 | /* check up to first ( if its there.. |
---|
119 | a.role(this) |
---|
120 | a.role(?,?Year:[100..200],?Value:[one,two,three]) |
---|
121 | a.role(isi) |
---|
122 | */ |
---|
123 | int abac_validate_clean_aspect_name(char *string) { |
---|
124 | int i; |
---|
125 | |
---|
126 | assert(string != NULL); |
---|
127 | |
---|
128 | // must start with a letter/number |
---|
129 | if (!isalnum(string[0])) return 0; |
---|
130 | |
---|
131 | // Name must be alphanumeric or - or _ up to the ( if there is one |
---|
132 | for (i = 1; string[i] != '\0' && string[i] != '(' ; ++i) |
---|
133 | if (!isalnum(string[i]) && string[i] != '-' && |
---|
134 | string[i] != '_' ) |
---|
135 | return 0; |
---|
136 | return 1; |
---|
137 | } |
---|
138 | |
---|
139 | chunk_t abac_generate_serial() { |
---|
140 | chunk_t serial = chunk_empty; |
---|
141 | |
---|
142 | // create a serial (stolen from strongswan pki) |
---|
143 | rng_t *rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK); |
---|
144 | if (!rng) |
---|
145 | errx(1, "no random number generator"); |
---|
146 | |
---|
147 | rng->allocate_bytes(rng, 8, &serial); |
---|
148 | while (serial.ptr[0] == 0) { |
---|
149 | // don't get leading 0's |
---|
150 | rng->get_bytes(rng, 1, serial.ptr); |
---|
151 | } |
---|
152 | rng->destroy(rng); |
---|
153 | |
---|
154 | if(debug) |
---|
155 | printf( "abac_generate_serial, serial generated is %#B\n", &serial); |
---|
156 | return serial; |
---|
157 | } |
---|
158 | |
---|
159 | // validate a princpal's name |
---|
160 | // makes sure it's a valid SHA1 identifier |
---|
161 | // return values: |
---|
162 | // success: malloc'd copy with all hex digits lowercase |
---|
163 | // fail: NULL |
---|
164 | char *abac_validate_principal(char *keyid) { |
---|
165 | int i; |
---|
166 | char *copy = NULL; |
---|
167 | |
---|
168 | if (strlen(keyid) != SHA1_LENGTH) |
---|
169 | return NULL; |
---|
170 | |
---|
171 | int len = strlen(keyid) + 1; |
---|
172 | copy = abac_xmalloc(len); |
---|
173 | sprintf(copy, "%s", keyid); |
---|
174 | |
---|
175 | i=0; |
---|
176 | for (; i < SHA1_LENGTH; ++i) { |
---|
177 | copy[i] = tolower(copy[i]); |
---|
178 | if (!isxdigit(copy[i])) |
---|
179 | goto error; |
---|
180 | } |
---|
181 | return copy; |
---|
182 | error: |
---|
183 | free(copy); |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | |
---|
187 | // parse&validate the validity |
---|
188 | int abac_validate_validity_string(char *validity_str) |
---|
189 | { |
---|
190 | int validity=0; |
---|
191 | if (validity_str != NULL) { |
---|
192 | char suffix = 'd'; // default suffix is days |
---|
193 | int multiplier; |
---|
194 | |
---|
195 | int len = strlen(validity_str); |
---|
196 | assert(len > 0); |
---|
197 | |
---|
198 | // get the suffix char if it's alphabetical |
---|
199 | if (isalpha(validity_str[len - 1])) { |
---|
200 | suffix = validity_str[len - 1]; |
---|
201 | |
---|
202 | // truncate |
---|
203 | validity_str[len - 1] = '\0'; |
---|
204 | --len; |
---|
205 | |
---|
206 | // make sure it's not only a suffix |
---|
207 | if (len == 0) { |
---|
208 | errx(1,"Invalid validity\n"); |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | // convert the suffix to a multiplier |
---|
213 | switch(suffix) { |
---|
214 | case 's': multiplier = 1; break; |
---|
215 | case 'm': multiplier = 60; break; |
---|
216 | case 'h': multiplier = 3600; break; |
---|
217 | case 'd': multiplier = 86400; break; |
---|
218 | case 'y': multiplier = 31536000; break; |
---|
219 | default: |
---|
220 | errx(1,"Invalid suffix, must be s m h d y\n"); |
---|
221 | } |
---|
222 | |
---|
223 | // ascii to int |
---|
224 | char *end; |
---|
225 | validity = strtol(validity_str, &end, 10); |
---|
226 | if (end - validity_str < len) { |
---|
227 | errx(1,"Invalid validity\n"); |
---|
228 | } |
---|
229 | |
---|
230 | if (validity <= 0) { |
---|
231 | errx(1,"Invalid validity: must be > 0\n"); |
---|
232 | } |
---|
233 | |
---|
234 | // multiply! |
---|
235 | validity *= multiplier; |
---|
236 | } |
---|
237 | return validity; |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | int file_exist(char *filename) |
---|
242 | { |
---|
243 | struct stat stbuf; |
---|
244 | if(stat(filename,&stbuf) == -1) |
---|
245 | return 0; |
---|
246 | return 1; |
---|
247 | } |
---|
248 | |
---|
249 | |
---|