Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / krb5 / krb / parse_host_string.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/parse_host_string.c - Parse host strings into host and port */
3 /*
4  * Copyright (C) 2016 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "k5-int.h"
34 #include <ctype.h>
35
36 /* Return true if s is composed solely of digits. */
37 krb5_boolean
38 k5_is_string_numeric(const char *s)
39 {
40     if (*s == '\0')
41         return FALSE;
42
43     for (; *s != '\0'; s++) {
44         if (!isdigit(*s))
45             return FALSE;
46     }
47
48     return TRUE;
49 }
50
51 /*
52  * Parse a string containing a host specifier. The expected format for the
53  * string is:
54  *
55  * host[:port] or port
56  *
57  * host and port are optional, though one must be present.  host may have
58  * brackets around it for IPv6 addresses.
59  *
60  * Arguments:
61  * address - The address string that should be parsed.
62  * default_port - The default port to use if no port is found.
63  * host_out - An output pointer for the parsed host, or NULL if no host was
64  * specified or an error occured.  Must be freed.
65  * port_out - An output pointer for the parsed port.  Will be 0 on error.
66  *
67  * Returns 0 on success, otherwise an error.
68  */
69 krb5_error_code
70 k5_parse_host_string(const char *address, int default_port, char **host_out,
71                      int *port_out)
72 {
73     krb5_error_code ret;
74     int port_num;
75     const char *p, *host = NULL, *port = NULL;
76     char *endptr, *hostname = NULL;
77     size_t hostlen = 0;
78     unsigned long l;
79
80     *host_out = NULL;
81     *port_out = 0;
82
83     if (address == NULL || *address == '\0' || *address == ':')
84         return EINVAL;
85     if (default_port < 0 || default_port > 65535)
86         return EINVAL;
87
88     /* Find the bounds of the host string and the start of the port string. */
89     if (k5_is_string_numeric(address)) {
90         port = address;
91     } else if (*address == '[' && (p = strchr(address, ']')) != NULL) {
92         host = address + 1;
93         hostlen = p - host;
94         if (*(p + 1) == ':')
95             port = p + 2;
96     } else {
97         host = address;
98         hostlen = strcspn(host, " \t:");
99         if (host[hostlen] == ':')
100             port = host + hostlen + 1;
101     }
102
103     /* Parse the port number, or use the default port. */
104     if (port != NULL) {
105         errno = 0;
106         l = strtoul(port, &endptr, 10);
107         if (errno || endptr == port || *endptr != '\0' || l > 65535)
108             return EINVAL;
109         port_num = l;
110     } else {
111         port_num = default_port;
112     }
113
114     /* Copy the host if it was specified. */
115     if (host != NULL) {
116         hostname = k5memdup0(host, hostlen, &ret);
117         if (hostname == NULL)
118             return ENOMEM;
119     }
120
121     *host_out = hostname;
122     *port_out = port_num;
123     return 0;
124 }