Improve structure of realm listings in 'show communities' response
[trust_router.git] / common / tr_idp_encoders.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 <talloc.h>
36 #include <time.h>
37
38 #include <tr_name_internal.h>
39 #include <tr_idp.h>
40 #include <tr_config.h>
41 #include <tr_debug.h>
42
43 static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa)
44 {
45   return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len);
46 }
47
48
49 char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp)
50 {
51   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
52   char **s_aaa=NULL, *aaa_servers=NULL;
53   char **s_apc=NULL, *apcs=NULL;
54   int ii=0, aaa_servers_strlen=0, apcs_strlen=0;
55   int n_aaa_servers=tr_idp_realm_aaa_server_count(idp);
56   int n_apcs=tr_idp_realm_apc_count(idp);
57   TR_AAA_SERVER *aaa=NULL;
58   TR_APC *apc=NULL;
59   char *result=NULL;
60
61   /* get the AAA servers */
62   if (n_aaa_servers<=0)
63     aaa_servers=talloc_strdup(tmp_ctx, "");
64   else {
65     s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers);
66     for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) {
67       s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa);
68       aaa_servers_strlen+=strlen(s_aaa[ii]);
69     }
70
71     /* add space for comma-space separators */
72     aaa_servers_strlen+=2*(n_aaa_servers-1);
73
74     aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1);
75     aaa_servers[0]='\0';
76     for (ii=0; ii<n_aaa_servers; ii++) {
77       strcat(aaa_servers, s_aaa[ii]);
78       if (ii<(n_aaa_servers-1))
79         strcat(aaa_servers, ", ");
80     }
81     talloc_free(s_aaa);
82   }
83
84   /* get the APCs */
85   if (n_apcs<=0)
86     apcs=talloc_strdup(tmp_ctx, "");
87   else {
88     s_apc=talloc_array(tmp_ctx, char *, n_apcs);
89     for (apc=idp->apcs,ii=0; apc!=NULL; apc=apc->next,ii++) {
90       s_apc[ii]=tr_apc_to_str(s_apc, apc);
91       apcs_strlen+=strlen(s_apc[ii]);
92     }
93
94     /* add space for comma-space separators */
95     apcs_strlen+=2*(n_apcs-1);
96
97     apcs=talloc_array(tmp_ctx, char, apcs_strlen+1);
98     apcs[0]='\0';
99     for (ii=0; ii<n_apcs; ii++) {
100       strcat(apcs, s_apc[ii]);
101       if (ii<(n_apcs-1))
102         strcat(apcs, ", ");
103     }
104     talloc_free(s_apc);
105   }
106
107   result=talloc_asprintf(mem_ctx,
108                          "IDP realm: \"%.*s\""
109                          "  shared: %s"
110                          "  local: %s"
111                          "  AAA servers: %s"
112                          "  APCs: %s",
113                          idp->realm_id->len, idp->realm_id->buf,
114                          (idp->shared_config)?"yes":"no",
115                          (idp->origin==TR_REALM_LOCAL)?"yes":"no",
116                          aaa_servers,
117                          apcs);
118   talloc_free(tmp_ctx);
119   return result;
120 }
121