xref: /illumos-gate/usr/src/cmd/lp/lib/lp/files.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate #include "stdio.h"
35*7c478bd9Sstevel@tonic-gate #include "fcntl.h"
36*7c478bd9Sstevel@tonic-gate #include "string.h"
37*7c478bd9Sstevel@tonic-gate #include "errno.h"
38*7c478bd9Sstevel@tonic-gate #include "pwd.h"
39*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
40*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
41*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
42*7c478bd9Sstevel@tonic-gate #include <stdarg.h>
43*7c478bd9Sstevel@tonic-gate #include <unistd.h>
44*7c478bd9Sstevel@tonic-gate #include "pwd.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #include "lp.h"
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate int
49*7c478bd9Sstevel@tonic-gate is_printer_uri(char *value)
50*7c478bd9Sstevel@tonic-gate {
51*7c478bd9Sstevel@tonic-gate 	if (value == NULL)
52*7c478bd9Sstevel@tonic-gate 		return (-1);
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if ((value[0] == '/') && (access(value, F_OK) == 0))
55*7c478bd9Sstevel@tonic-gate 		return (-1); /* a valid path */
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if (strstr(value, "://") == NULL)
58*7c478bd9Sstevel@tonic-gate 		return (-1); /* not in uri form */
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	return (0);
61*7c478bd9Sstevel@tonic-gate }
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate fdprintf(int fd, char *fmt, ...)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	char    buf[BUFSIZ];
66*7c478bd9Sstevel@tonic-gate 	va_list ap;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 	if (fd == 1)
69*7c478bd9Sstevel@tonic-gate 		fflush(stdout);
70*7c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
71*7c478bd9Sstevel@tonic-gate 	vsnprintf(buf, sizeof (buf), fmt, ap);
72*7c478bd9Sstevel@tonic-gate 	va_end(ap);
73*7c478bd9Sstevel@tonic-gate 	return (Write(fd, buf, (int)strlen(buf)));
74*7c478bd9Sstevel@tonic-gate }
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate char *
77*7c478bd9Sstevel@tonic-gate fdgets(char *buf, int len, int fd)
78*7c478bd9Sstevel@tonic-gate {
79*7c478bd9Sstevel@tonic-gate 	char    tmp;
80*7c478bd9Sstevel@tonic-gate 	int	count = 0;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	memset(buf, NULL, len);
83*7c478bd9Sstevel@tonic-gate 	while ((count < len) && (Read(fd, &tmp, 1) > 0))
84*7c478bd9Sstevel@tonic-gate 		if ((buf[count++] = tmp) == '\n') break;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if (count != 0)
87*7c478bd9Sstevel@tonic-gate 		return (buf);
88*7c478bd9Sstevel@tonic-gate 	return (NULL);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate fdputs(char *buf, int fd)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	return (fdprintf(fd, "%s", buf));
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate fdputc(char c, int fd)
97*7c478bd9Sstevel@tonic-gate {
98*7c478bd9Sstevel@tonic-gate 	if (fd == 1)
99*7c478bd9Sstevel@tonic-gate 		fflush(stdout);
100*7c478bd9Sstevel@tonic-gate 	return (write(fd, &c, 1));
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate int
105*7c478bd9Sstevel@tonic-gate open_locked(char *path, char *type, mode_t mode)
106*7c478bd9Sstevel@tonic-gate {
107*7c478bd9Sstevel@tonic-gate 	struct flock		l;
108*7c478bd9Sstevel@tonic-gate 	int			fd,
109*7c478bd9Sstevel@tonic-gate 				oflag,
110*7c478bd9Sstevel@tonic-gate 				create,
111*7c478bd9Sstevel@tonic-gate 				truncate = 0;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (!path || !type) {
114*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
115*7c478bd9Sstevel@tonic-gate 		return (-1);
116*7c478bd9Sstevel@tonic-gate 	}
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate #define	plus (type[1] == '+')
119*7c478bd9Sstevel@tonic-gate 	switch (type[0]) {
120*7c478bd9Sstevel@tonic-gate 	case 'w':
121*7c478bd9Sstevel@tonic-gate 		oflag = plus? O_RDWR : O_WRONLY;
122*7c478bd9Sstevel@tonic-gate 		create = 1;
123*7c478bd9Sstevel@tonic-gate 		truncate = 1;
124*7c478bd9Sstevel@tonic-gate 		break;
125*7c478bd9Sstevel@tonic-gate 	case 'a':
126*7c478bd9Sstevel@tonic-gate 		oflag = (plus? O_RDWR : O_WRONLY) | O_APPEND;
127*7c478bd9Sstevel@tonic-gate 		create = 1;
128*7c478bd9Sstevel@tonic-gate 		break;
129*7c478bd9Sstevel@tonic-gate 	case 'r':
130*7c478bd9Sstevel@tonic-gate 		oflag = plus? O_RDWR : O_RDONLY;
131*7c478bd9Sstevel@tonic-gate 		create = 0;
132*7c478bd9Sstevel@tonic-gate 		break;
133*7c478bd9Sstevel@tonic-gate 	default:
134*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
135*7c478bd9Sstevel@tonic-gate 		return (-1);
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 	if ((fd = Open(path, oflag, mode)) == -1)
138*7c478bd9Sstevel@tonic-gate 		if (errno == ENOENT && create) {
139*7c478bd9Sstevel@tonic-gate 			int		old_umask = umask(0);
140*7c478bd9Sstevel@tonic-gate 			int		save_errno;
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 			if ((fd = Open(path, oflag|O_CREAT, mode)) != -1)
143*7c478bd9Sstevel@tonic-gate 				chown_lppath(path);
144*7c478bd9Sstevel@tonic-gate 			save_errno = errno;
145*7c478bd9Sstevel@tonic-gate 			if (old_umask)
146*7c478bd9Sstevel@tonic-gate 				umask(old_umask);
147*7c478bd9Sstevel@tonic-gate 			errno = save_errno;
148*7c478bd9Sstevel@tonic-gate 		}
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	if (fd == -1)
151*7c478bd9Sstevel@tonic-gate 	switch (errno) {
152*7c478bd9Sstevel@tonic-gate 	case ENOTDIR:
153*7c478bd9Sstevel@tonic-gate 		errno = EACCES;
154*7c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
155*7c478bd9Sstevel@tonic-gate 	default:
156*7c478bd9Sstevel@tonic-gate 		return (-1);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	l.l_type = (oflag & (O_WRONLY|O_RDWR)? F_WRLCK : F_RDLCK);
160*7c478bd9Sstevel@tonic-gate 	l.l_whence = 1;
161*7c478bd9Sstevel@tonic-gate 	l.l_start = 0;
162*7c478bd9Sstevel@tonic-gate 	l.l_len = 0;
163*7c478bd9Sstevel@tonic-gate 	if (Fcntl(fd, F_SETLK, &l) == -1) {
164*7c478bd9Sstevel@tonic-gate 		/*
165*7c478bd9Sstevel@tonic-gate 		 * Early UNIX op. sys. have wrong errno.
166*7c478bd9Sstevel@tonic-gate 		 */
167*7c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
168*7c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
169*7c478bd9Sstevel@tonic-gate 		Close(fd);
170*7c478bd9Sstevel@tonic-gate 		return (-1);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if (truncate) {
174*7c478bd9Sstevel@tonic-gate 		if ((lseek(fd, 0, SEEK_SET) == (off_t)-1) ||
175*7c478bd9Sstevel@tonic-gate 		    (ftruncate(fd, 0) == -1)) {
176*7c478bd9Sstevel@tonic-gate 			Close(fd);
177*7c478bd9Sstevel@tonic-gate 			return (-1);
178*7c478bd9Sstevel@tonic-gate 		}
179*7c478bd9Sstevel@tonic-gate 	}
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 	return (fd);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate FILE *
186*7c478bd9Sstevel@tonic-gate open_lpfile(char *path, char *type, mode_t mode)
187*7c478bd9Sstevel@tonic-gate {
188*7c478bd9Sstevel@tonic-gate 	FILE		*fp = NULL;
189*7c478bd9Sstevel@tonic-gate 	int		fd;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, type, mode)) >= 0) {
192*7c478bd9Sstevel@tonic-gate 		errno = 0;	/* fdopen() may fail and not set errno */
193*7c478bd9Sstevel@tonic-gate 		if (!(fp = fdopen(fd, type))) {
194*7c478bd9Sstevel@tonic-gate 			Close(fd);
195*7c478bd9Sstevel@tonic-gate 		}
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 	return (fp);
198*7c478bd9Sstevel@tonic-gate }
199*7c478bd9Sstevel@tonic-gate int
200*7c478bd9Sstevel@tonic-gate close_lpfile(FILE *fp)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	return (fclose(fp));
203*7c478bd9Sstevel@tonic-gate }
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate /*
206*7c478bd9Sstevel@tonic-gate  * chown_lppath()
207*7c478bd9Sstevel@tonic-gate  */
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate int
210*7c478bd9Sstevel@tonic-gate chown_lppath(char *path)
211*7c478bd9Sstevel@tonic-gate {
212*7c478bd9Sstevel@tonic-gate 	static uid_t	lp_uid;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	static gid_t	lp_gid;
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	static int	gotids = 0;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	struct passwd	*ppw;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	if (!gotids) {
222*7c478bd9Sstevel@tonic-gate 		if (!(ppw = getpwnam(LPUSER)))
223*7c478bd9Sstevel@tonic-gate 			ppw = getpwnam(ROOTUSER);
224*7c478bd9Sstevel@tonic-gate 		endpwent();
225*7c478bd9Sstevel@tonic-gate 		if (!ppw)
226*7c478bd9Sstevel@tonic-gate 			return (-1);
227*7c478bd9Sstevel@tonic-gate 		lp_uid = ppw->pw_uid;
228*7c478bd9Sstevel@tonic-gate 		lp_gid = ppw->pw_gid;
229*7c478bd9Sstevel@tonic-gate 		gotids = 1;
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 	return (Chown(path, lp_uid, lp_gid));
232*7c478bd9Sstevel@tonic-gate }
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate /*
235*7c478bd9Sstevel@tonic-gate  * rmfile() - UNLINK FILE BUT NO COMPLAINT IF NOT THERE
236*7c478bd9Sstevel@tonic-gate  */
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate int
239*7c478bd9Sstevel@tonic-gate rmfile(char *path)
240*7c478bd9Sstevel@tonic-gate {
241*7c478bd9Sstevel@tonic-gate 	return (Unlink(path) == 0 || errno == ENOENT);
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate /*
245*7c478bd9Sstevel@tonic-gate  * loadline() - LOAD A ONE-LINE CHARACTER STRING FROM FILE
246*7c478bd9Sstevel@tonic-gate  */
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate char *
249*7c478bd9Sstevel@tonic-gate loadline(char *path)
250*7c478bd9Sstevel@tonic-gate {
251*7c478bd9Sstevel@tonic-gate 	int fd;
252*7c478bd9Sstevel@tonic-gate 	register char		*ret;
253*7c478bd9Sstevel@tonic-gate 	register int		len;
254*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_READ)) < 0)
257*7c478bd9Sstevel@tonic-gate 		return (0);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	if (fdgets(buf, BUFSIZ, fd)) {
260*7c478bd9Sstevel@tonic-gate 		if ((len = strlen(buf)) && buf[len - 1] == '\n')
261*7c478bd9Sstevel@tonic-gate 			buf[--len] = 0;
262*7c478bd9Sstevel@tonic-gate 		if ((ret = Malloc(len + 1)))
263*7c478bd9Sstevel@tonic-gate 			strcpy(ret, buf);
264*7c478bd9Sstevel@tonic-gate 	} else {
265*7c478bd9Sstevel@tonic-gate 		errno = 0;
266*7c478bd9Sstevel@tonic-gate 		ret = 0;
267*7c478bd9Sstevel@tonic-gate 	}
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	close(fd);
270*7c478bd9Sstevel@tonic-gate 	return (ret);
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*
274*7c478bd9Sstevel@tonic-gate  * loadstring() - LOAD A CHARACTER STRING FROM FILE
275*7c478bd9Sstevel@tonic-gate  */
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate char *
278*7c478bd9Sstevel@tonic-gate loadstring(char *path)
279*7c478bd9Sstevel@tonic-gate {
280*7c478bd9Sstevel@tonic-gate 	int fd;
281*7c478bd9Sstevel@tonic-gate 	register char		*ret;
282*7c478bd9Sstevel@tonic-gate 	register int		len;
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_READ)) < 0)
285*7c478bd9Sstevel@tonic-gate 		return (0);
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 	if ((ret = sop_up_rest(fd, (char *)0))) {
288*7c478bd9Sstevel@tonic-gate 		if ((len = strlen(ret)) && ret[len - 1] == '\n')
289*7c478bd9Sstevel@tonic-gate 			ret[len - 1] = 0;
290*7c478bd9Sstevel@tonic-gate 	} else
291*7c478bd9Sstevel@tonic-gate 		errno = 0;
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 	close(fd);
294*7c478bd9Sstevel@tonic-gate 	return (ret);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate /*
298*7c478bd9Sstevel@tonic-gate  * dumpstring() - DUMP CHARACTER STRING TO FILE
299*7c478bd9Sstevel@tonic-gate  */
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate int
302*7c478bd9Sstevel@tonic-gate dumpstring(char *path, char *str)
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	int fd;
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 	if (!str)
307*7c478bd9Sstevel@tonic-gate 		return (rmfile(path));
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_READ)) < 0)
310*7c478bd9Sstevel@tonic-gate 		return (-1);
311*7c478bd9Sstevel@tonic-gate 	fdprintf(fd, "%s\n", str);
312*7c478bd9Sstevel@tonic-gate 	close(fd);
313*7c478bd9Sstevel@tonic-gate 	return (0);
314*7c478bd9Sstevel@tonic-gate }
315