Add accidentally omitted 'port' parameter to error messages
[trust_router.git] / common / tr_config_orgs.c
1 /*
2  * Copyright (c) 2012-2018, 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 <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39 #include <talloc.h>
40
41 #include <tr_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
54 #endif
55
56 /* takes a talloc context, but currently does not use it */
57 static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
58 {
59   TR_NAME *name=NULL;
60
61   if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
62     tr_debug("tr_cfg_parse_org_name: Bad parameters.");
63     if (rc!=NULL)
64       *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
65     return NULL;
66   }
67
68   name=tr_new_name(json_string_value(j_org));
69   if (name==NULL)
70     *rc=TR_CFG_NOMEM;
71   else
72     *rc=TR_CFG_SUCCESS;
73   return name;
74 }
75
76 static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
77 {
78   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
79   TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
80   TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
81   TR_NAME *org_name=NULL;
82   json_t *j_org=NULL;
83   json_t *j_realms=NULL;
84   TR_IDP_REALM *new_idp_realms=NULL;
85   TR_RP_CLIENT *new_rp_clients=NULL;
86
87   tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
88
89   /* get organization_name (optional) */
90   if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
91     tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
92   } else {
93     org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
94     if (rc==TR_CFG_SUCCESS) {
95       tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
96                org_name->len,
97                org_name->buf);
98       /* we don't actually do anything with this, but we could */
99       tr_free_name(org_name);
100       org_name=NULL;
101     }
102   }
103
104   /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
105    * it's harmless. Report a warning because that might be unintentional. */
106   if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
107     tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
108   } else {
109     /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
110     new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
111     if (rc!=TR_CFG_SUCCESS)
112       goto cleanup;
113
114     new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
115     if (rc!=TR_CFG_SUCCESS)
116       goto cleanup;
117   }
118   retval=TR_CFG_SUCCESS;
119
120 cleanup:
121   /* if we succeeded, link things to the configuration and move out of tmp context */
122   if (retval==TR_CFG_SUCCESS) {
123     if (new_idp_realms!=NULL) {
124       tr_idp_realm_add(trc->ctable->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
125       talloc_steal(trc, trc->ctable->idp_realms); /* make sure the head is in the right context */
126     }
127
128     if (new_rp_clients!=NULL) {
129       tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
130       talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */
131     }
132   }
133
134   talloc_free(tmp_ctx);
135   return rc;
136 }
137
138 /* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
139 TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
140 {
141   json_t *jlocorgs=NULL;
142   size_t ii=0;
143
144   jlocorgs=json_object_get(jcfg, "local_organizations");
145   if (jlocorgs==NULL)
146     return TR_CFG_SUCCESS;
147
148   if (!json_is_array(jlocorgs)) {
149     tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
150     return TR_CFG_NOPARSE;
151   }
152
153   for (ii=0; ii<json_array_size(jlocorgs); ii++) {
154     if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
155       tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
156       return TR_CFG_NOPARSE;
157     }
158   }
159
160   return TR_CFG_SUCCESS;
161 }
162
163 static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
164 {
165   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
166   json_t *jhost=NULL;
167   json_t *jport=NULL;
168   json_t *jgss=NULL;
169   json_t *jfilt=NULL;
170   TRP_PEER *new_peer=NULL;
171   TR_GSS_NAMES *names=NULL;
172   TR_FILTER_SET *filt_set=NULL;
173   TR_CFG_RC rc=TR_CFG_ERROR;
174
175   jhost=json_object_get(jporg, "hostname");
176   jport=json_object_get(jporg, "port");
177   jgss=json_object_get(jporg, "gss_names");
178   jfilt=json_object_get(jporg, "filters");
179
180   if ((jhost==NULL) || (!json_is_string(jhost))) {
181     tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
182     rc=TR_CFG_NOPARSE;
183     goto cleanup;
184   }
185
186   if ((jport!=NULL) && (!json_is_number(jport))) {
187     /* note that not specifying the port is allowed, but if set it must be a number */
188     tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
189     rc=TR_CFG_NOPARSE;
190     goto cleanup;
191   }
192
193   if ((jgss==NULL) || (!json_is_array(jgss))) {
194     tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
195     rc=TR_CFG_NOPARSE;
196     goto cleanup;
197   }
198
199   if ((jfilt!=NULL) && (!json_is_object(jfilt))) {
200     tr_err("tr_cfg_parse_one_peer_org: filters is not an object.");
201     rc=TR_CFG_NOPARSE;
202     goto cleanup;
203   }
204
205   new_peer=trp_peer_new(tmp_ctx);
206   if (new_peer==NULL) {
207     tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
208     rc=TR_CFG_NOMEM;
209     goto cleanup;
210   }
211
212   trp_peer_set_server(new_peer, json_string_value(jhost)); /* string is strdup'ed in _set_server() */
213   if (jport==NULL)
214     trp_peer_set_port(new_peer, TRP_PORT);
215   else
216     trp_peer_set_port(new_peer, json_integer_value(jport));
217
218   rc = tr_cfg_parse_gss_names(tmp_ctx, jgss, &names);
219   if (rc!=TR_CFG_SUCCESS) {
220     tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
221     rc=TR_CFG_NOPARSE;
222     goto cleanup;
223   }
224   trp_peer_set_gss_names(new_peer, names);
225
226   if (jfilt) {
227     filt_set=tr_cfg_parse_filters(tmp_ctx, jfilt, &rc);
228     if (rc!=TR_CFG_SUCCESS) {
229       tr_err("tr_cfg_parse_one_peer_org: unable to parse filters.");
230       rc=TR_CFG_NOPARSE;
231       goto cleanup;
232     }
233     trp_peer_set_filters(new_peer, filt_set);
234   }
235
236   /* success! */
237   trp_ptable_add(trc->peers, new_peer);
238   rc=TR_CFG_SUCCESS;
239
240 cleanup:
241   talloc_free(tmp_ctx);
242   return rc;
243 }
244
245 /* Parse peer organizations, if present. Returns success if there are none. */
246 TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
247 {
248   json_t *jpeerorgs=NULL;
249   int ii=0;
250
251   jpeerorgs=json_object_get(jcfg, "peer_organizations");
252   if (jpeerorgs==NULL)
253     return TR_CFG_SUCCESS;
254
255   if (!json_is_array(jpeerorgs)) {
256     tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
257     return TR_CFG_NOPARSE;
258   }
259
260   for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
261     if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
262       tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
263       return TR_CFG_NOPARSE;
264     }
265   }
266
267   return TR_CFG_SUCCESS;
268 }
269