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
586b1a8baSrotondo  * Common Development and Distribution License (the "License").
686b1a8baSrotondo  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2236e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <unistd.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <stdarg.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <pwd.h>
377c478bd9Sstevel@tonic-gate #include <libintl.h>
387c478bd9Sstevel@tonic-gate #include <netdb.h>	/* for rcmd() */
397c478bd9Sstevel@tonic-gate 
40355b4669Sjacobs #include <ns.h>
41355b4669Sjacobs #include <list.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*  escaped chars include delimiters and shell meta characters */
447c478bd9Sstevel@tonic-gate #define	ESCAPE_CHARS	"\\\n=: `&;|>^$()<*?["
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * This modules contains all of the code nedessary to write back to each
487c478bd9Sstevel@tonic-gate  * printing configuration data repository.  The support is intended to
497c478bd9Sstevel@tonic-gate  * introduce the least number of dependencies in the library, so it doesn't
507c478bd9Sstevel@tonic-gate  * always perform it's operations in the cleanest fashion.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * Generic Files support begins here.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static char *
freadline(FILE * fp,char * buf,int buflen)587c478bd9Sstevel@tonic-gate freadline(FILE *fp, char *buf, int buflen)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	char *s = buf;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	while (fgets(s, buflen, fp)) {
637c478bd9Sstevel@tonic-gate 		if ((s == buf) && ((*s == '#') || (*s == '\n'))) {
647c478bd9Sstevel@tonic-gate 			continue;
657c478bd9Sstevel@tonic-gate 		} else {
667c478bd9Sstevel@tonic-gate 			if ((*s == '#') || (*s == '\n')) {
67f4593de7SToomas Soome 				*s = '\0';
687c478bd9Sstevel@tonic-gate 				break;
697c478bd9Sstevel@tonic-gate 			}
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 			buflen -= strlen(s);
727c478bd9Sstevel@tonic-gate 			s += strlen(s);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 			if (*(s - 2) != '\\')
757c478bd9Sstevel@tonic-gate 				break;
767c478bd9Sstevel@tonic-gate #ifdef STRIP_CONTINUATION
777c478bd9Sstevel@tonic-gate 			buflen -= 2;
787c478bd9Sstevel@tonic-gate 			s -= 2;
797c478bd9Sstevel@tonic-gate #endif
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if (s == buf)
847c478bd9Sstevel@tonic-gate 		return (NULL);
857c478bd9Sstevel@tonic-gate 	else
867c478bd9Sstevel@tonic-gate 		return (buf);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static int
_file_put_printer(const char * file,const ns_printer_t * printer)917c478bd9Sstevel@tonic-gate _file_put_printer(const char *file, const ns_printer_t *printer)
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate 	FILE	*ifp,
9436e852a1SRaja Andra 	    *ofp;
957c478bd9Sstevel@tonic-gate 	char *tmpfile;
967c478bd9Sstevel@tonic-gate 	int fd;
977c478bd9Sstevel@tonic-gate 	int exit_status = 0;
987c478bd9Sstevel@tonic-gate 	int size;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	size = strlen(file) + 1 + 20;
1017c478bd9Sstevel@tonic-gate 	if ((tmpfile = malloc(size)) == NULL)
1027c478bd9Sstevel@tonic-gate 		return (-1);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (snprintf(tmpfile, size, "%sXXXXXX", file) >= size) {
1057c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "_file_put_printer:buffer overflow:tmpfile");
1067c478bd9Sstevel@tonic-gate 		return (-1);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* LINTED */
1107c478bd9Sstevel@tonic-gate 	while (1) {	/* syncronize writes */
111f4593de7SToomas Soome 		fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
1127c478bd9Sstevel@tonic-gate 		if ((fd < 0) && (errno == EEXIST))
1137c478bd9Sstevel@tonic-gate 			fd = open(file, O_RDWR);
1147c478bd9Sstevel@tonic-gate 		if (fd < 0) {
1157c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN)
1167c478bd9Sstevel@tonic-gate 				continue;
1177c478bd9Sstevel@tonic-gate 			free(tmpfile);
1187c478bd9Sstevel@tonic-gate 			return (-1);
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 		if (lockf(fd, F_TLOCK, 0) == 0)
1217c478bd9Sstevel@tonic-gate 			break;
12286b1a8baSrotondo 		(void) close(fd);
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	if ((ifp = fdopen(fd, "r")) == NULL) {
12686b1a8baSrotondo 		(void) close(fd);
1277c478bd9Sstevel@tonic-gate 		free(tmpfile);
1287c478bd9Sstevel@tonic-gate 		return (-1);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	if ((fd = mkstemp(tmpfile)) < 0) {
13286b1a8baSrotondo 		(void) fclose(ifp);
1337c478bd9Sstevel@tonic-gate 		free(tmpfile);
1347c478bd9Sstevel@tonic-gate 		return (-1);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	(void) fchmod(fd, 0644);
1387c478bd9Sstevel@tonic-gate 	if ((ofp = fdopen(fd, "wb+")) != NULL) {
1397c478bd9Sstevel@tonic-gate 		char buf[4096];
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		(void) fprintf(ofp,
1427c478bd9Sstevel@tonic-gate 	"#\n#\tIf you hand edit this file, comments and structure may change.\n"
1437c478bd9Sstevel@tonic-gate 	"#\tThe preferred method of modifying this file is through the use of\n"
144*bbf21555SRichard Lowe 	"#\tlpset(8)\n#\n");
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	/*
1477c478bd9Sstevel@tonic-gate 	 * Handle the special case of lpset -x all
1487c478bd9Sstevel@tonic-gate 	 * This deletes all entries in the file
1497c478bd9Sstevel@tonic-gate 	 * In this case, just don't write any entries to the tmpfile
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if (!((strcmp(printer->name, "all") == 0) &&
15336e852a1SRaja Andra 		    (printer->attributes == NULL))) {
1547c478bd9Sstevel@tonic-gate 			char *t, *entry, *pentry;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 			(void) _cvt_printer_to_entry((ns_printer_t *)printer,
15736e852a1SRaja Andra 			    buf, sizeof (buf));
1587c478bd9Sstevel@tonic-gate 			t = pentry = strdup(buf);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 			while (freadline(ifp, buf, sizeof (buf)) != NULL) {
1617c478bd9Sstevel@tonic-gate 				ns_printer_t *tmp = (ns_printer_t *)
16236e852a1SRaja Andra 				    _cvt_nss_entry_to_printer(buf, "");
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 				if (ns_printer_match_name(tmp, printer->name)
16536e852a1SRaja Andra 				    == 0) {
1667c478bd9Sstevel@tonic-gate 					entry = pentry;
1677c478bd9Sstevel@tonic-gate 					pentry = NULL;
168f4593de7SToomas Soome 				} else {
1697c478bd9Sstevel@tonic-gate 					entry = buf;
170f4593de7SToomas Soome 				}
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 				(void) fprintf(ofp, "%s\n", entry);
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			if (pentry != NULL)
1767c478bd9Sstevel@tonic-gate 				(void) fprintf(ofp, "%s\n", pentry);
1777c478bd9Sstevel@tonic-gate 			free(t);
1787c478bd9Sstevel@tonic-gate 		}
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		(void) fclose(ofp);
18186b1a8baSrotondo 		(void) rename(tmpfile, file);
1827c478bd9Sstevel@tonic-gate 	} else {
18386b1a8baSrotondo 		(void) close(fd);
18486b1a8baSrotondo 		(void) unlink(tmpfile);
1857c478bd9Sstevel@tonic-gate 		exit_status = -1;
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	(void) fclose(ifp);	/* releases the lock, after rename on purpose */
1897c478bd9Sstevel@tonic-gate 	(void) free(tmpfile);
1907c478bd9Sstevel@tonic-gate 	return (exit_status);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Support for writing a printer into the FILES /etc/printers.conf
1967c478bd9Sstevel@tonic-gate  * file.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate int
files_put_printer(const ns_printer_t * printer)1997c478bd9Sstevel@tonic-gate files_put_printer(const ns_printer_t *printer)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	static char *file = "/etc/printers.conf";
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	return (_file_put_printer(file, printer));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate /*
2077c478bd9Sstevel@tonic-gate  * Support for writing a printer into the NIS printers.conf.byname
2087c478bd9Sstevel@tonic-gate  * map.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
2127c478bd9Sstevel@tonic-gate #include <rpcsvc/ypclnt.h>
2137c478bd9Sstevel@tonic-gate #include <rpcsvc/yp_prot.h>
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Run the remote command.  We aren't interested in any io, Only the
2177c478bd9Sstevel@tonic-gate  * return code.
2187c478bd9Sstevel@tonic-gate  */
2197c478bd9Sstevel@tonic-gate static int
remote_command(char * command,char * host)2207c478bd9Sstevel@tonic-gate remote_command(char *command, char *host)
2217c478bd9Sstevel@tonic-gate {
2227c478bd9Sstevel@tonic-gate 	struct passwd *pw;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	if ((pw = getpwuid(getuid())) != NULL) {
2257c478bd9Sstevel@tonic-gate 		int fd;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if ((fd = rcmd_af(&host, htons(514), pw->pw_name, "root",
22836e852a1SRaja Andra 		    command, NULL, AF_INET6)) < 0)
2297c478bd9Sstevel@tonic-gate 			return (-1);
23086b1a8baSrotondo 		(void) close(fd);
2317c478bd9Sstevel@tonic-gate 		return (0);
232f4593de7SToomas Soome 	} else {
2337c478bd9Sstevel@tonic-gate 		return (-1);
234f4593de7SToomas Soome 	}
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate  * This isn't all that pretty, but you can update NIS if the machine this
2407c478bd9Sstevel@tonic-gate  * runs on is in the /.rhosts or /etc/hosts.equiv on the NIS master.
2417c478bd9Sstevel@tonic-gate  *   copy it local, update it, copy it remote
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate #define	TMP_PRINTERS_FILE	"/tmp/printers.NIS"
2447c478bd9Sstevel@tonic-gate #define	NIS_MAKEFILE		"/var/yp/Makefile"
2457c478bd9Sstevel@tonic-gate #define	MAKE_EXCERPT		"/usr/lib/print/Makefile.yp"
2467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2477c478bd9Sstevel@tonic-gate int
nis_put_printer(const ns_printer_t * printer)2487c478bd9Sstevel@tonic-gate nis_put_printer(const ns_printer_t *printer)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	static char	*domain = NULL;
2517c478bd9Sstevel@tonic-gate 	char *map = "printers.conf.byname";
2527c478bd9Sstevel@tonic-gate 	char *tmp = NULL;
2537c478bd9Sstevel@tonic-gate 	char *host = NULL;
2547c478bd9Sstevel@tonic-gate 	char lfile[BUFSIZ];
2557c478bd9Sstevel@tonic-gate 	char rfile[BUFSIZ];
2567c478bd9Sstevel@tonic-gate 	char cmd[BUFSIZ];
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (domain == NULL)
2597c478bd9Sstevel@tonic-gate 		(void) yp_get_default_domain(&domain);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	if ((yp_master(domain, (char *)map, &host) != 0) &&
2627c478bd9Sstevel@tonic-gate 	    (yp_master(domain, "passwd.byname", &host) != 0))
2637c478bd9Sstevel@tonic-gate 		return (-1);
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (snprintf(lfile, sizeof (lfile), "/tmp/%s", map) >=
26636e852a1SRaja Andra 	    sizeof (lfile)) {
2677c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nis_put_printer:lfile buffer overflow");
2687c478bd9Sstevel@tonic-gate 		return (-1);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	if (snprintf(rfile, sizeof (rfile), "root@%s:/etc/%s", host, map) >=
27136e852a1SRaja Andra 	    sizeof (rfile)) {
2727c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "nis_put_printer:rfile buffer overflow");
2737c478bd9Sstevel@tonic-gate 		return (-1);
2747c478bd9Sstevel@tonic-gate 	}
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if (((tmp = strrchr(rfile, '.')) != NULL) &&
2777c478bd9Sstevel@tonic-gate 	    (strcmp(tmp, ".byname") == 0))
278f4593de7SToomas Soome 		*tmp = '\0';	/* strip the .byname */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	/* copy it local */
2817c478bd9Sstevel@tonic-gate 	if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1",
28236e852a1SRaja Andra 	    rfile, lfile) >= sizeof (cmd)) {
28336e852a1SRaja Andra 		syslog(LOG_ERR,
28436e852a1SRaja Andra 		    "nis_put_printer:buffer overflow building cmd");
28536e852a1SRaja Andra 		return (-1);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	(void) system(cmd);	/* could fail because it doesn't exist */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/* update it */
2917c478bd9Sstevel@tonic-gate 	if (_file_put_printer(lfile, printer) != 0)
2927c478bd9Sstevel@tonic-gate 		return (-1);
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	/* copy it back */
2957c478bd9Sstevel@tonic-gate 	if (snprintf(cmd, sizeof (cmd), "rcp %s %s >/dev/null 2>&1",
29636e852a1SRaja Andra 	    lfile, rfile) >= sizeof (cmd)) {
29736e852a1SRaja Andra 		syslog(LOG_ERR,
29836e852a1SRaja Andra 		    "nis_put_printer:buffer overflow building cmd");
29936e852a1SRaja Andra 		return (-1);
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	if (system(cmd) != 0)
3027c478bd9Sstevel@tonic-gate 		return (-1);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/* copy the Makefile excerpt */
3057c478bd9Sstevel@tonic-gate 	if (snprintf(cmd, sizeof (cmd),
30636e852a1SRaja Andra 	    "rcp %s root@%s:%s.print >/dev/null 2>&1",
30736e852a1SRaja Andra 	    MAKE_EXCERPT, host, NIS_MAKEFILE) >= sizeof (cmd)) {
3087c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
30936e852a1SRaja Andra 		    "nis_put_printer:buffer overflow building cmd");
3107c478bd9Sstevel@tonic-gate 		return (-1);
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	if (system(cmd) != 0)
3147c478bd9Sstevel@tonic-gate 		return (-1);
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	/* run the make */
3177c478bd9Sstevel@tonic-gate 	if (snprintf(cmd, sizeof (cmd),
31836e852a1SRaja Andra 	    "/bin/sh -c 'PATH=/usr/ccs/bin:/bin:/usr/bin:$PATH "
31936e852a1SRaja Andra 	    "make -f %s -f %s.print printers.conf >/dev/null 2>&1'",
32036e852a1SRaja Andra 	    NIS_MAKEFILE, NIS_MAKEFILE) >= sizeof (cmd)) {
3217c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR,
32236e852a1SRaja Andra 		    "nis_put_printer:buffer overflow on make");
3237c478bd9Sstevel@tonic-gate 		return (-1);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return (remote_command(cmd, host));
3277c478bd9Sstevel@tonic-gate }
328