xref: /titanic_44/usr/src/lib/nsswitch/user/common/getprinter.c (revision cb5caa98562cf06753163f558cbcfe30b8f4673a)
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
5*cb5caa98Sdjl  * Common Development and Distribution License (the "License").
6*cb5caa98Sdjl  * 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 /*
22*cb5caa98Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * "user" backend for nsswitch "printers" database.  This module implements
267c478bd9Sstevel@tonic-gate  * the ${HOME}/.printers naming support.  This file provides users with a
277c478bd9Sstevel@tonic-gate  * convenient method of aliasing and specifying an interest list.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma weak _nss_user__printers_constr = _nss_user_printers_constr
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <nss_dbdefs.h>
357c478bd9Sstevel@tonic-gate #include "user_common.h"
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <ctype.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static nss_status_t
_nss_user_printers_convert(char * entry,nss_XbyY_args_t * args)417c478bd9Sstevel@tonic-gate _nss_user_printers_convert(char *entry, nss_XbyY_args_t *args)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	nss_status_t		res = NSS_NOTFOUND;
447c478bd9Sstevel@tonic-gate 	char 			*namelist = entry;
457c478bd9Sstevel@tonic-gate 	char			*key = NULL;
467c478bd9Sstevel@tonic-gate 	char			*value = NULL;
477c478bd9Sstevel@tonic-gate 	int			length = 0;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if ((value = strpbrk(entry, "\t ")) != NULL) {
507c478bd9Sstevel@tonic-gate 		*value = NULL; value++;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 		while ((*value != NULL) && (isspace(*value) != 0))
537c478bd9Sstevel@tonic-gate 			value++;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 		if ((key = strpbrk(value, "\n\t ")) != NULL)
567c478bd9Sstevel@tonic-gate 			*key = NULL;
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	args->buf.buffer[0] = NULL;
607c478bd9Sstevel@tonic-gate 	if ((value == NULL) || (*value == NULL)) {	/* bad value */
617c478bd9Sstevel@tonic-gate 		args->erange = 1;
627c478bd9Sstevel@tonic-gate 		return (res);
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (strcmp(namelist, "_all") == 0)
667c478bd9Sstevel@tonic-gate 		key = "all";
677c478bd9Sstevel@tonic-gate 	else
687c478bd9Sstevel@tonic-gate 		key = "use";
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	length = snprintf(args->buf.buffer, args->buf.buflen, "%s:%s=",
717c478bd9Sstevel@tonic-gate 			namelist, key);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/* append the value  ':' must be escaped for posix style names */
747c478bd9Sstevel@tonic-gate 	while ((length < args->buf.buflen) && (*value != NULL)) {
757c478bd9Sstevel@tonic-gate 		if (*value == ':')
767c478bd9Sstevel@tonic-gate 			args->buf.buffer[length++] = '\\';
777c478bd9Sstevel@tonic-gate 		args->buf.buffer[length++] = *value++;
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (length >= args->buf.buflen) {	/* the value was too big */
817c478bd9Sstevel@tonic-gate 		args->erange = 1;
827c478bd9Sstevel@tonic-gate 		return (res);
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	args->buf.buffer[length] = NULL;	/* terminate, just in case */
867c478bd9Sstevel@tonic-gate 	args->returnval = args->buf.result;
877c478bd9Sstevel@tonic-gate 	res = NSS_SUCCESS;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	return (res);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
937c478bd9Sstevel@tonic-gate  * printers has the hostname as part of the data in the file, but the other
947c478bd9Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
957c478bd9Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
967c478bd9Sstevel@tonic-gate  */
97*cb5caa98Sdjl /*ARGSUSED*/
987c478bd9Sstevel@tonic-gate static nss_status_t
_nss_user_XY_printers(be,args,filter)997c478bd9Sstevel@tonic-gate _nss_user_XY_printers(be, args, filter)
1007c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
1017c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
1027c478bd9Sstevel@tonic-gate 	const char		*filter;
1037c478bd9Sstevel@tonic-gate 			/*
1047c478bd9Sstevel@tonic-gate 			 * filter not useful here since the key
1057c478bd9Sstevel@tonic-gate 			 * we are looking for is the first "word"
1067c478bd9Sstevel@tonic-gate 			 * on the line and we can be fast enough.
1077c478bd9Sstevel@tonic-gate 			 */
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 	nss_status_t		res;
1107c478bd9Sstevel@tonic-gate 	int namelen;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	if (be->buf == 0 &&
1137c478bd9Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
1147c478bd9Sstevel@tonic-gate 		(void) _nss_user_endent(be, 0);
1157c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1197c478bd9Sstevel@tonic-gate 	namelen = strlen(args->key.name);
1207c478bd9Sstevel@tonic-gate 
121*cb5caa98Sdjl 	/*CONSTCOND*/
1227c478bd9Sstevel@tonic-gate 	while (1) {
1237c478bd9Sstevel@tonic-gate 		char		*instr	= be->buf;
1247c478bd9Sstevel@tonic-gate 		char		*p, *limit;
1257c478bd9Sstevel@tonic-gate 		int		linelen;
1267c478bd9Sstevel@tonic-gate 		int		found = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		/*
1297c478bd9Sstevel@tonic-gate 		 * _nss_user_read_line does process the '\' that are used
1307c478bd9Sstevel@tonic-gate 		 * in /etc/printers.conf for continuation and gives one long
1317c478bd9Sstevel@tonic-gate 		 * buffer.
1327c478bd9Sstevel@tonic-gate 		 *
1337c478bd9Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
1367c478bd9Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1377c478bd9Sstevel@tonic-gate 			/* End of file */
1387c478bd9Sstevel@tonic-gate 			args->returnval = 0;
1397c478bd9Sstevel@tonic-gate 			args->erange    = 0;
1407c478bd9Sstevel@tonic-gate 			break;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 		p = instr;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		if (*p == '#')					/* comment */
1457c478bd9Sstevel@tonic-gate 			continue;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		/*
1487c478bd9Sstevel@tonic-gate 		 * find the name in the namelist a|b|c...:
1497c478bd9Sstevel@tonic-gate 		 */
1507c478bd9Sstevel@tonic-gate 		if ((limit = strpbrk(instr, "\t ")) == NULL)	/* bad line */
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		while ((p < limit) && (found == 0)) {
1537c478bd9Sstevel@tonic-gate 			if ((strncmp(p, args->key.name, namelen) == 0) &&
1547c478bd9Sstevel@tonic-gate 			    ((*(p+namelen) == '|') ||
1557c478bd9Sstevel@tonic-gate 			    (isspace(*(p+namelen)) != 0)))
1567c478bd9Sstevel@tonic-gate 				found++;
1577c478bd9Sstevel@tonic-gate 			else {
1587c478bd9Sstevel@tonic-gate 				if ((p = strchr(p, '|')) == NULL)
1597c478bd9Sstevel@tonic-gate 					p = limit;
1607c478bd9Sstevel@tonic-gate 				else	/* skip the '|' */
1617c478bd9Sstevel@tonic-gate 					p++;
1627c478bd9Sstevel@tonic-gate 			}
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		if (found == 0)
1657c478bd9Sstevel@tonic-gate 			continue;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if ((res = _nss_user_printers_convert(be->buf, args))
1687c478bd9Sstevel@tonic-gate 		    == NSS_SUCCESS)
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 	}
1717c478bd9Sstevel@tonic-gate 	(void) _nss_user_endent(be, 0);
1727c478bd9Sstevel@tonic-gate 	return (res);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static nss_status_t
getent(be,a)1767c478bd9Sstevel@tonic-gate getent(be, a)
1777c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
1787c478bd9Sstevel@tonic-gate 	void			*a;
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*args = (nss_XbyY_args_t *)a;
1817c478bd9Sstevel@tonic-gate 	nss_status_t		res = NSS_UNAVAIL;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	if (be->buf == 0 &&
1847c478bd9Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
1857c478bd9Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (be->f == 0) {
1897c478bd9Sstevel@tonic-gate 		if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
1907c478bd9Sstevel@tonic-gate 			return (res);
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1957c478bd9Sstevel@tonic-gate 
196*cb5caa98Sdjl 	/*CONSTCOND*/
1977c478bd9Sstevel@tonic-gate 	while (1) {
1987c478bd9Sstevel@tonic-gate 		char	*instr  = be->buf;
1997c478bd9Sstevel@tonic-gate 		int	linelen;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
2027c478bd9Sstevel@tonic-gate 		    be->minbuf)) < 0) {
2037c478bd9Sstevel@tonic-gate 			/* End of file */
2047c478bd9Sstevel@tonic-gate 			args->returnval = 0;
2057c478bd9Sstevel@tonic-gate 			args->erange    = 0;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		}
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		if (*(be->buf) == '#')				/* comment */
2107c478bd9Sstevel@tonic-gate 			continue;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if ((res = _nss_user_printers_convert(be->buf, args))
2137c478bd9Sstevel@tonic-gate 		    == NSS_SUCCESS)
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate 	return (res);
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate static nss_status_t
getbyname(be,a)2217c478bd9Sstevel@tonic-gate getbyname(be, a)
2227c478bd9Sstevel@tonic-gate 	user_backend_ptr_t	be;
2237c478bd9Sstevel@tonic-gate 	void			*a;
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
2267c478bd9Sstevel@tonic-gate 	nss_status_t		res;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	/* printers_getbyname() has not set/endent; rewind on each call */
2297c478bd9Sstevel@tonic-gate 	if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
2307c478bd9Sstevel@tonic-gate 		return (res);
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 	return (_nss_user_XY_printers(be, argp, argp->key.name));
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate static user_backend_op_t printers_ops[] = {
2367c478bd9Sstevel@tonic-gate 	_nss_user_destr,
2377c478bd9Sstevel@tonic-gate 	_nss_user_endent,
2387c478bd9Sstevel@tonic-gate 	_nss_user_setent,
2397c478bd9Sstevel@tonic-gate 	getent,
2407c478bd9Sstevel@tonic-gate 	getbyname
2417c478bd9Sstevel@tonic-gate };
2427c478bd9Sstevel@tonic-gate 
243*cb5caa98Sdjl /*ARGSUSED*/
2447c478bd9Sstevel@tonic-gate nss_backend_t *
_nss_user_printers_constr(dummy1,dummy2,dummy3)2457c478bd9Sstevel@tonic-gate _nss_user_printers_constr(dummy1, dummy2, dummy3)
2467c478bd9Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
2477c478bd9Sstevel@tonic-gate {
2487c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN], *home;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if ((home = getenv("HOME")) == NULL)
2517c478bd9Sstevel@tonic-gate 		home = "";
252*cb5caa98Sdjl 	(void) snprintf(path, sizeof (path), "%s/.printers", home);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	return (_nss_user_constr(printers_ops,
2557c478bd9Sstevel@tonic-gate 		sizeof (printers_ops) / sizeof (printers_ops[0]),
2567c478bd9Sstevel@tonic-gate 		path, NSS_LINELEN_PRINTERS));
2577c478bd9Sstevel@tonic-gate }
258