Path: blob/main/crypto/heimdal/lib/kadm5/get_princs_s.c
34889 views
/*1* Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "kadm5_locl.h"3435RCSID("$Id$");3637struct foreach_data {38const char *exp;39char *exp2;40char **princs;41int count;42};4344static krb5_error_code45add_princ(struct foreach_data *d, char *princ)46{47char **tmp;48tmp = realloc(d->princs, (d->count + 1) * sizeof(*tmp));49if(tmp == NULL)50return ENOMEM;51d->princs = tmp;52d->princs[d->count++] = princ;53return 0;54}5556static krb5_error_code57foreach(krb5_context context, HDB *db, hdb_entry_ex *ent, void *data)58{59struct foreach_data *d = data;60char *princ;61krb5_error_code ret;62ret = krb5_unparse_name(context, ent->entry.principal, &princ);63if(ret)64return ret;65if(d->exp){66if(fnmatch(d->exp, princ, 0) == 0 || fnmatch(d->exp2, princ, 0) == 0)67ret = add_princ(d, princ);68else69free(princ);70}else{71ret = add_princ(d, princ);72}73if(ret)74free(princ);75return ret;76}7778kadm5_ret_t79kadm5_s_get_principals(void *server_handle,80const char *expression,81char ***princs,82int *count)83{84struct foreach_data d;85kadm5_server_context *context = server_handle;86kadm5_ret_t ret;87ret = context->db->hdb_open(context->context, context->db, O_RDWR, 0);88if(ret) {89krb5_warn(context->context, ret, "opening database");90return ret;91}92d.exp = expression;93{94krb5_realm r;95if ((ret = krb5_get_default_realm(context->context, &r)))96return (ret);97asprintf(&d.exp2, "%s@%s", expression, r);98free(r);99}100d.princs = NULL;101d.count = 0;102ret = hdb_foreach(context->context, context->db, HDB_F_ADMIN_DATA, foreach, &d);103context->db->hdb_close(context->context, context->db);104if(ret == 0)105ret = add_princ(&d, NULL);106if(ret == 0){107*princs = d.princs;108*count = d.count - 1;109}else110kadm5_free_name_list(context, d.princs, &d.count);111free(d.exp2);112return _kadm5_error_code(ret);113}114115116