4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 #include <sys/types.h>
46 extern int optind
, opterr
, optopt
;
47 extern char *getenv(const char *);
49 static void _decode_ldapResult(int result
, char *printerName
);
60 if ((uid
= getuid()) == 0)
61 return (1); /* "root" is authorized */
63 if (((pw
= getpwnam("lp")) != NULL
) && (uid
== pw
->pw_uid
))
64 return (1); /* "lp" is authorized */
66 if ((pw
= getpwuid(uid
)) == NULL
)
67 return (0); /* intruders are not authorized */
69 if (chkauthattr("solaris.print.admin", pw
->pw_name
) == 1)
70 return (1); /* "solaris.print.admin" is authorized */
72 /* How many supplemental groups do we have? */
73 maxgrp
= getgroups(0, NULL
);
74 list
= alloca(maxgrp
* sizeof (gid_t
));
76 if ((len
= getgroups(maxgrp
, list
)) != -1)
79 return (1); /* group 14 is authorized */
81 return (0); /* nobody else is authorized */
87 (void) fprintf(stderr
,
88 gettext("Usage: %s [-n files | ldap] [-x] "
89 "[-h ldaphost] [-D binddn] [-w passwd] "
90 "[-a key=value] [-d key] (printer)\n"),
97 * main() calls the appropriate routine to parse the command line arguments
98 * and then calls the local remove routine, followed by the remote remove
99 * routine to remove jobs.
102 main(int ac
, char *av
[])
105 int delete_printer
= 0;
107 char *program
= NULL
,
114 char **changes
= NULL
;
115 ns_cred_t
*cred
= NULL
;
116 ns_printer_t
*printer_obj
= NULL
;
118 (void) setlocale(LC_ALL
, "");
120 #if !defined(TEXT_DOMAIN)
121 #define TEXT_DOMAIN "SYS_TEST"
123 (void) textdomain(TEXT_DOMAIN
);
125 if ((program
= strrchr(av
[0], '/')) == NULL
)
130 openlog(program
, LOG_PID
, LOG_LPR
);
135 while ((c
= getopt(ac
, av
, "a:d:D:h:n:r:w:x")) != EOF
)
138 if (strchr(optarg
, '=') != NULL
)
142 changes
= (char **)list_append((void**)changes
,
143 (void *)strdup(optarg
));
171 * Check required options have been given: [ -x | [ -a | -d ]]
173 if ((changes
== NULL
) && (delete_printer
== 0)) {
177 printer
= av
[optind
];
179 if (strchr(printer
, ':') != NULL
) {
180 (void) fprintf(stderr
, gettext(
181 "POSIX-Style names are not valid destinations (%s)\n"),
186 ins
= normalize_ns_name(ins
);
187 ons
= normalize_ns_name(ons
);
191 /* check / set the name service for writing */
192 if (strcasecmp("user", ons
) == 0) {
193 (void) setuid(getuid());
195 } else if (strcasecmp("files", ons
) == 0) {
196 if (authorized() == 0) {
197 (void) fprintf(stderr
, gettext(
198 "Permission denied: not authorized\n"));
202 } else if (strcasecmp("ldap", ons
) == 0) {
203 if ((cred
= calloc(1, sizeof (*cred
))) == NULL
) {
204 (void) fprintf(stderr
,
205 gettext("could not initialize credential\n"));
209 if (binddn
== NULL
) {
210 (void) fprintf(stderr
,
211 gettext("Distinguished Name is required.\n"));
215 if (passwd
== NULL
) {
216 passwd
= getpassphrase(gettext("Bind Password:"));
220 * Setup LDAP bind credentials, so that it uses
221 * the default ldap port, and the NS domain for this
222 * ldapclient box. Note: passwdType is currently not
223 * used but once the ldap native function can select
224 * secure or insure password it will pass the user selected
227 cred
->passwd
= passwd
;
228 cred
->passwdType
= NS_PW_INSECURE
; /* use default */
229 cred
->binddn
= binddn
;
231 cred
->port
= 0; /* use default */
232 cred
->domainDN
= NULL
; /* use default */
235 (void) setuid(getuid());
237 (void) fprintf(stderr
,
238 gettext("%s is not a supported name service.\n"),
243 if (strcasecmp(NS_SVC_LDAP
, ons
) != 0) {
245 /* Naming Service is not LDAP */
247 /* get the printer object */
248 if ((printer_obj
= ns_printer_get_name(printer
, ins
)) == NULL
) {
249 if (delete_printer
!= 0) {
250 (void) fprintf(stderr
, gettext
251 ("%s: unknown printer\n"), printer
);
254 if ((printer_obj
= calloc(1, sizeof (*printer_obj
)))
256 (void) fprintf(stderr
, gettext(
257 "could not initialize printer object\n"));
260 printer_obj
->name
= strdup(printer
);
263 printer_obj
->source
= ons
;
266 printer_obj
->cred
= cred
;
269 /* make the changes to it */
270 while (changes
!= NULL
&& *changes
!= NULL
) {
271 int has_equals
= (strchr(*changes
, '=') != NULL
);
272 char *p
, *key
= NULL
, *value
= NULL
;
276 for (p
= key
; ((p
!= NULL
) && (*p
!= '\0')); p
++)
281 } else if (*p
== '\\')
284 if ((value
!= NULL
) && (*value
== '\0'))
287 if ((key
!= NULL
) && (key
[0] != '\0')) {
288 if ((value
== NULL
) &&
289 (ns_get_value(key
, printer_obj
) == NULL
) &&
292 gettext("%s: unknown attribute\n"),
296 (void) ns_set_value_from_string(key
, value
,
300 if (delete_printer
!= 0)
301 printer_obj
->attributes
= NULL
;
304 if (ns_printer_put(printer_obj
) != 0) {
305 (void) fprintf(stderr
,
306 gettext("Failed to write into %s database\n"),
314 * Naming Service is LDAP
316 * Action the request by calling ns ldap functions to
317 * add, modify or delete the printer object.
320 if ((printer_obj
= calloc(1, sizeof (*printer_obj
))) == NULL
) {
321 (void) fprintf(stderr
, gettext(
322 "could not initialize printer object\n"));
326 if ((cred
!= NULL
) && (printer_obj
!= NULL
)) {
327 printer_obj
->name
= strdup(printer
);
328 printer_obj
->cred
= cred
;
329 printer_obj
->cred
->domainDN
= NULL
; /* use default */
330 printer_obj
->source
= ons
;
331 printer_obj
->nsdata
= malloc(sizeof (NS_LDAPDATA
));
333 if (printer_obj
->nsdata
!= NULL
) {
335 * Update the LDAP directory for this printer
338 if (delete_printer
!= 0) {
339 /* Delete the printer object */
341 (printer_obj
->nsdata
))->attrList
344 /* Add or modify the printer object */
346 (printer_obj
->nsdata
))->attrList
=
350 result
= ns_printer_put(printer_obj
);
352 /* display LDAP specific message */
353 _decode_ldapResult(result
, printer
);
355 (void) fprintf(stderr
, gettext(
356 "Failed to update %s database\n"), ons
);
360 free(printer_obj
->nsdata
);
364 _decode_ldapResult(NSL_ERR_MEMORY
, NULL
);
371 (void) fprintf(stderr
,
372 gettext("Error - no LDAP credentials\n"));
375 if (printer_obj
!= NULL
) {
376 if (printer_obj
->name
!= NULL
) {
377 free(printer_obj
->name
);
391 * *****************************************************************************
393 * Function: _decode_ldapResult()
395 * Description: Decode the ldap_put_printer specific error codes and display
396 * the appropriate error message.
399 * Input: int result - contains the NSL_RESULT codes
400 * char *printerName - name of printer
405 * *****************************************************************************
409 _decode_ldapResult(int result
, char *printerName
)
412 NSL_RESULT lresult
= (NSL_RESULT
)result
;
423 case NSL_ERR_INTERNAL
:
425 (void) fprintf(stderr
,
426 gettext("Unexpected software error\n"));
430 case NSL_ERR_ADD_FAILED
:
432 (void) fprintf(stderr
, "%s %s\n",
433 gettext("Failed to add printer:"), printerName
);
437 case NSL_ERR_MOD_FAILED
:
439 (void) fprintf(stderr
, "%s %s\n",
440 gettext("Failed to modify printer:"),
445 case NSL_ERR_DEL_FAILED
:
447 (void) fprintf(stderr
, "%s %s\n",
448 gettext("Failed to delete printer:"),
454 case NSL_ERR_UNKNOWN_PRINTER
:
456 (void) fprintf(stderr
, "%s %s\n",
457 gettext("Unknown printer:"), printerName
);
461 case NSL_ERR_CREDENTIALS
:
463 (void) fprintf(stderr
, "%s\n",
464 gettext("Missing LDAP credential information for printer:"));
468 case NSL_ERR_CONNECT
:
470 (void) fprintf(stderr
, "%s\n",
471 gettext("Failed to connect to LDAP server"));
477 (void) fprintf(stderr
, gettext("LDAP bind failed\n"));
483 (void) fprintf(stderr
, "%s %s\n",
484 gettext("Object rename not allowed for printer:"),
491 (void) fprintf(stderr
, "%s",
492 gettext("Setting sun-printer-kvp attribute is "
493 "not supported through this command.\n"));
497 case NSL_ERR_BSDADDR
:
499 (void) fprintf(stderr
, "%s",
500 gettext("Setting sun-printer-bsdaddr attribute is "
501 "not supported through this command.\n"
502 "Use the bsaddr attribute instead.\n"));
508 (void) fprintf(stderr
, "%s",
509 gettext("Setting printer-name attribute is "
510 "not supported through this command.\n"));
516 (void) fprintf(stderr
,
517 gettext("Memory allocation error\n"));
521 case NSL_ERR_MULTIOP
:
523 (void) fprintf(stderr
,
524 gettext("Delete and add operation on the "
525 "same key attribute is not allowed\n"));
529 case NSL_ERR_NOTALLOWED
:
531 (void) fprintf(stderr
,
532 gettext("KVP attribute is not allowed\n"));
538 (void) fprintf(stderr
,
539 gettext("Error code = %d\n"), result
);
544 } /* _decode_ldapResult */