xref: /onnv-gate/usr/src/lib/print/libprint/common/nss_convert.c (revision 7253:39650c3caeca)
12264Sjacobs /*
22264Sjacobs  * CDDL HEADER START
32264Sjacobs  *
42264Sjacobs  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
72264Sjacobs  *
82264Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92264Sjacobs  * or http://www.opensolaris.org/os/licensing.
102264Sjacobs  * See the License for the specific language governing permissions
112264Sjacobs  * and limitations under the License.
122264Sjacobs  *
132264Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
142264Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152264Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
162264Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
172264Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
182264Sjacobs  *
192264Sjacobs  * CDDL HEADER END
202264Sjacobs  */
212264Sjacobs /*
22*7253Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
232264Sjacobs  * Use is subject to license terms.
242264Sjacobs  */
252264Sjacobs 
262264Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
272264Sjacobs 
282264Sjacobs /*
292264Sjacobs  * This file contains routines necessary to convert a string buffer into
302264Sjacobs  * a printer object.
312264Sjacobs  */
322264Sjacobs 
332264Sjacobs #include <stdio.h>
342264Sjacobs #include <stdlib.h>
352264Sjacobs #include <unistd.h>
362264Sjacobs #include <sys/types.h>
372264Sjacobs #include <string.h>
382264Sjacobs #include <stdarg.h>
392264Sjacobs #include <syslog.h>
402264Sjacobs 
412264Sjacobs #include <ns.h>
422264Sjacobs #include <list.h>
432264Sjacobs 
442264Sjacobs #define	ESCAPE_CHARS	"\\\n=:"	/* \, \n, =, : */
452264Sjacobs 
462264Sjacobs /*
472264Sjacobs  * Just like strncat(3C), but escapes the supplied characters.
482264Sjacobs  * This allows the escape character '\' and seperators to be part of the
492264Sjacobs  * keys or values.
502264Sjacobs  */
512264Sjacobs char *
strncat_escaped(char * d,char * s,int len,char * escape)522264Sjacobs strncat_escaped(char *d, char *s, int len, char *escape)
532264Sjacobs {
542264Sjacobs 	char *t = d;
552264Sjacobs 
562264Sjacobs 	while ((*t != NULL) && (len > 0))
572264Sjacobs 		len--, t++;
582264Sjacobs 
592264Sjacobs 	if (escape == NULL)
602264Sjacobs 		escape = "\\";
612264Sjacobs 
622264Sjacobs 	while ((*s != NULL) && (len > 0)) {
632264Sjacobs 		if (strchr(escape, *s) != NULL)
642264Sjacobs 			len--, *t++ = '\\';
652264Sjacobs 		len--, *t++ = *s++;
662264Sjacobs 	}
672264Sjacobs 	*t = NULL;
682264Sjacobs 
692264Sjacobs 	return (d);
702264Sjacobs }
712264Sjacobs 
722264Sjacobs 
732264Sjacobs 
742264Sjacobs char *
_cvt_printer_to_entry(ns_printer_t * printer,char * buf,int buflen)752264Sjacobs _cvt_printer_to_entry(ns_printer_t *printer, char *buf, int buflen)
762264Sjacobs {
772264Sjacobs 	int i, len;
782264Sjacobs 	int bufferok = 1;
792264Sjacobs 
802264Sjacobs 	(void) memset(buf, NULL, buflen);
812264Sjacobs 
822264Sjacobs 	if ((printer == NULL) || (printer->attributes == NULL))
832264Sjacobs 		return (NULL);
842264Sjacobs 
852264Sjacobs 	if (snprintf(buf, buflen, "%s", printer->name) >= buflen) {
862264Sjacobs 		(void) memset(buf, NULL, buflen);
872264Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
882264Sjacobs 		return (NULL);
892264Sjacobs 	}
902264Sjacobs 
912264Sjacobs 	if ((printer->aliases != NULL) && (printer->aliases[0] != NULL)) {
922264Sjacobs 		char **alias = printer->aliases;
932264Sjacobs 
942264Sjacobs 		while (*alias != NULL) {
952264Sjacobs 			(void) strlcat(buf, "|", buflen);
962264Sjacobs 			(void) strncat_escaped(buf, *alias++, buflen,
972264Sjacobs 			    ESCAPE_CHARS);
982264Sjacobs 		}
992264Sjacobs 	}
1002264Sjacobs 
1012264Sjacobs 	if (strlcat(buf, ":", buflen) >= buflen) {
1022264Sjacobs 		(void) memset(buf, NULL, buflen);
1032264Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
1042264Sjacobs 		return (NULL);
1052264Sjacobs 	}
1062264Sjacobs 
1072264Sjacobs 	len = strlen(buf);
1082264Sjacobs 
1092264Sjacobs 	for (i = 0; printer->attributes[i] != NULL && bufferok; i++) {
1102264Sjacobs 		ns_kvp_t *kvp = printer->attributes[i];
1112264Sjacobs 
1122264Sjacobs 		if (kvp->value == NULL)
1132264Sjacobs 			continue;
1142264Sjacobs 		(void) strlcat(buf, "\\\n\t:", buflen);
1152264Sjacobs 		(void) strncat_escaped(buf, kvp->key, buflen, ESCAPE_CHARS);
1162264Sjacobs 		(void) strlcat(buf, "=", buflen);
1172264Sjacobs 		(void) strncat_escaped(buf, kvp->value, buflen, ESCAPE_CHARS);
1182264Sjacobs 		if (strlcat(buf, ":", buflen) >= buflen) {
1192264Sjacobs 			bufferok = 0;
1202264Sjacobs 		}
1212264Sjacobs 	}
1222264Sjacobs 
1232264Sjacobs 	if (!bufferok) {
1242264Sjacobs 		(void) memset(buf, NULL, buflen);
1252264Sjacobs 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
1262264Sjacobs 		return (NULL);
1272264Sjacobs 	}
1282264Sjacobs 
1292264Sjacobs 	if (strlen(buf) == len) {	/* there were no attributes */
1302264Sjacobs 		(void) memset(buf, NULL, buflen);
1312264Sjacobs 		buf = NULL;
1322264Sjacobs 	}
1332264Sjacobs 
1342264Sjacobs 	return (buf);
1352264Sjacobs }
1362264Sjacobs 
1372264Sjacobs 
1382264Sjacobs ns_printer_t *
_cvt_nss_entry_to_printer(char * entry,char * ns)1392264Sjacobs _cvt_nss_entry_to_printer(char *entry, char *ns)
1402264Sjacobs {
1412264Sjacobs 	char	*name = NULL,
1422264Sjacobs 		*key = NULL,
1432264Sjacobs 		**aliases = NULL,
1442264Sjacobs 		*cp,
1452264Sjacobs 		buf[BUFSIZ];
1462264Sjacobs 	int in_namelist = 1, buf_pos = 0;
1472264Sjacobs 	ns_printer_t *printer = NULL;
1482264Sjacobs 
1492264Sjacobs 	if (entry == NULL)
1502264Sjacobs 		return (NULL);
1512264Sjacobs 
1522264Sjacobs 	(void) memset(buf, NULL, sizeof (buf));
1532264Sjacobs 	for (cp = entry; *cp != NULL; cp++) {
1542264Sjacobs 		switch (*cp) {
1552264Sjacobs 		case ':':	/* end of kvp */
1562264Sjacobs 			if (in_namelist != 0) {
1572264Sjacobs 				if (name == NULL)
1582264Sjacobs 					name = strdup(buf);
1592264Sjacobs 				else
1602264Sjacobs 					aliases = (char **)list_append(
1612264Sjacobs 							(void **)aliases,
1622264Sjacobs 							(void *)strdup(buf));
1632264Sjacobs 				printer = (ns_printer_t *)ns_printer_create(
1642264Sjacobs 						name, aliases, ns, NULL);
1652264Sjacobs 				in_namelist = 0;
1662264Sjacobs 			} else if (key != NULL)
1672264Sjacobs 				(void) ns_set_value_from_string(key, buf,
1682264Sjacobs 				    printer);
1692264Sjacobs 			(void) memset(buf, NULL, sizeof (buf));
1702264Sjacobs 			buf_pos = 0;
1712264Sjacobs 			key = NULL;
1722264Sjacobs 			break;
1732264Sjacobs 		case '=':	/* kvp seperator */
1742264Sjacobs 			if (key == NULL) {
1752264Sjacobs 				key = strdup(buf);
1762264Sjacobs 				(void) memset(buf, NULL, sizeof (buf));
1772264Sjacobs 				buf_pos = 0;
1782264Sjacobs 			} else
1792264Sjacobs 				buf[buf_pos++] = *cp;
1802264Sjacobs 			break;
1812264Sjacobs 		case '|':	/* namelist seperator */
1822264Sjacobs 			if (in_namelist != 0) {
1832264Sjacobs 				if (name == NULL)
1842264Sjacobs 					name = strdup(buf);
1852264Sjacobs 				else
1862264Sjacobs 					aliases = (char **)list_append(
1872264Sjacobs 							(void **)aliases,
1882264Sjacobs 							(void *)strdup(buf));
1892264Sjacobs 				(void) memset(buf, NULL, sizeof (buf));
1902264Sjacobs 				buf_pos = 0;
1912264Sjacobs 			} else	/* add it to the buffer */
1922264Sjacobs 				buf[buf_pos++] = *cp;
1932264Sjacobs 			break;
1942264Sjacobs 		case '\\':	/* escape char */
1952264Sjacobs 			buf[buf_pos++] = *(++cp);
1962264Sjacobs 			break;
1972264Sjacobs 		default:
1982264Sjacobs 			buf[buf_pos++] = *cp;
1992264Sjacobs 		}
2002264Sjacobs 
2012264Sjacobs 	}
2022264Sjacobs 
2032264Sjacobs 	if (key != NULL)
2042264Sjacobs 		(void) ns_set_value_from_string(key, buf, printer);
2052264Sjacobs 
2062264Sjacobs 	return (printer);
2072264Sjacobs }
208