xref: /illumos-gate/usr/src/lib/libslp/clib/SLPParseSrvURL.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <stdio.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <ctype.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <syslog.h>
32*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * URL parsing
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #define	SLP_IANA	"iana"
39*7c478bd9Sstevel@tonic-gate #define	SERVICE_PREFIX	"service"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /* service type struct */
42*7c478bd9Sstevel@tonic-gate typedef struct slp_type {
43*7c478bd9Sstevel@tonic-gate 	SLPBoolean isServiceURL;
44*7c478bd9Sstevel@tonic-gate 	char *atype;
45*7c478bd9Sstevel@tonic-gate 	char *ctype;
46*7c478bd9Sstevel@tonic-gate 	char *na;
47*7c478bd9Sstevel@tonic-gate 	char *orig;
48*7c478bd9Sstevel@tonic-gate } slp_type_t;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static SLPError parseType(char *, slp_type_t *);
51*7c478bd9Sstevel@tonic-gate static int validateTypeChars(char *);
52*7c478bd9Sstevel@tonic-gate static int validateTransport(char *);
53*7c478bd9Sstevel@tonic-gate static int checkURLString(char *);
54*7c478bd9Sstevel@tonic-gate 
SLPParseSrvURL(char * pcSrvURL,SLPSrvURL ** ppSrvURL)55*7c478bd9Sstevel@tonic-gate SLPError SLPParseSrvURL(char *pcSrvURL, SLPSrvURL** ppSrvURL) {
56*7c478bd9Sstevel@tonic-gate 	char *p, *q, *r;
57*7c478bd9Sstevel@tonic-gate 	SLPSrvURL *surl;
58*7c478bd9Sstevel@tonic-gate 	slp_type_t type[1];
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (!pcSrvURL || !ppSrvURL) {
61*7c478bd9Sstevel@tonic-gate 		return (SLP_PARAMETER_BAD);
62*7c478bd9Sstevel@tonic-gate 	}
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	*ppSrvURL = NULL;
65*7c478bd9Sstevel@tonic-gate 	if (!checkURLString((char *)pcSrvURL))
66*7c478bd9Sstevel@tonic-gate 		return (SLP_PARSE_ERROR);
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (!(surl = malloc(sizeof (*surl)))) {
69*7c478bd9Sstevel@tonic-gate 		slp_err(LOG_CRIT, 0, "SLPParseSrvURL", "out of memory");
70*7c478bd9Sstevel@tonic-gate 		return (SLP_MEMORY_ALLOC_FAILED);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate 	*ppSrvURL = surl;
73*7c478bd9Sstevel@tonic-gate 	surl->s_pcSrvType = "";
74*7c478bd9Sstevel@tonic-gate 	surl->s_pcNetFamily = "";
75*7c478bd9Sstevel@tonic-gate 	surl->s_pcHost = "";
76*7c478bd9Sstevel@tonic-gate 	surl->s_iPort = 0;
77*7c478bd9Sstevel@tonic-gate 	surl->s_pcSrvPart = "";
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	/* parse type */
80*7c478bd9Sstevel@tonic-gate 	p = strstr(pcSrvURL, ":/");
81*7c478bd9Sstevel@tonic-gate 	if (!p)
82*7c478bd9Sstevel@tonic-gate 		goto error;
83*7c478bd9Sstevel@tonic-gate 	q = pcSrvURL;
84*7c478bd9Sstevel@tonic-gate 	*p++ = 0; p++;
85*7c478bd9Sstevel@tonic-gate 	r = strdup(q);
86*7c478bd9Sstevel@tonic-gate 	if (parseType(r, type) != SLP_OK)
87*7c478bd9Sstevel@tonic-gate 	goto error;
88*7c478bd9Sstevel@tonic-gate 	free(r);
89*7c478bd9Sstevel@tonic-gate 	/* no need to free type since it is on the stack */
90*7c478bd9Sstevel@tonic-gate 	surl->s_pcSrvType = q;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	/* do we have a transport? */
93*7c478bd9Sstevel@tonic-gate 	q = strchr(p, '/');
94*7c478bd9Sstevel@tonic-gate 	if (!q)
95*7c478bd9Sstevel@tonic-gate 		goto error;
96*7c478bd9Sstevel@tonic-gate 	*q++ = 0;
97*7c478bd9Sstevel@tonic-gate 	if (!validateTransport(p))
98*7c478bd9Sstevel@tonic-gate 		goto error;
99*7c478bd9Sstevel@tonic-gate 	surl->s_pcNetFamily = p;	/* may be \0 */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	/* host part */
102*7c478bd9Sstevel@tonic-gate 	/* do we have a port #? */
103*7c478bd9Sstevel@tonic-gate 	p = strchr(q, ':');
104*7c478bd9Sstevel@tonic-gate 	r = strchr(q, '/');
105*7c478bd9Sstevel@tonic-gate 	if (!p && !r) {	/* only host part */
106*7c478bd9Sstevel@tonic-gate 		surl->s_pcHost = q;
107*7c478bd9Sstevel@tonic-gate 		return (SLP_OK);
108*7c478bd9Sstevel@tonic-gate 	}
109*7c478bd9Sstevel@tonic-gate 	if (p && !r) {	/* host + port, no URL part */
110*7c478bd9Sstevel@tonic-gate 		int port;
111*7c478bd9Sstevel@tonic-gate 		surl->s_pcHost = q;
112*7c478bd9Sstevel@tonic-gate 		*p++ = 0;
113*7c478bd9Sstevel@tonic-gate 		port = atoi(p);
114*7c478bd9Sstevel@tonic-gate 		if (port <= 0)
115*7c478bd9Sstevel@tonic-gate 			goto error;
116*7c478bd9Sstevel@tonic-gate 		surl->s_iPort = port;
117*7c478bd9Sstevel@tonic-gate 		return (SLP_OK);
118*7c478bd9Sstevel@tonic-gate 	}
119*7c478bd9Sstevel@tonic-gate 	*r++ = 0;
120*7c478bd9Sstevel@tonic-gate 	if (!p || p > r) {	/* no port */
121*7c478bd9Sstevel@tonic-gate 		surl->s_pcHost = q;
122*7c478bd9Sstevel@tonic-gate 	} else {		/* host + port + url part */
123*7c478bd9Sstevel@tonic-gate 		int port;
124*7c478bd9Sstevel@tonic-gate 		surl->s_pcHost = q;
125*7c478bd9Sstevel@tonic-gate 		*p++ = 0;
126*7c478bd9Sstevel@tonic-gate 		port = atoi(p);
127*7c478bd9Sstevel@tonic-gate 		if (port <= 0)
128*7c478bd9Sstevel@tonic-gate 			goto error;
129*7c478bd9Sstevel@tonic-gate 		surl->s_iPort = port;
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	/* r now points to the URL part */
133*7c478bd9Sstevel@tonic-gate 	surl->s_pcSrvPart = r;
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate error:
138*7c478bd9Sstevel@tonic-gate 	free(surl);
139*7c478bd9Sstevel@tonic-gate 	*ppSrvURL = NULL;
140*7c478bd9Sstevel@tonic-gate 	return (SLP_PARSE_ERROR);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * typeString contains only the service type part of an URL. It should
145*7c478bd9Sstevel@tonic-gate  * point to a string which parseType can destructively modify.
146*7c478bd9Sstevel@tonic-gate  */
parseType(char * typeString,slp_type_t * type)147*7c478bd9Sstevel@tonic-gate static SLPError parseType(char *typeString, slp_type_t *type) {
148*7c478bd9Sstevel@tonic-gate 	char *p, *q;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	/* Initialize type structure */
151*7c478bd9Sstevel@tonic-gate 	type->isServiceURL = SLP_FALSE;
152*7c478bd9Sstevel@tonic-gate 	type->atype = NULL;
153*7c478bd9Sstevel@tonic-gate 	type->ctype = NULL;
154*7c478bd9Sstevel@tonic-gate 	type->na = NULL;
155*7c478bd9Sstevel@tonic-gate 	type->orig = typeString;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	if (!validateTypeChars(typeString))
158*7c478bd9Sstevel@tonic-gate 		return (SLP_PARSE_ERROR);
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	/* Is this a service: URL? */
161*7c478bd9Sstevel@tonic-gate 	p = strchr(typeString, ':');
162*7c478bd9Sstevel@tonic-gate 	if (strncasecmp(
163*7c478bd9Sstevel@tonic-gate 		typeString, SERVICE_PREFIX, strlen(SERVICE_PREFIX)) == 0) {
164*7c478bd9Sstevel@tonic-gate 		type->isServiceURL = SLP_TRUE;
165*7c478bd9Sstevel@tonic-gate 		if (!p)
166*7c478bd9Sstevel@tonic-gate 			return (SLP_PARSE_ERROR);
167*7c478bd9Sstevel@tonic-gate 		*p++ = 0;
168*7c478bd9Sstevel@tonic-gate 	} else {
169*7c478bd9Sstevel@tonic-gate 		if (p)	/* can't have an abstract type in a non-service url */
170*7c478bd9Sstevel@tonic-gate 			return (SLP_PARSE_ERROR);
171*7c478bd9Sstevel@tonic-gate 		p = typeString;
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* p now points to the beginning of the type */
175*7c478bd9Sstevel@tonic-gate 	/* is this an abstract type? */
176*7c478bd9Sstevel@tonic-gate 	q = strchr(p, ':');
177*7c478bd9Sstevel@tonic-gate 	if (q) {
178*7c478bd9Sstevel@tonic-gate 		type->atype = p;
179*7c478bd9Sstevel@tonic-gate 		*q++ = 0;
180*7c478bd9Sstevel@tonic-gate 		if (!*p)
181*7c478bd9Sstevel@tonic-gate 			return (SLP_PARSE_ERROR);
182*7c478bd9Sstevel@tonic-gate 	} else { q = p; }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	/* q should now point to the concrete type */
185*7c478bd9Sstevel@tonic-gate 	/* is there a naming authority? */
186*7c478bd9Sstevel@tonic-gate 	p = strchr(q, '.');
187*7c478bd9Sstevel@tonic-gate 	if (p) {
188*7c478bd9Sstevel@tonic-gate 		*p++ = 0;
189*7c478bd9Sstevel@tonic-gate 		if (!*p)
190*7c478bd9Sstevel@tonic-gate 			return (SLP_PARSE_ERROR);
191*7c478bd9Sstevel@tonic-gate 		type->na = p;
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	if (!*q)
194*7c478bd9Sstevel@tonic-gate 		return (SLP_PARSE_ERROR);
195*7c478bd9Sstevel@tonic-gate 	type->ctype = q;
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate 	return (SLP_OK);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate 
validateTransport(char * t)200*7c478bd9Sstevel@tonic-gate static int validateTransport(char *t) {
201*7c478bd9Sstevel@tonic-gate 	if (*t == 0 ||
202*7c478bd9Sstevel@tonic-gate 	    strcasecmp(t, "ipx") == 0 ||
203*7c478bd9Sstevel@tonic-gate 	    strcasecmp(t, "at") == 0)
204*7c478bd9Sstevel@tonic-gate 		return (1);
205*7c478bd9Sstevel@tonic-gate 	return (0);
206*7c478bd9Sstevel@tonic-gate }
207*7c478bd9Sstevel@tonic-gate 
checkURLString(char * s)208*7c478bd9Sstevel@tonic-gate static int checkURLString(char *s) {
209*7c478bd9Sstevel@tonic-gate 	int i;
210*7c478bd9Sstevel@tonic-gate 	size_t l = strlen(s);
211*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < l; i++) {
212*7c478bd9Sstevel@tonic-gate 		if (isalnum(s[i]) ||
213*7c478bd9Sstevel@tonic-gate 		    s[i] == '/' || s[i] == ':' || s[i] == '-' ||
214*7c478bd9Sstevel@tonic-gate 		    s[i] == ':' || s[i] == '.' || s[i] == '%' ||
215*7c478bd9Sstevel@tonic-gate 		    s[i] == '_' || s[i] == '\''|| s[i] == '*' ||
216*7c478bd9Sstevel@tonic-gate 		    s[i] == '(' || s[i] == ')' || s[i] == '$' ||
217*7c478bd9Sstevel@tonic-gate 		    s[i] == '!' || s[i] == ',' || s[i] == '+' ||
218*7c478bd9Sstevel@tonic-gate 		    s[i] == '\\'|| s[i] == ';' || s[i] == '@' ||
219*7c478bd9Sstevel@tonic-gate 		    s[i] == '?' || s[i] == '&' || s[i] == '=')
220*7c478bd9Sstevel@tonic-gate 			continue;
221*7c478bd9Sstevel@tonic-gate 		return (0);
222*7c478bd9Sstevel@tonic-gate 	}
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	return (1);
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 
validateTypeChars(char * s)228*7c478bd9Sstevel@tonic-gate static int validateTypeChars(char *s) {
229*7c478bd9Sstevel@tonic-gate 	int i;
230*7c478bd9Sstevel@tonic-gate 	size_t l = strlen(s);
231*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < l; i++)
232*7c478bd9Sstevel@tonic-gate 		if (!isalnum(s[i]) &&
233*7c478bd9Sstevel@tonic-gate 		    s[i] != '-' &&
234*7c478bd9Sstevel@tonic-gate 		    s[i] != '+' &&
235*7c478bd9Sstevel@tonic-gate 		    s[i] != '.' &&
236*7c478bd9Sstevel@tonic-gate 		    s[i] != ':')
237*7c478bd9Sstevel@tonic-gate 			return (0);
238*7c478bd9Sstevel@tonic-gate 	return (1);
239*7c478bd9Sstevel@tonic-gate }
240