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