xref: /freebsd/crypto/heimdal/lib/kadm5/get_princs_s.c (revision 879b0e9cd2ada8f815e208d31feb4b7c05ba7e4a)
1b528cefcSMark Murray /*
2ae771770SStanislav Sedov  * Copyright (c) 1997, 1998, 1999 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 "kadm5_locl.h"
35b528cefcSMark Murray 
36ae771770SStanislav Sedov RCSID("$Id$");
37b528cefcSMark Murray 
38b528cefcSMark Murray struct foreach_data {
39b528cefcSMark Murray     const char *exp;
40b528cefcSMark Murray     char *exp2;
41b528cefcSMark Murray     char **princs;
42b528cefcSMark Murray     int count;
43b528cefcSMark Murray };
44b528cefcSMark Murray 
45b528cefcSMark Murray static krb5_error_code
add_princ(struct foreach_data * d,char * princ)46b528cefcSMark Murray add_princ(struct foreach_data *d, char *princ)
47b528cefcSMark Murray {
48b528cefcSMark Murray     char **tmp;
49b528cefcSMark Murray     tmp = realloc(d->princs, (d->count + 1) * sizeof(*tmp));
50b528cefcSMark Murray     if(tmp == NULL)
51b528cefcSMark Murray 	return ENOMEM;
52b528cefcSMark Murray     d->princs = tmp;
53b528cefcSMark Murray     d->princs[d->count++] = princ;
54b528cefcSMark Murray     return 0;
55b528cefcSMark Murray }
56b528cefcSMark Murray 
57b528cefcSMark Murray static krb5_error_code
foreach(krb5_context context,HDB * db,hdb_entry_ex * ent,void * data)58c19800e8SDoug Rabson foreach(krb5_context context, HDB *db, hdb_entry_ex *ent, void *data)
59b528cefcSMark Murray {
60b528cefcSMark Murray     struct foreach_data *d = data;
61b528cefcSMark Murray     char *princ;
62b528cefcSMark Murray     krb5_error_code ret;
63c19800e8SDoug Rabson     ret = krb5_unparse_name(context, ent->entry.principal, &princ);
64b528cefcSMark Murray     if(ret)
65b528cefcSMark Murray 	return ret;
66b528cefcSMark Murray     if(d->exp){
67b528cefcSMark Murray 	if(fnmatch(d->exp, princ, 0) == 0 || fnmatch(d->exp2, princ, 0) == 0)
68b528cefcSMark Murray 	    ret = add_princ(d, princ);
69b528cefcSMark Murray 	else
70b528cefcSMark Murray 	    free(princ);
71b528cefcSMark Murray     }else{
72b528cefcSMark Murray 	ret = add_princ(d, princ);
73b528cefcSMark Murray     }
74b528cefcSMark Murray     if(ret)
75b528cefcSMark Murray 	free(princ);
76b528cefcSMark Murray     return ret;
77b528cefcSMark Murray }
78b528cefcSMark Murray 
79b528cefcSMark Murray kadm5_ret_t
kadm5_s_get_principals(void * server_handle,const char * expression,char *** princs,int * count)80b528cefcSMark Murray kadm5_s_get_principals(void *server_handle,
81c19800e8SDoug Rabson 		       const char *expression,
82b528cefcSMark Murray 		       char ***princs,
83b528cefcSMark Murray 		       int *count)
84b528cefcSMark Murray {
85b528cefcSMark Murray     struct foreach_data d;
86b528cefcSMark Murray     kadm5_server_context *context = server_handle;
87b528cefcSMark Murray     kadm5_ret_t ret;
88c19800e8SDoug Rabson     ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);
89b528cefcSMark Murray     if(ret) {
90b528cefcSMark Murray 	krb5_warn(context->context, ret, "opening database");
91b528cefcSMark Murray 	return ret;
92b528cefcSMark Murray     }
93c19800e8SDoug Rabson     d.exp = expression;
94b528cefcSMark Murray     {
95b528cefcSMark Murray 	krb5_realm r;
96*879b0e9cSCy Schubert 	if ((ret = krb5_get_default_realm(context->context, &r)))
97*879b0e9cSCy Schubert 	    return (ret);
98c19800e8SDoug Rabson 	asprintf(&d.exp2, "%s@%s", expression, r);
99b528cefcSMark Murray 	free(r);
100b528cefcSMark Murray     }
101b528cefcSMark Murray     d.princs = NULL;
102b528cefcSMark Murray     d.count = 0;
103ae771770SStanislav Sedov     ret = hdb_foreach(context->context, context->db, HDB_F_ADMIN_DATA, foreach, &d);
104c19800e8SDoug Rabson     context->db->hdb_close(context->context, context->db);
105b528cefcSMark Murray     if(ret == 0)
106b528cefcSMark Murray 	ret = add_princ(&d, NULL);
107b528cefcSMark Murray     if(ret == 0){
108b528cefcSMark Murray 	*princs = d.princs;
109b528cefcSMark Murray 	*count = d.count - 1;
110b528cefcSMark Murray     }else
111b528cefcSMark Murray 	kadm5_free_name_list(context, d.princs, &d.count);
112b528cefcSMark Murray     free(d.exp2);
113b528cefcSMark Murray     return _kadm5_error_code(ret);
114b528cefcSMark Murray }
115