xref: /titanic_50/usr/src/cmd/fm/fmd/common/fmd_fmri.c (revision 47f258d370fe585ec2f4768e76dcfc71031dcbab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <ctype.h>
31 #include <errno.h>
32 #include <stdarg.h>
33 
34 #include <fmd_alloc.h>
35 #include <fmd_subr.h>
36 #include <fmd_error.h>
37 #include <fmd_string.h>
38 #include <fmd_scheme.h>
39 #include <fmd_fmri.h>
40 #include <fmd.h>
41 
42 /*
43  * Interfaces to be used by the plugins
44  */
45 
46 void *
47 fmd_fmri_alloc(size_t size)
48 {
49 	return (fmd_alloc(size, FMD_SLEEP));
50 }
51 
52 void *
53 fmd_fmri_zalloc(size_t size)
54 {
55 	return (fmd_zalloc(size, FMD_SLEEP));
56 }
57 
58 void
59 fmd_fmri_free(void *data, size_t size)
60 {
61 	fmd_free(data, size);
62 }
63 
64 int
65 fmd_fmri_set_errno(int err)
66 {
67 	errno = err;
68 	return (-1);
69 }
70 
71 void
72 fmd_fmri_warn(const char *format, ...)
73 {
74 	va_list ap;
75 
76 	va_start(ap, format);
77 	fmd_verror(EFMD_FMRI_SCHEME, format, ap);
78 	va_end(ap);
79 }
80 
81 /*
82  * Convert an input string to a URI escaped string and return the new string.
83  * RFC2396 Section 2.4 says that data must be escaped if it does not have a
84  * representation using an unreserved character, where an unreserved character
85  * is one that is either alphanumberic or one of the marks defined in S2.3.
86  */
87 static size_t
88 fmd_fmri_uriescape(const char *s, const char *xmark, char *buf, size_t len)
89 {
90 	static const char rfc2396_mark[] = "-_.!~*'()";
91 	static const char hex_digits[] = "0123456789ABCDEF";
92 	static const char empty_str[] = "";
93 
94 	const char *p;
95 	char c, *q;
96 	size_t n = 0;
97 
98 	if (s == NULL)
99 		s = empty_str;
100 
101 	if (xmark == NULL)
102 		xmark = empty_str;
103 
104 	for (p = s; (c = *p) != '\0'; p++) {
105 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c))
106 			n++;	/* represent c as itself */
107 		else
108 			n += 3; /* represent c as escape */
109 	}
110 
111 	if (buf == NULL)
112 		return (n);
113 
114 	for (p = s, q = buf; (c = *p) != '\0' && q < buf + len; p++) {
115 		if (isalnum(c) || strchr(rfc2396_mark, c) || strchr(xmark, c)) {
116 			*q++ = c;
117 		} else {
118 			*q++ = '%';
119 			*q++ = hex_digits[((uchar_t)c & 0xf0) >> 4];
120 			*q++ = hex_digits[(uchar_t)c & 0xf];
121 		}
122 	}
123 
124 	if (q == buf + len)
125 		q--; /* len is too small: truncate output string */
126 
127 	*q = '\0';
128 	return (n);
129 }
130 
131 /*
132  * Convert a name-value pair list representing an FMRI authority into the
133  * corresponding RFC2396 string representation and return the new string.
134  */
135 char *
136 fmd_fmri_auth2str(nvlist_t *nvl)
137 {
138 	nvpair_t *nvp;
139 	char *s, *p, *v;
140 	size_t n = 0;
141 
142 	for (nvp = nvlist_next_nvpair(nvl, NULL);
143 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
144 
145 		if (nvpair_type(nvp) != DATA_TYPE_STRING)
146 			continue; /* do not format non-string elements */
147 
148 		n += fmd_fmri_uriescape(nvpair_name(nvp), NULL, NULL, 0) + 1;
149 		(void) nvpair_value_string(nvp, &v);
150 		n += fmd_fmri_uriescape(v, ":", NULL, 0) + 1;
151 	}
152 
153 	p = s = fmd_alloc(n, FMD_SLEEP);
154 
155 	for (nvp = nvlist_next_nvpair(nvl, NULL);
156 	    nvp != NULL; nvp = nvlist_next_nvpair(nvl, nvp)) {
157 
158 		if (nvpair_type(nvp) != DATA_TYPE_STRING)
159 			continue; /* do not format non-string elements */
160 
161 		if (p != s)
162 			*p++ = ',';
163 
164 		p += fmd_fmri_uriescape(nvpair_name(nvp), NULL, p, n);
165 		*p++ = '=';
166 		(void) nvpair_value_string(nvp, &v);
167 		p += fmd_fmri_uriescape(v, ":", p, n);
168 	}
169 
170 	return (s);
171 }
172 
173 /*
174  * Convert an input string to a URI escaped string and return the new string.
175  * We amend the unreserved character list to include commas and colons,
176  * as both are needed to make FMRIs readable without escaping.  We also permit
177  * "/" to pass through unescaped as any path delimiters used by the event
178  * creator are presumably intended to appear in the final path.
179  */
180 char *
181 fmd_fmri_strescape(const char *s)
182 {
183 	char *s2;
184 	size_t n;
185 
186 	if (s == NULL)
187 		return (NULL);
188 
189 	n = fmd_fmri_uriescape(s, ":,/", NULL, 0);
190 	s2 = fmd_alloc(n + 1, FMD_SLEEP);
191 	(void) fmd_fmri_uriescape(s, ":,/", s2, n + 1);
192 
193 	return (s2);
194 }
195 
196 char *
197 fmd_fmri_strdup(const char *s)
198 {
199 	return (fmd_strdup(s, FMD_SLEEP));
200 }
201 
202 void
203 fmd_fmri_strfree(char *s)
204 {
205 	fmd_strfree(s);
206 }
207 
208 const char *
209 fmd_fmri_get_rootdir(void)
210 {
211 	return (fmd.d_rootdir);
212 }
213 
214 const char *
215 fmd_fmri_get_platform(void)
216 {
217 	return (fmd.d_platform);
218 }
219 
220 uint64_t
221 fmd_fmri_get_drgen(void)
222 {
223 	uint64_t gen;
224 
225 	(void) pthread_mutex_lock(&fmd.d_stats_lock);
226 	gen = fmd.d_stats->ds_dr_gen.fmds_value.ui64;
227 	(void) pthread_mutex_unlock(&fmd.d_stats_lock);
228 
229 	return (gen);
230 }
231 
232 /*
233  * Interfaces for users of the plugins
234  */
235 
236 static fmd_scheme_t *
237 nvl2scheme(nvlist_t *nvl)
238 {
239 	char *name;
240 
241 	if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) != 0) {
242 		(void) fmd_set_errno(EFMD_FMRI_INVAL);
243 		return (NULL);
244 	}
245 
246 	return (fmd_scheme_hash_lookup(fmd.d_schemes, name));
247 }
248 
249 ssize_t
250 fmd_fmri_nvl2str(nvlist_t *nvl, char *buf, size_t buflen)
251 {
252 	fmd_scheme_t *sp;
253 	char c;
254 	ssize_t rv;
255 
256 	if (buf == NULL && buflen == 0) {
257 		buf = &c;
258 		buflen = sizeof (c);
259 	}
260 
261 	if ((sp = nvl2scheme(nvl)) == NULL)
262 		return (-1); /* errno is set for us */
263 
264 	(void) pthread_mutex_lock(&sp->sch_opslock);
265 	ASSERT(buf != NULL || buflen == 0);
266 	rv = sp->sch_ops.sop_nvl2str(nvl, buf, buflen);
267 	(void) pthread_mutex_unlock(&sp->sch_opslock);
268 
269 	fmd_scheme_hash_release(fmd.d_schemes, sp);
270 	return (rv);
271 }
272 
273 int
274 fmd_fmri_expand(nvlist_t *nvl)
275 {
276 	fmd_scheme_t *sp;
277 	int rv;
278 
279 	if ((sp = nvl2scheme(nvl)) == NULL)
280 		return (-1); /* errno is set for us */
281 
282 	(void) pthread_mutex_lock(&sp->sch_opslock);
283 	rv = sp->sch_ops.sop_expand(nvl);
284 	(void) pthread_mutex_unlock(&sp->sch_opslock);
285 
286 	fmd_scheme_hash_release(fmd.d_schemes, sp);
287 	return (rv);
288 }
289 
290 int
291 fmd_fmri_present(nvlist_t *nvl)
292 {
293 	fmd_scheme_t *sp;
294 	int rv;
295 
296 	if ((sp = nvl2scheme(nvl)) == NULL)
297 		return (-1); /* errno is set for us */
298 
299 	(void) pthread_mutex_lock(&sp->sch_opslock);
300 	rv = sp->sch_ops.sop_present(nvl);
301 	(void) pthread_mutex_unlock(&sp->sch_opslock);
302 
303 	fmd_scheme_hash_release(fmd.d_schemes, sp);
304 	return (rv);
305 }
306 
307 int
308 fmd_fmri_unusable(nvlist_t *nvl)
309 {
310 	fmd_scheme_t *sp;
311 	int rv;
312 
313 	if ((sp = nvl2scheme(nvl)) == NULL)
314 		return (-1); /* errno is set for us */
315 
316 	(void) pthread_mutex_lock(&sp->sch_opslock);
317 	rv = sp->sch_ops.sop_unusable(nvl);
318 	(void) pthread_mutex_unlock(&sp->sch_opslock);
319 
320 	fmd_scheme_hash_release(fmd.d_schemes, sp);
321 	return (rv);
322 }
323 
324 int
325 fmd_fmri_contains(nvlist_t *er, nvlist_t *ee)
326 {
327 	fmd_scheme_t *sp;
328 	char *ername, *eename;
329 	int rv;
330 
331 	if (nvlist_lookup_string(er, FM_FMRI_SCHEME, &ername) != 0 ||
332 	    nvlist_lookup_string(ee, FM_FMRI_SCHEME, &eename) != 0 ||
333 	    strcmp(ername, eename) != 0)
334 		return (fmd_set_errno(EFMD_FMRI_INVAL));
335 
336 	if ((sp = fmd_scheme_hash_lookup(fmd.d_schemes, ername)) == NULL)
337 		return (-1); /* errno is set for us */
338 
339 	(void) pthread_mutex_lock(&sp->sch_opslock);
340 	rv = sp->sch_ops.sop_contains(er, ee);
341 	(void) pthread_mutex_unlock(&sp->sch_opslock);
342 
343 	fmd_scheme_hash_release(fmd.d_schemes, sp);
344 	return (rv);
345 }
346 
347 nvlist_t *
348 fmd_fmri_translate(nvlist_t *fmri, nvlist_t *auth)
349 {
350 	fmd_scheme_t *sp;
351 	nvlist_t *nvl;
352 
353 	if ((sp = nvl2scheme(fmri)) == NULL)
354 		return (NULL); /* errno is set for us */
355 
356 	(void) pthread_mutex_lock(&sp->sch_opslock);
357 	nvl = sp->sch_ops.sop_translate(fmri, auth);
358 	(void) pthread_mutex_unlock(&sp->sch_opslock);
359 
360 	fmd_scheme_hash_release(fmd.d_schemes, sp);
361 	return (nvl);
362 }
363