aboutsummaryrefslogtreecommitdiff
path: root/src/lib/kadm5/srv/svr_iters.c
blob: 9774d76ea9bc233beb00e44fcd7b0e71c1ca51dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
 *
 * $Header$
 */

#if !defined(lint) && !defined(__CODECENTER__)
static char *rcsid = "$Header$";
#endif

#include "autoconf.h"
#if defined(HAVE_COMPILE) && defined(HAVE_STEP)
#define SOLARIS_REGEXPS
#elif defined(HAVE_REGCOMP) && defined(HAVE_REGEXEC)
#define POSIX_REGEXPS
#elif defined(HAVE_RE_COMP) && defined(HAVE_RE_EXEC)
#define BSD_REGEXPS
#else
#error I cannot find any regexp functions
#endif

#include	<sys/types.h>
#include	<string.h>
#include	<kadm5/admin.h>
#ifdef SOLARIS_REGEXPS
#include	<regexpr.h>
#endif
#ifdef POSIX_REGEXPS
#include	<regex.h>
#endif
#include <stdlib.h>

#include	"server_internal.h"

struct iter_data {
     krb5_context context;
     char **names;
     int n_names, sz_names;
     unsigned int malloc_failed;
     char *exp;
#ifdef SOLARIS_REGEXPS
     char *expbuf;
#endif
#ifdef POSIX_REGEXPS
     regex_t preg;
#endif
};

/*
 * Function: glob_to_regexp
 *
 * Arguments:
 *
 *	glob	(r) the shell-style glob (?*[]) to convert
 *	realm	(r) the default realm to append, or NULL
 *	regexp	(w) the ed-style regexp created from glob
 *
 * Effects:
 *
 * regexp is filled in with allocated memory contained a regular
 * expression to be used with re_comp/compile that matches what the
 * shell-style glob would match.  If glob does not contain an "@"
 * character and realm is not NULL, "@*" is appended to the regexp.
 *
 * Conversion algorithm:
 *
 *	quoted characters are copied quoted
 *	? is converted to .
 *	* is converted to .*
 * 	active characters are quoted: ^, $, .
 *	[ and ] are active but supported and have the same meaning, so
 *		they are copied
 *	other characters are copied
 *	regexp is anchored with ^ and $
 */
static kadm5_ret_t glob_to_regexp(char *glob, char *realm, char **regexp)
{
     int append_realm;
     char *p;

     /* validate the glob */
     if (glob[strlen(glob)-1] == '\\')
	  return EINVAL;

     /* A character of glob can turn into two in regexp, plus ^ and $ */
     /* and trailing null.  If glob has no @, also allocate space for */
     /* the realm. */
     append_realm = (realm != NULL) && (strchr(glob, '@') == NULL);
     p = (char *) malloc(strlen(glob)*2+ 3 + (append_realm ? 3 : 0));
     if (p == NULL)
	  return ENOMEM;
     *regexp = p;

     *p++ = '^';
     while (*glob) {
	  switch (*glob) {
	  case '?':
	       *p++ = '.';
	       break;
	  case '*':
	       *p++ = '.';
	       *p++ = '*';
	       break;
	  case '.':
	  case '^':
	  case '$':
	       *p++ = '\\';
	       *p++ = *glob;
	       break;
	  case '\\':
	       *p++ = '\\';
	       *p++ = *++glob;
	       break;
	  default:
	       *p++ = *glob;
	       break;
	  }
	  glob++;
     }

     if (append_realm) {
	  *p++ = '@';
	  *p++ = '.';
	  *p++ = '*';
     }

     *p++ = '$';
     *p++ = '\0';
     return KADM5_OK;
}

static void get_either_iter(struct iter_data *data, char *name)
{
     int match;
#ifdef SOLARIS_REGEXPS
     match = (step(name, data->expbuf) != 0);
#endif
#ifdef POSIX_REGEXPS
     match = (regexec(&data->preg, name, 0, NULL, 0) == 0);
#endif
#ifdef BSD_REGEXPS
     match = (re_exec(name) != 0);
#endif
     if (match) {
	  if (data->n_names == data->sz_names) {
	       int new_sz = data->sz_names * 2;
	       char **new_names = realloc(data->names,
					  new_sz * sizeof(char *));
	       if (new_names) {
		    data->names = new_names;
		    data->sz_names = new_sz;
	       } else {
		    data->malloc_failed = 1;
		    free(name);
		    return;
	       }
	  }
	  data->names[data->n_names++] = name;
     } else
	  free(name);
}

static void get_pols_iter(void *data, osa_policy_ent_t entry)
{
     char *name;

     if ((name = strdup(entry->name)) == NULL)
	  return;
     get_either_iter(data, name);
}

static void get_princs_iter(void *data, krb5_principal princ)
{
     struct iter_data *id = (struct iter_data *) data;
     char *name;
     
     if (krb5_unparse_name(id->context, princ, &name) != 0)
	  return;
     get_either_iter(data, name);
}

static kadm5_ret_t kadm5_get_either(int princ,
				       void *server_handle,
				       char *exp,
				       char ***princs,
				       int *count)
{
     struct iter_data data;
#ifdef BSD_REGEXPS
     char *msg;
#endif
     char *regexp;
     int i, ret;
     kadm5_server_handle_t handle = server_handle;
     
     *count = 0;
     if (exp == NULL)
	  exp = "*";

     CHECK_HANDLE(server_handle);

     if ((ret = glob_to_regexp(exp, princ ? handle->params.realm : NULL,
			       &regexp)) != KADM5_OK)
	  return ret;

     if (
#ifdef SOLARIS_REGEXPS
	 ((data.expbuf = compile(regexp, NULL, NULL)) == NULL)
#endif
#ifdef POSIX_REGEXPS
	 ((regcomp(&data.preg, regexp, REG_NOSUB)) != 0)
#endif
#ifdef BSD_REGEXPS
	 ((msg = (char *) re_comp(regexp)) != NULL)
#endif
	 )
     {
	  /* XXX syslog msg or regerr(regerrno) */
	  free(regexp);
	  return EINVAL;
     }

     data.n_names = 0;
     data.sz_names = 10;
     data.malloc_failed = 0;
     data.names = malloc(sizeof(char *) * data.sz_names);
     if (data.names == NULL) {
	  free(regexp);
	  return ENOMEM;
     }

     if (princ) {
	  data.context = handle->context;
	  ret = kdb_iter_entry(handle, exp, get_princs_iter, (void *) &data);
     } else {
	  ret = krb5_db_iter_policy(handle->context, exp, get_pols_iter, (void *)&data);
     }
     
     free(regexp);
#ifdef POSIX_REGEXPS
     regfree(&data.preg);
#endif
     if ( !ret && data.malloc_failed)
	  ret = ENOMEM;
     if ( ret ) {
	  for (i = 0; i < data.n_names; i++)
	       free(data.names[i]);
	  free(data.names);
	  return ret;
     }

     *princs = data.names;
     *count = data.n_names;
     return KADM5_OK;
}

kadm5_ret_t kadm5_get_principals(void *server_handle,
					   char *exp,
					   char ***princs,
					   int *count)
{
     return kadm5_get_either(1, server_handle, exp, princs, count);
}

kadm5_ret_t kadm5_get_policies(void *server_handle,
					   char *exp,
					   char ***pols,
					   int *count)
{
     return kadm5_get_either(0, server_handle, exp, pols, count);
}