xref: /illumos-gate/usr/src/cmd/print/lpset/lpset.c (revision 53ac4dca)
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
5355b4669Sjacobs  * Common Development and Distribution License (the "License").
6355b4669Sjacobs  * 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 /*
22*53ac4dcaSjacobs  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <stdlib.h>
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <unistd.h>
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <locale.h>
387c478bd9Sstevel@tonic-gate #ifndef SUNOS_4
397c478bd9Sstevel@tonic-gate #include <libintl.h>
407c478bd9Sstevel@tonic-gate #endif
417c478bd9Sstevel@tonic-gate #include <pwd.h>
427c478bd9Sstevel@tonic-gate 
43355b4669Sjacobs #include <ns.h>
44355b4669Sjacobs #include <misc.h>
45355b4669Sjacobs #include <list.h>
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate extern char *optarg;
487c478bd9Sstevel@tonic-gate extern int optind, opterr, optopt;
497c478bd9Sstevel@tonic-gate extern char *getenv(const char *);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static void _decode_ldapResult(int result, char *printerName);
527c478bd9Sstevel@tonic-gate 
53*53ac4dcaSjacobs static int
54*53ac4dcaSjacobs authorized()
55*53ac4dcaSjacobs {
56*53ac4dcaSjacobs 	struct passwd *pw;
57*53ac4dcaSjacobs 	uid_t uid;
58*53ac4dcaSjacobs 	gid_t list[NGROUPS_MAX];
59*53ac4dcaSjacobs 	int len;
60*53ac4dcaSjacobs 
61*53ac4dcaSjacobs 	if ((uid = getuid()) == 0)
62*53ac4dcaSjacobs 		return (1);	/* "root" is authorized */
63*53ac4dcaSjacobs 
64*53ac4dcaSjacobs 	if (((pw = getpwnam("lp")) != NULL) && (uid == pw->pw_uid))
65*53ac4dcaSjacobs 		return (1);	/* "lp" is authorized */
66*53ac4dcaSjacobs 
67*53ac4dcaSjacobs 	if ((pw = getpwuid(uid)) == NULL)
68*53ac4dcaSjacobs 		return (0);	/* intruders are not authorized */
69*53ac4dcaSjacobs 
70*53ac4dcaSjacobs 	if (chkauthattr("solaris.print.admin", pw->pw_name) == 1)
71*53ac4dcaSjacobs 		return (1);	/* "solaris.print.admin" is authorized */
72*53ac4dcaSjacobs 
73*53ac4dcaSjacobs 	if ((len = getgroups(sizeof (list), list)) != -1)
74*53ac4dcaSjacobs 		for (; len >= 0; len--)
75*53ac4dcaSjacobs 			if (list[len] == 14)
76*53ac4dcaSjacobs 				return (1);	/* group 14 is authorized */
77*53ac4dcaSjacobs 
78*53ac4dcaSjacobs 	return (0);	/* nobody else is authorized */
79*53ac4dcaSjacobs }
80*53ac4dcaSjacobs 
817c478bd9Sstevel@tonic-gate static void
827c478bd9Sstevel@tonic-gate Usage(char *name)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
857c478bd9Sstevel@tonic-gate 		gettext("Usage: %s [-n files | nisplus | ldap] [-x] "
867c478bd9Sstevel@tonic-gate 			"[-h ldaphost] [-D binddn] [-w passwd] "
877c478bd9Sstevel@tonic-gate 			"[-a key=value] [-d key] (printer)\n"),
887c478bd9Sstevel@tonic-gate 		name);
897c478bd9Sstevel@tonic-gate 	exit(1);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  *  main() calls the appropriate routine to parse the command line arguments
957c478bd9Sstevel@tonic-gate  *	and then calls the local remove routine, followed by the remote remove
967c478bd9Sstevel@tonic-gate  *	routine to remove jobs.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate int
997c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	int result = 0;
1027c478bd9Sstevel@tonic-gate 	int delete_printer = 0;
1037c478bd9Sstevel@tonic-gate 	int c;
1047c478bd9Sstevel@tonic-gate 	char	*program = NULL,
1057c478bd9Sstevel@tonic-gate 		*printer = NULL,
1067c478bd9Sstevel@tonic-gate 		*host = NULL,
1077c478bd9Sstevel@tonic-gate 		*binddn = NULL,
1087c478bd9Sstevel@tonic-gate 		*passwd = NULL,
1097c478bd9Sstevel@tonic-gate 		*ins = NULL,
1107c478bd9Sstevel@tonic-gate 		*ons = "files";
1117c478bd9Sstevel@tonic-gate 	char	**changes = NULL;
1127c478bd9Sstevel@tonic-gate 	ns_cred_t	*cred = NULL;
1137c478bd9Sstevel@tonic-gate 	ns_printer_t 	*printer_obj = NULL;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
1187c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((program = strrchr(av[0], '/')) == NULL)
1237c478bd9Sstevel@tonic-gate 		program = av[0];
1247c478bd9Sstevel@tonic-gate 	else
1257c478bd9Sstevel@tonic-gate 		program++;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	openlog(program, LOG_PID, LOG_LPR);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	if (ac < 2)
1307c478bd9Sstevel@tonic-gate 		Usage(program);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	while ((c = getopt(ac, av, "a:d:D:h:n:r:w:x")) != EOF)
1337c478bd9Sstevel@tonic-gate 		switch (c) {
1347c478bd9Sstevel@tonic-gate 		case 'd':
1357c478bd9Sstevel@tonic-gate 			if (strchr(optarg, '=') != NULL)
1367c478bd9Sstevel@tonic-gate 				Usage(program);
1377c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
1387c478bd9Sstevel@tonic-gate 		case 'a':
1397c478bd9Sstevel@tonic-gate 			changes = (char **)list_append((void**)changes,
1407c478bd9Sstevel@tonic-gate 						(void *)strdup(optarg));
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		case 'D':
1437c478bd9Sstevel@tonic-gate 			binddn = optarg;
1447c478bd9Sstevel@tonic-gate 			break;
1457c478bd9Sstevel@tonic-gate 		case 'h':
1467c478bd9Sstevel@tonic-gate 			host = optarg;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case 'n':
1497c478bd9Sstevel@tonic-gate 			ons = optarg;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		case 'r':
1527c478bd9Sstevel@tonic-gate 			ins = optarg;
1537c478bd9Sstevel@tonic-gate 			break;
1547c478bd9Sstevel@tonic-gate 		case 'w':
1557c478bd9Sstevel@tonic-gate 			passwd = optarg;
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 		case 'x':
1587c478bd9Sstevel@tonic-gate 			delete_printer++;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		default:
1617c478bd9Sstevel@tonic-gate 			Usage(program);
1627c478bd9Sstevel@tonic-gate 		}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (optind != ac-1)
1657c478bd9Sstevel@tonic-gate 		Usage(program);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * Check required options have been given: [ -x | [ -a | -d ]]
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	if ((changes == NULL) && (delete_printer == 0)) {
1717c478bd9Sstevel@tonic-gate 		Usage(program);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	printer = av[optind];
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (strchr(printer, ':') != NULL) {
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
1787c478bd9Sstevel@tonic-gate 			"POSIX-Style names are not valid destinations (%s)\n"),
1797c478bd9Sstevel@tonic-gate 			printer);
1807c478bd9Sstevel@tonic-gate 		return (1);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	ins = normalize_ns_name(ins);
1847c478bd9Sstevel@tonic-gate 	ons = normalize_ns_name(ons);
1857c478bd9Sstevel@tonic-gate 	if (ins == NULL)
1867c478bd9Sstevel@tonic-gate 		ins = ons;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/* check / set the name service for writing */
1897c478bd9Sstevel@tonic-gate 	if (strcasecmp("user", ons) == 0) {
1907c478bd9Sstevel@tonic-gate 		(void) setuid(getuid());
1917c478bd9Sstevel@tonic-gate 		ons = "user";
1927c478bd9Sstevel@tonic-gate 	} else if (strcasecmp("files", ons) == 0) {
193*53ac4dcaSjacobs 		if (authorized() == 0) {
1947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
195*53ac4dcaSjacobs 				"Permission denied: not authorized\n"));
1967c478bd9Sstevel@tonic-gate 			return (1);
1977c478bd9Sstevel@tonic-gate 		}
1987c478bd9Sstevel@tonic-gate 		ons = "files";
1997c478bd9Sstevel@tonic-gate 	} else if (strcasecmp("nisplus", ons) == 0) {
2007c478bd9Sstevel@tonic-gate 		ons = "nisplus";
2017c478bd9Sstevel@tonic-gate 	} else if (strcasecmp("ldap", ons) == 0) {
2027c478bd9Sstevel@tonic-gate 		if ((cred = calloc(1, sizeof (*cred))) == NULL) {
2037c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2047c478bd9Sstevel@tonic-gate 				gettext("could not initialize credential\n"));
2057c478bd9Sstevel@tonic-gate 			return (1);
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if (binddn == NULL) {
2097c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2107c478bd9Sstevel@tonic-gate 			    gettext("Distinguished Name is required.\n"));
2117c478bd9Sstevel@tonic-gate 			return (1);
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		if (passwd == NULL) {
2157c478bd9Sstevel@tonic-gate 			passwd = getpassphrase(gettext("Bind Password:"));
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		/*
2197c478bd9Sstevel@tonic-gate 		 * Setup LDAP bind credentials, so that it uses
2207c478bd9Sstevel@tonic-gate 		 * the default ldap port, and the NS domain for this
2217c478bd9Sstevel@tonic-gate 		 * ldapclient box. Note: passwdType is currently not
2227c478bd9Sstevel@tonic-gate 		 * used but once the ldap native function can select
2237c478bd9Sstevel@tonic-gate 		 * secure or insure password it will pass the user selected
2247c478bd9Sstevel@tonic-gate 		 * security type.
2257c478bd9Sstevel@tonic-gate 		 */
2267c478bd9Sstevel@tonic-gate 		cred->passwd = passwd;
2277c478bd9Sstevel@tonic-gate 		cred->passwdType = NS_PW_INSECURE; /* use default */
2287c478bd9Sstevel@tonic-gate 		cred->binddn = binddn;
2297c478bd9Sstevel@tonic-gate 		cred->host = host;
2307c478bd9Sstevel@tonic-gate 		cred->port = 0;		/* use default */
2317c478bd9Sstevel@tonic-gate 		cred->domainDN = NULL;	/* use default */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		ons = "ldap";
2347c478bd9Sstevel@tonic-gate 		(void) setuid(getuid());
2357c478bd9Sstevel@tonic-gate 	} else {
2367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2377c478bd9Sstevel@tonic-gate 			gettext("%s is not a supported name service.\n"),
2387c478bd9Sstevel@tonic-gate 			ons);
2397c478bd9Sstevel@tonic-gate 		return (1);
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (strcasecmp(NS_SVC_LDAP, ons) != 0) {
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	    /* Naming Service is not LDAP */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	    /* get the printer object */
2477c478bd9Sstevel@tonic-gate 	    if ((printer_obj = ns_printer_get_name(printer, ins)) == NULL) {
2487c478bd9Sstevel@tonic-gate 		if (delete_printer != 0) {
2497c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("%s: unknown printer\n"),
2507c478bd9Sstevel@tonic-gate 				printer);
2517c478bd9Sstevel@tonic-gate 			return (1);
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		if ((printer_obj = calloc(1, sizeof (*printer_obj))) == NULL) {
2547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
2557c478bd9Sstevel@tonic-gate 				"could not initialize printer object\n"));
2567c478bd9Sstevel@tonic-gate 			return (1);
2577c478bd9Sstevel@tonic-gate 		}
2587c478bd9Sstevel@tonic-gate 		printer_obj->name = strdup(printer);
2597c478bd9Sstevel@tonic-gate 	    }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	    printer_obj->source = ons;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	    if (cred != NULL) {
2647c478bd9Sstevel@tonic-gate 		printer_obj->cred = cred;
2657c478bd9Sstevel@tonic-gate 	    }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	    /* make the changes to it */
2687c478bd9Sstevel@tonic-gate 	    while (changes != NULL && *changes != NULL) {
2697c478bd9Sstevel@tonic-gate 		int has_equals = (strchr(*changes, '=') != NULL);
2707c478bd9Sstevel@tonic-gate 		char *p, *key = NULL, *value = NULL;
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		key = *(changes++);
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		for (p = key; ((p != NULL) && (*p != NULL)); p++)
2757c478bd9Sstevel@tonic-gate 			if (*p == '=') {
2767c478bd9Sstevel@tonic-gate 				*p = NULL;
2777c478bd9Sstevel@tonic-gate 				value = ++p;
2787c478bd9Sstevel@tonic-gate 				break;
2797c478bd9Sstevel@tonic-gate 			} else if (*p == '\\')
2807c478bd9Sstevel@tonic-gate 				p++;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		if ((value != NULL) && (*value == NULL))
2837c478bd9Sstevel@tonic-gate 			value = NULL;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		if ((key != NULL) && (key[0] != NULL)) {
2867c478bd9Sstevel@tonic-gate 			if ((value == NULL) &&
2877c478bd9Sstevel@tonic-gate 			    (ns_get_value(key, printer_obj) == NULL) &&
2887c478bd9Sstevel@tonic-gate 			    (has_equals == 0)) {
2897c478bd9Sstevel@tonic-gate 				fprintf(stderr,
2907c478bd9Sstevel@tonic-gate 					gettext("%s: unknown attribute\n"),
2917c478bd9Sstevel@tonic-gate 					key);
2927c478bd9Sstevel@tonic-gate 				result = 1;
2937c478bd9Sstevel@tonic-gate 			} else
2947c478bd9Sstevel@tonic-gate 			(void) ns_set_value_from_string(key, value,
2957c478bd9Sstevel@tonic-gate 				printer_obj);
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	    }
2987c478bd9Sstevel@tonic-gate 	    if (delete_printer != 0)
2997c478bd9Sstevel@tonic-gate 		printer_obj->attributes = NULL;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	    /* write it back */
3027c478bd9Sstevel@tonic-gate 	    if (ns_printer_put(printer_obj) != 0) {
3037c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
3047c478bd9Sstevel@tonic-gate 				gettext("Failed to write into %s database\n"),
3057c478bd9Sstevel@tonic-gate 				ons);
3067c478bd9Sstevel@tonic-gate 		result = 1;
3077c478bd9Sstevel@tonic-gate 	    }
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	else {
3117c478bd9Sstevel@tonic-gate 		/*
3127c478bd9Sstevel@tonic-gate 		 * Naming Service is LDAP
3137c478bd9Sstevel@tonic-gate 		 *
3147c478bd9Sstevel@tonic-gate 		 * Action the request by calling ns ldap functions to
3157c478bd9Sstevel@tonic-gate 		 * add, modify or delete the printer object.
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		if ((printer_obj = calloc(1, sizeof (*printer_obj))) == NULL) {
3197c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
3207c478bd9Sstevel@tonic-gate 				"could not initialize printer object\n"));
3217c478bd9Sstevel@tonic-gate 			return (1);
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		if ((cred != NULL) && (printer_obj != NULL)) {
3257c478bd9Sstevel@tonic-gate 			printer_obj->name = strdup(printer);
3267c478bd9Sstevel@tonic-gate 			printer_obj->cred = cred;
3277c478bd9Sstevel@tonic-gate 			printer_obj->cred->domainDN = NULL; /* use default */
3287c478bd9Sstevel@tonic-gate 			printer_obj->source = ons;
3297c478bd9Sstevel@tonic-gate 			printer_obj->nsdata = malloc(sizeof (NS_LDAPDATA));
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 			if (printer_obj->nsdata != NULL) {
3327c478bd9Sstevel@tonic-gate 				/*
3337c478bd9Sstevel@tonic-gate 				 * Update the LDAP directory for this printer
3347c478bd9Sstevel@tonic-gate 				 */
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 				if (delete_printer != 0) {
3377c478bd9Sstevel@tonic-gate 					/* Delete the printer object */
3387c478bd9Sstevel@tonic-gate 					((NS_LDAPDATA *)
3397c478bd9Sstevel@tonic-gate 					(printer_obj->nsdata))->attrList = NULL;
3407c478bd9Sstevel@tonic-gate 				} else {
3417c478bd9Sstevel@tonic-gate 					/* Add or modify the printer object */
3427c478bd9Sstevel@tonic-gate 					((NS_LDAPDATA *)
3437c478bd9Sstevel@tonic-gate 					(printer_obj->nsdata))->attrList =
3447c478bd9Sstevel@tonic-gate 									changes;
3457c478bd9Sstevel@tonic-gate 				}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 				result = ns_printer_put(printer_obj);
3487c478bd9Sstevel@tonic-gate 				if (result != 0) {
3497c478bd9Sstevel@tonic-gate 					/* display LDAP specific message */
3507c478bd9Sstevel@tonic-gate 					_decode_ldapResult(result, printer);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
3537c478bd9Sstevel@tonic-gate 					"Failed to update %s database\n"), ons);
3547c478bd9Sstevel@tonic-gate 					result = 1;
3557c478bd9Sstevel@tonic-gate 				}
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 				free(printer_obj->nsdata);
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 			else {
3617c478bd9Sstevel@tonic-gate 				_decode_ldapResult(NSL_ERR_MEMORY, NULL);
3627c478bd9Sstevel@tonic-gate 				result = 1;
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 		}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 		else {
3677c478bd9Sstevel@tonic-gate 			result = 1;
3687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3697c478bd9Sstevel@tonic-gate 				gettext("Error - no LDAP credentials\n"));
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 		if (printer_obj != NULL) {
3737c478bd9Sstevel@tonic-gate 			if (printer_obj->name != NULL) {
3747c478bd9Sstevel@tonic-gate 				free(printer_obj->name);
3757c478bd9Sstevel@tonic-gate 			}
3767c478bd9Sstevel@tonic-gate 			free(printer_obj);
3777c478bd9Sstevel@tonic-gate 		}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	return (result);
3827c478bd9Sstevel@tonic-gate } /* main */
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * *****************************************************************************
3897c478bd9Sstevel@tonic-gate  *
3907c478bd9Sstevel@tonic-gate  * Function:    _decode_ldapResult()
3917c478bd9Sstevel@tonic-gate  *
3927c478bd9Sstevel@tonic-gate  * Description: Decode the ldap_put_printer specific error codes and display
3937c478bd9Sstevel@tonic-gate  *              the appropriate error message.
3947c478bd9Sstevel@tonic-gate  *
3957c478bd9Sstevel@tonic-gate  * Parameters:
3967c478bd9Sstevel@tonic-gate  * Input:       int result - contains the NSL_RESULT codes
3977c478bd9Sstevel@tonic-gate  *              char *printerName - name of printer
3987c478bd9Sstevel@tonic-gate  * Output:      None
3997c478bd9Sstevel@tonic-gate  *
4007c478bd9Sstevel@tonic-gate  * Returns:     void
4017c478bd9Sstevel@tonic-gate  *
4027c478bd9Sstevel@tonic-gate  * *****************************************************************************
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate _decode_ldapResult(int result, char *printerName)
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	NSL_RESULT lresult = (NSL_RESULT)result;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/* ------------- */
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 	switch (lresult)
4147c478bd9Sstevel@tonic-gate 	{
4157c478bd9Sstevel@tonic-gate 		case NSL_OK:
4167c478bd9Sstevel@tonic-gate 		{
4177c478bd9Sstevel@tonic-gate 			break;
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 		case NSL_ERR_INTERNAL:
4217c478bd9Sstevel@tonic-gate 		{
4227c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
4237c478bd9Sstevel@tonic-gate 				gettext("Unexpected software error\n"));
4247c478bd9Sstevel@tonic-gate 			break;
4257c478bd9Sstevel@tonic-gate 		}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 		case NSL_ERR_ADD_FAILED:
4287c478bd9Sstevel@tonic-gate 		{
4297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s\n",
4307c478bd9Sstevel@tonic-gate 				gettext("Failed to add printer:"), printerName);
4317c478bd9Sstevel@tonic-gate 			break;
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		case NSL_ERR_MOD_FAILED:
4357c478bd9Sstevel@tonic-gate 		{
4367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s\n",
4377c478bd9Sstevel@tonic-gate 				gettext("Failed to modify printer:"),
4387c478bd9Sstevel@tonic-gate 					printerName);
4397c478bd9Sstevel@tonic-gate 			break;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		case NSL_ERR_DEL_FAILED:
4437c478bd9Sstevel@tonic-gate 		{
4447c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s\n",
4457c478bd9Sstevel@tonic-gate 				gettext("Failed to delete printer:"),
4467c478bd9Sstevel@tonic-gate 					printerName);
4477c478bd9Sstevel@tonic-gate 			break;
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 		case NSL_ERR_UNKNOWN_PRINTER:
4527c478bd9Sstevel@tonic-gate 		{
4537c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s\n",
4547c478bd9Sstevel@tonic-gate 				gettext("Unknown printer:"), printerName);
4557c478bd9Sstevel@tonic-gate 			break;
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		case NSL_ERR_CREDENTIALS:
4597c478bd9Sstevel@tonic-gate 		{
4607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n",
4617c478bd9Sstevel@tonic-gate 		gettext("Missing LDAP credential information for printer:"));
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		case NSL_ERR_CONNECT:
4667c478bd9Sstevel@tonic-gate 		{
4677c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s\n",
4687c478bd9Sstevel@tonic-gate 				gettext("Failed to connect to LDAP server"));
4697c478bd9Sstevel@tonic-gate 			break;
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		case NSL_ERR_BIND:
4737c478bd9Sstevel@tonic-gate 		{
4747c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("LDAP bind failed\n"));
4757c478bd9Sstevel@tonic-gate 			break;
4767c478bd9Sstevel@tonic-gate 		}
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 		case NSL_ERR_RENAME:
4797c478bd9Sstevel@tonic-gate 		{
4807c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s\n",
4817c478bd9Sstevel@tonic-gate 			    gettext("Object rename not allowed for printer:"),
4827c478bd9Sstevel@tonic-gate 			    printerName);
4837c478bd9Sstevel@tonic-gate 			break;
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 		case NSL_ERR_KVP:
4877c478bd9Sstevel@tonic-gate 		{
4887c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s",
4897c478bd9Sstevel@tonic-gate 			    gettext("Setting sun-printer-kvp attribute is "
4907c478bd9Sstevel@tonic-gate 				"not supported through this command.\n"));
4917c478bd9Sstevel@tonic-gate 			break;
4927c478bd9Sstevel@tonic-gate 		}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		case NSL_ERR_BSDADDR:
4957c478bd9Sstevel@tonic-gate 		{
4967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s",
4977c478bd9Sstevel@tonic-gate 			    gettext("Setting sun-printer-bsdaddr attribute is "
4987c478bd9Sstevel@tonic-gate 				"not supported through this command.\n"
4997c478bd9Sstevel@tonic-gate 				"Use the bsaddr attribute instead.\n"));
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		case NSL_ERR_PNAME:
5047c478bd9Sstevel@tonic-gate 		{
5057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s",
5067c478bd9Sstevel@tonic-gate 			    gettext("Setting printer-name attribute is "
5077c478bd9Sstevel@tonic-gate 				"not supported through this command.\n"));
5087c478bd9Sstevel@tonic-gate 			break;
5097c478bd9Sstevel@tonic-gate 		}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		case NSL_ERR_MEMORY:
5127c478bd9Sstevel@tonic-gate 		{
5137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5147c478bd9Sstevel@tonic-gate 					gettext("Memory allocation error\n"));
5157c478bd9Sstevel@tonic-gate 			break;
5167c478bd9Sstevel@tonic-gate 		}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 		case NSL_ERR_MULTIOP:
5197c478bd9Sstevel@tonic-gate 		{
5207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5217c478bd9Sstevel@tonic-gate 				gettext("Delete and add operation on the "
5227c478bd9Sstevel@tonic-gate 					"same key attribute is not allowed\n"));
5237c478bd9Sstevel@tonic-gate 			break;
5247c478bd9Sstevel@tonic-gate 		}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 		case NSL_ERR_NOTALLOWED:
5277c478bd9Sstevel@tonic-gate 		{
5287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5297c478bd9Sstevel@tonic-gate 				gettext("KVP attribute is not allowed\n"));
5307c478bd9Sstevel@tonic-gate 			break;
5317c478bd9Sstevel@tonic-gate 		}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 		default:
5347c478bd9Sstevel@tonic-gate 		{
5357c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
5367c478bd9Sstevel@tonic-gate 					gettext("Error code = %d\n"), result);
5377c478bd9Sstevel@tonic-gate 			break;
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate } /* _decode_ldapResult */
542