1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov * Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan
3b528cefcSMark Murray * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray * All rights reserved.
5b528cefcSMark Murray *
6b528cefcSMark Murray * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray * modification, are permitted provided that the following conditions
8b528cefcSMark Murray * are met:
9b528cefcSMark Murray *
10b528cefcSMark Murray * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray * notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray *
13b528cefcSMark Murray * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray * notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray * documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray *
17b528cefcSMark Murray * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray * may be used to endorse or promote products derived from this software
19b528cefcSMark Murray * without specific prior written permission.
20b528cefcSMark Murray *
21b528cefcSMark Murray * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray * SUCH DAMAGE.
32b528cefcSMark Murray */
33b528cefcSMark Murray
34b528cefcSMark Murray #include "kadmin_locl.h"
35b528cefcSMark Murray #include <parse_units.h>
36b528cefcSMark Murray
37b528cefcSMark Murray /*
38b528cefcSMark Murray * util.c - functions for parsing, unparsing, and editing different
39b528cefcSMark Murray * types of data used in kadmin.
40b528cefcSMark Murray */
41b528cefcSMark Murray
424137ff4cSJacques Vidrine static int
434137ff4cSJacques Vidrine get_response(const char *prompt, const char *def, char *buf, size_t len);
444137ff4cSJacques Vidrine
45b528cefcSMark Murray /*
46b528cefcSMark Murray * attributes
47b528cefcSMark Murray */
48b528cefcSMark Murray
49b528cefcSMark Murray struct units kdb_attrs[] = {
50c19800e8SDoug Rabson { "allow-digest", KRB5_KDB_ALLOW_DIGEST },
51c19800e8SDoug Rabson { "allow-kerberos4", KRB5_KDB_ALLOW_KERBEROS4 },
52c19800e8SDoug Rabson { "trusted-for-delegation", KRB5_KDB_TRUSTED_FOR_DELEGATION },
53c19800e8SDoug Rabson { "ok-as-delegate", KRB5_KDB_OK_AS_DELEGATE },
54b528cefcSMark Murray { "new-princ", KRB5_KDB_NEW_PRINC },
55b528cefcSMark Murray { "support-desmd5", KRB5_KDB_SUPPORT_DESMD5 },
56b528cefcSMark Murray { "pwchange-service", KRB5_KDB_PWCHANGE_SERVICE },
57b528cefcSMark Murray { "disallow-svr", KRB5_KDB_DISALLOW_SVR },
58b528cefcSMark Murray { "requires-pw-change", KRB5_KDB_REQUIRES_PWCHANGE },
59b528cefcSMark Murray { "requires-hw-auth", KRB5_KDB_REQUIRES_HW_AUTH },
60b528cefcSMark Murray { "requires-pre-auth", KRB5_KDB_REQUIRES_PRE_AUTH },
61b528cefcSMark Murray { "disallow-all-tix", KRB5_KDB_DISALLOW_ALL_TIX },
62b528cefcSMark Murray { "disallow-dup-skey", KRB5_KDB_DISALLOW_DUP_SKEY },
63b528cefcSMark Murray { "disallow-proxiable", KRB5_KDB_DISALLOW_PROXIABLE },
64b528cefcSMark Murray { "disallow-renewable", KRB5_KDB_DISALLOW_RENEWABLE },
65b528cefcSMark Murray { "disallow-tgt-based", KRB5_KDB_DISALLOW_TGT_BASED },
66b528cefcSMark Murray { "disallow-forwardable", KRB5_KDB_DISALLOW_FORWARDABLE },
67b528cefcSMark Murray { "disallow-postdated", KRB5_KDB_DISALLOW_POSTDATED },
68*ae771770SStanislav Sedov { NULL, 0 }
69b528cefcSMark Murray };
70b528cefcSMark Murray
71b528cefcSMark Murray /*
72b528cefcSMark Murray * convert the attributes in `attributes' into a printable string
73b528cefcSMark Murray * in `str, len'
74b528cefcSMark Murray */
75b528cefcSMark Murray
76b528cefcSMark Murray void
attributes2str(krb5_flags attributes,char * str,size_t len)77b528cefcSMark Murray attributes2str(krb5_flags attributes, char *str, size_t len)
78b528cefcSMark Murray {
79b528cefcSMark Murray unparse_flags (attributes, kdb_attrs, str, len);
80b528cefcSMark Murray }
81b528cefcSMark Murray
82b528cefcSMark Murray /*
83b528cefcSMark Murray * convert the string in `str' into attributes in `flags'
84b528cefcSMark Murray * return 0 if parsed ok, else -1.
85b528cefcSMark Murray */
86b528cefcSMark Murray
87b528cefcSMark Murray int
str2attributes(const char * str,krb5_flags * flags)88b528cefcSMark Murray str2attributes(const char *str, krb5_flags *flags)
89b528cefcSMark Murray {
90b528cefcSMark Murray int res;
91b528cefcSMark Murray
92b528cefcSMark Murray res = parse_flags (str, kdb_attrs, *flags);
93b528cefcSMark Murray if (res < 0)
94b528cefcSMark Murray return res;
95b528cefcSMark Murray else {
96b528cefcSMark Murray *flags = res;
97b528cefcSMark Murray return 0;
98b528cefcSMark Murray }
99b528cefcSMark Murray }
100b528cefcSMark Murray
101b528cefcSMark Murray /*
102b528cefcSMark Murray * try to parse the string `resp' into attributes in `attr', also
103b528cefcSMark Murray * setting the `bit' in `mask' if attributes are given and valid.
104b528cefcSMark Murray */
105b528cefcSMark Murray
106b528cefcSMark Murray int
parse_attributes(const char * resp,krb5_flags * attr,int * mask,int bit)107b528cefcSMark Murray parse_attributes (const char *resp, krb5_flags *attr, int *mask, int bit)
108b528cefcSMark Murray {
109b528cefcSMark Murray krb5_flags tmp = *attr;
110b528cefcSMark Murray
1115e9cd1aeSAssar Westerlund if (str2attributes(resp, &tmp) == 0) {
112b528cefcSMark Murray *attr = tmp;
113b528cefcSMark Murray if (mask)
114b528cefcSMark Murray *mask |= bit;
115b528cefcSMark Murray return 0;
116b528cefcSMark Murray } else if(*resp == '?') {
117b528cefcSMark Murray print_flags_table (kdb_attrs, stderr);
118b528cefcSMark Murray } else {
119c19800e8SDoug Rabson fprintf (stderr, "Unable to parse \"%s\"\n", resp);
120b528cefcSMark Murray }
121b528cefcSMark Murray return -1;
122b528cefcSMark Murray }
123b528cefcSMark Murray
124b528cefcSMark Murray /*
125b528cefcSMark Murray * allow the user to edit the attributes in `attr', prompting with `prompt'
126b528cefcSMark Murray */
127b528cefcSMark Murray
128b528cefcSMark Murray int
edit_attributes(const char * prompt,krb5_flags * attr,int * mask,int bit)129b528cefcSMark Murray edit_attributes (const char *prompt, krb5_flags *attr, int *mask, int bit)
130b528cefcSMark Murray {
131b528cefcSMark Murray char buf[1024], resp[1024];
132b528cefcSMark Murray
133b528cefcSMark Murray if (mask && (*mask & bit))
134b528cefcSMark Murray return 0;
135b528cefcSMark Murray
136b528cefcSMark Murray attributes2str(*attr, buf, sizeof(buf));
137b528cefcSMark Murray for (;;) {
1384137ff4cSJacques Vidrine if(get_response("Attributes", buf, resp, sizeof(resp)) != 0)
1394137ff4cSJacques Vidrine return 1;
1405e9cd1aeSAssar Westerlund if (resp[0] == '\0')
1415e9cd1aeSAssar Westerlund break;
142b528cefcSMark Murray if (parse_attributes (resp, attr, mask, bit) == 0)
143b528cefcSMark Murray break;
144b528cefcSMark Murray }
145b528cefcSMark Murray return 0;
146b528cefcSMark Murray }
147b528cefcSMark Murray
148b528cefcSMark Murray /*
149b528cefcSMark Murray * time_t
150b528cefcSMark Murray * the special value 0 means ``never''
151b528cefcSMark Murray */
152b528cefcSMark Murray
153b528cefcSMark Murray /*
154b528cefcSMark Murray * Convert the time `t' to a string representation in `str' (of max
155b528cefcSMark Murray * size `len'). If include_time also include time, otherwise just
156b528cefcSMark Murray * date.
157b528cefcSMark Murray */
158b528cefcSMark Murray
159b528cefcSMark Murray void
time_t2str(time_t t,char * str,size_t len,int include_time)160b528cefcSMark Murray time_t2str(time_t t, char *str, size_t len, int include_time)
161b528cefcSMark Murray {
162b528cefcSMark Murray if(t) {
163b528cefcSMark Murray if(include_time)
164b528cefcSMark Murray strftime(str, len, "%Y-%m-%d %H:%M:%S UTC", gmtime(&t));
165b528cefcSMark Murray else
166b528cefcSMark Murray strftime(str, len, "%Y-%m-%d", gmtime(&t));
167b528cefcSMark Murray } else
168b528cefcSMark Murray snprintf(str, len, "never");
169b528cefcSMark Murray }
170b528cefcSMark Murray
171b528cefcSMark Murray /*
172b528cefcSMark Murray * Convert the time representation in `str' to a time in `time'.
173b528cefcSMark Murray * Return 0 if succesful, else -1.
174b528cefcSMark Murray */
175b528cefcSMark Murray
176b528cefcSMark Murray int
str2time_t(const char * str,time_t * t)1775e9cd1aeSAssar Westerlund str2time_t (const char *str, time_t *t)
178b528cefcSMark Murray {
179b528cefcSMark Murray const char *p;
1805e9cd1aeSAssar Westerlund struct tm tm, tm2;
181b528cefcSMark Murray
182b528cefcSMark Murray memset (&tm, 0, sizeof (tm));
183c19800e8SDoug Rabson memset (&tm2, 0, sizeof (tm2));
184b528cefcSMark Murray
185*ae771770SStanislav Sedov while(isspace((unsigned char)*str))
186*ae771770SStanislav Sedov str++;
187*ae771770SStanislav Sedov
188*ae771770SStanislav Sedov if (str[0] == '+') {
189*ae771770SStanislav Sedov str++;
190*ae771770SStanislav Sedov *t = parse_time(str, "month");
191*ae771770SStanislav Sedov if (*t < 0)
192*ae771770SStanislav Sedov return -1;
193*ae771770SStanislav Sedov *t += time(NULL);
194*ae771770SStanislav Sedov return 0;
195*ae771770SStanislav Sedov }
196*ae771770SStanislav Sedov
197b528cefcSMark Murray if(strcasecmp(str, "never") == 0) {
1985e9cd1aeSAssar Westerlund *t = 0;
1995e9cd1aeSAssar Westerlund return 0;
2005e9cd1aeSAssar Westerlund }
2015e9cd1aeSAssar Westerlund
2025e9cd1aeSAssar Westerlund if(strcasecmp(str, "now") == 0) {
2035e9cd1aeSAssar Westerlund *t = time(NULL);
204b528cefcSMark Murray return 0;
205b528cefcSMark Murray }
206b528cefcSMark Murray
207b528cefcSMark Murray p = strptime (str, "%Y-%m-%d", &tm);
208b528cefcSMark Murray
209b528cefcSMark Murray if (p == NULL)
210b528cefcSMark Murray return -1;
211b528cefcSMark Murray
212c19800e8SDoug Rabson while(isspace((unsigned char)*p))
213c19800e8SDoug Rabson p++;
214b528cefcSMark Murray
215c19800e8SDoug Rabson /* XXX this is really a bit optimistic, we should really complain
216c19800e8SDoug Rabson if there was a problem parsing the time */
217c19800e8SDoug Rabson if(p[0] != '\0' && strptime (p, "%H:%M:%S", &tm2) != NULL) {
2185e9cd1aeSAssar Westerlund tm.tm_hour = tm2.tm_hour;
2195e9cd1aeSAssar Westerlund tm.tm_min = tm2.tm_min;
2205e9cd1aeSAssar Westerlund tm.tm_sec = tm2.tm_sec;
221c19800e8SDoug Rabson } else {
222c19800e8SDoug Rabson /* Do it on the end of the day */
223c19800e8SDoug Rabson tm.tm_hour = 23;
224c19800e8SDoug Rabson tm.tm_min = 59;
225c19800e8SDoug Rabson tm.tm_sec = 59;
2265e9cd1aeSAssar Westerlund }
227b528cefcSMark Murray
2285e9cd1aeSAssar Westerlund *t = tm2time (tm, 0);
229b528cefcSMark Murray return 0;
230b528cefcSMark Murray }
231b528cefcSMark Murray
232b528cefcSMark Murray /*
233b528cefcSMark Murray * try to parse the time in `resp' storing it in `value'
234b528cefcSMark Murray */
235b528cefcSMark Murray
236b528cefcSMark Murray int
parse_timet(const char * resp,krb5_timestamp * value,int * mask,int bit)237b528cefcSMark Murray parse_timet (const char *resp, krb5_timestamp *value, int *mask, int bit)
238b528cefcSMark Murray {
239b528cefcSMark Murray time_t tmp;
240b528cefcSMark Murray
241b528cefcSMark Murray if (str2time_t(resp, &tmp) == 0) {
242b528cefcSMark Murray *value = tmp;
243b528cefcSMark Murray if(mask)
244b528cefcSMark Murray *mask |= bit;
245b528cefcSMark Murray return 0;
246b528cefcSMark Murray }
247c19800e8SDoug Rabson if(*resp != '?')
248c19800e8SDoug Rabson fprintf (stderr, "Unable to parse time \"%s\"\n", resp);
249c19800e8SDoug Rabson fprintf (stderr, "Print date on format YYYY-mm-dd [hh:mm:ss]\n");
250b528cefcSMark Murray return -1;
251b528cefcSMark Murray }
252b528cefcSMark Murray
253b528cefcSMark Murray /*
254b528cefcSMark Murray * allow the user to edit the time in `value'
255b528cefcSMark Murray */
256b528cefcSMark Murray
257b528cefcSMark Murray int
edit_timet(const char * prompt,krb5_timestamp * value,int * mask,int bit)258b528cefcSMark Murray edit_timet (const char *prompt, krb5_timestamp *value, int *mask, int bit)
259b528cefcSMark Murray {
260b528cefcSMark Murray char buf[1024], resp[1024];
261b528cefcSMark Murray
262b528cefcSMark Murray if (mask && (*mask & bit))
263b528cefcSMark Murray return 0;
264b528cefcSMark Murray
265b528cefcSMark Murray time_t2str (*value, buf, sizeof (buf), 0);
266b528cefcSMark Murray
267b528cefcSMark Murray for (;;) {
2684137ff4cSJacques Vidrine if(get_response(prompt, buf, resp, sizeof(resp)) != 0)
2694137ff4cSJacques Vidrine return 1;
270b528cefcSMark Murray if (parse_timet (resp, value, mask, bit) == 0)
271b528cefcSMark Murray break;
272b528cefcSMark Murray }
273b528cefcSMark Murray return 0;
274b528cefcSMark Murray }
275b528cefcSMark Murray
276b528cefcSMark Murray /*
277b528cefcSMark Murray * deltat
278b528cefcSMark Murray * the special value 0 means ``unlimited''
279b528cefcSMark Murray */
280b528cefcSMark Murray
281b528cefcSMark Murray /*
282b528cefcSMark Murray * convert the delta_t value in `t' into a printable form in `str, len'
283b528cefcSMark Murray */
284b528cefcSMark Murray
285b528cefcSMark Murray void
deltat2str(unsigned t,char * str,size_t len)286b528cefcSMark Murray deltat2str(unsigned t, char *str, size_t len)
287b528cefcSMark Murray {
2885e9cd1aeSAssar Westerlund if(t == 0 || t == INT_MAX)
289b528cefcSMark Murray snprintf(str, len, "unlimited");
2905e9cd1aeSAssar Westerlund else
2915e9cd1aeSAssar Westerlund unparse_time(t, str, len);
292b528cefcSMark Murray }
293b528cefcSMark Murray
294b528cefcSMark Murray /*
295b528cefcSMark Murray * parse the delta value in `str', storing result in `*delta'
296b528cefcSMark Murray * return 0 if ok, else -1
297b528cefcSMark Murray */
298b528cefcSMark Murray
299b528cefcSMark Murray int
str2deltat(const char * str,krb5_deltat * delta)300b528cefcSMark Murray str2deltat(const char *str, krb5_deltat *delta)
301b528cefcSMark Murray {
302b528cefcSMark Murray int res;
303b528cefcSMark Murray
304b528cefcSMark Murray if(strcasecmp(str, "unlimited") == 0) {
305b528cefcSMark Murray *delta = 0;
306b528cefcSMark Murray return 0;
307b528cefcSMark Murray }
308b528cefcSMark Murray res = parse_time(str, "day");
309b528cefcSMark Murray if (res < 0)
310b528cefcSMark Murray return res;
311b528cefcSMark Murray else {
312b528cefcSMark Murray *delta = res;
313b528cefcSMark Murray return 0;
314b528cefcSMark Murray }
315b528cefcSMark Murray }
316b528cefcSMark Murray
317b528cefcSMark Murray /*
318b528cefcSMark Murray * try to parse the string in `resp' into a deltad in `value'
319b528cefcSMark Murray * `mask' will get the bit `bit' set if a value was given.
320b528cefcSMark Murray */
321b528cefcSMark Murray
322b528cefcSMark Murray int
parse_deltat(const char * resp,krb5_deltat * value,int * mask,int bit)323b528cefcSMark Murray parse_deltat (const char *resp, krb5_deltat *value, int *mask, int bit)
324b528cefcSMark Murray {
325b528cefcSMark Murray krb5_deltat tmp;
326b528cefcSMark Murray
327b528cefcSMark Murray if (str2deltat(resp, &tmp) == 0) {
328b528cefcSMark Murray *value = tmp;
329b528cefcSMark Murray if (mask)
330b528cefcSMark Murray *mask |= bit;
331b528cefcSMark Murray return 0;
332b528cefcSMark Murray } else if(*resp == '?') {
333b528cefcSMark Murray print_time_table (stderr);
334b528cefcSMark Murray } else {
335c19800e8SDoug Rabson fprintf (stderr, "Unable to parse time \"%s\"\n", resp);
336b528cefcSMark Murray }
337b528cefcSMark Murray return -1;
338b528cefcSMark Murray }
339b528cefcSMark Murray
340b528cefcSMark Murray /*
341b528cefcSMark Murray * allow the user to edit the deltat in `value'
342b528cefcSMark Murray */
343b528cefcSMark Murray
344b528cefcSMark Murray int
edit_deltat(const char * prompt,krb5_deltat * value,int * mask,int bit)345b528cefcSMark Murray edit_deltat (const char *prompt, krb5_deltat *value, int *mask, int bit)
346b528cefcSMark Murray {
347b528cefcSMark Murray char buf[1024], resp[1024];
348b528cefcSMark Murray
349b528cefcSMark Murray if (mask && (*mask & bit))
350b528cefcSMark Murray return 0;
351b528cefcSMark Murray
352b528cefcSMark Murray deltat2str(*value, buf, sizeof(buf));
353b528cefcSMark Murray for (;;) {
3544137ff4cSJacques Vidrine if(get_response(prompt, buf, resp, sizeof(resp)) != 0)
3554137ff4cSJacques Vidrine return 1;
356b528cefcSMark Murray if (parse_deltat (resp, value, mask, bit) == 0)
357b528cefcSMark Murray break;
358b528cefcSMark Murray }
359b528cefcSMark Murray return 0;
360b528cefcSMark Murray }
361b528cefcSMark Murray
362b528cefcSMark Murray /*
363b528cefcSMark Murray * allow the user to edit `ent'
364b528cefcSMark Murray */
365b528cefcSMark Murray
3664137ff4cSJacques Vidrine void
set_defaults(kadm5_principal_ent_t ent,int * mask,kadm5_principal_ent_t default_ent,int default_mask)3674137ff4cSJacques Vidrine set_defaults(kadm5_principal_ent_t ent, int *mask,
368b528cefcSMark Murray kadm5_principal_ent_t default_ent, int default_mask)
369b528cefcSMark Murray {
3705e9cd1aeSAssar Westerlund if (default_ent
3715e9cd1aeSAssar Westerlund && (default_mask & KADM5_MAX_LIFE)
3725e9cd1aeSAssar Westerlund && !(*mask & KADM5_MAX_LIFE))
373b528cefcSMark Murray ent->max_life = default_ent->max_life;
374b528cefcSMark Murray
3755e9cd1aeSAssar Westerlund if (default_ent
3765e9cd1aeSAssar Westerlund && (default_mask & KADM5_MAX_RLIFE)
3775e9cd1aeSAssar Westerlund && !(*mask & KADM5_MAX_RLIFE))
378b528cefcSMark Murray ent->max_renewable_life = default_ent->max_renewable_life;
379b528cefcSMark Murray
3805e9cd1aeSAssar Westerlund if (default_ent
3815e9cd1aeSAssar Westerlund && (default_mask & KADM5_PRINC_EXPIRE_TIME)
3825e9cd1aeSAssar Westerlund && !(*mask & KADM5_PRINC_EXPIRE_TIME))
383b528cefcSMark Murray ent->princ_expire_time = default_ent->princ_expire_time;
384b528cefcSMark Murray
3855e9cd1aeSAssar Westerlund if (default_ent
3865e9cd1aeSAssar Westerlund && (default_mask & KADM5_PW_EXPIRATION)
3875e9cd1aeSAssar Westerlund && !(*mask & KADM5_PW_EXPIRATION))
388b528cefcSMark Murray ent->pw_expiration = default_ent->pw_expiration;
389b528cefcSMark Murray
3905e9cd1aeSAssar Westerlund if (default_ent
3915e9cd1aeSAssar Westerlund && (default_mask & KADM5_ATTRIBUTES)
3925e9cd1aeSAssar Westerlund && !(*mask & KADM5_ATTRIBUTES))
393b528cefcSMark Murray ent->attributes = default_ent->attributes & ~KRB5_KDB_DISALLOW_ALL_TIX;
3944137ff4cSJacques Vidrine }
3954137ff4cSJacques Vidrine
3964137ff4cSJacques Vidrine int
edit_entry(kadm5_principal_ent_t ent,int * mask,kadm5_principal_ent_t default_ent,int default_mask)3974137ff4cSJacques Vidrine edit_entry(kadm5_principal_ent_t ent, int *mask,
3984137ff4cSJacques Vidrine kadm5_principal_ent_t default_ent, int default_mask)
3994137ff4cSJacques Vidrine {
4004137ff4cSJacques Vidrine
4014137ff4cSJacques Vidrine set_defaults(ent, mask, default_ent, default_mask);
4024137ff4cSJacques Vidrine
4034137ff4cSJacques Vidrine if(edit_deltat ("Max ticket life", &ent->max_life, mask,
4044137ff4cSJacques Vidrine KADM5_MAX_LIFE) != 0)
4054137ff4cSJacques Vidrine return 1;
4064137ff4cSJacques Vidrine
4074137ff4cSJacques Vidrine if(edit_deltat ("Max renewable life", &ent->max_renewable_life, mask,
4084137ff4cSJacques Vidrine KADM5_MAX_RLIFE) != 0)
4094137ff4cSJacques Vidrine return 1;
4104137ff4cSJacques Vidrine
4114137ff4cSJacques Vidrine if(edit_timet ("Principal expiration time", &ent->princ_expire_time, mask,
4124137ff4cSJacques Vidrine KADM5_PRINC_EXPIRE_TIME) != 0)
4134137ff4cSJacques Vidrine return 1;
4144137ff4cSJacques Vidrine
4154137ff4cSJacques Vidrine if(edit_timet ("Password expiration time", &ent->pw_expiration, mask,
4164137ff4cSJacques Vidrine KADM5_PW_EXPIRATION) != 0)
4174137ff4cSJacques Vidrine return 1;
4184137ff4cSJacques Vidrine
4194137ff4cSJacques Vidrine if(edit_attributes ("Attributes", &ent->attributes, mask,
4204137ff4cSJacques Vidrine KADM5_ATTRIBUTES) != 0)
4214137ff4cSJacques Vidrine return 1;
4224137ff4cSJacques Vidrine
423b528cefcSMark Murray return 0;
424b528cefcSMark Murray }
425b528cefcSMark Murray
426b528cefcSMark Murray /*
427b528cefcSMark Murray * Parse the arguments, set the fields in `ent' and the `mask' for the
428b528cefcSMark Murray * entries having been set.
429b528cefcSMark Murray * Return 1 on failure and 0 on success.
430b528cefcSMark Murray */
431b528cefcSMark Murray
432b528cefcSMark Murray 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)433*ae771770SStanislav Sedov set_entry(krb5_context contextp,
434b528cefcSMark Murray kadm5_principal_ent_t ent,
435b528cefcSMark Murray int *mask,
436b528cefcSMark Murray const char *max_ticket_life,
437b528cefcSMark Murray const char *max_renewable_life,
438b528cefcSMark Murray const char *expiration,
439b528cefcSMark Murray const char *pw_expiration,
440b528cefcSMark Murray const char *attributes)
441b528cefcSMark Murray {
442b528cefcSMark Murray if (max_ticket_life != NULL) {
443b528cefcSMark Murray if (parse_deltat (max_ticket_life, &ent->max_life,
444b528cefcSMark Murray mask, KADM5_MAX_LIFE)) {
445*ae771770SStanislav Sedov krb5_warnx (contextp, "unable to parse `%s'", max_ticket_life);
446b528cefcSMark Murray return 1;
447b528cefcSMark Murray }
448b528cefcSMark Murray }
449b528cefcSMark Murray if (max_renewable_life != NULL) {
450b528cefcSMark Murray if (parse_deltat (max_renewable_life, &ent->max_renewable_life,
451b528cefcSMark Murray mask, KADM5_MAX_RLIFE)) {
452*ae771770SStanislav Sedov krb5_warnx (contextp, "unable to parse `%s'", max_renewable_life);
453b528cefcSMark Murray return 1;
454b528cefcSMark Murray }
455b528cefcSMark Murray }
456b528cefcSMark Murray
457b528cefcSMark Murray if (expiration) {
458b528cefcSMark Murray if (parse_timet (expiration, &ent->princ_expire_time,
459b528cefcSMark Murray mask, KADM5_PRINC_EXPIRE_TIME)) {
460*ae771770SStanislav Sedov krb5_warnx (contextp, "unable to parse `%s'", expiration);
461b528cefcSMark Murray return 1;
462b528cefcSMark Murray }
463b528cefcSMark Murray }
464b528cefcSMark Murray if (pw_expiration) {
465b528cefcSMark Murray if (parse_timet (pw_expiration, &ent->pw_expiration,
466b528cefcSMark Murray mask, KADM5_PW_EXPIRATION)) {
467*ae771770SStanislav Sedov krb5_warnx (contextp, "unable to parse `%s'", pw_expiration);
468b528cefcSMark Murray return 1;
469b528cefcSMark Murray }
470b528cefcSMark Murray }
471b528cefcSMark Murray if (attributes != NULL) {
472b528cefcSMark Murray if (parse_attributes (attributes, &ent->attributes,
473b528cefcSMark Murray mask, KADM5_ATTRIBUTES)) {
474*ae771770SStanislav Sedov krb5_warnx (contextp, "unable to parse `%s'", attributes);
475b528cefcSMark Murray return 1;
476b528cefcSMark Murray }
477b528cefcSMark Murray }
478b528cefcSMark Murray return 0;
479b528cefcSMark Murray }
480b528cefcSMark Murray
481b528cefcSMark Murray /*
482b528cefcSMark Murray * Does `string' contain any globing characters?
483b528cefcSMark Murray */
484b528cefcSMark Murray
485b528cefcSMark Murray static int
is_expression(const char * string)486b528cefcSMark Murray is_expression(const char *string)
487b528cefcSMark Murray {
488b528cefcSMark Murray const char *p;
489b528cefcSMark Murray int quote = 0;
490b528cefcSMark Murray
491b528cefcSMark Murray for(p = string; *p; p++) {
492b528cefcSMark Murray if(quote) {
493b528cefcSMark Murray quote = 0;
494b528cefcSMark Murray continue;
495b528cefcSMark Murray }
496b528cefcSMark Murray if(*p == '\\')
497b528cefcSMark Murray quote++;
498b528cefcSMark Murray else if(strchr("[]*?", *p) != NULL)
499b528cefcSMark Murray return 1;
500b528cefcSMark Murray }
501b528cefcSMark Murray return 0;
502b528cefcSMark Murray }
503b528cefcSMark Murray
504c19800e8SDoug Rabson /*
505c19800e8SDoug Rabson * Loop over all principals matching exp. If any of calls to `func'
506c19800e8SDoug Rabson * failes, the first error is returned when all principals are
507c19800e8SDoug Rabson * processed.
508c19800e8SDoug Rabson */
509b528cefcSMark Murray int
foreach_principal(const char * exp_str,int (* func)(krb5_principal,void *),const char * funcname,void * data)510c19800e8SDoug Rabson foreach_principal(const char *exp_str,
511b528cefcSMark Murray int (*func)(krb5_principal, void*),
512adb0ddaeSAssar Westerlund const char *funcname,
513b528cefcSMark Murray void *data)
514b528cefcSMark Murray {
515*ae771770SStanislav Sedov char **princs = NULL;
516*ae771770SStanislav Sedov int num_princs = 0;
517b528cefcSMark Murray int i;
518c19800e8SDoug Rabson krb5_error_code saved_ret = 0, ret = 0;
519b528cefcSMark Murray krb5_principal princ_ent;
520b528cefcSMark Murray int is_expr;
521b528cefcSMark Murray
522b528cefcSMark Murray /* if this isn't an expression, there is no point in wading
523b528cefcSMark Murray through the whole database looking for matches */
524c19800e8SDoug Rabson is_expr = is_expression(exp_str);
525b528cefcSMark Murray if(is_expr)
526c19800e8SDoug Rabson ret = kadm5_get_principals(kadm_handle, exp_str, &princs, &num_princs);
527b528cefcSMark Murray if(!is_expr || ret == KADM5_AUTH_LIST) {
528b528cefcSMark Murray /* we might be able to perform the requested opreration even
529b528cefcSMark Murray if we're not allowed to list principals */
530b528cefcSMark Murray num_princs = 1;
531b528cefcSMark Murray princs = malloc(sizeof(*princs));
532b528cefcSMark Murray if(princs == NULL)
533b528cefcSMark Murray return ENOMEM;
534c19800e8SDoug Rabson princs[0] = strdup(exp_str);
535b528cefcSMark Murray if(princs[0] == NULL){
536b528cefcSMark Murray free(princs);
537b528cefcSMark Murray return ENOMEM;
538b528cefcSMark Murray }
539b528cefcSMark Murray } else if(ret) {
540b528cefcSMark Murray krb5_warn(context, ret, "kadm5_get_principals");
541b528cefcSMark Murray return ret;
542b528cefcSMark Murray }
543b528cefcSMark Murray for(i = 0; i < num_princs; i++) {
544b528cefcSMark Murray ret = krb5_parse_name(context, princs[i], &princ_ent);
545b528cefcSMark Murray if(ret){
546b528cefcSMark Murray krb5_warn(context, ret, "krb5_parse_name(%s)", princs[i]);
547b528cefcSMark Murray continue;
548b528cefcSMark Murray }
549b528cefcSMark Murray ret = (*func)(princ_ent, data);
550c19800e8SDoug Rabson if(ret) {
551*ae771770SStanislav Sedov krb5_clear_error_message(context);
552adb0ddaeSAssar Westerlund krb5_warn(context, ret, "%s %s", funcname, princs[i]);
553c19800e8SDoug Rabson if (saved_ret == 0)
554c19800e8SDoug Rabson saved_ret = ret;
555c19800e8SDoug Rabson }
556b528cefcSMark Murray krb5_free_principal(context, princ_ent);
557b528cefcSMark Murray }
558c19800e8SDoug Rabson if (ret == 0 && saved_ret != 0)
559c19800e8SDoug Rabson ret = saved_ret;
560b528cefcSMark Murray kadm5_free_name_list(kadm_handle, princs, &num_princs);
561c19800e8SDoug Rabson return ret;
562b528cefcSMark Murray }
563b528cefcSMark Murray
564b528cefcSMark Murray /*
565b528cefcSMark Murray * prompt with `prompt' and default value `def', and store the reply
566b528cefcSMark Murray * in `buf, len'
567b528cefcSMark Murray */
568b528cefcSMark Murray
5694137ff4cSJacques Vidrine #include <setjmp.h>
5704137ff4cSJacques Vidrine
5714137ff4cSJacques Vidrine static jmp_buf jmpbuf;
5724137ff4cSJacques Vidrine
5734137ff4cSJacques Vidrine static void
interrupt(int sig)5744137ff4cSJacques Vidrine interrupt(int sig)
5754137ff4cSJacques Vidrine {
5764137ff4cSJacques Vidrine longjmp(jmpbuf, 1);
5774137ff4cSJacques Vidrine }
5784137ff4cSJacques Vidrine
5794137ff4cSJacques Vidrine static int
get_response(const char * prompt,const char * def,char * buf,size_t len)580b528cefcSMark Murray get_response(const char *prompt, const char *def, char *buf, size_t len)
581b528cefcSMark Murray {
582b528cefcSMark Murray char *p;
5834137ff4cSJacques Vidrine void (*osig)(int);
5844137ff4cSJacques Vidrine
5854137ff4cSJacques Vidrine osig = signal(SIGINT, interrupt);
5864137ff4cSJacques Vidrine if(setjmp(jmpbuf)) {
5874137ff4cSJacques Vidrine signal(SIGINT, osig);
588c19800e8SDoug Rabson fprintf(stderr, "\n");
5894137ff4cSJacques Vidrine return 1;
5904137ff4cSJacques Vidrine }
591b528cefcSMark Murray
592c19800e8SDoug Rabson fprintf(stderr, "%s [%s]:", prompt, def);
5934137ff4cSJacques Vidrine if(fgets(buf, len, stdin) == NULL) {
5944137ff4cSJacques Vidrine int save_errno = errno;
5954137ff4cSJacques Vidrine if(ferror(stdin))
5964137ff4cSJacques Vidrine krb5_err(context, 1, save_errno, "<stdin>");
5974137ff4cSJacques Vidrine signal(SIGINT, osig);
5984137ff4cSJacques Vidrine return 1;
5994137ff4cSJacques Vidrine }
600b528cefcSMark Murray p = strchr(buf, '\n');
601b528cefcSMark Murray if(p)
602b528cefcSMark Murray *p = '\0';
603b528cefcSMark Murray if(strcmp(buf, "") == 0)
6044137ff4cSJacques Vidrine strlcpy(buf, def, len);
6054137ff4cSJacques Vidrine signal(SIGINT, osig);
6064137ff4cSJacques Vidrine return 0;
607b528cefcSMark Murray }
6085e9cd1aeSAssar Westerlund
6095e9cd1aeSAssar Westerlund /*
6105e9cd1aeSAssar Westerlund * return [0, 16) or -1
6115e9cd1aeSAssar Westerlund */
6125e9cd1aeSAssar Westerlund
6135e9cd1aeSAssar Westerlund static int
hex2n(char c)6145e9cd1aeSAssar Westerlund hex2n (char c)
6155e9cd1aeSAssar Westerlund {
6165e9cd1aeSAssar Westerlund static char hexdigits[] = "0123456789abcdef";
6175e9cd1aeSAssar Westerlund const char *p;
6185e9cd1aeSAssar Westerlund
619bbd80c28SJacques Vidrine p = strchr (hexdigits, tolower((unsigned char)c));
6205e9cd1aeSAssar Westerlund if (p == NULL)
6215e9cd1aeSAssar Westerlund return -1;
6225e9cd1aeSAssar Westerlund else
6235e9cd1aeSAssar Westerlund return p - hexdigits;
6245e9cd1aeSAssar Westerlund }
6255e9cd1aeSAssar Westerlund
6265e9cd1aeSAssar Westerlund /*
6275e9cd1aeSAssar Westerlund * convert a key in a readable format into a keyblock.
6285e9cd1aeSAssar Westerlund * return 0 iff succesful, otherwise `err' should point to an error message
6295e9cd1aeSAssar Westerlund */
6305e9cd1aeSAssar Westerlund
6315e9cd1aeSAssar Westerlund int
parse_des_key(const char * key_string,krb5_key_data * key_data,const char ** error)6325e9cd1aeSAssar Westerlund parse_des_key (const char *key_string, krb5_key_data *key_data,
633c19800e8SDoug Rabson const char **error)
6345e9cd1aeSAssar Westerlund {
6355e9cd1aeSAssar Westerlund const char *p = key_string;
6365e9cd1aeSAssar Westerlund unsigned char bits[8];
6375e9cd1aeSAssar Westerlund int i;
6385e9cd1aeSAssar Westerlund
6395e9cd1aeSAssar Westerlund if (strlen (key_string) != 16) {
640c19800e8SDoug Rabson *error = "bad length, should be 16 for DES key";
6415e9cd1aeSAssar Westerlund return 1;
6425e9cd1aeSAssar Westerlund }
6435e9cd1aeSAssar Westerlund for (i = 0; i < 8; ++i) {
6445e9cd1aeSAssar Westerlund int d1, d2;
6455e9cd1aeSAssar Westerlund
6465e9cd1aeSAssar Westerlund d1 = hex2n(p[2 * i]);
6475e9cd1aeSAssar Westerlund d2 = hex2n(p[2 * i + 1]);
6485e9cd1aeSAssar Westerlund if (d1 < 0 || d2 < 0) {
649c19800e8SDoug Rabson *error = "non-hex character";
6505e9cd1aeSAssar Westerlund return 1;
6515e9cd1aeSAssar Westerlund }
6525e9cd1aeSAssar Westerlund bits[i] = (d1 << 4) | d2;
6535e9cd1aeSAssar Westerlund }
6545e9cd1aeSAssar Westerlund for (i = 0; i < 3; ++i) {
6555e9cd1aeSAssar Westerlund key_data[i].key_data_ver = 2;
6565e9cd1aeSAssar Westerlund key_data[i].key_data_kvno = 0;
6575e9cd1aeSAssar Westerlund /* key */
6585e9cd1aeSAssar Westerlund key_data[i].key_data_type[0] = ETYPE_DES_CBC_CRC;
6595e9cd1aeSAssar Westerlund key_data[i].key_data_length[0] = 8;
6605e9cd1aeSAssar Westerlund key_data[i].key_data_contents[0] = malloc(8);
661c19800e8SDoug Rabson if (key_data[i].key_data_contents[0] == NULL) {
662c19800e8SDoug Rabson *error = "malloc";
663c19800e8SDoug Rabson return ENOMEM;
664c19800e8SDoug Rabson }
6655e9cd1aeSAssar Westerlund memcpy (key_data[i].key_data_contents[0], bits, 8);
6665e9cd1aeSAssar Westerlund /* salt */
6675e9cd1aeSAssar Westerlund key_data[i].key_data_type[1] = KRB5_PW_SALT;
6685e9cd1aeSAssar Westerlund key_data[i].key_data_length[1] = 0;
6695e9cd1aeSAssar Westerlund key_data[i].key_data_contents[1] = NULL;
6705e9cd1aeSAssar Westerlund }
6715e9cd1aeSAssar Westerlund key_data[0].key_data_type[0] = ETYPE_DES_CBC_MD5;
6725e9cd1aeSAssar Westerlund key_data[1].key_data_type[0] = ETYPE_DES_CBC_MD4;
6735e9cd1aeSAssar Westerlund return 0;
6745e9cd1aeSAssar Westerlund }
675