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 #pragma ident "%Z%%M% %I% %E% SMI" 327c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate #include "stdio.h" 357c478bd9Sstevel@tonic-gate #include "fcntl.h" 367c478bd9Sstevel@tonic-gate #include "string.h" 377c478bd9Sstevel@tonic-gate #include "errno.h" 387c478bd9Sstevel@tonic-gate #include "pwd.h" 397c478bd9Sstevel@tonic-gate #include "sys/types.h" 407c478bd9Sstevel@tonic-gate #include "sys/stat.h" 417c478bd9Sstevel@tonic-gate #include "stdlib.h" 427c478bd9Sstevel@tonic-gate #include <stdarg.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include "pwd.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate #include "lp.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate int 497c478bd9Sstevel@tonic-gate is_printer_uri(char *value) 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate if (value == NULL) 527c478bd9Sstevel@tonic-gate return (-1); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate if ((value[0] == '/') && (access(value, F_OK) == 0)) 557c478bd9Sstevel@tonic-gate return (-1); /* a valid path */ 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate if (strstr(value, "://") == NULL) 587c478bd9Sstevel@tonic-gate return (-1); /* not in uri form */ 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate return (0); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 63*df1eb1adSjacobs /* 64*df1eb1adSjacobs * To avoid a race condition, chown() should always be called before 65*df1eb1adSjacobs * chmod(). 66*df1eb1adSjacobs */ 67*df1eb1adSjacobs int 68*df1eb1adSjacobs chownmod(char *path, uid_t owner, gid_t group, mode_t mode) 69*df1eb1adSjacobs { 70*df1eb1adSjacobs int rc; 71*df1eb1adSjacobs 72*df1eb1adSjacobs if ((rc = Chown(path, owner, group)) == 0) 73*df1eb1adSjacobs rc = Chmod(path, mode); 74*df1eb1adSjacobs 75*df1eb1adSjacobs return (rc); 76*df1eb1adSjacobs } 77*df1eb1adSjacobs 78*df1eb1adSjacobs 79f928ce67Sceastha int 807c478bd9Sstevel@tonic-gate fdprintf(int fd, char *fmt, ...) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 837c478bd9Sstevel@tonic-gate va_list ap; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (fd == 1) 867c478bd9Sstevel@tonic-gate fflush(stdout); 877c478bd9Sstevel@tonic-gate va_start(ap, fmt); 887c478bd9Sstevel@tonic-gate vsnprintf(buf, sizeof (buf), fmt, ap); 897c478bd9Sstevel@tonic-gate va_end(ap); 907c478bd9Sstevel@tonic-gate return (Write(fd, buf, (int)strlen(buf))); 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate char * 947c478bd9Sstevel@tonic-gate fdgets(char *buf, int len, int fd) 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate char tmp; 977c478bd9Sstevel@tonic-gate int count = 0; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate memset(buf, NULL, len); 1007c478bd9Sstevel@tonic-gate while ((count < len) && (Read(fd, &tmp, 1) > 0)) 1017c478bd9Sstevel@tonic-gate if ((buf[count++] = tmp) == '\n') break; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate if (count != 0) 1047c478bd9Sstevel@tonic-gate return (buf); 1057c478bd9Sstevel@tonic-gate return (NULL); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 108f928ce67Sceastha int 1097c478bd9Sstevel@tonic-gate fdputs(char *buf, int fd) 1107c478bd9Sstevel@tonic-gate { 1117c478bd9Sstevel@tonic-gate return (fdprintf(fd, "%s", buf)); 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate 114f928ce67Sceastha int 1157c478bd9Sstevel@tonic-gate fdputc(char c, int fd) 1167c478bd9Sstevel@tonic-gate { 1177c478bd9Sstevel@tonic-gate if (fd == 1) 1187c478bd9Sstevel@tonic-gate fflush(stdout); 1197c478bd9Sstevel@tonic-gate return (write(fd, &c, 1)); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate int 1237c478bd9Sstevel@tonic-gate open_locked(char *path, char *type, mode_t mode) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate struct flock l; 1267c478bd9Sstevel@tonic-gate int fd, 1277c478bd9Sstevel@tonic-gate oflag, 1287c478bd9Sstevel@tonic-gate create, 1297c478bd9Sstevel@tonic-gate truncate = 0; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if (!path || !type) { 1327c478bd9Sstevel@tonic-gate errno = EINVAL; 1337c478bd9Sstevel@tonic-gate return (-1); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate #define plus (type[1] == '+') 1377c478bd9Sstevel@tonic-gate switch (type[0]) { 1387c478bd9Sstevel@tonic-gate case 'w': 1397c478bd9Sstevel@tonic-gate oflag = plus? O_RDWR : O_WRONLY; 1407c478bd9Sstevel@tonic-gate create = 1; 1417c478bd9Sstevel@tonic-gate truncate = 1; 1427c478bd9Sstevel@tonic-gate break; 1437c478bd9Sstevel@tonic-gate case 'a': 1447c478bd9Sstevel@tonic-gate oflag = (plus? O_RDWR : O_WRONLY) | O_APPEND; 1457c478bd9Sstevel@tonic-gate create = 1; 1467c478bd9Sstevel@tonic-gate break; 1477c478bd9Sstevel@tonic-gate case 'r': 1487c478bd9Sstevel@tonic-gate oflag = plus? O_RDWR : O_RDONLY; 1497c478bd9Sstevel@tonic-gate create = 0; 1507c478bd9Sstevel@tonic-gate break; 1517c478bd9Sstevel@tonic-gate default: 1527c478bd9Sstevel@tonic-gate errno = EINVAL; 1537c478bd9Sstevel@tonic-gate return (-1); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate if ((fd = Open(path, oflag, mode)) == -1) 1567c478bd9Sstevel@tonic-gate if (errno == ENOENT && create) { 1577c478bd9Sstevel@tonic-gate int old_umask = umask(0); 1587c478bd9Sstevel@tonic-gate int save_errno; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate if ((fd = Open(path, oflag|O_CREAT, mode)) != -1) 1617c478bd9Sstevel@tonic-gate chown_lppath(path); 1627c478bd9Sstevel@tonic-gate save_errno = errno; 1637c478bd9Sstevel@tonic-gate if (old_umask) 1647c478bd9Sstevel@tonic-gate umask(old_umask); 1657c478bd9Sstevel@tonic-gate errno = save_errno; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate if (fd == -1) 1697c478bd9Sstevel@tonic-gate switch (errno) { 1707c478bd9Sstevel@tonic-gate case ENOTDIR: 1717c478bd9Sstevel@tonic-gate errno = EACCES; 1727c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 1737c478bd9Sstevel@tonic-gate default: 1747c478bd9Sstevel@tonic-gate return (-1); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate l.l_type = (oflag & (O_WRONLY|O_RDWR)? F_WRLCK : F_RDLCK); 1787c478bd9Sstevel@tonic-gate l.l_whence = 1; 1797c478bd9Sstevel@tonic-gate l.l_start = 0; 1807c478bd9Sstevel@tonic-gate l.l_len = 0; 1817c478bd9Sstevel@tonic-gate if (Fcntl(fd, F_SETLK, &l) == -1) { 1827c478bd9Sstevel@tonic-gate /* 1837c478bd9Sstevel@tonic-gate * Early UNIX op. sys. have wrong errno. 1847c478bd9Sstevel@tonic-gate */ 1857c478bd9Sstevel@tonic-gate if (errno == EACCES) 1867c478bd9Sstevel@tonic-gate errno = EAGAIN; 1877c478bd9Sstevel@tonic-gate Close(fd); 1887c478bd9Sstevel@tonic-gate return (-1); 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate if (truncate) { 1927c478bd9Sstevel@tonic-gate if ((lseek(fd, 0, SEEK_SET) == (off_t)-1) || 1937c478bd9Sstevel@tonic-gate (ftruncate(fd, 0) == -1)) { 1947c478bd9Sstevel@tonic-gate Close(fd); 1957c478bd9Sstevel@tonic-gate return (-1); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate return (fd); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate FILE * 2047c478bd9Sstevel@tonic-gate open_lpfile(char *path, char *type, mode_t mode) 2057c478bd9Sstevel@tonic-gate { 2067c478bd9Sstevel@tonic-gate FILE *fp = NULL; 2077c478bd9Sstevel@tonic-gate int fd; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, type, mode)) >= 0) { 2107c478bd9Sstevel@tonic-gate errno = 0; /* fdopen() may fail and not set errno */ 2117c478bd9Sstevel@tonic-gate if (!(fp = fdopen(fd, type))) { 2127c478bd9Sstevel@tonic-gate Close(fd); 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate } 2157c478bd9Sstevel@tonic-gate return (fp); 2167c478bd9Sstevel@tonic-gate } 2177c478bd9Sstevel@tonic-gate int 2187c478bd9Sstevel@tonic-gate close_lpfile(FILE *fp) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate return (fclose(fp)); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * chown_lppath() 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate int 2287c478bd9Sstevel@tonic-gate chown_lppath(char *path) 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate static uid_t lp_uid; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate static gid_t lp_gid; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static int gotids = 0; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate struct passwd *ppw; 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate if (!gotids) { 2407c478bd9Sstevel@tonic-gate if (!(ppw = getpwnam(LPUSER))) 2417c478bd9Sstevel@tonic-gate ppw = getpwnam(ROOTUSER); 2427c478bd9Sstevel@tonic-gate endpwent(); 2437c478bd9Sstevel@tonic-gate if (!ppw) 2447c478bd9Sstevel@tonic-gate return (-1); 2457c478bd9Sstevel@tonic-gate lp_uid = ppw->pw_uid; 2467c478bd9Sstevel@tonic-gate lp_gid = ppw->pw_gid; 2477c478bd9Sstevel@tonic-gate gotids = 1; 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate return (Chown(path, lp_uid, lp_gid)); 2507c478bd9Sstevel@tonic-gate } 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * rmfile() - UNLINK FILE BUT NO COMPLAINT IF NOT THERE 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate int 2577c478bd9Sstevel@tonic-gate rmfile(char *path) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate return (Unlink(path) == 0 || errno == ENOENT); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate /* 2637c478bd9Sstevel@tonic-gate * loadline() - LOAD A ONE-LINE CHARACTER STRING FROM FILE 2647c478bd9Sstevel@tonic-gate */ 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate char * 2677c478bd9Sstevel@tonic-gate loadline(char *path) 2687c478bd9Sstevel@tonic-gate { 2697c478bd9Sstevel@tonic-gate int fd; 2707c478bd9Sstevel@tonic-gate register char *ret; 2717c478bd9Sstevel@tonic-gate register int len; 2727c478bd9Sstevel@tonic-gate char buf[BUFSIZ]; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "r", MODE_READ)) < 0) 2757c478bd9Sstevel@tonic-gate return (0); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate if (fdgets(buf, BUFSIZ, fd)) { 2787c478bd9Sstevel@tonic-gate if ((len = strlen(buf)) && buf[len - 1] == '\n') 2797c478bd9Sstevel@tonic-gate buf[--len] = 0; 2807c478bd9Sstevel@tonic-gate if ((ret = Malloc(len + 1))) 2817c478bd9Sstevel@tonic-gate strcpy(ret, buf); 2827c478bd9Sstevel@tonic-gate } else { 2837c478bd9Sstevel@tonic-gate errno = 0; 2847c478bd9Sstevel@tonic-gate ret = 0; 2857c478bd9Sstevel@tonic-gate } 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate close(fd); 2887c478bd9Sstevel@tonic-gate return (ret); 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate /* 2927c478bd9Sstevel@tonic-gate * loadstring() - LOAD A CHARACTER STRING FROM FILE 2937c478bd9Sstevel@tonic-gate */ 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate char * 2967c478bd9Sstevel@tonic-gate loadstring(char *path) 2977c478bd9Sstevel@tonic-gate { 2987c478bd9Sstevel@tonic-gate int fd; 2997c478bd9Sstevel@tonic-gate register char *ret; 3007c478bd9Sstevel@tonic-gate register int len; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "r", MODE_READ)) < 0) 3037c478bd9Sstevel@tonic-gate return (0); 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate if ((ret = sop_up_rest(fd, (char *)0))) { 3067c478bd9Sstevel@tonic-gate if ((len = strlen(ret)) && ret[len - 1] == '\n') 3077c478bd9Sstevel@tonic-gate ret[len - 1] = 0; 3087c478bd9Sstevel@tonic-gate } else 3097c478bd9Sstevel@tonic-gate errno = 0; 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate close(fd); 3127c478bd9Sstevel@tonic-gate return (ret); 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate /* 3167c478bd9Sstevel@tonic-gate * dumpstring() - DUMP CHARACTER STRING TO FILE 3177c478bd9Sstevel@tonic-gate */ 3187c478bd9Sstevel@tonic-gate 3197c478bd9Sstevel@tonic-gate int 3207c478bd9Sstevel@tonic-gate dumpstring(char *path, char *str) 3217c478bd9Sstevel@tonic-gate { 3227c478bd9Sstevel@tonic-gate int fd; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (!str) 3257c478bd9Sstevel@tonic-gate return (rmfile(path)); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if ((fd = open_locked(path, "w", MODE_READ)) < 0) 3287c478bd9Sstevel@tonic-gate return (-1); 3297c478bd9Sstevel@tonic-gate fdprintf(fd, "%s\n", str); 3307c478bd9Sstevel@tonic-gate close(fd); 3317c478bd9Sstevel@tonic-gate return (0); 3327c478bd9Sstevel@tonic-gate } 333