Change most while loops over TR_LISTs to for loops
[trust_router.git] / common / tr_gss_names.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <talloc.h>
36 #include <glib.h>
37
38 #include <tr_gss_names.h>
39 #include <tr_debug.h>
40
41 static int tr_gss_names_destructor(void *obj)
42 {
43   TR_GSS_NAMES *gss_names=talloc_get_type_abort(obj, TR_GSS_NAMES);
44   if (gss_names->names)
45     g_ptr_array_unref(gss_names->names);
46   return 0;
47 }
48 TR_GSS_NAMES *tr_gss_names_new(TALLOC_CTX *mem_ctx)
49 {
50   TR_GSS_NAMES *gn=talloc(mem_ctx, TR_GSS_NAMES);
51
52   if (gn != NULL) {
53     gn->names = g_ptr_array_new_with_free_func((GDestroyNotify) tr_free_name);
54     if (gn->names == NULL) {
55       talloc_free(gn);
56       return NULL;
57     }
58     talloc_set_destructor((void *)gn, tr_gss_names_destructor);
59   }
60   return gn;
61 }
62
63 void tr_gss_names_free(TR_GSS_NAMES *gn)
64 {
65   talloc_free(gn);
66 }
67
68 /* returns 0 on success */
69 int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new)
70 {
71   guint old_len = gn->names->len;
72   g_ptr_array_add(gn->names, new);
73   return (gn->names->len == old_len); /* nonzero if the add failed */
74 }
75
76 /**
77  * Create a duplicate GSS names struct
78  *
79  * @param mem_ctx
80  * @param orig
81  * @return
82  */
83 TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig)
84 {
85   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
86   TR_GSS_NAMES *new = tr_gss_names_new(tmp_ctx);
87   TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(tmp_ctx);
88   TR_NAME *this = NULL;
89
90   if ( !orig || !new || !iter ) {
91     talloc_free(tmp_ctx);
92     return NULL;
93   }
94   for (this = tr_gss_names_iter_first(iter, orig);
95        this != NULL;
96        this = tr_gss_names_iter_next(iter)) {
97     if (tr_gss_names_add(new, tr_dup_name(this)) != 0) {
98       talloc_free(tmp_ctx);
99       return NULL;
100     }
101   }
102   /* success */
103   talloc_steal(mem_ctx, new);
104   return new;
105 }
106
107 static gboolean names_equal_helper(gconstpointer a, gconstpointer b)
108 {
109   return (tr_name_cmp(a, b) == 0);
110 }
111
112 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name)
113 {
114   if (!gn)
115     return 0;
116
117   return(TRUE == g_ptr_array_find_with_equal_func(gn->names,
118                                                   name,
119                                                   names_equal_helper,
120                                                   NULL));
121 }
122
123 /* iterators */
124 TR_GSS_NAMES_ITER *tr_gss_names_iter_new(TALLOC_CTX *mem_ctx)
125 {
126   TR_GSS_NAMES_ITER *iter=talloc(mem_ctx, TR_GSS_NAMES_ITER);
127   if (iter!=NULL) {
128     iter->gn=NULL;
129     iter->ii=0;
130   }
131   return iter;
132 }
133
134 TR_NAME *tr_gss_names_iter_first(TR_GSS_NAMES_ITER *iter, TR_GSS_NAMES *gn)
135 {
136   iter->gn=gn;
137   iter->ii=0;
138   return tr_gss_names_iter_next(iter);
139 }
140
141 TR_NAME *tr_gss_names_iter_next(TR_GSS_NAMES_ITER *iter)
142 {
143   if (iter->ii < iter->gn->names->len)
144     return g_ptr_array_index(iter->gn->names, iter->ii++);
145   return NULL;
146 }
147
148 void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter)
149 {
150   talloc_free(iter);
151 }
152
153 json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names)
154 {
155   TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(NULL);
156   json_t *jarray = json_array();
157   TR_NAME *name = tr_gss_names_iter_first(iter, gss_names);
158   while (name) {
159     json_array_append_new(jarray, tr_name_to_json_string(name));
160     name = tr_gss_names_iter_next(iter);
161   }
162   tr_gss_names_iter_free(iter);
163   return jarray;
164 }
165