Alphabetise radsec.sym.
[libradsec.git] / lib / confutil.c
1 /* Copyright 2013 NORDUnet A/S. All rights reserved.
2    See LICENSE for licensing information.  */
3
4 #if defined HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <assert.h>
12 #include <radsec/radsec.h>
13 #include <radsec/radsec-impl.h>
14
15 #if 0
16 /* This code triggers the memory-stomping-detector of electric fence */
17
18 #define MEMCHUNK 30
19
20 static int
21 print_to_buf (const struct rs_context *ctx,
22               char **buf_ptr, ssize_t *i_ptr, ssize_t *len_ptr,
23               const char *fmt, ...)
24 {
25   char *buf = *buf_ptr;
26   ssize_t i = *i_ptr;
27   ssize_t len = *len_ptr;
28
29   va_list args;
30   for (;;)
31     {
32       int n;
33       va_start (args, fmt);
34       fprintf (stdout, "sprintf (%p + %ld, %ld, \"%s\") -->",
35                buf, i, len, buf);
36       fflush (stdout);
37       n = vsnprintf (buf + i, len - i, fmt, args);
38       fprintf (stdout, "%d\n", n);
39       va_end (args);
40       if (n < 0)
41         return -RSE_INTERNAL;
42       if (n >= len - i)
43         {
44           int newlen = len + MEMCHUNK;
45           buf = rs_realloc (ctx, buf, newlen);
46           if (buf == NULL)
47             return -RSE_NOMEM;
48           len = newlen;
49           continue;
50         }
51       len -= n;
52       i += n;
53
54       *buf_ptr = buf;
55       *i_ptr = i;
56       *len_ptr = len;
57       return RSE_OK;
58     }
59 }
60 #endif  /* 0 */
61
62 static int
63 pp (char **out, size_t *len, const char *fmt, ...)
64 {
65   int n;
66   va_list args;
67   va_start (args, fmt);
68   n = vsnprintf (*out, *len, fmt, args);
69   va_end (args);
70   if (n == -1 || n >= *len)
71     return -RSE_INTERNAL;
72   *out += n;
73   *len -= n;
74   return RSE_OK;
75 }
76
77 int
78 rs_context_print_config (struct rs_context *ctx, char **buf_out)
79 {
80   char *buf = rs_malloc (ctx, 8192);
81   char *out = NULL;
82   size_t len = 8192;
83   struct rs_config *cfg = ctx->config;
84   struct rs_realm *r = NULL;
85   struct rs_peer *p = NULL;
86   char *peer_type[] = {"<no type>", "client", "server"};
87   char *realm_type[] = {"<no type>", "UDP", "TCP", "TLS", "DTLS"};
88   char *cred_type[] = {"<no type>", "PSK", "DHE_PSK", "RSA_PSK"};
89
90   out = buf;
91   assert (out);
92   assert (cfg);
93
94   for (r = cfg->realms; r != NULL; r = r->next)
95     {
96       if (pp (&out, &len, "realm %s {\n", r->name)
97           || pp (&out, &len,
98                  "\ttype = \"%s\"\n"
99                  "\ttimeout = %d\n"
100                  "\tretries = %d\n"
101                  "\tlisten_addr = \"%s\"\n"
102                  "\tlisten_service = \"%s\"\n",
103                  realm_type[r->type],
104                  r->timeout,
105                  r->retries,
106                  r->local_addr->hostname,
107                  r->local_addr->service))
108         return -RSE_INTERNAL;
109       for (p = r->peers; p != NULL; p = p->next)
110         {
111           if (pp (&out, &len,
112                   "\t%s {\n"
113                   "\t\thostname = \"%s\"\n"
114                   "\t\tservice = \"%s\"\n"
115                   "\t\tsecret = \"%s\"\n",
116                   peer_type[p->type],
117                   p->hostname,
118                   p->service,
119                   p->secret))
120             return -RSE_INTERNAL;
121           if (p->cacertfile)
122             if (pp (&out, &len, "\t\tcacertfile = \"%s\"\n", p->cacertfile))
123               return -RSE_INTERNAL;
124           if (p->certfile)
125             if (pp (&out, &len, "\t\tcertfile = \"%s\"\n", p->certfile))
126               return -RSE_INTERNAL;
127           if (p->certkeyfile)
128             if (pp (&out, &len, "\t\tcertkeyfile = \"%s\"\n", p->certkeyfile))
129               return -RSE_INTERNAL;
130           if (p->transport_cred)
131             {
132               if (pp (&out, &len, "\t\tpskex = \"%s\"\n",
133                       cred_type[p->transport_cred->type])
134                   || pp (&out, &len, "\t\tpskid = \"%s\"\n",
135                          p->transport_cred->identity)
136                   || pp (&out, &len,
137                          "\t\t%s = \"%s\"\n", (p->transport_cred->secret_encoding
138                                                == RS_KEY_ENCODING_ASCII_HEX
139                                                ? "pskhexstr" : "pskstr"),
140                          p->transport_cred->secret))
141                 return -RSE_INTERNAL;
142             }
143           if (pp (&out, &len, "\t}\n"))
144             return -RSE_INTERNAL;
145         }
146       if (pp (&out, &len, "}\n"))
147         return -RSE_INTERNAL;
148     }
149
150   if (buf_out)
151     *buf_out = buf;
152   return RSE_OK;
153 }