xref: /titanic_41/usr/src/lib/nsswitch/files/common/getprinter.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  *	files/printers_getbyname.c -- "files" backend for
24  *	nsswitch "printers" database.
25  *
26  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 static const char *printers = "/etc/printers.conf";
33 
34 #pragma weak _nss_files__printers_constr = _nss_files_printers_constr
35 
36 #include "files_common.h"
37 #include <stdlib.h>
38 #include <strings.h>
39 
40 static nss_status_t _nss_files_XY_printers(files_backend_ptr_t,
41 	nss_XbyY_args_t *, const char *);
42 
43 
44 static nss_status_t
45 getent(be, a)
46 	files_backend_ptr_t	be;
47 	void			*a;
48 {
49 	nss_XbyY_args_t	 *args = (nss_XbyY_args_t *)a;
50 
51 	return (_nss_files_XY_all(be, args, 1, 0, 0));
52 }
53 
54 
55 static nss_status_t
56 getbyname(be, a)
57 	files_backend_ptr_t	be;
58 	void			*a;
59 {
60 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
61 	nss_status_t		res;
62 
63 	/* printers_getbyname() has not set/endent; rewind on each call */
64 	if ((res = _nss_files_setent(be, 0)) != NSS_SUCCESS) {
65 		return (res);
66 	}
67 	return (_nss_files_XY_printers(be, argp, argp->key.name));
68 }
69 
70 static files_backend_op_t printers_ops[] = {
71 	_nss_files_destr,
72 	_nss_files_endent,
73 	_nss_files_setent,
74 	getent,
75 	getbyname
76 };
77 
78 nss_backend_t *
79 _nss_files_printers_constr(dummy1, dummy2, dummy3)
80 	const char	*dummy1, *dummy2, *dummy3;
81 {
82 	return (_nss_files_constr(printers_ops,
83 		sizeof (printers_ops) / sizeof (printers_ops[0]),
84 		printers,
85 		NSS_LINELEN_PRINTERS,
86 		NULL));
87 }
88 
89 /*
90  * printers has the hostname as part of the data in the file, but the other
91  * backends don't include it in the data passed to the backend.  For this
92  * reason, we process everything here and don't bother calling the backend.
93  */
94 static nss_status_t
95 _nss_files_XY_printers(be, args, filter)
96 	files_backend_ptr_t	be;
97 	nss_XbyY_args_t		*args;
98 	const char		*filter;
99 			/*
100 			 * filter not useful here since the key
101 			 * we are looking for is the first "word"
102 			 * on the line and we can be fast enough.
103 			 */
104 {
105 	nss_status_t		res;
106 	int	parsestat;
107 	int namelen;
108 
109 	if (be->buf == 0 &&
110 		(be->buf = (char *)malloc(be->minbuf)) == 0) {
111 		(void) _nss_files_endent(be, 0);
112 		return (NSS_UNAVAIL); /* really panic, malloc failed */
113 	}
114 
115 	res = NSS_NOTFOUND;
116 	namelen = strlen(args->key.name);
117 
118 	while (1) {
119 		char		*instr	= be->buf;
120 		char		*p, *limit;
121 		int		linelen;
122 		int		found = 0;
123 
124 		/*
125 		 * _nss_files_read_line does process the '\' that are used
126 		 * in /etc/printers.conf for continuation and gives one long
127 		 * buffer.
128 		 *
129 		 * linelen counts the characters up to but excluding the '\n'
130 		 */
131 		if ((linelen = _nss_files_read_line(be->f, instr,
132 		    be->minbuf)) < 0) {
133 			/* End of file */
134 			args->returnval = 0;
135 			args->erange    = 0;
136 			break;
137 		}
138 		p = instr;
139 
140 		if (*p == '#')					/* comment */
141 			continue;
142 
143 		/*
144 		 * find the name in the namelist a|b|c...:
145 		 */
146 		if ((limit = strchr(instr, ':')) == NULL)	/* bad line */
147 			continue;
148 		while ((p < limit) && (found == 0)) {
149 			if ((strncmp(p, args->key.name, namelen) == 0) &&
150 			    ((*(p+namelen) == '|') || (*(p+namelen) == ':')))
151 				found++;
152 			else {
153 				if ((p = strchr(p, '|')) == NULL)
154 					p = limit;
155 				else	/* skip the '|' */
156 					p++;
157 			}
158 		}
159 		if (found == 0)
160 			continue;
161 
162 		p = instr;
163 
164 		if (args->buf.buflen <= linelen) {	/* not enough buffer */
165 			args->erange = 1;
166 			break;
167 		}
168 		(void) memcpy(args->buf.buffer, p, linelen);
169 		args->buf.buffer[linelen] = '\0';
170 		args->returnval = args->buf.result;
171 		res = NSS_SUCCESS;
172 		break;
173 	}
174 	(void) _nss_files_endent(be, 0);
175 	return (res);
176 }
177