]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/dirsvc.c
Merge changes from CUPS 1.4svn-r8033.
[thirdparty/cups.git] / scheduler / dirsvc.c
1 /*
2 * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
3 *
4 * Directory services routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
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/".
14 *
15 * Contents:
16 *
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 * ldap_rebind_proc() - Callback function for LDAP rebind
27 * ldap_connect() - Start new LDAP connection
28 * ldap_reconnect() - Reconnect to LDAP Server
29 * ldap_disconnect() - Disconnect from LDAP Server
30 * cupsdStartBrowsing() - Start sending and receiving broadcast
31 * information.
32 * cupsdStartPolling() - Start polling servers as needed.
33 * cupsdStopBrowsing() - Stop sending and receiving broadcast
34 * information.
35 * cupsdStopPolling() - Stop polling servers as needed.
36 * cupsdUpdateDNSSDName() - Update the computer name we use for
37 * browsing...
38 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
39 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
40 * dequote() - Remote quotes from a string.
41 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
42 * dnssdComparePrinters() - Compare the registered names of two printers.
43 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
44 * printer.
45 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
46 * record format.
47 * dnssdRegisterCallback() - DNSServiceRegister callback.
48 * dnssdRegisterPrinter() - Start sending broadcast information for a
49 * printer or update the broadcast contents.
50 * dnssdUpdate() - Handle DNS-SD queries.
51 * get_hostconfig() - Get an /etc/hostconfig service setting.
52 * is_local_queue() - Determine whether the URI points at a local
53 * queue.
54 * process_browse_data() - Process new browse data.
55 * process_implicit_classes() - Create/update implicit classes as needed.
56 * send_cups_browse() - Send new browsing information using the CUPS
57 * protocol.
58 * ldap_search_rec() - LDAP Search with reconnect
59 * ldap_freeres() - Free LDAPMessage
60 * ldap_getval_char() - Get first LDAP value and convert to string
61 * send_ldap_ou() - Send LDAP ou registrations.
62 * send_ldap_browse() - Send LDAP printer registrations.
63 * ldap_dereg_printer() - Delete printer from directory
64 * send_slp_browse() - Register the specified printer with SLP.
65 * slp_attr_callback() - SLP attribute callback
66 * slp_dereg_printer() - SLPDereg() the specified printer
67 * slp_get_attr() - Get an attribute from an SLP registration.
68 * slp_reg_callback() - Empty SLPRegReport.
69 * slp_url_callback() - SLP service url callback
70 * update_cups_browse() - Update the browse lists using the CUPS
71 * protocol.
72 * update_lpd() - Update the LPD configuration as needed.
73 * update_polling() - Read status messages from the poll daemons.
74 * update_smb() - Update the SMB configuration as needed.
75 */
76
77 /*
78 * Include necessary headers...
79 */
80
81 #include "cupsd.h"
82 #include <grp.h>
83
84 #ifdef HAVE_DNSSD
85 # include <dns_sd.h>
86 # ifdef __APPLE__
87 # include <nameser.h>
88 # ifdef HAVE_COREFOUNDATION
89 # include <CoreFoundation/CoreFoundation.h>
90 # endif /* HAVE_COREFOUNDATION */
91 # ifdef HAVE_SYSTEMCONFIGURATION
92 # include <SystemConfiguration/SystemConfiguration.h>
93 # endif /* HAVE_SYSTEMCONFIGURATION */
94 # endif /* __APPLE__ */
95 #endif /* HAVE_DNSSD */
96
97
98 /*
99 * Local functions...
100 */
101
102 static char *dequote(char *d, const char *s, int dlen);
103 #ifdef __APPLE__
104 static int get_hostconfig(const char *name);
105 #endif /* __APPLE__ */
106 static int is_local_queue(const char *uri, char *host, int hostlen,
107 char *resource, int resourcelen);
108 static void process_browse_data(const char *uri, const char *host,
109 const char *resource, cups_ptype_t type,
110 ipp_pstate_t state, const char *location,
111 const char *info, const char *make_model,
112 int num_attrs, cups_option_t *attrs);
113 static void process_implicit_classes(void);
114 static void send_cups_browse(cupsd_printer_t *p);
115 #ifdef HAVE_LDAP
116 static LDAP *ldap_connect(void);
117 static void ldap_reconnect(void);
118 static void ldap_disconnect(LDAP *ld);
119 static int ldap_search_rec(LDAP *ld, char *base, int scope,
120 char *filter, char *attrs[],
121 int attrsonly, LDAPMessage **res);
122 static int ldap_getval_firststring(LDAP *ld, LDAPMessage *entry,
123 char *attr, char *retval,
124 unsigned long maxsize);
125 static void ldap_freeres(LDAPMessage *entry);
126 static void send_ldap_ou(char *ou, char *basedn, char *descstring);
127 static void send_ldap_browse(cupsd_printer_t *p);
128 static void ldap_dereg_printer(cupsd_printer_t *p);
129 static void ldap_dereg_ou(char *ou, char *basedn);
130 # ifdef HAVE_LDAP_REBIND_PROC
131 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
132 static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
133 LDAP_CONST char *refsp,
134 ber_tag_t request,
135 ber_int_t msgid,
136 void *params);
137 # else
138 static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
139 char **dnp,
140 char **passwdp,
141 int *authmethodp,
142 int freeit,
143 void *arg);
144 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
145 # endif /* HAVE_LDAP_REBIND_PROC */
146 #endif /* HAVE_LDAP */
147 #ifdef HAVE_LIBSLP
148 static void send_slp_browse(cupsd_printer_t *p);
149 #endif /* HAVE_LIBSLP */
150 static void update_cups_browse(void);
151 static void update_lpd(int onoff);
152 static void update_polling(void);
153 static void update_smb(int onoff);
154
155
156 #ifdef HAVE_DNSSD
157 static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
158 int for_lpd);
159 static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
160 static void dnssdDeregisterPrinter(cupsd_printer_t *p);
161 static char *dnssdPackTxtRecord(int *txt_len, char *keyvalue[][2],
162 int count);
163 static void dnssdRegisterCallback(DNSServiceRef sdRef,
164 DNSServiceFlags flags,
165 DNSServiceErrorType errorCode,
166 const char *name, const char *regtype,
167 const char *domain, void *context);
168 static void dnssdRegisterPrinter(cupsd_printer_t *p);
169 static void dnssdUpdate(void);
170 #endif /* HAVE_DNSSD */
171
172 #ifdef HAVE_LDAP
173 static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
174 {
175 "printerDescription",
176 "printerLocation",
177 "printerMakeAndModel",
178 "printerType",
179 "printerURI",
180 NULL
181 };
182 #endif /* HAVE_LDAP */
183
184 #ifdef HAVE_LIBSLP
185 /*
186 * SLP definitions...
187 */
188
189 /*
190 * SLP service name for CUPS...
191 */
192
193 # define SLP_CUPS_SRVTYPE "service:printer"
194 # define SLP_CUPS_SRVLEN 15
195
196
197 /*
198 * Printer service URL structure
199 */
200
201 typedef struct _slpsrvurl_s /**** SLP URL list ****/
202 {
203 struct _slpsrvurl_s *next; /* Next URL in list */
204 char url[HTTP_MAX_URI];
205 /* URL */
206 } slpsrvurl_t;
207
208
209 /*
210 * Local functions...
211 */
212
213 static SLPBoolean slp_attr_callback(SLPHandle hslp, const char *attrlist,
214 SLPError errcode, void *cookie);
215 static void slp_dereg_printer(cupsd_printer_t *p);
216 static int slp_get_attr(const char *attrlist, const char *tag,
217 char **valbuf);
218 static void slp_reg_callback(SLPHandle hslp, SLPError errcode,
219 void *cookie);
220 static SLPBoolean slp_url_callback(SLPHandle hslp, const char *srvurl,
221 unsigned short lifetime,
222 SLPError errcode, void *cookie);
223 #endif /* HAVE_LIBSLP */
224
225
226 /*
227 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
228 * local printer and remove any pending
229 * references to remote printers.
230 */
231
232 void
233 cupsdDeregisterPrinter(
234 cupsd_printer_t *p, /* I - Printer to register */
235 int removeit) /* I - Printer being permanently removed */
236 {
237 /*
238 * Only deregister if browsing is enabled and it's a local printer...
239 */
240
241 if (!Browsing || !p->shared ||
242 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
243 return;
244
245 /*
246 * Announce the deletion...
247 */
248
249 if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
250 {
251 cups_ptype_t savedtype = p->type; /* Saved printer type */
252
253 p->type |= CUPS_PRINTER_DELETE;
254
255 send_cups_browse(p);
256
257 p->type = savedtype;
258 }
259
260 #ifdef HAVE_LIBSLP
261 if (BrowseLocalProtocols & BROWSE_SLP)
262 slp_dereg_printer(p);
263 #endif /* HAVE_LIBSLP */
264
265 #ifdef HAVE_LDAP
266 if (BrowseLocalProtocols & BROWSE_LDAP)
267 ldap_dereg_printer(p);
268 #endif /* HAVE_LDAP */
269
270 #ifdef HAVE_DNSSD
271 if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
272 dnssdDeregisterPrinter(p);
273 #endif /* HAVE_DNSSD */
274 }
275
276
277 /*
278 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
279 */
280
281 void
282 cupsdLoadRemoteCache(void)
283 {
284 cups_file_t *fp; /* remote.cache file */
285 int linenum; /* Current line number */
286 char line[1024], /* Line from file */
287 *value, /* Pointer to value */
288 *valueptr, /* Pointer into value */
289 scheme[32], /* Scheme portion of URI */
290 username[64], /* Username portion of URI */
291 host[HTTP_MAX_HOST],
292 /* Hostname portion of URI */
293 resource[HTTP_MAX_URI];
294 /* Resource portion of URI */
295 int port; /* Port number */
296 cupsd_printer_t *p; /* Current printer */
297 time_t now; /* Current time */
298
299
300 /*
301 * Don't load the cache if the remote protocols are disabled...
302 */
303
304 if (!Browsing)
305 {
306 cupsdLogMessage(CUPSD_LOG_DEBUG,
307 "cupsdLoadRemoteCache: Not loading remote cache.");
308 return;
309 }
310
311 /*
312 * Open the remote.cache file...
313 */
314
315 snprintf(line, sizeof(line), "%s/remote.cache", CacheDir);
316 if ((fp = cupsFileOpen(line, "r")) == NULL)
317 return;
318
319 /*
320 * Read printer configurations until we hit EOF...
321 */
322
323 linenum = 0;
324 p = NULL;
325 now = time(NULL);
326
327 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
328 {
329 /*
330 * Decode the directive...
331 */
332
333 if (!strcasecmp(line, "<Printer") ||
334 !strcasecmp(line, "<DefaultPrinter"))
335 {
336 /*
337 * <Printer name> or <DefaultPrinter name>
338 */
339
340 if (p == NULL && value)
341 {
342 /*
343 * Add the printer and a base file type...
344 */
345
346 cupsdLogMessage(CUPSD_LOG_DEBUG,
347 "cupsdLoadRemoteCache: Loading printer %s...", value);
348
349 if ((p = cupsdFindDest(value)) != NULL)
350 {
351 if (p->type & CUPS_PRINTER_CLASS)
352 {
353 cupsdLogMessage(CUPSD_LOG_WARN,
354 "Cached remote printer \"%s\" conflicts with "
355 "existing class!",
356 value);
357 p = NULL;
358 continue;
359 }
360 }
361 else
362 p = cupsdAddPrinter(value);
363
364 p->accepting = 1;
365 p->state = IPP_PRINTER_IDLE;
366 p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
367 p->browse_time = now;
368 p->browse_expire = now + BrowseTimeout;
369
370 /*
371 * Set the default printer as needed...
372 */
373
374 if (!strcasecmp(line, "<DefaultPrinter"))
375 DefaultPrinter = p;
376 }
377 else
378 {
379 cupsdLogMessage(CUPSD_LOG_ERROR,
380 "Syntax error on line %d of remote.cache.", linenum);
381 break;
382 }
383 }
384 else if (!strcasecmp(line, "<Class") ||
385 !strcasecmp(line, "<DefaultClass"))
386 {
387 /*
388 * <Class name> or <DefaultClass name>
389 */
390
391 if (p == NULL && value)
392 {
393 /*
394 * Add the printer and a base file type...
395 */
396
397 cupsdLogMessage(CUPSD_LOG_DEBUG,
398 "cupsdLoadRemoteCache: Loading class %s...", value);
399
400 if ((p = cupsdFindDest(value)) != NULL)
401 p->type = CUPS_PRINTER_CLASS;
402 else
403 p = cupsdAddClass(value);
404
405 p->accepting = 1;
406 p->state = IPP_PRINTER_IDLE;
407 p->type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
408 p->browse_time = now;
409 p->browse_expire = now + BrowseTimeout;
410
411 /*
412 * Set the default printer as needed...
413 */
414
415 if (!strcasecmp(line, "<DefaultClass"))
416 DefaultPrinter = p;
417 }
418 else
419 {
420 cupsdLogMessage(CUPSD_LOG_ERROR,
421 "Syntax error on line %d of remote.cache.", linenum);
422 break;
423 }
424 }
425 else if (!strcasecmp(line, "</Printer>") ||
426 !strcasecmp(line, "</Class>"))
427 {
428 if (p != NULL)
429 {
430 /*
431 * Close out the current printer...
432 */
433
434 cupsdSetPrinterAttrs(p);
435
436 p = NULL;
437 }
438 else
439 cupsdLogMessage(CUPSD_LOG_ERROR,
440 "Syntax error on line %d of remote.cache.", linenum);
441 }
442 else if (!p)
443 {
444 cupsdLogMessage(CUPSD_LOG_ERROR,
445 "Syntax error on line %d of remote.cache.", linenum);
446 }
447 else if (!strcasecmp(line, "Info"))
448 {
449 if (value)
450 cupsdSetString(&p->info, value);
451 }
452 else if (!strcasecmp(line, "MakeModel"))
453 {
454 if (value)
455 cupsdSetString(&p->make_model, value);
456 }
457 else if (!strcasecmp(line, "Location"))
458 {
459 if (value)
460 cupsdSetString(&p->location, value);
461 }
462 else if (!strcasecmp(line, "DeviceURI"))
463 {
464 if (value)
465 {
466 httpSeparateURI(HTTP_URI_CODING_ALL, value, scheme, sizeof(scheme),
467 username, sizeof(username), host, sizeof(host), &port,
468 resource, sizeof(resource));
469
470 cupsdSetString(&p->hostname, host);
471 cupsdSetString(&p->uri, value);
472 cupsdSetDeviceURI(p, value);
473 }
474 else
475 cupsdLogMessage(CUPSD_LOG_ERROR,
476 "Syntax error on line %d of remote.cache.", linenum);
477 }
478 else if (!strcasecmp(line, "Option") && value)
479 {
480 /*
481 * Option name value
482 */
483
484 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
485
486 if (!*valueptr)
487 cupsdLogMessage(CUPSD_LOG_ERROR,
488 "Syntax error on line %d of remote.cache.", linenum);
489 else
490 {
491 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
492
493 p->num_options = cupsAddOption(value, valueptr, p->num_options,
494 &(p->options));
495 }
496 }
497 else if (!strcasecmp(line, "Reason"))
498 {
499 if (value &&
500 p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
501 {
502 p->reasons[p->num_reasons] = _cupsStrAlloc(value);
503 p->num_reasons ++;
504 }
505 else
506 cupsdLogMessage(CUPSD_LOG_ERROR,
507 "Syntax error on line %d of remote.cache.", linenum);
508 }
509 else if (!strcasecmp(line, "State"))
510 {
511 /*
512 * Set the initial queue state...
513 */
514
515 if (value && !strcasecmp(value, "idle"))
516 p->state = IPP_PRINTER_IDLE;
517 else if (value && !strcasecmp(value, "stopped"))
518 p->state = IPP_PRINTER_STOPPED;
519 else
520 cupsdLogMessage(CUPSD_LOG_ERROR,
521 "Syntax error on line %d of remote.cache.", linenum);
522 }
523 else if (!strcasecmp(line, "StateMessage"))
524 {
525 /*
526 * Set the initial queue state message...
527 */
528
529 if (value)
530 strlcpy(p->state_message, value, sizeof(p->state_message));
531 }
532 else if (!strcasecmp(line, "Accepting"))
533 {
534 /*
535 * Set the initial accepting state...
536 */
537
538 if (value &&
539 (!strcasecmp(value, "yes") ||
540 !strcasecmp(value, "on") ||
541 !strcasecmp(value, "true")))
542 p->accepting = 1;
543 else if (value &&
544 (!strcasecmp(value, "no") ||
545 !strcasecmp(value, "off") ||
546 !strcasecmp(value, "false")))
547 p->accepting = 0;
548 else
549 cupsdLogMessage(CUPSD_LOG_ERROR,
550 "Syntax error on line %d of remote.cache.", linenum);
551 }
552 else if (!strcasecmp(line, "Type"))
553 {
554 if (value)
555 p->type = atoi(value);
556 else
557 cupsdLogMessage(CUPSD_LOG_ERROR,
558 "Syntax error on line %d of remote.cache.", linenum);
559 }
560 else if (!strcasecmp(line, "BrowseTime"))
561 {
562 if (value)
563 {
564 time_t t = atoi(value);
565
566 if (t > p->browse_expire)
567 p->browse_expire = t;
568 }
569 else
570 cupsdLogMessage(CUPSD_LOG_ERROR,
571 "Syntax error on line %d of remote.cache.", linenum);
572 }
573 else if (!strcasecmp(line, "JobSheets"))
574 {
575 /*
576 * Set the initial job sheets...
577 */
578
579 if (value)
580 {
581 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
582
583 if (*valueptr)
584 *valueptr++ = '\0';
585
586 cupsdSetString(&p->job_sheets[0], value);
587
588 while (isspace(*valueptr & 255))
589 valueptr ++;
590
591 if (*valueptr)
592 {
593 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
594
595 if (*valueptr)
596 *valueptr = '\0';
597
598 cupsdSetString(&p->job_sheets[1], value);
599 }
600 }
601 else
602 cupsdLogMessage(CUPSD_LOG_ERROR,
603 "Syntax error on line %d of remote.cache.", linenum);
604 }
605 else if (!strcasecmp(line, "AllowUser"))
606 {
607 if (value)
608 {
609 p->deny_users = 0;
610 cupsdAddPrinterUser(p, value);
611 }
612 else
613 cupsdLogMessage(CUPSD_LOG_ERROR,
614 "Syntax error on line %d of remote.cache.", linenum);
615 }
616 else if (!strcasecmp(line, "DenyUser"))
617 {
618 if (value)
619 {
620 p->deny_users = 1;
621 cupsdAddPrinterUser(p, value);
622 }
623 else
624 cupsdLogMessage(CUPSD_LOG_ERROR,
625 "Syntax error on line %d of remote.cache.", linenum);
626 }
627 else
628 {
629 /*
630 * Something else we don't understand...
631 */
632
633 cupsdLogMessage(CUPSD_LOG_ERROR,
634 "Unknown configuration directive %s on line %d of remote.cache.",
635 line, linenum);
636 }
637 }
638
639 cupsFileClose(fp);
640
641 /*
642 * Do auto-classing if needed...
643 */
644
645 process_implicit_classes();
646 }
647
648
649 /*
650 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
651 * printer or update the broadcast contents.
652 */
653
654 void
655 cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
656 {
657 if (!Browsing || !BrowseLocalProtocols ||
658 (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
659 return;
660
661 #ifdef HAVE_LIBSLP
662 /* if (BrowseLocalProtocols & BROWSE_SLP)
663 slpRegisterPrinter(p); */
664 #endif /* HAVE_LIBSLP */
665
666 #ifdef HAVE_DNSSD
667 if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
668 dnssdRegisterPrinter(p);
669 #endif /* HAVE_DNSSD */
670 }
671
672
673 /*
674 * 'cupsdRestartPolling()' - Restart polling servers as needed.
675 */
676
677 void
678 cupsdRestartPolling(void)
679 {
680 int i; /* Looping var */
681 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
682
683
684 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
685 if (pollp->pid)
686 kill(pollp->pid, SIGHUP);
687 }
688
689
690 /*
691 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
692 */
693
694 void
695 cupsdSaveRemoteCache(void)
696 {
697 int i; /* Looping var */
698 cups_file_t *fp; /* printers.conf file */
699 char temp[1024], /* Temporary string */
700 value[2048]; /* Value string */
701 cupsd_printer_t *printer; /* Current printer class */
702 time_t curtime; /* Current time */
703 struct tm *curdate; /* Current date */
704 cups_option_t *option; /* Current option */
705
706
707 /*
708 * Create the remote.cache file...
709 */
710
711 snprintf(temp, sizeof(temp), "%s/remote.cache", CacheDir);
712
713 if ((fp = cupsFileOpen(temp, "w")) == NULL)
714 {
715 cupsdLogMessage(CUPSD_LOG_ERROR,
716 "Unable to save remote.cache - %s", strerror(errno));
717 return;
718 }
719 else
720 cupsdLogMessage(CUPSD_LOG_DEBUG, "Saving remote.cache...");
721
722 /*
723 * Restrict access to the file...
724 */
725
726 fchown(cupsFileNumber(fp), getuid(), Group);
727 fchmod(cupsFileNumber(fp), ConfigFilePerm);
728
729 /*
730 * Write a small header to the file...
731 */
732
733 curtime = time(NULL);
734 curdate = localtime(&curtime);
735 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
736
737 cupsFilePuts(fp, "# Remote cache file for " CUPS_SVERSION "\n");
738 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
739
740 /*
741 * Write each local printer known to the system...
742 */
743
744 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
745 printer;
746 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
747 {
748 /*
749 * Skip local destinations...
750 */
751
752 if (!(printer->type & CUPS_PRINTER_DISCOVERED))
753 continue;
754
755 /*
756 * Write printers as needed...
757 */
758
759 if (printer == DefaultPrinter)
760 cupsFilePuts(fp, "<Default");
761 else
762 cupsFilePutChar(fp, '<');
763
764 if (printer->type & CUPS_PRINTER_CLASS)
765 cupsFilePrintf(fp, "Class %s>\n", printer->name);
766 else
767 cupsFilePrintf(fp, "Printer %s>\n", printer->name);
768
769 cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire);
770
771 if (printer->info)
772 cupsFilePutConf(fp, "Info", printer->info);
773
774 if (printer->location)
775 cupsFilePutConf(fp, "Location", printer->location);
776
777 if (printer->make_model)
778 cupsFilePutConf(fp, "MakeModel", printer->make_model);
779
780 cupsFilePutConf(fp, "DeviceURI", printer->device_uri);
781
782 if (printer->state == IPP_PRINTER_STOPPED)
783 cupsFilePuts(fp, "State Stopped\n");
784 else
785 cupsFilePuts(fp, "State Idle\n");
786
787 for (i = 0; i < printer->num_reasons; i ++)
788 cupsFilePutConf(fp, "Reason", printer->reasons[i]);
789
790 cupsFilePrintf(fp, "Type %d\n", printer->type);
791
792 if (printer->accepting)
793 cupsFilePuts(fp, "Accepting Yes\n");
794 else
795 cupsFilePuts(fp, "Accepting No\n");
796
797 snprintf(value, sizeof(value), "%s %s", printer->job_sheets[0],
798 printer->job_sheets[1]);
799 cupsFilePutConf(fp, "JobSheets", value);
800
801 for (i = 0; i < printer->num_users; i ++)
802 cupsFilePutConf(fp, printer->deny_users ? "DenyUser" : "AllowUser",
803 printer->users[i]);
804
805 for (i = printer->num_options, option = printer->options;
806 i > 0;
807 i --, option ++)
808 {
809 snprintf(value, sizeof(value), "%s %s", option->name, option->value);
810 cupsFilePutConf(fp, "Option", value);
811 }
812
813 if (printer->type & CUPS_PRINTER_CLASS)
814 cupsFilePuts(fp, "</Class>\n");
815 else
816 cupsFilePuts(fp, "</Printer>\n");
817 }
818
819 cupsFileClose(fp);
820 }
821
822
823 /*
824 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
825 */
826
827 void
828 cupsdSendBrowseList(void)
829 {
830 int count; /* Number of dests to update */
831 cupsd_printer_t *p; /* Current printer */
832 time_t ut, /* Minimum update time */
833 to; /* Timeout time */
834
835
836 if (!Browsing || !Printers)
837 return;
838
839 /*
840 * Compute the update and timeout times...
841 */
842
843 to = time(NULL);
844 ut = to - BrowseInterval;
845
846 /*
847 * Figure out how many printers need an update...
848 */
849
850 if (BrowseInterval > 0 && BrowseLocalProtocols)
851 {
852 int max_count; /* Maximum number to update */
853
854
855 /*
856 * Throttle the number of printers we'll be updating this time
857 * around based on the number of queues that need updating and
858 * the maximum number of queues to update each second...
859 */
860
861 max_count = 2 * cupsArrayCount(Printers) / BrowseInterval + 1;
862
863 for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
864 count < max_count && p != NULL;
865 p = (cupsd_printer_t *)cupsArrayNext(Printers))
866 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
867 p->shared && p->browse_time < ut)
868 count ++;
869
870 /*
871 * Loop through all of the printers and send local updates as needed...
872 */
873
874 if (BrowseNext)
875 p = (cupsd_printer_t *)cupsArrayFind(Printers, BrowseNext);
876 else
877 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
878
879 for (;
880 count > 0;
881 p = (cupsd_printer_t *)cupsArrayNext(Printers))
882 {
883 /*
884 * Check for wraparound...
885 */
886
887 if (!p)
888 p = (cupsd_printer_t *)cupsArrayFirst(Printers);
889
890 if (!p)
891 break;
892 else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
893 !p->shared)
894 continue;
895 else if (p->browse_time < ut)
896 {
897 /*
898 * Need to send an update...
899 */
900
901 count --;
902
903 p->browse_time = time(NULL);
904
905 if ((BrowseLocalProtocols & BROWSE_CUPS) && BrowseSocket >= 0)
906 send_cups_browse(p);
907
908 #ifdef HAVE_LIBSLP
909 if (BrowseLocalProtocols & BROWSE_SLP)
910 send_slp_browse(p);
911 #endif /* HAVE_LIBSLP */
912
913 #ifdef HAVE_LDAP
914 if (BrowseLocalProtocols & BROWSE_LDAP)
915 send_ldap_browse(p);
916 #endif /* HAVE_LDAP */
917 }
918 }
919
920 /*
921 * Save where we left off so that all printers get updated...
922 */
923
924 BrowseNext = p;
925 }
926
927 /*
928 * Loop through all of the printers and timeout old printers as needed...
929 */
930
931 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
932 p;
933 p = (cupsd_printer_t *)cupsArrayNext(Printers))
934 {
935 /*
936 * If this is a remote queue, see if it needs to be timed out...
937 */
938
939 if ((p->type & CUPS_PRINTER_DISCOVERED) &&
940 !(p->type & CUPS_PRINTER_IMPLICIT) &&
941 p->browse_expire < to)
942 {
943 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
944 "%s \'%s\' deleted by directory services (timeout).",
945 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
946 p->name);
947
948 cupsdLogMessage(CUPSD_LOG_DEBUG,
949 "Remote destination \"%s\" has timed out; "
950 "deleting it...",
951 p->name);
952
953 cupsArraySave(Printers);
954 cupsdDeletePrinter(p, 1);
955 cupsArrayRestore(Printers);
956 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
957 }
958 }
959 }
960
961
962 #ifdef HAVE_LDAP_REBIND_PROC
963 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
964 /*
965 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
966 */
967
968 static int
969 ldap_rebind_proc (LDAP *RebindLDAPHandle,
970 LDAP_CONST char *refsp,
971 ber_tag_t request,
972 ber_int_t msgid,
973 void *params)
974 {
975 int rc;
976
977 /*
978 * Bind to new LDAP server...
979 */
980
981 cupsdLogMessage(CUPSD_LOG_DEBUG2,
982 "ldap_rebind_proc: Rebind to %s", refsp);
983
984 # if LDAP_API_VERSION > 3000
985 struct berval bval;
986 bval.bv_val = BrowseLDAPPassword;
987 bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword);
988
989 rc = ldap_sasl_bind_s(RebindLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE, &bval, NULL, NULL, NULL);
990 # else
991 rc = ldap_bind_s(RebindLDAPHandle, BrowseLDAPBindDN,
992 BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
993 # endif /* LDAP_API_VERSION > 3000 */
994
995 return (rc);
996 }
997
998 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
999
1000 /*
1001 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
1002 */
1003
1004 static int
1005 ldap_rebind_proc (LDAP *RebindLDAPHandle,
1006 char **dnp,
1007 char **passwdp,
1008 int *authmethodp,
1009 int freeit,
1010 void *arg)
1011 {
1012 switch ( freeit ) {
1013
1014 case 1:
1015
1016 /*
1017 * Free current values...
1018 */
1019
1020 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1021 "ldap_rebind_proc: Free values...");
1022
1023 if ( dnp && *dnp ) {
1024 free( *dnp );
1025 }
1026 if ( passwdp && *passwdp ) {
1027 free( *passwdp );
1028 }
1029 break;
1030
1031 case 0:
1032
1033 /*
1034 * Return credentials for LDAP referal...
1035 */
1036
1037 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1038 "ldap_rebind_proc: Return necessary values...");
1039
1040 *dnp = strdup(BrowseLDAPBindDN);
1041 *passwdp = strdup(BrowseLDAPPassword);
1042 *authmethodp = LDAP_AUTH_SIMPLE;
1043 break;
1044
1045 default:
1046
1047 /*
1048 * Should never happen...
1049 */
1050
1051 cupsdLogMessage(CUPSD_LOG_ERROR,
1052 "LDAP rebind has been called with wrong freeit value!");
1053 break;
1054
1055 }
1056
1057 return (LDAP_SUCCESS);
1058 }
1059 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1060 #endif /* HAVE_LDAP_REBIND_PROC */
1061
1062
1063 #ifdef HAVE_LDAP
1064 /*
1065 * 'ldap_connect()' - Start new LDAP connection
1066 */
1067
1068 static LDAP *
1069 ldap_connect(void)
1070 {
1071 /*
1072 * Open LDAP handle...
1073 */
1074
1075 int rc; /* LDAP API status */
1076 int version = 3; /* LDAP version */
1077 struct berval bv = {0, ""}; /* SASL bind value */
1078 LDAP *TempBrowseLDAPHandle=NULL; /* Temporary LDAP Handle */
1079 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1080 int ldap_ssl = 0; /* LDAP SSL indicator */
1081 int ssl_err = 0; /* LDAP SSL error value */
1082 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1083
1084 # ifdef HAVE_OPENLDAP
1085 # ifdef HAVE_LDAP_SSL
1086
1087 /*
1088 * Set the certificate file to use for encrypted LDAP sessions...
1089 */
1090
1091 if (BrowseLDAPCACertFile)
1092 {
1093 cupsdLogMessage(CUPSD_LOG_DEBUG,
1094 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1095 BrowseLDAPCACertFile);
1096
1097 if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
1098 (void *)BrowseLDAPCACertFile)) != LDAP_SUCCESS)
1099 cupsdLogMessage(CUPSD_LOG_ERROR,
1100 "Unable to set CA certificate file for LDAP "
1101 "connections: %d - %s", rc, ldap_err2string(rc));
1102 }
1103
1104 # endif /* HAVE_LDAP_SSL */
1105 /*
1106 * Initialize OPENLDAP connection...
1107 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1108 */
1109
1110 if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
1111 rc = ldap_initialize(&TempBrowseLDAPHandle, "ldapi:///");
1112 else
1113 rc = ldap_initialize(&TempBrowseLDAPHandle, BrowseLDAPServer);
1114
1115 # else /* HAVE_OPENLDAP */
1116
1117 int ldap_port = 0; /* LDAP port */
1118 char ldap_protocol[11], /* LDAP protocol */
1119 ldap_host[255]; /* LDAP host */
1120
1121 /*
1122 * Split LDAP URI into its components...
1123 */
1124
1125 if (! BrowseLDAPServer)
1126 {
1127 cupsdLogMessage(CUPSD_LOG_ERROR,
1128 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1129 BrowseLocalProtocols &= ~BROWSE_LDAP;
1130 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1131 return (NULL);
1132 }
1133
1134 sscanf(BrowseLDAPServer, "%10[^:]://%254[^:/]:%d", ldap_protocol, ldap_host, &ldap_port);
1135
1136 if (strcmp(ldap_protocol, "ldap") == 0) {
1137 ldap_ssl = 0;
1138 } else if (strcmp(ldap_protocol, "ldaps") == 0) {
1139 ldap_ssl = 1;
1140 } else {
1141 cupsdLogMessage(CUPSD_LOG_ERROR,
1142 "unrecognised ldap protocol (%s)!", ldap_protocol);
1143 cupsdLogMessage(CUPSD_LOG_ERROR,
1144 "Disable LDAP browsing!");
1145 BrowseLocalProtocols &= ~BROWSE_LDAP;
1146 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1147 return (NULL);
1148 }
1149
1150 if (ldap_port == 0)
1151 {
1152 if (ldap_ssl)
1153 ldap_port = LDAPS_PORT;
1154 else
1155 ldap_port = LDAP_PORT;
1156 }
1157
1158 cupsdLogMessage(CUPSD_LOG_DEBUG,
1159 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1160 ldap_protocol, ldap_host, ldap_port);
1161
1162 /*
1163 * Initialize LDAP connection...
1164 */
1165
1166 if (! ldap_ssl)
1167 {
1168 if ((TempBrowseLDAPHandle = ldap_init(ldap_host, ldap_port)) == NULL)
1169 rc = LDAP_OPERATIONS_ERROR;
1170 else
1171 rc = LDAP_SUCCESS;
1172
1173 # ifdef HAVE_LDAP_SSL
1174 }
1175 else
1176 {
1177
1178 /*
1179 * Initialize SSL LDAP connection...
1180 */
1181 if (BrowseLDAPCACertFile)
1182 {
1183 rc = ldapssl_client_init(BrowseLDAPCACertFile, (void *)NULL);
1184 if (rc != LDAP_SUCCESS) {
1185 cupsdLogMessage(CUPSD_LOG_ERROR,
1186 "Failed to initialize LDAP SSL client!");
1187 rc = LDAP_OPERATIONS_ERROR;
1188 } else {
1189 if ((TempBrowseLDAPHandle = ldapssl_init(ldap_host, ldap_port, 1)) == NULL)
1190 rc = LDAP_OPERATIONS_ERROR;
1191 else
1192 rc = LDAP_SUCCESS;
1193 }
1194 }
1195 else
1196 {
1197 cupsdLogMessage(CUPSD_LOG_ERROR,
1198 "LDAP SSL certificate file/database not configured!");
1199 rc = LDAP_OPERATIONS_ERROR;
1200 }
1201
1202 # else /* HAVE_LDAP_SSL */
1203
1204 /*
1205 * Return error, because client libraries doesn't support SSL
1206 */
1207
1208 cupsdLogMessage(CUPSD_LOG_ERROR,
1209 "LDAP client libraries does not support TLS");
1210 rc = LDAP_OPERATIONS_ERROR;
1211
1212 # endif /* HAVE_LDAP_SSL */
1213 }
1214 # endif /* HAVE_OPENLDAP */
1215
1216 /*
1217 * Check return code from LDAP initialize...
1218 */
1219
1220 if (rc != LDAP_SUCCESS)
1221 {
1222 if ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR))
1223 {
1224 cupsdLogMessage(CUPSD_LOG_ERROR,
1225 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1226 }
1227 else
1228 {
1229 cupsdLogMessage(CUPSD_LOG_ERROR,
1230 "Unable to initialize LDAP! Disable LDAP browsing!");
1231 BrowseLocalProtocols &= ~BROWSE_LDAP;
1232 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1233 }
1234
1235 ldap_disconnect(TempBrowseLDAPHandle);
1236 TempBrowseLDAPHandle = NULL;
1237 }
1238
1239 /*
1240 * Upgrade LDAP version...
1241 */
1242
1243 else if (ldap_set_option(TempBrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION,
1244 (const void *)&version) != LDAP_SUCCESS)
1245 {
1246 cupsdLogMessage(CUPSD_LOG_ERROR,
1247 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1248 version);
1249 BrowseLocalProtocols &= ~BROWSE_LDAP;
1250 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1251 ldap_disconnect(TempBrowseLDAPHandle);
1252 TempBrowseLDAPHandle = NULL;
1253 }
1254 else
1255 {
1256
1257 /*
1258 * Register LDAP rebind procedure...
1259 */
1260
1261 # ifdef HAVE_LDAP_REBIND_PROC
1262 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1263
1264 rc = ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc, (void *)NULL);
1265 if ( rc != LDAP_SUCCESS )
1266 cupsdLogMessage(CUPSD_LOG_ERROR,
1267 "Setting LDAP rebind function failed with status %d: %s",
1268 rc, ldap_err2string(rc));
1269
1270 # else
1271
1272 ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc, (void *)NULL);
1273
1274 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1275 # endif /* HAVE_LDAP_REBIND_PROC */
1276
1277 /*
1278 * Start LDAP bind...
1279 */
1280
1281 # if LDAP_API_VERSION > 3000
1282 struct berval bval;
1283 bval.bv_val = BrowseLDAPPassword;
1284 bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword);
1285
1286 if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
1287 rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL,
1288 NULL, NULL);
1289 else
1290 rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE, &bval, NULL, NULL, NULL);
1291 # else
1292 rc = ldap_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN,
1293 BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
1294 # endif /* LDAP_API_VERSION > 3000 */
1295
1296 if (rc != LDAP_SUCCESS)
1297 {
1298 cupsdLogMessage(CUPSD_LOG_ERROR,
1299 "LDAP bind failed with error %d: %s",
1300 rc, ldap_err2string(rc));
1301 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1302 if (ldap_ssl && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR)))
1303 {
1304 ssl_err = PORT_GetError();
1305 if (ssl_err != 0)
1306 cupsdLogMessage(CUPSD_LOG_ERROR,
1307 "LDAP SSL error %d: %s",
1308 ssl_err, ldapssl_err2string(ssl_err));
1309 }
1310 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1311 ldap_disconnect(TempBrowseLDAPHandle);
1312 TempBrowseLDAPHandle = NULL;
1313 }
1314 else
1315 {
1316 cupsdLogMessage(CUPSD_LOG_INFO,
1317 "LDAP connection established");
1318 }
1319
1320 }
1321 return (TempBrowseLDAPHandle);
1322 }
1323
1324
1325 /*
1326 * 'ldap_reconnect()' - Reconnect to LDAP Server
1327 */
1328
1329 static void
1330 ldap_reconnect(void)
1331 {
1332 LDAP *TempBrowseLDAPHandle = NULL; /* Temp Handle to LDAP server */
1333
1334 cupsdLogMessage(CUPSD_LOG_INFO,
1335 "Try LDAP reconnect...");
1336
1337 /*
1338 * Get a new LDAP Handle and replace the global Handle
1339 * if the new connection was successful
1340 */
1341
1342 TempBrowseLDAPHandle = ldap_connect();
1343
1344 if (TempBrowseLDAPHandle != NULL)
1345 {
1346 if (BrowseLDAPHandle != NULL)
1347 {
1348 ldap_disconnect(BrowseLDAPHandle);
1349 }
1350 BrowseLDAPHandle = TempBrowseLDAPHandle;
1351 }
1352 }
1353
1354
1355 /*
1356 * 'ldap_disconnect()' - Disconnect from LDAP Server
1357 */
1358
1359 static void
1360 ldap_disconnect(LDAP *ld) /* I - LDAP handle */
1361 {
1362 int rc; /* return code */
1363
1364 /*
1365 * Close LDAP handle...
1366 */
1367
1368 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1369 rc = ldap_unbind_ext_s(ld, NULL, NULL);
1370 # else
1371 rc = ldap_unbind_s(ld);
1372 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1373 if (rc != LDAP_SUCCESS)
1374 cupsdLogMessage(CUPSD_LOG_ERROR,
1375 "Unbind from LDAP server failed with status %d: %s",
1376 rc, ldap_err2string(rc));
1377 }
1378 #endif /* HAVE_LDAP */
1379
1380
1381 /*
1382 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1383 */
1384
1385 void
1386 cupsdStartBrowsing(void)
1387 {
1388 int val; /* Socket option value */
1389 struct sockaddr_in addr; /* Broadcast address */
1390 cupsd_printer_t *p; /* Current printer */
1391
1392
1393 BrowseNext = NULL;
1394
1395 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
1396 return;
1397
1398 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
1399 {
1400 if (BrowseSocket < 0)
1401 {
1402 /*
1403 * Create the broadcast socket...
1404 */
1405
1406 if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
1407 {
1408 cupsdLogMessage(CUPSD_LOG_ERROR,
1409 "Unable to create broadcast socket - %s.",
1410 strerror(errno));
1411 BrowseLocalProtocols &= ~BROWSE_CUPS;
1412 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1413
1414 if (FatalErrors & CUPSD_FATAL_BROWSE)
1415 cupsdEndProcess(getpid(), 0);
1416 }
1417 }
1418
1419 if (BrowseSocket >= 0)
1420 {
1421 /*
1422 * Bind the socket to browse port...
1423 */
1424
1425 memset(&addr, 0, sizeof(addr));
1426 addr.sin_addr.s_addr = htonl(INADDR_ANY);
1427 addr.sin_family = AF_INET;
1428 addr.sin_port = htons(BrowsePort);
1429
1430 if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
1431 {
1432 cupsdLogMessage(CUPSD_LOG_ERROR,
1433 "Unable to bind broadcast socket - %s.",
1434 strerror(errno));
1435
1436 #ifdef WIN32
1437 closesocket(BrowseSocket);
1438 #else
1439 close(BrowseSocket);
1440 #endif /* WIN32 */
1441
1442 BrowseSocket = -1;
1443 BrowseLocalProtocols &= ~BROWSE_CUPS;
1444 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1445
1446 if (FatalErrors & CUPSD_FATAL_BROWSE)
1447 cupsdEndProcess(getpid(), 0);
1448 }
1449 }
1450
1451 if (BrowseSocket >= 0)
1452 {
1453 /*
1454 * Set the "broadcast" flag...
1455 */
1456
1457 val = 1;
1458 if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
1459 {
1460 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
1461 strerror(errno));
1462
1463 #ifdef WIN32
1464 closesocket(BrowseSocket);
1465 #else
1466 close(BrowseSocket);
1467 #endif /* WIN32 */
1468
1469 BrowseSocket = -1;
1470 BrowseLocalProtocols &= ~BROWSE_CUPS;
1471 BrowseRemoteProtocols &= ~BROWSE_CUPS;
1472
1473 if (FatalErrors & CUPSD_FATAL_BROWSE)
1474 cupsdEndProcess(getpid(), 0);
1475 }
1476 }
1477
1478 if (BrowseSocket >= 0)
1479 {
1480 /*
1481 * Close the socket on exec...
1482 */
1483
1484 fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
1485
1486 /*
1487 * Finally, add the socket to the input selection set as needed...
1488 */
1489
1490 if (BrowseRemoteProtocols & BROWSE_CUPS)
1491 {
1492 /*
1493 * We only listen if we want remote printers...
1494 */
1495
1496 cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
1497 NULL, NULL);
1498 }
1499 }
1500 }
1501 else
1502 BrowseSocket = -1;
1503
1504 #ifdef HAVE_DNSSD
1505 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
1506 {
1507 DNSServiceErrorType error; /* Error from service creation */
1508 cupsd_listener_t *lis; /* Current listening socket */
1509
1510
1511 /*
1512 * First create a "master" connection for all registrations...
1513 */
1514
1515 if ((error = DNSServiceCreateConnection(&DNSSDRef))
1516 != kDNSServiceErr_NoError)
1517 {
1518 cupsdLogMessage(CUPSD_LOG_ERROR,
1519 "Unable to create master DNS-SD reference: %d", error);
1520
1521 if (FatalErrors & CUPSD_FATAL_BROWSE)
1522 cupsdEndProcess(getpid(), 0);
1523 }
1524 else
1525 {
1526 /*
1527 * Add the master connection to the select list...
1528 */
1529
1530 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef),
1531 (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
1532
1533 /*
1534 * Then get the port we use for registrations. If we are not listening
1535 * on any non-local ports, there is no sense sharing local printers via
1536 * Bonjour...
1537 */
1538
1539 DNSSDPort = 0;
1540
1541 for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
1542 lis;
1543 lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
1544 {
1545 if (httpAddrLocalhost(&(lis->address)))
1546 continue;
1547
1548 if (lis->address.addr.sa_family == AF_INET)
1549 {
1550 DNSSDPort = ntohs(lis->address.ipv4.sin_port);
1551 break;
1552 }
1553 else if (lis->address.addr.sa_family == AF_INET6)
1554 {
1555 DNSSDPort = ntohs(lis->address.ipv6.sin6_port);
1556 break;
1557 }
1558 }
1559
1560 /*
1561 * Create an array to track the printers we share...
1562 */
1563
1564 if (BrowseRemoteProtocols & BROWSE_DNSSD)
1565 DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters,
1566 NULL);
1567
1568 /*
1569 * Set the computer name and register the web interface...
1570 */
1571
1572 cupsdUpdateDNSSDName();
1573 }
1574 }
1575 #endif /* HAVE_DNSSD */
1576
1577 #ifdef HAVE_LIBSLP
1578 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
1579 {
1580 /*
1581 * Open SLP handle...
1582 */
1583
1584 if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
1585 {
1586 cupsdLogMessage(CUPSD_LOG_ERROR,
1587 "Unable to open an SLP handle; disabling SLP browsing!");
1588 BrowseLocalProtocols &= ~BROWSE_SLP;
1589 BrowseRemoteProtocols &= ~BROWSE_SLP;
1590 BrowseSLPHandle = NULL;
1591
1592 if (FatalErrors & CUPSD_FATAL_BROWSE)
1593 cupsdEndProcess(getpid(), 0);
1594 }
1595
1596 BrowseSLPRefresh = 0;
1597 }
1598 else
1599 BrowseSLPHandle = NULL;
1600 #endif /* HAVE_LIBSLP */
1601
1602 #ifdef HAVE_LDAP
1603 if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP)
1604 {
1605 if (!BrowseLDAPDN)
1606 {
1607 cupsdLogMessage(CUPSD_LOG_ERROR,
1608 "Need to set BrowseLDAPDN to use LDAP browsing!");
1609 BrowseLocalProtocols &= ~BROWSE_LDAP;
1610 BrowseRemoteProtocols &= ~BROWSE_LDAP;
1611
1612 if (FatalErrors & CUPSD_FATAL_BROWSE)
1613 cupsdEndProcess(getpid(), 0);
1614 }
1615 else
1616 {
1617 /*
1618 * Open LDAP handle...
1619 */
1620
1621 if ((BrowseLDAPHandle = ldap_connect()) == NULL &&
1622 (FatalErrors & CUPSD_FATAL_BROWSE))
1623 cupsdEndProcess(getpid(), 0);
1624 }
1625
1626 BrowseLDAPRefresh = 0;
1627 }
1628 #endif /* HAVE_LDAP */
1629
1630 /*
1631 * Enable LPD and SMB printer sharing as needed through external programs...
1632 */
1633
1634 if (BrowseLocalProtocols & BROWSE_LPD)
1635 update_lpd(1);
1636
1637 if (BrowseLocalProtocols & BROWSE_SMB)
1638 update_smb(1);
1639
1640 /*
1641 * Register the individual printers
1642 */
1643
1644 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1645 p;
1646 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1647 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1648 cupsdRegisterPrinter(p);
1649 }
1650
1651
1652 /*
1653 * 'cupsdStartPolling()' - Start polling servers as needed.
1654 */
1655
1656 void
1657 cupsdStartPolling(void)
1658 {
1659 int i; /* Looping var */
1660 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1661 char polld[1024]; /* Poll daemon path */
1662 char sport[255]; /* Server port */
1663 char bport[255]; /* Browser port */
1664 char interval[255]; /* Poll interval */
1665 int statusfds[2]; /* Status pipe */
1666 char *argv[6]; /* Arguments */
1667 char *envp[100]; /* Environment */
1668
1669
1670 /*
1671 * Don't do anything if we aren't polling...
1672 */
1673
1674 if (NumPolled == 0 || BrowseSocket < 0)
1675 {
1676 PollPipe = -1;
1677 PollStatusBuffer = NULL;
1678 return;
1679 }
1680
1681 /*
1682 * Setup string arguments for polld, port and interval options.
1683 */
1684
1685 snprintf(polld, sizeof(polld), "%s/daemon/cups-polld", ServerBin);
1686
1687 sprintf(bport, "%d", BrowsePort);
1688
1689 if (BrowseInterval)
1690 sprintf(interval, "%d", BrowseInterval);
1691 else
1692 strcpy(interval, "30");
1693
1694 argv[0] = "cups-polld";
1695 argv[2] = sport;
1696 argv[3] = interval;
1697 argv[4] = bport;
1698 argv[5] = NULL;
1699
1700 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
1701
1702 /*
1703 * Create a pipe that receives the status messages from each
1704 * polling daemon...
1705 */
1706
1707 if (cupsdOpenPipe(statusfds))
1708 {
1709 cupsdLogMessage(CUPSD_LOG_ERROR,
1710 "Unable to create polling status pipes - %s.",
1711 strerror(errno));
1712 PollPipe = -1;
1713 PollStatusBuffer = NULL;
1714 return;
1715 }
1716
1717 PollPipe = statusfds[0];
1718 PollStatusBuffer = cupsdStatBufNew(PollPipe, "[Poll]");
1719
1720 /*
1721 * Run each polling daemon, redirecting stderr to the polling pipe...
1722 */
1723
1724 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1725 {
1726 sprintf(sport, "%d", pollp->port);
1727
1728 argv[1] = pollp->hostname;
1729
1730 if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1,
1731 0, DefaultProfile, &(pollp->pid)) < 0)
1732 {
1733 cupsdLogMessage(CUPSD_LOG_ERROR,
1734 "cupsdStartPolling: Unable to fork polling daemon - %s",
1735 strerror(errno));
1736 pollp->pid = 0;
1737 break;
1738 }
1739 else
1740 cupsdLogMessage(CUPSD_LOG_DEBUG,
1741 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1742 pollp->hostname, pollp->port, pollp->pid);
1743 }
1744
1745 close(statusfds[1]);
1746
1747 /*
1748 * Finally, add the pipe to the input selection set...
1749 */
1750
1751 cupsdAddSelect(PollPipe, (cupsd_selfunc_t)update_polling, NULL, NULL);
1752 }
1753
1754
1755 /*
1756 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1757 */
1758
1759 void
1760 cupsdStopBrowsing(void)
1761 {
1762 cupsd_printer_t *p; /* Current printer */
1763
1764
1765 if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
1766 return;
1767
1768 /*
1769 * De-register the individual printers
1770 */
1771
1772 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
1773 p;
1774 p = (cupsd_printer_t *)cupsArrayNext(Printers))
1775 if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
1776 cupsdDeregisterPrinter(p, 1);
1777
1778 /*
1779 * Shut down browsing sockets...
1780 */
1781
1782 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS) &&
1783 BrowseSocket >= 0)
1784 {
1785 /*
1786 * Close the socket and remove it from the input selection set.
1787 */
1788
1789 #ifdef WIN32
1790 closesocket(BrowseSocket);
1791 #else
1792 close(BrowseSocket);
1793 #endif /* WIN32 */
1794
1795 cupsdRemoveSelect(BrowseSocket);
1796 BrowseSocket = -1;
1797 }
1798
1799 #ifdef HAVE_DNSSD
1800 if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
1801 {
1802 if (WebIFRef)
1803 {
1804 DNSServiceRefDeallocate(WebIFRef);
1805 WebIFRef = NULL;
1806 }
1807
1808 if (RemoteRef)
1809 {
1810 DNSServiceRefDeallocate(RemoteRef);
1811 RemoteRef = NULL;
1812 }
1813
1814 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef));
1815
1816 DNSServiceRefDeallocate(DNSSDRef);
1817 DNSSDRef = NULL;
1818
1819 cupsArrayDelete(DNSSDPrinters);
1820 DNSSDPrinters = NULL;
1821
1822 DNSSDPort = 0;
1823 }
1824 #endif /* HAVE_DNSSD */
1825
1826 #ifdef HAVE_LIBSLP
1827 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP) &&
1828 BrowseSLPHandle)
1829 {
1830 /*
1831 * Close SLP handle...
1832 */
1833
1834 SLPClose(BrowseSLPHandle);
1835 BrowseSLPHandle = NULL;
1836 }
1837 #endif /* HAVE_LIBSLP */
1838
1839 #ifdef HAVE_LDAP
1840 if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) &&
1841 BrowseLDAPHandle)
1842 {
1843 ldap_dereg_ou(ServerName, BrowseLDAPDN);
1844 ldap_disconnect(BrowseLDAPHandle);
1845 BrowseLDAPHandle = NULL;
1846 }
1847 #endif /* HAVE_OPENLDAP */
1848
1849 /*
1850 * Disable LPD and SMB printer sharing as needed through external programs...
1851 */
1852
1853 if (BrowseLocalProtocols & BROWSE_LPD)
1854 update_lpd(0);
1855
1856 if (BrowseLocalProtocols & BROWSE_SMB)
1857 update_smb(0);
1858 }
1859
1860
1861 /*
1862 * 'cupsdStopPolling()' - Stop polling servers as needed.
1863 */
1864
1865 void
1866 cupsdStopPolling(void)
1867 {
1868 int i; /* Looping var */
1869 cupsd_dirsvc_poll_t *pollp; /* Current polling server */
1870
1871
1872 if (PollPipe >= 0)
1873 {
1874 cupsdStatBufDelete(PollStatusBuffer);
1875 close(PollPipe);
1876
1877 cupsdRemoveSelect(PollPipe);
1878
1879 PollPipe = -1;
1880 PollStatusBuffer = NULL;
1881 }
1882
1883 for (i = 0, pollp = Polled; i < NumPolled; i ++, pollp ++)
1884 if (pollp->pid)
1885 cupsdEndProcess(pollp->pid, 0);
1886 }
1887
1888
1889 #ifdef HAVE_DNSSD
1890 /*
1891 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1892 */
1893
1894 void
1895 cupsdUpdateDNSSDName(void)
1896 {
1897 DNSServiceErrorType error; /* Error from service creation */
1898 char webif[1024]; /* Web interface share name */
1899 #ifdef HAVE_COREFOUNDATION_H
1900 CFStringRef nameRef; /* Computer name CFString */
1901 char nameBuffer[1024]; /* C-string buffer */
1902 CFStringEncoding nameEncoding; /* Computer name encoding */
1903 #endif /* HAVE_COREFOUNDATION_H */
1904
1905
1906 /*
1907 * Only share the web interface and printers when non-local listening is
1908 * enabled...
1909 */
1910
1911 if (!DNSSDPort)
1912 return;
1913
1914 /*
1915 * Get the computer name as a c-string...
1916 */
1917
1918 #ifdef HAVE_COREFOUNDATION_H
1919 cupsdClearString(&DNSSDName);
1920
1921 if ((nameRef = SCDynamicStoreCopyComputerName(NULL,
1922 &nameEncoding)) != NULL)
1923 {
1924 if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
1925 kCFStringEncodingUTF8))
1926 cupsdSetString(&DNSSDName, nameBuffer);
1927
1928 CFRelease(nameRef);
1929 }
1930
1931 #else
1932 cupsdSetString(&DNSSDName, ServerName);
1933 #endif /* HAVE_COREFOUNDATION_H */
1934
1935 /*
1936 * Then (re)register the web interface if enabled...
1937 */
1938
1939 if (BrowseWebIF)
1940 {
1941 if (DNSSDName)
1942 snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDName);
1943 else
1944 strlcpy(webif, "CUPS Web Interface", sizeof(webif));
1945
1946 if (WebIFRef)
1947 DNSServiceRefDeallocate(WebIFRef);
1948
1949 WebIFRef = DNSSDRef;
1950 if ((error = DNSServiceRegister(&WebIFRef,
1951 kDNSServiceFlagsShareConnection,
1952 0, webif, "_http._tcp", NULL,
1953 NULL, htons(DNSSDPort), 7,
1954 "\006path=/", dnssdRegisterCallback,
1955 NULL)) != kDNSServiceErr_NoError)
1956 cupsdLogMessage(CUPSD_LOG_ERROR,
1957 "DNS-SD web interface registration failed: %d", error);
1958 }
1959 }
1960 #endif /* HAVE_DNSSD */
1961
1962
1963 #ifdef HAVE_LDAP
1964 /*
1965 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1966 */
1967
1968 void
1969 cupsdUpdateLDAPBrowse(void)
1970 {
1971 char uri[HTTP_MAX_URI], /* Printer URI */
1972 host[HTTP_MAX_URI], /* Hostname */
1973 resource[HTTP_MAX_URI], /* Resource path */
1974 location[1024], /* Printer location */
1975 info[1024], /* Printer information */
1976 make_model[1024], /* Printer make and model */
1977 type_num[30]; /* Printer type number */
1978 int type; /* Printer type */
1979 int rc; /* LDAP status */
1980 int limit; /* Size limit */
1981 LDAPMessage *res, /* LDAP search results */
1982 *e; /* Current entry from search */
1983
1984 cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
1985
1986 BrowseLDAPRefresh = time(NULL) + BrowseInterval;
1987
1988 /*
1989 * Reconnect if LDAP Handle is invalid...
1990 */
1991
1992 if (! BrowseLDAPHandle)
1993 {
1994 ldap_reconnect();
1995 return;
1996 }
1997
1998 /*
1999 * Search for cups printers in LDAP directory...
2000 */
2001
2002 rc = ldap_search_rec(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
2003 "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
2004
2005 /*
2006 * If ldap search was successfull then exit function
2007 * and temporary disable LDAP updates...
2008 */
2009
2010 if (rc != LDAP_SUCCESS)
2011 {
2012 if (BrowseLDAPUpdate && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR)))
2013 {
2014 BrowseLDAPUpdate = FALSE;
2015 cupsdLogMessage(CUPSD_LOG_INFO,
2016 "LDAP update temporary disabled");
2017 }
2018 return;
2019 }
2020
2021 /*
2022 * If LDAP updates were disabled, we will reenable them...
2023 */
2024
2025 if (! BrowseLDAPUpdate)
2026 {
2027 BrowseLDAPUpdate = TRUE;
2028 cupsdLogMessage(CUPSD_LOG_INFO,
2029 "LDAP update enabled");
2030 }
2031
2032 /*
2033 * Count LDAP entries and return if no entry exist...
2034 */
2035
2036 limit = ldap_count_entries(BrowseLDAPHandle, res);
2037 cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit);
2038 if (limit < 1)
2039 {
2040 ldap_freeres(res);
2041 return;
2042 }
2043
2044 /*
2045 * Loop through the available printers...
2046 */
2047
2048 for (e = ldap_first_entry(BrowseLDAPHandle, res);
2049 e;
2050 e = ldap_next_entry(BrowseLDAPHandle, e))
2051 {
2052 /*
2053 * Get the required values from this entry...
2054 */
2055
2056 if (ldap_getval_firststring(BrowseLDAPHandle, e,
2057 "printerDescription", info, sizeof(info)) == -1)
2058 continue;
2059
2060 if (ldap_getval_firststring(BrowseLDAPHandle, e,
2061 "printerLocation", location, sizeof(location)) == -1)
2062 continue;
2063
2064 if (ldap_getval_firststring(BrowseLDAPHandle, e,
2065 "printerMakeAndModel", make_model, sizeof(make_model)) == -1)
2066 continue;
2067
2068 if (ldap_getval_firststring(BrowseLDAPHandle, e,
2069 "printerType", type_num, sizeof(type_num)) == -1)
2070 continue;
2071
2072 type = atoi(type_num);
2073
2074 if (ldap_getval_firststring(BrowseLDAPHandle, e,
2075 "printerURI", uri, sizeof(uri)) == -1)
2076 continue;
2077
2078 /*
2079 * Process the entry as browse data...
2080 */
2081
2082 if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
2083 process_browse_data(uri, host, resource, type, IPP_PRINTER_IDLE,
2084 location, info, make_model, 0, NULL);
2085
2086 }
2087
2088 ldap_freeres(res);
2089 }
2090 #endif /* HAVE_LDAP */
2091
2092
2093 #ifdef HAVE_LIBSLP
2094 /*
2095 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2096 */
2097
2098 void
2099 cupsdUpdateSLPBrowse(void)
2100 {
2101 slpsrvurl_t *s, /* Temporary list of service URLs */
2102 *next; /* Next service in list */
2103 cupsd_printer_t p; /* Printer information */
2104 const char *uri; /* Pointer to printer URI */
2105 char host[HTTP_MAX_URI], /* Host portion of URI */
2106 resource[HTTP_MAX_URI]; /* Resource portion of URI */
2107
2108
2109 /*
2110 * Reset the refresh time...
2111 */
2112
2113 BrowseSLPRefresh = time(NULL) + BrowseInterval;
2114
2115 /*
2116 * Poll for remote printers using SLP...
2117 */
2118
2119 s = NULL;
2120
2121 SLPFindSrvs(BrowseSLPHandle, SLP_CUPS_SRVTYPE, "", "",
2122 slp_url_callback, &s);
2123
2124 /*
2125 * Loop through the list of available printers...
2126 */
2127
2128 for (; s; s = next)
2129 {
2130 /*
2131 * Save the "next" pointer...
2132 */
2133
2134 next = s->next;
2135
2136 /*
2137 * Load a cupsd_printer_t structure with the SLP service attributes...
2138 */
2139
2140 SLPFindAttrs(BrowseSLPHandle, s->url, "", "", slp_attr_callback, &p);
2141
2142 /*
2143 * Process this printer entry...
2144 */
2145
2146 uri = s->url + SLP_CUPS_SRVLEN + 1;
2147
2148 if (!strncmp(uri, "http://", 7) || !strncmp(uri, "ipp://", 6))
2149 {
2150 /*
2151 * Pull the URI apart to see if this is a local or remote printer...
2152 */
2153
2154 if (!is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
2155 process_browse_data(uri, host, resource, p.type, IPP_PRINTER_IDLE,
2156 p.location, p.info, p.make_model, 0, NULL);
2157 }
2158
2159 /*
2160 * Free this listing...
2161 */
2162
2163 cupsdClearString(&p.info);
2164 cupsdClearString(&p.location);
2165 cupsdClearString(&p.make_model);
2166
2167 free(s);
2168 }
2169 }
2170 #endif /* HAVE_LIBSLP */
2171
2172
2173 /*
2174 * 'dequote()' - Remote quotes from a string.
2175 */
2176
2177 static char * /* O - Dequoted string */
2178 dequote(char *d, /* I - Destination string */
2179 const char *s, /* I - Source string */
2180 int dlen) /* I - Destination length */
2181 {
2182 char *dptr; /* Pointer into destination */
2183
2184
2185 if (s)
2186 {
2187 for (dptr = d, dlen --; *s && dlen > 0; s ++)
2188 if (*s != '\"')
2189 {
2190 *dptr++ = *s;
2191 dlen --;
2192 }
2193
2194 *dptr = '\0';
2195 }
2196 else
2197 *d = '\0';
2198
2199 return (d);
2200 }
2201
2202
2203 #ifdef HAVE_DNSSD
2204 /*
2205 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2206 */
2207
2208 static char * /* O - TXT record */
2209 dnssdBuildTxtRecord(
2210 int *txt_len, /* O - TXT record length */
2211 cupsd_printer_t *p, /* I - Printer information */
2212 int for_lpd) /* I - 1 = LPD, 0 = IPP */
2213 {
2214 int i, j; /* Looping vars */
2215 char type_str[32], /* Type to string buffer */
2216 state_str[32], /* State to string buffer */
2217 rp_str[1024], /* Queue name string buffer */
2218 air_str[1024], /* auth-info-required string buffer */
2219 *keyvalue[32][2]; /* Table of key/value pairs */
2220
2221
2222 /*
2223 * Load up the key value pairs...
2224 */
2225
2226 i = 0;
2227
2228 keyvalue[i ][0] = "txtvers";
2229 keyvalue[i++][1] = "1";
2230
2231 keyvalue[i ][0] = "qtotal";
2232 keyvalue[i++][1] = "1";
2233
2234 keyvalue[i ][0] = "rp";
2235 keyvalue[i++][1] = rp_str;
2236 if (for_lpd)
2237 strlcpy(rp_str, p->name, sizeof(rp_str));
2238 else
2239 snprintf(rp_str, sizeof(rp_str), "%s/%s",
2240 (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
2241
2242 keyvalue[i ][0] = "ty";
2243 keyvalue[i++][1] = p->make_model;
2244
2245 if (p->location && *p->location != '\0')
2246 {
2247 keyvalue[i ][0] = "note";
2248 keyvalue[i++][1] = p->location;
2249 }
2250
2251 keyvalue[i ][0] = "priority";
2252 keyvalue[i++][1] = for_lpd ? "100" : "0";
2253
2254 keyvalue[i ][0] = "product";
2255 keyvalue[i++][1] = p->product ? p->product : "Unknown";
2256
2257 snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
2258 snprintf(state_str, sizeof(state_str), "%d", p->state);
2259
2260 keyvalue[i ][0] = "printer-state";
2261 keyvalue[i++][1] = state_str;
2262
2263 keyvalue[i ][0] = "printer-type";
2264 keyvalue[i++][1] = type_str;
2265
2266 keyvalue[i ][0] = "Transparent";
2267 keyvalue[i++][1] = "T";
2268
2269 keyvalue[i ][0] = "Binary";
2270 keyvalue[i++][1] = "T";
2271
2272 if ((p->type & CUPS_PRINTER_FAX))
2273 {
2274 keyvalue[i ][0] = "Fax";
2275 keyvalue[i++][1] = "T";
2276 }
2277
2278 if ((p->type & CUPS_PRINTER_COLOR))
2279 {
2280 keyvalue[i ][0] = "Color";
2281 keyvalue[i++][1] = "T";
2282 }
2283
2284 if ((p->type & CUPS_PRINTER_DUPLEX))
2285 {
2286 keyvalue[i ][0] = "Duplex";
2287 keyvalue[i++][1] = "T";
2288 }
2289
2290 if ((p->type & CUPS_PRINTER_STAPLE))
2291 {
2292 keyvalue[i ][0] = "Staple";
2293 keyvalue[i++][1] = "T";
2294 }
2295
2296 if ((p->type & CUPS_PRINTER_COPIES))
2297 {
2298 keyvalue[i ][0] = "Copies";
2299 keyvalue[i++][1] = "T";
2300 }
2301
2302 if ((p->type & CUPS_PRINTER_COLLATE))
2303 {
2304 keyvalue[i ][0] = "Collate";
2305 keyvalue[i++][1] = "T";
2306 }
2307
2308 if ((p->type & CUPS_PRINTER_PUNCH))
2309 {
2310 keyvalue[i ][0] = "Punch";
2311 keyvalue[i++][1] = "T";
2312 }
2313
2314 if ((p->type & CUPS_PRINTER_BIND))
2315 {
2316 keyvalue[i ][0] = "Bind";
2317 keyvalue[i++][1] = "T";
2318 }
2319
2320 if ((p->type & CUPS_PRINTER_SORT))
2321 {
2322 keyvalue[i ][0] = "Sort";
2323 keyvalue[i++][1] = "T";
2324 }
2325
2326 keyvalue[i ][0] = "pdl";
2327 keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
2328
2329 if (p->num_auth_info_required)
2330 {
2331 char *air = air_str; /* Pointer into string */
2332
2333
2334 for (j = 0; j < p->num_auth_info_required; j ++)
2335 {
2336 if (air >= (air_str + sizeof(air_str) - 2))
2337 break;
2338
2339 if (j)
2340 *air++ = ',';
2341
2342 strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str));
2343 air += strlen(air);
2344 }
2345
2346 keyvalue[i ][0] = "air";
2347 keyvalue[i++][1] = air_str;
2348 }
2349
2350 /*
2351 * Then pack them into a proper txt record...
2352 */
2353
2354 return (dnssdPackTxtRecord(txt_len, keyvalue, i));
2355 }
2356
2357
2358 /*
2359 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2360 */
2361
2362 static int /* O - Result of comparison */
2363 dnssdComparePrinters(cupsd_printer_t *a,/* I - First printer */
2364 cupsd_printer_t *b)/* I - Second printer */
2365 {
2366 return (strcasecmp(a->reg_name, b->reg_name));
2367 }
2368
2369
2370 /*
2371 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2372 * printer.
2373 */
2374
2375 static void
2376 dnssdDeregisterPrinter(
2377 cupsd_printer_t *p) /* I - Printer */
2378 {
2379 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdDeregisterPrinter(%s)", p->name);
2380
2381 /*
2382 * Closing the socket deregisters the service
2383 */
2384
2385 if (p->ipp_ref)
2386 {
2387 DNSServiceRefDeallocate(p->ipp_ref);
2388 p->ipp_ref = NULL;
2389 }
2390
2391 cupsArrayRemove(DNSSDPrinters, p);
2392 cupsdClearString(&p->reg_name);
2393
2394 if (p->ipp_txt)
2395 {
2396 /*
2397 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2398 */
2399
2400 free(p->ipp_txt);
2401 p->ipp_txt = NULL;
2402 }
2403 }
2404
2405
2406 /*
2407 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2408 * TXT record format.
2409 */
2410
2411 static char * /* O - TXT record */
2412 dnssdPackTxtRecord(int *txt_len, /* O - TXT record length */
2413 char *keyvalue[][2], /* I - Table of key value pairs */
2414 int count) /* I - Items in table */
2415 {
2416 int i; /* Looping var */
2417 int length; /* Length of TXT record */
2418 int length2; /* Length of value */
2419 char *txtRecord; /* TXT record buffer */
2420 char *cursor; /* Looping pointer */
2421
2422
2423 /*
2424 * Calculate the buffer size
2425 */
2426
2427 for (length = i = 0; i < count; i++)
2428 length += 1 + strlen(keyvalue[i][0]) +
2429 (keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
2430
2431 /*
2432 * Allocate and fill it
2433 */
2434
2435 txtRecord = malloc(length);
2436 if (txtRecord)
2437 {
2438 *txt_len = length;
2439
2440 for (cursor = txtRecord, i = 0; i < count; i++)
2441 {
2442 /*
2443 * Drop in the p-string style length byte followed by the data
2444 */
2445
2446 length = strlen(keyvalue[i][0]);
2447 length2 = keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0;
2448
2449 *cursor++ = (unsigned char)(length + length2);
2450
2451 memcpy(cursor, keyvalue[i][0], length);
2452 cursor += length;
2453
2454 if (length2)
2455 {
2456 length2 --;
2457 *cursor++ = '=';
2458 memcpy(cursor, keyvalue[i][1], length2);
2459 cursor += length2;
2460 }
2461 }
2462 }
2463
2464 return (txtRecord);
2465 }
2466
2467
2468 /*
2469 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2470 */
2471
2472 static void
2473 dnssdRegisterCallback(
2474 DNSServiceRef sdRef, /* I - DNS Service reference */
2475 DNSServiceFlags flags, /* I - Reserved for future use */
2476 DNSServiceErrorType errorCode, /* I - Error code */
2477 const char *name, /* I - Service name */
2478 const char *regtype, /* I - Service type */
2479 const char *domain, /* I - Domain. ".local" for now */
2480 void *context) /* I - User-defined context */
2481 {
2482 cupsd_printer_t *p = (cupsd_printer_t *)context;
2483 /* Current printer */
2484
2485
2486 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s",
2487 name, regtype, p ? p->name : "Web Interface");
2488
2489 if (errorCode)
2490 {
2491 cupsdLogMessage(CUPSD_LOG_ERROR,
2492 "DNSServiceRegister failed with error %d", (int)errorCode);
2493 return;
2494 }
2495 else if (p && strcasecmp(name, p->reg_name))
2496 {
2497 cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
2498 name, p->name);
2499
2500 cupsArrayRemove(DNSSDPrinters, p);
2501 cupsdSetString(&p->reg_name, name);
2502 cupsArrayAdd(DNSSDPrinters, p);
2503
2504 LastEvent |= CUPSD_EVENT_PRINTER_MODIFIED;
2505 }
2506 }
2507
2508
2509 /*
2510 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2511 * or update the broadcast contents.
2512 */
2513
2514 static void
2515 dnssdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
2516 {
2517 DNSServiceErrorType se; /* dnssd errors */
2518 char *ipp_txt, /* IPP TXT record buffer */
2519 *printer_txt, /* LPD TXT record buffer */
2520 name[1024], /* Service name */
2521 *nameptr; /* Pointer into name */
2522 int ipp_len, /* IPP TXT record length */
2523 printer_len; /* LPD TXT record length */
2524 char resource[1024]; /* Resource path for printer */
2525 const char *regtype; /* Registration type */
2526 const char *domain; /* Registration domain */
2527 cupsd_location_t *location, /* Printer location */
2528 *policy; /* Operation policy for Print-Job */
2529 unsigned address[4]; /* INADDR_ANY address */
2530
2531
2532 cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
2533 !p->ipp_ref ? "new" : "update");
2534
2535 /*
2536 * If per-printer sharing was just disabled make sure we're not
2537 * registered before returning.
2538 */
2539
2540 if (!p->shared)
2541 {
2542 dnssdDeregisterPrinter(p);
2543 return;
2544 }
2545
2546 /*
2547 * The registered name takes the form of "<printer-info> @ <computer name>"...
2548 */
2549
2550 if (p->info && strlen(p->info) > 0)
2551 {
2552 if (DNSSDName)
2553 snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDName);
2554 else
2555 strlcpy(name, p->info, sizeof(name));
2556 }
2557 else if (DNSSDName)
2558 snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDName);
2559 else
2560 strlcpy(name, p->name, sizeof(name));
2561
2562 /*
2563 * If an existing printer was renamed, unregister it and start over...
2564 */
2565
2566 if (p->reg_name && strcmp(p->reg_name, name))
2567 dnssdDeregisterPrinter(p);
2568
2569 if (!p->reg_name)
2570 {
2571 cupsdSetString(&p->reg_name, name);
2572 cupsArrayAdd(DNSSDPrinters, p);
2573 }
2574
2575 /*
2576 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2577 * let dnssd decide on the domain, otherwise restrict it to ".local".
2578 */
2579
2580 if (p->type & CUPS_PRINTER_CLASS)
2581 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
2582 else
2583 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
2584
2585 address[0] = address[1] = address[2] = address[3] = 0;
2586 location = cupsdFindBest(resource, HTTP_POST);
2587 policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
2588
2589 if ((location && !cupsdCheckAccess(address, "", 0, location)) ||
2590 (policy && !cupsdCheckAccess(address, "", 0, policy)))
2591 domain = "local.";
2592 else
2593 domain = NULL;
2594
2595 /*
2596 * Register IPP and (optionally) LPD...
2597 */
2598
2599 ipp_len = 0; /* anti-compiler-warning-code */
2600 ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
2601
2602 if (!p->ipp_ref)
2603 {
2604 /*
2605 * Initial registration. Use the _fax subtype for fax queues...
2606 */
2607
2608 regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" :
2609 "_ipp._tcp,_cups";
2610
2611 cupsdLogMessage(CUPSD_LOG_DEBUG,
2612 "Registering DNS-SD printer %s with name \"%s\", "
2613 "type \"%s\", and domain \"%s\"", p->name, name, regtype,
2614 domain ? domain : "(null)");
2615
2616 /*
2617 * Register the queue, dropping characters as needed until we succeed...
2618 */
2619
2620 nameptr = name + strlen(name);
2621
2622 do
2623 {
2624 p->ipp_ref = DNSSDRef;
2625 if ((se = DNSServiceRegister(&p->ipp_ref, kDNSServiceFlagsShareConnection,
2626 0, name, regtype, domain, NULL,
2627 htons(DNSSDPort), ipp_len, ipp_txt,
2628 dnssdRegisterCallback,
2629 p)) == kDNSServiceErr_BadParam)
2630 {
2631 /*
2632 * Name is too long, drop trailing characters, taking into account
2633 * UTF-8 encoding...
2634 */
2635
2636 nameptr --;
2637
2638 while (nameptr > name && (*nameptr & 0xc0) == 0x80)
2639 nameptr --;
2640
2641 if (nameptr > name)
2642 *nameptr = '\0';
2643 }
2644 }
2645 while (se == kDNSServiceErr_BadParam && nameptr > name);
2646
2647 if (se == kDNSServiceErr_NoError)
2648 {
2649 p->ipp_txt = ipp_txt;
2650 p->ipp_len = ipp_len;
2651 ipp_txt = NULL;
2652 }
2653 else
2654 cupsdLogMessage(CUPSD_LOG_WARN,
2655 "DNS-SD IPP registration of \"%s\" failed: %d",
2656 p->name, se);
2657 }
2658 else if (ipp_len != p->ipp_len || memcmp(ipp_txt, p->ipp_txt, ipp_len))
2659 {
2660 /*
2661 * Update the existing registration...
2662 */
2663
2664 /* A TTL of 0 means use record's original value (Radar 3176248) */
2665 DNSServiceUpdateRecord(p->ipp_ref, NULL, 0, ipp_len, ipp_txt, 0);
2666
2667 if (p->ipp_txt)
2668 free(p->ipp_txt);
2669
2670 p->ipp_txt = ipp_txt;
2671 p->ipp_len = ipp_len;
2672 ipp_txt = NULL;
2673 }
2674
2675 if (ipp_txt)
2676 free(ipp_txt);
2677
2678 if (BrowseLocalProtocols & BROWSE_LPD)
2679 {
2680 printer_len = 0; /* anti-compiler-warning-code */
2681 printer_txt = dnssdBuildTxtRecord(&printer_len, p, 1);
2682
2683 if (!p->printer_ref)
2684 {
2685 /*
2686 * Initial registration...
2687 */
2688
2689 cupsdLogMessage(CUPSD_LOG_DEBUG,
2690 "Registering DNS-SD printer %s with name \"%s\", "
2691 "type \"_printer._tcp\", and domain \"%s\"", p->name,
2692 name, domain ? domain : "(null)");
2693
2694 p->printer_ref = DNSSDRef;
2695 if ((se = DNSServiceRegister(&p->printer_ref,
2696 kDNSServiceFlagsShareConnection,
2697 0, name, "_printer._tcp", domain, NULL,
2698 htons(515), printer_len, printer_txt,
2699 dnssdRegisterCallback,
2700 p)) == kDNSServiceErr_NoError)
2701 {
2702 p->printer_txt = printer_txt;
2703 p->printer_len = printer_len;
2704 printer_txt = NULL;
2705 }
2706 else
2707 cupsdLogMessage(CUPSD_LOG_WARN,
2708 "DNS-SD LPD registration of \"%s\" failed: %d",
2709 p->name, se);
2710 }
2711 else if (printer_len != p->printer_len ||
2712 memcmp(printer_txt, p->printer_txt, printer_len))
2713 {
2714 /*
2715 * Update the existing registration...
2716 */
2717
2718 /* A TTL of 0 means use record's original value (Radar 3176248) */
2719 DNSServiceUpdateRecord(p->printer_ref, NULL, 0, printer_len,
2720 printer_txt, 0);
2721
2722 if (p->printer_txt)
2723 free(p->printer_txt);
2724
2725 p->printer_txt = printer_txt;
2726 p->printer_len = printer_len;
2727 printer_txt = NULL;
2728 }
2729
2730 if (printer_txt)
2731 free(printer_txt);
2732 }
2733 }
2734
2735
2736 /*
2737 * 'dnssdUpdate()' - Handle DNS-SD queries.
2738 */
2739
2740 static void
2741 dnssdUpdate(void)
2742 {
2743 DNSServiceErrorType sdErr; /* Service discovery error */
2744
2745
2746 if ((sdErr = DNSServiceProcessResult(DNSSDRef)) != kDNSServiceErr_NoError)
2747 cupsdLogMessage(CUPSD_LOG_ERROR,
2748 "DNS Service Discovery registration error %d!",
2749 sdErr);
2750 }
2751 #endif /* HAVE_DNSSD */
2752
2753
2754 #ifdef __APPLE__
2755 /*
2756 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2757 */
2758
2759 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2760 get_hostconfig(const char *name) /* I - Name of service */
2761 {
2762 cups_file_t *fp; /* Hostconfig file */
2763 char line[1024], /* Line from file */
2764 *ptr; /* Pointer to value */
2765 int state = 1; /* State of service */
2766
2767
2768 /*
2769 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2770 * the service is enabled/auto.
2771 */
2772
2773 if ((fp = cupsFileOpen("/etc/hostconfig", "r")) != NULL)
2774 {
2775 /*
2776 * Read lines from the file until we find the service...
2777 */
2778
2779 while (cupsFileGets(fp, line, sizeof(line)))
2780 {
2781 if (line[0] == '#' || (ptr = strchr(line, '=')) == NULL)
2782 continue;
2783
2784 *ptr++ = '\0';
2785
2786 if (!strcasecmp(line, name))
2787 {
2788 /*
2789 * Found the service, see if it is set to "-NO-"...
2790 */
2791
2792 if (!strncasecmp(ptr, "-NO-", 4))
2793 state = 0;
2794 break;
2795 }
2796 }
2797
2798 cupsFileClose(fp);
2799 }
2800
2801 return (state);
2802 }
2803 #endif /* __APPLE__ */
2804
2805
2806 /*
2807 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2808 */
2809
2810 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2811 is_local_queue(const char *uri, /* I - Printer URI */
2812 char *host, /* O - Host string */
2813 int hostlen, /* I - Length of host buffer */
2814 char *resource, /* O - Resource string */
2815 int resourcelen) /* I - Length of resource buffer */
2816 {
2817 char scheme[32], /* Scheme portion of URI */
2818 username[HTTP_MAX_URI]; /* Username portion of URI */
2819 int port; /* Port portion of URI */
2820 cupsd_netif_t *iface; /* Network interface */
2821
2822
2823 /*
2824 * Pull the URI apart to see if this is a local or remote printer...
2825 */
2826
2827 if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
2828 username, sizeof(username), host, hostlen, &port,
2829 resource, resourcelen) < HTTP_URI_OK)
2830 return (-1);
2831
2832 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host, ServerName));
2833
2834 /*
2835 * Check for local server addresses...
2836 */
2837
2838 if (!strcasecmp(host, ServerName) && port == LocalPort)
2839 return (1);
2840
2841 cupsdNetIFUpdate();
2842
2843 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
2844 iface;
2845 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
2846 if (!strcasecmp(host, iface->hostname) && port == iface->port)
2847 return (1);
2848
2849 /*
2850 * If we get here, the printer is remote...
2851 */
2852
2853 return (0);
2854 }
2855
2856
2857 /*
2858 * 'process_browse_data()' - Process new browse data.
2859 */
2860
2861 static void
2862 process_browse_data(
2863 const char *uri, /* I - URI of printer/class */
2864 const char *host, /* I - Hostname */
2865 const char *resource, /* I - Resource path */
2866 cups_ptype_t type, /* I - Printer type */
2867 ipp_pstate_t state, /* I - Printer state */
2868 const char *location, /* I - Printer location */
2869 const char *info, /* I - Printer information */
2870 const char *make_model, /* I - Printer make and model */
2871 int num_attrs, /* I - Number of attributes */
2872 cups_option_t *attrs) /* I - Attributes */
2873 {
2874 int i; /* Looping var */
2875 int update; /* Update printer attributes? */
2876 char finaluri[HTTP_MAX_URI], /* Final URI for printer */
2877 name[IPP_MAX_NAME], /* Name of printer */
2878 newname[IPP_MAX_NAME], /* New name of printer */
2879 *hptr, /* Pointer into hostname */
2880 *sptr; /* Pointer into ServerName */
2881 const char *shortname; /* Short queue name (queue) */
2882 char local_make_model[IPP_MAX_NAME];
2883 /* Local make and model */
2884 cupsd_printer_t *p; /* Printer information */
2885 const char *ipp_options, /* ipp-options value */
2886 *lease_duration; /* lease-duration value */
2887 int is_class; /* Is this queue a class? */
2888
2889
2890 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2891 "process_browse_data(uri=\"%s\", host=\"%s\", "
2892 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2893 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2894 uri, host, resource, type, state,
2895 location ? location : "(nil)", info ? info : "(nil)",
2896 make_model ? make_model : "(nil)", num_attrs, attrs);
2897
2898 /*
2899 * Determine if the URI contains any illegal characters in it...
2900 */
2901
2902 if (strncmp(uri, "ipp://", 6) || !host[0] ||
2903 (strncmp(resource, "/printers/", 10) &&
2904 strncmp(resource, "/classes/", 9)))
2905 {
2906 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad printer URI in browse data: %s", uri);
2907 return;
2908 }
2909
2910 if (strchr(resource, '?') ||
2911 (!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
2912 (!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
2913 {
2914 cupsdLogMessage(CUPSD_LOG_ERROR, "Bad resource in browse data: %s",
2915 resource);
2916 return;
2917 }
2918
2919 /*
2920 * OK, this isn't a local printer; add any remote options...
2921 */
2922
2923 ipp_options = cupsGetOption("ipp-options", num_attrs, attrs);
2924
2925 if (BrowseRemoteOptions)
2926 {
2927 if (BrowseRemoteOptions[0] == '?')
2928 {
2929 /*
2930 * Override server-supplied options...
2931 */
2932
2933 snprintf(finaluri, sizeof(finaluri), "%s%s", uri, BrowseRemoteOptions);
2934 }
2935 else if (ipp_options)
2936 {
2937 /*
2938 * Combine the server and local options...
2939 */
2940
2941 snprintf(finaluri, sizeof(finaluri), "%s?%s+%s", uri, ipp_options,
2942 BrowseRemoteOptions);
2943 }
2944 else
2945 {
2946 /*
2947 * Just use the local options...
2948 */
2949
2950 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, BrowseRemoteOptions);
2951 }
2952
2953 uri = finaluri;
2954 }
2955 else if (ipp_options)
2956 {
2957 /*
2958 * Just use the server-supplied options...
2959 */
2960
2961 snprintf(finaluri, sizeof(finaluri), "%s?%s", uri, ipp_options);
2962 uri = finaluri;
2963 }
2964
2965 /*
2966 * See if we already have it listed in the Printers list, and add it if not...
2967 */
2968
2969 type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
2970 type &= ~CUPS_PRINTER_IMPLICIT;
2971 update = 0;
2972 hptr = strchr(host, '.');
2973 sptr = strchr(ServerName, '.');
2974 is_class = type & CUPS_PRINTER_CLASS;
2975
2976 if (!ServerNameIsIP && sptr != NULL && hptr != NULL)
2977 {
2978 /*
2979 * Strip the common domain name components...
2980 */
2981
2982 while (hptr != NULL)
2983 {
2984 if (!strcasecmp(hptr, sptr))
2985 {
2986 *hptr = '\0';
2987 break;
2988 }
2989 else
2990 hptr = strchr(hptr + 1, '.');
2991 }
2992 }
2993
2994 if (is_class)
2995 {
2996 /*
2997 * Remote destination is a class...
2998 */
2999
3000 if (!strncmp(resource, "/classes/", 9))
3001 snprintf(name, sizeof(name), "%s@%s", resource + 9, host);
3002 else
3003 return;
3004
3005 shortname = resource + 9;
3006 }
3007 else
3008 {
3009 /*
3010 * Remote destination is a printer...
3011 */
3012
3013 if (!strncmp(resource, "/printers/", 10))
3014 snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
3015 else
3016 return;
3017
3018 shortname = resource + 10;
3019 }
3020
3021 if (hptr && !*hptr)
3022 *hptr = '.'; /* Resource FQDN */
3023
3024 if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
3025 {
3026 /*
3027 * Long name doesn't exist, try short name...
3028 */
3029
3030 cupsdLogMessage(CUPSD_LOG_DEBUG, "process_browse_data: %s not found...",
3031 name);
3032
3033 if ((p = cupsdFindDest(shortname)) == NULL)
3034 {
3035 /*
3036 * Short name doesn't exist, use it for this shared queue.
3037 */
3038
3039 cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_browse_data: %s not found...",
3040 shortname);
3041 strlcpy(name, shortname, sizeof(name));
3042 }
3043 else
3044 {
3045 /*
3046 * Short name exists...
3047 */
3048
3049 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3050 "process_browse_data: %s found, type=%x, hostname=%s...",
3051 shortname, p->type, p->hostname ? p->hostname : "(nil)");
3052
3053 if (p->type & CUPS_PRINTER_IMPLICIT)
3054 p = NULL; /* Don't replace implicit classes */
3055 else if (p->hostname && strcasecmp(p->hostname, host))
3056 {
3057 /*
3058 * Short name exists but is for a different host. If this is a remote
3059 * queue, rename it and use the long name...
3060 */
3061
3062 if (p->type & CUPS_PRINTER_REMOTE)
3063 {
3064 cupsdLogMessage(CUPSD_LOG_DEBUG,
3065 "Renamed remote %s \"%s\" to \"%s@%s\"...",
3066 is_class ? "class" : "printer", p->name, p->name,
3067 p->hostname);
3068 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
3069 "%s \'%s\' deleted by directory services.",
3070 is_class ? "Class" : "Printer", p->name);
3071
3072 snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
3073 cupsdRenamePrinter(p, newname);
3074
3075 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
3076 "%s \'%s\' added by directory services.",
3077 is_class ? "Class" : "Printer", p->name);
3078 }
3079
3080 /*
3081 * Force creation with long name...
3082 */
3083
3084 p = NULL;
3085 }
3086 }
3087 }
3088 else if (p)
3089 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3090 "process_browse_data: %s found, type=%x, hostname=%s...",
3091 name, p->type, p->hostname ? p->hostname : "(nil)");
3092
3093 if (!p)
3094 {
3095 /*
3096 * Queue doesn't exist; add it...
3097 */
3098
3099 if (is_class)
3100 p = cupsdAddClass(name);
3101 else
3102 p = cupsdAddPrinter(name);
3103
3104 if (!p)
3105 return;
3106
3107 cupsdClearString(&(p->hostname));
3108
3109 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote %s \"%s\"...",
3110 is_class ? "class" : "printer", name);
3111
3112 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
3113 "%s \'%s\' added by directory services.",
3114 is_class ? "Class" : "Printer", name);
3115
3116 /*
3117 * Force the URI to point to the real server...
3118 */
3119
3120 p->type = type & ~CUPS_PRINTER_REJECTING;
3121 p->accepting = 1;
3122
3123 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
3124 }
3125
3126 if (!p->hostname)
3127 {
3128 /*
3129 * Hostname not set, so this must be a cached remote printer
3130 * that was created for a pending print job...
3131 */
3132
3133 cupsdSetString(&p->hostname, host);
3134 cupsdSetString(&p->uri, uri);
3135 cupsdSetString(&p->device_uri, uri);
3136 update = 1;
3137
3138 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3139 }
3140
3141 /*
3142 * Update the state...
3143 */
3144
3145 p->state = state;
3146 p->browse_time = time(NULL);
3147
3148 if ((lease_duration = cupsGetOption("lease-duration", num_attrs,
3149 attrs)) != NULL)
3150 {
3151 /*
3152 * Grab the lease-duration for the browse data; anything less then 1
3153 * second or more than 1 week gets the default BrowseTimeout...
3154 */
3155
3156 i = atoi(lease_duration);
3157 if (i < 1 || i > 604800)
3158 i = BrowseTimeout;
3159
3160 p->browse_expire = p->browse_time + i;
3161 }
3162 else
3163 p->browse_expire = p->browse_time + BrowseTimeout;
3164
3165 if (type & CUPS_PRINTER_REJECTING)
3166 {
3167 type &= ~CUPS_PRINTER_REJECTING;
3168
3169 if (p->accepting)
3170 {
3171 update = 1;
3172 p->accepting = 0;
3173 }
3174 }
3175 else if (!p->accepting)
3176 {
3177 update = 1;
3178 p->accepting = 1;
3179 }
3180
3181 if (p->type != type)
3182 {
3183 p->type = type;
3184 update = 1;
3185 }
3186
3187 if (location && (!p->location || strcmp(p->location, location)))
3188 {
3189 cupsdSetString(&p->location, location);
3190 update = 1;
3191 }
3192
3193 if (info && (!p->info || strcmp(p->info, info)))
3194 {
3195 cupsdSetString(&p->info, info);
3196 update = 1;
3197
3198 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
3199 }
3200
3201 if (!make_model || !make_model[0])
3202 {
3203 if (is_class)
3204 snprintf(local_make_model, sizeof(local_make_model),
3205 "Remote Class on %s", host);
3206 else
3207 snprintf(local_make_model, sizeof(local_make_model),
3208 "Remote Printer on %s", host);
3209 }
3210 else
3211 snprintf(local_make_model, sizeof(local_make_model),
3212 "%s on %s", make_model, host);
3213
3214 if (!p->make_model || strcmp(p->make_model, local_make_model))
3215 {
3216 cupsdSetString(&p->make_model, local_make_model);
3217 update = 1;
3218 }
3219
3220 if (p->num_options)
3221 {
3222 if (!update && !(type & CUPS_PRINTER_DELETE))
3223 {
3224 /*
3225 * See if we need to update the attributes...
3226 */
3227
3228 if (p->num_options != num_attrs)
3229 update = 1;
3230 else
3231 {
3232 for (i = 0; i < num_attrs; i ++)
3233 if (strcmp(attrs[i].name, p->options[i].name) ||
3234 (!attrs[i].value != !p->options[i].value) ||
3235 (attrs[i].value && strcmp(attrs[i].value, p->options[i].value)))
3236 {
3237 update = 1;
3238 break;
3239 }
3240 }
3241 }
3242
3243 /*
3244 * Free the old options...
3245 */
3246
3247 cupsFreeOptions(p->num_options, p->options);
3248 }
3249
3250 p->num_options = num_attrs;
3251 p->options = attrs;
3252
3253 if (type & CUPS_PRINTER_DELETE)
3254 {
3255 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
3256 "%s \'%s\' deleted by directory services.",
3257 is_class ? "Class" : "Printer", p->name);
3258
3259 cupsdExpireSubscriptions(p, NULL);
3260
3261 cupsdDeletePrinter(p, 1);
3262 cupsdUpdateImplicitClasses();
3263 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
3264 }
3265 else if (update)
3266 {
3267 cupsdSetPrinterAttrs(p);
3268 cupsdUpdateImplicitClasses();
3269 }
3270
3271 /*
3272 * See if we have a default printer... If not, make the first network
3273 * default printer the default.
3274 */
3275
3276 if (DefaultPrinter == NULL && Printers != NULL && UseNetworkDefault)
3277 {
3278 /*
3279 * Find the first network default printer and use it...
3280 */
3281
3282 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3283 p;
3284 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3285 if (p->type & CUPS_PRINTER_DEFAULT)
3286 {
3287 DefaultPrinter = p;
3288 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
3289 break;
3290 }
3291 }
3292
3293 /*
3294 * Do auto-classing if needed...
3295 */
3296
3297 process_implicit_classes();
3298 }
3299
3300
3301 /*
3302 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3303 */
3304
3305 static void
3306 process_implicit_classes(void)
3307 {
3308 int i; /* Looping var */
3309 int update; /* Update printer attributes? */
3310 char name[IPP_MAX_NAME], /* Name of printer */
3311 *hptr; /* Pointer into hostname */
3312 cupsd_printer_t *p, /* Printer information */
3313 *pclass, /* Printer class */
3314 *first; /* First printer in class */
3315 int offset, /* Offset of name */
3316 len; /* Length of name */
3317
3318
3319 if (!ImplicitClasses || !Printers)
3320 return;
3321
3322 /*
3323 * Loop through all available printers and create classes as needed...
3324 */
3325
3326 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers), len = 0, offset = 0,
3327 update = 0, pclass = NULL, first = NULL;
3328 p != NULL;
3329 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3330 {
3331 /*
3332 * Skip implicit classes...
3333 */
3334
3335 if (p->type & CUPS_PRINTER_IMPLICIT)
3336 {
3337 len = 0;
3338 continue;
3339 }
3340
3341 /*
3342 * If len == 0, get the length of this printer name up to the "@"
3343 * sign (if any).
3344 */
3345
3346 cupsArraySave(Printers);
3347
3348 if (len > 0 &&
3349 !strncasecmp(p->name, name + offset, len) &&
3350 (p->name[len] == '\0' || p->name[len] == '@'))
3351 {
3352 /*
3353 * We have more than one printer with the same name; see if
3354 * we have a class, and if this printer is a member...
3355 */
3356
3357 if (pclass && strcasecmp(pclass->name, name))
3358 {
3359 if (update)
3360 cupsdSetPrinterAttrs(pclass);
3361
3362 update = 0;
3363 pclass = NULL;
3364 }
3365
3366 if (!pclass && (pclass = cupsdFindDest(name)) == NULL)
3367 {
3368 /*
3369 * Need to add the class...
3370 */
3371
3372 pclass = cupsdAddPrinter(name);
3373 cupsArrayAdd(ImplicitPrinters, pclass);
3374
3375 pclass->type |= CUPS_PRINTER_IMPLICIT;
3376 pclass->accepting = 1;
3377 pclass->state = IPP_PRINTER_IDLE;
3378
3379 cupsdSetString(&pclass->location, p->location);
3380 cupsdSetString(&pclass->info, p->info);
3381
3382 cupsdSetString(&pclass->job_sheets[0], p->job_sheets[0]);
3383 cupsdSetString(&pclass->job_sheets[1], p->job_sheets[1]);
3384
3385 update = 1;
3386
3387 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
3388
3389 cupsdLogMessage(CUPSD_LOG_DEBUG, "Added implicit class \"%s\"...",
3390 name);
3391 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
3392 "Implicit class \'%s\' added by directory services.",
3393 name);
3394 }
3395
3396 if (first != NULL)
3397 {
3398 for (i = 0; i < pclass->num_printers; i ++)
3399 if (pclass->printers[i] == first)
3400 break;
3401
3402 if (i >= pclass->num_printers)
3403 {
3404 first->in_implicit_class = 1;
3405 cupsdAddPrinterToClass(pclass, first);
3406 }
3407
3408 first = NULL;
3409 }
3410
3411 for (i = 0; i < pclass->num_printers; i ++)
3412 if (pclass->printers[i] == p)
3413 break;
3414
3415 if (i >= pclass->num_printers)
3416 {
3417 p->in_implicit_class = 1;
3418 cupsdAddPrinterToClass(pclass, p);
3419 update = 1;
3420 }
3421 }
3422 else
3423 {
3424 /*
3425 * First time around; just get name length and mark it as first
3426 * in the list...
3427 */
3428
3429 if ((hptr = strchr(p->name, '@')) != NULL)
3430 len = hptr - p->name;
3431 else
3432 len = strlen(p->name);
3433
3434 strncpy(name, p->name, len);
3435 name[len] = '\0';
3436 offset = 0;
3437
3438 if ((first = (hptr ? cupsdFindDest(name) : p)) != NULL &&
3439 !(first->type & CUPS_PRINTER_IMPLICIT))
3440 {
3441 /*
3442 * Can't use same name as a local printer; add "Any" to the
3443 * front of the name, unless we have explicitly disabled
3444 * the "ImplicitAnyClasses"...
3445 */
3446
3447 if (ImplicitAnyClasses && len < (sizeof(name) - 4))
3448 {
3449 /*
3450 * Add "Any" to the class name...
3451 */
3452
3453 strcpy(name, "Any");
3454 strncpy(name + 3, p->name, len);
3455 name[len + 3] = '\0';
3456 offset = 3;
3457 }
3458 else
3459 {
3460 /*
3461 * Don't create an implicit class if we have a local printer
3462 * with the same name...
3463 */
3464
3465 len = 0;
3466 cupsArrayRestore(Printers);
3467 continue;
3468 }
3469 }
3470
3471 first = p;
3472 }
3473
3474 cupsArrayRestore(Printers);
3475 }
3476
3477 /*
3478 * Update the last printer class as needed...
3479 */
3480
3481 if (pclass && update)
3482 cupsdSetPrinterAttrs(pclass);
3483 }
3484
3485
3486 /*
3487 * 'send_cups_browse()' - Send new browsing information using the CUPS
3488 * protocol.
3489 */
3490
3491 static void
3492 send_cups_browse(cupsd_printer_t *p) /* I - Printer to send */
3493 {
3494 int i; /* Looping var */
3495 cups_ptype_t type; /* Printer type */
3496 cupsd_dirsvc_addr_t *b; /* Browse address */
3497 int bytes; /* Length of packet */
3498 char packet[1453], /* Browse data packet */
3499 uri[1024], /* Printer URI */
3500 location[1024], /* printer-location */
3501 info[1024], /* printer-info */
3502 make_model[1024];
3503 /* printer-make-and-model */
3504 cupsd_netif_t *iface; /* Network interface */
3505
3506
3507 /*
3508 * Figure out the printer type value...
3509 */
3510
3511 type = p->type | CUPS_PRINTER_REMOTE;
3512
3513 if (!p->accepting)
3514 type |= CUPS_PRINTER_REJECTING;
3515
3516 if (p == DefaultPrinter)
3517 type |= CUPS_PRINTER_DEFAULT;
3518
3519 /*
3520 * Remove quotes from printer-info, printer-location, and
3521 * printer-make-and-model attributes...
3522 */
3523
3524 dequote(location, p->location, sizeof(location));
3525 dequote(info, p->info, sizeof(info));
3526
3527 if (p->make_model)
3528 dequote(make_model, p->make_model, sizeof(make_model));
3529 else if (p->type & CUPS_PRINTER_CLASS)
3530 {
3531 if (p->num_printers > 0 && p->printers[0]->make_model)
3532 strlcpy(make_model, p->printers[0]->make_model, sizeof(make_model));
3533 else
3534 strlcpy(make_model, "Local Printer Class", sizeof(make_model));
3535 }
3536 else if (p->raw)
3537 strlcpy(make_model, "Local Raw Printer", sizeof(make_model));
3538 else
3539 strlcpy(make_model, "Local System V Printer", sizeof(make_model));
3540
3541 /*
3542 * Send a packet to each browse address...
3543 */
3544
3545 for (i = NumBrowsers, b = Browsers; i > 0; i --, b ++)
3546 if (b->iface[0])
3547 {
3548 /*
3549 * Send the browse packet to one or more interfaces...
3550 */
3551
3552 if (!strcmp(b->iface, "*"))
3553 {
3554 /*
3555 * Send to all local interfaces...
3556 */
3557
3558 cupsdNetIFUpdate();
3559
3560 for (iface = (cupsd_netif_t *)cupsArrayFirst(NetIFList);
3561 iface;
3562 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList))
3563 {
3564 /*
3565 * Only send to local, IPv4 interfaces...
3566 */
3567
3568 if (!iface->is_local || !iface->port ||
3569 iface->address.addr.sa_family != AF_INET)
3570 continue;
3571
3572 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
3573 iface->hostname, iface->port,
3574 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
3575 "/printers/%s",
3576 p->name);
3577 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3578 type, p->state, uri, location, info, make_model,
3579 p->browse_attrs ? p->browse_attrs : "");
3580
3581 bytes = strlen(packet);
3582
3583 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3584 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
3585 iface->name, packet);
3586
3587 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
3588
3589 sendto(BrowseSocket, packet, bytes, 0,
3590 (struct sockaddr *)&(iface->broadcast),
3591 httpAddrLength(&(iface->broadcast)));
3592 }
3593 }
3594 else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
3595 {
3596 /*
3597 * Send to the named interface using the IPv4 address...
3598 */
3599
3600 while (iface)
3601 if (strcmp(b->iface, iface->name))
3602 {
3603 iface = NULL;
3604 break;
3605 }
3606 else if (iface->address.addr.sa_family == AF_INET && iface->port)
3607 break;
3608 else
3609 iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
3610
3611 if (iface)
3612 {
3613 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
3614 iface->hostname, iface->port,
3615 (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
3616 "/printers/%s",
3617 p->name);
3618 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3619 type, p->state, uri, location, info, make_model,
3620 p->browse_attrs ? p->browse_attrs : "");
3621
3622 bytes = strlen(packet);
3623
3624 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3625 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
3626 iface->name, packet);
3627
3628 iface->broadcast.ipv4.sin_port = htons(BrowsePort);
3629
3630 sendto(BrowseSocket, packet, bytes, 0,
3631 (struct sockaddr *)&(iface->broadcast),
3632 httpAddrLength(&(iface->broadcast)));
3633 }
3634 }
3635 }
3636 else
3637 {
3638 /*
3639 * Send the browse packet to the indicated address using
3640 * the default server name...
3641 */
3642
3643 snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3644 type, p->state, p->uri, location, info, make_model,
3645 p->browse_attrs ? p->browse_attrs : "");
3646
3647 bytes = strlen(packet);
3648 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3649 "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
3650
3651 if (sendto(BrowseSocket, packet, bytes, 0,
3652 (struct sockaddr *)&(b->to),
3653 httpAddrLength(&(b->to))) <= 0)
3654 {
3655 /*
3656 * Unable to send browse packet, so remove this address from the
3657 * list...
3658 */
3659
3660 cupsdLogMessage(CUPSD_LOG_ERROR,
3661 "cupsdSendBrowseList: sendto failed for browser "
3662 "%d - %s.",
3663 (int)(b - Browsers + 1), strerror(errno));
3664
3665 if (i > 1)
3666 memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
3667
3668 b --;
3669 NumBrowsers --;
3670 }
3671 }
3672 }
3673
3674
3675 #ifdef HAVE_LDAP
3676 /*
3677 * 'ldap_search_rec()' - LDAP Search with reconnect
3678 */
3679
3680 static int
3681 ldap_search_rec(LDAP *ld, /* I - LDAP handler */
3682 char *base, /* I - Base dn */
3683 int scope, /* I - LDAP search scope */
3684 char *filter, /* I - Filter string */
3685 char *attrs[], /* I - Requested attributes */
3686 int attrsonly, /* I - Return only attributes? */
3687 LDAPMessage **res) /* I - LDAP handler */
3688 {
3689 int rc; /* Return code */
3690
3691
3692 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3693 rc = ldap_search_ext_s(ld, base, scope, filter, attrs, attrsonly, NULL, NULL,
3694 NULL, LDAP_NO_LIMIT, res);
3695 # else
3696 rc = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res);
3697 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3698
3699 /*
3700 * If we have a connection problem try again...
3701 */
3702
3703 if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
3704 {
3705 cupsdLogMessage(CUPSD_LOG_ERROR,
3706 "LDAP search failed with status %d: %s",
3707 rc, ldap_err2string(rc));
3708 cupsdLogMessage(CUPSD_LOG_INFO,
3709 "We try the LDAP search once again after reconnecting to "
3710 "the server");
3711 ldap_freeres(*res);
3712 ldap_reconnect();
3713
3714 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3715 rc = ldap_search_ext_s(ld, base, scope, filter, attrs, attrsonly, NULL,
3716 NULL, NULL, LDAP_NO_LIMIT, res);
3717 # else
3718 rc = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res);
3719 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3720 }
3721
3722 if (rc == LDAP_NO_SUCH_OBJECT)
3723 cupsdLogMessage(CUPSD_LOG_DEBUG,
3724 "ldap_search_rec: LDAP entry/object not found");
3725 else if (rc != LDAP_SUCCESS)
3726 cupsdLogMessage(CUPSD_LOG_ERROR,
3727 "ldap_search_rec: LDAP search failed with status %d: %s",
3728 rc, ldap_err2string(rc));
3729
3730 if (rc != LDAP_SUCCESS)
3731 ldap_freeres(*res);
3732
3733 return (rc);
3734 }
3735
3736
3737 /*
3738 * 'ldap_freeres()' - Free LDAPMessage
3739 */
3740
3741 static void
3742 ldap_freeres(LDAPMessage *entry) /* I - LDAP handler */
3743 {
3744 int rc; /* Return value */
3745
3746
3747 rc = ldap_msgfree(entry);
3748 if (rc == -1)
3749 cupsdLogMessage(CUPSD_LOG_WARN,
3750 "Can't free LDAPMessage!");
3751 else if (rc == 0)
3752 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3753 "Freeing LDAPMessage was unnecessary");
3754 }
3755
3756
3757 /*
3758 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3759 */
3760
3761 static int
3762 ldap_getval_firststring(
3763 LDAP *ld, /* I - LDAP handler */
3764 LDAPMessage *entry, /* I - LDAP message or search result */
3765 char *attr, /* I - the wanted attribute */
3766 char *retval, /* O - String to return */
3767 unsigned long maxsize) /* I - Max string size */
3768 {
3769 char *dn; /* LDAP DN */
3770 int rc = 0; /* Return code */
3771 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3772 struct berval **bval; /* LDAP value array */
3773 unsigned long size; /* String size */
3774
3775
3776 /*
3777 * Get value from LDAPMessage...
3778 */
3779
3780 if ((bval = ldap_get_values_len(ld, entry, attr)) == NULL)
3781 {
3782 rc = -1;
3783 dn = ldap_get_dn(ld, entry);
3784 cupsdLogMessage(CUPSD_LOG_WARN,
3785 "Failed to get LDAP value %s for %s!",
3786 attr, dn);
3787 ldap_memfree(dn);
3788 }
3789 else
3790 {
3791
3792 /*
3793 * Check size and copy value into our string...
3794 */
3795
3796 size = maxsize;
3797 if (size < bval[0]->bv_len)
3798 {
3799 rc = -1;
3800 dn = ldap_get_dn(ld, entry);
3801 cupsdLogMessage(CUPSD_LOG_WARN,
3802 "Attribute %s is too big! (dn: %s)",
3803 attr, dn);
3804 ldap_memfree(dn);
3805 }
3806 else
3807 size = bval[0]->bv_len;
3808
3809 strlcpy(retval, bval[0]->bv_val, size);
3810 ldap_value_free_len(bval);
3811 }
3812 # else
3813 char **value; /* LDAP value */
3814
3815 /*
3816 * Get value from LDAPMessage...
3817 */
3818
3819 if ((value = (char **)ldap_get_values(ld, entry, attr)) == NULL)
3820 {
3821 rc = -1;
3822 dn = ldap_get_dn(ld, entry);
3823 cupsdLogMessage(CUPSD_LOG_WARN,
3824 "Failed to get LDAP value %s for %s!",
3825 attr, dn);
3826 ldap_memfree(dn);
3827 }
3828 else
3829 {
3830 strlcpy(retval, *value, maxsize);
3831 ldap_value_free(value);
3832 }
3833 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3834
3835 return (rc);
3836 }
3837
3838
3839 /*
3840 * 'send_ldap_ou()' - Send LDAP ou registrations.
3841 */
3842
3843 static void
3844 send_ldap_ou(char *ou, /* I - Servername/ou to register */
3845 char *basedn, /* I - Our base dn */
3846 char *descstring) /* I - Description for ou */
3847 {
3848 int i; /* Looping var... */
3849 LDAPMod mods[3]; /* The 3 attributes we will be adding */
3850 LDAPMod *pmods[4]; /* Pointers to the 3 attributes + NULL */
3851 LDAPMessage *res, /* Search result token */
3852 *e; /* Current entry from search */
3853 int rc; /* LDAP status */
3854 char dn[1024], /* DN of the organizational unit we are adding */
3855 *desc[2], /* Change records */
3856 *ou_value[2];
3857 char old_desc[1024]; /* Old description */
3858 static const char * const objectClass_values[] =
3859 { /* The 2 objectClass's we use in */
3860 "top", /* our LDAP entries */
3861 "organizationalUnit",
3862 NULL
3863 };
3864 static const char * const ou_attrs[] =/* CUPS LDAP attributes */
3865 {
3866 "description",
3867 NULL
3868 };
3869
3870
3871 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: %s", ou);
3872
3873 /*
3874 * Reconnect if LDAP Handle is invalid...
3875 */
3876
3877 if (! BrowseLDAPHandle)
3878 {
3879 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3880 "send_ldap_ou: LDAP Handle is invalid. Try "
3881 "reconnecting...");
3882 ldap_reconnect();
3883 return;
3884 }
3885
3886 /*
3887 * Prepare ldap search...
3888 */
3889
3890 snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn);
3891 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: dn=\"%s\"", dn);
3892
3893 ou_value[0] = ou;
3894 ou_value[1] = NULL;
3895 desc[0] = descstring;
3896 desc[1] = NULL;
3897
3898 mods[0].mod_type = "ou";
3899 mods[0].mod_values = ou_value;
3900 mods[1].mod_type = "description";
3901 mods[1].mod_values = desc;
3902 mods[2].mod_type = "objectClass";
3903 mods[2].mod_values = (char **)objectClass_values;
3904
3905 rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL,
3906 (char **)ou_attrs, 0, &res);
3907
3908 /*
3909 * If ldap search was not successfull then exit function...
3910 */
3911
3912 if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT)
3913 return;
3914
3915 /*
3916 * Check if we need to insert or update the LDAP entry...
3917 */
3918
3919 if (ldap_count_entries(BrowseLDAPHandle, res) > 0 &&
3920 rc != LDAP_NO_SUCH_OBJECT)
3921 {
3922 /*
3923 * Printserver has already been registered, check if
3924 * modification is required...
3925 */
3926
3927 e = ldap_first_entry(BrowseLDAPHandle, res);
3928
3929 /*
3930 * Get the required values from this entry...
3931 */
3932
3933 if (ldap_getval_firststring(BrowseLDAPHandle, e, "description", old_desc,
3934 sizeof(old_desc)) == -1)
3935 old_desc[0] = '\0';
3936
3937 /*
3938 * Check if modification is required...
3939 */
3940
3941 if ( strcmp(desc[0], old_desc) == 0 )
3942 {
3943 /*
3944 * LDAP entry for the printer exists.
3945 * Printer has already been registered,
3946 * no modifications required...
3947 */
3948 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3949 "send_ldap_ou: No updates required for %s", ou);
3950 }
3951 else
3952 {
3953
3954 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3955 "send_ldap_ou: Replace entry for %s", ou);
3956
3957 for (i = 0; i < 3; i ++)
3958 {
3959 pmods[i] = mods + i;
3960 pmods[i]->mod_op = LDAP_MOD_REPLACE;
3961 }
3962 pmods[i] = NULL;
3963
3964 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3965 if ((rc = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
3966 NULL)) != LDAP_SUCCESS)
3967 # else
3968 if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
3969 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3970 {
3971 cupsdLogMessage(CUPSD_LOG_ERROR,
3972 "LDAP modify for %s failed with status %d: %s",
3973 ou, rc, ldap_err2string(rc));
3974 if ( LDAP_SERVER_DOWN == rc )
3975 ldap_reconnect();
3976 }
3977 }
3978 }
3979 else
3980 {
3981 /*
3982 * Printserver has never been registered,
3983 * add registration...
3984 */
3985
3986 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3987 "send_ldap_ou: Add entry for %s", ou);
3988
3989 for (i = 0; i < 3; i ++)
3990 {
3991 pmods[i] = mods + i;
3992 pmods[i]->mod_op = LDAP_MOD_ADD;
3993 }
3994 pmods[i] = NULL;
3995
3996 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3997 if ((rc = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
3998 NULL)) != LDAP_SUCCESS)
3999 # else
4000 if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
4001 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4002 {
4003 cupsdLogMessage(CUPSD_LOG_ERROR,
4004 "LDAP add for %s failed with status %d: %s",
4005 ou, rc, ldap_err2string(rc));
4006 if ( LDAP_SERVER_DOWN == rc )
4007 ldap_reconnect();
4008 }
4009 }
4010
4011 ldap_freeres(res);
4012 }
4013
4014
4015 /*
4016 * 'send_ldap_browse()' - Send LDAP printer registrations.
4017 */
4018
4019 static void
4020 send_ldap_browse(cupsd_printer_t *p) /* I - Printer to register */
4021 {
4022 int i; /* Looping var... */
4023 LDAPMod mods[7]; /* The 7 attributes we will be adding */
4024 LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
4025 LDAPMessage *res, /* Search result token */
4026 *e; /* Current entry from search */
4027 char *cn_value[2], /* Change records */
4028 *uri[2],
4029 *info[2],
4030 *location[2],
4031 *make_model[2],
4032 *type[2],
4033 typestring[255], /* String to hold printer-type */
4034 dn[1024]; /* DN of the printer we are adding */
4035 int rc; /* LDAP status */
4036 char old_uri[HTTP_MAX_URI], /* Printer URI */
4037 old_location[1024], /* Printer location */
4038 old_info[1024], /* Printer information */
4039 old_make_model[1024], /* Printer make and model */
4040 old_type_string[30]; /* Temporary type number */
4041 int old_type; /* Printer type */
4042 static const char * const objectClass_values[] =
4043 { /* The 3 objectClass's we use in */
4044 "top", /* our LDAP entries */
4045 "device",
4046 "cupsPrinter",
4047 NULL
4048 };
4049
4050
4051 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s", p->name);
4052
4053 /*
4054 * Exit function if LDAP updates has been disabled...
4055 */
4056
4057 if (!BrowseLDAPUpdate)
4058 {
4059 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4060 "send_ldap_browse: Updates temporary disabled; "
4061 "skipping...");
4062 return;
4063 }
4064
4065 /*
4066 * Reconnect if LDAP Handle is invalid...
4067 */
4068
4069 if (!BrowseLDAPHandle)
4070 {
4071 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4072 "send_ldap_browse: LDAP Handle is invalid. Try "
4073 "reconnecting...");
4074 ldap_reconnect();
4075 return;
4076 }
4077
4078 /*
4079 * Everything in ldap is ** so we fudge around it...
4080 */
4081
4082 sprintf(typestring, "%u", p->type);
4083
4084 cn_value[0] = p->name;
4085 cn_value[1] = NULL;
4086 info[0] = p->info ? p->info : "Unknown";
4087 info[1] = NULL;
4088 location[0] = p->location ? p->location : "Unknown";
4089 location[1] = NULL;
4090 make_model[0] = p->make_model ? p->make_model : "Unknown";
4091 make_model[1] = NULL;
4092 type[0] = typestring;
4093 type[1] = NULL;
4094 uri[0] = p->uri;
4095 uri[1] = NULL;
4096
4097 /*
4098 * Get ldap entry for printer ...
4099 */
4100
4101 snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName,
4102 BrowseLDAPDN);
4103 cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
4104
4105 rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL,
4106 (char **)ldap_attrs, 0, &res);
4107
4108 /*
4109 * If ldap search was not successfull then exit function
4110 * and temporary disable LDAP updates...
4111 */
4112
4113 if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT)
4114 {
4115 if (BrowseLDAPUpdate &&
4116 (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR))
4117 {
4118 BrowseLDAPUpdate = FALSE;
4119 cupsdLogMessage(CUPSD_LOG_INFO,
4120 "LDAP update temporary disabled");
4121 }
4122
4123 return;
4124 }
4125
4126 /*
4127 * Fill modification array...
4128 */
4129
4130 mods[0].mod_type = "cn";
4131 mods[0].mod_values = cn_value;
4132 mods[1].mod_type = "printerDescription";
4133 mods[1].mod_values = info;
4134 mods[2].mod_type = "printerURI";
4135 mods[2].mod_values = uri;
4136 mods[3].mod_type = "printerLocation";
4137 mods[3].mod_values = location;
4138 mods[4].mod_type = "printerMakeAndModel";
4139 mods[4].mod_values = make_model;
4140 mods[5].mod_type = "printerType";
4141 mods[5].mod_values = type;
4142 mods[6].mod_type = "objectClass";
4143 mods[6].mod_values = (char **)objectClass_values;
4144
4145 /*
4146 * Check if we need to insert or update the LDAP entry...
4147 */
4148
4149 if (ldap_count_entries(BrowseLDAPHandle, res) > 0 &&
4150 rc != LDAP_NO_SUCH_OBJECT)
4151 {
4152 /*
4153 * Printer has already been registered, check if
4154 * modification is required...
4155 */
4156
4157 e = ldap_first_entry(BrowseLDAPHandle, res);
4158
4159 /*
4160 * Get the required values from this entry...
4161 */
4162
4163 if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerDescription",
4164 old_info, sizeof(old_info)) == -1)
4165 old_info[0] = '\0';
4166
4167 if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerLocation",
4168 old_location, sizeof(old_location)) == -1)
4169 old_info[0] = '\0';
4170
4171 if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerMakeAndModel",
4172 old_make_model, sizeof(old_make_model)) == -1)
4173 old_info[0] = '\0';
4174
4175 if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerType",
4176 old_type_string, sizeof(old_type_string)) == -1)
4177 old_info[0] = '\0';
4178
4179 old_type = atoi(old_type_string);
4180
4181 if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerURI", old_uri,
4182 sizeof(old_uri)) == -1)
4183 old_info[0] = '\0';
4184
4185 /*
4186 * Check if modification is required...
4187 */
4188
4189 if (!strcmp(info[0], old_info) && !strcmp(uri[0], old_uri) &&
4190 !strcmp(location[0], old_location) &&
4191 !strcmp(make_model[0], old_make_model) && p->type == old_type)
4192 {
4193 /*
4194 * LDAP entry for the printer exists. Printer has already been registered,
4195 * no modifications required...
4196 */
4197
4198 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4199 "send_ldap_browse: No updates required for %s", p->name);
4200 }
4201 else
4202 {
4203 /*
4204 * LDAP entry for the printer exists. Printer has already been registered,
4205 * modify the current registration...
4206 */
4207
4208 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4209 "send_ldap_browse: Replace entry for %s", p->name);
4210
4211 for (i = 0; i < 7; i ++)
4212 {
4213 pmods[i] = mods + i;
4214 pmods[i]->mod_op = LDAP_MOD_REPLACE;
4215 }
4216 pmods[i] = NULL;
4217
4218 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4219 if ((rc = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
4220 NULL)) != LDAP_SUCCESS)
4221 # else
4222 if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
4223 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4224 {
4225 cupsdLogMessage(CUPSD_LOG_ERROR,
4226 "LDAP modify for %s failed with status %d: %s",
4227 p->name, rc, ldap_err2string(rc));
4228 if (rc == LDAP_SERVER_DOWN)
4229 ldap_reconnect();
4230 }
4231 }
4232 }
4233 else
4234 {
4235 /*
4236 * No LDAP entry exists for the printer. Printer has never been registered,
4237 * add the current registration...
4238 */
4239
4240 send_ldap_ou(ServerName, BrowseLDAPDN, "CUPS Server");
4241
4242 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4243 "send_ldap_browse: Add entry for %s", p->name);
4244
4245 for (i = 0; i < 7; i ++)
4246 {
4247 pmods[i] = mods + i;
4248 pmods[i]->mod_op = LDAP_MOD_ADD;
4249 }
4250 pmods[i] = NULL;
4251
4252 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4253 if ((rc = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
4254 NULL)) != LDAP_SUCCESS)
4255 # else
4256 if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
4257 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4258 {
4259 cupsdLogMessage(CUPSD_LOG_ERROR,
4260 "LDAP add for %s failed with status %d: %s",
4261 p->name, rc, ldap_err2string(rc));
4262 if (rc == LDAP_SERVER_DOWN)
4263 ldap_reconnect();
4264 }
4265 }
4266
4267 ldap_freeres(res);
4268 }
4269
4270
4271 /*
4272 * 'ldap_dereg_printer()' - Delete printer from directory
4273 */
4274
4275 static void
4276 ldap_dereg_printer(cupsd_printer_t *p) /* I - Printer to deregister */
4277 {
4278 char dn[1024]; /* DN of the printer */
4279 int rc; /* LDAP status */
4280
4281
4282 cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: Remove entry for %s",
4283 p->name);
4284
4285 /*
4286 * Reconnect if LDAP Handle is invalid...
4287 */
4288
4289 if (!BrowseLDAPHandle)
4290 {
4291 ldap_reconnect();
4292 return;
4293 }
4294
4295 /*
4296 * Get dn for printer and delete LDAP entry...
4297 */
4298
4299 snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName,
4300 BrowseLDAPDN);
4301 cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: dn=\"%s\"", dn);
4302
4303 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4304 if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
4305 NULL)) != LDAP_SUCCESS)
4306 # else
4307 if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
4308 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4309 {
4310 cupsdLogMessage(CUPSD_LOG_WARN,
4311 "LDAP delete for %s failed with status %d: %s",
4312 p->name, rc, ldap_err2string(rc));
4313
4314 /*
4315 * If we had a connection problem (connection timed out, etc.)
4316 * we should reconnect and try again to delete the entry...
4317 */
4318
4319 if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
4320 {
4321 cupsdLogMessage(CUPSD_LOG_INFO,
4322 "Retry deleting LDAP entry for %s after a reconnect...", p->name);
4323 ldap_reconnect();
4324
4325 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4326 if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
4327 NULL)) != LDAP_SUCCESS)
4328 # else
4329 if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
4330 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4331 cupsdLogMessage(CUPSD_LOG_WARN,
4332 "LDAP delete for %s failed with status %d: %s",
4333 p->name, rc, ldap_err2string(rc));
4334 }
4335 }
4336 }
4337
4338
4339 static void
4340 ldap_dereg_ou(char *ou, /* I - Organizational unit (servername) */
4341 char *basedn) /* I - Dase dn */
4342 {
4343 char dn[1024]; /* DN of the printer */
4344 int rc; /* LDAP status */
4345
4346
4347 cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: Remove entry for %s", ou);
4348
4349 /*
4350 * Reconnect if LDAP Handle is invalid...
4351 */
4352
4353 if (!BrowseLDAPHandle)
4354 {
4355 ldap_reconnect();
4356 return;
4357 }
4358
4359 /*
4360 * Get dn for printer and delete LDAP entry...
4361 */
4362
4363 snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn);
4364 cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: dn=\"%s\"", dn);
4365
4366 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4367 if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
4368 NULL)) != LDAP_SUCCESS)
4369 # else
4370 if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
4371 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4372 {
4373 cupsdLogMessage(CUPSD_LOG_WARN,
4374 "LDAP delete for %s failed with status %d: %s",
4375 ou, rc, ldap_err2string(rc));
4376
4377 /*
4378 * If we had a connection problem (connection timed out, etc.)
4379 * we should reconnect and try again to delete the entry...
4380 */
4381
4382 if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
4383 {
4384 cupsdLogMessage(CUPSD_LOG_INFO,
4385 "Retry deleting LDAP entry for %s after a reconnect...", ou);
4386 ldap_reconnect();
4387 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4388 if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
4389 NULL)) != LDAP_SUCCESS)
4390 # else
4391 if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
4392 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4393 cupsdLogMessage(CUPSD_LOG_WARN,
4394 "LDAP delete for %s failed with status %d: %s",
4395 ou, rc, ldap_err2string(rc));
4396 }
4397
4398 }
4399 }
4400 #endif /* HAVE_LDAP */
4401
4402
4403 #ifdef HAVE_LIBSLP
4404 /*
4405 * 'send_slp_browse()' - Register the specified printer with SLP.
4406 */
4407
4408 static void
4409 send_slp_browse(cupsd_printer_t *p) /* I - Printer to register */
4410 {
4411 char srvurl[HTTP_MAX_URI], /* Printer service URI */
4412 attrs[8192], /* Printer attributes */
4413 finishings[1024], /* Finishings to support */
4414 make_model[IPP_MAX_NAME * 2],
4415 /* Make and model, quoted */
4416 location[IPP_MAX_NAME * 2],
4417 /* Location, quoted */
4418 info[IPP_MAX_NAME * 2], /* Info, quoted */
4419 *src, /* Pointer to original string */
4420 *dst; /* Pointer to destination string */
4421 ipp_attribute_t *authentication; /* uri-authentication-supported value */
4422 SLPError error; /* SLP error, if any */
4423
4424
4425 cupsdLogMessage(CUPSD_LOG_DEBUG, "send_slp_browse(%p = \"%s\")", p,
4426 p->name);
4427
4428 /*
4429 * Make the SLP service URL that conforms to the IANA
4430 * 'printer:' template.
4431 */
4432
4433 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
4434
4435 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Service URL = \"%s\"", srvurl);
4436
4437 /*
4438 * Figure out the finishings string...
4439 */
4440
4441 if (p->type & CUPS_PRINTER_STAPLE)
4442 strcpy(finishings, "staple");
4443 else
4444 finishings[0] = '\0';
4445
4446 if (p->type & CUPS_PRINTER_BIND)
4447 {
4448 if (finishings[0])
4449 strlcat(finishings, ",bind", sizeof(finishings));
4450 else
4451 strcpy(finishings, "bind");
4452 }
4453
4454 if (p->type & CUPS_PRINTER_PUNCH)
4455 {
4456 if (finishings[0])
4457 strlcat(finishings, ",punch", sizeof(finishings));
4458 else
4459 strcpy(finishings, "punch");
4460 }
4461
4462 if (p->type & CUPS_PRINTER_COVER)
4463 {
4464 if (finishings[0])
4465 strlcat(finishings, ",cover", sizeof(finishings));
4466 else
4467 strcpy(finishings, "cover");
4468 }
4469
4470 if (p->type & CUPS_PRINTER_SORT)
4471 {
4472 if (finishings[0])
4473 strlcat(finishings, ",sort", sizeof(finishings));
4474 else
4475 strcpy(finishings, "sort");
4476 }
4477
4478 if (!finishings[0])
4479 strcpy(finishings, "none");
4480
4481 /*
4482 * Quote any commas in the make and model, location, and info strings...
4483 */
4484
4485 for (src = p->make_model, dst = make_model;
4486 src && *src && dst < (make_model + sizeof(make_model) - 2);)
4487 {
4488 if (*src == ',' || *src == '\\' || *src == ')')
4489 *dst++ = '\\';
4490
4491 *dst++ = *src++;
4492 }
4493
4494 *dst = '\0';
4495
4496 if (!make_model[0])
4497 strcpy(make_model, "Unknown");
4498
4499 for (src = p->location, dst = location;
4500 src && *src && dst < (location + sizeof(location) - 2);)
4501 {
4502 if (*src == ',' || *src == '\\' || *src == ')')
4503 *dst++ = '\\';
4504
4505 *dst++ = *src++;
4506 }
4507
4508 *dst = '\0';
4509
4510 if (!location[0])
4511 strcpy(location, "Unknown");
4512
4513 for (src = p->info, dst = info;
4514 src && *src && dst < (info + sizeof(info) - 2);)
4515 {
4516 if (*src == ',' || *src == '\\' || *src == ')')
4517 *dst++ = '\\';
4518
4519 *dst++ = *src++;
4520 }
4521
4522 *dst = '\0';
4523
4524 if (!info[0])
4525 strcpy(info, "Unknown");
4526
4527 /*
4528 * Get the authentication value...
4529 */
4530
4531 authentication = ippFindAttribute(p->attrs, "uri-authentication-supported",
4532 IPP_TAG_KEYWORD);
4533
4534 /*
4535 * Make the SLP attribute string list that conforms to
4536 * the IANA 'printer:' template.
4537 */
4538
4539 snprintf(attrs, sizeof(attrs),
4540 "(printer-uri-supported=%s),"
4541 "(uri-authentication-supported=%s>),"
4542 #ifdef HAVE_SSL
4543 "(uri-security-supported=tls>),"
4544 #else
4545 "(uri-security-supported=none>),"
4546 #endif /* HAVE_SSL */
4547 "(printer-name=%s),"
4548 "(printer-location=%s),"
4549 "(printer-info=%s),"
4550 "(printer-more-info=%s),"
4551 "(printer-make-and-model=%s),"
4552 "(printer-type=%d),"
4553 "(charset-supported=utf-8),"
4554 "(natural-language-configured=%s),"
4555 "(natural-language-supported=de,en,es,fr,it),"
4556 "(color-supported=%s),"
4557 "(finishings-supported=%s),"
4558 "(sides-supported=one-sided%s),"
4559 "(multiple-document-jobs-supported=true)"
4560 "(ipp-versions-supported=1.0,1.1)",
4561 p->uri, authentication->values[0].string.text, p->name, location,
4562 info, p->uri, make_model, p->type, DefaultLanguage,
4563 p->type & CUPS_PRINTER_COLOR ? "true" : "false",
4564 finishings,
4565 p->type & CUPS_PRINTER_DUPLEX ?
4566 ",two-sided-long-edge,two-sided-short-edge" : "");
4567
4568 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Attributes = \"%s\"", attrs);
4569
4570 /*
4571 * Register the printer with the SLP server...
4572 */
4573
4574 error = SLPReg(BrowseSLPHandle, srvurl, BrowseTimeout,
4575 SLP_CUPS_SRVTYPE, attrs, SLP_TRUE, slp_reg_callback, 0);
4576
4577 if (error != SLP_OK)
4578 cupsdLogMessage(CUPSD_LOG_ERROR, "SLPReg of \"%s\" failed with status %d!", p->name,
4579 error);
4580 }
4581
4582
4583 /*
4584 * 'slp_attr_callback()' - SLP attribute callback
4585 */
4586
4587 static SLPBoolean /* O - SLP_TRUE for success */
4588 slp_attr_callback(
4589 SLPHandle hslp, /* I - SLP handle */
4590 const char *attrlist, /* I - Attribute list */
4591 SLPError errcode, /* I - Parsing status for this attr */
4592 void *cookie) /* I - Current printer */
4593 {
4594 char *tmp = 0; /* Temporary string */
4595 cupsd_printer_t *p = (cupsd_printer_t*)cookie;
4596 /* Current printer */
4597
4598
4599 (void)hslp; /* anti-compiler-warning-code */
4600
4601 /*
4602 * Bail if there was an error
4603 */
4604
4605 if (errcode != SLP_OK)
4606 return (SLP_TRUE);
4607
4608 /*
4609 * Parse the attrlist to obtain things needed to build CUPS browse packet
4610 */
4611
4612 memset(p, 0, sizeof(cupsd_printer_t));
4613
4614 if (slp_get_attr(attrlist, "(printer-location=", &(p->location)))
4615 return (SLP_FALSE);
4616 if (slp_get_attr(attrlist, "(printer-info=", &(p->info)))
4617 return (SLP_FALSE);
4618 if (slp_get_attr(attrlist, "(printer-make-and-model=", &(p->make_model)))
4619 return (SLP_FALSE);
4620 if (!slp_get_attr(attrlist, "(printer-type=", &tmp))
4621 p->type = atoi(tmp);
4622 else
4623 p->type = CUPS_PRINTER_REMOTE;
4624
4625 cupsdClearString(&tmp);
4626
4627 return (SLP_TRUE);
4628 }
4629
4630
4631 /*
4632 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4633 */
4634
4635 static void
4636 slp_dereg_printer(cupsd_printer_t *p) /* I - Printer */
4637 {
4638 char srvurl[HTTP_MAX_URI]; /* Printer service URI */
4639
4640
4641 cupsdLogMessage(CUPSD_LOG_DEBUG, "slp_dereg_printer: printer=\"%s\"", p->name);
4642
4643 if (!(p->type & CUPS_PRINTER_REMOTE))
4644 {
4645 /*
4646 * Make the SLP service URL that conforms to the IANA
4647 * 'printer:' template.
4648 */
4649
4650 snprintf(srvurl, sizeof(srvurl), SLP_CUPS_SRVTYPE ":%s", p->uri);
4651
4652 /*
4653 * Deregister the printer...
4654 */
4655
4656 SLPDereg(BrowseSLPHandle, srvurl, slp_reg_callback, 0);
4657 }
4658 }
4659
4660
4661 /*
4662 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4663 */
4664
4665 static int /* O - 0 on success */
4666 slp_get_attr(const char *attrlist, /* I - Attribute list string */
4667 const char *tag, /* I - Name of attribute */
4668 char **valbuf) /* O - Value */
4669 {
4670 char *ptr1, /* Pointer into string */
4671 *ptr2; /* ... */
4672
4673
4674 cupsdClearString(valbuf);
4675
4676 if ((ptr1 = strstr(attrlist, tag)) != NULL)
4677 {
4678 ptr1 += strlen(tag);
4679
4680 if ((ptr2 = strchr(ptr1,')')) != NULL)
4681 {
4682 /*
4683 * Copy the value...
4684 */
4685
4686 *valbuf = calloc(ptr2 - ptr1 + 1, 1);
4687 strncpy(*valbuf, ptr1, ptr2 - ptr1);
4688
4689 /*
4690 * Dequote the value...
4691 */
4692
4693 for (ptr1 = *valbuf; *ptr1; ptr1 ++)
4694 if (*ptr1 == '\\' && ptr1[1])
4695 _cups_strcpy(ptr1, ptr1 + 1);
4696
4697 return (0);
4698 }
4699 }
4700
4701 return (-1);
4702 }
4703
4704
4705 /*
4706 * 'slp_reg_callback()' - Empty SLPRegReport.
4707 */
4708
4709 static void
4710 slp_reg_callback(SLPHandle hslp, /* I - SLP handle */
4711 SLPError errcode, /* I - Error code, if any */
4712 void *cookie) /* I - App data */
4713 {
4714 (void)hslp;
4715 (void)errcode;
4716 (void)cookie;
4717
4718 return;
4719 }
4720
4721
4722 /*
4723 * 'slp_url_callback()' - SLP service url callback
4724 */
4725
4726 static SLPBoolean /* O - TRUE = OK, FALSE = error */
4727 slp_url_callback(
4728 SLPHandle hslp, /* I - SLP handle */
4729 const char *srvurl, /* I - URL of service */
4730 unsigned short lifetime, /* I - Life of service */
4731 SLPError errcode, /* I - Existing error code */
4732 void *cookie) /* I - Pointer to service list */
4733 {
4734 slpsrvurl_t *s, /* New service entry */
4735 **head; /* Pointer to head of entry */
4736
4737
4738 /*
4739 * Let the compiler know we won't be using these vars...
4740 */
4741
4742 (void)hslp;
4743 (void)lifetime;
4744
4745 /*
4746 * Bail if there was an error
4747 */
4748
4749 if (errcode != SLP_OK)
4750 return (SLP_TRUE);
4751
4752 /*
4753 * Grab the head of the list...
4754 */
4755
4756 head = (slpsrvurl_t**)cookie;
4757
4758 /*
4759 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4760 */
4761
4762 if ((s = (slpsrvurl_t *)calloc(1, sizeof(slpsrvurl_t))) == NULL)
4763 return (SLP_FALSE);
4764
4765 /*
4766 * Copy the SLP service URL...
4767 */
4768
4769 strlcpy(s->url, srvurl, sizeof(s->url));
4770
4771 /*
4772 * Link the SLP service URL into the head of the list
4773 */
4774
4775 if (*head)
4776 s->next = *head;
4777
4778 *head = s;
4779
4780 return (SLP_TRUE);
4781 }
4782 #endif /* HAVE_LIBSLP */
4783
4784
4785 /*
4786 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4787 */
4788
4789 static void
4790 update_cups_browse(void)
4791 {
4792 int i; /* Looping var */
4793 int auth; /* Authorization status */
4794 int len; /* Length of name string */
4795 int bytes; /* Number of bytes left */
4796 char packet[1541], /* Broadcast packet */
4797 *pptr; /* Pointer into packet */
4798 socklen_t srclen; /* Length of source address */
4799 http_addr_t srcaddr; /* Source address */
4800 char srcname[1024]; /* Source hostname */
4801 unsigned address[4]; /* Source address */
4802 unsigned type; /* Printer type */
4803 unsigned state; /* Printer state */
4804 char uri[HTTP_MAX_URI], /* Printer URI */
4805 host[HTTP_MAX_URI], /* Host portion of URI */
4806 resource[HTTP_MAX_URI], /* Resource portion of URI */
4807 info[IPP_MAX_NAME], /* Information string */
4808 location[IPP_MAX_NAME], /* Location string */
4809 make_model[IPP_MAX_NAME];/* Make and model string */
4810 int num_attrs; /* Number of attributes */
4811 cups_option_t *attrs; /* Attributes */
4812
4813
4814 /*
4815 * Read a packet from the browse socket...
4816 */
4817
4818 srclen = sizeof(srcaddr);
4819 if ((bytes = recvfrom(BrowseSocket, packet, sizeof(packet) - 1, 0,
4820 (struct sockaddr *)&srcaddr, &srclen)) < 0)
4821 {
4822 /*
4823 * "Connection refused" is returned under Linux if the destination port
4824 * or address is unreachable from a previous sendto(); check for the
4825 * error here and ignore it for now...
4826 */
4827
4828 if (errno != ECONNREFUSED && errno != EAGAIN)
4829 {
4830 cupsdLogMessage(CUPSD_LOG_ERROR, "Browse recv failed - %s.",
4831 strerror(errno));
4832 cupsdLogMessage(CUPSD_LOG_ERROR, "CUPS browsing turned off.");
4833
4834 #ifdef WIN32
4835 closesocket(BrowseSocket);
4836 #else
4837 close(BrowseSocket);
4838 #endif /* WIN32 */
4839
4840 cupsdRemoveSelect(BrowseSocket);
4841 BrowseSocket = -1;
4842
4843 BrowseLocalProtocols &= ~BROWSE_CUPS;
4844 BrowseRemoteProtocols &= ~BROWSE_CUPS;
4845 }
4846
4847 return;
4848 }
4849
4850 packet[bytes] = '\0';
4851
4852 /*
4853 * If we're about to sleep, ignore incoming browse packets.
4854 */
4855
4856 if (Sleeping)
4857 return;
4858
4859 /*
4860 * Figure out where it came from...
4861 */
4862
4863 #ifdef AF_INET6
4864 if (srcaddr.addr.sa_family == AF_INET6)
4865 {
4866 address[0] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[0]);
4867 address[1] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[1]);
4868 address[2] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[2]);
4869 address[3] = ntohl(srcaddr.ipv6.sin6_addr.s6_addr32[3]);
4870 }
4871 else
4872 #endif /* AF_INET6 */
4873 {
4874 address[0] = 0;
4875 address[1] = 0;
4876 address[2] = 0;
4877 address[3] = ntohl(srcaddr.ipv4.sin_addr.s_addr);
4878 }
4879
4880 if (HostNameLookups)
4881 httpAddrLookup(&srcaddr, srcname, sizeof(srcname));
4882 else
4883 httpAddrString(&srcaddr, srcname, sizeof(srcname));
4884
4885 len = strlen(srcname);
4886
4887 /*
4888 * Do ACL stuff...
4889 */
4890
4891 if (BrowseACL)
4892 {
4893 if (httpAddrLocalhost(&srcaddr) || !strcasecmp(srcname, "localhost"))
4894 {
4895 /*
4896 * Access from localhost (127.0.0.1) is always allowed...
4897 */
4898
4899 auth = CUPSD_AUTH_ALLOW;
4900 }
4901 else
4902 {
4903 /*
4904 * Do authorization checks on the domain/address...
4905 */
4906
4907 switch (BrowseACL->order_type)
4908 {
4909 default :
4910 auth = CUPSD_AUTH_DENY; /* anti-compiler-warning-code */
4911 break;
4912
4913 case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */
4914 auth = CUPSD_AUTH_ALLOW;
4915
4916 if (cupsdCheckAuth(address, srcname, len,
4917 BrowseACL->num_deny, BrowseACL->deny))
4918 auth = CUPSD_AUTH_DENY;
4919
4920 if (cupsdCheckAuth(address, srcname, len,
4921 BrowseACL->num_allow, BrowseACL->allow))
4922 auth = CUPSD_AUTH_ALLOW;
4923 break;
4924
4925 case CUPSD_AUTH_DENY : /* Order Allow,Deny */
4926 auth = CUPSD_AUTH_DENY;
4927
4928 if (cupsdCheckAuth(address, srcname, len,
4929 BrowseACL->num_allow, BrowseACL->allow))
4930 auth = CUPSD_AUTH_ALLOW;
4931
4932 if (cupsdCheckAuth(address, srcname, len,
4933 BrowseACL->num_deny, BrowseACL->deny))
4934 auth = CUPSD_AUTH_DENY;
4935 break;
4936 }
4937 }
4938 }
4939 else
4940 auth = CUPSD_AUTH_ALLOW;
4941
4942 if (auth == CUPSD_AUTH_DENY)
4943 {
4944 cupsdLogMessage(CUPSD_LOG_DEBUG,
4945 "update_cups_browse: Refused %d bytes from %s", bytes,
4946 srcname);
4947 return;
4948 }
4949
4950 cupsdLogMessage(CUPSD_LOG_DEBUG2,
4951 "update_cups_browse: (%d bytes from %s) %s", bytes,
4952 srcname, packet);
4953
4954 /*
4955 * Parse packet...
4956 */
4957
4958 if (sscanf(packet, "%x%x%1023s", &type, &state, uri) < 3)
4959 {
4960 cupsdLogMessage(CUPSD_LOG_WARN,
4961 "update_cups_browse: Garbled browse packet - %s", packet);
4962 return;
4963 }
4964
4965 strcpy(location, "Location Unknown");
4966 strcpy(info, "No Information Available");
4967 make_model[0] = '\0';
4968 num_attrs = 0;
4969 attrs = NULL;
4970
4971 if ((pptr = strchr(packet, '\"')) != NULL)
4972 {
4973 /*
4974 * Have extended information; can't use sscanf for it because not all
4975 * sscanf's allow empty strings with %[^\"]...
4976 */
4977
4978 for (i = 0, pptr ++;
4979 i < (sizeof(location) - 1) && *pptr && *pptr != '\"';
4980 i ++, pptr ++)
4981 location[i] = *pptr;
4982
4983 if (i)
4984 location[i] = '\0';
4985
4986 if (*pptr == '\"')
4987 pptr ++;
4988
4989 while (*pptr && isspace(*pptr & 255))
4990 pptr ++;
4991
4992 if (*pptr == '\"')
4993 {
4994 for (i = 0, pptr ++;
4995 i < (sizeof(info) - 1) && *pptr && *pptr != '\"';
4996 i ++, pptr ++)
4997 info[i] = *pptr;
4998
4999 info[i] = '\0';
5000
5001 if (*pptr == '\"')
5002 pptr ++;
5003
5004 while (*pptr && isspace(*pptr & 255))
5005 pptr ++;
5006
5007 if (*pptr == '\"')
5008 {
5009 for (i = 0, pptr ++;
5010 i < (sizeof(make_model) - 1) && *pptr && *pptr != '\"';
5011 i ++, pptr ++)
5012 make_model[i] = *pptr;
5013
5014 if (*pptr == '\"')
5015 pptr ++;
5016
5017 make_model[i] = '\0';
5018
5019 if (*pptr)
5020 num_attrs = cupsParseOptions(pptr, num_attrs, &attrs);
5021 }
5022 }
5023 }
5024
5025 DEBUG_puts(packet);
5026 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
5027 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
5028 type, state, uri, location, info, make_model));
5029
5030 /*
5031 * Pull the URI apart to see if this is a local or remote printer...
5032 */
5033
5034 if (is_local_queue(uri, host, sizeof(host), resource, sizeof(resource)))
5035 {
5036 cupsFreeOptions(num_attrs, attrs);
5037 return;
5038 }
5039
5040 /*
5041 * Do relaying...
5042 */
5043
5044 for (i = 0; i < NumRelays; i ++)
5045 if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
5046 if (sendto(BrowseSocket, packet, bytes, 0,
5047 (struct sockaddr *)&(Relays[i].to),
5048 httpAddrLength(&(Relays[i].to))) <= 0)
5049 {
5050 cupsdLogMessage(CUPSD_LOG_ERROR,
5051 "update_cups_browse: sendto failed for relay %d - %s.",
5052 i + 1, strerror(errno));
5053 cupsFreeOptions(num_attrs, attrs);
5054 return;
5055 }
5056
5057 /*
5058 * Process the browse data...
5059 */
5060
5061 process_browse_data(uri, host, resource, (cups_ptype_t)type,
5062 (ipp_pstate_t)state, location, info, make_model,
5063 num_attrs, attrs);
5064 }
5065
5066
5067 /*
5068 * 'update_lpd()' - Update the LPD configuration as needed.
5069 */
5070
5071 static void
5072 update_lpd(int onoff) /* - 1 = turn on, 0 = turn off */
5073 {
5074 if (!LPDConfigFile)
5075 return;
5076
5077 #ifdef __APPLE__
5078 /*
5079 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
5080 * setting for backwards-compatibility.
5081 */
5082
5083 if (onoff && !get_hostconfig("CUPS_LPD"))
5084 onoff = 0;
5085 #endif /* __APPLE__ */
5086
5087 if (!strncmp(LPDConfigFile, "xinetd:///", 10))
5088 {
5089 /*
5090 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5091 */
5092
5093 char newfile[1024]; /* New cups-lpd.N file */
5094 cups_file_t *ofp, /* Original file pointer */
5095 *nfp; /* New file pointer */
5096 char line[1024]; /* Line from file */
5097
5098
5099 snprintf(newfile, sizeof(newfile), "%s.N", LPDConfigFile + 9);
5100
5101 if ((ofp = cupsFileOpen(LPDConfigFile + 9, "r")) == NULL)
5102 {
5103 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
5104 LPDConfigFile + 9, strerror(errno));
5105 return;
5106 }
5107
5108 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
5109 {
5110 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
5111 newfile, strerror(errno));
5112 cupsFileClose(ofp);
5113 return;
5114 }
5115
5116 /*
5117 * Copy all of the lines from the cups-lpd file...
5118 */
5119
5120 while (cupsFileGets(ofp, line, sizeof(line)))
5121 {
5122 if (line[0] == '{')
5123 {
5124 cupsFilePrintf(nfp, "%s\n", line);
5125 snprintf(line, sizeof(line), "\tdisable = %s",
5126 onoff ? "no" : "yes");
5127 }
5128 else if (!strstr(line, "disable ="))
5129 cupsFilePrintf(nfp, "%s\n", line);
5130 }
5131
5132 cupsFileClose(nfp);
5133 cupsFileClose(ofp);
5134 rename(newfile, LPDConfigFile + 9);
5135 }
5136 #ifdef __APPLE__
5137 else if (!strncmp(LPDConfigFile, "launchd:///", 11))
5138 {
5139 /*
5140 * Enable/disable LPD via the launchctl command...
5141 */
5142
5143 char *argv[5], /* Arguments for command */
5144 *envp[MAX_ENV]; /* Environment for command */
5145 int pid; /* Process ID */
5146
5147
5148 cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
5149 argv[0] = (char *)"launchctl";
5150 argv[1] = (char *)(onoff ? "load" : "unload");
5151 argv[2] = (char *)"-w";
5152 argv[3] = LPDConfigFile + 10;
5153 argv[4] = NULL;
5154
5155 cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1,
5156 NULL, &pid);
5157 }
5158 #endif /* __APPLE__ */
5159 else
5160 cupsdLogMessage(CUPSD_LOG_INFO, "Unknown LPDConfigFile scheme!");
5161 }
5162
5163
5164 /*
5165 * 'update_polling()' - Read status messages from the poll daemons.
5166 */
5167
5168 static void
5169 update_polling(void)
5170 {
5171 char *ptr, /* Pointer to end of line in buffer */
5172 message[1024]; /* Pointer to message text */
5173 int loglevel; /* Log level for message */
5174
5175
5176 while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
5177 message, sizeof(message))) != NULL)
5178 if (!strchr(PollStatusBuffer->buffer, '\n'))
5179 break;
5180
5181 if (ptr == NULL && !PollStatusBuffer->bufused)
5182 {
5183 /*
5184 * All polling processes have died; stop polling...
5185 */
5186
5187 cupsdLogMessage(CUPSD_LOG_ERROR,
5188 "update_polling: all polling processes have exited!");
5189 cupsdStopPolling();
5190 }
5191 }
5192
5193
5194 /*
5195 * 'update_smb()' - Update the SMB configuration as needed.
5196 */
5197
5198 static void
5199 update_smb(int onoff) /* I - 1 = turn on, 0 = turn off */
5200 {
5201 if (!SMBConfigFile)
5202 return;
5203
5204 if (!strncmp(SMBConfigFile, "samba:///", 9))
5205 {
5206 /*
5207 * Enable/disable SMB via the specified smb.conf config file...
5208 */
5209
5210 char newfile[1024]; /* New smb.conf.N file */
5211 cups_file_t *ofp, /* Original file pointer */
5212 *nfp; /* New file pointer */
5213 char line[1024]; /* Line from file */
5214 int in_printers; /* In [printers] section? */
5215
5216
5217 snprintf(newfile, sizeof(newfile), "%s.N", SMBConfigFile + 8);
5218
5219 if ((ofp = cupsFileOpen(SMBConfigFile + 8, "r")) == NULL)
5220 {
5221 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open \"%s\" - %s",
5222 SMBConfigFile + 8, strerror(errno));
5223 return;
5224 }
5225
5226 if ((nfp = cupsFileOpen(newfile, "w")) == NULL)
5227 {
5228 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create \"%s\" - %s",
5229 newfile, strerror(errno));
5230 cupsFileClose(ofp);
5231 return;
5232 }
5233
5234 /*
5235 * Copy all of the lines from the smb.conf file...
5236 */
5237
5238 in_printers = 0;
5239
5240 while (cupsFileGets(ofp, line, sizeof(line)))
5241 {
5242 if (in_printers && strstr(line, "printable ="))
5243 snprintf(line, sizeof(line), " printable = %s",
5244 onoff ? "yes" : "no");
5245
5246 cupsFilePrintf(nfp, "%s\n", line);
5247
5248 if (line[0] == '[')
5249 in_printers = !strcmp(line, "[printers]");
5250 }
5251
5252 cupsFileClose(nfp);
5253 cupsFileClose(ofp);
5254 rename(newfile, SMBConfigFile + 8);
5255 }
5256 else
5257 cupsdLogMessage(CUPSD_LOG_INFO, "Unknown SMBConfigFile scheme!");
5258 }
5259
5260
5261 /*
5262 * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".
5263 */