xref: /minix3/crypto/external/bsd/heimdal/dist/kadmin/util.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: util.c,v 1.3 2014/04/24 13:45:33 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadmin_locl.h"
37ebfedea0SLionel Sambuc #include <krb5/parse_units.h>
38ebfedea0SLionel Sambuc 
39ebfedea0SLionel Sambuc /*
40ebfedea0SLionel Sambuc  * util.c - functions for parsing, unparsing, and editing different
41ebfedea0SLionel Sambuc  * types of data used in kadmin.
42ebfedea0SLionel Sambuc  */
43ebfedea0SLionel Sambuc 
44ebfedea0SLionel Sambuc static int
45ebfedea0SLionel Sambuc get_response(const char *prompt, const char *def, char *buf, size_t len);
46ebfedea0SLionel Sambuc 
47ebfedea0SLionel Sambuc /*
48ebfedea0SLionel Sambuc  * attributes
49ebfedea0SLionel Sambuc  */
50ebfedea0SLionel Sambuc 
51ebfedea0SLionel Sambuc struct units kdb_attrs[] = {
52ebfedea0SLionel Sambuc     { "allow-digest",		KRB5_KDB_ALLOW_DIGEST },
53ebfedea0SLionel Sambuc     { "allow-kerberos4",	KRB5_KDB_ALLOW_KERBEROS4 },
54ebfedea0SLionel Sambuc     { "trusted-for-delegation",	KRB5_KDB_TRUSTED_FOR_DELEGATION },
55ebfedea0SLionel Sambuc     { "ok-as-delegate",		KRB5_KDB_OK_AS_DELEGATE },
56ebfedea0SLionel Sambuc     { "new-princ",		KRB5_KDB_NEW_PRINC },
57ebfedea0SLionel Sambuc     { "support-desmd5",		KRB5_KDB_SUPPORT_DESMD5 },
58ebfedea0SLionel Sambuc     { "pwchange-service",	KRB5_KDB_PWCHANGE_SERVICE },
59ebfedea0SLionel Sambuc     { "disallow-svr",		KRB5_KDB_DISALLOW_SVR },
60ebfedea0SLionel Sambuc     { "requires-pw-change",	KRB5_KDB_REQUIRES_PWCHANGE },
61ebfedea0SLionel Sambuc     { "requires-hw-auth",	KRB5_KDB_REQUIRES_HW_AUTH },
62ebfedea0SLionel Sambuc     { "requires-pre-auth",	KRB5_KDB_REQUIRES_PRE_AUTH },
63ebfedea0SLionel Sambuc     { "disallow-all-tix",	KRB5_KDB_DISALLOW_ALL_TIX },
64ebfedea0SLionel Sambuc     { "disallow-dup-skey",	KRB5_KDB_DISALLOW_DUP_SKEY },
65ebfedea0SLionel Sambuc     { "disallow-proxiable",	KRB5_KDB_DISALLOW_PROXIABLE },
66ebfedea0SLionel Sambuc     { "disallow-renewable",	KRB5_KDB_DISALLOW_RENEWABLE },
67ebfedea0SLionel Sambuc     { "disallow-tgt-based",	KRB5_KDB_DISALLOW_TGT_BASED },
68ebfedea0SLionel Sambuc     { "disallow-forwardable",	KRB5_KDB_DISALLOW_FORWARDABLE },
69ebfedea0SLionel Sambuc     { "disallow-postdated",	KRB5_KDB_DISALLOW_POSTDATED },
70ebfedea0SLionel Sambuc     { NULL, 0 }
71ebfedea0SLionel Sambuc };
72ebfedea0SLionel Sambuc 
73ebfedea0SLionel Sambuc /*
74ebfedea0SLionel Sambuc  * convert the attributes in `attributes' into a printable string
75ebfedea0SLionel Sambuc  * in `str, len'
76ebfedea0SLionel Sambuc  */
77ebfedea0SLionel Sambuc 
78ebfedea0SLionel Sambuc void
attributes2str(krb5_flags attributes,char * str,size_t len)79ebfedea0SLionel Sambuc attributes2str(krb5_flags attributes, char *str, size_t len)
80ebfedea0SLionel Sambuc {
81ebfedea0SLionel Sambuc     unparse_flags (attributes, kdb_attrs, str, len);
82ebfedea0SLionel Sambuc }
83ebfedea0SLionel Sambuc 
84ebfedea0SLionel Sambuc /*
85ebfedea0SLionel Sambuc  * convert the string in `str' into attributes in `flags'
86ebfedea0SLionel Sambuc  * return 0 if parsed ok, else -1.
87ebfedea0SLionel Sambuc  */
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc int
str2attributes(const char * str,krb5_flags * flags)90ebfedea0SLionel Sambuc str2attributes(const char *str, krb5_flags *flags)
91ebfedea0SLionel Sambuc {
92ebfedea0SLionel Sambuc     int res;
93ebfedea0SLionel Sambuc 
94ebfedea0SLionel Sambuc     res = parse_flags (str, kdb_attrs, *flags);
95ebfedea0SLionel Sambuc     if (res < 0)
96ebfedea0SLionel Sambuc 	return res;
97ebfedea0SLionel Sambuc     else {
98ebfedea0SLionel Sambuc 	*flags = res;
99ebfedea0SLionel Sambuc 	return 0;
100ebfedea0SLionel Sambuc     }
101ebfedea0SLionel Sambuc }
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc /*
104ebfedea0SLionel Sambuc  * try to parse the string `resp' into attributes in `attr', also
105ebfedea0SLionel Sambuc  * setting the `bit' in `mask' if attributes are given and valid.
106ebfedea0SLionel Sambuc  */
107ebfedea0SLionel Sambuc 
108ebfedea0SLionel Sambuc int
parse_attributes(const char * resp,krb5_flags * attr,int * mask,int bit)109ebfedea0SLionel Sambuc parse_attributes (const char *resp, krb5_flags *attr, int *mask, int bit)
110ebfedea0SLionel Sambuc {
111ebfedea0SLionel Sambuc     krb5_flags tmp = *attr;
112ebfedea0SLionel Sambuc 
113ebfedea0SLionel Sambuc     if (str2attributes(resp, &tmp) == 0) {
114ebfedea0SLionel Sambuc 	*attr = tmp;
115ebfedea0SLionel Sambuc 	if (mask)
116ebfedea0SLionel Sambuc 	    *mask |= bit;
117ebfedea0SLionel Sambuc 	return 0;
118ebfedea0SLionel Sambuc     } else if(*resp == '?') {
119ebfedea0SLionel Sambuc 	print_flags_table (kdb_attrs, stderr);
120ebfedea0SLionel Sambuc     } else {
121ebfedea0SLionel Sambuc 	fprintf (stderr, "Unable to parse \"%s\"\n", resp);
122ebfedea0SLionel Sambuc     }
123ebfedea0SLionel Sambuc     return -1;
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc /*
127ebfedea0SLionel Sambuc  * allow the user to edit the attributes in `attr', prompting with `prompt'
128ebfedea0SLionel Sambuc  */
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc int
edit_attributes(const char * prompt,krb5_flags * attr,int * mask,int bit)131ebfedea0SLionel Sambuc edit_attributes (const char *prompt, krb5_flags *attr, int *mask, int bit)
132ebfedea0SLionel Sambuc {
133ebfedea0SLionel Sambuc     char buf[1024], resp[1024];
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     if (mask && (*mask & bit))
136ebfedea0SLionel Sambuc 	return 0;
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc     attributes2str(*attr, buf, sizeof(buf));
139ebfedea0SLionel Sambuc     for (;;) {
140ebfedea0SLionel Sambuc 	if(get_response("Attributes", buf, resp, sizeof(resp)) != 0)
141ebfedea0SLionel Sambuc 	    return 1;
142ebfedea0SLionel Sambuc 	if (resp[0] == '\0')
143ebfedea0SLionel Sambuc 	    break;
144ebfedea0SLionel Sambuc 	if (parse_attributes (resp, attr, mask, bit) == 0)
145ebfedea0SLionel Sambuc 	    break;
146ebfedea0SLionel Sambuc     }
147ebfedea0SLionel Sambuc     return 0;
148ebfedea0SLionel Sambuc }
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc /*
151ebfedea0SLionel Sambuc  * time_t
152ebfedea0SLionel Sambuc  * the special value 0 means ``never''
153ebfedea0SLionel Sambuc  */
154ebfedea0SLionel Sambuc 
155ebfedea0SLionel Sambuc /*
156ebfedea0SLionel Sambuc  * Convert the time `t' to a string representation in `str' (of max
157ebfedea0SLionel Sambuc  * size `len').  If include_time also include time, otherwise just
158ebfedea0SLionel Sambuc  * date.
159ebfedea0SLionel Sambuc  */
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc void
time_t2str(time_t t,char * str,size_t len,int include_time)162ebfedea0SLionel Sambuc time_t2str(time_t t, char *str, size_t len, int include_time)
163ebfedea0SLionel Sambuc {
164ebfedea0SLionel Sambuc     if(t) {
165ebfedea0SLionel Sambuc 	if(include_time)
166ebfedea0SLionel Sambuc 	    strftime(str, len, "%Y-%m-%d %H:%M:%S UTC", gmtime(&t));
167ebfedea0SLionel Sambuc 	else
168ebfedea0SLionel Sambuc 	    strftime(str, len, "%Y-%m-%d", gmtime(&t));
169ebfedea0SLionel Sambuc     } else
170ebfedea0SLionel Sambuc 	snprintf(str, len, "never");
171ebfedea0SLionel Sambuc }
172ebfedea0SLionel Sambuc 
173ebfedea0SLionel Sambuc /*
174ebfedea0SLionel Sambuc  * Convert the time representation in `str' to a time in `time'.
175ebfedea0SLionel Sambuc  * Return 0 if succesful, else -1.
176ebfedea0SLionel Sambuc  */
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc int
str2time_t(const char * str,time_t * t)179ebfedea0SLionel Sambuc str2time_t (const char *str, time_t *t)
180ebfedea0SLionel Sambuc {
181ebfedea0SLionel Sambuc     const char *p;
182ebfedea0SLionel Sambuc     struct tm tm, tm2;
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc     memset (&tm, 0, sizeof (tm));
185ebfedea0SLionel Sambuc     memset (&tm2, 0, sizeof (tm2));
186ebfedea0SLionel Sambuc 
187ebfedea0SLionel Sambuc     while(isspace((unsigned char)*str))
188ebfedea0SLionel Sambuc 	str++;
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     if (str[0] == '+') {
191ebfedea0SLionel Sambuc 	str++;
192ebfedea0SLionel Sambuc 	*t = parse_time(str, "month");
193ebfedea0SLionel Sambuc 	if (*t < 0)
194ebfedea0SLionel Sambuc 	    return -1;
195ebfedea0SLionel Sambuc 	*t += time(NULL);
196ebfedea0SLionel Sambuc 	return 0;
197ebfedea0SLionel Sambuc     }
198ebfedea0SLionel Sambuc 
199ebfedea0SLionel Sambuc     if(strcasecmp(str, "never") == 0) {
200ebfedea0SLionel Sambuc 	*t = 0;
201ebfedea0SLionel Sambuc 	return 0;
202ebfedea0SLionel Sambuc     }
203ebfedea0SLionel Sambuc 
204ebfedea0SLionel Sambuc     if(strcasecmp(str, "now") == 0) {
205ebfedea0SLionel Sambuc 	*t = time(NULL);
206ebfedea0SLionel Sambuc 	return 0;
207ebfedea0SLionel Sambuc     }
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     p = strptime (str, "%Y-%m-%d", &tm);
210ebfedea0SLionel Sambuc 
211ebfedea0SLionel Sambuc     if (p == NULL)
212ebfedea0SLionel Sambuc 	return -1;
213ebfedea0SLionel Sambuc 
214ebfedea0SLionel Sambuc     while(isspace((unsigned char)*p))
215ebfedea0SLionel Sambuc 	p++;
216ebfedea0SLionel Sambuc 
217ebfedea0SLionel Sambuc     /* XXX this is really a bit optimistic, we should really complain
218ebfedea0SLionel Sambuc        if there was a problem parsing the time */
219ebfedea0SLionel Sambuc     if(p[0] != '\0' && strptime (p, "%H:%M:%S", &tm2) != NULL) {
220ebfedea0SLionel Sambuc 	tm.tm_hour = tm2.tm_hour;
221ebfedea0SLionel Sambuc 	tm.tm_min  = tm2.tm_min;
222ebfedea0SLionel Sambuc 	tm.tm_sec  = tm2.tm_sec;
223ebfedea0SLionel Sambuc     } else {
224ebfedea0SLionel Sambuc 	/* Do it on the end of the day */
225ebfedea0SLionel Sambuc 	tm.tm_hour = 23;
226ebfedea0SLionel Sambuc 	tm.tm_min  = 59;
227ebfedea0SLionel Sambuc 	tm.tm_sec  = 59;
228ebfedea0SLionel Sambuc     }
229ebfedea0SLionel Sambuc 
230ebfedea0SLionel Sambuc     *t = tm2time (tm, 0);
231ebfedea0SLionel Sambuc     return 0;
232ebfedea0SLionel Sambuc }
233ebfedea0SLionel Sambuc 
234ebfedea0SLionel Sambuc /*
235ebfedea0SLionel Sambuc  * try to parse the time in `resp' storing it in `value'
236ebfedea0SLionel Sambuc  */
237ebfedea0SLionel Sambuc 
238ebfedea0SLionel Sambuc int
parse_timet(const char * resp,krb5_timestamp * value,int * mask,int bit)239ebfedea0SLionel Sambuc parse_timet (const char *resp, krb5_timestamp *value, int *mask, int bit)
240ebfedea0SLionel Sambuc {
241ebfedea0SLionel Sambuc     time_t tmp;
242ebfedea0SLionel Sambuc 
243ebfedea0SLionel Sambuc     if (str2time_t(resp, &tmp) == 0) {
244ebfedea0SLionel Sambuc 	*value = tmp;
245ebfedea0SLionel Sambuc 	if(mask)
246ebfedea0SLionel Sambuc 	    *mask |= bit;
247ebfedea0SLionel Sambuc 	return 0;
248ebfedea0SLionel Sambuc     }
249ebfedea0SLionel Sambuc     if(*resp != '?')
250ebfedea0SLionel Sambuc 	fprintf (stderr, "Unable to parse time \"%s\"\n", resp);
251ebfedea0SLionel Sambuc     fprintf (stderr, "Print date on format YYYY-mm-dd [hh:mm:ss]\n");
252ebfedea0SLionel Sambuc     return -1;
253ebfedea0SLionel Sambuc }
254ebfedea0SLionel Sambuc 
255ebfedea0SLionel Sambuc /*
256ebfedea0SLionel Sambuc  * allow the user to edit the time in `value'
257ebfedea0SLionel Sambuc  */
258ebfedea0SLionel Sambuc 
259ebfedea0SLionel Sambuc int
edit_timet(const char * prompt,krb5_timestamp * value,int * mask,int bit)260ebfedea0SLionel Sambuc edit_timet (const char *prompt, krb5_timestamp *value, int *mask, int bit)
261ebfedea0SLionel Sambuc {
262ebfedea0SLionel Sambuc     char buf[1024], resp[1024];
263ebfedea0SLionel Sambuc 
264ebfedea0SLionel Sambuc     if (mask && (*mask & bit))
265ebfedea0SLionel Sambuc 	return 0;
266ebfedea0SLionel Sambuc 
267ebfedea0SLionel Sambuc     time_t2str (*value, buf, sizeof (buf), 0);
268ebfedea0SLionel Sambuc 
269ebfedea0SLionel Sambuc     for (;;) {
270ebfedea0SLionel Sambuc 	if(get_response(prompt, buf, resp, sizeof(resp)) != 0)
271ebfedea0SLionel Sambuc 	    return 1;
272ebfedea0SLionel Sambuc 	if (parse_timet (resp, value, mask, bit) == 0)
273ebfedea0SLionel Sambuc 	    break;
274ebfedea0SLionel Sambuc     }
275ebfedea0SLionel Sambuc     return 0;
276ebfedea0SLionel Sambuc }
277ebfedea0SLionel Sambuc 
278ebfedea0SLionel Sambuc /*
279ebfedea0SLionel Sambuc  * deltat
280ebfedea0SLionel Sambuc  * the special value 0 means ``unlimited''
281ebfedea0SLionel Sambuc  */
282ebfedea0SLionel Sambuc 
283ebfedea0SLionel Sambuc /*
284ebfedea0SLionel Sambuc  * convert the delta_t value in `t' into a printable form in `str, len'
285ebfedea0SLionel Sambuc  */
286ebfedea0SLionel Sambuc 
287ebfedea0SLionel Sambuc void
deltat2str(unsigned t,char * str,size_t len)288ebfedea0SLionel Sambuc deltat2str(unsigned t, char *str, size_t len)
289ebfedea0SLionel Sambuc {
290ebfedea0SLionel Sambuc     if(t == 0 || t == INT_MAX)
291ebfedea0SLionel Sambuc 	snprintf(str, len, "unlimited");
292ebfedea0SLionel Sambuc     else
293ebfedea0SLionel Sambuc 	unparse_time(t, str, len);
294ebfedea0SLionel Sambuc }
295ebfedea0SLionel Sambuc 
296ebfedea0SLionel Sambuc /*
297ebfedea0SLionel Sambuc  * parse the delta value in `str', storing result in `*delta'
298ebfedea0SLionel Sambuc  * return 0 if ok, else -1
299ebfedea0SLionel Sambuc  */
300ebfedea0SLionel Sambuc 
301ebfedea0SLionel Sambuc int
str2deltat(const char * str,krb5_deltat * delta)302ebfedea0SLionel Sambuc str2deltat(const char *str, krb5_deltat *delta)
303ebfedea0SLionel Sambuc {
304ebfedea0SLionel Sambuc     int res;
305ebfedea0SLionel Sambuc 
306ebfedea0SLionel Sambuc     if(strcasecmp(str, "unlimited") == 0) {
307ebfedea0SLionel Sambuc 	*delta = 0;
308ebfedea0SLionel Sambuc 	return 0;
309ebfedea0SLionel Sambuc     }
310ebfedea0SLionel Sambuc     res = parse_time(str, "day");
311ebfedea0SLionel Sambuc     if (res < 0)
312ebfedea0SLionel Sambuc 	return res;
313ebfedea0SLionel Sambuc     else {
314ebfedea0SLionel Sambuc 	*delta = res;
315ebfedea0SLionel Sambuc 	return 0;
316ebfedea0SLionel Sambuc     }
317ebfedea0SLionel Sambuc }
318ebfedea0SLionel Sambuc 
319ebfedea0SLionel Sambuc /*
320ebfedea0SLionel Sambuc  * try to parse the string in `resp' into a deltad in `value'
321ebfedea0SLionel Sambuc  * `mask' will get the bit `bit' set if a value was given.
322ebfedea0SLionel Sambuc  */
323ebfedea0SLionel Sambuc 
324ebfedea0SLionel Sambuc int
parse_deltat(const char * resp,krb5_deltat * value,int * mask,int bit)325ebfedea0SLionel Sambuc parse_deltat (const char *resp, krb5_deltat *value, int *mask, int bit)
326ebfedea0SLionel Sambuc {
327ebfedea0SLionel Sambuc     krb5_deltat tmp;
328ebfedea0SLionel Sambuc 
329ebfedea0SLionel Sambuc     if (str2deltat(resp, &tmp) == 0) {
330ebfedea0SLionel Sambuc 	*value = tmp;
331ebfedea0SLionel Sambuc 	if (mask)
332ebfedea0SLionel Sambuc 	    *mask |= bit;
333ebfedea0SLionel Sambuc 	return 0;
334ebfedea0SLionel Sambuc     } else if(*resp == '?') {
335ebfedea0SLionel Sambuc 	print_time_table (stderr);
336ebfedea0SLionel Sambuc     } else {
337ebfedea0SLionel Sambuc 	fprintf (stderr, "Unable to parse time \"%s\"\n", resp);
338ebfedea0SLionel Sambuc     }
339ebfedea0SLionel Sambuc     return -1;
340ebfedea0SLionel Sambuc }
341ebfedea0SLionel Sambuc 
342ebfedea0SLionel Sambuc /*
343ebfedea0SLionel Sambuc  * allow the user to edit the deltat in `value'
344ebfedea0SLionel Sambuc  */
345ebfedea0SLionel Sambuc 
346ebfedea0SLionel Sambuc int
edit_deltat(const char * prompt,krb5_deltat * value,int * mask,int bit)347ebfedea0SLionel Sambuc edit_deltat (const char *prompt, krb5_deltat *value, int *mask, int bit)
348ebfedea0SLionel Sambuc {
349ebfedea0SLionel Sambuc     char buf[1024], resp[1024];
350ebfedea0SLionel Sambuc 
351ebfedea0SLionel Sambuc     if (mask && (*mask & bit))
352ebfedea0SLionel Sambuc 	return 0;
353ebfedea0SLionel Sambuc 
354ebfedea0SLionel Sambuc     deltat2str(*value, buf, sizeof(buf));
355ebfedea0SLionel Sambuc     for (;;) {
356ebfedea0SLionel Sambuc 	if(get_response(prompt, buf, resp, sizeof(resp)) != 0)
357ebfedea0SLionel Sambuc 	    return 1;
358ebfedea0SLionel Sambuc 	if (parse_deltat (resp, value, mask, bit) == 0)
359ebfedea0SLionel Sambuc 	    break;
360ebfedea0SLionel Sambuc     }
361ebfedea0SLionel Sambuc     return 0;
362ebfedea0SLionel Sambuc }
363ebfedea0SLionel Sambuc 
364ebfedea0SLionel Sambuc /*
365ebfedea0SLionel Sambuc  * allow the user to edit `ent'
366ebfedea0SLionel Sambuc  */
367ebfedea0SLionel Sambuc 
368ebfedea0SLionel Sambuc void
set_defaults(kadm5_principal_ent_t ent,int * mask,kadm5_principal_ent_t default_ent,int default_mask)369ebfedea0SLionel Sambuc set_defaults(kadm5_principal_ent_t ent, int *mask,
370ebfedea0SLionel Sambuc 	     kadm5_principal_ent_t default_ent, int default_mask)
371ebfedea0SLionel Sambuc {
372ebfedea0SLionel Sambuc     if (default_ent
373ebfedea0SLionel Sambuc 	&& (default_mask & KADM5_MAX_LIFE)
374ebfedea0SLionel Sambuc 	&& !(*mask & KADM5_MAX_LIFE))
375ebfedea0SLionel Sambuc 	ent->max_life = default_ent->max_life;
376ebfedea0SLionel Sambuc 
377ebfedea0SLionel Sambuc     if (default_ent
378ebfedea0SLionel Sambuc 	&& (default_mask & KADM5_MAX_RLIFE)
379ebfedea0SLionel Sambuc 	&& !(*mask & KADM5_MAX_RLIFE))
380ebfedea0SLionel Sambuc 	ent->max_renewable_life = default_ent->max_renewable_life;
381ebfedea0SLionel Sambuc 
382ebfedea0SLionel Sambuc     if (default_ent
383ebfedea0SLionel Sambuc 	&& (default_mask & KADM5_PRINC_EXPIRE_TIME)
384ebfedea0SLionel Sambuc 	&& !(*mask & KADM5_PRINC_EXPIRE_TIME))
385ebfedea0SLionel Sambuc 	ent->princ_expire_time = default_ent->princ_expire_time;
386ebfedea0SLionel Sambuc 
387ebfedea0SLionel Sambuc     if (default_ent
388ebfedea0SLionel Sambuc 	&& (default_mask & KADM5_PW_EXPIRATION)
389ebfedea0SLionel Sambuc 	&& !(*mask & KADM5_PW_EXPIRATION))
390ebfedea0SLionel Sambuc 	ent->pw_expiration = default_ent->pw_expiration;
391ebfedea0SLionel Sambuc 
392ebfedea0SLionel Sambuc     if (default_ent
393ebfedea0SLionel Sambuc 	&& (default_mask & KADM5_ATTRIBUTES)
394ebfedea0SLionel Sambuc 	&& !(*mask & KADM5_ATTRIBUTES))
395ebfedea0SLionel Sambuc 	ent->attributes = default_ent->attributes & ~KRB5_KDB_DISALLOW_ALL_TIX;
396ebfedea0SLionel Sambuc }
397ebfedea0SLionel Sambuc 
398ebfedea0SLionel Sambuc int
edit_entry(kadm5_principal_ent_t ent,int * mask,kadm5_principal_ent_t default_ent,int default_mask)399ebfedea0SLionel Sambuc edit_entry(kadm5_principal_ent_t ent, int *mask,
400ebfedea0SLionel Sambuc 	   kadm5_principal_ent_t default_ent, int default_mask)
401ebfedea0SLionel Sambuc {
402ebfedea0SLionel Sambuc 
403ebfedea0SLionel Sambuc     set_defaults(ent, mask, default_ent, default_mask);
404ebfedea0SLionel Sambuc 
405ebfedea0SLionel Sambuc     if(edit_deltat ("Max ticket life", &ent->max_life, mask,
406ebfedea0SLionel Sambuc 		    KADM5_MAX_LIFE) != 0)
407ebfedea0SLionel Sambuc 	return 1;
408ebfedea0SLionel Sambuc 
409ebfedea0SLionel Sambuc     if(edit_deltat ("Max renewable life", &ent->max_renewable_life, mask,
410ebfedea0SLionel Sambuc 		    KADM5_MAX_RLIFE) != 0)
411ebfedea0SLionel Sambuc 	return 1;
412ebfedea0SLionel Sambuc 
413ebfedea0SLionel Sambuc     if(edit_timet ("Principal expiration time", &ent->princ_expire_time, mask,
414ebfedea0SLionel Sambuc 		   KADM5_PRINC_EXPIRE_TIME) != 0)
415ebfedea0SLionel Sambuc 	return 1;
416ebfedea0SLionel Sambuc 
417ebfedea0SLionel Sambuc     if(edit_timet ("Password expiration time", &ent->pw_expiration, mask,
418ebfedea0SLionel Sambuc 		   KADM5_PW_EXPIRATION) != 0)
419ebfedea0SLionel Sambuc 	return 1;
420ebfedea0SLionel Sambuc 
421ebfedea0SLionel Sambuc     if(edit_attributes ("Attributes", &ent->attributes, mask,
422ebfedea0SLionel Sambuc 			KADM5_ATTRIBUTES) != 0)
423ebfedea0SLionel Sambuc 	return 1;
424ebfedea0SLionel Sambuc 
425ebfedea0SLionel Sambuc     return 0;
426ebfedea0SLionel Sambuc }
427ebfedea0SLionel Sambuc 
428ebfedea0SLionel Sambuc /*
429ebfedea0SLionel Sambuc  * Parse the arguments, set the fields in `ent' and the `mask' for the
430ebfedea0SLionel Sambuc  * entries having been set.
431ebfedea0SLionel Sambuc  * Return 1 on failure and 0 on success.
432ebfedea0SLionel Sambuc  */
433ebfedea0SLionel Sambuc 
434ebfedea0SLionel Sambuc int
set_entry(krb5_context contextp,kadm5_principal_ent_t ent,int * mask,const char * max_ticket_life,const char * max_renewable_life,const char * expiration,const char * pw_expiration,const char * attributes)435ebfedea0SLionel Sambuc set_entry(krb5_context contextp,
436ebfedea0SLionel Sambuc 	  kadm5_principal_ent_t ent,
437ebfedea0SLionel Sambuc 	  int *mask,
438ebfedea0SLionel Sambuc 	  const char *max_ticket_life,
439ebfedea0SLionel Sambuc 	  const char *max_renewable_life,
440ebfedea0SLionel Sambuc 	  const char *expiration,
441ebfedea0SLionel Sambuc 	  const char *pw_expiration,
442ebfedea0SLionel Sambuc 	  const char *attributes)
443ebfedea0SLionel Sambuc {
444ebfedea0SLionel Sambuc     if (max_ticket_life != NULL) {
445ebfedea0SLionel Sambuc 	if (parse_deltat (max_ticket_life, &ent->max_life,
446ebfedea0SLionel Sambuc 			  mask, KADM5_MAX_LIFE)) {
447ebfedea0SLionel Sambuc 	    krb5_warnx (contextp, "unable to parse `%s'", max_ticket_life);
448ebfedea0SLionel Sambuc 	    return 1;
449ebfedea0SLionel Sambuc 	}
450ebfedea0SLionel Sambuc     }
451ebfedea0SLionel Sambuc     if (max_renewable_life != NULL) {
452ebfedea0SLionel Sambuc 	if (parse_deltat (max_renewable_life, &ent->max_renewable_life,
453ebfedea0SLionel Sambuc 			  mask, KADM5_MAX_RLIFE)) {
454ebfedea0SLionel Sambuc 	    krb5_warnx (contextp, "unable to parse `%s'", max_renewable_life);
455ebfedea0SLionel Sambuc 	    return 1;
456ebfedea0SLionel Sambuc 	}
457ebfedea0SLionel Sambuc     }
458ebfedea0SLionel Sambuc 
459ebfedea0SLionel Sambuc     if (expiration) {
460ebfedea0SLionel Sambuc 	if (parse_timet (expiration, &ent->princ_expire_time,
461ebfedea0SLionel Sambuc 			mask, KADM5_PRINC_EXPIRE_TIME)) {
462ebfedea0SLionel Sambuc 	    krb5_warnx (contextp, "unable to parse `%s'", expiration);
463ebfedea0SLionel Sambuc 	    return 1;
464ebfedea0SLionel Sambuc 	}
465ebfedea0SLionel Sambuc     }
466ebfedea0SLionel Sambuc     if (pw_expiration) {
467ebfedea0SLionel Sambuc 	if (parse_timet (pw_expiration, &ent->pw_expiration,
468ebfedea0SLionel Sambuc 			 mask, KADM5_PW_EXPIRATION)) {
469ebfedea0SLionel Sambuc 	    krb5_warnx (contextp, "unable to parse `%s'", pw_expiration);
470ebfedea0SLionel Sambuc 	    return 1;
471ebfedea0SLionel Sambuc 	}
472ebfedea0SLionel Sambuc     }
473ebfedea0SLionel Sambuc     if (attributes != NULL) {
474ebfedea0SLionel Sambuc 	if (parse_attributes (attributes, &ent->attributes,
475ebfedea0SLionel Sambuc 			      mask, KADM5_ATTRIBUTES)) {
476ebfedea0SLionel Sambuc 	    krb5_warnx (contextp, "unable to parse `%s'", attributes);
477ebfedea0SLionel Sambuc 	    return 1;
478ebfedea0SLionel Sambuc 	}
479ebfedea0SLionel Sambuc     }
480ebfedea0SLionel Sambuc     return 0;
481ebfedea0SLionel Sambuc }
482ebfedea0SLionel Sambuc 
483ebfedea0SLionel Sambuc /*
484ebfedea0SLionel Sambuc  * Does `string' contain any globing characters?
485ebfedea0SLionel Sambuc  */
486ebfedea0SLionel Sambuc 
487ebfedea0SLionel Sambuc static int
is_expression(const char * string)488ebfedea0SLionel Sambuc is_expression(const char *string)
489ebfedea0SLionel Sambuc {
490ebfedea0SLionel Sambuc     const char *p;
491ebfedea0SLionel Sambuc     int quote = 0;
492ebfedea0SLionel Sambuc 
493ebfedea0SLionel Sambuc     for(p = string; *p; p++) {
494ebfedea0SLionel Sambuc 	if(quote) {
495ebfedea0SLionel Sambuc 	    quote = 0;
496ebfedea0SLionel Sambuc 	    continue;
497ebfedea0SLionel Sambuc 	}
498ebfedea0SLionel Sambuc 	if(*p == '\\')
499ebfedea0SLionel Sambuc 	    quote++;
500ebfedea0SLionel Sambuc 	else if(strchr("[]*?", *p) != NULL)
501ebfedea0SLionel Sambuc 	    return 1;
502ebfedea0SLionel Sambuc     }
503ebfedea0SLionel Sambuc     return 0;
504ebfedea0SLionel Sambuc }
505ebfedea0SLionel Sambuc 
506ebfedea0SLionel Sambuc /*
507ebfedea0SLionel Sambuc  * Loop over all principals matching exp.  If any of calls to `func'
508ebfedea0SLionel Sambuc  * failes, the first error is returned when all principals are
509ebfedea0SLionel Sambuc  * processed.
510ebfedea0SLionel Sambuc  */
511ebfedea0SLionel Sambuc int
foreach_principal(const char * exp_str,int (* func)(krb5_principal,void *),const char * funcname,void * data)512ebfedea0SLionel Sambuc foreach_principal(const char *exp_str,
513ebfedea0SLionel Sambuc 		  int (*func)(krb5_principal, void*),
514ebfedea0SLionel Sambuc 		  const char *funcname,
515ebfedea0SLionel Sambuc 		  void *data)
516ebfedea0SLionel Sambuc {
517*0a6a1f1dSLionel Sambuc     char **princs = NULL;
518*0a6a1f1dSLionel Sambuc     int num_princs = 0;
519ebfedea0SLionel Sambuc     int i;
520ebfedea0SLionel Sambuc     krb5_error_code saved_ret = 0, ret = 0;
521ebfedea0SLionel Sambuc     krb5_principal princ_ent;
522ebfedea0SLionel Sambuc     int is_expr;
523ebfedea0SLionel Sambuc 
524ebfedea0SLionel Sambuc     /* if this isn't an expression, there is no point in wading
525ebfedea0SLionel Sambuc        through the whole database looking for matches */
526ebfedea0SLionel Sambuc     is_expr = is_expression(exp_str);
527ebfedea0SLionel Sambuc     if(is_expr)
528ebfedea0SLionel Sambuc 	ret = kadm5_get_principals(kadm_handle, exp_str, &princs, &num_princs);
529ebfedea0SLionel Sambuc     if(!is_expr || ret == KADM5_AUTH_LIST) {
530ebfedea0SLionel Sambuc 	/* we might be able to perform the requested opreration even
531ebfedea0SLionel Sambuc            if we're not allowed to list principals */
532ebfedea0SLionel Sambuc 	num_princs = 1;
533ebfedea0SLionel Sambuc 	princs = malloc(sizeof(*princs));
534ebfedea0SLionel Sambuc 	if(princs == NULL)
535ebfedea0SLionel Sambuc 	    return ENOMEM;
536ebfedea0SLionel Sambuc 	princs[0] = strdup(exp_str);
537ebfedea0SLionel Sambuc 	if(princs[0] == NULL){
538ebfedea0SLionel Sambuc 	    free(princs);
539ebfedea0SLionel Sambuc 	    return ENOMEM;
540ebfedea0SLionel Sambuc 	}
541ebfedea0SLionel Sambuc     } else if(ret) {
542ebfedea0SLionel Sambuc 	krb5_warn(context, ret, "kadm5_get_principals");
543ebfedea0SLionel Sambuc 	return ret;
544ebfedea0SLionel Sambuc     }
545ebfedea0SLionel Sambuc     for(i = 0; i < num_princs; i++) {
546ebfedea0SLionel Sambuc 	ret = krb5_parse_name(context, princs[i], &princ_ent);
547ebfedea0SLionel Sambuc 	if(ret){
548ebfedea0SLionel Sambuc 	    krb5_warn(context, ret, "krb5_parse_name(%s)", princs[i]);
549ebfedea0SLionel Sambuc 	    continue;
550ebfedea0SLionel Sambuc 	}
551ebfedea0SLionel Sambuc 	ret = (*func)(princ_ent, data);
552ebfedea0SLionel Sambuc 	if(ret) {
553ebfedea0SLionel Sambuc 	    krb5_clear_error_message(context);
554ebfedea0SLionel Sambuc 	    krb5_warn(context, ret, "%s %s", funcname, princs[i]);
555ebfedea0SLionel Sambuc 	    if (saved_ret == 0)
556ebfedea0SLionel Sambuc 		saved_ret = ret;
557ebfedea0SLionel Sambuc 	}
558ebfedea0SLionel Sambuc 	krb5_free_principal(context, princ_ent);
559ebfedea0SLionel Sambuc     }
560ebfedea0SLionel Sambuc     if (ret == 0 && saved_ret != 0)
561ebfedea0SLionel Sambuc 	ret = saved_ret;
562ebfedea0SLionel Sambuc     kadm5_free_name_list(kadm_handle, princs, &num_princs);
563ebfedea0SLionel Sambuc     return ret;
564ebfedea0SLionel Sambuc }
565ebfedea0SLionel Sambuc 
566ebfedea0SLionel Sambuc /*
567ebfedea0SLionel Sambuc  * prompt with `prompt' and default value `def', and store the reply
568ebfedea0SLionel Sambuc  * in `buf, len'
569ebfedea0SLionel Sambuc  */
570ebfedea0SLionel Sambuc 
571ebfedea0SLionel Sambuc #include <setjmp.h>
572ebfedea0SLionel Sambuc 
573ebfedea0SLionel Sambuc static jmp_buf jmpbuf;
574ebfedea0SLionel Sambuc 
575ebfedea0SLionel Sambuc static void
interrupt(int sig)576ebfedea0SLionel Sambuc interrupt(int sig)
577ebfedea0SLionel Sambuc {
578ebfedea0SLionel Sambuc     longjmp(jmpbuf, 1);
579ebfedea0SLionel Sambuc }
580ebfedea0SLionel Sambuc 
581ebfedea0SLionel Sambuc static int
get_response(const char * prompt,const char * def,char * buf,size_t len)582ebfedea0SLionel Sambuc get_response(const char *prompt, const char *def, char *buf, size_t len)
583ebfedea0SLionel Sambuc {
584ebfedea0SLionel Sambuc     char *p;
585ebfedea0SLionel Sambuc     void (*osig)(int);
586ebfedea0SLionel Sambuc 
587ebfedea0SLionel Sambuc     osig = signal(SIGINT, interrupt);
588ebfedea0SLionel Sambuc     if(setjmp(jmpbuf)) {
589ebfedea0SLionel Sambuc 	signal(SIGINT, osig);
590ebfedea0SLionel Sambuc 	fprintf(stderr, "\n");
591ebfedea0SLionel Sambuc 	return 1;
592ebfedea0SLionel Sambuc     }
593ebfedea0SLionel Sambuc 
594ebfedea0SLionel Sambuc     fprintf(stderr, "%s [%s]:", prompt, def);
595ebfedea0SLionel Sambuc     if(fgets(buf, len, stdin) == NULL) {
596ebfedea0SLionel Sambuc 	int save_errno = errno;
597ebfedea0SLionel Sambuc 	if(ferror(stdin))
598ebfedea0SLionel Sambuc 	    krb5_err(context, 1, save_errno, "<stdin>");
599ebfedea0SLionel Sambuc 	signal(SIGINT, osig);
600ebfedea0SLionel Sambuc 	return 1;
601ebfedea0SLionel Sambuc     }
602ebfedea0SLionel Sambuc     p = strchr(buf, '\n');
603ebfedea0SLionel Sambuc     if(p)
604ebfedea0SLionel Sambuc 	*p = '\0';
605ebfedea0SLionel Sambuc     if(strcmp(buf, "") == 0)
606ebfedea0SLionel Sambuc 	strlcpy(buf, def, len);
607ebfedea0SLionel Sambuc     signal(SIGINT, osig);
608ebfedea0SLionel Sambuc     return 0;
609ebfedea0SLionel Sambuc }
610ebfedea0SLionel Sambuc 
611ebfedea0SLionel Sambuc /*
612ebfedea0SLionel Sambuc  * return [0, 16) or -1
613ebfedea0SLionel Sambuc  */
614ebfedea0SLionel Sambuc 
615ebfedea0SLionel Sambuc static int
hex2n(char c)616ebfedea0SLionel Sambuc hex2n (char c)
617ebfedea0SLionel Sambuc {
618ebfedea0SLionel Sambuc     static char hexdigits[] = "0123456789abcdef";
619ebfedea0SLionel Sambuc     const char *p;
620ebfedea0SLionel Sambuc 
621ebfedea0SLionel Sambuc     p = strchr (hexdigits, tolower((unsigned char)c));
622ebfedea0SLionel Sambuc     if (p == NULL)
623ebfedea0SLionel Sambuc 	return -1;
624ebfedea0SLionel Sambuc     else
625ebfedea0SLionel Sambuc 	return p - hexdigits;
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc 
628ebfedea0SLionel Sambuc /*
629ebfedea0SLionel Sambuc  * convert a key in a readable format into a keyblock.
630ebfedea0SLionel Sambuc  * return 0 iff succesful, otherwise `err' should point to an error message
631ebfedea0SLionel Sambuc  */
632ebfedea0SLionel Sambuc 
633ebfedea0SLionel Sambuc int
parse_des_key(const char * key_string,krb5_key_data * key_data,const char ** error)634ebfedea0SLionel Sambuc parse_des_key (const char *key_string, krb5_key_data *key_data,
635ebfedea0SLionel Sambuc 	       const char **error)
636ebfedea0SLionel Sambuc {
637ebfedea0SLionel Sambuc     const char *p = key_string;
638ebfedea0SLionel Sambuc     unsigned char bits[8];
639ebfedea0SLionel Sambuc     int i;
640ebfedea0SLionel Sambuc 
641ebfedea0SLionel Sambuc     if (strlen (key_string) != 16) {
642ebfedea0SLionel Sambuc 	*error = "bad length, should be 16 for DES key";
643ebfedea0SLionel Sambuc 	return 1;
644ebfedea0SLionel Sambuc     }
645ebfedea0SLionel Sambuc     for (i = 0; i < 8; ++i) {
646ebfedea0SLionel Sambuc 	int d1, d2;
647ebfedea0SLionel Sambuc 
648ebfedea0SLionel Sambuc 	d1 = hex2n(p[2 * i]);
649ebfedea0SLionel Sambuc 	d2 = hex2n(p[2 * i + 1]);
650ebfedea0SLionel Sambuc 	if (d1 < 0 || d2 < 0) {
651ebfedea0SLionel Sambuc 	    *error = "non-hex character";
652ebfedea0SLionel Sambuc 	    return 1;
653ebfedea0SLionel Sambuc 	}
654ebfedea0SLionel Sambuc 	bits[i] = (d1 << 4) | d2;
655ebfedea0SLionel Sambuc     }
656ebfedea0SLionel Sambuc     for (i = 0; i < 3; ++i) {
657ebfedea0SLionel Sambuc 	key_data[i].key_data_ver  = 2;
658ebfedea0SLionel Sambuc 	key_data[i].key_data_kvno = 0;
659ebfedea0SLionel Sambuc 	/* key */
660ebfedea0SLionel Sambuc 	key_data[i].key_data_type[0]     = ETYPE_DES_CBC_CRC;
661ebfedea0SLionel Sambuc 	key_data[i].key_data_length[0]   = 8;
662ebfedea0SLionel Sambuc 	key_data[i].key_data_contents[0] = malloc(8);
663ebfedea0SLionel Sambuc 	if (key_data[i].key_data_contents[0] == NULL) {
664ebfedea0SLionel Sambuc 	    *error = "malloc";
665ebfedea0SLionel Sambuc 	    return ENOMEM;
666ebfedea0SLionel Sambuc 	}
667ebfedea0SLionel Sambuc 	memcpy (key_data[i].key_data_contents[0], bits, 8);
668ebfedea0SLionel Sambuc 	/* salt */
669ebfedea0SLionel Sambuc 	key_data[i].key_data_type[1]     = KRB5_PW_SALT;
670ebfedea0SLionel Sambuc 	key_data[i].key_data_length[1]   = 0;
671ebfedea0SLionel Sambuc 	key_data[i].key_data_contents[1] = NULL;
672ebfedea0SLionel Sambuc     }
673ebfedea0SLionel Sambuc     key_data[0].key_data_type[0] = ETYPE_DES_CBC_MD5;
674ebfedea0SLionel Sambuc     key_data[1].key_data_type[0] = ETYPE_DES_CBC_MD4;
675ebfedea0SLionel Sambuc     return 0;
676ebfedea0SLionel Sambuc }
677