xref: /illumos-gate/usr/src/cmd/lp/lib/lp/files.c (revision e4fb8a5f)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "stdio.h"
347c478bd9Sstevel@tonic-gate #include "fcntl.h"
357c478bd9Sstevel@tonic-gate #include "string.h"
367c478bd9Sstevel@tonic-gate #include "errno.h"
377c478bd9Sstevel@tonic-gate #include "pwd.h"
387c478bd9Sstevel@tonic-gate #include "sys/types.h"
397c478bd9Sstevel@tonic-gate #include "sys/stat.h"
407c478bd9Sstevel@tonic-gate #include "stdlib.h"
417c478bd9Sstevel@tonic-gate #include <stdarg.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
437c478bd9Sstevel@tonic-gate #include "pwd.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "lp.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int
is_printer_uri(char * value)487c478bd9Sstevel@tonic-gate is_printer_uri(char *value)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	if (value == NULL)
517c478bd9Sstevel@tonic-gate 		return (-1);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((value[0] == '/') && (access(value, F_OK) == 0))
547c478bd9Sstevel@tonic-gate 		return (-1); /* a valid path */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (strstr(value, "://") == NULL)
577c478bd9Sstevel@tonic-gate 		return (-1); /* not in uri form */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	return (0);
607c478bd9Sstevel@tonic-gate }
617c478bd9Sstevel@tonic-gate 
62df1eb1adSjacobs /*
63df1eb1adSjacobs  * To avoid a race condition, chown() should always be called before
64df1eb1adSjacobs  * chmod().
65df1eb1adSjacobs  */
66df1eb1adSjacobs int
chownmod(char * path,uid_t owner,gid_t group,mode_t mode)67df1eb1adSjacobs chownmod(char *path, uid_t owner, gid_t group, mode_t mode)
68df1eb1adSjacobs {
69df1eb1adSjacobs 	int rc;
70df1eb1adSjacobs 
71df1eb1adSjacobs 	if ((rc = Chown(path, owner, group)) == 0)
72df1eb1adSjacobs 		rc = Chmod(path, mode);
73df1eb1adSjacobs 
74df1eb1adSjacobs 	return (rc);
75df1eb1adSjacobs }
76df1eb1adSjacobs 
77df1eb1adSjacobs 
78f928ce67Sceastha int
fdprintf(int fd,char * fmt,...)797c478bd9Sstevel@tonic-gate fdprintf(int fd, char *fmt, ...)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	char    buf[BUFSIZ];
827c478bd9Sstevel@tonic-gate 	va_list ap;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	if (fd == 1)
857c478bd9Sstevel@tonic-gate 		fflush(stdout);
867c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
877c478bd9Sstevel@tonic-gate 	vsnprintf(buf, sizeof (buf), fmt, ap);
887c478bd9Sstevel@tonic-gate 	va_end(ap);
897c478bd9Sstevel@tonic-gate 	return (Write(fd, buf, (int)strlen(buf)));
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate char *
fdgets(char * buf,int len,int fd)937c478bd9Sstevel@tonic-gate fdgets(char *buf, int len, int fd)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	char    tmp;
967c478bd9Sstevel@tonic-gate 	int	count = 0;
977c478bd9Sstevel@tonic-gate 
98*e4fb8a5fSToomas Soome 	memset(buf, 0, len);
997c478bd9Sstevel@tonic-gate 	while ((count < len) && (Read(fd, &tmp, 1) > 0))
1007c478bd9Sstevel@tonic-gate 		if ((buf[count++] = tmp) == '\n') break;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (count != 0)
1037c478bd9Sstevel@tonic-gate 		return (buf);
1047c478bd9Sstevel@tonic-gate 	return (NULL);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
107f928ce67Sceastha int
fdputs(char * buf,int fd)1087c478bd9Sstevel@tonic-gate fdputs(char *buf, int fd)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	return (fdprintf(fd, "%s", buf));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
113f928ce67Sceastha int
fdputc(char c,int fd)1147c478bd9Sstevel@tonic-gate fdputc(char c, int fd)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	if (fd == 1)
1177c478bd9Sstevel@tonic-gate 		fflush(stdout);
1187c478bd9Sstevel@tonic-gate 	return (write(fd, &c, 1));
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate int
open_locked(char * path,char * type,mode_t mode)1227c478bd9Sstevel@tonic-gate open_locked(char *path, char *type, mode_t mode)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	struct flock		l;
1257c478bd9Sstevel@tonic-gate 	int			fd,
1267c478bd9Sstevel@tonic-gate 				oflag,
1277c478bd9Sstevel@tonic-gate 				create,
1287c478bd9Sstevel@tonic-gate 				truncate = 0;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (!path || !type) {
1317c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1327c478bd9Sstevel@tonic-gate 		return (-1);
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate #define	plus (type[1] == '+')
1367c478bd9Sstevel@tonic-gate 	switch (type[0]) {
1377c478bd9Sstevel@tonic-gate 	case 'w':
1387c478bd9Sstevel@tonic-gate 		oflag = plus? O_RDWR : O_WRONLY;
1397c478bd9Sstevel@tonic-gate 		create = 1;
1407c478bd9Sstevel@tonic-gate 		truncate = 1;
1417c478bd9Sstevel@tonic-gate 		break;
1427c478bd9Sstevel@tonic-gate 	case 'a':
1437c478bd9Sstevel@tonic-gate 		oflag = (plus? O_RDWR : O_WRONLY) | O_APPEND;
1447c478bd9Sstevel@tonic-gate 		create = 1;
1457c478bd9Sstevel@tonic-gate 		break;
1467c478bd9Sstevel@tonic-gate 	case 'r':
1477c478bd9Sstevel@tonic-gate 		oflag = plus? O_RDWR : O_RDONLY;
1487c478bd9Sstevel@tonic-gate 		create = 0;
1497c478bd9Sstevel@tonic-gate 		break;
1507c478bd9Sstevel@tonic-gate 	default:
1517c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1527c478bd9Sstevel@tonic-gate 		return (-1);
1537c478bd9Sstevel@tonic-gate 	}
1547c478bd9Sstevel@tonic-gate 	if ((fd = Open(path, oflag, mode)) == -1)
1557c478bd9Sstevel@tonic-gate 		if (errno == ENOENT && create) {
1567c478bd9Sstevel@tonic-gate 			int		old_umask = umask(0);
1577c478bd9Sstevel@tonic-gate 			int		save_errno;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 			if ((fd = Open(path, oflag|O_CREAT, mode)) != -1)
1607c478bd9Sstevel@tonic-gate 				chown_lppath(path);
1617c478bd9Sstevel@tonic-gate 			save_errno = errno;
1627c478bd9Sstevel@tonic-gate 			if (old_umask)
1637c478bd9Sstevel@tonic-gate 				umask(old_umask);
1647c478bd9Sstevel@tonic-gate 			errno = save_errno;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (fd == -1)
1687c478bd9Sstevel@tonic-gate 	switch (errno) {
1697c478bd9Sstevel@tonic-gate 	case ENOTDIR:
1707c478bd9Sstevel@tonic-gate 		errno = EACCES;
1717c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1727c478bd9Sstevel@tonic-gate 	default:
1737c478bd9Sstevel@tonic-gate 		return (-1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	l.l_type = (oflag & (O_WRONLY|O_RDWR)? F_WRLCK : F_RDLCK);
1777c478bd9Sstevel@tonic-gate 	l.l_whence = 1;
1787c478bd9Sstevel@tonic-gate 	l.l_start = 0;
1797c478bd9Sstevel@tonic-gate 	l.l_len = 0;
1807c478bd9Sstevel@tonic-gate 	if (Fcntl(fd, F_SETLK, &l) == -1) {
1817c478bd9Sstevel@tonic-gate 		/*
1827c478bd9Sstevel@tonic-gate 		 * Early UNIX op. sys. have wrong errno.
1837c478bd9Sstevel@tonic-gate 		 */
1847c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
1857c478bd9Sstevel@tonic-gate 			errno = EAGAIN;
1867c478bd9Sstevel@tonic-gate 		Close(fd);
1877c478bd9Sstevel@tonic-gate 		return (-1);
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if (truncate) {
1917c478bd9Sstevel@tonic-gate 		if ((lseek(fd, 0, SEEK_SET) == (off_t)-1) ||
1927c478bd9Sstevel@tonic-gate 		    (ftruncate(fd, 0) == -1)) {
1937c478bd9Sstevel@tonic-gate 			Close(fd);
1947c478bd9Sstevel@tonic-gate 			return (-1);
1957c478bd9Sstevel@tonic-gate 		}
1967c478bd9Sstevel@tonic-gate 	}
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (fd);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate FILE *
open_lpfile(char * path,char * type,mode_t mode)2037c478bd9Sstevel@tonic-gate open_lpfile(char *path, char *type, mode_t mode)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	FILE		*fp = NULL;
2067c478bd9Sstevel@tonic-gate 	int		fd;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, type, mode)) >= 0) {
2097c478bd9Sstevel@tonic-gate 		errno = 0;	/* fdopen() may fail and not set errno */
2107c478bd9Sstevel@tonic-gate 		if (!(fp = fdopen(fd, type))) {
2117c478bd9Sstevel@tonic-gate 			Close(fd);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 	return (fp);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate int
close_lpfile(FILE * fp)2177c478bd9Sstevel@tonic-gate close_lpfile(FILE *fp)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	return (fclose(fp));
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * chown_lppath()
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate int
chown_lppath(char * path)2277c478bd9Sstevel@tonic-gate chown_lppath(char *path)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	static uid_t	lp_uid;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	static gid_t	lp_gid;
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	static int	gotids = 0;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	struct passwd	*ppw;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	if (!gotids) {
2397c478bd9Sstevel@tonic-gate 		if (!(ppw = getpwnam(LPUSER)))
2407c478bd9Sstevel@tonic-gate 			ppw = getpwnam(ROOTUSER);
2417c478bd9Sstevel@tonic-gate 		endpwent();
2427c478bd9Sstevel@tonic-gate 		if (!ppw)
2437c478bd9Sstevel@tonic-gate 			return (-1);
2447c478bd9Sstevel@tonic-gate 		lp_uid = ppw->pw_uid;
2457c478bd9Sstevel@tonic-gate 		lp_gid = ppw->pw_gid;
2467c478bd9Sstevel@tonic-gate 		gotids = 1;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	return (Chown(path, lp_uid, lp_gid));
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * rmfile() - UNLINK FILE BUT NO COMPLAINT IF NOT THERE
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate int
rmfile(char * path)2567c478bd9Sstevel@tonic-gate rmfile(char *path)
2577c478bd9Sstevel@tonic-gate {
2587c478bd9Sstevel@tonic-gate 	return (Unlink(path) == 0 || errno == ENOENT);
2597c478bd9Sstevel@tonic-gate }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * loadline() - LOAD A ONE-LINE CHARACTER STRING FROM FILE
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate char *
loadline(char * path)2667c478bd9Sstevel@tonic-gate loadline(char *path)
2677c478bd9Sstevel@tonic-gate {
2687c478bd9Sstevel@tonic-gate 	int fd;
2697c478bd9Sstevel@tonic-gate 	register char		*ret;
2707c478bd9Sstevel@tonic-gate 	register int		len;
2717c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_READ)) < 0)
2747c478bd9Sstevel@tonic-gate 		return (0);
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (fdgets(buf, BUFSIZ, fd)) {
2777c478bd9Sstevel@tonic-gate 		if ((len = strlen(buf)) && buf[len - 1] == '\n')
2787c478bd9Sstevel@tonic-gate 			buf[--len] = 0;
2797c478bd9Sstevel@tonic-gate 		if ((ret = Malloc(len + 1)))
2807c478bd9Sstevel@tonic-gate 			strcpy(ret, buf);
2817c478bd9Sstevel@tonic-gate 	} else {
2827c478bd9Sstevel@tonic-gate 		errno = 0;
2837c478bd9Sstevel@tonic-gate 		ret = 0;
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	close(fd);
2877c478bd9Sstevel@tonic-gate 	return (ret);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * loadstring() - LOAD A CHARACTER STRING FROM FILE
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate char *
loadstring(char * path)2957c478bd9Sstevel@tonic-gate loadstring(char *path)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	int fd;
2987c478bd9Sstevel@tonic-gate 	register char		*ret;
2997c478bd9Sstevel@tonic-gate 	register int		len;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", MODE_READ)) < 0)
3027c478bd9Sstevel@tonic-gate 		return (0);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	if ((ret = sop_up_rest(fd, (char *)0))) {
3057c478bd9Sstevel@tonic-gate 		if ((len = strlen(ret)) && ret[len - 1] == '\n')
3067c478bd9Sstevel@tonic-gate 			ret[len - 1] = 0;
3077c478bd9Sstevel@tonic-gate 	} else
3087c478bd9Sstevel@tonic-gate 		errno = 0;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	close(fd);
3117c478bd9Sstevel@tonic-gate 	return (ret);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * dumpstring() - DUMP CHARACTER STRING TO FILE
3167c478bd9Sstevel@tonic-gate  */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate int
dumpstring(char * path,char * str)3197c478bd9Sstevel@tonic-gate dumpstring(char *path, char *str)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	int fd;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (!str)
3247c478bd9Sstevel@tonic-gate 		return (rmfile(path));
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "w", MODE_READ)) < 0)
3277c478bd9Sstevel@tonic-gate 		return (-1);
3287c478bd9Sstevel@tonic-gate 	fdprintf(fd, "%s\n", str);
3297c478bd9Sstevel@tonic-gate 	close(fd);
3307c478bd9Sstevel@tonic-gate 	return (0);
3317c478bd9Sstevel@tonic-gate }
332