+static RADCLIENT *client_parse(CONF_SECTION *cs, int in_server)
+{
+ RADCLIENT *c;
+ const char *name2;
+
+ name2 = cf_section_name2(cs);
+ if (!name2) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Missing client name");
+ return NULL;
+ }
+
+ /*
+ * The size is fine.. Let's create the buffer
+ */
+ c = rad_malloc(sizeof(*c));
+ memset(c, 0, sizeof(*c));
+ c->cs = cs;
+
+#ifdef WITH_STATS
+ c->auth = rad_malloc(sizeof(*c->auth));
+ memset(c->auth, 0, sizeof(*c->auth));
+
+#ifdef WITH_ACCOUNTING
+ c->acct = rad_malloc(sizeof(*c->acct));
+ memset(c->acct, 0, sizeof(*c->acct));
+#endif
+#endif
+
+ memset(&cl_ip4addr, 0, sizeof(cl_ip4addr));
+ memset(&cl_ip6addr, 0, sizeof(cl_ip6addr));
+ c->prefix = -1;
+
+ if (cf_section_parse(cs, c, client_config) < 0) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Error parsing client section.");
+ error:
+ client_free(c);
+#ifdef WITH_TCP
+ free(hs_proto);
+ hs_proto = NULL;
+#endif
+
+ return NULL;
+ }
+
+ /*
+ * Global clients can set servers to use,
+ * per-server clients cannot.
+ */
+ if (in_server && c->server) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Clients inside of an server section cannot point to a server.");
+ goto error;
+ }
+
+ /*
+ * No "ipaddr" or "ipv6addr", use old-style
+ * "client <ipaddr> {" syntax.
+ */
+ if (!cf_pair_find(cs, "ipaddr") &&
+ !cf_pair_find(cs, "ipv6addr")) {
+ char *prefix_ptr;
+
+ prefix_ptr = strchr(name2, '/');
+
+ /*
+ * Look for prefixes.
+ */
+ if (prefix_ptr) {
+ c->prefix = atoi(prefix_ptr + 1);
+ if ((c->prefix < 0) || (c->prefix > 128)) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Invalid Prefix value '%s' for IP.",
+ prefix_ptr + 1);
+ goto error;
+ }
+ /* Replace '/' with '\0' */
+ *prefix_ptr = '\0';
+ }
+
+ /*
+ * Always get the numeric representation of IP
+ */
+ if (ip_hton(name2, AF_UNSPEC, &c->ipaddr) < 0) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Failed to look up hostname %s: %s",
+ name2, fr_strerror());
+ goto error;
+ }
+
+ if (prefix_ptr) *prefix_ptr = '/';
+ c->longname = strdup(name2);
+
+ if (!c->shortname) c->shortname = strdup(c->longname);
+
+ } else {
+ char buffer[1024];
+
+ /*
+ * Figure out which one to use.
+ */
+ if (cf_pair_find(cs, "ipaddr")) {
+ c->ipaddr.af = AF_INET;
+ c->ipaddr.ipaddr.ip4addr = cl_ip4addr;
+
+ if ((c->prefix < -1) || (c->prefix > 32)) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Netmask must be between 0 and 32");
+ goto error;
+ }
+
+ } else if (cf_pair_find(cs, "ipv6addr")) {
+ c->ipaddr.af = AF_INET6;
+ c->ipaddr.ipaddr.ip6addr = cl_ip6addr;
+
+ if ((c->prefix < -1) || (c->prefix > 128)) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Netmask must be between 0 and 128");
+ goto error;
+ }
+ } else {
+ cf_log_err(cf_sectiontoitem(cs),
+ "No IP address defined for the client");
+ goto error;
+ }
+
+ ip_ntoh(&c->ipaddr, buffer, sizeof(buffer));
+ c->longname = strdup(buffer);
+
+ /*
+ * Set the short name to the name2
+ */
+ if (!c->shortname) c->shortname = strdup(name2);
+
+ c->proto = IPPROTO_UDP;
+#ifdef WITH_TCP
+ if (hs_proto) {
+ if (strcmp(hs_proto, "udp") == 0) {
+ free(hs_proto);
+ hs_proto = NULL;
+
+ } else if (strcmp(hs_proto, "tcp") == 0) {
+ free(hs_proto);
+ hs_proto = NULL;
+ c->proto = IPPROTO_TCP;
+
+ } else if (strcmp(hs_proto, "*") == 0) {
+ free(hs_proto);
+ hs_proto = NULL;
+ c->proto = IPPROTO_IP; /* fake for dual */
+
+ } else {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Unknown proto \"%s\".", hs_proto);
+ goto error;
+ }
+ }
+#endif
+ }
+
+ if (c->prefix < 0) switch (c->ipaddr.af) {
+ case AF_INET:
+ c->prefix = 32;
+ break;
+ case AF_INET6:
+ c->prefix = 128;
+ break;
+ default:
+ break;
+ }
+
+#ifdef WITH_DYNAMIC_CLIENTS
+ if (c->client_server) {
+ free(c->secret);
+ c->secret = strdup("testing123");
+
+ if (((c->ipaddr.af == AF_INET) &&
+ (c->prefix == 32)) ||
+ ((c->ipaddr.af == AF_INET6) &&
+ (c->prefix == 128))) {
+ cf_log_err(cf_sectiontoitem(cs),
+ "Dynamic clients MUST be a network, not a single IP address.");
+ goto error;
+ }
+
+ return c;
+ }
+#endif
+
+ if (!c->secret || !*c->secret) {
+#ifdef WITH_DHCP
+ const char *value = NULL;
+ CONF_PAIR *cp = cf_pair_find(cs, "dhcp");
+
+ if (cp) value = cf_pair_value(cp);
+
+ /*
+ * Secrets aren't needed for DHCP.
+ */
+ if (value && (strcmp(value, "yes") == 0)) return c;
+
+#endif
+ cf_log_err(cf_sectiontoitem(cs),
+ "secret must be at least 1 character long");
+ goto error;
+ }
+
+#ifdef WITH_COA
+ /*
+ * Point the client to the home server pool, OR to the
+ * home server. This gets around the problem of figuring
+ * out which port to use.
+ */
+ if (c->coa_name) {
+ c->coa_pool = home_pool_byname(c->coa_name, HOME_TYPE_COA);
+ if (!c->coa_pool) {
+ c->coa_server = home_server_byname(c->coa_name,
+ HOME_TYPE_COA);
+ }
+ if (!c->coa_pool && !c->coa_server) {
+ cf_log_err(cf_sectiontoitem(cs), "No such home_server or home_server_pool \"%s\"", c->coa_name);
+ goto error;
+ }
+ }
+#endif
+
+ return c;
+}
+
+