]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
2e4ff8af | 2 | * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $" |
ef416fc2 | 3 | * |
4 | * Directory services routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
080811b1 | 6 | * Copyright 2007-2008 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
9 | * These coded instructions, statements, and computer programs are the | |
bc44d920 | 10 | * property of Apple Inc. and are protected by Federal copyright |
11 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
12 | * which should have been included with this file. If this file is | |
13 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 14 | * |
15 | * Contents: | |
16 | * | |
080811b1 MS |
17 | * cupsdDeregisterPrinter() - Stop sending broadcast information for a local |
18 | * printer and remove any pending references to | |
19 | * remote printers. | |
20 | * cupsdLoadRemoteCache() - Load the remote printer cache. | |
21 | * cupsdRegisterPrinter() - Start sending broadcast information for a | |
22 | * printer or update the broadcast contents. | |
23 | * cupsdRestartPolling() - Restart polling servers as needed. | |
24 | * cupsdSaveRemoteCache() - Save the remote printer cache. | |
25 | * cupsdSendBrowseList() - Send new browsing information as necessary. | |
26 | * cupsdStartBrowsing() - Start sending and receiving broadcast | |
27 | * information. | |
28 | * cupsdStartPolling() - Start polling servers as needed. | |
29 | * cupsdStopBrowsing() - Stop sending and receiving broadcast | |
30 | * information. | |
31 | * cupsdStopPolling() - Stop polling servers as needed. | |
32 | * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries. | |
33 | * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP... | |
34 | * cupsdUpdateSLPBrowse() - Get browsing information via SLP. | |
35 | * dequote() - Remote quotes from a string. | |
36 | * is_local_queue() - Determine whether the URI points at a local | |
37 | * queue. | |
38 | * process_browse_data() - Process new browse data. | |
39 | * dnssdBuildTxtRecord() - Build a TXT record from printer info. | |
40 | * dnssdDeregisterPrinter() - Stop sending broadcast information for a | |
41 | * printer. | |
42 | * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT | |
43 | * record format. | |
44 | * dnssdRegisterCallback() - DNSServiceRegister callback. | |
45 | * dnssdRegisterPrinter() - Start sending broadcast information for a | |
46 | * printer or update the broadcast contents. | |
47 | * process_implicit_classes() - Create/update implicit classes as needed. | |
48 | * send_cups_browse() - Send new browsing information using the CUPS | |
49 | * protocol. | |
50 | * send_ldap_browse() - Send LDAP printer registrations. | |
51 | * send_slp_browse() - Register the specified printer with SLP. | |
52 | * slp_attr_callback() - SLP attribute callback | |
53 | * slp_dereg_printer() - SLPDereg() the specified printer | |
54 | * slp_get_attr() - Get an attribute from an SLP registration. | |
55 | * slp_reg_callback() - Empty SLPRegReport. | |
56 | * slp_url_callback() - SLP service url callback | |
57 | * update_cups_browse() - Update the browse lists using the CUPS | |
58 | * protocol. | |
59 | * update_lpd() - Update the LPD configuration as needed. | |
60 | * update_polling() - Read status messages from the poll daemons. | |
61 | * update_smb() - Update the SMB configuration as needed. | |
ef416fc2 | 62 | */ |
63 | ||
64 | /* | |
65 | * Include necessary headers... | |
66 | */ | |
67 | ||
68 | #include "cupsd.h" | |
69 | #include <grp.h> | |
70 | ||
f7deaa1a | 71 | #ifdef HAVE_DNSSD |
72 | # include <dns_sd.h> | |
cc0d019f MS |
73 | # ifdef __APPLE__ |
74 | # include <nameser.h> | |
75 | # ifdef HAVE_COREFOUNDATION | |
76 | # include <CoreFoundation/CoreFoundation.h> | |
77 | # endif /* HAVE_COREFOUNDATION */ | |
78 | # ifdef HAVE_SYSTEMCONFIGURATION | |
79 | # include <SystemConfiguration/SystemConfiguration.h> | |
80 | # endif /* HAVE_SYSTEMCONFIGURATION */ | |
81 | # endif /* __APPLE__ */ | |
f7deaa1a | 82 | #endif /* HAVE_DNSSD */ |
83 | ||
ef416fc2 | 84 | |
85 | /* | |
e00b005a | 86 | * Local functions... |
ef416fc2 | 87 | */ |
88 | ||
e00b005a | 89 | static char *dequote(char *d, const char *s, int dlen); |
b423cd4c | 90 | static int is_local_queue(const char *uri, char *host, int hostlen, |
91 | char *resource, int resourcelen); | |
92 | static void process_browse_data(const char *uri, const char *host, | |
93 | const char *resource, cups_ptype_t type, | |
e00b005a | 94 | ipp_pstate_t state, const char *location, |
95 | const char *info, const char *make_model, | |
96 | int num_attrs, cups_option_t *attrs); | |
c277e2f8 | 97 | static void process_implicit_classes(int *write_printcap); |
e1d6a774 | 98 | static void send_cups_browse(cupsd_printer_t *p); |
99 | #ifdef HAVE_LDAP | |
100 | static void send_ldap_browse(cupsd_printer_t *p); | |
101 | #endif /* HAVE_LDAP */ | |
80ca4592 | 102 | #ifdef HAVE_LIBSLP |
e1d6a774 | 103 | static void send_slp_browse(cupsd_printer_t *p); |
80ca4592 | 104 | #endif /* HAVE_LIBSLP */ |
f899b121 | 105 | static void update_cups_browse(void); |
2e4ff8af | 106 | static void update_lpd(int onoff); |
f899b121 | 107 | static void update_polling(void); |
2e4ff8af | 108 | static void update_smb(int onoff); |
f899b121 | 109 | |
e00b005a | 110 | |
b423cd4c | 111 | #ifdef HAVE_OPENLDAP |
112 | static const char * const ldap_attrs[] =/* CUPS LDAP attributes */ | |
113 | { | |
114 | "printerDescription", | |
115 | "printerLocation", | |
116 | "printerMakeAndModel", | |
117 | "printerType", | |
118 | "printerURI", | |
119 | NULL | |
120 | }; | |
121 | #endif /* HAVE_OPENLDAP */ | |
122 | ||
ef416fc2 | 123 | #ifdef HAVE_LIBSLP |
e00b005a | 124 | /* |
125 | * SLP definitions... | |
126 | */ | |
127 | ||
ef416fc2 | 128 | /* |
129 | * SLP service name for CUPS... | |
130 | */ | |
131 | ||
132 | # define SLP_CUPS_SRVTYPE "service:printer" | |
133 | # define SLP_CUPS_SRVLEN 15 | |
134 | ||
135 | ||
136 | /* | |
137 | * Printer service URL structure | |
138 | */ | |
139 | ||
140 | typedef struct _slpsrvurl_s /**** SLP URL list ****/ | |
141 | { | |
142 | struct _slpsrvurl_s *next; /* Next URL in list */ | |
143 | char url[HTTP_MAX_URI]; | |
144 | /* URL */ | |
145 | } slpsrvurl_t; | |
146 | ||
147 | ||
148 | /* | |
149 | * Local functions... | |
150 | */ | |
151 | ||
152 | static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist, | |
153 | SLPError errcode, void *cookie); | |
154 | static void slp_dereg_printer(cupsd_printer_t *p); | |
155 | static int slp_get_attr(const char *attrlist, const char *tag, | |
156 | char **valbuf); | |
157 | static void slp_reg_callback(SLPHandle hslp, SLPError errcode, | |
158 | void *cookie); | |
159 | static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl, | |
160 | unsigned short lifetime, | |
161 | SLPError errcode, void *cookie); | |
162 | #endif /* HAVE_LIBSLP */ | |
163 | ||
f7deaa1a | 164 | #ifdef HAVE_DNSSD |
165 | /* | |
166 | * For IPP register using a subtype of 'cups' so that shared printer browsing | |
167 | * only finds other CUPS servers (not all IPP based printers). | |
168 | */ | |
169 | static char dnssdIPPRegType[] = "_ipp._tcp,_cups"; | |
170 | static char dnssdIPPFaxRegType[] = "_fax-ipp._tcp"; | |
171 | ||
172 | static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p); | |
173 | static void dnssdDeregisterPrinter(cupsd_printer_t *p); | |
174 | static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2], | |
175 | int count); | |
176 | static void dnssdRegisterCallback(DNSServiceRef sdRef, | |
177 | DNSServiceFlags flags, | |
178 | DNSServiceErrorType errorCode, | |
179 | const char *name, const char *regtype, | |
180 | const char *domain, void *context); | |
181 | static void dnssdRegisterPrinter(cupsd_printer_t *p); | |
182 | #endif /* HAVE_DNSSD */ | |
183 | ||
184 | ||
185 | /* | |
186 | * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a | |
187 | * local printer and remove any pending | |
188 | * references to remote printers. | |
189 | */ | |
190 | ||
191 | void | |
192 | cupsdDeregisterPrinter( | |
193 | cupsd_printer_t *p, /* I - Printer to register */ | |
194 | int removeit) /* I - Printer being permanently removed */ | |
195 | { | |
196 | /* | |
197 | * Only deregister if browsing is enabled and it's a local printers... | |
198 | */ | |
199 | ||
200 | if (!Browsing || !p->shared || | |
09a101d6 | 201 | (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
f7deaa1a | 202 | return; |
203 | ||
204 | /* | |
205 | * Announce the deletion... | |
206 | */ | |
207 | ||
0a682745 | 208 | if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0) |
f7deaa1a | 209 | { |
210 | cups_ptype_t savedtype = p->type; /* Saved printer type */ | |
211 | ||
212 | p->type |= CUPS_PRINTER_DELETE; | |
213 | ||
214 | send_cups_browse(p); | |
215 | ||
216 | p->type = savedtype; | |
217 | } | |
218 | ||
219 | #ifdef HAVE_LIBSLP | |
220 | if (BrowseLocalProtocols & BROWSE_SLP) | |
221 | slp_dereg_printer(p); | |
222 | #endif /* HAVE_LIBSLP */ | |
223 | ||
224 | #ifdef HAVE_DNSSD | |
225 | if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD)) | |
226 | dnssdDeregisterPrinter(p); | |
227 | #endif /* HAVE_DNSSD */ | |
228 | } | |
229 | ||
ef416fc2 | 230 | |
231 | /* | |
232 | * 'cupsdLoadRemoteCache()' - Load the remote printer cache. | |
233 | */ | |
234 | ||
235 | void | |
236 | cupsdLoadRemoteCache(void) | |
237 | { | |
238 | cups_file_t *fp; /* remote.cache file */ | |
239 | int linenum; /* Current line number */ | |
240 | char line[1024], /* Line from file */ | |
241 | *value, /* Pointer to value */ | |
bd7854cb | 242 | *valueptr, /* Pointer into value */ |
243 | scheme[32], /* Scheme portion of URI */ | |
244 | username[64], /* Username portion of URI */ | |
245 | host[HTTP_MAX_HOST], | |
246 | /* Hostname portion of URI */ | |
247 | resource[HTTP_MAX_URI]; | |
248 | /* Resource portion of URI */ | |
249 | int port; /* Port number */ | |
ef416fc2 | 250 | cupsd_printer_t *p; /* Current printer */ |
251 | time_t now; /* Current time */ | |
252 | ||
253 | ||
411affcf | 254 | /* |
255 | * Don't load the cache if the CUPS remote protocol is disabled... | |
256 | */ | |
257 | ||
b86bc4cf | 258 | if (!Browsing || !(BrowseRemoteProtocols & BROWSE_CUPS)) |
411affcf | 259 | { |
260 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
261 | "cupsdLoadRemoteCache: Not loading remote cache."); | |
262 | return; | |
263 | } | |
264 | ||
ef416fc2 | 265 | /* |
266 | * Open the remote.cache file... | |
267 | */ | |
268 | ||
269 | snprintf(line, sizeof(line), "%s/remote.cache", CacheDir); | |
270 | if ((fp = cupsFileOpen(line, "r")) == NULL) | |
271 | return; | |
272 | ||
273 | /* | |
274 | * Read printer configurations until we hit EOF... | |
275 | */ | |
276 | ||
277 | linenum = 0; | |
278 | p = NULL; | |
279 | now = time(NULL); | |
280 | ||
281 | while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum)) | |
282 | { | |
283 | /* | |
284 | * Decode the directive... | |
285 | */ | |
286 | ||
287 | if (!strcasecmp(line, "<Printer") || | |
288 | !strcasecmp(line, "<DefaultPrinter")) | |
289 | { | |
290 | /* | |
291 | * <Printer name> or <DefaultPrinter name> | |
292 | */ | |
293 | ||
294 | if (p == NULL && value) | |
295 | { | |
296 | /* | |
297 | * Add the printer and a base file type... | |
298 | */ | |
299 | ||
300 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
301 | "cupsdLoadRemoteCache: Loading printer %s...", value); | |
302 | ||
bd7854cb | 303 | if ((p = cupsdFindDest(value)) != NULL) |
304 | { | |
305 | if (p->type & CUPS_PRINTER_CLASS) | |
306 | { | |
307 | cupsdLogMessage(CUPSD_LOG_WARN, | |
308 | "Cached remote printer \"%s\" conflicts with " | |
309 | "existing class!", | |
310 | value); | |
311 | p = NULL; | |
312 | continue; | |
313 | } | |
314 | } | |
315 | else | |
316 | p = cupsdAddPrinter(value); | |
317 | ||
8ca02f3c | 318 | p->accepting = 1; |
319 | p->state = IPP_PRINTER_IDLE; | |
09a101d6 | 320 | p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
8ca02f3c | 321 | p->browse_time = now; |
322 | p->browse_expire = now + BrowseTimeout; | |
ef416fc2 | 323 | |
324 | /* | |
325 | * Set the default printer as needed... | |
326 | */ | |
327 | ||
328 | if (!strcasecmp(line, "<DefaultPrinter")) | |
329 | DefaultPrinter = p; | |
330 | } | |
331 | else | |
332 | { | |
333 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
334 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 335 | break; |
ef416fc2 | 336 | } |
337 | } | |
338 | else if (!strcasecmp(line, "<Class") || | |
339 | !strcasecmp(line, "<DefaultClass")) | |
340 | { | |
341 | /* | |
342 | * <Class name> or <DefaultClass name> | |
343 | */ | |
344 | ||
345 | if (p == NULL && value) | |
346 | { | |
347 | /* | |
348 | * Add the printer and a base file type... | |
349 | */ | |
350 | ||
351 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
352 | "cupsdLoadRemoteCache: Loading class %s...", value); | |
353 | ||
bd7854cb | 354 | if ((p = cupsdFindDest(value)) != NULL) |
355 | p->type = CUPS_PRINTER_CLASS; | |
356 | else | |
357 | p = cupsdAddClass(value); | |
358 | ||
8ca02f3c | 359 | p->accepting = 1; |
360 | p->state = IPP_PRINTER_IDLE; | |
09a101d6 | 361 | p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
8ca02f3c | 362 | p->browse_time = now; |
363 | p->browse_expire = now + BrowseTimeout; | |
ef416fc2 | 364 | |
365 | /* | |
366 | * Set the default printer as needed... | |
367 | */ | |
368 | ||
369 | if (!strcasecmp(line, "<DefaultClass")) | |
370 | DefaultPrinter = p; | |
371 | } | |
372 | else | |
373 | { | |
374 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
375 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 376 | break; |
ef416fc2 | 377 | } |
378 | } | |
379 | else if (!strcasecmp(line, "</Printer>") || | |
380 | !strcasecmp(line, "</Class>")) | |
381 | { | |
382 | if (p != NULL) | |
383 | { | |
384 | /* | |
385 | * Close out the current printer... | |
386 | */ | |
387 | ||
388 | cupsdSetPrinterAttrs(p); | |
389 | ||
390 | p = NULL; | |
391 | } | |
392 | else | |
393 | { | |
394 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
395 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 396 | break; |
ef416fc2 | 397 | } |
398 | } | |
399 | else if (!p) | |
400 | { | |
401 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
402 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 403 | break; |
ef416fc2 | 404 | } |
405 | else if (!strcasecmp(line, "Info")) | |
406 | { | |
407 | if (value) | |
408 | cupsdSetString(&p->info, value); | |
409 | } | |
410 | else if (!strcasecmp(line, "MakeModel")) | |
411 | { | |
412 | if (value) | |
413 | cupsdSetString(&p->make_model, value); | |
414 | } | |
415 | else if (!strcasecmp(line, "Location")) | |
416 | { | |
417 | if (value) | |
418 | cupsdSetString(&p->location, value); | |
419 | } | |
420 | else if (!strcasecmp(line, "DeviceURI")) | |
421 | { | |
422 | if (value) | |
4400e98d | 423 | { |
bd7854cb | 424 | httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme), |
425 | username, sizeof(username), host, sizeof(host), &port, | |
426 | resource, sizeof(resource)); | |
427 | ||
428 | cupsdSetString(&p->hostname, host); | |
4400e98d | 429 | cupsdSetString(&p->uri, value); |
ef416fc2 | 430 | cupsdSetString(&p->device_uri, value); |
4400e98d | 431 | } |
ef416fc2 | 432 | else |
433 | { | |
434 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
435 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 436 | break; |
ef416fc2 | 437 | } |
438 | } | |
b423cd4c | 439 | else if (!strcasecmp(line, "Option") && value) |
440 | { | |
441 | /* | |
442 | * Option name value | |
443 | */ | |
444 | ||
445 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
446 | ||
447 | if (!*valueptr) | |
448 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
449 | "Syntax error on line %d of remote.cache.", linenum); | |
450 | else | |
451 | { | |
452 | for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0'); | |
453 | ||
454 | p->num_options = cupsAddOption(value, valueptr, p->num_options, | |
455 | &(p->options)); | |
456 | } | |
457 | } | |
ef416fc2 | 458 | else if (!strcasecmp(line, "State")) |
459 | { | |
460 | /* | |
461 | * Set the initial queue state... | |
462 | */ | |
463 | ||
464 | if (value && !strcasecmp(value, "idle")) | |
465 | p->state = IPP_PRINTER_IDLE; | |
466 | else if (value && !strcasecmp(value, "stopped")) | |
467 | p->state = IPP_PRINTER_STOPPED; | |
468 | else | |
469 | { | |
470 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
471 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 472 | break; |
ef416fc2 | 473 | } |
474 | } | |
475 | else if (!strcasecmp(line, "StateMessage")) | |
476 | { | |
477 | /* | |
478 | * Set the initial queue state message... | |
479 | */ | |
480 | ||
481 | if (value) | |
482 | strlcpy(p->state_message, value, sizeof(p->state_message)); | |
483 | } | |
484 | else if (!strcasecmp(line, "Accepting")) | |
485 | { | |
486 | /* | |
487 | * Set the initial accepting state... | |
488 | */ | |
489 | ||
490 | if (value && | |
491 | (!strcasecmp(value, "yes") || | |
492 | !strcasecmp(value, "on") || | |
493 | !strcasecmp(value, "true"))) | |
494 | p->accepting = 1; | |
495 | else if (value && | |
496 | (!strcasecmp(value, "no") || | |
497 | !strcasecmp(value, "off") || | |
498 | !strcasecmp(value, "false"))) | |
499 | p->accepting = 0; | |
500 | else | |
501 | { | |
502 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
503 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 504 | break; |
ef416fc2 | 505 | } |
506 | } | |
507 | else if (!strcasecmp(line, "Type")) | |
508 | { | |
509 | if (value) | |
510 | p->type = atoi(value); | |
511 | else | |
512 | { | |
513 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
514 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 515 | break; |
ef416fc2 | 516 | } |
517 | } | |
518 | else if (!strcasecmp(line, "BrowseTime")) | |
519 | { | |
520 | if (value) | |
521 | { | |
522 | time_t t = atoi(value); | |
523 | ||
8ca02f3c | 524 | if (t > p->browse_expire) |
525 | p->browse_expire = t; | |
ef416fc2 | 526 | } |
527 | else | |
528 | { | |
529 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
530 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 531 | break; |
ef416fc2 | 532 | } |
533 | } | |
534 | else if (!strcasecmp(line, "JobSheets")) | |
535 | { | |
536 | /* | |
537 | * Set the initial job sheets... | |
538 | */ | |
539 | ||
540 | if (value) | |
541 | { | |
542 | for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
543 | ||
544 | if (*valueptr) | |
545 | *valueptr++ = '\0'; | |
546 | ||
547 | cupsdSetString(&p->job_sheets[0], value); | |
548 | ||
549 | while (isspace(*valueptr & 255)) | |
550 | valueptr ++; | |
551 | ||
552 | if (*valueptr) | |
553 | { | |
554 | for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++); | |
555 | ||
556 | if (*valueptr) | |
557 | *valueptr++ = '\0'; | |
558 | ||
559 | cupsdSetString(&p->job_sheets[1], value); | |
560 | } | |
561 | } | |
562 | else | |
563 | { | |
564 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
565 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 566 | break; |
ef416fc2 | 567 | } |
568 | } | |
569 | else if (!strcasecmp(line, "AllowUser")) | |
570 | { | |
571 | if (value) | |
572 | { | |
573 | p->deny_users = 0; | |
574 | cupsdAddPrinterUser(p, value); | |
575 | } | |
576 | else | |
577 | { | |
578 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
579 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 580 | break; |
ef416fc2 | 581 | } |
582 | } | |
583 | else if (!strcasecmp(line, "DenyUser")) | |
584 | { | |
585 | if (value) | |
586 | { | |
587 | p->deny_users = 1; | |
588 | cupsdAddPrinterUser(p, value); | |
589 | } | |
590 | else | |
591 | { | |
592 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
593 | "Syntax error on line %d of remote.cache.", linenum); | |
91c84a35 | 594 | break; |
ef416fc2 | 595 | } |
596 | } | |
597 | else | |
598 | { | |
599 | /* | |
600 | * Something else we don't understand... | |
601 | */ | |
602 | ||
603 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
604 | "Unknown configuration directive %s on line %d of remote.cache.", | |
605 | line, linenum); | |
606 | } | |
607 | } | |
608 | ||
609 | cupsFileClose(fp); | |
610 | ||
611 | /* | |
612 | * Do auto-classing if needed... | |
613 | */ | |
614 | ||
c277e2f8 | 615 | process_implicit_classes(NULL); |
ef416fc2 | 616 | } |
617 | ||
618 | ||
f7deaa1a | 619 | /* |
620 | * 'cupsdRegisterPrinter()' - Start sending broadcast information for a | |
621 | * printer or update the broadcast contents. | |
622 | */ | |
623 | ||
624 | void | |
625 | cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */ | |
626 | { | |
627 | if (!Browsing || !BrowseLocalProtocols || !BrowseInterval || !NumBrowsers || | |
09a101d6 | 628 | (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) |
f7deaa1a | 629 | return; |
630 | ||
631 | #ifdef HAVE_LIBSLP | |
632 | /* if (BrowseLocalProtocols & BROWSE_SLP) | |
633 | slpRegisterPrinter(p); */ | |
634 | #endif /* HAVE_LIBSLP */ | |
635 | ||
636 | #ifdef HAVE_DNSSD | |
637 | if (BrowseLocalProtocols & BROWSE_DNSSD) | |
638 | dnssdRegisterPrinter(p); | |
639 | #endif /* HAVE_DNSSD */ | |
640 | } | |
641 | ||
642 | ||
d09495fa | 643 | /* |
644 | * 'cupsdRestartPolling()' - Restart polling servers as needed. | |
645 | */ | |
646 | ||
647 | void | |
648 | cupsdRestartPolling(void) | |
649 | { | |
650 | int i; /* Looping var */ | |
651 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
652 | ||
653 | ||
654 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) | |
655 | if (pollp->pid) | |
656 | kill(pollp->pid, SIGHUP); | |
657 | } | |
658 | ||
659 | ||
ef416fc2 | 660 | /* |
e00b005a | 661 | * 'cupsdSaveRemoteCache()' - Save the remote printer cache. |
ef416fc2 | 662 | */ |
663 | ||
664 | void | |
e00b005a | 665 | cupsdSaveRemoteCache(void) |
ef416fc2 | 666 | { |
e00b005a | 667 | int i; /* Looping var */ |
668 | cups_file_t *fp; /* printers.conf file */ | |
669 | char temp[1024]; /* Temporary string */ | |
670 | cupsd_printer_t *printer; /* Current printer class */ | |
671 | time_t curtime; /* Current time */ | |
672 | struct tm *curdate; /* Current date */ | |
b423cd4c | 673 | cups_option_t *option; /* Current option */ |
ef416fc2 | 674 | |
ef416fc2 | 675 | |
676 | /* | |
e00b005a | 677 | * Create the remote.cache file... |
ef416fc2 | 678 | */ |
679 | ||
e00b005a | 680 | snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir); |
ef416fc2 | 681 | |
e00b005a | 682 | if ((fp = cupsFileOpen(temp, "w")) == NULL) |
ef416fc2 | 683 | { |
684 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
e00b005a | 685 | "Unable to save remote.cache - %s", strerror(errno)); |
ef416fc2 | 686 | return; |
687 | } | |
e00b005a | 688 | else |
f7deaa1a | 689 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache..."); |
ef416fc2 | 690 | |
691 | /* | |
e00b005a | 692 | * Restrict access to the file... |
ef416fc2 | 693 | */ |
694 | ||
e00b005a | 695 | fchown(cupsFileNumber(fp), getuid(), Group); |
696 | fchmod(cupsFileNumber(fp), ConfigFilePerm); | |
ef416fc2 | 697 | |
e00b005a | 698 | /* |
699 | * Write a small header to the file... | |
700 | */ | |
ef416fc2 | 701 | |
e00b005a | 702 | curtime = time(NULL); |
703 | curdate = localtime(&curtime); | |
704 | strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate); | |
ef416fc2 | 705 | |
e00b005a | 706 | cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n"); |
707 | cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp); | |
ef416fc2 | 708 | |
709 | /* | |
e00b005a | 710 | * Write each local printer known to the system... |
ef416fc2 | 711 | */ |
712 | ||
e00b005a | 713 | for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers); |
714 | printer; | |
715 | printer = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 716 | { |
717 | /* | |
e00b005a | 718 | * Skip local destinations... |
ef416fc2 | 719 | */ |
720 | ||
09a101d6 | 721 | if (!(printer->type & CUPS_PRINTER_DISCOVERED)) |
e00b005a | 722 | continue; |
ef416fc2 | 723 | |
ef416fc2 | 724 | /* |
e00b005a | 725 | * Write printers as needed... |
ef416fc2 | 726 | */ |
727 | ||
e00b005a | 728 | if (printer == DefaultPrinter) |
729 | cupsFilePuts(fp, "<Default"); | |
ef416fc2 | 730 | else |
e00b005a | 731 | cupsFilePutChar(fp, '<'); |
ef416fc2 | 732 | |
e00b005a | 733 | if (printer->type & CUPS_PRINTER_CLASS) |
734 | cupsFilePrintf(fp, "Class %s>\n", printer->name); | |
735 | else | |
736 | cupsFilePrintf(fp, "Printer %s>\n", printer->name); | |
ef416fc2 | 737 | |
e00b005a | 738 | cupsFilePrintf(fp, "Type %d\n", printer->type); |
ef416fc2 | 739 | |
8ca02f3c | 740 | cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire); |
ef416fc2 | 741 | |
e00b005a | 742 | if (printer->info) |
743 | cupsFilePrintf(fp, "Info %s\n", printer->info); | |
ef416fc2 | 744 | |
e00b005a | 745 | if (printer->make_model) |
746 | cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model); | |
ef416fc2 | 747 | |
e00b005a | 748 | if (printer->location) |
749 | cupsFilePrintf(fp, "Location %s\n", printer->location); | |
ef416fc2 | 750 | |
e00b005a | 751 | if (printer->device_uri) |
752 | cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri); | |
ef416fc2 | 753 | |
e00b005a | 754 | if (printer->state == IPP_PRINTER_STOPPED) |
755 | { | |
756 | cupsFilePuts(fp, "State Stopped\n"); | |
757 | cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message); | |
ef416fc2 | 758 | } |
e00b005a | 759 | else |
760 | cupsFilePuts(fp, "State Idle\n"); | |
ef416fc2 | 761 | |
e00b005a | 762 | if (printer->accepting) |
763 | cupsFilePuts(fp, "Accepting Yes\n"); | |
764 | else | |
765 | cupsFilePuts(fp, "Accepting No\n"); | |
ef416fc2 | 766 | |
e00b005a | 767 | cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0], |
768 | printer->job_sheets[1]); | |
ef416fc2 | 769 | |
e00b005a | 770 | for (i = 0; i < printer->num_users; i ++) |
771 | cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow", | |
772 | printer->users[i]); | |
ef416fc2 | 773 | |
b423cd4c | 774 | for (i = printer->num_options, option = printer->options; |
775 | i > 0; | |
776 | i --, option ++) | |
777 | cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value); | |
778 | ||
e00b005a | 779 | if (printer->type & CUPS_PRINTER_CLASS) |
780 | cupsFilePuts(fp, "</Class>\n"); | |
781 | else | |
782 | cupsFilePuts(fp, "</Printer>\n"); | |
783 | } | |
ef416fc2 | 784 | |
e00b005a | 785 | cupsFileClose(fp); |
786 | } | |
ef416fc2 | 787 | |
ef416fc2 | 788 | |
e00b005a | 789 | /* |
790 | * 'cupsdSendBrowseList()' - Send new browsing information as necessary. | |
791 | */ | |
ef416fc2 | 792 | |
e00b005a | 793 | void |
794 | cupsdSendBrowseList(void) | |
795 | { | |
796 | int count; /* Number of dests to update */ | |
797 | cupsd_printer_t *p; /* Current printer */ | |
798 | time_t ut, /* Minimum update time */ | |
799 | to; /* Timeout time */ | |
2e4ff8af | 800 | int write_printcap; /* Write the printcap file? */ |
ef416fc2 | 801 | |
e00b005a | 802 | |
803 | if (!Browsing || !BrowseLocalProtocols || !Printers) | |
804 | return; | |
805 | ||
806 | /* | |
807 | * Compute the update and timeout times... | |
808 | */ | |
809 | ||
b423cd4c | 810 | to = time(NULL); |
811 | ut = to - BrowseInterval; | |
e00b005a | 812 | |
813 | /* | |
814 | * Figure out how many printers need an update... | |
815 | */ | |
816 | ||
817 | if (BrowseInterval > 0) | |
818 | { | |
819 | int max_count; /* Maximum number to update */ | |
820 | ||
821 | ||
822 | /* | |
823 | * Throttle the number of printers we'll be updating this time | |
824 | * around based on the number of queues that need updating and | |
825 | * the maximum number of queues to update each second... | |
826 | */ | |
827 | ||
828 | max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1; | |
829 | ||
830 | for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
831 | count < max_count && p != NULL; | |
832 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
09a101d6 | 833 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) && |
e00b005a | 834 | p->shared && p->browse_time < ut) |
835 | count ++; | |
836 | ||
837 | /* | |
838 | * Loop through all of the printers and send local updates as needed... | |
839 | */ | |
840 | ||
841 | if (BrowseNext) | |
842 | p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext); | |
843 | else | |
844 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
845 | ||
846 | for (; | |
847 | count > 0; | |
848 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 849 | { |
850 | /* | |
e00b005a | 851 | * Check for wraparound... |
ef416fc2 | 852 | */ |
853 | ||
e00b005a | 854 | if (!p) |
855 | p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
ef416fc2 | 856 | |
e00b005a | 857 | if (!p) |
858 | break; | |
09a101d6 | 859 | else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) || |
e00b005a | 860 | !p->shared) |
861 | continue; | |
862 | else if (p->browse_time < ut) | |
863 | { | |
864 | /* | |
865 | * Need to send an update... | |
866 | */ | |
ef416fc2 | 867 | |
e00b005a | 868 | count --; |
ef416fc2 | 869 | |
e00b005a | 870 | p->browse_time = time(NULL); |
ef416fc2 | 871 | |
0a682745 | 872 | if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0) |
e1d6a774 | 873 | send_cups_browse(p); |
ef416fc2 | 874 | |
e00b005a | 875 | #ifdef HAVE_LIBSLP |
876 | if (BrowseLocalProtocols & BROWSE_SLP) | |
e1d6a774 | 877 | send_slp_browse(p); |
e00b005a | 878 | #endif /* HAVE_LIBSLP */ |
b423cd4c | 879 | |
880 | #ifdef HAVE_LDAP | |
881 | if (BrowseLocalProtocols & BROWSE_LDAP) | |
e1d6a774 | 882 | send_ldap_browse(p); |
b423cd4c | 883 | #endif /* HAVE_LDAP */ |
e00b005a | 884 | } |
ef416fc2 | 885 | } |
e00b005a | 886 | |
887 | /* | |
888 | * Save where we left off so that all printers get updated... | |
889 | */ | |
890 | ||
891 | BrowseNext = p; | |
ef416fc2 | 892 | } |
893 | ||
894 | /* | |
e00b005a | 895 | * Loop through all of the printers and send local updates as needed... |
ef416fc2 | 896 | */ |
897 | ||
2e4ff8af | 898 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), write_printcap = 0; |
e00b005a | 899 | p; |
900 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 901 | { |
e00b005a | 902 | /* |
903 | * If this is a remote queue, see if it needs to be timed out... | |
904 | */ | |
ef416fc2 | 905 | |
09a101d6 | 906 | if (p->type & CUPS_PRINTER_DISCOVERED) |
ef416fc2 | 907 | { |
b423cd4c | 908 | if (p->browse_expire < to) |
e00b005a | 909 | { |
910 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
911 | "%s \'%s\' deleted by directory services (timeout).", | |
912 | (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", | |
913 | p->name); | |
914 | ||
89d46774 | 915 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
b423cd4c | 916 | "Remote destination \"%s\" has timed out; " |
917 | "deleting it...", | |
e00b005a | 918 | p->name); |
919 | ||
920 | cupsArraySave(Printers); | |
921 | cupsdDeletePrinter(p, 1); | |
922 | cupsArrayRestore(Printers); | |
2e4ff8af | 923 | write_printcap = 1; |
e00b005a | 924 | } |
ef416fc2 | 925 | } |
926 | } | |
2e4ff8af MS |
927 | |
928 | if (write_printcap) | |
929 | cupsdWritePrintcap(); | |
e00b005a | 930 | } |
ef416fc2 | 931 | |
ef416fc2 | 932 | |
e00b005a | 933 | /* |
e1d6a774 | 934 | * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information. |
e00b005a | 935 | */ |
ef416fc2 | 936 | |
e00b005a | 937 | void |
e1d6a774 | 938 | cupsdStartBrowsing(void) |
e00b005a | 939 | { |
e1d6a774 | 940 | int val; /* Socket option value */ |
941 | struct sockaddr_in addr; /* Broadcast address */ | |
f7deaa1a | 942 | cupsd_printer_t *p; /* Current printer */ |
ef416fc2 | 943 | |
ef416fc2 | 944 | |
e1d6a774 | 945 | BrowseNext = NULL; |
ef416fc2 | 946 | |
e1d6a774 | 947 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
948 | return; | |
ef416fc2 | 949 | |
e1d6a774 | 950 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) |
951 | { | |
952 | if (BrowseSocket < 0) | |
953 | { | |
954 | /* | |
955 | * Create the broadcast socket... | |
956 | */ | |
ef416fc2 | 957 | |
e1d6a774 | 958 | if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
959 | { | |
960 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
bc44d920 | 961 | "Unable to create broadcast socket - %s.", |
962 | strerror(errno)); | |
e1d6a774 | 963 | BrowseLocalProtocols &= ~BROWSE_CUPS; |
964 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
965 | return; | |
966 | } | |
ef416fc2 | 967 | |
ef416fc2 | 968 | /* |
e1d6a774 | 969 | * Bind the socket to browse port... |
ef416fc2 | 970 | */ |
971 | ||
e1d6a774 | 972 | memset(&addr, 0, sizeof(addr)); |
973 | addr.sin_addr.s_addr = htonl(INADDR_ANY); | |
974 | addr.sin_family = AF_INET; | |
975 | addr.sin_port = htons(BrowsePort); | |
976 | ||
977 | if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr))) | |
ef416fc2 | 978 | { |
e1d6a774 | 979 | cupsdLogMessage(CUPSD_LOG_ERROR, |
bc44d920 | 980 | "Unable to bind broadcast socket - %s.", |
981 | strerror(errno)); | |
ef416fc2 | 982 | |
e1d6a774 | 983 | #ifdef WIN32 |
984 | closesocket(BrowseSocket); | |
985 | #else | |
986 | close(BrowseSocket); | |
987 | #endif /* WIN32 */ | |
ef416fc2 | 988 | |
e1d6a774 | 989 | BrowseSocket = -1; |
990 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
991 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
992 | return; | |
993 | } | |
994 | } | |
ef416fc2 | 995 | |
e1d6a774 | 996 | /* |
997 | * Set the "broadcast" flag... | |
998 | */ | |
ef416fc2 | 999 | |
e1d6a774 | 1000 | val = 1; |
1001 | if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val))) | |
1002 | { | |
bc44d920 | 1003 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.", |
e1d6a774 | 1004 | strerror(errno)); |
ef416fc2 | 1005 | |
e1d6a774 | 1006 | #ifdef WIN32 |
1007 | closesocket(BrowseSocket); | |
1008 | #else | |
1009 | close(BrowseSocket); | |
1010 | #endif /* WIN32 */ | |
ef416fc2 | 1011 | |
e1d6a774 | 1012 | BrowseSocket = -1; |
1013 | BrowseLocalProtocols &= ~BROWSE_CUPS; | |
1014 | BrowseRemoteProtocols &= ~BROWSE_CUPS; | |
1015 | return; | |
1016 | } | |
e00b005a | 1017 | |
e1d6a774 | 1018 | /* |
1019 | * Close the socket on exec... | |
1020 | */ | |
e00b005a | 1021 | |
e1d6a774 | 1022 | fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC); |
e00b005a | 1023 | |
e1d6a774 | 1024 | /* |
8ca02f3c | 1025 | * Finally, add the socket to the input selection set as needed... |
e1d6a774 | 1026 | */ |
ef416fc2 | 1027 | |
8ca02f3c | 1028 | if (BrowseRemoteProtocols & BROWSE_CUPS) |
1029 | { | |
1030 | /* | |
1031 | * We only listen if we want remote printers... | |
1032 | */ | |
ef416fc2 | 1033 | |
f899b121 | 1034 | cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse, |
f7deaa1a | 1035 | NULL, NULL); |
8ca02f3c | 1036 | } |
e1d6a774 | 1037 | } |
1038 | else | |
1039 | BrowseSocket = -1; | |
ef416fc2 | 1040 | |
e1d6a774 | 1041 | #ifdef HAVE_LIBSLP |
1042 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) | |
1043 | { | |
1044 | /* | |
1045 | * Open SLP handle... | |
1046 | */ | |
ef416fc2 | 1047 | |
e1d6a774 | 1048 | if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK) |
1049 | { | |
1050 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1051 | "Unable to open an SLP handle; disabling SLP browsing!"); | |
1052 | BrowseLocalProtocols &= ~BROWSE_SLP; | |
1053 | BrowseRemoteProtocols &= ~BROWSE_SLP; | |
1054 | } | |
e00b005a | 1055 | |
e1d6a774 | 1056 | BrowseSLPRefresh = 0; |
1057 | } | |
f301802f | 1058 | else |
1059 | BrowseSLPHandle = NULL; | |
e1d6a774 | 1060 | #endif /* HAVE_LIBSLP */ |
1061 | ||
1062 | #ifdef HAVE_OPENLDAP | |
1063 | if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) | |
1064 | { | |
1065 | if (!BrowseLDAPDN) | |
1066 | { | |
1067 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1068 | "Need to set BrowseLDAPDN to use LDAP browsing!"); | |
1069 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1070 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
ef416fc2 | 1071 | } |
1072 | else | |
1073 | { | |
e1d6a774 | 1074 | /* |
1075 | * Open LDAP handle... | |
ef416fc2 | 1076 | */ |
1077 | ||
e1d6a774 | 1078 | int rc; /* LDAP API status */ |
1079 | int version = 3; /* LDAP version */ | |
1080 | struct berval bv = {0, ""}; /* SASL bind value */ | |
ef416fc2 | 1081 | |
ef416fc2 | 1082 | |
bc44d920 | 1083 | /* |
1084 | * Set the certificate file to use for encrypted LDAP sessions... | |
1085 | */ | |
1086 | ||
1087 | if (BrowseLDAPCACertFile) | |
1088 | { | |
1089 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1090 | "cupsdStartBrowsing: Setting CA certificate file \"%s\"", | |
1091 | BrowseLDAPCACertFile); | |
1092 | ||
1093 | if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE, | |
1094 | (void *)BrowseLDAPCACertFile)) | |
1095 | != LDAP_SUCCESS) | |
1096 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1097 | "Unable to set CA certificate file for LDAP " | |
1098 | "connections: %d - %s", rc, ldap_err2string(rc)); | |
1099 | } | |
1100 | ||
e1d6a774 | 1101 | /* |
1102 | * LDAP stuff currently only supports ldapi EXTERNAL SASL binds... | |
1103 | */ | |
ef416fc2 | 1104 | |
e1d6a774 | 1105 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) |
1106 | rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///"); | |
1107 | else | |
1108 | rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer); | |
ef416fc2 | 1109 | |
e1d6a774 | 1110 | if (rc != LDAP_SUCCESS) |
1111 | { | |
1112 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1113 | "Unable to initialize LDAP; disabling LDAP browsing!"); | |
1114 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1115 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1116 | } | |
1117 | else if (ldap_set_option(BrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION, | |
1118 | (const void *)&version) != LDAP_SUCCESS) | |
1119 | { | |
1120 | ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL); | |
1121 | BrowseLDAPHandle = NULL; | |
1122 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1123 | "Unable to set LDAP protocol version; " | |
1124 | "disabling LDAP browsing!"); | |
1125 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1126 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1127 | } | |
1128 | else | |
1129 | { | |
1130 | if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost")) | |
1131 | rc = ldap_sasl_bind_s(BrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL, | |
1132 | NULL, NULL); | |
1133 | else | |
1134 | rc = ldap_bind_s(BrowseLDAPHandle, BrowseLDAPBindDN, | |
1135 | BrowseLDAPPassword, LDAP_AUTH_SIMPLE); | |
1136 | ||
1137 | if (rc != LDAP_SUCCESS) | |
1138 | { | |
1139 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1140 | "Unable to bind to LDAP server; " | |
1141 | "disabling LDAP browsing!"); | |
1142 | ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL); | |
1143 | BrowseLocalProtocols &= ~BROWSE_LDAP; | |
1144 | BrowseRemoteProtocols &= ~BROWSE_LDAP; | |
1145 | } | |
e00b005a | 1146 | } |
ef416fc2 | 1147 | } |
e1d6a774 | 1148 | |
1149 | BrowseLDAPRefresh = 0; | |
1150 | } | |
1151 | #endif /* HAVE_OPENLDAP */ | |
f7deaa1a | 1152 | |
2e4ff8af MS |
1153 | /* |
1154 | * Enable LPD and SMB printer sharing as needed through external programs... | |
1155 | */ | |
1156 | ||
1157 | if (BrowseLocalProtocols & BROWSE_LPD) | |
1158 | update_lpd(1); | |
1159 | ||
1160 | if (BrowseLocalProtocols & BROWSE_SMB) | |
1161 | update_smb(1); | |
1162 | ||
f7deaa1a | 1163 | /* |
1164 | * Register the individual printers | |
1165 | */ | |
1166 | ||
1167 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
1168 | p; | |
1169 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1170 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) | |
1171 | cupsdRegisterPrinter(p); | |
ef416fc2 | 1172 | } |
1173 | ||
1174 | ||
b423cd4c | 1175 | /* |
e1d6a774 | 1176 | * 'cupsdStartPolling()' - Start polling servers as needed. |
b423cd4c | 1177 | */ |
1178 | ||
e1d6a774 | 1179 | void |
1180 | cupsdStartPolling(void) | |
b423cd4c | 1181 | { |
e1d6a774 | 1182 | int i; /* Looping var */ |
1183 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
1184 | char polld[1024]; /* Poll daemon path */ | |
07725fee | 1185 | char sport[255]; /* Server port */ |
1186 | char bport[255]; /* Browser port */ | |
1187 | char interval[255]; /* Poll interval */ | |
e1d6a774 | 1188 | int statusfds[2]; /* Status pipe */ |
1189 | char *argv[6]; /* Arguments */ | |
1190 | char *envp[100]; /* Environment */ | |
b423cd4c | 1191 | |
b423cd4c | 1192 | |
1193 | /* | |
e1d6a774 | 1194 | * Don't do anything if we aren't polling... |
b423cd4c | 1195 | */ |
1196 | ||
2abf387c | 1197 | if (NumPolled == 0 || BrowseSocket < 0) |
e1d6a774 | 1198 | { |
1199 | PollPipe = -1; | |
1200 | PollStatusBuffer = NULL; | |
1201 | return; | |
1202 | } | |
b423cd4c | 1203 | |
e1d6a774 | 1204 | /* |
1205 | * Setup string arguments for polld, port and interval options. | |
1206 | */ | |
b423cd4c | 1207 | |
e1d6a774 | 1208 | snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin); |
b423cd4c | 1209 | |
e1d6a774 | 1210 | sprintf(bport, "%d", BrowsePort); |
b423cd4c | 1211 | |
e1d6a774 | 1212 | if (BrowseInterval) |
1213 | sprintf(interval, "%d", BrowseInterval); | |
1214 | else | |
1215 | strcpy(interval, "30"); | |
b423cd4c | 1216 | |
e1d6a774 | 1217 | argv[0] = "cups-polld"; |
1218 | argv[2] = sport; | |
1219 | argv[3] = interval; | |
1220 | argv[4] = bport; | |
1221 | argv[5] = NULL; | |
b423cd4c | 1222 | |
e1d6a774 | 1223 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); |
1224 | ||
1225 | /* | |
1226 | * Create a pipe that receives the status messages from each | |
1227 | * polling daemon... | |
1228 | */ | |
1229 | ||
1230 | if (cupsdOpenPipe(statusfds)) | |
b423cd4c | 1231 | { |
e1d6a774 | 1232 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1233 | "Unable to create polling status pipes - %s.", | |
1234 | strerror(errno)); | |
1235 | PollPipe = -1; | |
1236 | PollStatusBuffer = NULL; | |
1237 | return; | |
1238 | } | |
b423cd4c | 1239 | |
e1d6a774 | 1240 | PollPipe = statusfds[0]; |
1241 | PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]"); | |
b423cd4c | 1242 | |
e1d6a774 | 1243 | /* |
1244 | * Run each polling daemon, redirecting stderr to the polling pipe... | |
1245 | */ | |
b423cd4c | 1246 | |
e1d6a774 | 1247 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
b423cd4c | 1248 | { |
e1d6a774 | 1249 | sprintf(sport, "%d", pollp->port); |
b423cd4c | 1250 | |
e1d6a774 | 1251 | argv[1] = pollp->hostname; |
b423cd4c | 1252 | |
f7deaa1a | 1253 | if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1, |
a4924f6c | 1254 | 0, DefaultProfile, &(pollp->pid)) < 0) |
b423cd4c | 1255 | { |
b423cd4c | 1256 | cupsdLogMessage(CUPSD_LOG_ERROR, |
e1d6a774 | 1257 | "cupsdStartPolling: Unable to fork polling daemon - %s", |
1258 | strerror(errno)); | |
1259 | pollp->pid = 0; | |
1260 | break; | |
1261 | } | |
1262 | else | |
1263 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
1264 | "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d", | |
1265 | pollp->hostname, pollp->port, pollp->pid); | |
b423cd4c | 1266 | } |
e1d6a774 | 1267 | |
1268 | close(statusfds[1]); | |
1269 | ||
1270 | /* | |
1271 | * Finally, add the pipe to the input selection set... | |
1272 | */ | |
1273 | ||
f899b121 | 1274 | cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL); |
b423cd4c | 1275 | } |
b423cd4c | 1276 | |
1277 | ||
ef416fc2 | 1278 | /* |
e1d6a774 | 1279 | * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information. |
ef416fc2 | 1280 | */ |
1281 | ||
e1d6a774 | 1282 | void |
1283 | cupsdStopBrowsing(void) | |
ef416fc2 | 1284 | { |
f7deaa1a | 1285 | cupsd_printer_t *p; /* Current printer */ |
1286 | ||
1287 | ||
e1d6a774 | 1288 | if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols)) |
1289 | return; | |
ef416fc2 | 1290 | |
f7deaa1a | 1291 | /* |
1292 | * De-register the individual printers | |
1293 | */ | |
1294 | ||
1295 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
1296 | p; | |
1297 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
1298 | if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))) | |
1299 | cupsdDeregisterPrinter(p, 1); | |
1300 | ||
1301 | /* | |
1302 | * Shut down browsing sockets... | |
1303 | */ | |
1304 | ||
e1d6a774 | 1305 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) && |
1306 | BrowseSocket >= 0) | |
1307 | { | |
1308 | /* | |
1309 | * Close the socket and remove it from the input selection set. | |
1310 | */ | |
ef416fc2 | 1311 | |
e1d6a774 | 1312 | #ifdef WIN32 |
1313 | closesocket(BrowseSocket); | |
1314 | #else | |
1315 | close(BrowseSocket); | |
1316 | #endif /* WIN32 */ | |
ef416fc2 | 1317 | |
f7deaa1a | 1318 | cupsdRemoveSelect(BrowseSocket); |
e1d6a774 | 1319 | BrowseSocket = -1; |
1320 | } | |
ef416fc2 | 1321 | |
e1d6a774 | 1322 | #ifdef HAVE_LIBSLP |
1323 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) && | |
1324 | BrowseSLPHandle) | |
1325 | { | |
1326 | /* | |
1327 | * Close SLP handle... | |
1328 | */ | |
ef416fc2 | 1329 | |
e1d6a774 | 1330 | SLPClose(BrowseSLPHandle); |
1331 | BrowseSLPHandle = NULL; | |
e00b005a | 1332 | } |
e1d6a774 | 1333 | #endif /* HAVE_LIBSLP */ |
ef416fc2 | 1334 | |
f301802f | 1335 | #ifdef HAVE_OPENLDAP |
e1d6a774 | 1336 | if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) && |
1337 | BrowseLDAPHandle) | |
e00b005a | 1338 | { |
e1d6a774 | 1339 | ldap_unbind(BrowseLDAPHandle); |
1340 | BrowseLDAPHandle = NULL; | |
e00b005a | 1341 | } |
e1d6a774 | 1342 | #endif /* HAVE_OPENLDAP */ |
2e4ff8af MS |
1343 | |
1344 | /* | |
1345 | * Disable LPD and SMB printer sharing as needed through external programs... | |
1346 | */ | |
1347 | ||
1348 | if (BrowseLocalProtocols & BROWSE_LPD) | |
1349 | update_lpd(0); | |
1350 | ||
1351 | if (BrowseLocalProtocols & BROWSE_SMB) | |
1352 | update_smb(0); | |
e1d6a774 | 1353 | } |
ef416fc2 | 1354 | |
ef416fc2 | 1355 | |
e1d6a774 | 1356 | /* |
1357 | * 'cupsdStopPolling()' - Stop polling servers as needed. | |
1358 | */ | |
ef416fc2 | 1359 | |
e1d6a774 | 1360 | void |
1361 | cupsdStopPolling(void) | |
1362 | { | |
1363 | int i; /* Looping var */ | |
1364 | cupsd_dirsvc_poll_t *pollp; /* Current polling server */ | |
ef416fc2 | 1365 | |
ef416fc2 | 1366 | |
e1d6a774 | 1367 | if (PollPipe >= 0) |
e00b005a | 1368 | { |
e1d6a774 | 1369 | cupsdStatBufDelete(PollStatusBuffer); |
1370 | close(PollPipe); | |
ef416fc2 | 1371 | |
f7deaa1a | 1372 | cupsdRemoveSelect(PollPipe); |
e1d6a774 | 1373 | |
1374 | PollPipe = -1; | |
1375 | PollStatusBuffer = NULL; | |
e00b005a | 1376 | } |
ef416fc2 | 1377 | |
e1d6a774 | 1378 | for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++) |
1379 | if (pollp->pid) | |
1380 | cupsdEndProcess(pollp->pid, 0); | |
1381 | } | |
ef416fc2 | 1382 | |
ef416fc2 | 1383 | |
f899b121 | 1384 | #ifdef HAVE_DNSSD |
e1d6a774 | 1385 | /* |
f899b121 | 1386 | * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries. |
e1d6a774 | 1387 | */ |
ef416fc2 | 1388 | |
e1d6a774 | 1389 | void |
f899b121 | 1390 | cupsdUpdateDNSSDBrowse( |
1391 | cupsd_printer_t *p) /* I - Printer being queried */ | |
e1d6a774 | 1392 | { |
f899b121 | 1393 | DNSServiceErrorType sdErr; /* Service discovery error */ |
ef416fc2 | 1394 | |
ef416fc2 | 1395 | |
f899b121 | 1396 | if ((sdErr = DNSServiceProcessResult(p->dnssd_ipp_ref)) |
1397 | != kDNSServiceErr_NoError) | |
e00b005a | 1398 | { |
f899b121 | 1399 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1400 | "DNS Service Discovery registration error %d for \"%s\"!", | |
1401 | sdErr, p->name); | |
1402 | cupsdRemoveSelect(p->dnssd_ipp_fd); | |
1403 | DNSServiceRefDeallocate(p->dnssd_ipp_ref); | |
ef416fc2 | 1404 | |
f899b121 | 1405 | p->dnssd_ipp_ref = NULL; |
1406 | p->dnssd_ipp_fd = -1; | |
1407 | } | |
1408 | } | |
1409 | #endif /* HAVE_DNSSD */ | |
ef416fc2 | 1410 | |
ef416fc2 | 1411 | |
f899b121 | 1412 | #ifdef HAVE_OPENLDAP |
1413 | /* | |
1414 | * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP... | |
1415 | */ | |
1416 | ||
1417 | void | |
1418 | cupsdUpdateLDAPBrowse(void) | |
1419 | { | |
1420 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
1421 | host[HTTP_MAX_URI], /* Hostname */ | |
1422 | resource[HTTP_MAX_URI], /* Resource path */ | |
1423 | location[1024], /* Printer location */ | |
1424 | info[1024], /* Printer information */ | |
1425 | make_model[1024], /* Printer make and model */ | |
1426 | **value; /* Holds the returned data from LDAP */ | |
1427 | int type; /* Printer type */ | |
1428 | int rc; /* LDAP status */ | |
1429 | int limit; /* Size limit */ | |
1430 | LDAPMessage *res, /* LDAP search results */ | |
1431 | *e; /* Current entry from search */ | |
ef416fc2 | 1432 | |
ef416fc2 | 1433 | |
1434 | /* | |
f899b121 | 1435 | * Search for printers... |
ef416fc2 | 1436 | */ |
1437 | ||
f899b121 | 1438 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName); |
ef416fc2 | 1439 | |
f899b121 | 1440 | BrowseLDAPRefresh = time(NULL) + BrowseInterval; |
ef416fc2 | 1441 | |
f899b121 | 1442 | rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE, |
1443 | "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res); | |
1444 | if (rc != LDAP_SUCCESS) | |
e1d6a774 | 1445 | { |
f899b121 | 1446 | cupsdLogMessage(CUPSD_LOG_ERROR, |
1447 | "LDAP search returned error %d: %s", rc, | |
1448 | ldap_err2string(rc)); | |
1449 | return; | |
e1d6a774 | 1450 | } |
ef416fc2 | 1451 | |
f899b121 | 1452 | limit = ldap_count_entries(BrowseLDAPHandle, res); |
1453 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit); | |
1454 | if (limit < 1) | |
1455 | return; | |
ef416fc2 | 1456 | |
e1d6a774 | 1457 | /* |
f899b121 | 1458 | * Loop through the available printers... |
e1d6a774 | 1459 | */ |
ef416fc2 | 1460 | |
f899b121 | 1461 | for (e = ldap_first_entry(BrowseLDAPHandle, res); |
1462 | e; | |
1463 | e = ldap_next_entry(BrowseLDAPHandle, e)) | |
e00b005a | 1464 | { |
f899b121 | 1465 | /* |
1466 | * Get the required values from this entry... | |
1467 | */ | |
ef416fc2 | 1468 | |
f899b121 | 1469 | if ((value = ldap_get_values(BrowseLDAPHandle, e, |
1470 | "printerDescription")) == NULL) | |
1471 | continue; | |
ef416fc2 | 1472 | |
f899b121 | 1473 | strlcpy(info, *value, sizeof(info)); |
1474 | ldap_value_free(value); | |
ef416fc2 | 1475 | |
f899b121 | 1476 | if ((value = ldap_get_values(BrowseLDAPHandle, e, |
1477 | "printerLocation")) == NULL) | |
1478 | continue; | |
ef416fc2 | 1479 | |
f899b121 | 1480 | strlcpy(location, *value, sizeof(location)); |
1481 | ldap_value_free(value); | |
ef416fc2 | 1482 | |
f899b121 | 1483 | if ((value = ldap_get_values(BrowseLDAPHandle, e, |
1484 | "printerMakeAndModel")) == NULL) | |
1485 | continue; | |
ef416fc2 | 1486 | |
f899b121 | 1487 | strlcpy(make_model, *value, sizeof(make_model)); |
1488 | ldap_value_free(value); | |
ef416fc2 | 1489 | |
f899b121 | 1490 | if ((value = ldap_get_values(BrowseLDAPHandle, e, |
1491 | "printerType")) == NULL) | |
1492 | continue; | |
ef416fc2 | 1493 | |
f899b121 | 1494 | type = atoi(*value); |
1495 | ldap_value_free(value); | |
ef416fc2 | 1496 | |
f899b121 | 1497 | if ((value = ldap_get_values(BrowseLDAPHandle, e, |
1498 | "printerURI")) == NULL) | |
1499 | continue; | |
ef416fc2 | 1500 | |
f899b121 | 1501 | strlcpy(uri, *value, sizeof(uri)); |
1502 | ldap_value_free(value); | |
ef416fc2 | 1503 | |
f899b121 | 1504 | /* |
1505 | * Process the entry as browse data... | |
1506 | */ | |
1507 | ||
1508 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) | |
1509 | process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE, | |
1510 | location, info, make_model, 0, NULL); | |
ef416fc2 | 1511 | |
e00b005a | 1512 | } |
f899b121 | 1513 | } |
1514 | #endif /* HAVE_OPENLDAP */ | |
ef416fc2 | 1515 | |
e1d6a774 | 1516 | |
f899b121 | 1517 | #ifdef HAVE_LIBSLP |
1518 | /* | |
1519 | * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP. | |
1520 | */ | |
ef416fc2 | 1521 | |
f899b121 | 1522 | void |
1523 | cupsdUpdateSLPBrowse(void) | |
1524 | { | |
1525 | slpsrvurl_t *s, /* Temporary list of service URLs */ | |
1526 | *next; /* Next service in list */ | |
1527 | cupsd_printer_t p; /* Printer information */ | |
1528 | const char *uri; /* Pointer to printer URI */ | |
1529 | char host[HTTP_MAX_URI], /* Host portion of URI */ | |
1530 | resource[HTTP_MAX_URI]; /* Resource portion of URI */ | |
e00b005a | 1531 | |
b423cd4c | 1532 | |
f899b121 | 1533 | /* |
1534 | * Reset the refresh time... | |
1535 | */ | |
b423cd4c | 1536 | |
f899b121 | 1537 | BrowseSLPRefresh = time(NULL) + BrowseInterval; |
b423cd4c | 1538 | |
f899b121 | 1539 | /* |
1540 | * Poll for remote printers using SLP... | |
1541 | */ | |
b423cd4c | 1542 | |
f899b121 | 1543 | s = NULL; |
b423cd4c | 1544 | |
f899b121 | 1545 | SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "", |
1546 | slp_url_callback, &s); | |
b423cd4c | 1547 | |
f899b121 | 1548 | /* |
1549 | * Loop through the list of available printers... | |
1550 | */ | |
e1d6a774 | 1551 | |
f899b121 | 1552 | for (; s; s = next) |
1553 | { | |
1554 | /* | |
1555 | * Save the "next" pointer... | |
1556 | */ | |
b423cd4c | 1557 | |
f899b121 | 1558 | next = s->next; |
e1d6a774 | 1559 | |
f899b121 | 1560 | /* |
1561 | * Load a cupsd_printer_t structure with the SLP service attributes... | |
1562 | */ | |
e1d6a774 | 1563 | |
f899b121 | 1564 | SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p); |
ef416fc2 | 1565 | |
f899b121 | 1566 | /* |
1567 | * Process this printer entry... | |
1568 | */ | |
ef416fc2 | 1569 | |
f899b121 | 1570 | uri = s->url + SLP_CUPS_SRVLEN + 1; |
ef416fc2 | 1571 | |
f899b121 | 1572 | if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6)) |
1573 | { | |
1574 | /* | |
1575 | * Pull the URI apart to see if this is a local or remote printer... | |
1576 | */ | |
ef416fc2 | 1577 | |
f899b121 | 1578 | if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) |
1579 | process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE, | |
1580 | p.location, p.info, p.make_model, 0, NULL); | |
1581 | } | |
ef416fc2 | 1582 | |
f899b121 | 1583 | /* |
1584 | * Free this listing... | |
1585 | */ | |
ef416fc2 | 1586 | |
f899b121 | 1587 | cupsdClearString(&p.info); |
1588 | cupsdClearString(&p.location); | |
1589 | cupsdClearString(&p.make_model); | |
ef416fc2 | 1590 | |
f899b121 | 1591 | free(s); |
1592 | } | |
e1d6a774 | 1593 | } |
f899b121 | 1594 | #endif /* HAVE_LIBSLP */ |
ef416fc2 | 1595 | |
ef416fc2 | 1596 | |
f7deaa1a | 1597 | /* |
f899b121 | 1598 | * 'dequote()' - Remote quotes from a string. |
f7deaa1a | 1599 | */ |
1600 | ||
f899b121 | 1601 | static char * /* O - Dequoted string */ |
1602 | dequote(char *d, /* I - Destination string */ | |
1603 | const char *s, /* I - Source string */ | |
1604 | int dlen) /* I - Destination length */ | |
f7deaa1a | 1605 | { |
f899b121 | 1606 | char *dptr; /* Pointer into destination */ |
f7deaa1a | 1607 | |
1608 | ||
f899b121 | 1609 | if (s) |
f7deaa1a | 1610 | { |
f899b121 | 1611 | for (dptr = d, dlen --; *s && dlen > 0; s ++) |
1612 | if (*s != '\"') | |
1613 | { | |
1614 | *dptr++ = *s; | |
1615 | dlen --; | |
1616 | } | |
f7deaa1a | 1617 | |
f899b121 | 1618 | *dptr = '\0'; |
f7deaa1a | 1619 | } |
f899b121 | 1620 | else |
1621 | *d = '\0'; | |
1622 | ||
1623 | return (d); | |
f7deaa1a | 1624 | } |
f7deaa1a | 1625 | |
1626 | ||
e1d6a774 | 1627 | /* |
f899b121 | 1628 | * 'is_local_queue()' - Determine whether the URI points at a local queue. |
e1d6a774 | 1629 | */ |
1630 | ||
f899b121 | 1631 | static int /* O - 1 = local, 0 = remote, -1 = bad URI */ |
1632 | is_local_queue(const char *uri, /* I - Printer URI */ | |
1633 | char *host, /* O - Host string */ | |
1634 | int hostlen, /* I - Length of host buffer */ | |
1635 | char *resource, /* O - Resource string */ | |
1636 | int resourcelen) /* I - Length of resource buffer */ | |
e1d6a774 | 1637 | { |
f899b121 | 1638 | char scheme[32], /* Scheme portion of URI */ |
1639 | username[HTTP_MAX_URI]; /* Username portion of URI */ | |
1640 | int port; /* Port portion of URI */ | |
1641 | cupsd_netif_t *iface; /* Network interface */ | |
e1d6a774 | 1642 | |
ef416fc2 | 1643 | |
e00b005a | 1644 | /* |
f899b121 | 1645 | * Pull the URI apart to see if this is a local or remote printer... |
e00b005a | 1646 | */ |
ef416fc2 | 1647 | |
f899b121 | 1648 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), |
1649 | username, sizeof(username), host, hostlen, &port, | |
1650 | resource, resourcelen) < HTTP_URI_OK) | |
1651 | return (-1); | |
ef416fc2 | 1652 | |
f899b121 | 1653 | DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName)); |
ef416fc2 | 1654 | |
e00b005a | 1655 | /* |
f899b121 | 1656 | * Check for local server addresses... |
e00b005a | 1657 | */ |
ef416fc2 | 1658 | |
f899b121 | 1659 | if (!strcasecmp(host, ServerName) && port == LocalPort) |
1660 | return (1); | |
ed486911 | 1661 | |
f899b121 | 1662 | cupsdNetIFUpdate(); |
ef416fc2 | 1663 | |
f899b121 | 1664 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); |
1665 | iface; | |
1666 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
1667 | if (!strcasecmp(host, iface->hostname) && port == iface->port) | |
1668 | return (1); | |
ed486911 | 1669 | |
f899b121 | 1670 | /* |
1671 | * If we get here, the printer is remote... | |
1672 | */ | |
e1d6a774 | 1673 | |
f899b121 | 1674 | return (0); |
ef416fc2 | 1675 | } |
1676 | ||
1677 | ||
ef416fc2 | 1678 | /* |
f899b121 | 1679 | * 'process_browse_data()' - Process new browse data. |
b423cd4c | 1680 | */ |
1681 | ||
e1d6a774 | 1682 | static void |
1683 | process_browse_data( | |
1684 | const char *uri, /* I - URI of printer/class */ | |
1685 | const char *host, /* I - Hostname */ | |
1686 | const char *resource, /* I - Resource path */ | |
1687 | cups_ptype_t type, /* I - Printer type */ | |
1688 | ipp_pstate_t state, /* I - Printer state */ | |
1689 | const char *location, /* I - Printer location */ | |
1690 | const char *info, /* I - Printer information */ | |
1691 | const char *make_model, /* I - Printer make and model */ | |
1692 | int num_attrs, /* I - Number of attributes */ | |
1693 | cups_option_t *attrs) /* I - Attributes */ | |
b423cd4c | 1694 | { |
e1d6a774 | 1695 | int i; /* Looping var */ |
c277e2f8 MS |
1696 | int update, /* Update printer attributes? */ |
1697 | write_printcap; /* Write the printcap file? */ | |
e1d6a774 | 1698 | char finaluri[HTTP_MAX_URI], /* Final URI for printer */ |
1699 | name[IPP_MAX_NAME], /* Name of printer */ | |
1700 | newname[IPP_MAX_NAME], /* New name of printer */ | |
1701 | *hptr, /* Pointer into hostname */ | |
1702 | *sptr; /* Pointer into ServerName */ | |
1703 | char local_make_model[IPP_MAX_NAME]; | |
1704 | /* Local make and model */ | |
1705 | cupsd_printer_t *p; /* Printer information */ | |
1706 | const char *ipp_options, /* ipp-options value */ | |
1707 | *lease_duration; /* lease-duration value */ | |
b423cd4c | 1708 | |
1709 | ||
1710 | /* | |
e1d6a774 | 1711 | * Determine if the URI contains any illegal characters in it... |
b423cd4c | 1712 | */ |
1713 | ||
e1d6a774 | 1714 | if (strncmp(uri, "ipp://", 6) || !host[0] || |
1715 | (strncmp(resource, "/printers/", 10) && | |
1716 | strncmp(resource, "/classes/", 9))) | |
b423cd4c | 1717 | { |
1718 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
e1d6a774 | 1719 | "process_browse_data: Bad printer URI in browse data: %s", |
1720 | uri); | |
b423cd4c | 1721 | return; |
1722 | } | |
1723 | ||
e1d6a774 | 1724 | if (strchr(resource, '?') || |
1725 | (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) || | |
1726 | (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/'))) | |
1727 | { | |
1728 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
1729 | "process_browse_data: Bad resource in browse data: %s", | |
1730 | resource); | |
b423cd4c | 1731 | return; |
e1d6a774 | 1732 | } |
b423cd4c | 1733 | |
1734 | /* | |
e1d6a774 | 1735 | * OK, this isn't a local printer; add any remote options... |
b423cd4c | 1736 | */ |
1737 | ||
e1d6a774 | 1738 | ipp_options = cupsGetOption("ipp-options", num_attrs, attrs); |
b423cd4c | 1739 | |
e1d6a774 | 1740 | if (BrowseRemoteOptions) |
b423cd4c | 1741 | { |
e1d6a774 | 1742 | if (BrowseRemoteOptions[0] == '?') |
1743 | { | |
1744 | /* | |
1745 | * Override server-supplied options... | |
1746 | */ | |
b423cd4c | 1747 | |
e1d6a774 | 1748 | snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions); |
1749 | } | |
1750 | else if (ipp_options) | |
1751 | { | |
1752 | /* | |
1753 | * Combine the server and local options... | |
1754 | */ | |
b423cd4c | 1755 | |
e1d6a774 | 1756 | snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options, |
1757 | BrowseRemoteOptions); | |
1758 | } | |
1759 | else | |
1760 | { | |
1761 | /* | |
1762 | * Just use the local options... | |
1763 | */ | |
b423cd4c | 1764 | |
e1d6a774 | 1765 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions); |
1766 | } | |
b423cd4c | 1767 | |
e1d6a774 | 1768 | uri = finaluri; |
b423cd4c | 1769 | } |
e1d6a774 | 1770 | else if (ipp_options) |
ef416fc2 | 1771 | { |
e00b005a | 1772 | /* |
e1d6a774 | 1773 | * Just use the server-supplied options... |
ef416fc2 | 1774 | */ |
1775 | ||
e1d6a774 | 1776 | snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options); |
1777 | uri = finaluri; | |
ef416fc2 | 1778 | } |
ef416fc2 | 1779 | |
e00b005a | 1780 | /* |
e1d6a774 | 1781 | * See if we already have it listed in the Printers list, and add it if not... |
e00b005a | 1782 | */ |
1783 | ||
c277e2f8 MS |
1784 | type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED; |
1785 | type &= ~CUPS_PRINTER_IMPLICIT; | |
1786 | update = 0; | |
1787 | write_printcap = 0; | |
1788 | hptr = strchr(host, '.'); | |
1789 | sptr = strchr(ServerName, '.'); | |
e00b005a | 1790 | |
8ca02f3c | 1791 | if (!ServerNameIsIP && sptr != NULL && hptr != NULL) |
e00b005a | 1792 | { |
1793 | /* | |
e1d6a774 | 1794 | * Strip the common domain name components... |
e00b005a | 1795 | */ |
1796 | ||
e1d6a774 | 1797 | while (hptr != NULL) |
1798 | { | |
1799 | if (!strcasecmp(hptr, sptr)) | |
1800 | { | |
1801 | *hptr = '\0'; | |
1802 | break; | |
1803 | } | |
1804 | else | |
1805 | hptr = strchr(hptr + 1, '.'); | |
1806 | } | |
1807 | } | |
e00b005a | 1808 | |
e1d6a774 | 1809 | if (type & CUPS_PRINTER_CLASS) |
1810 | { | |
e00b005a | 1811 | /* |
e1d6a774 | 1812 | * Remote destination is a class... |
e00b005a | 1813 | */ |
1814 | ||
e1d6a774 | 1815 | if (!strncmp(resource, "/classes/", 9)) |
1816 | snprintf(name, sizeof(name), "%s@%s", resource + 9, host); | |
1817 | else | |
1818 | return; | |
e00b005a | 1819 | |
d09495fa | 1820 | if (hptr && !*hptr) |
1821 | *hptr = '.'; /* Resource FQDN */ | |
1822 | ||
080811b1 | 1823 | if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames) |
e00b005a | 1824 | { |
080811b1 | 1825 | if ((p = cupsdFindDest(resource + 9)) != NULL) |
e1d6a774 | 1826 | { |
1827 | if (p->hostname && strcasecmp(p->hostname, host)) | |
1828 | { | |
1829 | /* | |
1830 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
1831 | * add it to the other class and then find a class using the full host | |
1832 | * name... | |
1833 | */ | |
e00b005a | 1834 | |
e1d6a774 | 1835 | if (p->type & CUPS_PRINTER_REMOTE) |
1836 | { | |
89d46774 | 1837 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
e1d6a774 | 1838 | "Renamed remote class \"%s\" to \"%s@%s\"...", |
1839 | p->name, p->name, p->hostname); | |
1840 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
1841 | "Class \'%s\' deleted by directory services.", | |
1842 | p->name); | |
e00b005a | 1843 | |
e1d6a774 | 1844 | snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname); |
1845 | cupsdRenamePrinter(p, newname); | |
e00b005a | 1846 | |
1847 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, | |
1848 | "Class \'%s\' added by directory services.", | |
1849 | p->name); | |
1850 | } | |
1851 | ||
1852 | p = NULL; | |
1853 | } | |
1854 | else if (!p->hostname) | |
1855 | { | |
1856 | /* | |
1857 | * Hostname not set, so this must be a cached remote printer | |
1858 | * that was created for a pending print job... | |
1859 | */ | |
1860 | ||
1861 | cupsdSetString(&p->hostname, host); | |
1862 | cupsdSetString(&p->uri, uri); | |
1863 | cupsdSetString(&p->device_uri, uri); | |
1864 | update = 1; | |
1865 | } | |
1866 | } | |
1867 | else | |
1868 | { | |
1869 | /* | |
1870 | * Use the short name for this shared class. | |
1871 | */ | |
1872 | ||
1873 | strlcpy(name, resource + 9, sizeof(name)); | |
1874 | } | |
1875 | } | |
1876 | else if (p && !p->hostname) | |
1877 | { | |
1878 | /* | |
1879 | * Hostname not set, so this must be a cached remote printer | |
1880 | * that was created for a pending print job... | |
1881 | */ | |
1882 | ||
1883 | cupsdSetString(&p->hostname, host); | |
1884 | cupsdSetString(&p->uri, uri); | |
1885 | cupsdSetString(&p->device_uri, uri); | |
1886 | update = 1; | |
1887 | } | |
1888 | ||
1889 | if (!p) | |
1890 | { | |
1891 | /* | |
1892 | * Class doesn't exist; add it... | |
1893 | */ | |
1894 | ||
1895 | p = cupsdAddClass(name); | |
1896 | ||
89d46774 | 1897 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote class \"%s\"...", name); |
e00b005a | 1898 | |
1899 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, | |
1900 | "Class \'%s\' added by directory services.", name); | |
1901 | ||
1902 | /* | |
1903 | * Force the URI to point to the real server... | |
1904 | */ | |
1905 | ||
1906 | p->type = type & ~CUPS_PRINTER_REJECTING; | |
1907 | p->accepting = 1; | |
1908 | cupsdSetString(&p->uri, uri); | |
1909 | cupsdSetString(&p->device_uri, uri); | |
1910 | cupsdSetString(&p->hostname, host); | |
1911 | ||
c277e2f8 MS |
1912 | update = 1; |
1913 | write_printcap = 1; | |
e00b005a | 1914 | } |
ef416fc2 | 1915 | } |
1916 | else | |
ef416fc2 | 1917 | { |
e00b005a | 1918 | /* |
1919 | * Remote destination is a printer... | |
1920 | */ | |
1921 | ||
1922 | if (!strncmp(resource, "/printers/", 10)) | |
1923 | snprintf(name, sizeof(name), "%s@%s", resource + 10, host); | |
1924 | else | |
1925 | return; | |
1926 | ||
d09495fa | 1927 | if (hptr && !*hptr) |
1928 | *hptr = '.'; /* Resource FQDN */ | |
1929 | ||
080811b1 | 1930 | if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames) |
e00b005a | 1931 | { |
080811b1 | 1932 | if ((p = cupsdFindDest(resource + 10)) != NULL) |
e00b005a | 1933 | { |
1934 | if (p->hostname && strcasecmp(p->hostname, host)) | |
1935 | { | |
1936 | /* | |
1937 | * Nope, this isn't the same host; if the hostname isn't the local host, | |
1938 | * add it to the other printer and then find a printer using the full host | |
1939 | * name... | |
1940 | */ | |
ef416fc2 | 1941 | |
e00b005a | 1942 | if (p->type & CUPS_PRINTER_REMOTE) |
1943 | { | |
89d46774 | 1944 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
e00b005a | 1945 | "Renamed remote printer \"%s\" to \"%s@%s\"...", |
1946 | p->name, p->name, p->hostname); | |
1947 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
1948 | "Printer \'%s\' deleted by directory services.", | |
1949 | p->name); | |
ef416fc2 | 1950 | |
b423cd4c | 1951 | snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname); |
1952 | cupsdRenamePrinter(p, newname); | |
ef416fc2 | 1953 | |
e00b005a | 1954 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
1955 | "Printer \'%s\' added by directory services.", | |
1956 | p->name); | |
1957 | } | |
ef416fc2 | 1958 | |
e00b005a | 1959 | p = NULL; |
1960 | } | |
1961 | else if (!p->hostname) | |
1962 | { | |
1963 | /* | |
1964 | * Hostname not set, so this must be a cached remote printer | |
1965 | * that was created for a pending print job... | |
1966 | */ | |
1967 | ||
1968 | cupsdSetString(&p->hostname, host); | |
1969 | cupsdSetString(&p->uri, uri); | |
1970 | cupsdSetString(&p->device_uri, uri); | |
1971 | update = 1; | |
1972 | } | |
1973 | } | |
1974 | else | |
1975 | { | |
1976 | /* | |
1977 | * Use the short name for this shared printer. | |
1978 | */ | |
1979 | ||
1980 | strlcpy(name, resource + 10, sizeof(name)); | |
1981 | } | |
1982 | } | |
1983 | else if (p && !p->hostname) | |
ef416fc2 | 1984 | { |
1985 | /* | |
e00b005a | 1986 | * Hostname not set, so this must be a cached remote printer |
1987 | * that was created for a pending print job... | |
ef416fc2 | 1988 | */ |
1989 | ||
e00b005a | 1990 | cupsdSetString(&p->hostname, host); |
1991 | cupsdSetString(&p->uri, uri); | |
1992 | cupsdSetString(&p->device_uri, uri); | |
1993 | update = 1; | |
ef416fc2 | 1994 | } |
e00b005a | 1995 | |
1996 | if (!p) | |
ef416fc2 | 1997 | { |
1998 | /* | |
e00b005a | 1999 | * Printer doesn't exist; add it... |
ef416fc2 | 2000 | */ |
2001 | ||
e00b005a | 2002 | p = cupsdAddPrinter(name); |
ef416fc2 | 2003 | |
e00b005a | 2004 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
2005 | "Printer \'%s\' added by directory services.", name); | |
ef416fc2 | 2006 | |
89d46774 | 2007 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote printer \"%s\"...", name); |
ef416fc2 | 2008 | |
e00b005a | 2009 | /* |
2010 | * Force the URI to point to the real server... | |
2011 | */ | |
ef416fc2 | 2012 | |
e00b005a | 2013 | p->type = type & ~CUPS_PRINTER_REJECTING; |
2014 | p->accepting = 1; | |
2015 | cupsdSetString(&p->hostname, host); | |
2016 | cupsdSetString(&p->uri, uri); | |
2017 | cupsdSetString(&p->device_uri, uri); | |
ef416fc2 | 2018 | |
c277e2f8 MS |
2019 | write_printcap = 1; |
2020 | update = 1; | |
ef416fc2 | 2021 | } |
2022 | } | |
ef416fc2 | 2023 | |
2024 | /* | |
e00b005a | 2025 | * Update the state... |
ef416fc2 | 2026 | */ |
2027 | ||
e00b005a | 2028 | p->state = state; |
2029 | p->browse_time = time(NULL); | |
ef416fc2 | 2030 | |
b423cd4c | 2031 | if ((lease_duration = cupsGetOption("lease-duration", num_attrs, |
2032 | attrs)) != NULL) | |
2033 | { | |
2034 | /* | |
2035 | * Grab the lease-duration for the browse data; anything less then 1 | |
2036 | * second or more than 1 week gets the default BrowseTimeout... | |
2037 | */ | |
2038 | ||
2039 | i = atoi(lease_duration); | |
2040 | if (i < 1 || i > 604800) | |
2041 | i = BrowseTimeout; | |
2042 | ||
2043 | p->browse_expire = p->browse_time + i; | |
2044 | } | |
2045 | else | |
2046 | p->browse_expire = p->browse_time + BrowseTimeout; | |
2047 | ||
e00b005a | 2048 | if (type & CUPS_PRINTER_REJECTING) |
ef416fc2 | 2049 | { |
e00b005a | 2050 | type &= ~CUPS_PRINTER_REJECTING; |
ef416fc2 | 2051 | |
e00b005a | 2052 | if (p->accepting) |
ef416fc2 | 2053 | { |
e00b005a | 2054 | update = 1; |
2055 | p->accepting = 0; | |
ef416fc2 | 2056 | } |
2057 | } | |
e00b005a | 2058 | else if (!p->accepting) |
2059 | { | |
2060 | update = 1; | |
2061 | p->accepting = 1; | |
2062 | } | |
ef416fc2 | 2063 | |
e00b005a | 2064 | if (p->type != type) |
2065 | { | |
2066 | p->type = type; | |
2067 | update = 1; | |
2068 | } | |
ef416fc2 | 2069 | |
e00b005a | 2070 | if (location && (!p->location || strcmp(p->location, location))) |
2071 | { | |
2072 | cupsdSetString(&p->location, location); | |
2073 | update = 1; | |
2074 | } | |
ef416fc2 | 2075 | |
e00b005a | 2076 | if (info && (!p->info || strcmp(p->info, info))) |
2077 | { | |
2078 | cupsdSetString(&p->info, info); | |
c277e2f8 MS |
2079 | update = 1; |
2080 | write_printcap = 1; | |
e00b005a | 2081 | } |
ef416fc2 | 2082 | |
e00b005a | 2083 | if (!make_model || !make_model[0]) |
2084 | { | |
2085 | if (type & CUPS_PRINTER_CLASS) | |
2086 | snprintf(local_make_model, sizeof(local_make_model), | |
2087 | "Remote Class on %s", host); | |
2088 | else | |
2089 | snprintf(local_make_model, sizeof(local_make_model), | |
2090 | "Remote Printer on %s", host); | |
2091 | } | |
2092 | else | |
2093 | snprintf(local_make_model, sizeof(local_make_model), | |
2094 | "%s on %s", make_model, host); | |
ef416fc2 | 2095 | |
e00b005a | 2096 | if (!p->make_model || strcmp(p->make_model, local_make_model)) |
ef416fc2 | 2097 | { |
e00b005a | 2098 | cupsdSetString(&p->make_model, local_make_model); |
2099 | update = 1; | |
ef416fc2 | 2100 | } |
2101 | ||
b423cd4c | 2102 | if (p->num_options) |
2103 | { | |
2104 | if (!update && !(type & CUPS_PRINTER_DELETE)) | |
2105 | { | |
2106 | /* | |
2107 | * See if we need to update the attributes... | |
2108 | */ | |
2109 | ||
2110 | if (p->num_options != num_attrs) | |
2111 | update = 1; | |
2112 | else | |
2113 | { | |
2114 | for (i = 0; i < num_attrs; i ++) | |
2115 | if (strcmp(attrs[i].name, p->options[i].name) || | |
2116 | (!attrs[i].value != !p->options[i].value) || | |
2117 | (attrs[i].value && strcmp(attrs[i].value, p->options[i].value))) | |
2118 | { | |
2119 | update = 1; | |
2120 | break; | |
2121 | } | |
2122 | } | |
2123 | } | |
2124 | ||
2125 | /* | |
2126 | * Free the old options... | |
2127 | */ | |
2128 | ||
2129 | cupsFreeOptions(p->num_options, p->options); | |
2130 | } | |
2131 | ||
2132 | p->num_options = num_attrs; | |
2133 | p->options = attrs; | |
2134 | ||
e00b005a | 2135 | if (type & CUPS_PRINTER_DELETE) |
2136 | { | |
2137 | cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL, | |
2138 | "%s \'%s\' deleted by directory services.", | |
2139 | (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name); | |
ef416fc2 | 2140 | |
e00b005a | 2141 | cupsdExpireSubscriptions(p, NULL); |
2142 | ||
2143 | cupsdDeletePrinter(p, 1); | |
2144 | cupsdUpdateImplicitClasses(); | |
c277e2f8 | 2145 | write_printcap = 1; |
e00b005a | 2146 | } |
2147 | else if (update) | |
2148 | { | |
2149 | cupsdSetPrinterAttrs(p); | |
2150 | cupsdUpdateImplicitClasses(); | |
2151 | } | |
ef416fc2 | 2152 | |
2153 | /* | |
b423cd4c | 2154 | * See if we have a default printer... If not, make the first network |
2155 | * default printer the default. | |
ef416fc2 | 2156 | */ |
2157 | ||
e00b005a | 2158 | if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault) |
2159 | { | |
2160 | /* | |
2161 | * Find the first network default printer and use it... | |
2162 | */ | |
2163 | ||
2164 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers); | |
2165 | p; | |
2166 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
2167 | if (p->type & CUPS_PRINTER_DEFAULT) | |
ef416fc2 | 2168 | { |
e00b005a | 2169 | DefaultPrinter = p; |
c277e2f8 | 2170 | write_printcap = 1; |
e00b005a | 2171 | break; |
ef416fc2 | 2172 | } |
e00b005a | 2173 | } |
ef416fc2 | 2174 | |
2175 | /* | |
e00b005a | 2176 | * Do auto-classing if needed... |
ef416fc2 | 2177 | */ |
2178 | ||
c277e2f8 | 2179 | process_implicit_classes(&write_printcap); |
e00b005a | 2180 | |
2181 | /* | |
2182 | * Update the printcap file... | |
2183 | */ | |
2184 | ||
c277e2f8 MS |
2185 | if (write_printcap) |
2186 | cupsdWritePrintcap(); | |
ef416fc2 | 2187 | } |
2188 | ||
2189 | ||
f7deaa1a | 2190 | #ifdef HAVE_DNSSD |
2191 | /* | |
2192 | * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info. | |
2193 | */ | |
2194 | ||
2195 | static char * /* O - TXT record */ | |
2196 | dnssdBuildTxtRecord( | |
2197 | int *txt_len, /* O - TXT record length */ | |
2198 | cupsd_printer_t *p) /* I - Printer information */ | |
2199 | { | |
7594b224 | 2200 | int i, j; /* Looping vars */ |
f7deaa1a | 2201 | char type_str[32], /* Type to string buffer */ |
2202 | state_str[32], /* State to string buffer */ | |
2203 | rp_str[1024], /* Queue name string buffer */ | |
7594b224 | 2204 | air_str[1024], /* auth-info-required string buffer */ |
f7deaa1a | 2205 | *keyvalue[32][2]; /* Table of key/value pairs */ |
2206 | ||
2207 | ||
2208 | /* | |
2209 | * Load up the key value pairs... | |
2210 | */ | |
2211 | ||
2212 | i = 0; | |
2213 | ||
2214 | keyvalue[i ][0] = "txtvers"; | |
2215 | keyvalue[i++][1] = "1"; | |
2216 | ||
2217 | keyvalue[i ][0] = "qtotal"; | |
2218 | keyvalue[i++][1] = "1"; | |
2219 | ||
2220 | keyvalue[i ][0] = "rp"; | |
2221 | keyvalue[i++][1] = rp_str; | |
2222 | snprintf(rp_str, sizeof(rp_str), "%s/%s", | |
2223 | (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name); | |
2224 | ||
2225 | keyvalue[i ][0] = "ty"; | |
2226 | keyvalue[i++][1] = p->make_model; | |
2227 | ||
2228 | if (p->location && *p->location != '\0') | |
2229 | { | |
2230 | keyvalue[i ][0] = "note"; | |
2231 | keyvalue[i++][1] = p->location; | |
2232 | } | |
2233 | ||
2234 | keyvalue[i ][0] = "product"; | |
2235 | keyvalue[i++][1] = p->product ? p->product : "Unknown"; | |
2236 | ||
2237 | snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE); | |
2238 | snprintf(state_str, sizeof(state_str), "%d", p->state); | |
2239 | ||
2240 | keyvalue[i ][0] = "printer-state"; | |
2241 | keyvalue[i++][1] = state_str; | |
2242 | ||
2243 | keyvalue[i ][0] = "printer-type"; | |
2244 | keyvalue[i++][1] = type_str; | |
2245 | ||
2246 | keyvalue[i ][0] = "Transparent"; | |
2247 | keyvalue[i++][1] = "T"; | |
2248 | ||
2249 | keyvalue[i ][0] = "Binary"; | |
2250 | keyvalue[i++][1] = "T"; | |
2251 | ||
2252 | if ((p->type & CUPS_PRINTER_FAX)) | |
2253 | { | |
2254 | keyvalue[i ][0] = "Fax"; | |
2255 | keyvalue[i++][1] = "T"; | |
2256 | } | |
2257 | ||
2258 | if ((p->type & CUPS_PRINTER_COLOR)) | |
2259 | { | |
2260 | keyvalue[i ][0] = "Color"; | |
2261 | keyvalue[i++][1] = "T"; | |
2262 | } | |
2263 | ||
2264 | if ((p->type & CUPS_PRINTER_DUPLEX)) | |
2265 | { | |
2266 | keyvalue[i ][0] = "Duplex"; | |
2267 | keyvalue[i++][1] = "T"; | |
2268 | } | |
2269 | ||
2270 | if ((p->type & CUPS_PRINTER_STAPLE)) | |
2271 | { | |
2272 | keyvalue[i ][0] = "Staple"; | |
2273 | keyvalue[i++][1] = "T"; | |
2274 | } | |
2275 | ||
2276 | if ((p->type & CUPS_PRINTER_COPIES)) | |
2277 | { | |
2278 | keyvalue[i ][0] = "Copies"; | |
2279 | keyvalue[i++][1] = "T"; | |
2280 | } | |
2281 | ||
2282 | if ((p->type & CUPS_PRINTER_COLLATE)) | |
2283 | { | |
2284 | keyvalue[i ][0] = "Collate"; | |
2285 | keyvalue[i++][1] = "T"; | |
2286 | } | |
2287 | ||
2288 | if ((p->type & CUPS_PRINTER_PUNCH)) | |
2289 | { | |
2290 | keyvalue[i ][0] = "Punch"; | |
2291 | keyvalue[i++][1] = "T"; | |
2292 | } | |
2293 | ||
2294 | if ((p->type & CUPS_PRINTER_BIND)) | |
2295 | { | |
2296 | keyvalue[i ][0] = "Bind"; | |
2297 | keyvalue[i++][1] = "T"; | |
2298 | } | |
2299 | ||
2300 | if ((p->type & CUPS_PRINTER_SORT)) | |
2301 | { | |
2302 | keyvalue[i ][0] = "Sort"; | |
2303 | keyvalue[i++][1] = "T"; | |
2304 | } | |
2305 | ||
2306 | keyvalue[i ][0] = "pdl"; | |
2307 | keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript"; | |
2308 | ||
7594b224 | 2309 | if (p->num_auth_info_required) |
2310 | { | |
2311 | char *air = air_str; /* Pointer into string */ | |
2312 | ||
2313 | ||
2314 | for (j = 0; j < p->num_auth_info_required; j ++) | |
2315 | { | |
2316 | if (air >= (air_str + sizeof(air_str) - 2)) | |
2317 | break; | |
2318 | ||
2319 | if (j) | |
2320 | *air++ = ','; | |
2321 | ||
2322 | strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str)); | |
2323 | air += strlen(air); | |
2324 | } | |
2325 | ||
2326 | keyvalue[i ][0] = "air"; | |
2327 | keyvalue[i++][1] = air; | |
2328 | } | |
2329 | ||
f7deaa1a | 2330 | /* |
2331 | * Then pack them into a proper txt record... | |
2332 | */ | |
2333 | ||
2334 | return (dnssdPackTxtRecord(txt_len, keyvalue, i)); | |
2335 | } | |
2336 | ||
2337 | ||
2338 | /* | |
2339 | * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a | |
2340 | * printer. | |
2341 | */ | |
2342 | ||
2343 | static void | |
2344 | dnssdDeregisterPrinter( | |
2345 | cupsd_printer_t *p) /* I - Printer */ | |
2346 | { | |
2347 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name); | |
2348 | ||
2349 | /* | |
2350 | * Closing the socket deregisters the service | |
2351 | */ | |
2352 | ||
2353 | if (p->dnssd_ipp_ref) | |
2354 | { | |
2355 | cupsdRemoveSelect(p->dnssd_ipp_fd); | |
2356 | DNSServiceRefDeallocate(p->dnssd_ipp_ref); | |
2357 | p->dnssd_ipp_ref = NULL; | |
2358 | p->dnssd_ipp_fd = -1; | |
2359 | } | |
2360 | ||
2361 | cupsdClearString(&p->reg_name); | |
2362 | ||
2363 | if (p->txt_record) | |
2364 | { | |
2365 | /* | |
2366 | * p->txt_record is malloc'd, not _cupsStrAlloc'd... | |
2367 | */ | |
2368 | ||
2369 | free(p->txt_record); | |
2370 | p->txt_record = NULL; | |
2371 | } | |
2372 | } | |
2373 | ||
2374 | ||
2375 | /* | |
2376 | * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the | |
2377 | * TXT record format. | |
2378 | */ | |
2379 | ||
2380 | static char * /* O - TXT record */ | |
2381 | dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */ | |
2382 | char *keyvalue[][2], /* I - Table of key value pairs */ | |
2383 | int count) /* I - Items in table */ | |
2384 | { | |
2385 | int i; /* Looping var */ | |
2386 | int length; /* Length of TXT record */ | |
2387 | int length2; /* Length of value */ | |
2388 | char *txtRecord; /* TXT record buffer */ | |
2389 | char *cursor; /* Looping pointer */ | |
2390 | ||
2391 | ||
2392 | /* | |
2393 | * Calculate the buffer size | |
2394 | */ | |
2395 | ||
2396 | for (length = i = 0; i < count; i++) | |
2397 | length += 1 + strlen(keyvalue[i][0]) + | |
2398 | (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0); | |
2399 | ||
2400 | /* | |
2401 | * Allocate and fill it | |
2402 | */ | |
2403 | ||
2404 | txtRecord = malloc(length); | |
2405 | if (txtRecord) | |
2406 | { | |
2407 | *txt_len = length; | |
2408 | ||
2409 | for (cursor = txtRecord, i = 0; i < count; i++) | |
2410 | { | |
2411 | /* | |
2412 | * Drop in the p-string style length byte followed by the data | |
2413 | */ | |
2414 | ||
2415 | length = strlen(keyvalue[i][0]); | |
2416 | length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0; | |
2417 | ||
2418 | *cursor++ = (unsigned char)(length + length2); | |
2419 | ||
2420 | memcpy(cursor, keyvalue[i][0], length); | |
2421 | cursor += length; | |
2422 | ||
2423 | if (length2) | |
2424 | { | |
2425 | length2 --; | |
2426 | *cursor++ = '='; | |
2427 | memcpy(cursor, keyvalue[i][1], length2); | |
2428 | cursor += length2; | |
2429 | } | |
2430 | } | |
2431 | } | |
2432 | ||
2433 | return (txtRecord); | |
2434 | } | |
2435 | ||
2436 | ||
2437 | /* | |
2438 | * 'dnssdRegisterCallback()' - DNSServiceRegister callback. | |
2439 | */ | |
2440 | ||
2441 | static void | |
2442 | dnssdRegisterCallback( | |
2443 | DNSServiceRef sdRef, /* I - DNS Service reference */ | |
2444 | DNSServiceFlags flags, /* I - Reserved for future use */ | |
2445 | DNSServiceErrorType errorCode, /* I - Error code */ | |
2446 | const char *name, /* I - Service name */ | |
2447 | const char *regtype, /* I - Service type */ | |
2448 | const char *domain, /* I - Domain. ".local" for now */ | |
2449 | void *context) /* I - User-defined context */ | |
2450 | { | |
d9bca400 MS |
2451 | cupsd_printer_t *p = (cupsd_printer_t *)context; |
2452 | /* Current printer */ | |
f7deaa1a | 2453 | |
d9bca400 MS |
2454 | |
2455 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s", | |
2456 | name, regtype, p->name); | |
f7deaa1a | 2457 | |
2458 | if (errorCode) | |
2459 | { | |
2460 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
2461 | "DNSServiceRegister failed with error %d", (int)errorCode); | |
2462 | return; | |
2463 | } | |
d9bca400 MS |
2464 | else if (strcasecmp(name, p->reg_name)) |
2465 | { | |
2466 | cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"", | |
2467 | name, p->name); | |
2468 | ||
2469 | cupsdSetString(&p->reg_name, name); | |
2470 | LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED; | |
2471 | } | |
f7deaa1a | 2472 | } |
2473 | ||
2474 | ||
2475 | /* | |
2476 | * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer | |
2477 | * or update the broadcast contents. | |
2478 | */ | |
2479 | ||
2480 | static void | |
2481 | dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */ | |
2482 | { | |
2483 | DNSServiceErrorType se; /* dnssd errors */ | |
2484 | cupsd_listener_t *lis; /* Current listening socket */ | |
2485 | char *txt_record, /* TXT record buffer */ | |
2486 | *name; /* Service name */ | |
2487 | int txt_len, /* TXT record length */ | |
2488 | port; /* IPP port number */ | |
080811b1 MS |
2489 | char resource[1024], /* Resource path for printer */ |
2490 | str_buffer[1024]; | |
f7deaa1a | 2491 | /* C-string buffer */ |
2492 | const char *computerName; /* Computer name c-string ptr */ | |
2493 | const char *regtype; /* Registration type */ | |
080811b1 MS |
2494 | const char *domain; /* Registration domain */ |
2495 | cupsd_location_t *location, /* Printer location */ | |
2496 | *policy; /* Operation policy for Print-Job */ | |
2497 | unsigned address[4]; /* INADDR_ANY address */ | |
f7deaa1a | 2498 | #ifdef HAVE_COREFOUNDATION_H |
2499 | CFStringRef computerNameRef;/* Computer name CFString */ | |
2500 | CFStringEncoding nameEncoding; /* Computer name encoding */ | |
2501 | CFMutableStringRef shortNameRef; /* Mutable name string */ | |
2502 | CFIndex nameLength; /* Name string length */ | |
2503 | #else | |
2504 | int nameLength; /* Name string length */ | |
2505 | #endif /* HAVE_COREFOUNDATION_H */ | |
2506 | ||
2507 | ||
2508 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name, | |
2509 | !p->dnssd_ipp_ref ? "new" : "update"); | |
2510 | ||
2511 | /* | |
2512 | * If per-printer sharing was just disabled make sure we're not | |
2513 | * registered before returning. | |
2514 | */ | |
2515 | ||
2516 | if (!p->shared) | |
2517 | { | |
2518 | dnssdDeregisterPrinter(p); | |
2519 | return; | |
2520 | } | |
2521 | ||
2522 | /* | |
2523 | * Get the computer name as a c-string... | |
2524 | */ | |
2525 | ||
2526 | #ifdef HAVE_COREFOUNDATION_H | |
2527 | computerName = NULL; | |
2528 | if ((computerNameRef = SCDynamicStoreCopyComputerName(NULL, &nameEncoding))) | |
2529 | if ((computerName = CFStringGetCStringPtr(computerNameRef, | |
2530 | kCFStringEncodingUTF8)) == NULL) | |
2531 | if (CFStringGetCString(computerNameRef, str_buffer, sizeof(str_buffer), | |
2532 | kCFStringEncodingUTF8)) | |
2533 | computerName = str_buffer; | |
2534 | #else | |
2535 | computerName = ServerName; | |
2536 | #endif /* HAVE_COREFOUNDATION_H */ | |
2537 | ||
2538 | /* | |
2539 | * The registered name takes the form of "<printer-info> @ <computer name>"... | |
2540 | */ | |
2541 | ||
2542 | name = NULL; | |
2543 | if (computerName) | |
2544 | cupsdSetStringf(&name, "%s @ %s", | |
2545 | (p->info && strlen(p->info)) ? p->info : p->name, | |
2546 | computerName); | |
2547 | else | |
2548 | cupsdSetString(&name, (p->info && strlen(p->info)) ? p->info : p->name); | |
2549 | ||
2550 | #ifdef HAVE_COREFOUNDATION_H | |
2551 | if (computerNameRef) | |
2552 | CFRelease(computerNameRef); | |
2553 | #endif /* HAVE_COREFOUNDATION_H */ | |
2554 | ||
2555 | /* | |
2556 | * If an existing printer was renamed, unregister it and start over... | |
2557 | */ | |
2558 | ||
2559 | if (p->reg_name && strcmp(p->reg_name, name)) | |
2560 | dnssdDeregisterPrinter(p); | |
2561 | ||
2562 | txt_len = 0; /* anti-compiler-warning-code */ | |
2563 | txt_record = dnssdBuildTxtRecord(&txt_len, p); | |
2564 | ||
2565 | if (!p->dnssd_ipp_ref) | |
2566 | { | |
2567 | /* | |
2568 | * Initial registration... | |
2569 | */ | |
2570 | ||
2571 | cupsdSetString(&p->reg_name, name); | |
2572 | ||
2573 | port = ippPort(); | |
2574 | ||
2575 | for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners); | |
2576 | lis; | |
2577 | lis = (cupsd_listener_t *)cupsArrayNext(Listeners)) | |
2578 | { | |
2579 | if (lis->address.addr.sa_family == AF_INET) | |
2580 | { | |
2581 | port = ntohs(lis->address.ipv4.sin_port); | |
2582 | break; | |
2583 | } | |
2584 | else if (lis->address.addr.sa_family == AF_INET6) | |
2585 | { | |
2586 | port = ntohs(lis->address.ipv6.sin6_port); | |
2587 | break; | |
2588 | } | |
2589 | } | |
2590 | ||
080811b1 MS |
2591 | /* |
2592 | * If 'Allow printing from the Internet' is enabled (i.e. from any address) | |
2593 | * let dnssd decide on the domain, otherwise restrict it to ".local". | |
2594 | */ | |
2595 | ||
2596 | if (p->type & CUPS_PRINTER_CLASS) | |
2597 | snprintf(resource, sizeof(resource), "/classes/%s", p->name); | |
2598 | else | |
2599 | snprintf(resource, sizeof(resource), "/printers/%s", p->name); | |
2600 | ||
2601 | address[0] = address[1] = address[2] = address[3] = 0; | |
2602 | location = cupsdFindBest(resource, HTTP_POST); | |
2603 | policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB); | |
2604 | ||
2605 | if ((location && !cupsdCheckAccess(address, "", 0, location)) || | |
2606 | (policy && !cupsdCheckAccess(address, "", 0, policy))) | |
2607 | domain = "local."; | |
2608 | else | |
2609 | domain = NULL; | |
2610 | ||
f7deaa1a | 2611 | /* |
2612 | * Use the _fax subtype for fax queues... | |
2613 | */ | |
2614 | ||
2615 | regtype = (p->type & CUPS_PRINTER_FAX) ? dnssdIPPFaxRegType : | |
2616 | dnssdIPPRegType; | |
2617 | ||
080811b1 MS |
2618 | |
2619 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
d9bca400 MS |
2620 | "dnssdRegisterPrinter(%s) type, domain is \"%s\", \"%s\"", |
2621 | p->name, regtype, domain ? domain : "(null)"); | |
f7deaa1a | 2622 | |
2623 | se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, name, regtype, | |
080811b1 | 2624 | domain, NULL, htons(port), txt_len, txt_record, |
f7deaa1a | 2625 | dnssdRegisterCallback, p); |
2626 | ||
2627 | /* | |
2628 | * In case the name is too long, try shortening the string one character | |
2629 | * at a time... | |
2630 | */ | |
2631 | ||
2632 | if (se == kDNSServiceErr_BadParam) | |
2633 | { | |
2634 | #ifdef HAVE_COREFOUNDATION_H | |
2635 | if ((shortNameRef = CFStringCreateMutable(NULL, 0)) != NULL) | |
2636 | { | |
2637 | CFStringAppendCString(shortNameRef, name, kCFStringEncodingUTF8); | |
2638 | nameLength = CFStringGetLength(shortNameRef); | |
2639 | ||
2640 | while (se == kDNSServiceErr_BadParam && nameLength > 1) | |
2641 | { | |
2642 | CFStringDelete(shortNameRef, CFRangeMake(--nameLength, 1)); | |
2643 | if (CFStringGetCString(shortNameRef, str_buffer, sizeof(str_buffer), | |
2644 | kCFStringEncodingUTF8)) | |
2645 | { | |
2646 | se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer, | |
2647 | regtype, NULL, NULL, htons(port), | |
2648 | txt_len, txt_record, | |
2649 | dnssdRegisterCallback, p); | |
2650 | } | |
2651 | } | |
2652 | ||
2653 | CFRelease(shortNameRef); | |
2654 | } | |
2655 | #else | |
2656 | nameLength = strlen(name); | |
2657 | while (se == kDNSServiceErr_BadParam && nameLength > 1) | |
2658 | { | |
2659 | name[--nameLength] = '\0'; | |
2660 | se = DNSServiceRegister(&p->dnssd_ipp_ref, 0, 0, str_buffer, regtype, | |
2661 | NULL, NULL, htons(port), txt_len, txt_record, | |
2662 | dnssdRegisterCallback, p); | |
2663 | } | |
2664 | #endif /* HAVE_COREFOUNDATION_H */ | |
2665 | } | |
2666 | ||
2667 | if (se == kDNSServiceErr_NoError) | |
2668 | { | |
2669 | p->dnssd_ipp_fd = DNSServiceRefSockFD(p->dnssd_ipp_ref); | |
2670 | p->txt_record = txt_record; | |
2671 | p->txt_len = txt_len; | |
2672 | txt_record = NULL; | |
2673 | ||
2674 | cupsdAddSelect(p->dnssd_ipp_fd, (cupsd_selfunc_t)cupsdUpdateDNSSDBrowse, | |
2675 | NULL, (void *)p); | |
2676 | } | |
2677 | else | |
2678 | cupsdLogMessage(CUPSD_LOG_WARN, | |
2679 | "DNS-SD registration of \"%s\" failed with %d", | |
2680 | p->name, se); | |
2681 | } | |
2682 | else if (txt_len != p->txt_len || memcmp(txt_record, p->txt_record, txt_len)) | |
2683 | { | |
2684 | /* | |
2685 | * Update the existing registration... | |
2686 | */ | |
2687 | ||
2688 | /* A TTL of 0 means use record's original value (Radar 3176248) */ | |
2689 | se = DNSServiceUpdateRecord(p->dnssd_ipp_ref, NULL, 0, | |
2690 | txt_len, txt_record, 0); | |
2691 | ||
2692 | if (p->txt_record) | |
2693 | free(p->txt_record); | |
2694 | ||
2695 | p->txt_record = txt_record; | |
2696 | p->txt_len = txt_len; | |
2697 | txt_record = NULL; | |
2698 | } | |
2699 | ||
2700 | if (txt_record) | |
2701 | free(txt_record); | |
2702 | ||
2703 | cupsdClearString(&name); | |
2704 | } | |
2705 | #endif /* HAVE_DNSSD */ | |
2706 | ||
2707 | ||
ef416fc2 | 2708 | /* |
e00b005a | 2709 | * 'process_implicit_classes()' - Create/update implicit classes as needed. |
ef416fc2 | 2710 | */ |
2711 | ||
e00b005a | 2712 | static void |
c277e2f8 MS |
2713 | process_implicit_classes( |
2714 | int *write_printcap) /* O - Write printcap file? */ | |
ef416fc2 | 2715 | { |
e00b005a | 2716 | int i; /* Looping var */ |
2717 | int update; /* Update printer attributes? */ | |
2718 | char name[IPP_MAX_NAME], /* Name of printer */ | |
2719 | *hptr; /* Pointer into hostname */ | |
2720 | cupsd_printer_t *p, /* Printer information */ | |
2721 | *pclass, /* Printer class */ | |
2722 | *first; /* First printer in class */ | |
2723 | int offset, /* Offset of name */ | |
2724 | len; /* Length of name */ | |
ef416fc2 | 2725 | |
2726 | ||
e00b005a | 2727 | if (!ImplicitClasses || !Printers) |
2728 | return; | |
ef416fc2 | 2729 | |
e00b005a | 2730 | /* |
2731 | * Loop through all available printers and create classes as needed... | |
2732 | */ | |
2733 | ||
2734 | for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0, | |
2735 | update = 0, pclass = NULL, first = NULL; | |
2736 | p != NULL; | |
2737 | p = (cupsd_printer_t *)cupsArrayNext(Printers)) | |
ef416fc2 | 2738 | { |
2739 | /* | |
e00b005a | 2740 | * Skip implicit classes... |
ef416fc2 | 2741 | */ |
2742 | ||
e00b005a | 2743 | if (p->type & CUPS_PRINTER_IMPLICIT) |
2744 | { | |
2745 | len = 0; | |
2746 | continue; | |
2747 | } | |
ef416fc2 | 2748 | |
e00b005a | 2749 | /* |
2750 | * If len == 0, get the length of this printer name up to the "@" | |
2751 | * sign (if any). | |
2752 | */ | |
ef416fc2 | 2753 | |
e00b005a | 2754 | cupsArraySave(Printers); |
ef416fc2 | 2755 | |
e00b005a | 2756 | if (len > 0 && |
2757 | !strncasecmp(p->name, name + offset, len) && | |
2758 | (p->name[len] == '\0' || p->name[len] == '@')) | |
2759 | { | |
2760 | /* | |
2761 | * We have more than one printer with the same name; see if | |
2762 | * we have a class, and if this printer is a member... | |
2763 | */ | |
ef416fc2 | 2764 | |
e00b005a | 2765 | if (pclass && strcasecmp(pclass->name, name)) |
2766 | { | |
2767 | if (update) | |
2768 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 2769 | |
e00b005a | 2770 | update = 0; |
2771 | pclass = NULL; | |
2772 | } | |
ef416fc2 | 2773 | |
e00b005a | 2774 | if (!pclass && (pclass = cupsdFindDest(name)) == NULL) |
2775 | { | |
2776 | /* | |
2777 | * Need to add the class... | |
2778 | */ | |
ef416fc2 | 2779 | |
e00b005a | 2780 | pclass = cupsdAddPrinter(name); |
2781 | cupsArrayAdd(ImplicitPrinters, pclass); | |
ef416fc2 | 2782 | |
e00b005a | 2783 | pclass->type |= CUPS_PRINTER_IMPLICIT; |
2784 | pclass->accepting = 1; | |
2785 | pclass->state = IPP_PRINTER_IDLE; | |
ef416fc2 | 2786 | |
e00b005a | 2787 | cupsdSetString(&pclass->location, p->location); |
2788 | cupsdSetString(&pclass->info, p->info); | |
ef416fc2 | 2789 | |
7594b224 | 2790 | cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]); |
2791 | cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]); | |
2792 | ||
e00b005a | 2793 | update = 1; |
ef416fc2 | 2794 | |
c277e2f8 MS |
2795 | if (write_printcap) |
2796 | *write_printcap = 1; | |
2797 | ||
89d46774 | 2798 | cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...", |
e00b005a | 2799 | name); |
b423cd4c | 2800 | cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL, |
2801 | "Implicit class \'%s\' added by directory services.", | |
2802 | name); | |
e00b005a | 2803 | } |
ef416fc2 | 2804 | |
e00b005a | 2805 | if (first != NULL) |
2806 | { | |
2807 | for (i = 0; i < pclass->num_printers; i ++) | |
2808 | if (pclass->printers[i] == first) | |
2809 | break; | |
ef416fc2 | 2810 | |
e00b005a | 2811 | if (i >= pclass->num_printers) |
2812 | { | |
2813 | first->in_implicit_class = 1; | |
2814 | cupsdAddPrinterToClass(pclass, first); | |
2815 | } | |
ef416fc2 | 2816 | |
e00b005a | 2817 | first = NULL; |
2818 | } | |
ef416fc2 | 2819 | |
e00b005a | 2820 | for (i = 0; i < pclass->num_printers; i ++) |
2821 | if (pclass->printers[i] == p) | |
2822 | break; | |
ef416fc2 | 2823 | |
e00b005a | 2824 | if (i >= pclass->num_printers) |
2825 | { | |
2826 | p->in_implicit_class = 1; | |
2827 | cupsdAddPrinterToClass(pclass, p); | |
2828 | update = 1; | |
2829 | } | |
2830 | } | |
2831 | else | |
ef416fc2 | 2832 | { |
2833 | /* | |
e00b005a | 2834 | * First time around; just get name length and mark it as first |
2835 | * in the list... | |
ef416fc2 | 2836 | */ |
2837 | ||
e00b005a | 2838 | if ((hptr = strchr(p->name, '@')) != NULL) |
2839 | len = hptr - p->name; | |
2840 | else | |
2841 | len = strlen(p->name); | |
ef416fc2 | 2842 | |
e00b005a | 2843 | strncpy(name, p->name, len); |
2844 | name[len] = '\0'; | |
2845 | offset = 0; | |
ef416fc2 | 2846 | |
e00b005a | 2847 | if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL && |
2848 | !(first->type & CUPS_PRINTER_IMPLICIT)) | |
2849 | { | |
2850 | /* | |
2851 | * Can't use same name as a local printer; add "Any" to the | |
2852 | * front of the name, unless we have explicitly disabled | |
2853 | * the "ImplicitAnyClasses"... | |
2854 | */ | |
ef416fc2 | 2855 | |
e00b005a | 2856 | if (ImplicitAnyClasses && len < (sizeof(name) - 4)) |
2857 | { | |
2858 | /* | |
2859 | * Add "Any" to the class name... | |
2860 | */ | |
2861 | ||
2862 | strcpy(name, "Any"); | |
2863 | strncpy(name + 3, p->name, len); | |
2864 | name[len + 3] = '\0'; | |
2865 | offset = 3; | |
2866 | } | |
2867 | else | |
2868 | { | |
2869 | /* | |
2870 | * Don't create an implicit class if we have a local printer | |
2871 | * with the same name... | |
2872 | */ | |
2873 | ||
2874 | len = 0; | |
2875 | cupsArrayRestore(Printers); | |
2876 | continue; | |
2877 | } | |
2878 | } | |
2879 | ||
2880 | first = p; | |
ef416fc2 | 2881 | } |
2882 | ||
e00b005a | 2883 | cupsArrayRestore(Printers); |
2884 | } | |
ef416fc2 | 2885 | |
e00b005a | 2886 | /* |
2887 | * Update the last printer class as needed... | |
2888 | */ | |
ef416fc2 | 2889 | |
e00b005a | 2890 | if (pclass && update) |
2891 | cupsdSetPrinterAttrs(pclass); | |
ef416fc2 | 2892 | } |
2893 | ||
2894 | ||
e1d6a774 | 2895 | /* |
2896 | * 'send_cups_browse()' - Send new browsing information using the CUPS | |
7594b224 | 2897 | * protocol. |
e1d6a774 | 2898 | */ |
2899 | ||
2900 | static void | |
2901 | send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */ | |
2902 | { | |
2903 | int i; /* Looping var */ | |
2904 | cups_ptype_t type; /* Printer type */ | |
2905 | cupsd_dirsvc_addr_t *b; /* Browse address */ | |
2906 | int bytes; /* Length of packet */ | |
2907 | char packet[1453], /* Browse data packet */ | |
2908 | uri[1024], /* Printer URI */ | |
2909 | location[1024], /* printer-location */ | |
2910 | info[1024], /* printer-info */ | |
2911 | make_model[1024]; | |
2912 | /* printer-make-and-model */ | |
2913 | cupsd_netif_t *iface; /* Network interface */ | |
2914 | ||
2915 | ||
2916 | /* | |
2917 | * Figure out the printer type value... | |
2918 | */ | |
2919 | ||
2920 | type = p->type | CUPS_PRINTER_REMOTE; | |
2921 | ||
2922 | if (!p->accepting) | |
2923 | type |= CUPS_PRINTER_REJECTING; | |
2924 | ||
2925 | if (p == DefaultPrinter) | |
2926 | type |= CUPS_PRINTER_DEFAULT; | |
2927 | ||
2928 | /* | |
2929 | * Remove quotes from printer-info, printer-location, and | |
2930 | * printer-make-and-model attributes... | |
2931 | */ | |
2932 | ||
2933 | dequote(location, p->location, sizeof(location)); | |
2934 | dequote(info, p->info, sizeof(info)); | |
d6ae789d | 2935 | |
2936 | if (p->make_model) | |
2937 | dequote(make_model, p->make_model, sizeof(make_model)); | |
2938 | else if (p->type & CUPS_PRINTER_CLASS) | |
2939 | { | |
2940 | if (p->num_printers > 0 && p->printers[0]->make_model) | |
2941 | strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model)); | |
2942 | else | |
2943 | strlcpy(make_model, "Local Printer Class", sizeof(make_model)); | |
2944 | } | |
2945 | else if (p->raw) | |
2946 | strlcpy(make_model, "Local Raw Printer", sizeof(make_model)); | |
2947 | else | |
2948 | strlcpy(make_model, "Local System V Printer", sizeof(make_model)); | |
e1d6a774 | 2949 | |
2950 | /* | |
2951 | * Send a packet to each browse address... | |
2952 | */ | |
2953 | ||
2954 | for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++) | |
2955 | if (b->iface[0]) | |
2956 | { | |
2957 | /* | |
2958 | * Send the browse packet to one or more interfaces... | |
2959 | */ | |
2960 | ||
2961 | if (!strcmp(b->iface, "*")) | |
2962 | { | |
2963 | /* | |
2964 | * Send to all local interfaces... | |
2965 | */ | |
2966 | ||
2967 | cupsdNetIFUpdate(); | |
2968 | ||
2969 | for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList); | |
2970 | iface; | |
2971 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList)) | |
2972 | { | |
2973 | /* | |
2974 | * Only send to local, IPv4 interfaces... | |
2975 | */ | |
2976 | ||
2977 | if (!iface->is_local || !iface->port || | |
2978 | iface->address.addr.sa_family != AF_INET) | |
2979 | continue; | |
2980 | ||
2981 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
2982 | iface->hostname, iface->port, | |
ed486911 | 2983 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : |
e1d6a774 | 2984 | "/printers/%s", |
2985 | p->name); | |
2986 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
2987 | type, p->state, uri, location, info, make_model, | |
2988 | p->browse_attrs ? p->browse_attrs : ""); | |
2989 | ||
2990 | bytes = strlen(packet); | |
2991 | ||
2992 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
2993 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
2994 | iface->name, packet); | |
2995 | ||
2996 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); | |
2997 | ||
2998 | sendto(BrowseSocket, packet, bytes, 0, | |
2999 | (struct sockaddr *)&(iface->broadcast), | |
d09495fa | 3000 | httpAddrLength(&(iface->broadcast))); |
e1d6a774 | 3001 | } |
3002 | } | |
3003 | else if ((iface = cupsdNetIFFind(b->iface)) != NULL) | |
3004 | { | |
3005 | /* | |
3006 | * Send to the named interface using the IPv4 address... | |
3007 | */ | |
3008 | ||
3009 | while (iface) | |
3010 | if (strcmp(b->iface, iface->name)) | |
3011 | { | |
3012 | iface = NULL; | |
3013 | break; | |
3014 | } | |
3015 | else if (iface->address.addr.sa_family == AF_INET && iface->port) | |
3016 | break; | |
3017 | else | |
3018 | iface = (cupsd_netif_t *)cupsArrayNext(NetIFList); | |
3019 | ||
3020 | if (iface) | |
3021 | { | |
3022 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
3023 | iface->hostname, iface->port, | |
b94498cf | 3024 | (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" : |
e1d6a774 | 3025 | "/printers/%s", |
3026 | p->name); | |
3027 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
3028 | type, p->state, uri, location, info, make_model, | |
3029 | p->browse_attrs ? p->browse_attrs : ""); | |
3030 | ||
3031 | bytes = strlen(packet); | |
3032 | ||
3033 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3034 | "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes, | |
3035 | iface->name, packet); | |
3036 | ||
3037 | iface->broadcast.ipv4.sin_port = htons(BrowsePort); | |
3038 | ||
3039 | sendto(BrowseSocket, packet, bytes, 0, | |
3040 | (struct sockaddr *)&(iface->broadcast), | |
d09495fa | 3041 | httpAddrLength(&(iface->broadcast))); |
e1d6a774 | 3042 | } |
3043 | } | |
3044 | } | |
3045 | else | |
3046 | { | |
3047 | /* | |
3048 | * Send the browse packet to the indicated address using | |
3049 | * the default server name... | |
3050 | */ | |
3051 | ||
3052 | snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n", | |
3053 | type, p->state, p->uri, location, info, make_model, | |
3054 | p->browse_attrs ? p->browse_attrs : ""); | |
3055 | ||
3056 | bytes = strlen(packet); | |
3057 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3058 | "cupsdSendBrowseList: (%d bytes) %s", bytes, packet); | |
3059 | ||
3060 | if (sendto(BrowseSocket, packet, bytes, 0, | |
3061 | (struct sockaddr *)&(b->to), | |
d09495fa | 3062 | httpAddrLength(&(b->to))) <= 0) |
e1d6a774 | 3063 | { |
3064 | /* | |
3065 | * Unable to send browse packet, so remove this address from the | |
3066 | * list... | |
3067 | */ | |
3068 | ||
3069 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3070 | "cupsdSendBrowseList: sendto failed for browser " | |
3071 | "%d - %s.", | |
3072 | (int)(b - Browsers + 1), strerror(errno)); | |
3073 | ||
3074 | if (i > 1) | |
3075 | memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t)); | |
3076 | ||
3077 | b --; | |
3078 | NumBrowsers --; | |
3079 | } | |
3080 | } | |
3081 | } | |
3082 | ||
3083 | ||
3084 | #ifdef HAVE_OPENLDAP | |
3085 | /* | |
3086 | * 'send_ldap_browse()' - Send LDAP printer registrations. | |
3087 | */ | |
3088 | ||
3089 | static void | |
3090 | send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */ | |
3091 | { | |
3092 | int i; /* Looping var... */ | |
3093 | LDAPMod mods[7]; /* The 7 attributes we will be adding */ | |
3094 | LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */ | |
3095 | LDAPMessage *res; /* Search result token */ | |
3096 | char *cn_value[2], /* Change records */ | |
3097 | *uri[2], | |
3098 | *info[2], | |
3099 | *location[2], | |
3100 | *make_model[2], | |
3101 | *type[2], | |
3102 | typestring[255], /* String to hold printer-type */ | |
3103 | filter[256], /* Search filter for possible UPDATEs */ | |
3104 | dn[1024]; /* DN of the printer we are adding */ | |
3105 | int rc; /* LDAP status */ | |
3106 | static const char * const objectClass_values[] = | |
3107 | { /* The 3 objectClass's we use in */ | |
3108 | "top", /* our LDAP entries */ | |
3109 | "device", | |
3110 | "cupsPrinter", | |
3111 | NULL | |
3112 | }; | |
3113 | ||
3114 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name); | |
3115 | ||
3116 | /* | |
3117 | * Everything in ldap is ** so we fudge around it... | |
3118 | */ | |
3119 | ||
3120 | sprintf(typestring, "%u", p->type); | |
3121 | ||
ed486911 | 3122 | cn_value[0] = p->name; |
e1d6a774 | 3123 | cn_value[1] = NULL; |
ed486911 | 3124 | info[0] = p->info ? p->info : "Unknown"; |
e1d6a774 | 3125 | info[1] = NULL; |
ed486911 | 3126 | location[0] = p->location ? p->location : "Unknown"; |
e1d6a774 | 3127 | location[1] = NULL; |
ed486911 | 3128 | make_model[0] = p->make_model ? p->make_model : "Unknown"; |
e1d6a774 | 3129 | make_model[1] = NULL; |
3130 | type[0] = typestring; | |
3131 | type[1] = NULL; | |
3132 | uri[0] = p->uri; | |
3133 | uri[1] = NULL; | |
3134 | ||
3135 | snprintf(filter, sizeof(filter), | |
ed486911 | 3136 | "(&(objectclass=cupsPrinter)(printerURI=%s))", p->uri); |
e1d6a774 | 3137 | |
3138 | ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE, | |
3139 | filter, (char **)ldap_attrs, 0, &res); | |
3140 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: Searching \"%s\"", | |
3141 | filter); | |
3142 | ||
3143 | mods[0].mod_type = "cn"; | |
3144 | mods[0].mod_values = cn_value; | |
3145 | mods[1].mod_type = "printerDescription"; | |
3146 | mods[1].mod_values = info; | |
3147 | mods[2].mod_type = "printerURI"; | |
3148 | mods[2].mod_values = uri; | |
3149 | mods[3].mod_type = "printerLocation"; | |
3150 | mods[3].mod_values = location; | |
3151 | mods[4].mod_type = "printerMakeAndModel"; | |
3152 | mods[4].mod_values = make_model; | |
3153 | mods[5].mod_type = "printerType"; | |
3154 | mods[5].mod_values = type; | |
3155 | mods[6].mod_type = "objectClass"; | |
3156 | mods[6].mod_values = (char **)objectClass_values; | |
3157 | ||
ed486911 | 3158 | snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->name, BrowseLDAPDN); |
e1d6a774 | 3159 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn); |
3160 | ||
3161 | if (ldap_count_entries(BrowseLDAPHandle, res) > 0) | |
3162 | { | |
3163 | /* | |
3164 | * Printer has already been registered, modify the current | |
3165 | * registration... | |
3166 | */ | |
3167 | ||
3168 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3169 | "send_ldap_browse: Replacing entry..."); | |
3170 | ||
3171 | for (i = 0; i < 7; i ++) | |
3172 | { | |
3173 | pmods[i] = mods + i; | |
3174 | pmods[i]->mod_op = LDAP_MOD_REPLACE; | |
3175 | } | |
3176 | pmods[i] = NULL; | |
3177 | ||
3178 | if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) | |
3179 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3180 | "LDAP modify for %s failed with status %d: %s", | |
3181 | p->name, rc, ldap_err2string(rc)); | |
3182 | } | |
3183 | else | |
3184 | { | |
3185 | /* | |
ed486911 | 3186 | * Printer has never been registered, add the current |
e1d6a774 | 3187 | * registration... |
3188 | */ | |
3189 | ||
3190 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3191 | "send_ldap_browse: Adding entry..."); | |
3192 | ||
3193 | for (i = 0; i < 7; i ++) | |
3194 | { | |
3195 | pmods[i] = mods + i; | |
ed486911 | 3196 | pmods[i]->mod_op = LDAP_MOD_ADD; |
e1d6a774 | 3197 | } |
3198 | pmods[i] = NULL; | |
3199 | ||
ed486911 | 3200 | if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS) |
e1d6a774 | 3201 | cupsdLogMessage(CUPSD_LOG_ERROR, |
3202 | "LDAP add for %s failed with status %d: %s", | |
3203 | p->name, rc, ldap_err2string(rc)); | |
3204 | } | |
3205 | } | |
3206 | #endif /* HAVE_OPENLDAP */ | |
3207 | ||
3208 | ||
3209 | #ifdef HAVE_LIBSLP | |
3210 | /* | |
3211 | * 'send_slp_browse()' - Register the specified printer with SLP. | |
3212 | */ | |
3213 | ||
3214 | static void | |
3215 | send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */ | |
3216 | { | |
3217 | char srvurl[HTTP_MAX_URI], /* Printer service URI */ | |
3218 | attrs[8192], /* Printer attributes */ | |
3219 | finishings[1024], /* Finishings to support */ | |
3220 | make_model[IPP_MAX_NAME * 2], | |
3221 | /* Make and model, quoted */ | |
3222 | location[IPP_MAX_NAME * 2], | |
3223 | /* Location, quoted */ | |
3224 | info[IPP_MAX_NAME * 2], /* Info, quoted */ | |
3225 | *src, /* Pointer to original string */ | |
3226 | *dst; /* Pointer to destination string */ | |
3227 | ipp_attribute_t *authentication; /* uri-authentication-supported value */ | |
3228 | SLPError error; /* SLP error, if any */ | |
3229 | ||
3230 | ||
3231 | cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p, | |
3232 | p->name); | |
3233 | ||
3234 | /* | |
3235 | * Make the SLP service URL that conforms to the IANA | |
3236 | * 'printer:' template. | |
3237 | */ | |
3238 | ||
3239 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); | |
3240 | ||
3241 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl); | |
3242 | ||
3243 | /* | |
3244 | * Figure out the finishings string... | |
3245 | */ | |
3246 | ||
3247 | if (p->type & CUPS_PRINTER_STAPLE) | |
3248 | strcpy(finishings, "staple"); | |
3249 | else | |
3250 | finishings[0] = '\0'; | |
3251 | ||
3252 | if (p->type & CUPS_PRINTER_BIND) | |
3253 | { | |
3254 | if (finishings[0]) | |
3255 | strlcat(finishings, ",bind", sizeof(finishings)); | |
3256 | else | |
3257 | strcpy(finishings, "bind"); | |
3258 | } | |
3259 | ||
3260 | if (p->type & CUPS_PRINTER_PUNCH) | |
3261 | { | |
3262 | if (finishings[0]) | |
3263 | strlcat(finishings, ",punch", sizeof(finishings)); | |
3264 | else | |
3265 | strcpy(finishings, "punch"); | |
3266 | } | |
3267 | ||
3268 | if (p->type & CUPS_PRINTER_COVER) | |
3269 | { | |
3270 | if (finishings[0]) | |
3271 | strlcat(finishings, ",cover", sizeof(finishings)); | |
3272 | else | |
3273 | strcpy(finishings, "cover"); | |
3274 | } | |
3275 | ||
3276 | if (p->type & CUPS_PRINTER_SORT) | |
3277 | { | |
3278 | if (finishings[0]) | |
3279 | strlcat(finishings, ",sort", sizeof(finishings)); | |
3280 | else | |
3281 | strcpy(finishings, "sort"); | |
3282 | } | |
3283 | ||
3284 | if (!finishings[0]) | |
3285 | strcpy(finishings, "none"); | |
3286 | ||
3287 | /* | |
3288 | * Quote any commas in the make and model, location, and info strings... | |
3289 | */ | |
3290 | ||
3291 | for (src = p->make_model, dst = make_model; | |
3292 | src && *src && dst < (make_model + sizeof(make_model) - 2);) | |
3293 | { | |
3294 | if (*src == ',' || *src == '\\' || *src == ')') | |
3295 | *dst++ = '\\'; | |
3296 | ||
3297 | *dst++ = *src++; | |
3298 | } | |
3299 | ||
3300 | *dst = '\0'; | |
3301 | ||
3302 | if (!make_model[0]) | |
3303 | strcpy(make_model, "Unknown"); | |
3304 | ||
3305 | for (src = p->location, dst = location; | |
3306 | src && *src && dst < (location + sizeof(location) - 2);) | |
3307 | { | |
3308 | if (*src == ',' || *src == '\\' || *src == ')') | |
3309 | *dst++ = '\\'; | |
3310 | ||
3311 | *dst++ = *src++; | |
3312 | } | |
3313 | ||
3314 | *dst = '\0'; | |
3315 | ||
3316 | if (!location[0]) | |
3317 | strcpy(location, "Unknown"); | |
3318 | ||
3319 | for (src = p->info, dst = info; | |
3320 | src && *src && dst < (info + sizeof(info) - 2);) | |
3321 | { | |
3322 | if (*src == ',' || *src == '\\' || *src == ')') | |
3323 | *dst++ = '\\'; | |
3324 | ||
3325 | *dst++ = *src++; | |
3326 | } | |
3327 | ||
3328 | *dst = '\0'; | |
3329 | ||
3330 | if (!info[0]) | |
3331 | strcpy(info, "Unknown"); | |
3332 | ||
3333 | /* | |
3334 | * Get the authentication value... | |
3335 | */ | |
3336 | ||
3337 | authentication = ippFindAttribute(p->attrs, "uri-authentication-supported", | |
3338 | IPP_TAG_KEYWORD); | |
3339 | ||
3340 | /* | |
3341 | * Make the SLP attribute string list that conforms to | |
3342 | * the IANA 'printer:' template. | |
3343 | */ | |
3344 | ||
3345 | snprintf(attrs, sizeof(attrs), | |
3346 | "(printer-uri-supported=%s)," | |
3347 | "(uri-authentication-supported=%s>)," | |
3348 | #ifdef HAVE_SSL | |
3349 | "(uri-security-supported=tls>)," | |
3350 | #else | |
3351 | "(uri-security-supported=none>)," | |
3352 | #endif /* HAVE_SSL */ | |
3353 | "(printer-name=%s)," | |
3354 | "(printer-location=%s)," | |
3355 | "(printer-info=%s)," | |
3356 | "(printer-more-info=%s)," | |
3357 | "(printer-make-and-model=%s)," | |
3358 | "(printer-type=%d)," | |
3359 | "(charset-supported=utf-8)," | |
3360 | "(natural-language-configured=%s)," | |
3361 | "(natural-language-supported=de,en,es,fr,it)," | |
3362 | "(color-supported=%s)," | |
3363 | "(finishings-supported=%s)," | |
3364 | "(sides-supported=one-sided%s)," | |
3365 | "(multiple-document-jobs-supported=true)" | |
3366 | "(ipp-versions-supported=1.0,1.1)", | |
3367 | p->uri, authentication->values[0].string.text, p->name, location, | |
3368 | info, p->uri, make_model, p->type, DefaultLanguage, | |
3369 | p->type & CUPS_PRINTER_COLOR ? "true" : "false", | |
3370 | finishings, | |
3371 | p->type & CUPS_PRINTER_DUPLEX ? | |
3372 | ",two-sided-long-edge,two-sided-short-edge" : ""); | |
3373 | ||
3374 | cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs); | |
3375 | ||
3376 | /* | |
3377 | * Register the printer with the SLP server... | |
3378 | */ | |
3379 | ||
3380 | error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout, | |
3381 | SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0); | |
3382 | ||
3383 | if (error != SLP_OK) | |
3384 | cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name, | |
3385 | error); | |
3386 | } | |
3387 | ||
3388 | ||
ef416fc2 | 3389 | /* |
3390 | * 'slp_attr_callback()' - SLP attribute callback | |
3391 | */ | |
3392 | ||
3393 | static SLPBoolean /* O - SLP_TRUE for success */ | |
3394 | slp_attr_callback( | |
3395 | SLPHandle hslp, /* I - SLP handle */ | |
3396 | const char *attrlist, /* I - Attribute list */ | |
3397 | SLPError errcode, /* I - Parsing status for this attr */ | |
3398 | void *cookie) /* I - Current printer */ | |
3399 | { | |
b423cd4c | 3400 | char *tmp = 0; /* Temporary string */ |
ef416fc2 | 3401 | cupsd_printer_t *p = (cupsd_printer_t*)cookie; |
b423cd4c | 3402 | /* Current printer */ |
ef416fc2 | 3403 | |
3404 | ||
b423cd4c | 3405 | (void)hslp; /* anti-compiler-warning-code */ |
ef416fc2 | 3406 | |
3407 | /* | |
3408 | * Bail if there was an error | |
3409 | */ | |
3410 | ||
3411 | if (errcode != SLP_OK) | |
3412 | return (SLP_TRUE); | |
3413 | ||
3414 | /* | |
3415 | * Parse the attrlist to obtain things needed to build CUPS browse packet | |
3416 | */ | |
3417 | ||
3418 | memset(p, 0, sizeof(cupsd_printer_t)); | |
3419 | ||
ef416fc2 | 3420 | if (slp_get_attr(attrlist, "(printer-location=", &(p->location))) |
3421 | return (SLP_FALSE); | |
3422 | if (slp_get_attr(attrlist, "(printer-info=", &(p->info))) | |
3423 | return (SLP_FALSE); | |
3424 | if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model))) | |
3425 | return (SLP_FALSE); | |
b423cd4c | 3426 | if (!slp_get_attr(attrlist, "(printer-type=", &tmp)) |
3427 | p->type = atoi(tmp); | |
3428 | else | |
3429 | p->type = CUPS_PRINTER_REMOTE; | |
ef416fc2 | 3430 | |
3431 | cupsdClearString(&tmp); | |
3432 | ||
3433 | return (SLP_TRUE); | |
3434 | } | |
3435 | ||
3436 | ||
3437 | /* | |
3438 | * 'slp_dereg_printer()' - SLPDereg() the specified printer | |
3439 | */ | |
3440 | ||
3441 | static void | |
3442 | slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */ | |
3443 | { | |
3444 | char srvurl[HTTP_MAX_URI]; /* Printer service URI */ | |
3445 | ||
3446 | ||
3447 | cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name); | |
3448 | ||
3449 | if (!(p->type & CUPS_PRINTER_REMOTE)) | |
3450 | { | |
3451 | /* | |
3452 | * Make the SLP service URL that conforms to the IANA | |
3453 | * 'printer:' template. | |
3454 | */ | |
3455 | ||
3456 | snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri); | |
3457 | ||
3458 | /* | |
3459 | * Deregister the printer... | |
3460 | */ | |
3461 | ||
3462 | SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0); | |
3463 | } | |
3464 | } | |
3465 | ||
3466 | ||
3467 | /* | |
3468 | * 'slp_get_attr()' - Get an attribute from an SLP registration. | |
3469 | */ | |
3470 | ||
3471 | static int /* O - 0 on success */ | |
3472 | slp_get_attr(const char *attrlist, /* I - Attribute list string */ | |
3473 | const char *tag, /* I - Name of attribute */ | |
3474 | char **valbuf) /* O - Value */ | |
3475 | { | |
3476 | char *ptr1, /* Pointer into string */ | |
3477 | *ptr2; /* ... */ | |
3478 | ||
3479 | ||
3480 | cupsdClearString(valbuf); | |
3481 | ||
3482 | if ((ptr1 = strstr(attrlist, tag)) != NULL) | |
3483 | { | |
3484 | ptr1 += strlen(tag); | |
3485 | ||
3486 | if ((ptr2 = strchr(ptr1,')')) != NULL) | |
3487 | { | |
3488 | /* | |
3489 | * Copy the value... | |
3490 | */ | |
3491 | ||
3492 | *valbuf = calloc(ptr2 - ptr1 + 1, 1); | |
3493 | strncpy(*valbuf, ptr1, ptr2 - ptr1); | |
3494 | ||
3495 | /* | |
3496 | * Dequote the value... | |
3497 | */ | |
3498 | ||
3499 | for (ptr1 = *valbuf; *ptr1; ptr1 ++) | |
3500 | if (*ptr1 == '\\' && ptr1[1]) | |
3501 | _cups_strcpy(ptr1, ptr1 + 1); | |
3502 | ||
3503 | return (0); | |
3504 | } | |
3505 | } | |
3506 | ||
3507 | return (-1); | |
3508 | } | |
3509 | ||
3510 | ||
3511 | /* | |
3512 | * 'slp_reg_callback()' - Empty SLPRegReport. | |
3513 | */ | |
3514 | ||
3515 | static void | |
3516 | slp_reg_callback(SLPHandle hslp, /* I - SLP handle */ | |
3517 | SLPError errcode, /* I - Error code, if any */ | |
3518 | void *cookie) /* I - App data */ | |
3519 | { | |
3520 | (void)hslp; | |
3521 | (void)errcode; | |
3522 | (void)cookie; | |
3523 | ||
3524 | return; | |
3525 | } | |
3526 | ||
3527 | ||
3528 | /* | |
3529 | * 'slp_url_callback()' - SLP service url callback | |
3530 | */ | |
3531 | ||
3532 | static SLPBoolean /* O - TRUE = OK, FALSE = error */ | |
3533 | slp_url_callback( | |
3534 | SLPHandle hslp, /* I - SLP handle */ | |
3535 | const char *srvurl, /* I - URL of service */ | |
3536 | unsigned short lifetime, /* I - Life of service */ | |
3537 | SLPError errcode, /* I - Existing error code */ | |
3538 | void *cookie) /* I - Pointer to service list */ | |
3539 | { | |
3540 | slpsrvurl_t *s, /* New service entry */ | |
3541 | **head; /* Pointer to head of entry */ | |
3542 | ||
3543 | ||
3544 | /* | |
3545 | * Let the compiler know we won't be using these vars... | |
3546 | */ | |
3547 | ||
3548 | (void)hslp; | |
3549 | (void)lifetime; | |
3550 | ||
3551 | /* | |
3552 | * Bail if there was an error | |
3553 | */ | |
3554 | ||
3555 | if (errcode != SLP_OK) | |
3556 | return (SLP_TRUE); | |
3557 | ||
3558 | /* | |
3559 | * Grab the head of the list... | |
3560 | */ | |
3561 | ||
3562 | head = (slpsrvurl_t**)cookie; | |
3563 | ||
3564 | /* | |
3565 | * Allocate a *temporary* slpsrvurl_t to hold this entry. | |
3566 | */ | |
3567 | ||
3568 | if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL) | |
3569 | return (SLP_FALSE); | |
3570 | ||
3571 | /* | |
3572 | * Copy the SLP service URL... | |
3573 | */ | |
3574 | ||
3575 | strlcpy(s->url, srvurl, sizeof(s->url)); | |
3576 | ||
3577 | /* | |
3578 | * Link the SLP service URL into the head of the list | |
3579 | */ | |
3580 | ||
3581 | if (*head) | |
3582 | s->next = *head; | |
3583 | ||
3584 | *head = s; | |
3585 | ||
3586 | return (SLP_TRUE); | |
3587 | } | |
3588 | #endif /* HAVE_LIBSLP */ | |
3589 | ||
3590 | ||
3591 | /* | |
f899b121 | 3592 | * 'update_cups_browse()' - Update the browse lists using the CUPS protocol. |
3593 | */ | |
3594 | ||
3595 | static void | |
3596 | update_cups_browse(void) | |
3597 | { | |
3598 | int i; /* Looping var */ | |
3599 | int auth; /* Authorization status */ | |
3600 | int len; /* Length of name string */ | |
3601 | int bytes; /* Number of bytes left */ | |
3602 | char packet[1541], /* Broadcast packet */ | |
3603 | *pptr; /* Pointer into packet */ | |
3604 | socklen_t srclen; /* Length of source address */ | |
3605 | http_addr_t srcaddr; /* Source address */ | |
3606 | char srcname[1024]; /* Source hostname */ | |
3607 | unsigned address[4]; /* Source address */ | |
3608 | unsigned type; /* Printer type */ | |
3609 | unsigned state; /* Printer state */ | |
3610 | char uri[HTTP_MAX_URI], /* Printer URI */ | |
3611 | host[HTTP_MAX_URI], /* Host portion of URI */ | |
3612 | resource[HTTP_MAX_URI], /* Resource portion of URI */ | |
3613 | info[IPP_MAX_NAME], /* Information string */ | |
3614 | location[IPP_MAX_NAME], /* Location string */ | |
3615 | make_model[IPP_MAX_NAME];/* Make and model string */ | |
3616 | int num_attrs; /* Number of attributes */ | |
3617 | cups_option_t *attrs; /* Attributes */ | |
3618 | ||
3619 | ||
3620 | /* | |
3621 | * Read a packet from the browse socket... | |
3622 | */ | |
3623 | ||
3624 | srclen = sizeof(srcaddr); | |
3625 | if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0, | |
3626 | (struct sockaddr *)&srcaddr, &srclen)) < 0) | |
3627 | { | |
3628 | /* | |
3629 | * "Connection refused" is returned under Linux if the destination port | |
3630 | * or address is unreachable from a previous sendto(); check for the | |
3631 | * error here and ignore it for now... | |
3632 | */ | |
3633 | ||
3634 | if (errno != ECONNREFUSED && errno != EAGAIN) | |
3635 | { | |
3636 | cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.", | |
3637 | strerror(errno)); | |
3638 | cupsdLogMessage(CUPSD_LOG_ERROR, "Browsing turned off."); | |
3639 | ||
3640 | cupsdStopBrowsing(); | |
3641 | Browsing = 0; | |
3642 | } | |
3643 | ||
3644 | return; | |
3645 | } | |
3646 | ||
3647 | packet[bytes] = '\0'; | |
3648 | ||
3649 | /* | |
3650 | * If we're about to sleep, ignore incoming browse packets. | |
3651 | */ | |
3652 | ||
3653 | if (Sleeping) | |
3654 | return; | |
3655 | ||
3656 | /* | |
3657 | * Figure out where it came from... | |
3658 | */ | |
3659 | ||
3660 | #ifdef AF_INET6 | |
3661 | if (srcaddr.addr.sa_family == AF_INET6) | |
3662 | { | |
3663 | address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]); | |
3664 | address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]); | |
3665 | address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]); | |
3666 | address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]); | |
3667 | } | |
3668 | else | |
3669 | #endif /* AF_INET6 */ | |
3670 | { | |
3671 | address[0] = 0; | |
3672 | address[1] = 0; | |
3673 | address[2] = 0; | |
3674 | address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr); | |
3675 | } | |
3676 | ||
3677 | if (HostNameLookups) | |
3678 | httpAddrLookup(&srcaddr, srcname, sizeof(srcname)); | |
3679 | else | |
3680 | httpAddrString(&srcaddr, srcname, sizeof(srcname)); | |
3681 | ||
3682 | len = strlen(srcname); | |
3683 | ||
3684 | /* | |
3685 | * Do ACL stuff... | |
3686 | */ | |
3687 | ||
3688 | if (BrowseACL) | |
3689 | { | |
3690 | if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost")) | |
3691 | { | |
3692 | /* | |
3693 | * Access from localhost (127.0.0.1) is always allowed... | |
3694 | */ | |
3695 | ||
5bd77a73 | 3696 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 3697 | } |
3698 | else | |
3699 | { | |
3700 | /* | |
3701 | * Do authorization checks on the domain/address... | |
3702 | */ | |
3703 | ||
3704 | switch (BrowseACL->order_type) | |
3705 | { | |
3706 | default : | |
5bd77a73 | 3707 | auth = CUPSD_AUTH_DENY; /* anti-compiler-warning-code */ |
f899b121 | 3708 | break; |
3709 | ||
5bd77a73 MS |
3710 | case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */ |
3711 | auth = CUPSD_AUTH_ALLOW; | |
f899b121 | 3712 | |
3713 | if (cupsdCheckAuth(address, srcname, len, | |
3714 | BrowseACL->num_deny, BrowseACL->deny)) | |
5bd77a73 | 3715 | auth = CUPSD_AUTH_DENY; |
f899b121 | 3716 | |
3717 | if (cupsdCheckAuth(address, srcname, len, | |
3718 | BrowseACL->num_allow, BrowseACL->allow)) | |
5bd77a73 | 3719 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 3720 | break; |
3721 | ||
5bd77a73 MS |
3722 | case CUPSD_AUTH_DENY : /* Order Allow,Deny */ |
3723 | auth = CUPSD_AUTH_DENY; | |
f899b121 | 3724 | |
3725 | if (cupsdCheckAuth(address, srcname, len, | |
3726 | BrowseACL->num_allow, BrowseACL->allow)) | |
5bd77a73 | 3727 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 3728 | |
3729 | if (cupsdCheckAuth(address, srcname, len, | |
3730 | BrowseACL->num_deny, BrowseACL->deny)) | |
5bd77a73 | 3731 | auth = CUPSD_AUTH_DENY; |
f899b121 | 3732 | break; |
3733 | } | |
3734 | } | |
3735 | } | |
3736 | else | |
5bd77a73 | 3737 | auth = CUPSD_AUTH_ALLOW; |
f899b121 | 3738 | |
5bd77a73 | 3739 | if (auth == CUPSD_AUTH_DENY) |
f899b121 | 3740 | { |
3741 | cupsdLogMessage(CUPSD_LOG_DEBUG, | |
3742 | "update_cups_browse: Refused %d bytes from %s", bytes, | |
3743 | srcname); | |
3744 | return; | |
3745 | } | |
3746 | ||
3747 | cupsdLogMessage(CUPSD_LOG_DEBUG2, | |
3748 | "update_cups_browse: (%d bytes from %s) %s", bytes, | |
3749 | srcname, packet); | |
3750 | ||
3751 | /* | |
3752 | * Parse packet... | |
3753 | */ | |
3754 | ||
3755 | if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3) | |
3756 | { | |
3757 | cupsdLogMessage(CUPSD_LOG_WARN, | |
3758 | "update_cups_browse: Garbled browse packet - %s", packet); | |
3759 | return; | |
3760 | } | |
3761 | ||
3762 | strcpy(location, "Location Unknown"); | |
3763 | strcpy(info, "No Information Available"); | |
3764 | make_model[0] = '\0'; | |
3765 | num_attrs = 0; | |
3766 | attrs = NULL; | |
3767 | ||
3768 | if ((pptr = strchr(packet, '\"')) != NULL) | |
3769 | { | |
3770 | /* | |
3771 | * Have extended information; can't use sscanf for it because not all | |
3772 | * sscanf's allow empty strings with %[^\"]... | |
3773 | */ | |
3774 | ||
3775 | for (i = 0, pptr ++; | |
3776 | i < (sizeof(location) - 1) && *pptr && *pptr != '\"'; | |
3777 | i ++, pptr ++) | |
3778 | location[i] = *pptr; | |
3779 | ||
3780 | if (i) | |
3781 | location[i] = '\0'; | |
3782 | ||
3783 | if (*pptr == '\"') | |
3784 | pptr ++; | |
3785 | ||
3786 | while (*pptr && isspace(*pptr & 255)) | |
3787 | pptr ++; | |
3788 | ||
3789 | if (*pptr == '\"') | |
3790 | { | |
3791 | for (i = 0, pptr ++; | |
3792 | i < (sizeof(info) - 1) && *pptr && *pptr != '\"'; | |
3793 | i ++, pptr ++) | |
3794 | info[i] = *pptr; | |
3795 | ||
3796 | info[i] = '\0'; | |
3797 | ||
3798 | if (*pptr == '\"') | |
3799 | pptr ++; | |
3800 | ||
3801 | while (*pptr && isspace(*pptr & 255)) | |
3802 | pptr ++; | |
3803 | ||
3804 | if (*pptr == '\"') | |
3805 | { | |
3806 | for (i = 0, pptr ++; | |
3807 | i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"'; | |
3808 | i ++, pptr ++) | |
3809 | make_model[i] = *pptr; | |
3810 | ||
3811 | if (*pptr == '\"') | |
3812 | pptr ++; | |
3813 | ||
3814 | make_model[i] = '\0'; | |
3815 | ||
3816 | if (*pptr) | |
3817 | num_attrs = cupsParseOptions(pptr, num_attrs, &attrs); | |
3818 | } | |
3819 | } | |
3820 | } | |
3821 | ||
3822 | DEBUG_puts(packet); | |
3823 | DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n" | |
3824 | "location=\"%s\", info=\"%s\", make_model=\"%s\"\n", | |
3825 | type, state, uri, location, info, make_model)); | |
3826 | ||
3827 | /* | |
3828 | * Pull the URI apart to see if this is a local or remote printer... | |
3829 | */ | |
3830 | ||
3831 | if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource))) | |
3832 | { | |
3833 | cupsFreeOptions(num_attrs, attrs); | |
3834 | return; | |
3835 | } | |
3836 | ||
3837 | /* | |
3838 | * Do relaying... | |
3839 | */ | |
3840 | ||
3841 | for (i = 0; i < NumRelays; i ++) | |
3842 | if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from))) | |
3843 | if (sendto(BrowseSocket, packet, bytes, 0, | |
3844 | (struct sockaddr *)&(Relays[i].to), | |
3845 | httpAddrLength(&(Relays[i].to))) <= 0) | |
3846 | { | |
3847 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3848 | "update_cups_browse: sendto failed for relay %d - %s.", | |
3849 | i + 1, strerror(errno)); | |
3850 | cupsFreeOptions(num_attrs, attrs); | |
3851 | return; | |
3852 | } | |
3853 | ||
3854 | /* | |
3855 | * Process the browse data... | |
3856 | */ | |
3857 | ||
3858 | process_browse_data(uri, host, resource, (cups_ptype_t)type, | |
3859 | (ipp_pstate_t)state, location, info, make_model, | |
3860 | num_attrs, attrs); | |
3861 | } | |
3862 | ||
3863 | ||
2e4ff8af MS |
3864 | /* |
3865 | * 'update_lpd()' - Update the LPD configuration as needed. | |
3866 | */ | |
3867 | ||
3868 | static void | |
3869 | update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */ | |
3870 | { | |
3871 | if (!LPDConfigFile) | |
3872 | return; | |
3873 | ||
3874 | if (!strncmp(LPDConfigFile, "xinetd:///", 10)) | |
3875 | { | |
3876 | /* | |
3877 | * Enable/disable LPD via the xinetd.d config file for cups-lpd... | |
3878 | */ | |
3879 | ||
3880 | char newfile[1024]; /* New cups-lpd.N file */ | |
3881 | cups_file_t *ofp, /* Original file pointer */ | |
3882 | *nfp; /* New file pointer */ | |
3883 | char line[1024]; /* Line from file */ | |
3884 | ||
3885 | ||
3886 | snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9); | |
3887 | ||
3888 | if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL) | |
3889 | { | |
3890 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s", | |
3891 | LPDConfigFile + 9, strerror(errno)); | |
3892 | return; | |
3893 | } | |
3894 | ||
3895 | if ((nfp = cupsFileOpen(newfile, "w")) == NULL) | |
3896 | { | |
3897 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s", | |
3898 | newfile, strerror(errno)); | |
3899 | cupsFileClose(ofp); | |
3900 | return; | |
3901 | } | |
3902 | ||
3903 | /* | |
3904 | * Copy all of the lines from the cups-lpd file... | |
3905 | */ | |
3906 | ||
3907 | while (cupsFileGets(ofp, line, sizeof(line))) | |
3908 | { | |
3909 | if (line[0] == '{') | |
3910 | { | |
3911 | cupsFilePrintf(nfp, "%s\n", line); | |
3912 | snprintf(line, sizeof(line), "\tdisable = %s", | |
3913 | onoff ? "no" : "yes"); | |
3914 | } | |
3915 | else if (strstr(line, "disable =")) | |
3916 | continue; | |
3917 | ||
3918 | cupsFilePrintf(nfp, "%s\n", line); | |
3919 | } | |
3920 | ||
3921 | cupsFileClose(nfp); | |
3922 | cupsFileClose(ofp); | |
3923 | rename(newfile, LPDConfigFile + 9); | |
3924 | } | |
3925 | else if (!strncmp(LPDConfigFile, "launchd:///", 11)) | |
3926 | { | |
3927 | /* | |
3928 | * Enable/disable LPD via the launchctl command... | |
3929 | */ | |
3930 | ||
3931 | char *argv[5], /* Arguments for command */ | |
3932 | *envp[MAX_ENV]; /* Environment for command */ | |
3933 | int pid; /* Process ID */ | |
3934 | ||
3935 | ||
3936 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
3937 | argv[0] = (char *)"launchctl"; | |
3938 | argv[1] = (char *)(onoff ? "load" : "unload"); | |
3939 | argv[2] = (char *)"-w"; | |
3940 | argv[3] = LPDConfigFile + 10; | |
3941 | argv[4] = NULL; | |
3942 | ||
a4924f6c MS |
3943 | cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1, |
3944 | NULL, &pid); | |
2e4ff8af MS |
3945 | } |
3946 | } | |
3947 | ||
3948 | ||
f899b121 | 3949 | /* |
3950 | * 'update_polling()' - Read status messages from the poll daemons. | |
3951 | */ | |
3952 | ||
3953 | static void | |
3954 | update_polling(void) | |
3955 | { | |
3956 | char *ptr, /* Pointer to end of line in buffer */ | |
3957 | message[1024]; /* Pointer to message text */ | |
3958 | int loglevel; /* Log level for message */ | |
3959 | ||
3960 | ||
3961 | while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel, | |
3962 | message, sizeof(message))) != NULL) | |
3963 | if (!strchr(PollStatusBuffer->buffer, '\n')) | |
3964 | break; | |
3965 | ||
3966 | if (ptr == NULL && !PollStatusBuffer->bufused) | |
3967 | { | |
3968 | /* | |
3969 | * All polling processes have died; stop polling... | |
3970 | */ | |
3971 | ||
3972 | cupsdLogMessage(CUPSD_LOG_ERROR, | |
3973 | "update_polling: all polling processes have exited!"); | |
3974 | cupsdStopPolling(); | |
3975 | } | |
3976 | } | |
3977 | ||
3978 | ||
3979 | /* | |
2e4ff8af MS |
3980 | * 'update_smb()' - Update the SMB configuration as needed. |
3981 | */ | |
3982 | ||
3983 | static void | |
3984 | update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */ | |
3985 | { | |
3986 | if (!SMBConfigFile) | |
3987 | return; | |
3988 | ||
3989 | if (!strncmp(SMBConfigFile, "samba:///", 9)) | |
3990 | { | |
3991 | /* | |
3992 | * Enable/disable SMB via the specified smb.conf config file... | |
3993 | */ | |
3994 | ||
3995 | char newfile[1024]; /* New smb.conf.N file */ | |
3996 | cups_file_t *ofp, /* Original file pointer */ | |
3997 | *nfp; /* New file pointer */ | |
3998 | char line[1024]; /* Line from file */ | |
3999 | int in_printers; /* In [printers] section? */ | |
4000 | ||
4001 | ||
4002 | snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8); | |
4003 | ||
4004 | if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL) | |
4005 | { | |
4006 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s", | |
4007 | SMBConfigFile + 8, strerror(errno)); | |
4008 | return; | |
4009 | } | |
4010 | ||
4011 | if ((nfp = cupsFileOpen(newfile, "w")) == NULL) | |
4012 | { | |
4013 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s", | |
4014 | newfile, strerror(errno)); | |
4015 | cupsFileClose(ofp); | |
4016 | return; | |
4017 | } | |
4018 | ||
4019 | /* | |
4020 | * Copy all of the lines from the smb.conf file... | |
4021 | */ | |
4022 | ||
4023 | in_printers = 0; | |
4024 | ||
4025 | while (cupsFileGets(ofp, line, sizeof(line))) | |
4026 | { | |
4027 | if (in_printers && strstr(line, "printable =")) | |
4028 | snprintf(line, sizeof(line), " printable = %s", | |
4029 | onoff ? "yes" : "no"); | |
4030 | ||
4031 | cupsFilePrintf(nfp, "%s\n", line); | |
4032 | ||
4033 | if (line[0] == '[') | |
4034 | in_printers = !strcmp(line, "[printers]"); | |
4035 | } | |
4036 | ||
4037 | cupsFileClose(nfp); | |
4038 | cupsFileClose(ofp); | |
4039 | rename(newfile, SMBConfigFile + 8); | |
4040 | } | |
4041 | else if (!strncmp(SMBConfigFile, "launchd:///", 11)) | |
4042 | { | |
4043 | /* | |
4044 | * Enable/disable SMB via the launchctl command... | |
4045 | */ | |
4046 | ||
4047 | char *argv[5], /* Arguments for command */ | |
4048 | *envp[MAX_ENV]; /* Environment for command */ | |
4049 | int pid; /* Process ID */ | |
4050 | ||
4051 | ||
4052 | cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0]))); | |
4053 | argv[0] = (char *)"launchctl"; | |
4054 | argv[1] = (char *)(onoff ? "load" : "unload"); | |
4055 | argv[2] = (char *)"-w"; | |
4056 | argv[3] = SMBConfigFile + 10; | |
4057 | argv[4] = NULL; | |
4058 | ||
a4924f6c MS |
4059 | cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1, |
4060 | NULL, &pid); | |
2e4ff8af MS |
4061 | } |
4062 | } | |
4063 | ||
4064 | ||
4065 | /* | |
4066 | * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $". | |
ef416fc2 | 4067 | */ |