xref: /onnv-gate/usr/src/lib/nsswitch/user/common/getprinter.c (revision 2830:5228d1267a01)
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*2830Sdjl  * Common Development and Distribution License (the "License").
6*2830Sdjl  * 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 /*
22*2830Sdjl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * "user" backend for nsswitch "printers" database.  This module implements
260Sstevel@tonic-gate  * the ${HOME}/.printers naming support.  This file provides users with a
270Sstevel@tonic-gate  * convenient method of aliasing and specifying an interest list.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma weak _nss_user__printers_constr = _nss_user_printers_constr
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <nss_dbdefs.h>
350Sstevel@tonic-gate #include "user_common.h"
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <stdlib.h>
380Sstevel@tonic-gate #include <ctype.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate static nss_status_t
_nss_user_printers_convert(char * entry,nss_XbyY_args_t * args)410Sstevel@tonic-gate _nss_user_printers_convert(char *entry, nss_XbyY_args_t *args)
420Sstevel@tonic-gate {
430Sstevel@tonic-gate 	nss_status_t		res = NSS_NOTFOUND;
440Sstevel@tonic-gate 	char 			*namelist = entry;
450Sstevel@tonic-gate 	char			*key = NULL;
460Sstevel@tonic-gate 	char			*value = NULL;
470Sstevel@tonic-gate 	int			length = 0;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	if ((value = strpbrk(entry, "\t ")) != NULL) {
500Sstevel@tonic-gate 		*value = NULL; value++;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 		while ((*value != NULL) && (isspace(*value) != 0))
530Sstevel@tonic-gate 			value++;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 		if ((key = strpbrk(value, "\n\t ")) != NULL)
560Sstevel@tonic-gate 			*key = NULL;
570Sstevel@tonic-gate 	}
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	args->buf.buffer[0] = NULL;
600Sstevel@tonic-gate 	if ((value == NULL) || (*value == NULL)) {	/* bad value */
610Sstevel@tonic-gate 		args->erange = 1;
620Sstevel@tonic-gate 		return (res);
630Sstevel@tonic-gate 	}
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	if (strcmp(namelist, "_all") == 0)
660Sstevel@tonic-gate 		key = "all";
670Sstevel@tonic-gate 	else
680Sstevel@tonic-gate 		key = "use";
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	length = snprintf(args->buf.buffer, args->buf.buflen, "%s:%s=",
710Sstevel@tonic-gate 			namelist, key);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	/* append the value  ':' must be escaped for posix style names */
740Sstevel@tonic-gate 	while ((length < args->buf.buflen) && (*value != NULL)) {
750Sstevel@tonic-gate 		if (*value == ':')
760Sstevel@tonic-gate 			args->buf.buffer[length++] = '\\';
770Sstevel@tonic-gate 		args->buf.buffer[length++] = *value++;
780Sstevel@tonic-gate 	}
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	if (length >= args->buf.buflen) {	/* the value was too big */
810Sstevel@tonic-gate 		args->erange = 1;
820Sstevel@tonic-gate 		return (res);
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	args->buf.buffer[length] = NULL;	/* terminate, just in case */
860Sstevel@tonic-gate 	args->returnval = args->buf.result;
870Sstevel@tonic-gate 	res = NSS_SUCCESS;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	return (res);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate 
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * printers has the hostname as part of the data in the file, but the other
940Sstevel@tonic-gate  * backends don't include it in the data passed to the backend.  For this
950Sstevel@tonic-gate  * reason, we process everything here and don't bother calling the backend.
960Sstevel@tonic-gate  */
97*2830Sdjl /*ARGSUSED*/
980Sstevel@tonic-gate static nss_status_t
_nss_user_XY_printers(be,args,filter)990Sstevel@tonic-gate _nss_user_XY_printers(be, args, filter)
1000Sstevel@tonic-gate 	user_backend_ptr_t	be;
1010Sstevel@tonic-gate 	nss_XbyY_args_t		*args;
1020Sstevel@tonic-gate 	const char		*filter;
1030Sstevel@tonic-gate 			/*
1040Sstevel@tonic-gate 			 * filter not useful here since the key
1050Sstevel@tonic-gate 			 * we are looking for is the first "word"
1060Sstevel@tonic-gate 			 * on the line and we can be fast enough.
1070Sstevel@tonic-gate 			 */
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate 	nss_status_t		res;
1100Sstevel@tonic-gate 	int namelen;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	if (be->buf == 0 &&
1130Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
1140Sstevel@tonic-gate 		(void) _nss_user_endent(be, 0);
1150Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1190Sstevel@tonic-gate 	namelen = strlen(args->key.name);
1200Sstevel@tonic-gate 
121*2830Sdjl 	/*CONSTCOND*/
1220Sstevel@tonic-gate 	while (1) {
1230Sstevel@tonic-gate 		char		*instr	= be->buf;
1240Sstevel@tonic-gate 		char		*p, *limit;
1250Sstevel@tonic-gate 		int		linelen;
1260Sstevel@tonic-gate 		int		found = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		/*
1290Sstevel@tonic-gate 		 * _nss_user_read_line does process the '\' that are used
1300Sstevel@tonic-gate 		 * in /etc/printers.conf for continuation and gives one long
1310Sstevel@tonic-gate 		 * buffer.
1320Sstevel@tonic-gate 		 *
1330Sstevel@tonic-gate 		 * linelen counts the characters up to but excluding the '\n'
1340Sstevel@tonic-gate 		 */
1350Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
1360Sstevel@tonic-gate 		    be->minbuf)) < 0) {
1370Sstevel@tonic-gate 			/* End of file */
1380Sstevel@tonic-gate 			args->returnval = 0;
1390Sstevel@tonic-gate 			args->erange    = 0;
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 		p = instr;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		if (*p == '#')					/* comment */
1450Sstevel@tonic-gate 			continue;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		/*
1480Sstevel@tonic-gate 		 * find the name in the namelist a|b|c...:
1490Sstevel@tonic-gate 		 */
1500Sstevel@tonic-gate 		if ((limit = strpbrk(instr, "\t ")) == NULL)	/* bad line */
1510Sstevel@tonic-gate 			continue;
1520Sstevel@tonic-gate 		while ((p < limit) && (found == 0)) {
1530Sstevel@tonic-gate 			if ((strncmp(p, args->key.name, namelen) == 0) &&
1540Sstevel@tonic-gate 			    ((*(p+namelen) == '|') ||
1550Sstevel@tonic-gate 			    (isspace(*(p+namelen)) != 0)))
1560Sstevel@tonic-gate 				found++;
1570Sstevel@tonic-gate 			else {
1580Sstevel@tonic-gate 				if ((p = strchr(p, '|')) == NULL)
1590Sstevel@tonic-gate 					p = limit;
1600Sstevel@tonic-gate 				else	/* skip the '|' */
1610Sstevel@tonic-gate 					p++;
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 		}
1640Sstevel@tonic-gate 		if (found == 0)
1650Sstevel@tonic-gate 			continue;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 		if ((res = _nss_user_printers_convert(be->buf, args))
1680Sstevel@tonic-gate 		    == NSS_SUCCESS)
1690Sstevel@tonic-gate 			break;
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	(void) _nss_user_endent(be, 0);
1720Sstevel@tonic-gate 	return (res);
1730Sstevel@tonic-gate }
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static nss_status_t
getent(be,a)1760Sstevel@tonic-gate getent(be, a)
1770Sstevel@tonic-gate 	user_backend_ptr_t	be;
1780Sstevel@tonic-gate 	void			*a;
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate 	nss_XbyY_args_t		*args = (nss_XbyY_args_t *)a;
1810Sstevel@tonic-gate 	nss_status_t		res = NSS_UNAVAIL;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (be->buf == 0 &&
1840Sstevel@tonic-gate 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
1850Sstevel@tonic-gate 		return (NSS_UNAVAIL); /* really panic, malloc failed */
1860Sstevel@tonic-gate 	}
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (be->f == 0) {
1890Sstevel@tonic-gate 		if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
1900Sstevel@tonic-gate 			return (res);
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 	}
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	res = NSS_NOTFOUND;
1950Sstevel@tonic-gate 
196*2830Sdjl 	/*CONSTCOND*/
1970Sstevel@tonic-gate 	while (1) {
1980Sstevel@tonic-gate 		char	*instr  = be->buf;
1990Sstevel@tonic-gate 		int	linelen;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		if ((linelen = _nss_user_read_line(be->f, instr,
2020Sstevel@tonic-gate 		    be->minbuf)) < 0) {
2030Sstevel@tonic-gate 			/* End of file */
2040Sstevel@tonic-gate 			args->returnval = 0;
2050Sstevel@tonic-gate 			args->erange    = 0;
2060Sstevel@tonic-gate 			break;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		if (*(be->buf) == '#')				/* comment */
2100Sstevel@tonic-gate 			continue;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		if ((res = _nss_user_printers_convert(be->buf, args))
2130Sstevel@tonic-gate 		    == NSS_SUCCESS)
2140Sstevel@tonic-gate 			break;
2150Sstevel@tonic-gate 	}
2160Sstevel@tonic-gate 	return (res);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate static nss_status_t
getbyname(be,a)2210Sstevel@tonic-gate getbyname(be, a)
2220Sstevel@tonic-gate 	user_backend_ptr_t	be;
2230Sstevel@tonic-gate 	void			*a;
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
2260Sstevel@tonic-gate 	nss_status_t		res;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	/* printers_getbyname() has not set/endent; rewind on each call */
2290Sstevel@tonic-gate 	if ((res = _nss_user_setent(be, 0)) != NSS_SUCCESS) {
2300Sstevel@tonic-gate 		return (res);
2310Sstevel@tonic-gate 	}
2320Sstevel@tonic-gate 	return (_nss_user_XY_printers(be, argp, argp->key.name));
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate static user_backend_op_t printers_ops[] = {
2360Sstevel@tonic-gate 	_nss_user_destr,
2370Sstevel@tonic-gate 	_nss_user_endent,
2380Sstevel@tonic-gate 	_nss_user_setent,
2390Sstevel@tonic-gate 	getent,
2400Sstevel@tonic-gate 	getbyname
2410Sstevel@tonic-gate };
2420Sstevel@tonic-gate 
243*2830Sdjl /*ARGSUSED*/
2440Sstevel@tonic-gate nss_backend_t *
_nss_user_printers_constr(dummy1,dummy2,dummy3)2450Sstevel@tonic-gate _nss_user_printers_constr(dummy1, dummy2, dummy3)
2460Sstevel@tonic-gate 	const char	*dummy1, *dummy2, *dummy3;
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	char path[MAXPATHLEN], *home;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if ((home = getenv("HOME")) == NULL)
2510Sstevel@tonic-gate 		home = "";
252*2830Sdjl 	(void) snprintf(path, sizeof (path), "%s/.printers", home);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	return (_nss_user_constr(printers_ops,
2550Sstevel@tonic-gate 		sizeof (printers_ops) / sizeof (printers_ops[0]),
2560Sstevel@tonic-gate 		path, NSS_LINELEN_PRINTERS));
2570Sstevel@tonic-gate }
258