Log incoming IP address when accepting a connection
[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
37 #include <tr_gss_names.h>
38 #include <tr_debug.h>
39
40 static int tr_gss_names_destructor(void *obj)
41 {
42   TR_GSS_NAMES *gss_names=talloc_get_type_abort(obj, TR_GSS_NAMES);
43   int ii=0;
44
45   for (ii=0; ii<TR_MAX_GSS_NAMES; ii++) {
46     if (gss_names->names[ii]!=NULL)
47       tr_free_name(gss_names->names[ii]);
48   }
49   return 0;
50 }
51 TR_GSS_NAMES *tr_gss_names_new(TALLOC_CTX *mem_ctx)
52 {
53   TR_GSS_NAMES *gn=talloc(mem_ctx, TR_GSS_NAMES);
54   int ii=0;
55
56   if (gn!=NULL) {
57     for (ii=0; ii<TR_MAX_GSS_NAMES; ii++)
58       gn->names[ii]=NULL;
59     talloc_set_destructor((void *)gn, tr_gss_names_destructor);
60   }
61   return gn;
62 }
63
64 void tr_gss_names_free(TR_GSS_NAMES *gn)
65 {
66   talloc_free(gn);
67 }
68
69 /* returns 0 on success */
70 int tr_gss_names_add(TR_GSS_NAMES *gn, TR_NAME *new)
71 {
72   int ii=0;
73
74   for (ii=0; ii<TR_MAX_GSS_NAMES; ii++) {
75     if (gn->names[ii]==NULL)
76       break;
77   }
78   if (ii!=TR_MAX_GSS_NAMES) {
79     gn->names[ii]=new;
80     return 0;
81   } else
82     return -1;
83 }
84
85 /**
86  * Create a duplicate GSS names struct
87  *
88  * @param mem_ctx
89  * @param orig
90  * @return
91  */
92 TR_GSS_NAMES *tr_gss_names_dup(TALLOC_CTX *mem_ctx, TR_GSS_NAMES *orig)
93 {
94   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
95   TR_GSS_NAMES *new = tr_gss_names_new(tmp_ctx);
96   TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(tmp_ctx);
97   TR_NAME *this = NULL;
98
99   if ( !orig || !new || !iter ) {
100     talloc_free(tmp_ctx);
101     return NULL;
102   }
103   this = tr_gss_names_iter_first(iter, orig);
104   while (this) {
105     if (tr_gss_names_add(new, tr_dup_name(this)) != 0) {
106       talloc_free(tmp_ctx);
107       return NULL;
108     }
109     this = tr_gss_names_iter_next(iter);
110   }
111   /* success */
112   talloc_steal(mem_ctx, new);
113   return new;
114 }
115 int tr_gss_names_matches(TR_GSS_NAMES *gn, TR_NAME *name)
116 {
117   int ii=0;
118
119   if (!gn)
120     return 0;
121
122   for (ii=0; ii<TR_MAX_GSS_NAMES; ii++) {
123     if ((gn->names[ii]!=NULL) &&
124         (0==tr_name_cmp(gn->names[ii], name)))
125       return 1;
126   }
127   return 0;
128 }
129
130 /* iterators */
131 TR_GSS_NAMES_ITER *tr_gss_names_iter_new(TALLOC_CTX *mem_ctx)
132 {
133   TR_GSS_NAMES_ITER *iter=talloc(mem_ctx, TR_GSS_NAMES_ITER);
134   if (iter!=NULL) {
135     iter->gn=NULL;
136     iter->ii=0;
137   }
138   return iter;
139 }
140
141 TR_NAME *tr_gss_names_iter_first(TR_GSS_NAMES_ITER *iter, TR_GSS_NAMES *gn)
142 {
143   iter->gn=gn;
144   iter->ii=-1;
145   return tr_gss_names_iter_next(iter);
146 }
147
148 TR_NAME *tr_gss_names_iter_next(TR_GSS_NAMES_ITER *iter)
149 {
150   for (iter->ii++;
151        (iter->ii < TR_MAX_GSS_NAMES) && (iter->gn->names[iter->ii]==NULL);
152        iter->ii++) { }
153
154   if (iter->ii<TR_MAX_GSS_NAMES)
155     return iter->gn->names[iter->ii];
156   
157   return NULL;
158 }
159
160 void tr_gss_names_iter_free(TR_GSS_NAMES_ITER *iter)
161 {
162   talloc_free(iter);
163 }
164
165 json_t *tr_gss_names_to_json_array(TR_GSS_NAMES *gss_names)
166 {
167   TR_GSS_NAMES_ITER *iter = tr_gss_names_iter_new(NULL);
168   json_t *jarray = json_array();
169   TR_NAME *name = tr_gss_names_iter_first(iter, gss_names);
170   while (name) {
171     json_array_append_new(jarray, tr_name_to_json_string(name));
172     name = tr_gss_names_iter_next(iter);
173   }
174   tr_gss_names_iter_free(iter);
175   return jarray;
176 }
177