10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2571Sjacobs  * Common Development and Distribution License (the "License").
6*2571Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
220Sstevel@tonic-gate  *	files/printers_getbyname.c -- "files" backend for
230Sstevel@tonic-gate  *	nsswitch "printers" database.
240Sstevel@tonic-gate  *
25*2571Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate static const char *printers = "/etc/printers.conf";
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma weak _nss_files__printers_constr = _nss_files_printers_constr
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "files_common.h"
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <strings.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static nss_status_t _nss_files_XY_printers(files_backend_ptr_t,
400Sstevel@tonic-gate 	nss_XbyY_args_t *, const char *);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static nss_status_t
440Sstevel@tonic-gate getent(be, a)
450Sstevel@tonic-gate 	files_backend_ptr_t	be;
460Sstevel@tonic-gate 	void			*a;
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	nss_XbyY_args_t	 *args = (nss_XbyY_args_t *)a;
490Sstevel@tonic-gate 
50*2571Sjacobs 	return (_nss_files_XY_all(be, args, 0, 0, 0));
510Sstevel@tonic-gate }
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static nss_status_t
550Sstevel@tonic-gate getbyname(be, a)
560Sstevel@tonic-gate 	files_backend_ptr_t	be;
570Sstevel@tonic-gate 	void			*a;
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
600Sstevel@tonic-gate 	nss_status_t		res;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	/* printers_getbyname() has not set/endent; rewind on each call */
630Sstevel@tonic-gate 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
640Sstevel@tonic-gate 		return (res);
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 	return (_nss_files_XY_printers(be, argp, argp->key.name));
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static files_backend_op_t printers_ops[] = {
700Sstevel@tonic-gate 	_nss_files_destr,
710Sstevel@tonic-gate 	_nss_files_endent,
720Sstevel@tonic-gate 	_nss_files_setent,
730Sstevel@tonic-gate 	getent,
740Sstevel@tonic-gate 	getbyname
750Sstevel@tonic-gate };
760Sstevel@tonic-gate 
770Sstevel@tonic-gate nss_backend_t *
780Sstevel@tonic-gate _nss_files_printers_constr(dummy1, dummy2, dummy3)
790Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	return (_nss_files_constr(printers_ops,
820Sstevel@tonic-gate 		sizeof (printers_ops) / sizeof (printers_ops[0]),
830Sstevel@tonic-gate 		printers,
840Sstevel@tonic-gate 		NSS_LINELEN_PRINTERS,
850Sstevel@tonic-gate 		NULL));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * printers has the hostname as part of the data in the file, but the other
900Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
910Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate static nss_status_t
940Sstevel@tonic-gate _nss_files_XY_printers(be, args, filter)
950Sstevel@tonic-gate 	files_backend_ptr_t	be;
960Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
970Sstevel@tonic-gate 	const char		*filter;
980Sstevel@tonic-gate 			/*
990Sstevel@tonic-gate 			 * filter not useful here since the key
1000Sstevel@tonic-gate 			 * we are looking for is the first "word"
1010Sstevel@tonic-gate 			 * on the line and we can be fast enough.
1020Sstevel@tonic-gate 			 */
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	nss_status_t		res;
1050Sstevel@tonic-gate 	int	parsestat;
1060Sstevel@tonic-gate 	int namelen;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (be->buf == 0 &&
1090Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
1100Sstevel@tonic-gate 		(void) _nss_files_endent(be, 0);
1110Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1150Sstevel@tonic-gate 	namelen = strlen(args->key.name);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	while (1) {
1180Sstevel@tonic-gate 		char		*instr	= be->buf;
1190Sstevel@tonic-gate 		char		*p, *limit;
1200Sstevel@tonic-gate 		int		linelen;
1210Sstevel@tonic-gate 		int		found = 0;
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 		/*
1240Sstevel@tonic-gate 		 * _nss_files_read_line does process the '\' that are used
1250Sstevel@tonic-gate 		 * in /etc/printers.conf for continuation and gives one long
1260Sstevel@tonic-gate 		 * buffer.
1270Sstevel@tonic-gate 		 *
1280Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1290Sstevel@tonic-gate 		 */
1300Sstevel@tonic-gate 		if ((linelen = _nss_files_read_line(be->f, instr,
1310Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1320Sstevel@tonic-gate 			/* End of file */
1330Sstevel@tonic-gate 			args->returnval = 0;
1340Sstevel@tonic-gate 			args->erange    = 0;
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		}
1370Sstevel@tonic-gate 		p = instr;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 		if (*p == '#')					/* comment */
1400Sstevel@tonic-gate 			continue;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		/*
1430Sstevel@tonic-gate 		 * find the name in the namelist a|b|c...:
1440Sstevel@tonic-gate 		 */
1450Sstevel@tonic-gate 		if ((limit = strchr(instr, ':')) == NULL)	/* bad line */
1460Sstevel@tonic-gate 			continue;
1470Sstevel@tonic-gate 		while ((p < limit) && (found == 0)) {
1480Sstevel@tonic-gate 			if ((strncmp(p, args->key.name, namelen) == 0) &&
1490Sstevel@tonic-gate 			    ((*(p+namelen) == '|') || (*(p+namelen) == ':')))
1500Sstevel@tonic-gate 				found++;
1510Sstevel@tonic-gate 			else {
1520Sstevel@tonic-gate 				if ((p = strchr(p, '|')) == NULL)
1530Sstevel@tonic-gate 					p = limit;
1540Sstevel@tonic-gate 				else	/* skip the '|' */
1550Sstevel@tonic-gate 					p++;
1560Sstevel@tonic-gate 			}
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 		if (found == 0)
1590Sstevel@tonic-gate 			continue;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 		p = instr;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
1640Sstevel@tonic-gate 			args->erange = 1;
1650Sstevel@tonic-gate 			break;
1660Sstevel@tonic-gate 		}
1670Sstevel@tonic-gate 		(void) memcpy(args->buf.buffer, p, linelen);
1680Sstevel@tonic-gate 		args->buf.buffer[linelen] = '\0';
1690Sstevel@tonic-gate 		args->returnval = args->buf.result;
1700Sstevel@tonic-gate 		res = NSS_SUCCESS;
1710Sstevel@tonic-gate 		break;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	(void) _nss_files_endent(be, 0);
1740Sstevel@tonic-gate 	return (res);
1750Sstevel@tonic-gate }
176