17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
586b1a8baSrotondo  * Common Development and Distribution License (the "License").
686b1a8baSrotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22c1ecd8b9Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2386b1a8baSrotondo  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains routines necessary to convert a string buffer into
287c478bd9Sstevel@tonic-gate  * a printer object.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <stdarg.h>
377c478bd9Sstevel@tonic-gate #include <syslog.h>
387c478bd9Sstevel@tonic-gate 
39355b4669Sjacobs #include <ns.h>
40355b4669Sjacobs #include <list.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	ESCAPE_CHARS	"\\\n=:"	/* \, \n, =, : */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Just like strncat(3C), but escapes the supplied characters.
467c478bd9Sstevel@tonic-gate  * This allows the escape character '\' and seperators to be part of the
477c478bd9Sstevel@tonic-gate  * keys or values.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate char *
strncat_escaped(char * d,char * s,int len,char * escape)507c478bd9Sstevel@tonic-gate strncat_escaped(char *d, char *s, int len, char *escape)
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate 	char *t = d;
537c478bd9Sstevel@tonic-gate 
54*f4593de7SToomas Soome 	while ((*t != '\0') && (len > 0))
557c478bd9Sstevel@tonic-gate 		len--, t++;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if (escape == NULL)
587c478bd9Sstevel@tonic-gate 		escape = "\\";
597c478bd9Sstevel@tonic-gate 
60*f4593de7SToomas Soome 	while ((*s != '\0') && (len > 0)) {
617c478bd9Sstevel@tonic-gate 		if (strchr(escape, *s) != NULL)
627c478bd9Sstevel@tonic-gate 			len--, *t++ = '\\';
637c478bd9Sstevel@tonic-gate 		len--, *t++ = *s++;
647c478bd9Sstevel@tonic-gate 	}
65*f4593de7SToomas Soome 	*t = '\0';
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	return (d);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate char *
_cvt_printer_to_entry(ns_printer_t * printer,char * buf,int buflen)737c478bd9Sstevel@tonic-gate _cvt_printer_to_entry(ns_printer_t *printer, char *buf, int buflen)
747c478bd9Sstevel@tonic-gate {
757c478bd9Sstevel@tonic-gate 	int i, len;
767c478bd9Sstevel@tonic-gate 	int bufferok = 1;
777c478bd9Sstevel@tonic-gate 
78*f4593de7SToomas Soome 	(void) memset(buf, 0, buflen);
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if ((printer == NULL) || (printer->attributes == NULL))
817c478bd9Sstevel@tonic-gate 		return (NULL);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (snprintf(buf, buflen, "%s", printer->name) >= buflen) {
84*f4593de7SToomas Soome 		(void) memset(buf, 0, buflen);
857c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
867c478bd9Sstevel@tonic-gate 		return (NULL);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if ((printer->aliases != NULL) && (printer->aliases[0] != NULL)) {
907c478bd9Sstevel@tonic-gate 		char **alias = printer->aliases;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		while (*alias != NULL) {
9386b1a8baSrotondo 			(void) strlcat(buf, "|", buflen);
9486b1a8baSrotondo 			(void) strncat_escaped(buf, *alias++, buflen,
9586b1a8baSrotondo 			    ESCAPE_CHARS);
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (strlcat(buf, ":", buflen) >= buflen) {
100*f4593de7SToomas Soome 		(void) memset(buf, 0, buflen);
1017c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
1027c478bd9Sstevel@tonic-gate 		return (NULL);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	len = strlen(buf);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	for (i = 0; printer->attributes[i] != NULL && bufferok; i++) {
1087c478bd9Sstevel@tonic-gate 		ns_kvp_t *kvp = printer->attributes[i];
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		if (kvp->value == NULL)
1117c478bd9Sstevel@tonic-gate 			continue;
11286b1a8baSrotondo 		(void) strlcat(buf, "\\\n\t:", buflen);
11386b1a8baSrotondo 		(void) strncat_escaped(buf, kvp->key, buflen, ESCAPE_CHARS);
11486b1a8baSrotondo 		(void) strlcat(buf, "=", buflen);
11586b1a8baSrotondo 		(void) strncat_escaped(buf, kvp->value, buflen, ESCAPE_CHARS);
1167c478bd9Sstevel@tonic-gate 		if (strlcat(buf, ":", buflen) >= buflen) {
1177c478bd9Sstevel@tonic-gate 			bufferok = 0;
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!bufferok) {
122*f4593de7SToomas Soome 		(void) memset(buf, 0, buflen);
1237c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "_cvt_printer_to_entry: buffer overflow");
1247c478bd9Sstevel@tonic-gate 		return (NULL);
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if (strlen(buf) == len) {	/* there were no attributes */
128*f4593de7SToomas Soome 		(void) memset(buf, 0, buflen);
1297c478bd9Sstevel@tonic-gate 		buf = NULL;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	return (buf);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate ns_printer_t *
_cvt_nss_entry_to_printer(char * entry,char * ns)1377c478bd9Sstevel@tonic-gate _cvt_nss_entry_to_printer(char *entry, char *ns)
1387c478bd9Sstevel@tonic-gate {
139*f4593de7SToomas Soome 	char *name = NULL, *key = NULL, **aliases = NULL, *cp, buf[BUFSIZ];
1407c478bd9Sstevel@tonic-gate 	int in_namelist = 1, buf_pos = 0;
1417c478bd9Sstevel@tonic-gate 	ns_printer_t *printer = NULL;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	if (entry == NULL)
1447c478bd9Sstevel@tonic-gate 		return (NULL);
1457c478bd9Sstevel@tonic-gate 
146*f4593de7SToomas Soome 	(void) memset(buf, 0, sizeof (buf));
147*f4593de7SToomas Soome 	for (cp = entry; *cp != '\0'; cp++) {
1487c478bd9Sstevel@tonic-gate 		switch (*cp) {
1497c478bd9Sstevel@tonic-gate 		case ':':	/* end of kvp */
1507c478bd9Sstevel@tonic-gate 			if (in_namelist != 0) {
1517c478bd9Sstevel@tonic-gate 				if (name == NULL)
1527c478bd9Sstevel@tonic-gate 					name = strdup(buf);
1537c478bd9Sstevel@tonic-gate 				else
1547c478bd9Sstevel@tonic-gate 					aliases = (char **)list_append(
155*f4593de7SToomas Soome 					    (void **)aliases,
156*f4593de7SToomas Soome 					    (void *)strdup(buf));
1577c478bd9Sstevel@tonic-gate 				printer = (ns_printer_t *)ns_printer_create(
158*f4593de7SToomas Soome 				    name, aliases, ns, NULL);
1597c478bd9Sstevel@tonic-gate 				in_namelist = 0;
160*f4593de7SToomas Soome 			} else if (key != NULL) {
16186b1a8baSrotondo 				(void) ns_set_value_from_string(key, buf,
16286b1a8baSrotondo 				    printer);
163*f4593de7SToomas Soome 			}
164*f4593de7SToomas Soome 			(void) memset(buf, 0, sizeof (buf));
1657c478bd9Sstevel@tonic-gate 			buf_pos = 0;
1667c478bd9Sstevel@tonic-gate 			key = NULL;
1677c478bd9Sstevel@tonic-gate 			break;
1687c478bd9Sstevel@tonic-gate 		case '=':	/* kvp seperator */
1697c478bd9Sstevel@tonic-gate 			if (key == NULL) {
1707c478bd9Sstevel@tonic-gate 				key = strdup(buf);
171*f4593de7SToomas Soome 				(void) memset(buf, 0, sizeof (buf));
1727c478bd9Sstevel@tonic-gate 				buf_pos = 0;
173*f4593de7SToomas Soome 			} else {
1747c478bd9Sstevel@tonic-gate 				buf[buf_pos++] = *cp;
175*f4593de7SToomas Soome 			}
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		case '|':	/* namelist seperator */
1787c478bd9Sstevel@tonic-gate 			if (in_namelist != 0) {
1797c478bd9Sstevel@tonic-gate 				if (name == NULL)
1807c478bd9Sstevel@tonic-gate 					name = strdup(buf);
1817c478bd9Sstevel@tonic-gate 				else
1827c478bd9Sstevel@tonic-gate 					aliases = (char **)list_append(
183*f4593de7SToomas Soome 					    (void **)aliases,
184*f4593de7SToomas Soome 					    (void *)strdup(buf));
185*f4593de7SToomas Soome 				(void) memset(buf, 0, sizeof (buf));
1867c478bd9Sstevel@tonic-gate 				buf_pos = 0;
187*f4593de7SToomas Soome 			} else {
188*f4593de7SToomas Soome 				/* add it to the buffer */
1897c478bd9Sstevel@tonic-gate 				buf[buf_pos++] = *cp;
190*f4593de7SToomas Soome 			}
1917c478bd9Sstevel@tonic-gate 			break;
1927c478bd9Sstevel@tonic-gate 		case '\\':	/* escape char */
1937c478bd9Sstevel@tonic-gate 			buf[buf_pos++] = *(++cp);
1947c478bd9Sstevel@tonic-gate 			break;
1957c478bd9Sstevel@tonic-gate 		default:
1967c478bd9Sstevel@tonic-gate 			buf[buf_pos++] = *cp;
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	if (key != NULL)
20286b1a8baSrotondo 		(void) ns_set_value_from_string(key, buf, printer);
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	return (printer);
2057c478bd9Sstevel@tonic-gate }
206