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