summaryrefslogtreecommitdiff
path: root/lib/confutil.c
blob: 7bd4a374c3c6e99a2a49db320513b6144b06a8c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* Copyright 2013 NORDUnet A/S. All rights reserved.
   See LICENSE for licensing information.  */

#if defined HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/types.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <radsec/radsec.h>
#include <radsec/radsec-impl.h>

#if 0
/* This code triggers the memory-stomping-detector of electric fence */

#define MEMCHUNK 30

static int
print_to_buf (const struct rs_context *ctx,
              char **buf_ptr, ssize_t *i_ptr, ssize_t *len_ptr,
              const char *fmt, ...)
{
  char *buf = *buf_ptr;
  ssize_t i = *i_ptr;
  ssize_t len = *len_ptr;

  va_list args;
  for (;;)
    {
      int n;
      va_start (args, fmt);
      fprintf (stdout, "sprintf (%p + %ld, %ld, \"%s\") -->",
               buf, i, len, buf);
      fflush (stdout);
      n = vsnprintf (buf + i, len - i, fmt, args);
      fprintf (stdout, "%d\n", n);
      va_end (args);
      if (n < 0)
        return -RSE_INTERNAL;
      if (n >= len - i)
        {
          int newlen = len + MEMCHUNK;
          buf = rs_realloc (ctx, buf, newlen);
          if (buf == NULL)
            return -RSE_NOMEM;
          len = newlen;
          continue;
        }
      len -= n;
      i += n;

      *buf_ptr = buf;
      *i_ptr = i;
      *len_ptr = len;
      return RSE_OK;
    }
}
#endif  /* 0 */

static int
pp (char **out, size_t *len, const char *fmt, ...)
{
  int n;
  va_list args;
  va_start (args, fmt);
  n = vsnprintf (*out, *len, fmt, args);
  va_end (args);
  if (n == -1 || n >= *len)
    return -RSE_INTERNAL;
  *out += n;
  *len -= n;
  return RSE_OK;
}

int
rs_context_print_config (struct rs_context *ctx, char **buf_out)
{
  char *buf = rs_malloc (ctx, 8192);
  char *out = NULL;
  size_t len = 8192;
  struct rs_config *cfg = ctx->config;
  struct rs_realm *r = NULL;
  struct rs_peer *p = NULL;
  char *peer_type[] = {"<no type>", "client", "server"};
  char *realm_type[] = {"<no type>", "UDP", "TCP", "TLS", "DTLS"};
  char *cred_type[] = {"<no type>", "PSK", "DHE_PSK", "RSA_PSK"};

  out = buf;
  assert (out);
  assert (cfg);

  for (r = cfg->realms; r != NULL; r = r->next)
    {
      if (pp (&out, &len, "realm %s {\n", r->name)
          || pp (&out, &len,
                 "\ttype = \"%s\"\n"
                 "\ttimeout = %d\n"
                 "\tretries = %d\n"
                 "\tlisten_addr = \"%s\"\n"
                 "\tlisten_service = \"%s\"\n",
                 realm_type[r->type],
                 r->timeout,
                 r->retries,
                 r->local_addr->hostname,
                 r->local_addr->service))
        return -RSE_INTERNAL;
      for (p = r->peers; p != NULL; p = p->next)
        {
          if (pp (&out, &len,
                  "\t%s {\n"
                  "\t\thostname = \"%s\"\n"
                  "\t\tservice = \"%s\"\n"
                  "\t\tsecret = \"%s\"\n",
                  peer_type[p->type],
                  p->hostname,
                  p->service,
                  p->secret))
            return -RSE_INTERNAL;
          if (p->cacertfile)
            if (pp (&out, &len, "\t\tcacertfile = \"%s\"\n", p->cacertfile))
              return -RSE_INTERNAL;
          if (p->certfile)
            if (pp (&out, &len, "\t\tcertfile = \"%s\"\n", p->certfile))
              return -RSE_INTERNAL;
          if (p->certkeyfile)
            if (pp (&out, &len, "\t\tcertkeyfile = \"%s\"\n", p->certkeyfile))
              return -RSE_INTERNAL;
          if (p->transport_cred)
            {
              if (pp (&out, &len, "\t\tpskex = \"%s\"\n",
                      cred_type[p->transport_cred->type])
                  || pp (&out, &len, "\t\tpskid = \"%s\"\n",
                         p->transport_cred->identity)
                  || pp (&out, &len,
                         "\t\t%s = \"%s\"\n", (p->transport_cred->secret_encoding
                                               == RS_KEY_ENCODING_ASCII_HEX
                                               ? "pskhexstr" : "pskstr"),
                         p->transport_cred->secret))
                return -RSE_INTERNAL;
            }
          if (pp (&out, &len, "\t}\n"))
            return -RSE_INTERNAL;
        }
      if (pp (&out, &len, "}\n"))
        return -RSE_INTERNAL;
    }

  if (buf_out)
    *buf_out = buf;
  return RSE_OK;
}