416ba8cba51515611687ecd14dd74332e97a4865
[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 /**
42  * Helper for tr_gss_names_destructor - calls tr_free_name on its first argument
43  *
44  * @param item void pointer to a TR_NAME
45  * @param cookie ignored
46  */
47 static void gss_names_destruct_helper(void *item, void *cookie)
48 {
49   TR_NAME *name = (TR_NAME *) item;
50   tr_free_name(name);
51 }
52 static int tr_gss_names_destructor(void *obj)
53 {
54   TR_GSS_NAMES *gss_names=talloc_get_type_abort(obj, TR_GSS_NAMES);
55   if (gss_names->names)
56     tr_list_foreach(gss_names->names, gss_names_destruct_helper, NULL);
57   return 0;
58 }
59 TR_GSS_NAMES *tr_gss_names_new(TALLOC_CTX *mem_ctx)
60 {
61   TR_GSS_NAMES *gn = talloc(mem_ctx, TR_GSS_NAMES);
62   if (gn != NULL) {
63     gn->names = tr_list_new(gn);
64     if (gn->names == NULL) {
65       talloc_free(gn);
66       return NULL;
67     }
68     talloc_set_destructor((void *)gn, tr_gss_names_destructor);
69   }
70   return gn;
71 }
72
73 void tr_gss_names_free(TR_GSS_NAMES *gn)
74 {
75   talloc_free(gn);
76 }
77
78 /* returns 0 on success */
79 int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new)
80 {
81   return (NULL == tr_list_add(gn->names, new, 0)); /* nonzero if the add failed */
82 }
83
84 /**
85  * Create a duplicate GSS names struct
86  *
87  * @param mem_ctx
88  * @param orig
89  * @return
90  */
91 TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig)
92 {
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);
96   TR_NAME *this = NULL;
97
98   if ( !orig || !new || !iter ) {
99     talloc_free(tmp_ctx);
100     return NULL;
101   }
102   for (this = tr_gss_names_iter_first(iter, orig);
103        this != NULL;
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);
107       return NULL;
108     }
109   }
110   /* success */
111   talloc_steal(mem_ctx, new);
112   return new;
113 }
114
115 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name)
116 {
117   TR_GSS_NAMES_ITER iter={0};
118   TR_NAME *this = NULL;
119
120   if ((!gn) || (!name))
121     return 0;
122
123   for (this = tr_gss_names_iter_first(&iter, gn);
124       this != NULL;
125       this = tr_gss_names_iter_next(&iter)) {
126     if (tr_name_cmp(name, this) == 0)
127       return 1;
128   }
129   return 0;
130 }
131
132 json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names)
133 {
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);
137   while (name) {
138     json_array_append_new(jarray, tr_name_to_json_string(name));
139     name = tr_gss_names_iter_next(iter);
140   }
141   tr_gss_names_iter_free(iter);
142   return jarray;
143 }
144