xref: /titanic_41/usr/src/lib/print/libprint/common/nss_convert.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
1355b4669Sjacobs /*
2355b4669Sjacobs  * CDDL HEADER START
3355b4669Sjacobs  *
4355b4669Sjacobs  * The contents of this file are subject to the terms of the
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * You may not use this file except in compliance with the License.
7355b4669Sjacobs  *
8355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs  * See the License for the specific language governing permissions
11355b4669Sjacobs  * and limitations under the License.
12355b4669Sjacobs  *
13355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs  *
19355b4669Sjacobs  * CDDL HEADER END
20355b4669Sjacobs  */
21355b4669Sjacobs /*
22*c1ecd8b9Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23355b4669Sjacobs  * Use is subject to license terms.
24355b4669Sjacobs  */
25355b4669Sjacobs 
26355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
27355b4669Sjacobs 
28355b4669Sjacobs /*
29355b4669Sjacobs  * This file contains routines necessary to convert a string buffer into
30355b4669Sjacobs  * a printer object.
31355b4669Sjacobs  */
32355b4669Sjacobs 
33355b4669Sjacobs #include <stdio.h>
34355b4669Sjacobs #include <stdlib.h>
35355b4669Sjacobs #include <unistd.h>
36355b4669Sjacobs #include <sys/types.h>
37355b4669Sjacobs #include <string.h>
38355b4669Sjacobs #include <stdarg.h>
39355b4669Sjacobs #include <syslog.h>
40355b4669Sjacobs 
41355b4669Sjacobs #include <ns.h>
42355b4669Sjacobs #include <list.h>
43355b4669Sjacobs 
44355b4669Sjacobs #define	ESCAPE_CHARS	"\\\n=:"	/* \, \n, =, : */
45355b4669Sjacobs 
46355b4669Sjacobs /*
47355b4669Sjacobs  * Just like strncat(3C), but escapes the supplied characters.
48355b4669Sjacobs  * This allows the escape character '\' and seperators to be part of the
49355b4669Sjacobs  * keys or values.
50355b4669Sjacobs  */
51355b4669Sjacobs char *
strncat_escaped(char * d,char * s,int len,char * escape)52355b4669Sjacobs strncat_escaped(char *d, char *s, int len, char *escape)
53355b4669Sjacobs {
54355b4669Sjacobs 	char *t = d;
55355b4669Sjacobs 
56355b4669Sjacobs 	while ((*t != NULL) && (len > 0))
57355b4669Sjacobs 		len--, t++;
58355b4669Sjacobs 
59355b4669Sjacobs 	if (escape == NULL)
60355b4669Sjacobs 		escape = "\\";
61355b4669Sjacobs 
62355b4669Sjacobs 	while ((*s != NULL) && (len > 0)) {
63355b4669Sjacobs 		if (strchr(escape, *s) != NULL)
64355b4669Sjacobs 			len--, *t++ = '\\';
65355b4669Sjacobs 		len--, *t++ = *s++;
66355b4669Sjacobs 	}
67355b4669Sjacobs 	*t = NULL;
68355b4669Sjacobs 
69355b4669Sjacobs 	return (d);
70355b4669Sjacobs }
71355b4669Sjacobs 
72355b4669Sjacobs 
73355b4669Sjacobs 
74355b4669Sjacobs char *
_cvt_printer_to_entry(ns_printer_t * printer,char * buf,int buflen)75355b4669Sjacobs _cvt_printer_to_entry(ns_printer_t *printer, char *buf, int buflen)
76355b4669Sjacobs {
77355b4669Sjacobs 	int i, len;
78355b4669Sjacobs 	int bufferok = 1;
79355b4669Sjacobs 
80355b4669Sjacobs 	(void) memset(buf, NULL, buflen);
81355b4669Sjacobs 
82355b4669Sjacobs 	if ((printer == NULL) || (printer->attributes == NULL))
83355b4669Sjacobs 		return (NULL);
84355b4669Sjacobs 
85355b4669Sjacobs 	if (snprintf(buf, buflen, "%s", printer->name) >= buflen) {
86355b4669Sjacobs 		(void) memset(buf, NULL, buflen);
87355b4669Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
88355b4669Sjacobs 		return (NULL);
89355b4669Sjacobs 	}
90355b4669Sjacobs 
91355b4669Sjacobs 	if ((printer->aliases != NULL) && (printer->aliases[0] != NULL)) {
92355b4669Sjacobs 		char **alias = printer->aliases;
93355b4669Sjacobs 
94355b4669Sjacobs 		while (*alias != NULL) {
95355b4669Sjacobs 			(void) strlcat(buf, "|", buflen);
96355b4669Sjacobs 			(void) strncat_escaped(buf, *alias++, buflen,
97355b4669Sjacobs 			    ESCAPE_CHARS);
98355b4669Sjacobs 		}
99355b4669Sjacobs 	}
100355b4669Sjacobs 
101355b4669Sjacobs 	if (strlcat(buf, ":", buflen) >= buflen) {
102355b4669Sjacobs 		(void) memset(buf, NULL, buflen);
103355b4669Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
104355b4669Sjacobs 		return (NULL);
105355b4669Sjacobs 	}
106355b4669Sjacobs 
107355b4669Sjacobs 	len = strlen(buf);
108355b4669Sjacobs 
109355b4669Sjacobs 	for (i = 0; printer->attributes[i] != NULL && bufferok; i++) {
110355b4669Sjacobs 		ns_kvp_t *kvp = printer->attributes[i];
111355b4669Sjacobs 
112355b4669Sjacobs 		if (kvp->value == NULL)
113355b4669Sjacobs 			continue;
114355b4669Sjacobs 		(void) strlcat(buf, "\\\n\t:", buflen);
115355b4669Sjacobs 		(void) strncat_escaped(buf, kvp->key, buflen, ESCAPE_CHARS);
116355b4669Sjacobs 		(void) strlcat(buf, "=", buflen);
117355b4669Sjacobs 		(void) strncat_escaped(buf, kvp->value, buflen, ESCAPE_CHARS);
118355b4669Sjacobs 		if (strlcat(buf, ":", buflen) >= buflen) {
119355b4669Sjacobs 			bufferok = 0;
120355b4669Sjacobs 		}
121355b4669Sjacobs 	}
122355b4669Sjacobs 
123355b4669Sjacobs 	if (!bufferok) {
124355b4669Sjacobs 		(void) memset(buf, NULL, buflen);
125355b4669Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
126355b4669Sjacobs 		return (NULL);
127355b4669Sjacobs 	}
128355b4669Sjacobs 
129355b4669Sjacobs 	if (strlen(buf) == len) {	/* there were no attributes */
130355b4669Sjacobs 		(void) memset(buf, NULL, buflen);
131355b4669Sjacobs 		buf = NULL;
132355b4669Sjacobs 	}
133355b4669Sjacobs 
134355b4669Sjacobs 	return (buf);
135355b4669Sjacobs }
136355b4669Sjacobs 
137355b4669Sjacobs 
138355b4669Sjacobs ns_printer_t *
_cvt_nss_entry_to_printer(char * entry,char * ns)139355b4669Sjacobs _cvt_nss_entry_to_printer(char *entry, char *ns)
140355b4669Sjacobs {
141355b4669Sjacobs 	char	*name = NULL,
142355b4669Sjacobs 		*key = NULL,
143355b4669Sjacobs 		**aliases = NULL,
144355b4669Sjacobs 		*cp,
145355b4669Sjacobs 		buf[BUFSIZ];
146355b4669Sjacobs 	int in_namelist = 1, buf_pos = 0;
147355b4669Sjacobs 	ns_printer_t *printer = NULL;
148355b4669Sjacobs 
149355b4669Sjacobs 	if (entry == NULL)
150355b4669Sjacobs 		return (NULL);
151355b4669Sjacobs 
152355b4669Sjacobs 	(void) memset(buf, NULL, sizeof (buf));
153355b4669Sjacobs 	for (cp = entry; *cp != NULL; cp++) {
154355b4669Sjacobs 		switch (*cp) {
155355b4669Sjacobs 		case ':':	/* end of kvp */
156355b4669Sjacobs 			if (in_namelist != 0) {
157355b4669Sjacobs 				if (name == NULL)
158355b4669Sjacobs 					name = strdup(buf);
159355b4669Sjacobs 				else
160355b4669Sjacobs 					aliases = (char **)list_append(
161355b4669Sjacobs 							(void **)aliases,
162355b4669Sjacobs 							(void *)strdup(buf));
163355b4669Sjacobs 				printer = (ns_printer_t *)ns_printer_create(
164355b4669Sjacobs 						name, aliases, ns, NULL);
165355b4669Sjacobs 				in_namelist = 0;
166355b4669Sjacobs 			} else if (key != NULL)
167355b4669Sjacobs 				(void) ns_set_value_from_string(key, buf,
168355b4669Sjacobs 				    printer);
169355b4669Sjacobs 			(void) memset(buf, NULL, sizeof (buf));
170355b4669Sjacobs 			buf_pos = 0;
171355b4669Sjacobs 			key = NULL;
172355b4669Sjacobs 			break;
173355b4669Sjacobs 		case '=':	/* kvp seperator */
174355b4669Sjacobs 			if (key == NULL) {
175355b4669Sjacobs 				key = strdup(buf);
176355b4669Sjacobs 				(void) memset(buf, NULL, sizeof (buf));
177355b4669Sjacobs 				buf_pos = 0;
178355b4669Sjacobs 			} else
179355b4669Sjacobs 				buf[buf_pos++] = *cp;
180355b4669Sjacobs 			break;
181355b4669Sjacobs 		case '|':	/* namelist seperator */
182355b4669Sjacobs 			if (in_namelist != 0) {
183355b4669Sjacobs 				if (name == NULL)
184355b4669Sjacobs 					name = strdup(buf);
185355b4669Sjacobs 				else
186355b4669Sjacobs 					aliases = (char **)list_append(
187355b4669Sjacobs 							(void **)aliases,
188355b4669Sjacobs 							(void *)strdup(buf));
189355b4669Sjacobs 				(void) memset(buf, NULL, sizeof (buf));
190355b4669Sjacobs 				buf_pos = 0;
191355b4669Sjacobs 			} else	/* add it to the buffer */
192355b4669Sjacobs 				buf[buf_pos++] = *cp;
193355b4669Sjacobs 			break;
194355b4669Sjacobs 		case '\\':	/* escape char */
195355b4669Sjacobs 			buf[buf_pos++] = *(++cp);
196355b4669Sjacobs 			break;
197355b4669Sjacobs 		default:
198355b4669Sjacobs 			buf[buf_pos++] = *cp;
199355b4669Sjacobs 		}
200355b4669Sjacobs 
201355b4669Sjacobs 	}
202355b4669Sjacobs 
203355b4669Sjacobs 	if (key != NULL)
204355b4669Sjacobs 		(void) ns_set_value_from_string(key, buf, printer);
205355b4669Sjacobs 
206355b4669Sjacobs 	return (printer);
207355b4669Sjacobs }
208