1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdarg.h>
#include <unistd.h>
#include <limits.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <locale.h>
#ifndef SUNOS_4
#include <libintl.h>
#endif
#include <pwd.h>
#include <alloca.h>
#include <ns.h>
#include <list.h>
extern char *optarg;
extern int optind, opterr, optopt;
extern char *getenv(const char *);
static void _decode_ldapResult(int result, char *printerName);
static int
authorized()
{
struct passwd *pw;
uid_t uid;
gid_t *list;
int len;
int maxgrp;
if ((uid = getuid()) == 0)
return (1); /* "root" is authorized */
if (((pw = getpwnam("lp")) != NULL) && (uid == pw->pw_uid))
return (1); /* "lp" is authorized */
if ((pw = getpwuid(uid)) == NULL)
return (0); /* intruders are not authorized */
if (chkauthattr("solaris.print.admin", pw->pw_name) == 1)
return (1); /* "solaris.print.admin" is authorized */
/* How many supplemental groups do we have? */
maxgrp = getgroups(0, NULL);
list = alloca(maxgrp * sizeof (gid_t));
if ((len = getgroups(maxgrp, list)) != -1)
while (len-- > 0)
if (list[len] == 14)
return (1); /* group 14 is authorized */
return (0); /* nobody else is authorized */
}
static void
Usage(char *name)
{
(void) fprintf(stderr,
gettext("Usage: %s [-n files | ldap] [-x] "
"[-h ldaphost] [-D binddn] [-w passwd] "
"[-a key=value] [-d key] (printer)\n"),
name);
exit(1);
}
/*
* main() calls the appropriate routine to parse the command line arguments
* and then calls the local remove routine, followed by the remote remove
* routine to remove jobs.
*/
int
main(int ac, char *av[])
{
int result = 0;
int delete_printer = 0;
int c;
char *program = NULL,
*printer = NULL,
*host = NULL,
*binddn = NULL,
*passwd = NULL,
*ins = NULL,
*ons = "files";
char **changes = NULL;
ns_cred_t *cred = NULL;
ns_printer_t *printer_obj = NULL;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
if ((program = strrchr(av[0], '/')) == NULL)
program = av[0];
else
program++;
openlog(program, LOG_PID, LOG_LPR);
if (ac < 2)
Usage(program);
while ((c = getopt(ac, av, "a:d:D:h:n:r:w:x")) != EOF)
switch (c) {
case 'd':
if (strchr(optarg, '=') != NULL)
Usage(program);
/* FALLTHRU */
case 'a':
changes = (char **)list_append((void**)changes,
(void *)strdup(optarg));
break;
case 'D':
binddn = optarg;
break;
case 'h':
host = optarg;
break;
case 'n':
ons = optarg;
break;
case 'r':
ins = optarg;
break;
case 'w':
passwd = optarg;
break;
case 'x':
delete_printer++;
break;
default:
Usage(program);
}
if (optind != ac-1)
Usage(program);
/*
* Check required options have been given: [ -x | [ -a | -d ]]
*/
if ((changes == NULL) && (delete_printer == 0)) {
Usage(program);
}
printer = av[optind];
if (strchr(printer, ':') != NULL) {
(void) fprintf(stderr, gettext(
"POSIX-Style names are not valid destinations (%s)\n"),
printer);
return (1);
}
ins = normalize_ns_name(ins);
ons = normalize_ns_name(ons);
if (ins == NULL)
ins = ons;
/* check / set the name service for writing */
if (strcasecmp("user", ons) == 0) {
(void) setuid(getuid());
ons = "user";
} else if (strcasecmp("files", ons) == 0) {
if (authorized() == 0) {
(void) fprintf(stderr, gettext(
"Permission denied: not authorized\n"));
return (1);
}
ons = "files";
} else if (strcasecmp("ldap", ons) == 0) {
if ((cred = calloc(1, sizeof (*cred))) == NULL) {
(void) fprintf(stderr,
gettext("could not initialize credential\n"));
return (1);
}
if (binddn == NULL) {
(void) fprintf(stderr,
gettext("Distinguished Name is required.\n"));
return (1);
}
if (passwd == NULL) {
passwd = getpassphrase(gettext("Bind Password:"));
}
/*
* Setup LDAP bind credentials, so that it uses
* the default ldap port, and the NS domain for this
* ldapclient box. Note: passwdType is currently not
* used but once the ldap native function can select
* secure or insure password it will pass the user selected
* security type.
*/
cred->passwd = passwd;
cred->passwdType = NS_PW_INSECURE; /* use default */
cred->binddn = binddn;
cred->host = host;
cred->port = 0; /* use default */
cred->domainDN = NULL; /* use default */
ons = "ldap";
(void) setuid(getuid());
} else {
(void) fprintf(stderr,
gettext("%s is not a supported name service.\n"),
ons);
return (1);
}
if (strcasecmp(NS_SVC_LDAP, ons) != 0) {
/* Naming Service is not LDAP */
/* get the printer object */
if ((printer_obj = ns_printer_get_name(printer, ins)) == NULL) {
if (delete_printer != 0) {
(void) fprintf(stderr, gettext
("%s: unknown printer\n"), printer);
return (1);
}
if ((printer_obj = calloc(1, sizeof (*printer_obj)))
== NULL) {
(void) fprintf(stderr, gettext(
"could not initialize printer object\n"));
return (1);
}
printer_obj->name = strdup(printer);
}
printer_obj->source = ons;
if (cred != NULL) {
printer_obj->cred = cred;
}
/* make the changes to it */
while (changes != NULL && *changes != NULL) {
int has_equals = (strchr(*changes, '=') != NULL);
char *p, *key = NULL, *value = NULL;
key = *(changes++);
for (p = key; ((p != NULL) && (*p != '\0')); p++)
if (*p == '=') {
*p = '\0';
value = ++p;
break;
} else if (*p == '\\')
p++;
if ((value != NULL) && (*value == '\0'))
value = NULL;
if ((key != NULL) && (key[0] != '\0')) {
if ((value == NULL) &&
(ns_get_value(key, printer_obj) == NULL) &&
(has_equals == 0)) {
fprintf(stderr,
gettext("%s: unknown attribute\n"),
key);
result = 1;
} else
(void) ns_set_value_from_string(key, value,
printer_obj);
}
}
if (delete_printer != 0)
printer_obj->attributes = NULL;
/* write it back */
if (ns_printer_put(printer_obj) != 0) {
(void) fprintf(stderr,
gettext("Failed to write into %s database\n"),
ons);
result = 1;
}
}
else {
/*
* Naming Service is LDAP
*
* Action the request by calling ns ldap functions to
* add, modify or delete the printer object.
*/
if ((printer_obj = calloc(1, sizeof (*printer_obj))) == NULL) {
(void) fprintf(stderr, gettext(
"could not initialize printer object\n"));
return (1);
}
if ((cred != NULL) && (printer_obj != NULL)) {
printer_obj->name = strdup(printer);
printer_obj->cred = cred;
printer_obj->cred->domainDN = NULL; /* use default */
printer_obj->source = ons;
printer_obj->nsdata = malloc(sizeof (NS_LDAPDATA));
if (printer_obj->nsdata != NULL) {
/*
* Update the LDAP directory for this printer
*/
if (delete_printer != 0) {
/* Delete the printer object */
((NS_LDAPDATA *)
(printer_obj->nsdata))->attrList
= NULL;
} else {
/* Add or modify the printer object */
((NS_LDAPDATA *)
(printer_obj->nsdata))->attrList =
changes;
}
result = ns_printer_put(printer_obj);
if (result != 0) {
/* display LDAP specific message */
_decode_ldapResult(result, printer);
(void) fprintf(stderr, gettext(
"Failed to update %s database\n"), ons);
result = 1;
}
free(printer_obj->nsdata);
}
else {
_decode_ldapResult(NSL_ERR_MEMORY, NULL);
result = 1;
}
}
else {
result = 1;
(void) fprintf(stderr,
gettext("Error - no LDAP credentials\n"));
}
if (printer_obj != NULL) {
if (printer_obj->name != NULL) {
free(printer_obj->name);
}
free(printer_obj);
}
}
return (result);
} /* main */
/*
* *****************************************************************************
*
* Function: _decode_ldapResult()
*
* Description: Decode the ldap_put_printer specific error codes and display
* the appropriate error message.
*
* Parameters:
* Input: int result - contains the NSL_RESULT codes
* char *printerName - name of printer
* Output: None
*
* Returns: void
*
* *****************************************************************************
*/
static void
_decode_ldapResult(int result, char *printerName)
{
NSL_RESULT lresult = (NSL_RESULT)result;
/* ------------- */
switch (lresult)
{
case NSL_OK:
{
break;
}
case NSL_ERR_INTERNAL:
{
(void) fprintf(stderr,
gettext("Unexpected software error\n"));
break;
}
case NSL_ERR_ADD_FAILED:
{
(void) fprintf(stderr, "%s %s\n",
gettext("Failed to add printer:"), printerName);
break;
}
case NSL_ERR_MOD_FAILED:
{
(void) fprintf(stderr, "%s %s\n",
gettext("Failed to modify printer:"),
printerName);
break;
}
case NSL_ERR_DEL_FAILED:
{
(void) fprintf(stderr, "%s %s\n",
gettext("Failed to delete printer:"),
printerName);
break;
}
case NSL_ERR_UNKNOWN_PRINTER:
{
(void) fprintf(stderr, "%s %s\n",
gettext("Unknown printer:"), printerName);
break;
}
case NSL_ERR_CREDENTIALS:
{
(void) fprintf(stderr, "%s\n",
gettext("Missing LDAP credential information for printer:"));
break;
}
case NSL_ERR_CONNECT:
{
(void) fprintf(stderr, "%s\n",
gettext("Failed to connect to LDAP server"));
break;
}
case NSL_ERR_BIND:
{
(void) fprintf(stderr, gettext("LDAP bind failed\n"));
break;
}
case NSL_ERR_RENAME:
{
(void) fprintf(stderr, "%s %s\n",
gettext("Object rename not allowed for printer:"),
printerName);
break;
}
case NSL_ERR_KVP:
{
(void) fprintf(stderr, "%s",
gettext("Setting sun-printer-kvp attribute is "
"not supported through this command.\n"));
break;
}
case NSL_ERR_BSDADDR:
{
(void) fprintf(stderr, "%s",
gettext("Setting sun-printer-bsdaddr attribute is "
"not supported through this command.\n"
"Use the bsaddr attribute instead.\n"));
break;
}
case NSL_ERR_PNAME:
{
(void) fprintf(stderr, "%s",
gettext("Setting printer-name attribute is "
"not supported through this command.\n"));
break;
}
case NSL_ERR_MEMORY:
{
(void) fprintf(stderr,
gettext("Memory allocation error\n"));
break;
}
case NSL_ERR_MULTIOP:
{
(void) fprintf(stderr,
gettext("Delete and add operation on the "
"same key attribute is not allowed\n"));
break;
}
case NSL_ERR_NOTALLOWED:
{
(void) fprintf(stderr,
gettext("KVP attribute is not allowed\n"));
break;
}
default:
{
(void) fprintf(stderr,
gettext("Error code = %d\n"), result);
break;
}
}
} /* _decode_ldapResult */
|