2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <tr_gss_names.h>
42 * Helper for tr_gss_names_destructor - calls tr_free_name on its first argument
44 * @param item void pointer to a TR_NAME
45 * @param cookie ignored
47 static void gss_names_destruct_helper(void *item, void *cookie)
49 TR_NAME *name = (TR_NAME *) item;
52 static int tr_gss_names_destructor(void *obj)
54 TR_GSS_NAMES *gss_names=talloc_get_type_abort(obj, TR_GSS_NAMES);
56 tr_list_foreach(gss_names->names, gss_names_destruct_helper, NULL);
59 TR_GSS_NAMES *tr_gss_names_new(TALLOC_CTX *mem_ctx)
61 TR_GSS_NAMES *gn = talloc(mem_ctx, TR_GSS_NAMES);
63 gn->names = tr_list_new(gn);
64 if (gn->names == NULL) {
68 talloc_set_destructor((void *)gn, tr_gss_names_destructor);
73 void tr_gss_names_free(TR_GSS_NAMES *gn)
78 /* returns 0 on success */
79 int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new)
81 return (NULL == tr_list_add(gn->names, new, 0)); /* nonzero if the add failed */
85 * Create a duplicate GSS names struct
91 TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig)
93 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
94 TR_GSS_NAMES *new = tr_gss_names_new(tmp_ctx);
95 TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(tmp_ctx);
98 if ( !orig || !new || !iter ) {
102 for (this = tr_gss_names_iter_first(iter, orig);
104 this = tr_gss_names_iter_next(iter)) {
105 if (tr_gss_names_add(new, tr_dup_name(this)) != 0) {
106 talloc_free(tmp_ctx);
111 talloc_steal(mem_ctx, new);
115 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name)
117 TR_GSS_NAMES_ITER iter={0};
118 TR_NAME *this = NULL;
120 if ((!gn) || (!name))
123 for (this = tr_gss_names_iter_first(&iter, gn);
125 this = tr_gss_names_iter_next(&iter)) {
126 if (tr_name_cmp(name, this) == 0)
132 json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names)
134 TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(NULL);
135 json_t *jarray = json_array();
136 TR_NAME *name = tr_gss_names_iter_first(iter, gss_names);
138 json_array_append_new(jarray, tr_name_to_json_string(name));
139 name = tr_gss_names_iter_next(iter);
141 tr_gss_names_iter_free(iter);