]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
b423cd4c | 2 | * "$Id: dirsvc.c 5178 2006-02-26 00:24:23Z mike $" |
ef416fc2 | 3 | * |
4 | * Directory services routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
bd7854cb | 6 | * Copyright 1997-2006 by Easy Software Products, all rights reserved. |
ef416fc2 | 7 | * |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cupsdLoadRemoteCache() - Load the remote printer cache. | |
ef416fc2 | 27 | * cupsdSaveRemoteCache() - Save the remote printer cache. |
28 | * cupsdSendBrowseDelete() - Send a "browse delete" message for a | |
29 | * printer. | |
30 | * cupsdSendBrowseList() - Send new browsing information as necessary. | |
31 | * cupsdSendCUPSBrowse() - Send new browsing information using the | |
32 | * CUPS protocol. | |
33 | * cupsdSendSLPBrowse() - Register the specified printer with SLP. | |
34 | * cupsdStartBrowsing() - Start sending and receiving broadcast | |
35 | * information. | |
36 | * cupsdStartPolling() - Start polling servers as needed. | |
37 | * cupsdStopBrowsing() - Stop sending and receiving broadcast | |
38 | * information. | |
39 | * cupsdStopPolling() - Stop polling servers as needed. | |
40 | * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS | |
41 | * protocol. | |
42 | * cupsdUpdatePolling() - Read status messages from the poll daemons. | |
43 | * cupsdUpdateSLPBrowse() - Get browsing information via SLP. | |
e00b005a | 44 | * dequote() - Remote quotes from a string. |
45 | * process_browse_data() - Process new browse data. | |
46 | * process_implicit_classes() - Create/update implicit classes as needed. | |
ef416fc2 | 47 | * slp_attr_callback() - SLP attribute callback |
48 | * slp_dereg_printer() - SLPDereg() the specified printer | |
49 | * slp_get_attr() - Get an attribute from an SLP registration. | |
50 | * slp_reg_callback() - Empty SLPRegReport. | |
51 | * slp_url_callback() - SLP service url callback | |
52 | */ | |
53 | ||
54 | /* | |
55 | * Include necessary headers... | |
56 | */ | |
57 | ||
58 | #include "cupsd.h" | |
59 | #include <grp.h> | |
60 | ||
61 | ||
62 | /* | |
e00b005a | 63 | * Local functions... |
ef416fc2 | 64 | */ |
65 | ||
e00b005a | 66 | static char *dequote(char *d, const char *s, int dlen); |
b423cd4c | 67 | static int is_local_queue(const char *uri, char *host, int hostlen, |
68 | char *resource, int resourcelen); | |
69 | static void process_browse_data(const char *uri, const char *host, | |
70 | const char *resource, cups_ptype_t type, | |
e00b005a | 71 | ipp_pstate_t state, const char *location, |
72 | const char *info, const char *make_model, | |
73 | int num_attrs, cups_option_t *attrs); | |
74 | static void process_implicit_classes(void); | |
75 | ||
76 | ||
b423cd4c | 77 | #ifdef HAVE_OPENLDAP |
78 | static const char * const ldap_attrs[] =/* CUPS LDAP attributes */ | |
79 | { | |
80 | "printerDescription", | |
81 | "printerLocation", | |
82 | "printerMakeAndModel", | |
83 | "printerType", | |
84 | "printerURI", | |
85 | NULL | |
86 | }; | |
87 | #endif /* HAVE_OPENLDAP */ | |
88 | ||
ef416fc2 | 89 | #ifdef HAVE_LIBSLP |
e00b005a | 90 | /* |
91 | * SLP definitions... | |
92 | */ | |
93 | ||
ef416fc2 | 94 | /* |
95 | * SLP service name for CUPS... | |
96 | */ | |
97 | ||
98 | # define SLP_CUPS_SRVTYPE "service:printer" | |
99 | # define SLP_CUPS_SRVLEN 15 | |
100 | ||
101 | ||
102 | /* | |
103 | * Printer service URL structure | |
104 | */ | |
105 | ||
106 | typedef struct _slpsrvurl_s /**** SLP URL list ****/ | |
107 | { | |
108 | struct _slpsrvurl_s *next; /* Next URL in list */ | |
109 | char url[HTTP_MAX_URI]; | |
110 | /* URL */ | |
111 | } slpsrvurl_t; | |
112 | ||
113 | ||
114 | /* | |
115 | * Local functions... | |
116 | */ | |
117 | ||
118 | static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist, | |
119 | SLPError errcode, void *cookie); | |
120 | static void slp_dereg_printer(cupsd_printer_t *p); | |
121 | static int slp_get_attr(const char *attrlist, const char *tag, | |
122 | char **valbuf); | |
123 | static void slp_reg_callback(SLPHandle hslp, SLPError errcode, | |
124 | void *cookie); | |
125 | static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl, | |
126 | unsigned short lifetime, | |
127 | SLPError errcode, void *cookie); | |
128 | #endif /* HAVE_LIBSLP */ | |
129 | ||
130 | ||
131 | /* | |
132 | * 'cupsdLoadRemoteCache()' - Load the remote printer cache. | |
133 | */ | |
134 | ||
135 | void | |
136 | cupsdLoadRemoteCache(void) | |
137 | { | |
138 | cups_file_t *fp; /* remote.cache file */ | |
139 | int linenum; /* Current line number */ | |
140 | char line[1024], /* Line from file */ | |
141 | *value, /* Pointer to value */ | |
bd7854cb | 142 | *valueptr, /* Pointer into value */ |
143 | scheme[32], /* Scheme portion of URI */ | |
144 | username[64], /* Username portion of URI */ | |
145 | host[HTTP_MAX_HOST], | |
146 | /* Hostname portion of URI */ | |
147 | resource[HTTP_MAX_URI]; | |
148 | /* Resource portion of URI */ | |
149 | int port; /* Port number */ | |
ef416fc2 | 150 | cupsd_printer_t *p; /* Current printer */ |
151 | time_t now; /* Current time */ | |
152 | ||
153 | ||
154 | /* | |
155 | * Open the remote.cache file... | |
156 | */ | |
157 | ||
158 | snprintf(line, sizeof(line), "%s/remote.cache", CacheDir); | |
159 | if ((fp = cupsFileOpen(line, "r")) == NULL) | |
160 | return; | |
161 | ||
162 | /* | |
163 | * Read printer configurations until we hit EOF... | |
164 | */ | |
165 | ||
166 | linenum = 0; | |
167 | p = NULL; | |
168 | now = time(NULL); | |
169 | ||
170 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
171 | { | |
172 | /* | |
173 | * Decode the directive... | |
174 | */ | |
175 | ||
176 | if (!strcasecmp(line, "<Printer") || | |
177 | !strcasecmp(line, "<DefaultPrinter")) | |
178 | { | |
179 | /* | |
180 | * <Printer name> or <DefaultPrinter name> | |
181 | */ | |
182 | ||
183 | if (p == NULL && value) | |
184 | { | |
185 | /* | |
186 | * Add the printer and a base file type... | |
187 | */ | |
188 | ||
189 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
190 | "cupsdLoadRemoteCache: Loading printer %s...", value); | |
191 | ||
bd7854cb | 192 | if ((p = cupsdFindDest(value)) != NULL) |
193 | { | |
194 | if (p->type & CUPS_PRINTER_CLASS) | |
195 | { | |
196 | cupsdLogMessage(CUPSD_LOG_WARN, | |
197 | "Cached remote printer \"%s\" conflicts with " | |
198 | "existing class!", | |
199 | value); | |
200 | p = NULL; | |
201 | continue; | |
202 | } | |
203 | } | |
204 | else | |
205 | p = cupsdAddPrinter(value); | |
206 | ||
ef416fc2 | 207 | p->accepting = 1; |
208 | p->state = IPP_PRINTER_IDLE; | |
209 | p->type |= CUPS_PRINTER_REMOTE; | |
210 | p->browse_time = now + BrowseTimeout; | |
211 | ||
212 | /* | |
213 | * Set the default printer as needed... | |
214 | */ | |
215 | ||
216 | if (!strcasecmp(line, "<DefaultPrinter")) | |
217 | DefaultPrinter = p; | |
218 | } | |
219 | else | |
220 | { | |
221 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
222 | "Syntax error on line %d of remote.cache.", linenum); | |
223 | return; | |
224 | } | |
225 | } | |
226 | else if (!strcasecmp(line, "<Class") || | |
227 | !strcasecmp(line, "<DefaultClass")) | |
228 | { | |
229 | /* | |
230 | * <Class name> or <DefaultClass name> | |
231 | */ | |
232 | ||
233 | if (p == NULL && value) | |
234 | { | |
235 | /* | |
236 | * Add the printer and a base file type... | |
237 | */ | |
238 | ||
239 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
240 | "cupsdLoadRemoteCache: Loading class %s...", value); | |
241 | ||
bd7854cb | 242 | if ((p = cupsdFindDest(value)) != NULL) |
243 | p->type = CUPS_PRINTER_CLASS; | |
244 | else | |
245 | p = cupsdAddClass(value); | |
246 | ||
ef416fc2 | 247 | p->accepting = 1; |
248 | p->state = IPP_PRINTER_IDLE; | |
249 | p->type |= CUPS_PRINTER_REMOTE; | |
250 | p->browse_time = now + BrowseTimeout; | |
251 | ||
252 | /* | |
253 | * Set the default printer as needed... | |
254 | */ | |
255 | ||
256 | if (!strcasecmp(line, "<DefaultClass")) | |
257 | DefaultPrinter = p; | |
258 | } | |
259 | else | |
260 | { | |
261 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
262 | "Syntax error on line %d of remote.cache.", linenum); | |
263 | return; | |
264 | } | |
265 | } | |
266 | else if (!strcasecmp(line, "</Printer>") || | |
267 | !strcasecmp(line, "</Class>")) | |
268 | { | |
269 | if (p != NULL) | |
270 | { | |
271 | /* | |
272 | * Close out the current printer... | |
273 | */ | |
274 | ||
275 | cupsdSetPrinterAttrs(p); | |
276 | ||
277 | p = NULL; | |
278 | } | |
279 | else | |
280 | { | |
281 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
282 | "Syntax error on line %d of remote.cache.", linenum); | |
283 | return; | |
284 | } | |
285 | } | |
286 | else if (!p) | |
287 | { | |
288 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
289 | "Syntax error on line %d of remote.cache.", linenum); | |
290 | return; | |
291 | } | |
292 | else if (!strcasecmp(line, "Info")) | |
293 | { | |
294 | if (value) | |
295 | cupsdSetString(&p->info, value); | |
296 | } | |
297 | else if (!strcasecmp(line, "MakeModel")) | |
298 | { | |
299 | if (value) | |
300 | cupsdSetString(&p->make_model, value); | |
301 | } | |
302 | else if (!strcasecmp(line, "Location")) | |
303 | { | |
304 | if (value) | |
305 | cupsdSetString(&p->location, value); | |
306 | } | |
307 | else if (!strcasecmp(line, "DeviceURI")) | |
308 | { | |
309 | if (value) | |
4400e98d | 310 | { |
bd7854cb | 311 | httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), |
312 | username, sizeof(username), host, sizeof(host), &port, | |
313 | resource, sizeof(resource)); | |
314 | ||
315 | cupsdSetString(&p->hostname, host); | |
4400e98d | 316 | cupsdSetString(&p->uri, value); |
ef416fc2 | 317 | cupsdSetString(&p->device_uri, value); |
4400e98d | 318 | } |
ef416fc2 | 319 | else |
320 | { | |
321 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
322 | "Syntax error on line %d of remote.cache.", linenum); | |
323 | return; | |
324 | } | |
325 | } | |
b423cd4c | 326 | else if (!strcasecmp(line, "Option") && value) |
327 | { | |
328 | /* | |
329 | * Option name value | |
330 | */ | |
331 | ||
332 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
333 | ||
334 | if (!*valueptr) | |
335 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
336 | "Syntax error on line %d of remote.cache.", linenum); | |
337 | else | |
338 | { | |
339 | for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0'); | |
340 | ||
341 | p->num_options = cupsAddOption(value, valueptr, p->num_options, | |
342 | &(p->options)); | |
343 | } | |
344 | } | |
ef416fc2 | 345 | else if (!strcasecmp(line, "State")) |
346 | { | |
347 | /* | |
348 | * Set the initial queue state... | |
349 | */ | |
350 | ||
351 | if (value && !strcasecmp(value, "idle")) | |
352 | p->state = IPP_PRINTER_IDLE; | |
353 | else if (value && !strcasecmp(value, "stopped")) | |
354 | p->state = IPP_PRINTER_STOPPED; | |
355 | else | |
356 | { | |
357 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
358 | "Syntax error on line %d of remote.cache.", linenum); | |
359 | return; | |
360 | } | |
361 | } | |
362 | else if (!strcasecmp(line, "StateMessage")) | |
363 | { | |
364 | /* | |
365 | * Set the initial queue state message... | |
366 | */ | |
367 | ||
368 | if (value) | |
369 | strlcpy(p->state_message, value, sizeof(p->state_message)); | |
370 | } | |
371 | else if (!strcasecmp(line, "Accepting")) | |
372 | { | |
373 | /* | |
374 | * Set the initial accepting state... | |
375 | */ | |
376 | ||
377 | if (value && | |
378 | (!strcasecmp(value, "yes") || | |
379 | !strcasecmp(value, "on") || | |
380 | !strcasecmp(value, "true"))) | |
381 | p->accepting = 1; | |
382 | else if (value && | |
383 | (!strcasecmp(value, "no") || | |
384 | !strcasecmp(value, "off") || | |
385 | !strcasecmp(value, "false"))) | |
386 | p->accepting = 0; | |
387 | else | |
388 | { | |
389 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
390 | "Syntax error on line %d of remote.cache.", linenum); | |
391 | return; | |
392 | } | |
393 | } | |
394 | else if (!strcasecmp(line, "Type")) | |
395 | { | |
396 | if (value) | |
397 | p->type = atoi(value); | |
398 | else | |
399 | { | |
400 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
401 | "Syntax error on line %d of remote.cache.", linenum); | |
402 | return; | |
403 | } | |
404 | } | |
405 | else if (!strcasecmp(line, "BrowseTime")) | |
406 | { | |
407 | if (value) | |
408 | { | |
409 | time_t t = atoi(value); | |
410 | ||
411 | if (t > (now + BrowseInterval)) | |
412 | p->browse_time = t; | |
413 | } | |
414 | else | |
415 | { | |
416 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
417 | "Syntax error on line %d of remote.cache.", linenum); | |
418 | return; | |
419 | } | |
420 | } | |
421 | else if (!strcasecmp(line, "JobSheets")) | |
422 | { | |
423 | /* | |
424 | * Set the initial job sheets... | |
425 | */ | |
426 | ||
427 | if (value) | |
428 | { | |
429 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
430 | ||
431 | if (*valueptr) | |
432 | *valueptr++ = '\0'; | |
433 | ||
434 | cupsdSetString(&p->job_sheets[0], value); | |
435 | ||
436 | while (isspace(*valueptr & 255)) | |
437 | valueptr ++; | |
438 | ||
439 | if (*valueptr) | |
440 | { | |
441 | for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
442 | ||
443 | if (*valueptr) | |
444 | *valueptr++ = '\0'; | |
445 | ||
446 | cupsdSetString(&p->job_sheets[1], value); | |
447 | } | |
448 | } | |
449 | else | |
450 | { | |
451 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
452 | "Syntax error on line %d of remote.cache.", linenum); | |
453 | return; | |
454 | } | |
455 | } | |
456 | else if (!strcasecmp(line, "AllowUser")) | |
457 | { | |
458 | if (value) | |
459 | { | |
460 | p->deny_users = 0; | |
461 | cupsdAddPrinterUser(p, value); | |
462 | } | |
463 | else | |
464 | { | |
465 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
466 | "Syntax error on line %d of remote.cache.", linenum); | |
467 | return; | |
468 | } | |
469 | } | |
470 | else if (!strcasecmp(line, "DenyUser")) | |
471 | { | |
472 | if (value) | |
473 | { | |
474 | p->deny_users = 1; | |
475 | cupsdAddPrinterUser(p, value); | |
476 | } | |
477 | else | |
478 | { | |
479 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
480 | "Syntax error on line %d of remote.cache.", linenum); | |
481 | return; | |
482 | } | |
483 | } | |
484 | else | |
485 | { | |
486 | /* | |
487 | * Something else we don't understand... | |
488 | */ | |
489 | ||
490 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
491 | "Unknown configuration directive %s on line %d of remote.cache.", | |
492 | line, linenum); | |
493 | } | |
494 | } | |
495 | ||
496 | cupsFileClose(fp); | |
497 | ||
498 | /* | |
499 | * Do auto-classing if needed... | |
500 | */ | |
501 | ||
e00b005a | 502 | process_implicit_classes(); |
ef416fc2 | 503 | } |
504 | ||
505 | ||
506 | /* | |
e00b005a | 507 | * 'cupsdSaveRemoteCache()' - Save the remote printer cache. |
ef416fc2 | 508 | */ |
509 | ||
510 | void | |
e00b005a | 511 | cupsdSaveRemoteCache(void) |
ef416fc2 | 512 | { |
e00b005a | 513 | int i; /* Looping var */ |
514 | cups_file_t *fp; /* printers.conf file */ | |
515 | char temp[1024]; /* Temporary string */ | |
516 | cupsd_printer_t *printer; /* Current printer class */ | |
517 | time_t curtime; /* Current time */ | |
518 | struct tm *curdate; /* Current date */ | |
b423cd4c | 519 | cups_option_t *option; /* Current option */ |
ef416fc2 | 520 | |
ef416fc2 | 521 | |
522 | /* | |
e00b005a | 523 | * Create the remote.cache file... |
ef416fc2 | 524 | */ |
525 | ||
e00b005a | 526 | snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir); |
ef416fc2 | 527 | |
e00b005a | 528 | if ((fp = cupsFileOpen(temp, "w")) == NULL) |
ef416fc2 | 529 | { |
530 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
e00b005a | 531 | "Unable to save remote.cache - %s", strerror(errno)); |
ef416fc2 | 532 | return; |
533 | } | |
e00b005a | 534 | else |
535 | cupsdLogMessage(CUPSD_LOG_INFO, "Saving remote.cache..."); | |
ef416fc2 | 536 | |
537 | /* | |
e00b005a | 538 | * Restrict access to the file... |
ef416fc2 | 539 | */ |
540 | ||
e00b005a | 541 | fchown(cupsFileNumber(fp), getuid(), Group); |
542 | fchmod(cupsFileNumber(fp), ConfigFilePerm); | |
ef416fc2 | 543 | |
e00b005a | 544 | /* |
545 | * Write a small header to the file... | |
546 | */ | |
ef416fc2 | 547 | |
e00b005a | 548 | curtime = time(NULL); |
549 | curdate = localtime(&curtime); | |
550 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
ef416fc2 | 551 | |
e00b005a | 552 | cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n"); |
553 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
ef416fc2 | 554 | |
555 | /* | |
e00b005a | 556 | * Write each local printer known to the system... |
ef416fc2 | 557 | */ |
558 | ||
e00b005a | 559 | for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
560 | printer; | |
561 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 562 | { |
563 | /* | |
e00b005a | 564 | * Skip local destinations... |
ef416fc2 | 565 | */ |
566 | ||
e00b005a | 567 | if (!(printer->type & CUPS_PRINTER_REMOTE)) |
568 | continue; | |
ef416fc2 | 569 | |
ef416fc2 | 570 | /* |
e00b005a | 571 | * Write printers as needed... |
ef416fc2 | 572 | */ |
573 | ||
e00b005a | 574 | if (printer == DefaultPrinter) |
575 | cupsFilePuts(fp, "<Default"); | |
ef416fc2 | 576 | else |
e00b005a | 577 | cupsFilePutChar(fp, '<'); |
ef416fc2 | 578 | |
e00b005a | 579 | if (printer->type & CUPS_PRINTER_CLASS) |
580 | cupsFilePrintf(fp, "Class %s>\n", printer->name); | |
581 | else | |
582 | cupsFilePrintf(fp, "Printer %s>\n", printer->name); | |
ef416fc2 | 583 | |
e00b005a | 584 | cupsFilePrintf(fp, "Type %d\n", printer->type); |
ef416fc2 | 585 | |
e00b005a | 586 | cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_time); |
ef416fc2 | 587 | |
e00b005a | 588 | if (printer->info) |
589 | cupsFilePrintf(fp, "Info %s\n", printer->info); | |
ef416fc2 | 590 | |
e00b005a | 591 | if (printer->make_model) |
592 | cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model); | |
ef416fc2 | 593 | |
e00b005a | 594 | if (printer->location) |
595 | cupsFilePrintf(fp, "Location %s\n", printer->location); | |
ef416fc2 | 596 | |
e00b005a | 597 | if (printer->device_uri) |
598 | cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri); | |
ef416fc2 | 599 | |
e00b005a | 600 | if (printer->state == IPP_PRINTER_STOPPED) |
601 | { | |
602 | cupsFilePuts(fp, "State Stopped\n"); | |
603 | cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message); | |
ef416fc2 | 604 | } |
e00b005a | 605 | else |
606 | cupsFilePuts(fp, "State Idle\n"); | |
ef416fc2 | 607 | |
e00b005a | 608 | if (printer->accepting) |
609 | cupsFilePuts(fp, "Accepting Yes\n"); | |
610 | else | |
611 | cupsFilePuts(fp, "Accepting No\n"); | |
ef416fc2 | 612 | |
e00b005a | 613 | cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0], |
614 | printer->job_sheets[1]); | |
ef416fc2 | 615 | |
e00b005a | 616 | for (i = 0; i < printer->num_users; i ++) |
617 | cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow", | |
618 | printer->users[i]); | |
ef416fc2 | 619 | |
b423cd4c | 620 | for (i = printer->num_options, option = printer->options; |
621 | i > 0; | |
622 | i --, option ++) | |
623 | cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value); | |
624 | ||
e00b005a | 625 | if (printer->type & CUPS_PRINTER_CLASS) |
626 | cupsFilePuts(fp, "</Class>\n"); | |
627 | else | |
628 | cupsFilePuts(fp, "</Printer>\n"); | |
629 | } | |
ef416fc2 | 630 | |
e00b005a | 631 | cupsFileClose(fp); |
632 | } | |
ef416fc2 | 633 | |
ef416fc2 | 634 | |
e00b005a | 635 | /* |
636 | * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer. | |
637 | */ | |
ef416fc2 | 638 | |
e00b005a | 639 | void |
640 | cupsdSendBrowseDelete( | |
641 | cupsd_printer_t *p) /* I - Printer to delete */ | |
642 | { | |
643 | /* | |
644 | * Only announce if browsing is enabled... | |
645 | */ | |
ef416fc2 | 646 | |
e00b005a | 647 | if (!Browsing || !p->shared) |
648 | return; | |
ef416fc2 | 649 | |
e00b005a | 650 | /* |
651 | * First mark the printer for deletion... | |
652 | */ | |
ef416fc2 | 653 | |
e00b005a | 654 | p->type |= CUPS_PRINTER_DELETE; |
ef416fc2 | 655 | |
e00b005a | 656 | /* |
657 | * Announce the deletion... | |
658 | */ | |
ef416fc2 | 659 | |
e00b005a | 660 | if (BrowseLocalProtocols & BROWSE_CUPS) |
661 | cupsdSendCUPSBrowse(p); | |
662 | #ifdef HAVE_LIBSLP | |
663 | if (BrowseLocalProtocols & BROWSE_SLP) | |
664 | slp_dereg_printer(p); | |
665 | #endif /* HAVE_LIBSLP */ | |
666 | } | |
ef416fc2 | 667 | |
ef416fc2 | 668 | |
e00b005a | 669 | /* |
670 | * 'cupsdSendBrowseList()' - Send new browsing information as necessary. | |
671 | */ | |
ef416fc2 | 672 | |
e00b005a | 673 | void |
674 | cupsdSendBrowseList(void) | |
675 | { | |
676 | int count; /* Number of dests to update */ | |
677 | cupsd_printer_t *p; /* Current printer */ | |
678 | time_t ut, /* Minimum update time */ | |
679 | to; /* Timeout time */ | |
ef416fc2 | 680 | |
e00b005a | 681 | |
682 | if (!Browsing || !BrowseLocalProtocols || !Printers) | |
683 | return; | |
684 | ||
685 | /* | |
686 | * Compute the update and timeout times... | |
687 | */ | |
688 | ||
b423cd4c | 689 | to = time(NULL); |
690 | ut = to - BrowseInterval; | |
e00b005a | 691 | |
692 | /* | |
693 | * Figure out how many printers need an update... | |
694 | */ | |
695 | ||
696 | if (BrowseInterval > 0) | |
697 | { | |
698 | int max_count; /* Maximum number to update */ | |
699 | ||
700 | ||
701 | /* | |
702 | * Throttle the number of printers we'll be updating this time | |
703 | * around based on the number of queues that need updating and | |
704 | * the maximum number of queues to update each second... | |
705 | */ | |
706 | ||
707 | max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1; | |
708 | ||
709 | for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
710 | count < max_count && p != NULL; | |
711 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
712 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && | |
713 | p->shared && p->browse_time < ut) | |
714 | count ++; | |
715 | ||
716 | /* | |
717 | * Loop through all of the printers and send local updates as needed... | |
718 | */ | |
719 | ||
720 | if (BrowseNext) | |
721 | p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext); | |
722 | else | |
723 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
724 | ||
725 | for (; | |
726 | count > 0; | |
727 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 728 | { |
729 | /* | |
e00b005a | 730 | * Check for wraparound... |
ef416fc2 | 731 | */ |
732 | ||
e00b005a | 733 | if (!p) |
734 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 735 | |
e00b005a | 736 | if (!p) |
737 | break; | |
738 | else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || | |
739 | !p->shared) | |
740 | continue; | |
741 | else if (p->browse_time < ut) | |
742 | { | |
743 | /* | |
744 | * Need to send an update... | |
745 | */ | |
ef416fc2 | 746 | |
e00b005a | 747 | count --; |
ef416fc2 | 748 | |
e00b005a | 749 | p->browse_time = time(NULL); |
ef416fc2 | 750 | |
e00b005a | 751 | if (BrowseLocalProtocols & BROWSE_CUPS) |
752 | cupsdSendCUPSBrowse(p); | |
ef416fc2 | 753 | |
e00b005a | 754 | #ifdef HAVE_LIBSLP |
755 | if (BrowseLocalProtocols & BROWSE_SLP) | |
756 | cupsdSendSLPBrowse(p); | |
757 | #endif /* HAVE_LIBSLP */ | |
b423cd4c | 758 | |
759 | #ifdef HAVE_LDAP | |
760 | if (BrowseLocalProtocols & BROWSE_LDAP) | |
761 | cupsdSendLDAPBrowse(p); | |
762 | #endif /* HAVE_LDAP */ | |
e00b005a | 763 | } |
ef416fc2 | 764 | } |
e00b005a | 765 | |
766 | /* | |
767 | * Save where we left off so that all printers get updated... | |
768 | */ | |
769 | ||
770 | BrowseNext = p; | |
ef416fc2 | 771 | } |
772 | ||
773 | /* | |
e00b005a | 774 | * Loop through all of the printers and send local updates as needed... |
ef416fc2 | 775 | */ |
776 | ||
e00b005a | 777 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); |
778 | p; | |
779 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 780 | { |
e00b005a | 781 | /* |
782 | * If this is a remote queue, see if it needs to be timed out... | |
783 | */ | |
ef416fc2 | 784 | |
e00b005a | 785 | if (p->type & CUPS_PRINTER_REMOTE) |
ef416fc2 | 786 | { |
b423cd4c | 787 | if (p->browse_expire < to) |
e00b005a | 788 | { |
789 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
790 | "%s \'%s\' deleted by directory services (timeout).", | |
791 | (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
792 | p->name); | |
793 | ||
794 | cupsdLogMessage(CUPSD_LOG_INFO, | |
b423cd4c | 795 | "Remote destination \"%s\" has timed out; " |
796 | "deleting it...", | |
e00b005a | 797 | p->name); |
798 | ||
799 | cupsArraySave(Printers); | |
800 | cupsdDeletePrinter(p, 1); | |
801 | cupsArrayRestore(Printers); | |
802 | } | |
ef416fc2 | 803 | } |
804 | } | |
e00b005a | 805 | } |
ef416fc2 | 806 | |
ef416fc2 | 807 | |
e00b005a | 808 | /* |
b423cd4c | 809 | * 'cupsdSendCUPSBrowse()' - Send new browsing information using the CUPS |
810 | * protocol. | |
e00b005a | 811 | */ |
ef416fc2 | 812 | |
e00b005a | 813 | void |
814 | cupsdSendCUPSBrowse(cupsd_printer_t *p) /* I - Printer to send */ | |
815 | { | |
816 | int i; /* Looping var */ | |
817 | cups_ptype_t type; /* Printer type */ | |
818 | cupsd_dirsvc_addr_t *b; /* Browse address */ | |
819 | int bytes; /* Length of packet */ | |
820 | char packet[1453], /* Browse data packet */ | |
821 | uri[1024], /* Printer URI */ | |
e00b005a | 822 | location[1024], /* printer-location */ |
823 | info[1024], /* printer-info */ | |
824 | make_model[1024]; | |
825 | /* printer-make-and-model */ | |
826 | cupsd_netif_t *iface; /* Network interface */ | |
ef416fc2 | 827 | |
ef416fc2 | 828 | |
829 | /* | |
e00b005a | 830 | * Figure out the printer type value... |
ef416fc2 | 831 | */ |
832 | ||
e00b005a | 833 | type = p->type | CUPS_PRINTER_REMOTE; |
ef416fc2 | 834 | |
e00b005a | 835 | if (!p->accepting) |
836 | type |= CUPS_PRINTER_REJECTING; | |
ef416fc2 | 837 | |
e00b005a | 838 | if (p == DefaultPrinter) |
839 | type |= CUPS_PRINTER_DEFAULT; | |
ef416fc2 | 840 | |
ef416fc2 | 841 | /* |
e00b005a | 842 | * Remove quotes from printer-info, printer-location, and |
843 | * printer-make-and-model attributes... | |
ef416fc2 | 844 | */ |
845 | ||
e00b005a | 846 | dequote(location, p->location, sizeof(p->location)); |
847 | dequote(info, p->info, sizeof(p->info)); | |
848 | dequote(make_model, p->make_model ? p->make_model : "Unknown", | |
849 | sizeof(make_model)); | |
ef416fc2 | 850 | |
e00b005a | 851 | /* |
852 | * Send a packet to each browse address... | |
853 | */ | |
ef416fc2 | 854 | |
e00b005a | 855 | for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++) |
856 | if (b->iface[0]) | |
ef416fc2 | 857 | { |
858 | /* | |
e00b005a | 859 | * Send the browse packet to one or more interfaces... |
ef416fc2 | 860 | */ |
861 | ||
e00b005a | 862 | if (!strcmp(b->iface, "*")) |
ef416fc2 | 863 | { |
864 | /* | |
e00b005a | 865 | * Send to all local interfaces... |
ef416fc2 | 866 | */ |
867 | ||
e00b005a | 868 | cupsdNetIFUpdate(); |
ef416fc2 | 869 | |
e00b005a | 870 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); |
871 | iface; | |
872 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
873 | { | |
874 | /* | |
875 | * Only send to local, IPv4 interfaces... | |
876 | */ | |
ef416fc2 | 877 | |
e00b005a | 878 | if (!iface->is_local || !iface->port || |
879 | iface->address.addr.sa_family != AF_INET) | |
880 | continue; | |
ef416fc2 | 881 | |
e00b005a | 882 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
883 | iface->hostname, iface->port, | |
884 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" : | |
885 | "/printers/%s", | |
886 | p->name); | |
b423cd4c | 887 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", |
888 | type, p->state, uri, location, info, make_model, | |
889 | p->browse_attrs ? p->browse_attrs : ""); | |
ef416fc2 | 890 | |
e00b005a | 891 | bytes = strlen(packet); |
ef416fc2 | 892 | |
e00b005a | 893 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
894 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
895 | iface->name, packet); | |
896 | ||
897 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); | |
898 | ||
899 | sendto(BrowseSocket, packet, bytes, 0, | |
900 | (struct sockaddr *)&(iface->broadcast), | |
901 | sizeof(struct sockaddr_in)); | |
902 | } | |
903 | } | |
904 | else if ((iface = cupsdNetIFFind(b->iface)) != NULL) | |
ef416fc2 | 905 | { |
e00b005a | 906 | /* |
907 | * Send to the named interface using the IPv4 address... | |
908 | */ | |
909 | ||
910 | while (iface) | |
911 | if (strcmp(b->iface, iface->name)) | |
912 | { | |
913 | iface = NULL; | |
ef416fc2 | 914 | break; |
e00b005a | 915 | } |
916 | else if (iface->address.addr.sa_family == AF_INET && iface->port) | |
917 | break; | |
918 | else | |
919 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList); | |
ef416fc2 | 920 | |
e00b005a | 921 | if (iface) |
ef416fc2 | 922 | { |
e00b005a | 923 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
924 | iface->hostname, iface->port, | |
925 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s%s" : | |
926 | "/printers/%s", | |
927 | p->name); | |
b423cd4c | 928 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", |
929 | type, p->state, uri, location, info, make_model, | |
930 | p->browse_attrs ? p->browse_attrs : ""); | |
ef416fc2 | 931 | |
e00b005a | 932 | bytes = strlen(packet); |
ef416fc2 | 933 | |
e00b005a | 934 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
935 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
936 | iface->name, packet); | |
ef416fc2 | 937 | |
e00b005a | 938 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); |
939 | ||
940 | sendto(BrowseSocket, packet, bytes, 0, | |
941 | (struct sockaddr *)&(iface->broadcast), | |
942 | sizeof(struct sockaddr_in)); | |
943 | } | |
ef416fc2 | 944 | } |
945 | } | |
946 | else | |
947 | { | |
948 | /* | |
e00b005a | 949 | * Send the browse packet to the indicated address using |
950 | * the default server name... | |
ef416fc2 | 951 | */ |
952 | ||
b423cd4c | 953 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", |
954 | type, p->state, p->uri, location, info, make_model, | |
955 | p->browse_attrs ? p->browse_attrs : ""); | |
ef416fc2 | 956 | |
e00b005a | 957 | bytes = strlen(packet); |
958 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
959 | "cupsdSendBrowseList: (%d bytes) %s", bytes, packet); | |
ef416fc2 | 960 | |
e00b005a | 961 | if (sendto(BrowseSocket, packet, bytes, 0, |
962 | (struct sockaddr *)&(b->to), | |
963 | sizeof(struct sockaddr_in)) <= 0) | |
ef416fc2 | 964 | { |
965 | /* | |
e00b005a | 966 | * Unable to send browse packet, so remove this address from the |
967 | * list... | |
ef416fc2 | 968 | */ |
969 | ||
e00b005a | 970 | cupsdLogMessage(CUPSD_LOG_ERROR, |
b423cd4c | 971 | "cupsdSendBrowseList: sendto failed for browser " |
972 | "%d - %s.", | |
e00b005a | 973 | b - Browsers + 1, strerror(errno)); |
ef416fc2 | 974 | |
e00b005a | 975 | if (i > 1) |
b423cd4c | 976 | memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t)); |
ef416fc2 | 977 | |
e00b005a | 978 | b --; |
979 | NumBrowsers --; | |
980 | } | |
ef416fc2 | 981 | } |
ef416fc2 | 982 | } |
983 | ||
984 | ||
b423cd4c | 985 | #ifdef HAVE_OPENLDAP |
986 | /* | |
987 | * cupsdSendLDAPBrowse()' - Send LDAP printer registrations. | |
988 | */ | |
989 | ||
990 | void | |
991 | cupsdSendLDAPBrowse(cupsd_printer_t *p) /* I - Printer to register */ | |
992 | { | |
993 | int i; /* Looping var... */ | |
994 | LDAPMod mods[7]; /* The 7 attributes we will be adding */ | |
995 | LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */ | |
996 | LDAPMessage *res; /* Search result token */ | |
997 | char *cn_value[2], /* Change records */ | |
998 | *uri[2], | |
999 | *info[2], | |
1000 | *location[2], | |
1001 | *make_model[2], | |
1002 | *type[2], | |
1003 | typestring[255], /* String to hold printer-type */ | |
1004 | filter[256], /* Search filter for possible UPDATEs */ | |
1005 | dn[1024]; /* DN of the printer we are adding */ | |
1006 | int rc; /* LDAP status */ | |
1007 | static const char * const objectClass_values[] = | |
1008 | { /* The 3 objectClass's we use in */ | |
1009 | "top", /* our LDAP entries */ | |
1010 | "device", | |
1011 | "cupsPrinter", | |
1012 | NULL | |
1013 | }; | |
1014 | ||
1015 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSendLDAPBrowse: %s\n", p->name); | |
1016 | ||
1017 | /* | |
1018 | * Everything in ldap is ** so we fudge around it... | |
1019 | */ | |
1020 | ||
1021 | sprintf(typestring, "%u", p->type); | |
1022 | ||
1023 | cn_value[0] = p->info; | |
1024 | cn_value[1] = NULL; | |
1025 | info[0] = p->info; | |
1026 | info[1] = NULL; | |
1027 | location[0] = p->location; | |
1028 | location[1] = NULL; | |
1029 | make_model[0] = p->make_model; | |
1030 | make_model[1] = NULL; | |
1031 | type[0] = typestring; | |
1032 | type[1] = NULL; | |
1033 | uri[0] = p->uri; | |
1034 | uri[1] = NULL; | |
1035 | ||
1036 | snprintf(filter, sizeof(filter), | |
1037 | "(&(objectclass=cupsPrinter)(printerDescription~=%s))", p->info); | |
1038 | ||
1039 | ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE, | |
1040 | filter, (char **)ldap_attrs, 0, &res); | |
1041 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSendLDAPBrowse: Searching \"%s\"", | |
1042 | filter); | |
1043 | ||
1044 | mods[0].mod_type = "cn"; | |
1045 | mods[0].mod_values = cn_value; | |
1046 | mods[1].mod_type = "printerDescription"; | |
1047 | mods[1].mod_values = info; | |
1048 | mods[2].mod_type = "printerURI"; | |
1049 | mods[2].mod_values = uri; | |
1050 | mods[3].mod_type = "printerLocation"; | |
1051 | mods[3].mod_values = location; | |
1052 | mods[4].mod_type = "printerMakeAndModel"; | |
1053 | mods[4].mod_values = make_model; | |
1054 | mods[5].mod_type = "printerType"; | |
1055 | mods[5].mod_values = type; | |
1056 | mods[6].mod_type = "objectClass"; | |
1057 | mods[6].mod_values = (char **)objectClass_values; | |
1058 | ||
1059 | snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->info, BrowseLDAPDN); | |
1060 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdSendLDAPBrowse: dn=\"%s\"", dn); | |
1061 | ||
1062 | if (ldap_count_entries(BrowseLDAPHandle, res) > 0) | |
1063 | { | |
1064 | /* | |
1065 | * Printer has already been registered, modify the current | |
1066 | * registration... | |
1067 | */ | |
1068 | ||
1069 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1070 | "cupsdSendLDAPBrowse: Replacing entry..."); | |
1071 | ||
1072 | for (i = 0; i < 7; i ++) | |
1073 | { | |
1074 | pmods[i] = mods + i; | |
1075 | pmods[i]->mod_op = LDAP_MOD_REPLACE; | |
1076 | } | |
1077 | pmods[i] = NULL; | |
1078 | ||
1079 | if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
1080 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1081 | "LDAP modify for %s failed with status %d: %s", | |
1082 | p->name, rc, ldap_err2string(rc)); | |
1083 | } | |
1084 | else | |
1085 | { | |
1086 | /* | |
1087 | * Printer has already been registered, modify the current | |
1088 | * registration... | |
1089 | */ | |
1090 | ||
1091 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
1092 | "cupsdSendLDAPBrowse: Adding entry..."); | |
1093 | ||
1094 | for (i = 0; i < 7; i ++) | |
1095 | { | |
1096 | pmods[i] = mods + i; | |
1097 | pmods[i]->mod_op = LDAP_MOD_REPLACE; | |
1098 | } | |
1099 | pmods[i] = NULL; | |
1100 | ||
1101 | if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
1102 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1103 | "LDAP add for %s failed with status %d: %s", | |
1104 | p->name, rc, ldap_err2string(rc)); | |
1105 | } | |
1106 | } | |
1107 | #endif /* HAVE_OPENLDAP */ | |
1108 | ||
1109 | ||
e00b005a | 1110 | #ifdef HAVE_LIBSLP |
ef416fc2 | 1111 | /* |
e00b005a | 1112 | * 'cupsdSendSLPBrowse()' - Register the specified printer with SLP. |
ef416fc2 | 1113 | */ |
1114 | ||
e00b005a | 1115 | void |
1116 | cupsdSendSLPBrowse(cupsd_printer_t *p) /* I - Printer to register */ | |
ef416fc2 | 1117 | { |
e00b005a | 1118 | char srvurl[HTTP_MAX_URI], /* Printer service URI */ |
1119 | attrs[8192], /* Printer attributes */ | |
1120 | finishings[1024], /* Finishings to support */ | |
1121 | make_model[IPP_MAX_NAME * 2], | |
1122 | /* Make and model, quoted */ | |
1123 | location[IPP_MAX_NAME * 2], | |
1124 | /* Location, quoted */ | |
1125 | info[IPP_MAX_NAME * 2], /* Info, quoted */ | |
1126 | *src, /* Pointer to original string */ | |
1127 | *dst; /* Pointer to destination string */ | |
1128 | ipp_attribute_t *authentication; /* uri-authentication-supported value */ | |
1129 | SLPError error; /* SLP error, if any */ | |
ef416fc2 | 1130 | |
ef416fc2 | 1131 | |
e00b005a | 1132 | cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdSendSLPBrowse(%p = \"%s\")", p, |
1133 | p->name); | |
ef416fc2 | 1134 | |
1135 | /* | |
e00b005a | 1136 | * Make the SLP service URL that conforms to the IANA |
1137 | * 'printer:' template. | |
ef416fc2 | 1138 | */ |
1139 | ||
e00b005a | 1140 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); |
ef416fc2 | 1141 | |
e00b005a | 1142 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl); |
ef416fc2 | 1143 | |
1144 | /* | |
e00b005a | 1145 | * Figure out the finishings string... |
ef416fc2 | 1146 | */ |
1147 | ||
e00b005a | 1148 | if (p->type & CUPS_PRINTER_STAPLE) |
1149 | strcpy(finishings, "staple"); | |
1150 | else | |
1151 | finishings[0] = '\0'; | |
ef416fc2 | 1152 | |
e00b005a | 1153 | if (p->type & CUPS_PRINTER_BIND) |
1154 | { | |
1155 | if (finishings[0]) | |
1156 | strlcat(finishings, ",bind", sizeof(finishings)); | |
1157 | else | |
1158 | strcpy(finishings, "bind"); | |
1159 | } | |
ef416fc2 | 1160 | |
e00b005a | 1161 | if (p->type & CUPS_PRINTER_PUNCH) |
1162 | { | |
1163 | if (finishings[0]) | |
1164 | strlcat(finishings, ",punch", sizeof(finishings)); | |
1165 | else | |
1166 | strcpy(finishings, "punch"); | |
1167 | } | |
ef416fc2 | 1168 | |
e00b005a | 1169 | if (p->type & CUPS_PRINTER_COVER) |
1170 | { | |
1171 | if (finishings[0]) | |
1172 | strlcat(finishings, ",cover", sizeof(finishings)); | |
ef416fc2 | 1173 | else |
e00b005a | 1174 | strcpy(finishings, "cover"); |
1175 | } | |
ef416fc2 | 1176 | |
e00b005a | 1177 | if (p->type & CUPS_PRINTER_SORT) |
1178 | { | |
1179 | if (finishings[0]) | |
1180 | strlcat(finishings, ",sort", sizeof(finishings)); | |
ef416fc2 | 1181 | else |
e00b005a | 1182 | strcpy(finishings, "sort"); |
1183 | } | |
ef416fc2 | 1184 | |
e00b005a | 1185 | if (!finishings[0]) |
1186 | strcpy(finishings, "none"); | |
ef416fc2 | 1187 | |
e00b005a | 1188 | /* |
1189 | * Quote any commas in the make and model, location, and info strings... | |
1190 | */ | |
ef416fc2 | 1191 | |
e00b005a | 1192 | for (src = p->make_model, dst = make_model; |
1193 | src && *src && dst < (make_model + sizeof(make_model) - 2);) | |
1194 | { | |
1195 | if (*src == ',' || *src == '\\' || *src == ')') | |
1196 | *dst++ = '\\'; | |
ef416fc2 | 1197 | |
e00b005a | 1198 | *dst++ = *src++; |
1199 | } | |
ef416fc2 | 1200 | |
e00b005a | 1201 | *dst = '\0'; |
ef416fc2 | 1202 | |
e00b005a | 1203 | if (!make_model[0]) |
1204 | strcpy(make_model, "Unknown"); | |
ef416fc2 | 1205 | |
e00b005a | 1206 | for (src = p->location, dst = location; |
1207 | src && *src && dst < (location + sizeof(location) - 2);) | |
1208 | { | |
1209 | if (*src == ',' || *src == '\\' || *src == ')') | |
1210 | *dst++ = '\\'; | |
ef416fc2 | 1211 | |
e00b005a | 1212 | *dst++ = *src++; |
1213 | } | |
ef416fc2 | 1214 | |
e00b005a | 1215 | *dst = '\0'; |
ef416fc2 | 1216 | |
e00b005a | 1217 | if (!location[0]) |
1218 | strcpy(location, "Unknown"); | |
ef416fc2 | 1219 | |
e00b005a | 1220 | for (src = p->info, dst = info; |
1221 | src && *src && dst < (info + sizeof(info) - 2);) | |
1222 | { | |
1223 | if (*src == ',' || *src == '\\' || *src == ')') | |
1224 | *dst++ = '\\'; | |
ef416fc2 | 1225 | |
e00b005a | 1226 | *dst++ = *src++; |
1227 | } | |
ef416fc2 | 1228 | |
e00b005a | 1229 | *dst = '\0'; |
ef416fc2 | 1230 | |
e00b005a | 1231 | if (!info[0]) |
1232 | strcpy(info, "Unknown"); | |
ef416fc2 | 1233 | |
ef416fc2 | 1234 | /* |
e00b005a | 1235 | * Get the authentication value... |
ef416fc2 | 1236 | */ |
1237 | ||
e00b005a | 1238 | authentication = ippFindAttribute(p->attrs, "uri-authentication-supported", |
1239 | IPP_TAG_KEYWORD); | |
ef416fc2 | 1240 | |
1241 | /* | |
e00b005a | 1242 | * Make the SLP attribute string list that conforms to |
1243 | * the IANA 'printer:' template. | |
ef416fc2 | 1244 | */ |
1245 | ||
e00b005a | 1246 | snprintf(attrs, sizeof(attrs), |
1247 | "(printer-uri-supported=%s)," | |
1248 | "(uri-authentication-supported=%s>)," | |
1249 | #ifdef HAVE_SSL | |
1250 | "(uri-security-supported=tls>)," | |
1251 | #else | |
1252 | "(uri-security-supported=none>)," | |
1253 | #endif /* HAVE_SSL */ | |
1254 | "(printer-name=%s)," | |
1255 | "(printer-location=%s)," | |
1256 | "(printer-info=%s)," | |
1257 | "(printer-more-info=%s)," | |
1258 | "(printer-make-and-model=%s)," | |
b423cd4c | 1259 | "(printer-type=%d)," |
e00b005a | 1260 | "(charset-supported=utf-8)," |
1261 | "(natural-language-configured=%s)," | |
1262 | "(natural-language-supported=de,en,es,fr,it)," | |
1263 | "(color-supported=%s)," | |
1264 | "(finishings-supported=%s)," | |
1265 | "(sides-supported=one-sided%s)," | |
1266 | "(multiple-document-jobs-supported=true)" | |
1267 | "(ipp-versions-supported=1.0,1.1)", | |
1268 | p->uri, authentication->values[0].string.text, p->name, location, | |
b423cd4c | 1269 | info, p->uri, make_model, p->type, DefaultLanguage, |
e00b005a | 1270 | p->type & CUPS_PRINTER_COLOR ? "true" : "false", |
1271 | finishings, | |
1272 | p->type & CUPS_PRINTER_DUPLEX ? | |
1273 | ",two-sided-long-edge,two-sided-short-edge" : ""); | |
1274 | ||
1275 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs); | |
ef416fc2 | 1276 | |
1277 | /* | |
e00b005a | 1278 | * Register the printer with the SLP server... |
ef416fc2 | 1279 | */ |
1280 | ||
e00b005a | 1281 | error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout, |
1282 | SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0); | |
ef416fc2 | 1283 | |
e00b005a | 1284 | if (error != SLP_OK) |
1285 | cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name, | |
1286 | error); | |
1287 | } | |
1288 | #endif /* HAVE_LIBSLP */ | |
1289 | ||
1290 | ||
1291 | /* | |
1292 | * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information. | |
ef416fc2 | 1293 | */ |
1294 | ||
1295 | void | |
e00b005a | 1296 | cupsdStartBrowsing(void) |
ef416fc2 | 1297 | { |
e00b005a | 1298 | int val; /* Socket option value */ |
1299 | struct sockaddr_in addr; /* Broadcast address */ | |
ef416fc2 | 1300 | |
1301 | ||
e00b005a | 1302 | BrowseNext = NULL; |
ef416fc2 | 1303 | |
e00b005a | 1304 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
1305 | return; | |
ef416fc2 | 1306 | |
e00b005a | 1307 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) |
1308 | { | |
1309 | if (BrowseSocket < 0) | |
1310 | { | |
1311 | /* | |
1312 | * Create the broadcast socket... | |
1313 | */ | |
ef416fc2 | 1314 | |
e00b005a | 1315 | if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
1316 | { | |
1317 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1318 | "cupsdStartBrowsing: Unable to create broadcast " | |
1319 | "socket - %s.", strerror(errno)); | |
1320 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1321 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
1322 | return; | |
1323 | } | |
ef416fc2 | 1324 | |
e00b005a | 1325 | /* |
1326 | * Bind the socket to browse port... | |
1327 | */ | |
ef416fc2 | 1328 | |
e00b005a | 1329 | memset(&addr, 0, sizeof(addr)); |
1330 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
1331 | addr.sin_family = AF_INET; | |
1332 | addr.sin_port = htons(BrowsePort); | |
ef416fc2 | 1333 | |
e00b005a | 1334 | if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr))) |
1335 | { | |
1336 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1337 | "cupsdStartBrowsing: Unable to bind broadcast " | |
1338 | "socket - %s.", strerror(errno)); | |
ef416fc2 | 1339 | |
e00b005a | 1340 | #ifdef WIN32 |
1341 | closesocket(BrowseSocket); | |
1342 | #else | |
1343 | close(BrowseSocket); | |
1344 | #endif /* WIN32 */ | |
ef416fc2 | 1345 | |
e00b005a | 1346 | BrowseSocket = -1; |
1347 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1348 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
1349 | return; | |
1350 | } | |
1351 | } | |
ef416fc2 | 1352 | |
1353 | /* | |
e00b005a | 1354 | * Set the "broadcast" flag... |
ef416fc2 | 1355 | */ |
1356 | ||
e00b005a | 1357 | val = 1; |
1358 | if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) | |
ef416fc2 | 1359 | { |
e00b005a | 1360 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1361 | "cupsdStartBrowsing: Unable to set broadcast mode - %s.", | |
1362 | strerror(errno)); | |
ef416fc2 | 1363 | |
e00b005a | 1364 | #ifdef WIN32 |
1365 | closesocket(BrowseSocket); | |
1366 | #else | |
1367 | close(BrowseSocket); | |
1368 | #endif /* WIN32 */ | |
ef416fc2 | 1369 | |
e00b005a | 1370 | BrowseSocket = -1; |
1371 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1372 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
1373 | return; | |
1374 | } | |
ef416fc2 | 1375 | |
e00b005a | 1376 | /* |
1377 | * Close the socket on exec... | |
1378 | */ | |
ef416fc2 | 1379 | |
e00b005a | 1380 | fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC); |
ef416fc2 | 1381 | |
1382 | /* | |
e00b005a | 1383 | * Finally, add the socket to the input selection set... |
ef416fc2 | 1384 | */ |
1385 | ||
e00b005a | 1386 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1387 | "cupsdStartBrowsing: Adding fd %d to InputSet...", | |
1388 | BrowseSocket); | |
ef416fc2 | 1389 | |
e00b005a | 1390 | FD_SET(BrowseSocket, InputSet); |
1391 | } | |
1392 | else | |
1393 | BrowseSocket = -1; | |
ef416fc2 | 1394 | |
e00b005a | 1395 | #ifdef HAVE_LIBSLP |
1396 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) | |
ef416fc2 | 1397 | { |
e00b005a | 1398 | /* |
1399 | * Open SLP handle... | |
ef416fc2 | 1400 | */ |
1401 | ||
e00b005a | 1402 | if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK) |
ef416fc2 | 1403 | { |
e00b005a | 1404 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1405 | "Unable to open an SLP handle; disabling SLP browsing!"); | |
1406 | BrowseLocalProtocols &= ~BROWSE_SLP; | |
1407 | BrowseRemoteProtocols &= ~BROWSE_SLP; | |
ef416fc2 | 1408 | } |
e00b005a | 1409 | |
1410 | BrowseSLPRefresh = 0; | |
ef416fc2 | 1411 | } |
e00b005a | 1412 | #endif /* HAVE_LIBSLP */ |
b423cd4c | 1413 | |
1414 | #ifdef HAVE_OPENLDAP | |
1415 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) | |
1416 | { | |
1417 | if (!BrowseLDAPDN) | |
1418 | { | |
1419 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1420 | "Need to set BrowseLDAPDN to use LDAP browsing!"); | |
1421 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1422 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1423 | } | |
1424 | else | |
1425 | { | |
1426 | /* | |
1427 | * Open LDAP handle... | |
1428 | */ | |
1429 | ||
1430 | int rc; /* LDAP API status */ | |
1431 | int version = 3; /* LDAP version */ | |
1432 | struct berval bv = {0, ""}; /* SASL bind value */ | |
1433 | ||
1434 | ||
1435 | /* | |
1436 | * LDAP stuff currently only supports ldapi EXTERNAL SASL binds... | |
1437 | */ | |
1438 | ||
1439 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) | |
1440 | rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///"); | |
1441 | else | |
1442 | rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer); | |
1443 | ||
1444 | if (rc != LDAP_SUCCESS) | |
1445 | { | |
1446 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1447 | "Unable to initialize LDAP; disabling LDAP browsing!"); | |
1448 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1449 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1450 | } | |
1451 | else if (ldap_set_option(BrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION, | |
1452 | (const void *)&version) != LDAP_SUCCESS) | |
1453 | { | |
1454 | ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL); | |
1455 | BrowseLDAPHandle = NULL; | |
1456 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1457 | "Unable to set LDAP protocol version; " | |
1458 | "disabling LDAP browsing!"); | |
1459 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1460 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1461 | } | |
1462 | else | |
1463 | { | |
1464 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) | |
1465 | rc = ldap_sasl_bind_s(BrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL, | |
1466 | NULL, NULL); | |
1467 | else | |
1468 | rc = ldap_bind_s(BrowseLDAPHandle, BrowseLDAPBindDN, | |
1469 | BrowseLDAPPassword, LDAP_AUTH_SIMPLE); | |
1470 | ||
1471 | if (rc != LDAP_SUCCESS) | |
1472 | { | |
1473 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1474 | "Unable to bind to LDAP server; " | |
1475 | "disabling LDAP browsing!"); | |
1476 | ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL); | |
1477 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1478 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1479 | } | |
1480 | } | |
1481 | } | |
1482 | ||
1483 | BrowseLDAPRefresh = 0; | |
1484 | } | |
1485 | #endif /* HAVE_OPENLDAP */ | |
ef416fc2 | 1486 | } |
1487 | ||
1488 | ||
1489 | /* | |
e00b005a | 1490 | * 'cupsdStartPolling()' - Start polling servers as needed. |
ef416fc2 | 1491 | */ |
1492 | ||
1493 | void | |
e00b005a | 1494 | cupsdStartPolling(void) |
ef416fc2 | 1495 | { |
1496 | int i; /* Looping var */ | |
e00b005a | 1497 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ |
1498 | char polld[1024]; /* Poll daemon path */ | |
1499 | char sport[10]; /* Server port */ | |
1500 | char bport[10]; /* Browser port */ | |
1501 | char interval[10]; /* Poll interval */ | |
1502 | int statusfds[2]; /* Status pipe */ | |
1503 | char *argv[6]; /* Arguments */ | |
1504 | char *envp[100]; /* Environment */ | |
ef416fc2 | 1505 | |
1506 | ||
1507 | /* | |
e00b005a | 1508 | * Don't do anything if we aren't polling... |
ef416fc2 | 1509 | */ |
1510 | ||
e00b005a | 1511 | if (NumPolled == 0) |
1512 | { | |
1513 | PollPipe = -1; | |
1514 | PollStatusBuffer = NULL; | |
1515 | return; | |
1516 | } | |
ef416fc2 | 1517 | |
1518 | /* | |
e00b005a | 1519 | * Setup string arguments for polld, port and interval options. |
ef416fc2 | 1520 | */ |
1521 | ||
e00b005a | 1522 | snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin); |
ef416fc2 | 1523 | |
e00b005a | 1524 | sprintf(bport, "%d", BrowsePort); |
ef416fc2 | 1525 | |
e00b005a | 1526 | if (BrowseInterval) |
1527 | sprintf(interval, "%d", BrowseInterval); | |
1528 | else | |
1529 | strcpy(interval, "30"); | |
ef416fc2 | 1530 | |
e00b005a | 1531 | argv[0] = "cups-polld"; |
1532 | argv[2] = sport; | |
1533 | argv[3] = interval; | |
1534 | argv[4] = bport; | |
1535 | argv[5] = NULL; | |
ef416fc2 | 1536 | |
e00b005a | 1537 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
ef416fc2 | 1538 | |
e00b005a | 1539 | /* |
1540 | * Create a pipe that receives the status messages from each | |
1541 | * polling daemon... | |
1542 | */ | |
ef416fc2 | 1543 | |
e00b005a | 1544 | if (cupsdOpenPipe(statusfds)) |
1545 | { | |
1546 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1547 | "Unable to create polling status pipes - %s.", | |
1548 | strerror(errno)); | |
1549 | PollPipe = -1; | |
1550 | PollStatusBuffer = NULL; | |
1551 | return; | |
1552 | } | |
ef416fc2 | 1553 | |
e00b005a | 1554 | PollPipe = statusfds[0]; |
1555 | PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]"); | |
ef416fc2 | 1556 | |
e00b005a | 1557 | /* |
1558 | * Run each polling daemon, redirecting stderr to the polling pipe... | |
1559 | */ | |
ef416fc2 | 1560 | |
e00b005a | 1561 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
1562 | { | |
1563 | sprintf(sport, "%d", pollp->port); | |
ef416fc2 | 1564 | |
e00b005a | 1565 | argv[1] = pollp->hostname; |
ef416fc2 | 1566 | |
e00b005a | 1567 | if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, |
1568 | 0, &(pollp->pid)) < 0) | |
1569 | { | |
1570 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1571 | "cupsdStartPolling: Unable to fork polling daemon - %s", | |
1572 | strerror(errno)); | |
1573 | pollp->pid = 0; | |
1574 | break; | |
ef416fc2 | 1575 | } |
1576 | else | |
e00b005a | 1577 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1578 | "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d", | |
1579 | pollp->hostname, pollp->port, pollp->pid); | |
1580 | } | |
ef416fc2 | 1581 | |
e00b005a | 1582 | close(statusfds[1]); |
ef416fc2 | 1583 | |
e00b005a | 1584 | /* |
1585 | * Finally, add the pipe to the input selection set... | |
1586 | */ | |
ef416fc2 | 1587 | |
e00b005a | 1588 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1589 | "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe); | |
ef416fc2 | 1590 | |
e00b005a | 1591 | FD_SET(PollPipe, InputSet); |
ef416fc2 | 1592 | } |
1593 | ||
1594 | ||
ef416fc2 | 1595 | /* |
e00b005a | 1596 | * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information. |
ef416fc2 | 1597 | */ |
1598 | ||
e00b005a | 1599 | void |
1600 | cupsdStopBrowsing(void) | |
ef416fc2 | 1601 | { |
e00b005a | 1602 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
1603 | return; | |
ef416fc2 | 1604 | |
e00b005a | 1605 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) && |
1606 | BrowseSocket >= 0) | |
1607 | { | |
1608 | /* | |
1609 | * Close the socket and remove it from the input selection set. | |
1610 | */ | |
ef416fc2 | 1611 | |
e00b005a | 1612 | #ifdef WIN32 |
1613 | closesocket(BrowseSocket); | |
1614 | #else | |
1615 | close(BrowseSocket); | |
1616 | #endif /* WIN32 */ | |
ef416fc2 | 1617 | |
e00b005a | 1618 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1619 | "cupsdStopBrowsing: Removing fd %d from InputSet...", | |
1620 | BrowseSocket); | |
ef416fc2 | 1621 | |
e00b005a | 1622 | FD_CLR(BrowseSocket, InputSet); |
1623 | BrowseSocket = -1; | |
ef416fc2 | 1624 | } |
1625 | ||
e00b005a | 1626 | #ifdef HAVE_LIBSLP |
b423cd4c | 1627 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) && |
1628 | BrowseSLPHandle) | |
ef416fc2 | 1629 | { |
e00b005a | 1630 | /* |
1631 | * Close SLP handle... | |
1632 | */ | |
ef416fc2 | 1633 | |
e00b005a | 1634 | SLPClose(BrowseSLPHandle); |
b423cd4c | 1635 | BrowseSLPHandle = NULL; |
ef416fc2 | 1636 | } |
e00b005a | 1637 | #endif /* HAVE_LIBSLP */ |
b423cd4c | 1638 | |
1639 | #ifdef HAVE_OPENDAP | |
1640 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) && | |
1641 | BrowseLDAPHandle) | |
1642 | { | |
1643 | ldap_unbind(BrowseLDAPHandle); | |
1644 | BrowseLDAPHandle = NULL; | |
1645 | } | |
1646 | #endif /* HAVE_OPENLDAP */ | |
e00b005a | 1647 | } |
ef416fc2 | 1648 | |
ef416fc2 | 1649 | |
e00b005a | 1650 | /* |
1651 | * 'cupsdStopPolling()' - Stop polling servers as needed. | |
1652 | */ | |
ef416fc2 | 1653 | |
e00b005a | 1654 | void |
1655 | cupsdStopPolling(void) | |
1656 | { | |
1657 | int i; /* Looping var */ | |
1658 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
ef416fc2 | 1659 | |
e00b005a | 1660 | |
1661 | if (PollPipe >= 0) | |
ef416fc2 | 1662 | { |
e00b005a | 1663 | cupsdStatBufDelete(PollStatusBuffer); |
1664 | close(PollPipe); | |
ef416fc2 | 1665 | |
e00b005a | 1666 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1667 | "cupsdStopPolling: removing fd %d from InputSet.", PollPipe); | |
1668 | FD_CLR(PollPipe, InputSet); | |
ef416fc2 | 1669 | |
e00b005a | 1670 | PollPipe = -1; |
1671 | PollStatusBuffer = NULL; | |
1672 | } | |
ef416fc2 | 1673 | |
e00b005a | 1674 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
1675 | if (pollp->pid) | |
1676 | cupsdEndProcess(pollp->pid, 0); | |
1677 | } | |
ef416fc2 | 1678 | |
ef416fc2 | 1679 | |
e00b005a | 1680 | /* |
1681 | * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol. | |
1682 | */ | |
ef416fc2 | 1683 | |
e00b005a | 1684 | void |
1685 | cupsdUpdateCUPSBrowse(void) | |
1686 | { | |
1687 | int i; /* Looping var */ | |
1688 | int auth; /* Authorization status */ | |
1689 | int len; /* Length of name string */ | |
1690 | int bytes; /* Number of bytes left */ | |
1691 | char packet[1541], /* Broadcast packet */ | |
1692 | *pptr; /* Pointer into packet */ | |
1693 | socklen_t srclen; /* Length of source address */ | |
1694 | http_addr_t srcaddr; /* Source address */ | |
1695 | char srcname[1024]; /* Source hostname */ | |
1696 | unsigned address[4]; /* Source address */ | |
1697 | unsigned type; /* Printer type */ | |
1698 | unsigned state; /* Printer state */ | |
1699 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
e00b005a | 1700 | host[HTTP_MAX_URI], /* Host portion of URI */ |
1701 | resource[HTTP_MAX_URI], /* Resource portion of URI */ | |
1702 | info[IPP_MAX_NAME], /* Information string */ | |
1703 | location[IPP_MAX_NAME], /* Location string */ | |
1704 | make_model[IPP_MAX_NAME];/* Make and model string */ | |
e00b005a | 1705 | int num_attrs; /* Number of attributes */ |
1706 | cups_option_t *attrs; /* Attributes */ | |
ef416fc2 | 1707 | |
ef416fc2 | 1708 | |
e00b005a | 1709 | /* |
1710 | * Read a packet from the browse socket... | |
1711 | */ | |
ef416fc2 | 1712 | |
e00b005a | 1713 | srclen = sizeof(srcaddr); |
1714 | if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0, | |
1715 | (struct sockaddr *)&srcaddr, &srclen)) < 0) | |
1716 | { | |
1717 | /* | |
1718 | * "Connection refused" is returned under Linux if the destination port | |
1719 | * or address is unreachable from a previous sendto(); check for the | |
1720 | * error here and ignore it for now... | |
1721 | */ | |
ef416fc2 | 1722 | |
e00b005a | 1723 | if (errno != ECONNREFUSED && errno != EAGAIN) |
1724 | { | |
1725 | cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.", | |
1726 | strerror(errno)); | |
1727 | cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off."); | |
ef416fc2 | 1728 | |
e00b005a | 1729 | cupsdStopBrowsing(); |
1730 | Browsing = 0; | |
1731 | } | |
ef416fc2 | 1732 | |
e00b005a | 1733 | return; |
1734 | } | |
ef416fc2 | 1735 | |
e00b005a | 1736 | packet[bytes] = '\0'; |
ef416fc2 | 1737 | |
1738 | /* | |
e00b005a | 1739 | * If we're about to sleep, ignore incoming browse packets. |
ef416fc2 | 1740 | */ |
1741 | ||
e00b005a | 1742 | if (Sleeping) |
1743 | return; | |
ef416fc2 | 1744 | |
1745 | /* | |
e00b005a | 1746 | * Figure out where it came from... |
ef416fc2 | 1747 | */ |
1748 | ||
e00b005a | 1749 | #ifdef AF_INET6 |
1750 | if (srcaddr.addr.sa_family == AF_INET6) | |
1751 | { | |
1752 | address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]); | |
1753 | address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]); | |
1754 | address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]); | |
1755 | address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]); | |
1756 | } | |
1757 | else | |
1758 | #endif /* AF_INET6 */ | |
1759 | { | |
1760 | address[0] = 0; | |
1761 | address[1] = 0; | |
1762 | address[2] = 0; | |
1763 | address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr); | |
1764 | } | |
ef416fc2 | 1765 | |
e00b005a | 1766 | if (HostNameLookups) |
1767 | httpAddrLookup(&srcaddr, srcname, sizeof(srcname)); | |
1768 | else | |
1769 | httpAddrString(&srcaddr, srcname, sizeof(srcname)); | |
ef416fc2 | 1770 | |
e00b005a | 1771 | len = strlen(srcname); |
ef416fc2 | 1772 | |
e00b005a | 1773 | /* |
1774 | * Do ACL stuff... | |
1775 | */ | |
ef416fc2 | 1776 | |
e00b005a | 1777 | if (BrowseACL) |
ef416fc2 | 1778 | { |
e00b005a | 1779 | if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost")) |
ef416fc2 | 1780 | { |
09ec0018 | 1781 | /* |
e00b005a | 1782 | * Access from localhost (127.0.0.1) is always allowed... |
09ec0018 | 1783 | */ |
ef416fc2 | 1784 | |
e00b005a | 1785 | auth = AUTH_ALLOW; |
1786 | } | |
1787 | else | |
1788 | { | |
09ec0018 | 1789 | /* |
e00b005a | 1790 | * Do authorization checks on the domain/address... |
09ec0018 | 1791 | */ |
1792 | ||
e00b005a | 1793 | switch (BrowseACL->order_type) |
09ec0018 | 1794 | { |
e00b005a | 1795 | default : |
1796 | auth = AUTH_DENY; /* anti-compiler-warning-code */ | |
1797 | break; | |
ef416fc2 | 1798 | |
e00b005a | 1799 | case AUTH_ALLOW : /* Order Deny,Allow */ |
1800 | auth = AUTH_ALLOW; | |
ef416fc2 | 1801 | |
e00b005a | 1802 | if (cupsdCheckAuth(address, srcname, len, |
1803 | BrowseACL->num_deny, BrowseACL->deny)) | |
1804 | auth = AUTH_DENY; | |
ef416fc2 | 1805 | |
e00b005a | 1806 | if (cupsdCheckAuth(address, srcname, len, |
1807 | BrowseACL->num_allow, BrowseACL->allow)) | |
1808 | auth = AUTH_ALLOW; | |
1809 | break; | |
ef416fc2 | 1810 | |
e00b005a | 1811 | case AUTH_DENY : /* Order Allow,Deny */ |
1812 | auth = AUTH_DENY; | |
ef416fc2 | 1813 | |
e00b005a | 1814 | if (cupsdCheckAuth(address, srcname, len, |
1815 | BrowseACL->num_allow, BrowseACL->allow)) | |
1816 | auth = AUTH_ALLOW; | |
ef416fc2 | 1817 | |
e00b005a | 1818 | if (cupsdCheckAuth(address, srcname, len, |
1819 | BrowseACL->num_deny, BrowseACL->deny)) | |
1820 | auth = AUTH_DENY; | |
1821 | break; | |
1822 | } | |
ef416fc2 | 1823 | } |
ef416fc2 | 1824 | } |
1825 | else | |
e00b005a | 1826 | auth = AUTH_ALLOW; |
ef416fc2 | 1827 | |
e00b005a | 1828 | if (auth == AUTH_DENY) |
ef416fc2 | 1829 | { |
e00b005a | 1830 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
1831 | "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes, | |
1832 | srcname); | |
1833 | return; | |
ef416fc2 | 1834 | } |
ef416fc2 | 1835 | |
e00b005a | 1836 | cupsdLogMessage(CUPSD_LOG_DEBUG2, |
1837 | "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes, | |
1838 | srcname, packet); | |
ef416fc2 | 1839 | |
1840 | /* | |
e00b005a | 1841 | * Parse packet... |
ef416fc2 | 1842 | */ |
1843 | ||
e00b005a | 1844 | if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3) |
ef416fc2 | 1845 | { |
e00b005a | 1846 | cupsdLogMessage(CUPSD_LOG_WARN, |
1847 | "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet); | |
ef416fc2 | 1848 | return; |
1849 | } | |
1850 | ||
e00b005a | 1851 | strcpy(location, "Location Unknown"); |
1852 | strcpy(info, "No Information Available"); | |
1853 | make_model[0] = '\0'; | |
1854 | num_attrs = 0; | |
1855 | attrs = NULL; | |
ef416fc2 | 1856 | |
e00b005a | 1857 | if ((pptr = strchr(packet, '\"')) != NULL) |
1858 | { | |
1859 | /* | |
1860 | * Have extended information; can't use sscanf for it because not all | |
1861 | * sscanf's allow empty strings with %[^\"]... | |
1862 | */ | |
ef416fc2 | 1863 | |
e00b005a | 1864 | for (i = 0, pptr ++; |
1865 | i < (sizeof(location) - 1) && *pptr && *pptr != '\"'; | |
1866 | i ++, pptr ++) | |
1867 | location[i] = *pptr; | |
ef416fc2 | 1868 | |
e00b005a | 1869 | if (i) |
1870 | location[i] = '\0'; | |
ef416fc2 | 1871 | |
e00b005a | 1872 | if (*pptr == '\"') |
1873 | pptr ++; | |
ef416fc2 | 1874 | |
e00b005a | 1875 | while (*pptr && isspace(*pptr & 255)) |
1876 | pptr ++; | |
1877 | ||
1878 | if (*pptr == '\"') | |
1879 | { | |
1880 | for (i = 0, pptr ++; | |
1881 | i < (sizeof(info) - 1) && *pptr && *pptr != '\"'; | |
1882 | i ++, pptr ++) | |
1883 | info[i] = *pptr; | |
1884 | ||
1885 | info[i] = '\0'; | |
1886 | ||
1887 | if (*pptr == '\"') | |
1888 | pptr ++; | |
1889 | ||
1890 | while (*pptr && isspace(*pptr & 255)) | |
1891 | pptr ++; | |
1892 | ||
1893 | if (*pptr == '\"') | |
1894 | { | |
1895 | for (i = 0, pptr ++; | |
1896 | i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"'; | |
1897 | i ++, pptr ++) | |
1898 | make_model[i] = *pptr; | |
1899 | ||
1900 | if (*pptr == '\"') | |
1901 | pptr ++; | |
1902 | ||
1903 | make_model[i] = '\0'; | |
1904 | ||
1905 | if (*pptr) | |
1906 | num_attrs = cupsParseOptions(pptr, num_attrs, &attrs); | |
1907 | } | |
1908 | } | |
1909 | } | |
1910 | ||
1911 | DEBUG_puts(packet); | |
1912 | DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n" | |
1913 | "location=\"%s\", info=\"%s\", make_model=\"%s\"\n", | |
1914 | type, state, uri, location, info, make_model)); | |
ef416fc2 | 1915 | |
1916 | /* | |
e00b005a | 1917 | * Pull the URI apart to see if this is a local or remote printer... |
ef416fc2 | 1918 | */ |
1919 | ||
b423cd4c | 1920 | if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) |
ef416fc2 | 1921 | { |
e00b005a | 1922 | cupsFreeOptions(num_attrs, attrs); |
1923 | return; | |
1924 | } | |
ef416fc2 | 1925 | |
ef416fc2 | 1926 | /* |
e00b005a | 1927 | * Do relaying... |
ef416fc2 | 1928 | */ |
1929 | ||
e00b005a | 1930 | for (i = 0; i < NumRelays; i ++) |
1931 | if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from))) | |
1932 | if (sendto(BrowseSocket, packet, bytes, 0, | |
1933 | (struct sockaddr *)&(Relays[i].to), | |
1934 | sizeof(http_addr_t)) <= 0) | |
1935 | { | |
1936 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1937 | "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.", | |
1938 | i + 1, strerror(errno)); | |
1939 | cupsFreeOptions(num_attrs, attrs); | |
1940 | return; | |
1941 | } | |
ef416fc2 | 1942 | |
e00b005a | 1943 | /* |
1944 | * Process the browse data... | |
1945 | */ | |
1946 | ||
b423cd4c | 1947 | process_browse_data(uri, host, resource, (cups_ptype_t)type, |
1948 | (ipp_pstate_t)state, location, info, make_model, | |
1949 | num_attrs, attrs); | |
ef416fc2 | 1950 | } |
1951 | ||
1952 | ||
b423cd4c | 1953 | #ifdef HAVE_OPENLDAP |
1954 | /* | |
1955 | * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP... | |
1956 | */ | |
1957 | ||
1958 | void | |
1959 | cupsdUpdateLDAPBrowse(void) | |
1960 | { | |
1961 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
1962 | host[HTTP_MAX_URI], /* Hostname */ | |
1963 | resource[HTTP_MAX_URI], /* Resource path */ | |
1964 | location[1024], /* Printer location */ | |
1965 | info[1024], /* Printer information */ | |
1966 | make_model[1024], /* Printer make and model */ | |
1967 | **value; /* Holds the returned data from LDAP */ | |
1968 | int type; /* Printer type */ | |
1969 | int rc; /* LDAP status */ | |
1970 | int limit; /* Size limit */ | |
1971 | LDAPMessage *res, /* LDAP search results */ | |
1972 | *e; /* Current entry from search */ | |
1973 | ||
1974 | ||
1975 | /* | |
1976 | * Search for printers... | |
1977 | */ | |
1978 | ||
1979 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName); | |
1980 | ||
1981 | BrowseLDAPRefresh = time(NULL) + BrowseInterval; | |
1982 | ||
1983 | rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE, | |
1984 | "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res); | |
1985 | if (rc != LDAP_SUCCESS) | |
1986 | { | |
1987 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1988 | "LDAP search returned error %d: %s", rc, | |
1989 | ldap_err2string(rc)); | |
1990 | return; | |
1991 | } | |
1992 | ||
1993 | limit = ldap_count_entries(BrowseLDAPHandle, res); | |
1994 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit); | |
1995 | if (limit < 1) | |
1996 | return; | |
1997 | ||
1998 | /* | |
1999 | * Loop through the available printers... | |
2000 | */ | |
2001 | ||
2002 | if ((e = ldap_first_entry(BrowseLDAPHandle, res)) == NULL) | |
2003 | { | |
2004 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to get LDAP printer entry!"); | |
2005 | return; | |
2006 | } | |
2007 | ||
2008 | while (e) | |
2009 | { | |
2010 | value = ldap_get_values(BrowseLDAPHandle, e, "printerDescription"); | |
2011 | strlcpy(info, *value, sizeof(info)); | |
2012 | ldap_value_free(value); | |
2013 | ||
2014 | value = ldap_get_values(BrowseLDAPHandle, e, "printerLocation"); | |
2015 | strlcpy(location, *value, sizeof(location)); | |
2016 | ldap_value_free(value); | |
2017 | ||
2018 | value = ldap_get_values(BrowseLDAPHandle, e, "printerMakeAndModel"); | |
2019 | strlcpy(make_model, *value, sizeof(make_model)); | |
2020 | ldap_value_free(value); | |
2021 | ||
2022 | value = ldap_get_values(BrowseLDAPHandle, e, "printerType"); | |
2023 | type = atoi(*value); | |
2024 | ldap_value_free(value); | |
2025 | ||
2026 | value = ldap_get_values(BrowseLDAPHandle, e, "printerURI"); | |
2027 | strlcpy(uri, *value, sizeof(uri)); | |
2028 | ldap_value_free(value); | |
2029 | ||
2030 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) | |
2031 | process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE, | |
2032 | location, info, make_model, 0, NULL); | |
2033 | ||
2034 | e = ldap_next_entry(BrowseLDAPHandle, e); | |
2035 | } | |
2036 | } | |
2037 | #endif /* HAVE_OPENLDAP */ | |
2038 | ||
2039 | ||
ef416fc2 | 2040 | /* |
e00b005a | 2041 | * 'cupsdUpdatePolling()' - Read status messages from the poll daemons. |
ef416fc2 | 2042 | */ |
2043 | ||
2044 | void | |
e00b005a | 2045 | cupsdUpdatePolling(void) |
ef416fc2 | 2046 | { |
e00b005a | 2047 | char *ptr, /* Pointer to end of line in buffer */ |
2048 | message[1024]; /* Pointer to message text */ | |
2049 | int loglevel; /* Log level for message */ | |
ef416fc2 | 2050 | |
ef416fc2 | 2051 | |
e00b005a | 2052 | while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel, |
2053 | message, sizeof(message))) != NULL) | |
2054 | if (!strchr(PollStatusBuffer->buffer, '\n')) | |
2055 | break; | |
ef416fc2 | 2056 | |
e00b005a | 2057 | if (ptr == NULL) |
ef416fc2 | 2058 | { |
e00b005a | 2059 | /* |
2060 | * All polling processes have died; stop polling... | |
ef416fc2 | 2061 | */ |
2062 | ||
e00b005a | 2063 | cupsdLogMessage(CUPSD_LOG_ERROR, |
2064 | "cupsdUpdatePolling: all polling processes have exited!"); | |
2065 | cupsdStopPolling(); | |
ef416fc2 | 2066 | } |
ef416fc2 | 2067 | } |
2068 | ||
2069 | ||
e00b005a | 2070 | #ifdef HAVE_LIBSLP |
ef416fc2 | 2071 | /* |
e00b005a | 2072 | * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP. |
ef416fc2 | 2073 | */ |
2074 | ||
2075 | void | |
e00b005a | 2076 | cupsdUpdateSLPBrowse(void) |
ef416fc2 | 2077 | { |
b423cd4c | 2078 | slpsrvurl_t *s, /* Temporary list of service URLs */ |
2079 | *next; /* Next service in list */ | |
2080 | cupsd_printer_t p; /* Printer information */ | |
2081 | const char *uri; /* Pointer to printer URI */ | |
2082 | char host[HTTP_MAX_URI], /* Host portion of URI */ | |
2083 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
ef416fc2 | 2084 | |
ef416fc2 | 2085 | |
e00b005a | 2086 | /* |
2087 | * Reset the refresh time... | |
2088 | */ | |
ef416fc2 | 2089 | |
e00b005a | 2090 | BrowseSLPRefresh = time(NULL) + BrowseInterval; |
2091 | ||
2092 | /* | |
2093 | * Poll for remote printers using SLP... | |
2094 | */ | |
2095 | ||
2096 | s = NULL; | |
2097 | ||
2098 | SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "", | |
2099 | slp_url_callback, &s); | |
2100 | ||
2101 | /* | |
2102 | * Loop through the list of available printers... | |
2103 | */ | |
2104 | ||
2105 | for (; s; s = next) | |
2106 | { | |
2107 | /* | |
2108 | * Save the "next" pointer... | |
2109 | */ | |
2110 | ||
2111 | next = s->next; | |
2112 | ||
2113 | /* | |
2114 | * Load a cupsd_printer_t structure with the SLP service attributes... | |
2115 | */ | |
2116 | ||
2117 | SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p); | |
2118 | ||
2119 | /* | |
2120 | * Process this printer entry... | |
2121 | */ | |
2122 | ||
2123 | uri = s->url + SLP_CUPS_SRVLEN + 1; | |
2124 | ||
2125 | if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6)) | |
2126 | { | |
2127 | /* | |
2128 | * Pull the URI apart to see if this is a local or remote printer... | |
2129 | */ | |
2130 | ||
b423cd4c | 2131 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) |
2132 | process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE, | |
2133 | p.location, p.info, p.make_model, 0, NULL); | |
e00b005a | 2134 | } |
2135 | ||
2136 | /* | |
2137 | * Free this listing... | |
2138 | */ | |
2139 | ||
b423cd4c | 2140 | cupsdClearString(&p.info); |
2141 | cupsdClearString(&p.location); | |
2142 | cupsdClearString(&p.make_model); | |
2143 | ||
e00b005a | 2144 | free(s); |
2145 | } | |
e00b005a | 2146 | } |
2147 | #endif /* HAVE_LIBSLP */ | |
2148 | ||
2149 | ||
2150 | /* | |
2151 | * 'dequote()' - Remote quotes from a string. | |
2152 | */ | |
2153 | ||
2154 | static char * /* O - Dequoted string */ | |
2155 | dequote(char *d, /* I - Destination string */ | |
2156 | const char *s, /* I - Source string */ | |
2157 | int dlen) /* I - Destination length */ | |
2158 | { | |
2159 | char *dptr; /* Pointer into destination */ | |
2160 | ||
2161 | ||
2162 | if (s) | |
2163 | { | |
2164 | for (dptr = d, dlen --; *s && dlen > 0; s ++) | |
2165 | if (*s != '\"') | |
2166 | { | |
2167 | *dptr++ = *s; | |
2168 | dlen --; | |
2169 | } | |
2170 | ||
2171 | *dptr = '\0'; | |
ef416fc2 | 2172 | } |
e00b005a | 2173 | else |
2174 | *d = '\0'; | |
ef416fc2 | 2175 | |
e00b005a | 2176 | return (d); |
ef416fc2 | 2177 | } |
2178 | ||
2179 | ||
b423cd4c | 2180 | /* |
2181 | * 'is_local_queue()' - Determine whether the URI points at a local queue. | |
2182 | */ | |
2183 | ||
2184 | static int /* O - 1 = local, 0 = remote, -1 = bad URI */ | |
2185 | is_local_queue(const char *uri, /* I - Printer URI */ | |
2186 | char *host, /* O - Host string */ | |
2187 | int hostlen, /* I - Length of host buffer */ | |
2188 | char *resource, /* O - Resource string */ | |
2189 | int resourcelen) /* I - Length of resource buffer */ | |
2190 | { | |
2191 | char scheme[32], /* Scheme portion of URI */ | |
2192 | username[HTTP_MAX_URI]; /* Username portion of URI */ | |
2193 | int port; /* Port portion of URI */ | |
2194 | cupsd_netif_t *iface; /* Network interface */ | |
2195 | ||
2196 | ||
2197 | /* | |
2198 | * Pull the URI apart to see if this is a local or remote printer... | |
2199 | */ | |
2200 | ||
2201 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), | |
2202 | username, sizeof(username), host, hostlen, &port, | |
2203 | resource, resourcelen) < HTTP_URI_OK) | |
2204 | return (-1); | |
2205 | ||
2206 | DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName)); | |
2207 | ||
2208 | /* | |
2209 | * Check for local server addresses... | |
2210 | */ | |
2211 | ||
2212 | if (!strcasecmp(host, ServerName) && port == LocalPort) | |
2213 | return (1); | |
2214 | ||
2215 | cupsdNetIFUpdate(); | |
2216 | ||
2217 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); | |
2218 | iface; | |
2219 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
2220 | if (!strcasecmp(host, iface->hostname) && port == iface->port) | |
2221 | return (1); | |
2222 | ||
2223 | /* | |
2224 | * If we get here, the printer is remote... | |
2225 | */ | |
2226 | ||
2227 | return (0); | |
2228 | } | |
2229 | ||
2230 | ||
ef416fc2 | 2231 | /* |
e00b005a | 2232 | * 'process_browse_data()' - Process new browse data. |
ef416fc2 | 2233 | */ |
2234 | ||
e00b005a | 2235 | static void |
2236 | process_browse_data( | |
2237 | const char *uri, /* I - URI of printer/class */ | |
b423cd4c | 2238 | const char *host, /* I - Hostname */ |
2239 | const char *resource, /* I - Resource path */ | |
e00b005a | 2240 | cups_ptype_t type, /* I - Printer type */ |
2241 | ipp_pstate_t state, /* I - Printer state */ | |
2242 | const char *location, /* I - Printer location */ | |
2243 | const char *info, /* I - Printer information */ | |
2244 | const char *make_model, /* I - Printer make and model */ | |
2245 | int num_attrs, /* I - Number of attributes */ | |
2246 | cups_option_t *attrs) /* I - Attributes */ | |
ef416fc2 | 2247 | { |
b423cd4c | 2248 | int i; /* Looping var */ |
e00b005a | 2249 | int update; /* Update printer attributes? */ |
2250 | char finaluri[HTTP_MAX_URI], /* Final URI for printer */ | |
b423cd4c | 2251 | name[IPP_MAX_NAME], /* Name of printer */ |
2252 | newname[IPP_MAX_NAME], /* New name of printer */ | |
e00b005a | 2253 | *hptr, /* Pointer into hostname */ |
2254 | *sptr; /* Pointer into ServerName */ | |
2255 | char local_make_model[IPP_MAX_NAME]; | |
2256 | /* Local make and model */ | |
2257 | cupsd_printer_t *p; /* Printer information */ | |
b423cd4c | 2258 | const char *ipp_options, /* ipp-options value */ |
2259 | *lease_duration; /* lease-duration value */ | |
ef416fc2 | 2260 | |
e00b005a | 2261 | |
2262 | /* | |
2263 | * Determine if the URI contains any illegal characters in it... | |
2264 | */ | |
2265 | ||
2266 | if (strncmp(uri, "ipp://", 6) || !host[0] || | |
2267 | (strncmp(resource, "/printers/", 10) && | |
2268 | strncmp(resource, "/classes/", 9))) | |
ef416fc2 | 2269 | { |
e00b005a | 2270 | cupsdLogMessage(CUPSD_LOG_ERROR, |
2271 | "process_browse_data: Bad printer URI in browse data: %s", | |
2272 | uri); | |
2273 | return; | |
2274 | } | |
ef416fc2 | 2275 | |
e00b005a | 2276 | if (strchr(resource, '?') || |
2277 | (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) || | |
2278 | (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/'))) | |
2279 | { | |
2280 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2281 | "process_browse_data: Bad resource in browse data: %s", | |
2282 | resource); | |
2283 | return; | |
2284 | } | |
2285 | ||
2286 | /* | |
2287 | * OK, this isn't a local printer; add any remote options... | |
2288 | */ | |
2289 | ||
2290 | ipp_options = cupsGetOption("ipp-options", num_attrs, attrs); | |
2291 | ||
2292 | if (BrowseRemoteOptions) | |
2293 | { | |
2294 | if (BrowseRemoteOptions[0] == '?') | |
ef416fc2 | 2295 | { |
e00b005a | 2296 | /* |
2297 | * Override server-supplied options... | |
2298 | */ | |
ef416fc2 | 2299 | |
e00b005a | 2300 | snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions); |
2301 | } | |
2302 | else if (ipp_options) | |
2303 | { | |
2304 | /* | |
2305 | * Combine the server and local options... | |
2306 | */ | |
2307 | ||
2308 | snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options, | |
2309 | BrowseRemoteOptions); | |
2310 | } | |
2311 | else | |
2312 | { | |
2313 | /* | |
2314 | * Just use the local options... | |
2315 | */ | |
2316 | ||
2317 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions); | |
ef416fc2 | 2318 | } |
2319 | ||
e00b005a | 2320 | uri = finaluri; |
ef416fc2 | 2321 | } |
e00b005a | 2322 | else if (ipp_options) |
2323 | { | |
2324 | /* | |
2325 | * Just use the server-supplied options... | |
2326 | */ | |
ef416fc2 | 2327 | |
e00b005a | 2328 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options); |
2329 | uri = finaluri; | |
2330 | } | |
ef416fc2 | 2331 | |
09ec0018 | 2332 | /* |
e00b005a | 2333 | * See if we already have it listed in the Printers list, and add it if not... |
09ec0018 | 2334 | */ |
2335 | ||
e00b005a | 2336 | type |= CUPS_PRINTER_REMOTE; |
2337 | type &= ~CUPS_PRINTER_IMPLICIT; | |
2338 | update = 0; | |
2339 | hptr = strchr(host, '.'); | |
2340 | sptr = strchr(ServerName, '.'); | |
09ec0018 | 2341 | |
e00b005a | 2342 | if (sptr != NULL && hptr != NULL) |
2343 | { | |
2344 | /* | |
2345 | * Strip the common domain name components... | |
2346 | */ | |
ef416fc2 | 2347 | |
e00b005a | 2348 | while (hptr != NULL) |
2349 | { | |
2350 | if (!strcasecmp(hptr, sptr)) | |
2351 | { | |
2352 | *hptr = '\0'; | |
2353 | break; | |
2354 | } | |
2355 | else | |
2356 | hptr = strchr(hptr + 1, '.'); | |
2357 | } | |
2358 | } | |
2359 | ||
2360 | if (type & CUPS_PRINTER_CLASS) | |
ef416fc2 | 2361 | { |
e00b005a | 2362 | /* |
2363 | * Remote destination is a class... | |
2364 | */ | |
2365 | ||
2366 | if (!strncmp(resource, "/classes/", 9)) | |
2367 | snprintf(name, sizeof(name), "%s@%s", resource + 9, host); | |
2368 | else | |
2369 | return; | |
2370 | ||
2371 | if ((p = cupsdFindClass(name)) == NULL && BrowseShortNames) | |
2372 | { | |
2373 | if ((p = cupsdFindClass(resource + 9)) != NULL) | |
2374 | { | |
2375 | if (p->hostname && strcasecmp(p->hostname, host)) | |
2376 | { | |
2377 | /* | |
2378 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
2379 | * add it to the other class and then find a class using the full host | |
2380 | * name... | |
2381 | */ | |
2382 | ||
2383 | if (p->type & CUPS_PRINTER_REMOTE) | |
2384 | { | |
2385 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2386 | "Renamed remote class \"%s\" to \"%s@%s\"...", | |
2387 | p->name, p->name, p->hostname); | |
2388 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
2389 | "Class \'%s\' deleted by directory services.", | |
2390 | p->name); | |
2391 | ||
b423cd4c | 2392 | snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname); |
2393 | cupsdRenamePrinter(p, newname); | |
e00b005a | 2394 | |
2395 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, | |
2396 | "Class \'%s\' added by directory services.", | |
2397 | p->name); | |
2398 | } | |
2399 | ||
2400 | p = NULL; | |
2401 | } | |
2402 | else if (!p->hostname) | |
2403 | { | |
2404 | /* | |
2405 | * Hostname not set, so this must be a cached remote printer | |
2406 | * that was created for a pending print job... | |
2407 | */ | |
2408 | ||
2409 | cupsdSetString(&p->hostname, host); | |
2410 | cupsdSetString(&p->uri, uri); | |
2411 | cupsdSetString(&p->device_uri, uri); | |
2412 | update = 1; | |
2413 | } | |
2414 | } | |
2415 | else | |
2416 | { | |
2417 | /* | |
2418 | * Use the short name for this shared class. | |
2419 | */ | |
2420 | ||
2421 | strlcpy(name, resource + 9, sizeof(name)); | |
2422 | } | |
2423 | } | |
2424 | else if (p && !p->hostname) | |
2425 | { | |
2426 | /* | |
2427 | * Hostname not set, so this must be a cached remote printer | |
2428 | * that was created for a pending print job... | |
2429 | */ | |
2430 | ||
2431 | cupsdSetString(&p->hostname, host); | |
2432 | cupsdSetString(&p->uri, uri); | |
2433 | cupsdSetString(&p->device_uri, uri); | |
2434 | update = 1; | |
2435 | } | |
2436 | ||
2437 | if (!p) | |
2438 | { | |
2439 | /* | |
2440 | * Class doesn't exist; add it... | |
2441 | */ | |
2442 | ||
2443 | p = cupsdAddClass(name); | |
2444 | ||
2445 | cupsdLogMessage(CUPSD_LOG_INFO, "Added remote class \"%s\"...", name); | |
2446 | ||
2447 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, | |
2448 | "Class \'%s\' added by directory services.", name); | |
2449 | ||
2450 | /* | |
2451 | * Force the URI to point to the real server... | |
2452 | */ | |
2453 | ||
2454 | p->type = type & ~CUPS_PRINTER_REJECTING; | |
2455 | p->accepting = 1; | |
2456 | cupsdSetString(&p->uri, uri); | |
2457 | cupsdSetString(&p->device_uri, uri); | |
2458 | cupsdSetString(&p->hostname, host); | |
2459 | ||
2460 | update = 1; | |
2461 | } | |
ef416fc2 | 2462 | } |
2463 | else | |
ef416fc2 | 2464 | { |
e00b005a | 2465 | /* |
2466 | * Remote destination is a printer... | |
2467 | */ | |
2468 | ||
2469 | if (!strncmp(resource, "/printers/", 10)) | |
2470 | snprintf(name, sizeof(name), "%s@%s", resource + 10, host); | |
2471 | else | |
2472 | return; | |
2473 | ||
2474 | if ((p = cupsdFindPrinter(name)) == NULL && BrowseShortNames) | |
2475 | { | |
2476 | if ((p = cupsdFindPrinter(resource + 10)) != NULL) | |
2477 | { | |
2478 | if (p->hostname && strcasecmp(p->hostname, host)) | |
2479 | { | |
2480 | /* | |
2481 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
2482 | * add it to the other printer and then find a printer using the full host | |
2483 | * name... | |
2484 | */ | |
ef416fc2 | 2485 | |
e00b005a | 2486 | if (p->type & CUPS_PRINTER_REMOTE) |
2487 | { | |
2488 | cupsdLogMessage(CUPSD_LOG_INFO, | |
2489 | "Renamed remote printer \"%s\" to \"%s@%s\"...", | |
2490 | p->name, p->name, p->hostname); | |
2491 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
2492 | "Printer \'%s\' deleted by directory services.", | |
2493 | p->name); | |
ef416fc2 | 2494 | |
b423cd4c | 2495 | snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname); |
2496 | cupsdRenamePrinter(p, newname); | |
ef416fc2 | 2497 | |
e00b005a | 2498 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
2499 | "Printer \'%s\' added by directory services.", | |
2500 | p->name); | |
2501 | } | |
ef416fc2 | 2502 | |
e00b005a | 2503 | p = NULL; |
2504 | } | |
2505 | else if (!p->hostname) | |
2506 | { | |
2507 | /* | |
2508 | * Hostname not set, so this must be a cached remote printer | |
2509 | * that was created for a pending print job... | |
2510 | */ | |
2511 | ||
2512 | cupsdSetString(&p->hostname, host); | |
2513 | cupsdSetString(&p->uri, uri); | |
2514 | cupsdSetString(&p->device_uri, uri); | |
2515 | update = 1; | |
2516 | } | |
2517 | } | |
2518 | else | |
2519 | { | |
2520 | /* | |
2521 | * Use the short name for this shared printer. | |
2522 | */ | |
2523 | ||
2524 | strlcpy(name, resource + 10, sizeof(name)); | |
2525 | } | |
2526 | } | |
2527 | else if (p && !p->hostname) | |
ef416fc2 | 2528 | { |
2529 | /* | |
e00b005a | 2530 | * Hostname not set, so this must be a cached remote printer |
2531 | * that was created for a pending print job... | |
ef416fc2 | 2532 | */ |
2533 | ||
e00b005a | 2534 | cupsdSetString(&p->hostname, host); |
2535 | cupsdSetString(&p->uri, uri); | |
2536 | cupsdSetString(&p->device_uri, uri); | |
2537 | update = 1; | |
ef416fc2 | 2538 | } |
e00b005a | 2539 | |
2540 | if (!p) | |
ef416fc2 | 2541 | { |
2542 | /* | |
e00b005a | 2543 | * Printer doesn't exist; add it... |
ef416fc2 | 2544 | */ |
2545 | ||
e00b005a | 2546 | p = cupsdAddPrinter(name); |
ef416fc2 | 2547 | |
e00b005a | 2548 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
2549 | "Printer \'%s\' added by directory services.", name); | |
ef416fc2 | 2550 | |
e00b005a | 2551 | cupsdLogMessage(CUPSD_LOG_INFO, "Added remote printer \"%s\"...", name); |
ef416fc2 | 2552 | |
e00b005a | 2553 | /* |
2554 | * Force the URI to point to the real server... | |
2555 | */ | |
ef416fc2 | 2556 | |
e00b005a | 2557 | p->type = type & ~CUPS_PRINTER_REJECTING; |
2558 | p->accepting = 1; | |
2559 | cupsdSetString(&p->hostname, host); | |
2560 | cupsdSetString(&p->uri, uri); | |
2561 | cupsdSetString(&p->device_uri, uri); | |
ef416fc2 | 2562 | |
e00b005a | 2563 | update = 1; |
ef416fc2 | 2564 | } |
2565 | } | |
ef416fc2 | 2566 | |
2567 | /* | |
e00b005a | 2568 | * Update the state... |
ef416fc2 | 2569 | */ |
2570 | ||
e00b005a | 2571 | p->state = state; |
2572 | p->browse_time = time(NULL); | |
ef416fc2 | 2573 | |
b423cd4c | 2574 | if ((lease_duration = cupsGetOption("lease-duration", num_attrs, |
2575 | attrs)) != NULL) | |
2576 | { | |
2577 | /* | |
2578 | * Grab the lease-duration for the browse data; anything less then 1 | |
2579 | * second or more than 1 week gets the default BrowseTimeout... | |
2580 | */ | |
2581 | ||
2582 | i = atoi(lease_duration); | |
2583 | if (i < 1 || i > 604800) | |
2584 | i = BrowseTimeout; | |
2585 | ||
2586 | p->browse_expire = p->browse_time + i; | |
2587 | } | |
2588 | else | |
2589 | p->browse_expire = p->browse_time + BrowseTimeout; | |
2590 | ||
e00b005a | 2591 | if (type & CUPS_PRINTER_REJECTING) |
ef416fc2 | 2592 | { |
e00b005a | 2593 | type &= ~CUPS_PRINTER_REJECTING; |
ef416fc2 | 2594 | |
e00b005a | 2595 | if (p->accepting) |
ef416fc2 | 2596 | { |
e00b005a | 2597 | update = 1; |
2598 | p->accepting = 0; | |
ef416fc2 | 2599 | } |
2600 | } | |
e00b005a | 2601 | else if (!p->accepting) |
2602 | { | |
2603 | update = 1; | |
2604 | p->accepting = 1; | |
2605 | } | |
ef416fc2 | 2606 | |
e00b005a | 2607 | if (p->type != type) |
2608 | { | |
2609 | p->type = type; | |
2610 | update = 1; | |
2611 | } | |
ef416fc2 | 2612 | |
e00b005a | 2613 | if (location && (!p->location || strcmp(p->location, location))) |
2614 | { | |
2615 | cupsdSetString(&p->location, location); | |
2616 | update = 1; | |
2617 | } | |
ef416fc2 | 2618 | |
e00b005a | 2619 | if (info && (!p->info || strcmp(p->info, info))) |
2620 | { | |
2621 | cupsdSetString(&p->info, info); | |
2622 | update = 1; | |
2623 | } | |
ef416fc2 | 2624 | |
e00b005a | 2625 | if (!make_model || !make_model[0]) |
2626 | { | |
2627 | if (type & CUPS_PRINTER_CLASS) | |
2628 | snprintf(local_make_model, sizeof(local_make_model), | |
2629 | "Remote Class on %s", host); | |
2630 | else | |
2631 | snprintf(local_make_model, sizeof(local_make_model), | |
2632 | "Remote Printer on %s", host); | |
2633 | } | |
2634 | else | |
2635 | snprintf(local_make_model, sizeof(local_make_model), | |
2636 | "%s on %s", make_model, host); | |
ef416fc2 | 2637 | |
e00b005a | 2638 | if (!p->make_model || strcmp(p->make_model, local_make_model)) |
ef416fc2 | 2639 | { |
e00b005a | 2640 | cupsdSetString(&p->make_model, local_make_model); |
2641 | update = 1; | |
ef416fc2 | 2642 | } |
2643 | ||
b423cd4c | 2644 | if (p->num_options) |
2645 | { | |
2646 | if (!update && !(type & CUPS_PRINTER_DELETE)) | |
2647 | { | |
2648 | /* | |
2649 | * See if we need to update the attributes... | |
2650 | */ | |
2651 | ||
2652 | if (p->num_options != num_attrs) | |
2653 | update = 1; | |
2654 | else | |
2655 | { | |
2656 | for (i = 0; i < num_attrs; i ++) | |
2657 | if (strcmp(attrs[i].name, p->options[i].name) || | |
2658 | (!attrs[i].value != !p->options[i].value) || | |
2659 | (attrs[i].value && strcmp(attrs[i].value, p->options[i].value))) | |
2660 | { | |
2661 | update = 1; | |
2662 | break; | |
2663 | } | |
2664 | } | |
2665 | } | |
2666 | ||
2667 | /* | |
2668 | * Free the old options... | |
2669 | */ | |
2670 | ||
2671 | cupsFreeOptions(p->num_options, p->options); | |
2672 | } | |
2673 | ||
2674 | p->num_options = num_attrs; | |
2675 | p->options = attrs; | |
2676 | ||
e00b005a | 2677 | if (type & CUPS_PRINTER_DELETE) |
2678 | { | |
2679 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
2680 | "%s \'%s\' deleted by directory services.", | |
2681 | (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name); | |
ef416fc2 | 2682 | |
e00b005a | 2683 | cupsdExpireSubscriptions(p, NULL); |
2684 | ||
2685 | cupsdDeletePrinter(p, 1); | |
2686 | cupsdUpdateImplicitClasses(); | |
2687 | } | |
2688 | else if (update) | |
2689 | { | |
2690 | cupsdSetPrinterAttrs(p); | |
2691 | cupsdUpdateImplicitClasses(); | |
2692 | } | |
ef416fc2 | 2693 | |
2694 | /* | |
b423cd4c | 2695 | * See if we have a default printer... If not, make the first network |
2696 | * default printer the default. | |
ef416fc2 | 2697 | */ |
2698 | ||
e00b005a | 2699 | if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault) |
2700 | { | |
2701 | /* | |
2702 | * Find the first network default printer and use it... | |
2703 | */ | |
2704 | ||
2705 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
2706 | p; | |
2707 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
2708 | if (p->type & CUPS_PRINTER_DEFAULT) | |
ef416fc2 | 2709 | { |
e00b005a | 2710 | DefaultPrinter = p; |
2711 | break; | |
ef416fc2 | 2712 | } |
e00b005a | 2713 | } |
ef416fc2 | 2714 | |
2715 | /* | |
e00b005a | 2716 | * Do auto-classing if needed... |
ef416fc2 | 2717 | */ |
2718 | ||
e00b005a | 2719 | process_implicit_classes(); |
2720 | ||
2721 | /* | |
2722 | * Update the printcap file... | |
2723 | */ | |
2724 | ||
2725 | cupsdWritePrintcap(); | |
ef416fc2 | 2726 | } |
2727 | ||
2728 | ||
2729 | /* | |
e00b005a | 2730 | * 'process_implicit_classes()' - Create/update implicit classes as needed. |
ef416fc2 | 2731 | */ |
2732 | ||
e00b005a | 2733 | static void |
2734 | process_implicit_classes(void) | |
ef416fc2 | 2735 | { |
e00b005a | 2736 | int i; /* Looping var */ |
2737 | int update; /* Update printer attributes? */ | |
2738 | char name[IPP_MAX_NAME], /* Name of printer */ | |
2739 | *hptr; /* Pointer into hostname */ | |
2740 | cupsd_printer_t *p, /* Printer information */ | |
2741 | *pclass, /* Printer class */ | |
2742 | *first; /* First printer in class */ | |
2743 | int offset, /* Offset of name */ | |
2744 | len; /* Length of name */ | |
ef416fc2 | 2745 | |
2746 | ||
e00b005a | 2747 | if (!ImplicitClasses || !Printers) |
2748 | return; | |
ef416fc2 | 2749 | |
e00b005a | 2750 | /* |
2751 | * Loop through all available printers and create classes as needed... | |
2752 | */ | |
2753 | ||
2754 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0, | |
2755 | update = 0, pclass = NULL, first = NULL; | |
2756 | p != NULL; | |
2757 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 2758 | { |
2759 | /* | |
e00b005a | 2760 | * Skip implicit classes... |
ef416fc2 | 2761 | */ |
2762 | ||
e00b005a | 2763 | if (p->type & CUPS_PRINTER_IMPLICIT) |
2764 | { | |
2765 | len = 0; | |
2766 | continue; | |
2767 | } | |
ef416fc2 | 2768 | |
e00b005a | 2769 | /* |
2770 | * If len == 0, get the length of this printer name up to the "@" | |
2771 | * sign (if any). | |
2772 | */ | |
ef416fc2 | 2773 | |
e00b005a | 2774 | cupsArraySave(Printers); |
ef416fc2 | 2775 | |
e00b005a | 2776 | if (len > 0 && |
2777 | !strncasecmp(p->name, name + offset, len) && | |
2778 | (p->name[len] == '\0' || p->name[len] == '@')) | |
2779 | { | |
2780 | /* | |
2781 | * We have more than one printer with the same name; see if | |
2782 | * we have a class, and if this printer is a member... | |
2783 | */ | |
ef416fc2 | 2784 | |
e00b005a | 2785 | if (pclass && strcasecmp(pclass->name, name)) |
2786 | { | |
2787 | if (update) | |
2788 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 2789 | |
e00b005a | 2790 | update = 0; |
2791 | pclass = NULL; | |
2792 | } | |
ef416fc2 | 2793 | |
e00b005a | 2794 | if (!pclass && (pclass = cupsdFindDest(name)) == NULL) |
2795 | { | |
2796 | /* | |
2797 | * Need to add the class... | |
2798 | */ | |
ef416fc2 | 2799 | |
e00b005a | 2800 | pclass = cupsdAddPrinter(name); |
2801 | cupsArrayAdd(ImplicitPrinters, pclass); | |
ef416fc2 | 2802 | |
e00b005a | 2803 | pclass->type |= CUPS_PRINTER_IMPLICIT; |
2804 | pclass->accepting = 1; | |
2805 | pclass->state = IPP_PRINTER_IDLE; | |
ef416fc2 | 2806 | |
e00b005a | 2807 | cupsdSetString(&pclass->location, p->location); |
2808 | cupsdSetString(&pclass->info, p->info); | |
ef416fc2 | 2809 | |
e00b005a | 2810 | update = 1; |
ef416fc2 | 2811 | |
e00b005a | 2812 | cupsdLogMessage(CUPSD_LOG_INFO, "Added implicit class \"%s\"...", |
2813 | name); | |
b423cd4c | 2814 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
2815 | "Implicit class \'%s\' added by directory services.", | |
2816 | name); | |
e00b005a | 2817 | } |
ef416fc2 | 2818 | |
e00b005a | 2819 | if (first != NULL) |
2820 | { | |
2821 | for (i = 0; i < pclass->num_printers; i ++) | |
2822 | if (pclass->printers[i] == first) | |
2823 | break; | |
ef416fc2 | 2824 | |
e00b005a | 2825 | if (i >= pclass->num_printers) |
2826 | { | |
2827 | first->in_implicit_class = 1; | |
2828 | cupsdAddPrinterToClass(pclass, first); | |
2829 | } | |
ef416fc2 | 2830 | |
e00b005a | 2831 | first = NULL; |
2832 | } | |
ef416fc2 | 2833 | |
e00b005a | 2834 | for (i = 0; i < pclass->num_printers; i ++) |
2835 | if (pclass->printers[i] == p) | |
2836 | break; | |
ef416fc2 | 2837 | |
e00b005a | 2838 | if (i >= pclass->num_printers) |
2839 | { | |
2840 | p->in_implicit_class = 1; | |
2841 | cupsdAddPrinterToClass(pclass, p); | |
2842 | update = 1; | |
2843 | } | |
2844 | } | |
2845 | else | |
ef416fc2 | 2846 | { |
2847 | /* | |
e00b005a | 2848 | * First time around; just get name length and mark it as first |
2849 | * in the list... | |
ef416fc2 | 2850 | */ |
2851 | ||
e00b005a | 2852 | if ((hptr = strchr(p->name, '@')) != NULL) |
2853 | len = hptr - p->name; | |
2854 | else | |
2855 | len = strlen(p->name); | |
ef416fc2 | 2856 | |
e00b005a | 2857 | strncpy(name, p->name, len); |
2858 | name[len] = '\0'; | |
2859 | offset = 0; | |
ef416fc2 | 2860 | |
e00b005a | 2861 | if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL && |
2862 | !(first->type & CUPS_PRINTER_IMPLICIT)) | |
2863 | { | |
2864 | /* | |
2865 | * Can't use same name as a local printer; add "Any" to the | |
2866 | * front of the name, unless we have explicitly disabled | |
2867 | * the "ImplicitAnyClasses"... | |
2868 | */ | |
ef416fc2 | 2869 | |
e00b005a | 2870 | if (ImplicitAnyClasses && len < (sizeof(name) - 4)) |
2871 | { | |
2872 | /* | |
2873 | * Add "Any" to the class name... | |
2874 | */ | |
2875 | ||
2876 | strcpy(name, "Any"); | |
2877 | strncpy(name + 3, p->name, len); | |
2878 | name[len + 3] = '\0'; | |
2879 | offset = 3; | |
2880 | } | |
2881 | else | |
2882 | { | |
2883 | /* | |
2884 | * Don't create an implicit class if we have a local printer | |
2885 | * with the same name... | |
2886 | */ | |
2887 | ||
2888 | len = 0; | |
2889 | cupsArrayRestore(Printers); | |
2890 | continue; | |
2891 | } | |
2892 | } | |
2893 | ||
2894 | first = p; | |
ef416fc2 | 2895 | } |
2896 | ||
e00b005a | 2897 | cupsArrayRestore(Printers); |
2898 | } | |
ef416fc2 | 2899 | |
e00b005a | 2900 | /* |
2901 | * Update the last printer class as needed... | |
2902 | */ | |
ef416fc2 | 2903 | |
e00b005a | 2904 | if (pclass && update) |
2905 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 2906 | } |
2907 | ||
2908 | ||
e00b005a | 2909 | #ifdef HAVE_LIBSLP |
ef416fc2 | 2910 | /* |
2911 | * 'slp_attr_callback()' - SLP attribute callback | |
2912 | */ | |
2913 | ||
2914 | static SLPBoolean /* O - SLP_TRUE for success */ | |
2915 | slp_attr_callback( | |
2916 | SLPHandle hslp, /* I - SLP handle */ | |
2917 | const char *attrlist, /* I - Attribute list */ | |
2918 | SLPError errcode, /* I - Parsing status for this attr */ | |
2919 | void *cookie) /* I - Current printer */ | |
2920 | { | |
b423cd4c | 2921 | char *tmp = 0; /* Temporary string */ |
ef416fc2 | 2922 | cupsd_printer_t *p = (cupsd_printer_t*)cookie; |
b423cd4c | 2923 | /* Current printer */ |
ef416fc2 | 2924 | |
2925 | ||
b423cd4c | 2926 | (void)hslp; /* anti-compiler-warning-code */ |
ef416fc2 | 2927 | |
2928 | /* | |
2929 | * Bail if there was an error | |
2930 | */ | |
2931 | ||
2932 | if (errcode != SLP_OK) | |
2933 | return (SLP_TRUE); | |
2934 | ||
2935 | /* | |
2936 | * Parse the attrlist to obtain things needed to build CUPS browse packet | |
2937 | */ | |
2938 | ||
2939 | memset(p, 0, sizeof(cupsd_printer_t)); | |
2940 | ||
ef416fc2 | 2941 | if (slp_get_attr(attrlist, "(printer-location=", &(p->location))) |
2942 | return (SLP_FALSE); | |
2943 | if (slp_get_attr(attrlist, "(printer-info=", &(p->info))) | |
2944 | return (SLP_FALSE); | |
2945 | if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model))) | |
2946 | return (SLP_FALSE); | |
b423cd4c | 2947 | if (!slp_get_attr(attrlist, "(printer-type=", &tmp)) |
2948 | p->type = atoi(tmp); | |
2949 | else | |
2950 | p->type = CUPS_PRINTER_REMOTE; | |
ef416fc2 | 2951 | |
2952 | cupsdClearString(&tmp); | |
2953 | ||
2954 | return (SLP_TRUE); | |
2955 | } | |
2956 | ||
2957 | ||
2958 | /* | |
2959 | * 'slp_dereg_printer()' - SLPDereg() the specified printer | |
2960 | */ | |
2961 | ||
2962 | static void | |
2963 | slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */ | |
2964 | { | |
2965 | char srvurl[HTTP_MAX_URI]; /* Printer service URI */ | |
2966 | ||
2967 | ||
2968 | cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name); | |
2969 | ||
2970 | if (!(p->type & CUPS_PRINTER_REMOTE)) | |
2971 | { | |
2972 | /* | |
2973 | * Make the SLP service URL that conforms to the IANA | |
2974 | * 'printer:' template. | |
2975 | */ | |
2976 | ||
2977 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); | |
2978 | ||
2979 | /* | |
2980 | * Deregister the printer... | |
2981 | */ | |
2982 | ||
2983 | SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0); | |
2984 | } | |
2985 | } | |
2986 | ||
2987 | ||
2988 | /* | |
2989 | * 'slp_get_attr()' - Get an attribute from an SLP registration. | |
2990 | */ | |
2991 | ||
2992 | static int /* O - 0 on success */ | |
2993 | slp_get_attr(const char *attrlist, /* I - Attribute list string */ | |
2994 | const char *tag, /* I - Name of attribute */ | |
2995 | char **valbuf) /* O - Value */ | |
2996 | { | |
2997 | char *ptr1, /* Pointer into string */ | |
2998 | *ptr2; /* ... */ | |
2999 | ||
3000 | ||
3001 | cupsdClearString(valbuf); | |
3002 | ||
3003 | if ((ptr1 = strstr(attrlist, tag)) != NULL) | |
3004 | { | |
3005 | ptr1 += strlen(tag); | |
3006 | ||
3007 | if ((ptr2 = strchr(ptr1,')')) != NULL) | |
3008 | { | |
3009 | /* | |
3010 | * Copy the value... | |
3011 | */ | |
3012 | ||
3013 | *valbuf = calloc(ptr2 - ptr1 + 1, 1); | |
3014 | strncpy(*valbuf, ptr1, ptr2 - ptr1); | |
3015 | ||
3016 | /* | |
3017 | * Dequote the value... | |
3018 | */ | |
3019 | ||
3020 | for (ptr1 = *valbuf; *ptr1; ptr1 ++) | |
3021 | if (*ptr1 == '\\' && ptr1[1]) | |
3022 | _cups_strcpy(ptr1, ptr1 + 1); | |
3023 | ||
3024 | return (0); | |
3025 | } | |
3026 | } | |
3027 | ||
3028 | return (-1); | |
3029 | } | |
3030 | ||
3031 | ||
3032 | /* | |
3033 | * 'slp_reg_callback()' - Empty SLPRegReport. | |
3034 | */ | |
3035 | ||
3036 | static void | |
3037 | slp_reg_callback(SLPHandle hslp, /* I - SLP handle */ | |
3038 | SLPError errcode, /* I - Error code, if any */ | |
3039 | void *cookie) /* I - App data */ | |
3040 | { | |
3041 | (void)hslp; | |
3042 | (void)errcode; | |
3043 | (void)cookie; | |
3044 | ||
3045 | return; | |
3046 | } | |
3047 | ||
3048 | ||
3049 | /* | |
3050 | * 'slp_url_callback()' - SLP service url callback | |
3051 | */ | |
3052 | ||
3053 | static SLPBoolean /* O - TRUE = OK, FALSE = error */ | |
3054 | slp_url_callback( | |
3055 | SLPHandle hslp, /* I - SLP handle */ | |
3056 | const char *srvurl, /* I - URL of service */ | |
3057 | unsigned short lifetime, /* I - Life of service */ | |
3058 | SLPError errcode, /* I - Existing error code */ | |
3059 | void *cookie) /* I - Pointer to service list */ | |
3060 | { | |
3061 | slpsrvurl_t *s, /* New service entry */ | |
3062 | **head; /* Pointer to head of entry */ | |
3063 | ||
3064 | ||
3065 | /* | |
3066 | * Let the compiler know we won't be using these vars... | |
3067 | */ | |
3068 | ||
3069 | (void)hslp; | |
3070 | (void)lifetime; | |
3071 | ||
3072 | /* | |
3073 | * Bail if there was an error | |
3074 | */ | |
3075 | ||
3076 | if (errcode != SLP_OK) | |
3077 | return (SLP_TRUE); | |
3078 | ||
3079 | /* | |
3080 | * Grab the head of the list... | |
3081 | */ | |
3082 | ||
3083 | head = (slpsrvurl_t**)cookie; | |
3084 | ||
3085 | /* | |
3086 | * Allocate a *temporary* slpsrvurl_t to hold this entry. | |
3087 | */ | |
3088 | ||
3089 | if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL) | |
3090 | return (SLP_FALSE); | |
3091 | ||
3092 | /* | |
3093 | * Copy the SLP service URL... | |
3094 | */ | |
3095 | ||
3096 | strlcpy(s->url, srvurl, sizeof(s->url)); | |
3097 | ||
3098 | /* | |
3099 | * Link the SLP service URL into the head of the list | |
3100 | */ | |
3101 | ||
3102 | if (*head) | |
3103 | s->next = *head; | |
3104 | ||
3105 | *head = s; | |
3106 | ||
3107 | return (SLP_TRUE); | |
3108 | } | |
3109 | #endif /* HAVE_LIBSLP */ | |
3110 | ||
3111 | ||
3112 | /* | |
b423cd4c | 3113 | * End of "$Id: dirsvc.c 5178 2006-02-26 00:24:23Z mike $". |
ef416fc2 | 3114 | */ |