2 * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
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/".
17 * cupsdDeregisterPrinter() - Stop sending broadcast information for a
18 * local printer and remove any pending
19 * references to remote printers.
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer update the broadcast contents.
23 * cupsdSaveRemoteCache() - Save the remote printer cache.
24 * cupsdSendBrowseList() - Send new browsing information as necessary.
25 * cupsdStartBrowsing() - Start sending and receiving broadcast
27 * cupsdStartPolling() - Start polling servers as needed.
28 * cupsdStopBrowsing() - Stop sending and receiving broadcast
30 * cupsdStopPolling() - Stop polling servers as needed.
31 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
32 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
33 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
34 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
35 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
37 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the
39 * dnssdRegisterCallback() - DNSServiceRegister callback.
40 * dnssdRegisterPrinter() - Start sending broadcast information for a
41 * printer or update the broadcast contents.
42 * dequote() - Remote quotes from a string.
43 * process_browse_data() - Process new browse data.
44 * process_implicit_classes() - Create/update implicit classes as needed.
45 * send_cups_browse() - Send new browsing information using the
47 * send_ldap_browse() - Send LDAP printer registrations.
48 * send_slp_browse() - Register the specified printer with SLP.
49 * slp_attr_callback() - SLP attribute callback
50 * slp_dereg_printer() - SLPDereg() the specified printer
51 * slp_get_attr() - Get an attribute from an SLP registration.
52 * slp_reg_callback() - Empty SLPRegReport.
53 * slp_url_callback() - SLP service url callback
54 * update_cups_browse() - Update the browse lists using the CUPS
56 * update_lpd() - Update the LPD configuration as needed.
57 * update_polling() - Read status messages from the poll daemons.
58 * update_smb() - Update the SMB configuration as needed.
62 * Include necessary headers...
72 # ifdef HAVE_COREFOUNDATION
73 # include <CoreFoundation/CoreFoundation.h>
74 # endif /* HAVE_COREFOUNDATION */
75 # ifdef HAVE_SYSTEMCONFIGURATION
76 # include <SystemConfiguration/SystemConfiguration.h>
77 # endif /* HAVE_SYSTEMCONFIGURATION */
78 # endif /* __APPLE__ */
79 #endif /* HAVE_DNSSD */
86 static char *dequote(char *d
, const char *s
, int dlen
);
87 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
88 char *resource
, int resourcelen
);
89 static void process_browse_data(const char *uri
, const char *host
,
90 const char *resource
, cups_ptype_t type
,
91 ipp_pstate_t state
, const char *location
,
92 const char *info
, const char *make_model
,
93 int num_attrs
, cups_option_t
*attrs
);
94 static void process_implicit_classes(int *write_printcap
);
95 static void send_cups_browse(cupsd_printer_t
*p
);
97 static void send_ldap_browse(cupsd_printer_t
*p
);
98 #endif /* HAVE_LDAP */
100 static void send_slp_browse(cupsd_printer_t
*p
);
101 #endif /* HAVE_LIBSLP */
102 static void update_cups_browse(void);
103 static void update_lpd(int onoff
);
104 static void update_polling(void);
105 static void update_smb(int onoff
);
109 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
111 "printerDescription",
113 "printerMakeAndModel",
118 #endif /* HAVE_OPENLDAP */
126 * SLP service name for CUPS...
129 # define SLP_CUPS_SRVTYPE "service:printer"
130 # define SLP_CUPS_SRVLEN 15
134 * Printer service URL structure
137 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
139 struct _slpsrvurl_s
*next
; /* Next URL in list */
140 char url
[HTTP_MAX_URI
];
149 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
150 SLPError errcode
, void *cookie
);
151 static void slp_dereg_printer(cupsd_printer_t
*p
);
152 static int slp_get_attr(const char *attrlist
, const char *tag
,
154 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
156 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
157 unsigned short lifetime
,
158 SLPError errcode
, void *cookie
);
159 #endif /* HAVE_LIBSLP */
163 * For IPP register using a subtype of 'cups' so that shared printer browsing
164 * only finds other CUPS servers (not all IPP based printers).
166 static char dnssdIPPRegType
[] = "_ipp._tcp,_cups";
167 static char dnssdIPPFaxRegType
[] = "_fax-ipp._tcp";
169 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
);
170 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
171 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
173 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
174 DNSServiceFlags flags
,
175 DNSServiceErrorType errorCode
,
176 const char *name
, const char *regtype
,
177 const char *domain
, void *context
);
178 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
179 #endif /* HAVE_DNSSD */
183 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
184 * local printer and remove any pending
185 * references to remote printers.
189 cupsdDeregisterPrinter(
190 cupsd_printer_t
*p
, /* I - Printer to register */
191 int removeit
) /* I - Printer being permanently removed */
194 * Only deregister if browsing is enabled and it's a local printers...
197 if (!Browsing
|| !p
->shared
||
198 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
202 * Announce the deletion...
205 if ((BrowseLocalProtocols
& BROWSE_CUPS
))
207 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
209 p
->type
|= CUPS_PRINTER_DELETE
;
217 if (BrowseLocalProtocols
& BROWSE_SLP
)
218 slp_dereg_printer(p
);
219 #endif /* HAVE_LIBSLP */
222 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
))
223 dnssdDeregisterPrinter(p
);
224 #endif /* HAVE_DNSSD */
229 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
233 cupsdLoadRemoteCache(void)
235 cups_file_t
*fp
; /* remote.cache file */
236 int linenum
; /* Current line number */
237 char line
[1024], /* Line from file */
238 *value
, /* Pointer to value */
239 *valueptr
, /* Pointer into value */
240 scheme
[32], /* Scheme portion of URI */
241 username
[64], /* Username portion of URI */
243 /* Hostname portion of URI */
244 resource
[HTTP_MAX_URI
];
245 /* Resource portion of URI */
246 int port
; /* Port number */
247 cupsd_printer_t
*p
; /* Current printer */
248 time_t now
; /* Current time */
252 * Don't load the cache if the CUPS remote protocol is disabled...
255 if (!Browsing
|| !(BrowseRemoteProtocols
& BROWSE_CUPS
))
257 cupsdLogMessage(CUPSD_LOG_DEBUG
,
258 "cupsdLoadRemoteCache: Not loading remote cache.");
263 * Open the remote.cache file...
266 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
267 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
271 * Read printer configurations until we hit EOF...
278 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
281 * Decode the directive...
284 if (!strcasecmp(line
, "<Printer") ||
285 !strcasecmp(line
, "<DefaultPrinter"))
288 * <Printer name> or <DefaultPrinter name>
291 if (p
== NULL
&& value
)
294 * Add the printer and a base file type...
297 cupsdLogMessage(CUPSD_LOG_DEBUG
,
298 "cupsdLoadRemoteCache: Loading printer %s...", value
);
300 if ((p
= cupsdFindDest(value
)) != NULL
)
302 if (p
->type
& CUPS_PRINTER_CLASS
)
304 cupsdLogMessage(CUPSD_LOG_WARN
,
305 "Cached remote printer \"%s\" conflicts with "
313 p
= cupsdAddPrinter(value
);
316 p
->state
= IPP_PRINTER_IDLE
;
317 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
318 p
->browse_time
= now
;
319 p
->browse_expire
= now
+ BrowseTimeout
;
322 * Set the default printer as needed...
325 if (!strcasecmp(line
, "<DefaultPrinter"))
330 cupsdLogMessage(CUPSD_LOG_ERROR
,
331 "Syntax error on line %d of remote.cache.", linenum
);
335 else if (!strcasecmp(line
, "<Class") ||
336 !strcasecmp(line
, "<DefaultClass"))
339 * <Class name> or <DefaultClass name>
342 if (p
== NULL
&& value
)
345 * Add the printer and a base file type...
348 cupsdLogMessage(CUPSD_LOG_DEBUG
,
349 "cupsdLoadRemoteCache: Loading class %s...", value
);
351 if ((p
= cupsdFindDest(value
)) != NULL
)
352 p
->type
= CUPS_PRINTER_CLASS
;
354 p
= cupsdAddClass(value
);
357 p
->state
= IPP_PRINTER_IDLE
;
358 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
359 p
->browse_time
= now
;
360 p
->browse_expire
= now
+ BrowseTimeout
;
363 * Set the default printer as needed...
366 if (!strcasecmp(line
, "<DefaultClass"))
371 cupsdLogMessage(CUPSD_LOG_ERROR
,
372 "Syntax error on line %d of remote.cache.", linenum
);
376 else if (!strcasecmp(line
, "</Printer>") ||
377 !strcasecmp(line
, "</Class>"))
382 * Close out the current printer...
385 cupsdSetPrinterAttrs(p
);
391 cupsdLogMessage(CUPSD_LOG_ERROR
,
392 "Syntax error on line %d of remote.cache.", linenum
);
398 cupsdLogMessage(CUPSD_LOG_ERROR
,
399 "Syntax error on line %d of remote.cache.", linenum
);
402 else if (!strcasecmp(line
, "Info"))
405 cupsdSetString(&p
->info
, value
);
407 else if (!strcasecmp(line
, "MakeModel"))
410 cupsdSetString(&p
->make_model
, value
);
412 else if (!strcasecmp(line
, "Location"))
415 cupsdSetString(&p
->location
, value
);
417 else if (!strcasecmp(line
, "DeviceURI"))
421 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
422 username
, sizeof(username
), host
, sizeof(host
), &port
,
423 resource
, sizeof(resource
));
425 cupsdSetString(&p
->hostname
, host
);
426 cupsdSetString(&p
->uri
, value
);
427 cupsdSetString(&p
->device_uri
, value
);
431 cupsdLogMessage(CUPSD_LOG_ERROR
,
432 "Syntax error on line %d of remote.cache.", linenum
);
436 else if (!strcasecmp(line
, "Option") && value
)
442 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
445 cupsdLogMessage(CUPSD_LOG_ERROR
,
446 "Syntax error on line %d of remote.cache.", linenum
);
449 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
451 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
455 else if (!strcasecmp(line
, "State"))
458 * Set the initial queue state...
461 if (value
&& !strcasecmp(value
, "idle"))
462 p
->state
= IPP_PRINTER_IDLE
;
463 else if (value
&& !strcasecmp(value
, "stopped"))
464 p
->state
= IPP_PRINTER_STOPPED
;
467 cupsdLogMessage(CUPSD_LOG_ERROR
,
468 "Syntax error on line %d of remote.cache.", linenum
);
472 else if (!strcasecmp(line
, "StateMessage"))
475 * Set the initial queue state message...
479 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
481 else if (!strcasecmp(line
, "Accepting"))
484 * Set the initial accepting state...
488 (!strcasecmp(value
, "yes") ||
489 !strcasecmp(value
, "on") ||
490 !strcasecmp(value
, "true")))
493 (!strcasecmp(value
, "no") ||
494 !strcasecmp(value
, "off") ||
495 !strcasecmp(value
, "false")))
499 cupsdLogMessage(CUPSD_LOG_ERROR
,
500 "Syntax error on line %d of remote.cache.", linenum
);
504 else if (!strcasecmp(line
, "Type"))
507 p
->type
= atoi(value
);
510 cupsdLogMessage(CUPSD_LOG_ERROR
,
511 "Syntax error on line %d of remote.cache.", linenum
);
515 else if (!strcasecmp(line
, "BrowseTime"))
519 time_t t
= atoi(value
);
521 if (t
> p
->browse_expire
)
522 p
->browse_expire
= t
;
526 cupsdLogMessage(CUPSD_LOG_ERROR
,
527 "Syntax error on line %d of remote.cache.", linenum
);
531 else if (!strcasecmp(line
, "JobSheets"))
534 * Set the initial job sheets...
539 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
544 cupsdSetString(&p
->job_sheets
[0], value
);
546 while (isspace(*valueptr
& 255))
551 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
556 cupsdSetString(&p
->job_sheets
[1], value
);
561 cupsdLogMessage(CUPSD_LOG_ERROR
,
562 "Syntax error on line %d of remote.cache.", linenum
);
566 else if (!strcasecmp(line
, "AllowUser"))
571 cupsdAddPrinterUser(p
, value
);
575 cupsdLogMessage(CUPSD_LOG_ERROR
,
576 "Syntax error on line %d of remote.cache.", linenum
);
580 else if (!strcasecmp(line
, "DenyUser"))
585 cupsdAddPrinterUser(p
, value
);
589 cupsdLogMessage(CUPSD_LOG_ERROR
,
590 "Syntax error on line %d of remote.cache.", linenum
);
597 * Something else we don't understand...
600 cupsdLogMessage(CUPSD_LOG_ERROR
,
601 "Unknown configuration directive %s on line %d of remote.cache.",
609 * Do auto-classing if needed...
612 process_implicit_classes(NULL
);
617 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
618 * printer or update the broadcast contents.
622 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
624 if (!Browsing
|| !BrowseLocalProtocols
|| !BrowseInterval
|| !NumBrowsers
||
625 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
629 /* if (BrowseLocalProtocols & BROWSE_SLP)
630 slpRegisterPrinter(p); */
631 #endif /* HAVE_LIBSLP */
634 if (BrowseLocalProtocols
& BROWSE_DNSSD
)
635 dnssdRegisterPrinter(p
);
636 #endif /* HAVE_DNSSD */
641 * 'cupsdRestartPolling()' - Restart polling servers as needed.
645 cupsdRestartPolling(void)
647 int i
; /* Looping var */
648 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
651 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
653 kill(pollp
->pid
, SIGHUP
);
658 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
662 cupsdSaveRemoteCache(void)
664 int i
; /* Looping var */
665 cups_file_t
*fp
; /* printers.conf file */
666 char temp
[1024]; /* Temporary string */
667 cupsd_printer_t
*printer
; /* Current printer class */
668 time_t curtime
; /* Current time */
669 struct tm
*curdate
; /* Current date */
670 cups_option_t
*option
; /* Current option */
674 * Create the remote.cache file...
677 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
679 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
681 cupsdLogMessage(CUPSD_LOG_ERROR
,
682 "Unable to save remote.cache - %s", strerror(errno
));
686 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
689 * Restrict access to the file...
692 fchown(cupsFileNumber(fp
), getuid(), Group
);
693 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
696 * Write a small header to the file...
699 curtime
= time(NULL
);
700 curdate
= localtime(&curtime
);
701 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
703 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
704 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
707 * Write each local printer known to the system...
710 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
712 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
715 * Skip local destinations...
718 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
722 * Write printers as needed...
725 if (printer
== DefaultPrinter
)
726 cupsFilePuts(fp
, "<Default");
728 cupsFilePutChar(fp
, '<');
730 if (printer
->type
& CUPS_PRINTER_CLASS
)
731 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
733 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
735 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
737 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
740 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
742 if (printer
->make_model
)
743 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
745 if (printer
->location
)
746 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
748 if (printer
->device_uri
)
749 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
751 if (printer
->state
== IPP_PRINTER_STOPPED
)
753 cupsFilePuts(fp
, "State Stopped\n");
754 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
757 cupsFilePuts(fp
, "State Idle\n");
759 if (printer
->accepting
)
760 cupsFilePuts(fp
, "Accepting Yes\n");
762 cupsFilePuts(fp
, "Accepting No\n");
764 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
765 printer
->job_sheets
[1]);
767 for (i
= 0; i
< printer
->num_users
; i
++)
768 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
771 for (i
= printer
->num_options
, option
= printer
->options
;
774 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
776 if (printer
->type
& CUPS_PRINTER_CLASS
)
777 cupsFilePuts(fp
, "</Class>\n");
779 cupsFilePuts(fp
, "</Printer>\n");
787 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
791 cupsdSendBrowseList(void)
793 int count
; /* Number of dests to update */
794 cupsd_printer_t
*p
; /* Current printer */
795 time_t ut
, /* Minimum update time */
796 to
; /* Timeout time */
797 int write_printcap
; /* Write the printcap file? */
800 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
804 * Compute the update and timeout times...
808 ut
= to
- BrowseInterval
;
811 * Figure out how many printers need an update...
814 if (BrowseInterval
> 0)
816 int max_count
; /* Maximum number to update */
820 * Throttle the number of printers we'll be updating this time
821 * around based on the number of queues that need updating and
822 * the maximum number of queues to update each second...
825 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
827 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
828 count
< max_count
&& p
!= NULL
;
829 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
830 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
831 p
->shared
&& p
->browse_time
< ut
)
835 * Loop through all of the printers and send local updates as needed...
839 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
841 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
845 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
848 * Check for wraparound...
852 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
856 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
859 else if (p
->browse_time
< ut
)
862 * Need to send an update...
867 p
->browse_time
= time(NULL
);
869 if (BrowseLocalProtocols
& BROWSE_CUPS
)
873 if (BrowseLocalProtocols
& BROWSE_SLP
)
875 #endif /* HAVE_LIBSLP */
878 if (BrowseLocalProtocols
& BROWSE_LDAP
)
880 #endif /* HAVE_LDAP */
885 * Save where we left off so that all printers get updated...
892 * Loop through all of the printers and send local updates as needed...
895 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), write_printcap
= 0;
897 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
900 * If this is a remote queue, see if it needs to be timed out...
903 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
905 if (p
->browse_expire
< to
)
907 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
908 "%s \'%s\' deleted by directory services (timeout).",
909 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
912 cupsdLogMessage(CUPSD_LOG_DEBUG
,
913 "Remote destination \"%s\" has timed out; "
917 cupsArraySave(Printers
);
918 cupsdDeletePrinter(p
, 1);
919 cupsArrayRestore(Printers
);
926 cupsdWritePrintcap();
931 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
935 cupsdStartBrowsing(void)
937 int val
; /* Socket option value */
938 struct sockaddr_in addr
; /* Broadcast address */
939 cupsd_printer_t
*p
; /* Current printer */
944 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
947 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
949 if (BrowseSocket
< 0)
952 * Create the broadcast socket...
955 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
957 cupsdLogMessage(CUPSD_LOG_ERROR
,
958 "Unable to create broadcast socket - %s.",
960 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
961 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
966 * Bind the socket to browse port...
969 memset(&addr
, 0, sizeof(addr
));
970 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
971 addr
.sin_family
= AF_INET
;
972 addr
.sin_port
= htons(BrowsePort
);
974 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
976 cupsdLogMessage(CUPSD_LOG_ERROR
,
977 "Unable to bind broadcast socket - %s.",
981 closesocket(BrowseSocket
);
987 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
988 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
994 * Set the "broadcast" flag...
998 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1000 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1004 closesocket(BrowseSocket
);
1006 close(BrowseSocket
);
1010 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1011 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1016 * Close the socket on exec...
1019 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1022 * Finally, add the socket to the input selection set as needed...
1025 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1028 * We only listen if we want remote printers...
1031 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1039 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1042 * Open SLP handle...
1045 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1047 cupsdLogMessage(CUPSD_LOG_ERROR
,
1048 "Unable to open an SLP handle; disabling SLP browsing!");
1049 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1050 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1053 BrowseSLPRefresh
= 0;
1056 BrowseSLPHandle
= NULL
;
1057 #endif /* HAVE_LIBSLP */
1059 #ifdef HAVE_OPENLDAP
1060 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1064 cupsdLogMessage(CUPSD_LOG_ERROR
,
1065 "Need to set BrowseLDAPDN to use LDAP browsing!");
1066 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1067 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1072 * Open LDAP handle...
1075 int rc
; /* LDAP API status */
1076 int version
= 3; /* LDAP version */
1077 struct berval bv
= {0, ""}; /* SASL bind value */
1081 * Set the certificate file to use for encrypted LDAP sessions...
1084 if (BrowseLDAPCACertFile
)
1086 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1087 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1088 BrowseLDAPCACertFile
);
1090 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1091 (void *)BrowseLDAPCACertFile
))
1093 cupsdLogMessage(CUPSD_LOG_ERROR
,
1094 "Unable to set CA certificate file for LDAP "
1095 "connections: %d - %s", rc
, ldap_err2string(rc
));
1099 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1102 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1103 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1105 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1107 if (rc
!= LDAP_SUCCESS
)
1109 cupsdLogMessage(CUPSD_LOG_ERROR
,
1110 "Unable to initialize LDAP; disabling LDAP browsing!");
1111 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1112 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1114 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1115 (const void *)&version
) != LDAP_SUCCESS
)
1117 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1118 BrowseLDAPHandle
= NULL
;
1119 cupsdLogMessage(CUPSD_LOG_ERROR
,
1120 "Unable to set LDAP protocol version; "
1121 "disabling LDAP browsing!");
1122 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1123 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1127 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1128 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1131 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1132 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1134 if (rc
!= LDAP_SUCCESS
)
1136 cupsdLogMessage(CUPSD_LOG_ERROR
,
1137 "Unable to bind to LDAP server; "
1138 "disabling LDAP browsing!");
1139 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1140 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1141 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1146 BrowseLDAPRefresh
= 0;
1148 #endif /* HAVE_OPENLDAP */
1151 * Enable LPD and SMB printer sharing as needed through external programs...
1154 if (BrowseLocalProtocols
& BROWSE_LPD
)
1157 if (BrowseLocalProtocols
& BROWSE_SMB
)
1161 * Register the individual printers
1164 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1166 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1167 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1168 cupsdRegisterPrinter(p
);
1173 * 'cupsdStartPolling()' - Start polling servers as needed.
1177 cupsdStartPolling(void)
1179 int i
; /* Looping var */
1180 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1181 char polld
[1024]; /* Poll daemon path */
1182 char sport
[255]; /* Server port */
1183 char bport
[255]; /* Browser port */
1184 char interval
[255]; /* Poll interval */
1185 int statusfds
[2]; /* Status pipe */
1186 char *argv
[6]; /* Arguments */
1187 char *envp
[100]; /* Environment */
1191 * Don't do anything if we aren't polling...
1194 if (NumPolled
== 0 || BrowseSocket
< 0)
1197 PollStatusBuffer
= NULL
;
1202 * Setup string arguments for polld, port and interval options.
1205 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1207 sprintf(bport
, "%d", BrowsePort
);
1210 sprintf(interval
, "%d", BrowseInterval
);
1212 strcpy(interval
, "30");
1214 argv
[0] = "cups-polld";
1220 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1223 * Create a pipe that receives the status messages from each
1227 if (cupsdOpenPipe(statusfds
))
1229 cupsdLogMessage(CUPSD_LOG_ERROR
,
1230 "Unable to create polling status pipes - %s.",
1233 PollStatusBuffer
= NULL
;
1237 PollPipe
= statusfds
[0];
1238 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1241 * Run each polling daemon, redirecting stderr to the polling pipe...
1244 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1246 sprintf(sport
, "%d", pollp
->port
);
1248 argv
[1] = pollp
->hostname
;
1250 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1251 0, &(pollp
->pid
)) < 0)
1253 cupsdLogMessage(CUPSD_LOG_ERROR
,
1254 "cupsdStartPolling: Unable to fork polling daemon - %s",
1260 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1261 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1262 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1265 close(statusfds
[1]);
1268 * Finally, add the pipe to the input selection set...
1271 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1276 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1280 cupsdStopBrowsing(void)
1282 cupsd_printer_t
*p
; /* Current printer */
1285 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1289 * De-register the individual printers
1292 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1294 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1295 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1296 cupsdDeregisterPrinter(p
, 1);
1299 * Shut down browsing sockets...
1302 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1306 * Close the socket and remove it from the input selection set.
1310 closesocket(BrowseSocket
);
1312 close(BrowseSocket
);
1315 cupsdRemoveSelect(BrowseSocket
);
1320 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1324 * Close SLP handle...
1327 SLPClose(BrowseSLPHandle
);
1328 BrowseSLPHandle
= NULL
;
1330 #endif /* HAVE_LIBSLP */
1332 #ifdef HAVE_OPENLDAP
1333 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1336 ldap_unbind(BrowseLDAPHandle
);
1337 BrowseLDAPHandle
= NULL
;
1339 #endif /* HAVE_OPENLDAP */
1342 * Disable LPD and SMB printer sharing as needed through external programs...
1345 if (BrowseLocalProtocols
& BROWSE_LPD
)
1348 if (BrowseLocalProtocols
& BROWSE_SMB
)
1354 * 'cupsdStopPolling()' - Stop polling servers as needed.
1358 cupsdStopPolling(void)
1360 int i
; /* Looping var */
1361 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1366 cupsdStatBufDelete(PollStatusBuffer
);
1369 cupsdRemoveSelect(PollPipe
);
1372 PollStatusBuffer
= NULL
;
1375 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1377 cupsdEndProcess(pollp
->pid
, 0);
1383 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1387 cupsdUpdateDNSSDBrowse(
1388 cupsd_printer_t
*p
) /* I - Printer being queried */
1390 DNSServiceErrorType sdErr
; /* Service discovery error */
1393 if ((sdErr
= DNSServiceProcessResult(p
->dnssd_ipp_ref
))
1394 != kDNSServiceErr_NoError
)
1396 cupsdLogMessage(CUPSD_LOG_ERROR
,
1397 "DNS Service Discovery registration error %d for \"%s\"!",
1399 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
1400 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
1402 p
->dnssd_ipp_ref
= NULL
;
1403 p
->dnssd_ipp_fd
= -1;
1406 #endif /* HAVE_DNSSD */
1409 #ifdef HAVE_OPENLDAP
1411 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1415 cupsdUpdateLDAPBrowse(void)
1417 char uri
[HTTP_MAX_URI
], /* Printer URI */
1418 host
[HTTP_MAX_URI
], /* Hostname */
1419 resource
[HTTP_MAX_URI
], /* Resource path */
1420 location
[1024], /* Printer location */
1421 info
[1024], /* Printer information */
1422 make_model
[1024], /* Printer make and model */
1423 **value
; /* Holds the returned data from LDAP */
1424 int type
; /* Printer type */
1425 int rc
; /* LDAP status */
1426 int limit
; /* Size limit */
1427 LDAPMessage
*res
, /* LDAP search results */
1428 *e
; /* Current entry from search */
1432 * Search for printers...
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1437 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1439 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1440 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1441 if (rc
!= LDAP_SUCCESS
)
1443 cupsdLogMessage(CUPSD_LOG_ERROR
,
1444 "LDAP search returned error %d: %s", rc
,
1445 ldap_err2string(rc
));
1449 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1450 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1455 * Loop through the available printers...
1458 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1460 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1463 * Get the required values from this entry...
1466 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1467 "printerDescription")) == NULL
)
1470 strlcpy(info
, *value
, sizeof(info
));
1471 ldap_value_free(value
);
1473 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1474 "printerLocation")) == NULL
)
1477 strlcpy(location
, *value
, sizeof(location
));
1478 ldap_value_free(value
);
1480 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1481 "printerMakeAndModel")) == NULL
)
1484 strlcpy(make_model
, *value
, sizeof(make_model
));
1485 ldap_value_free(value
);
1487 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1488 "printerType")) == NULL
)
1491 type
= atoi(*value
);
1492 ldap_value_free(value
);
1494 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1495 "printerURI")) == NULL
)
1498 strlcpy(uri
, *value
, sizeof(uri
));
1499 ldap_value_free(value
);
1502 * Process the entry as browse data...
1505 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1506 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1507 location
, info
, make_model
, 0, NULL
);
1511 #endif /* HAVE_OPENLDAP */
1516 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1520 cupsdUpdateSLPBrowse(void)
1522 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1523 *next
; /* Next service in list */
1524 cupsd_printer_t p
; /* Printer information */
1525 const char *uri
; /* Pointer to printer URI */
1526 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1527 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1531 * Reset the refresh time...
1534 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1537 * Poll for remote printers using SLP...
1542 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1543 slp_url_callback
, &s
);
1546 * Loop through the list of available printers...
1552 * Save the "next" pointer...
1558 * Load a cupsd_printer_t structure with the SLP service attributes...
1561 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1564 * Process this printer entry...
1567 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1569 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1572 * Pull the URI apart to see if this is a local or remote printer...
1575 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1576 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1577 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1581 * Free this listing...
1584 cupsdClearString(&p
.info
);
1585 cupsdClearString(&p
.location
);
1586 cupsdClearString(&p
.make_model
);
1591 #endif /* HAVE_LIBSLP */
1595 * 'dequote()' - Remote quotes from a string.
1598 static char * /* O - Dequoted string */
1599 dequote(char *d
, /* I - Destination string */
1600 const char *s
, /* I - Source string */
1601 int dlen
) /* I - Destination length */
1603 char *dptr
; /* Pointer into destination */
1608 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1625 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1628 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1629 is_local_queue(const char *uri
, /* I - Printer URI */
1630 char *host
, /* O - Host string */
1631 int hostlen
, /* I - Length of host buffer */
1632 char *resource
, /* O - Resource string */
1633 int resourcelen
) /* I - Length of resource buffer */
1635 char scheme
[32], /* Scheme portion of URI */
1636 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1637 int port
; /* Port portion of URI */
1638 cupsd_netif_t
*iface
; /* Network interface */
1642 * Pull the URI apart to see if this is a local or remote printer...
1645 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1646 username
, sizeof(username
), host
, hostlen
, &port
,
1647 resource
, resourcelen
) < HTTP_URI_OK
)
1650 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1653 * Check for local server addresses...
1656 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1661 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1663 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1664 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1668 * If we get here, the printer is remote...
1676 * 'process_browse_data()' - Process new browse data.
1680 process_browse_data(
1681 const char *uri
, /* I - URI of printer/class */
1682 const char *host
, /* I - Hostname */
1683 const char *resource
, /* I - Resource path */
1684 cups_ptype_t type
, /* I - Printer type */
1685 ipp_pstate_t state
, /* I - Printer state */
1686 const char *location
, /* I - Printer location */
1687 const char *info
, /* I - Printer information */
1688 const char *make_model
, /* I - Printer make and model */
1689 int num_attrs
, /* I - Number of attributes */
1690 cups_option_t
*attrs
) /* I - Attributes */
1692 int i
; /* Looping var */
1693 int update
, /* Update printer attributes? */
1694 write_printcap
; /* Write the printcap file? */
1695 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1696 name
[IPP_MAX_NAME
], /* Name of printer */
1697 newname
[IPP_MAX_NAME
], /* New name of printer */
1698 *hptr
, /* Pointer into hostname */
1699 *sptr
; /* Pointer into ServerName */
1700 char local_make_model
[IPP_MAX_NAME
];
1701 /* Local make and model */
1702 cupsd_printer_t
*p
; /* Printer information */
1703 const char *ipp_options
, /* ipp-options value */
1704 *lease_duration
; /* lease-duration value */
1708 * Determine if the URI contains any illegal characters in it...
1711 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1712 (strncmp(resource
, "/printers/", 10) &&
1713 strncmp(resource
, "/classes/", 9)))
1715 cupsdLogMessage(CUPSD_LOG_ERROR
,
1716 "process_browse_data: Bad printer URI in browse data: %s",
1721 if (strchr(resource
, '?') ||
1722 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1723 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1725 cupsdLogMessage(CUPSD_LOG_ERROR
,
1726 "process_browse_data: Bad resource in browse data: %s",
1732 * OK, this isn't a local printer; add any remote options...
1735 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1737 if (BrowseRemoteOptions
)
1739 if (BrowseRemoteOptions
[0] == '?')
1742 * Override server-supplied options...
1745 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1747 else if (ipp_options
)
1750 * Combine the server and local options...
1753 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1754 BrowseRemoteOptions
);
1759 * Just use the local options...
1762 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1767 else if (ipp_options
)
1770 * Just use the server-supplied options...
1773 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1778 * See if we already have it listed in the Printers list, and add it if not...
1781 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
1782 type
&= ~CUPS_PRINTER_IMPLICIT
;
1785 hptr
= strchr(host
, '.');
1786 sptr
= strchr(ServerName
, '.');
1788 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1791 * Strip the common domain name components...
1794 while (hptr
!= NULL
)
1796 if (!strcasecmp(hptr
, sptr
))
1802 hptr
= strchr(hptr
+ 1, '.');
1806 if (type
& CUPS_PRINTER_CLASS
)
1809 * Remote destination is a class...
1812 if (!strncmp(resource
, "/classes/", 9))
1813 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1818 *hptr
= '.'; /* Resource FQDN */
1820 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1822 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1824 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1827 * Nope, this isn't the same host; if the hostname isn't the local host,
1828 * add it to the other class and then find a class using the full host
1832 if (p
->type
& CUPS_PRINTER_REMOTE
)
1834 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1835 "Renamed remote class \"%s\" to \"%s@%s\"...",
1836 p
->name
, p
->name
, p
->hostname
);
1837 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1838 "Class \'%s\' deleted by directory services.",
1841 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1842 cupsdRenamePrinter(p
, newname
);
1844 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1845 "Class \'%s\' added by directory services.",
1851 else if (!p
->hostname
)
1854 * Hostname not set, so this must be a cached remote printer
1855 * that was created for a pending print job...
1858 cupsdSetString(&p
->hostname
, host
);
1859 cupsdSetString(&p
->uri
, uri
);
1860 cupsdSetString(&p
->device_uri
, uri
);
1867 * Use the short name for this shared class.
1870 strlcpy(name
, resource
+ 9, sizeof(name
));
1873 else if (p
&& !p
->hostname
)
1876 * Hostname not set, so this must be a cached remote printer
1877 * that was created for a pending print job...
1880 cupsdSetString(&p
->hostname
, host
);
1881 cupsdSetString(&p
->uri
, uri
);
1882 cupsdSetString(&p
->device_uri
, uri
);
1889 * Class doesn't exist; add it...
1892 p
= cupsdAddClass(name
);
1894 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1896 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1897 "Class \'%s\' added by directory services.", name
);
1900 * Force the URI to point to the real server...
1903 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1905 cupsdSetString(&p
->uri
, uri
);
1906 cupsdSetString(&p
->device_uri
, uri
);
1907 cupsdSetString(&p
->hostname
, host
);
1916 * Remote destination is a printer...
1919 if (!strncmp(resource
, "/printers/", 10))
1920 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1925 *hptr
= '.'; /* Resource FQDN */
1927 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
1929 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
1931 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1934 * Nope, this isn't the same host; if the hostname isn't the local host,
1935 * add it to the other printer and then find a printer using the full host
1939 if (p
->type
& CUPS_PRINTER_REMOTE
)
1941 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1942 "Renamed remote printer \"%s\" to \"%s@%s\"...",
1943 p
->name
, p
->name
, p
->hostname
);
1944 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1945 "Printer \'%s\' deleted by directory services.",
1948 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1949 cupsdRenamePrinter(p
, newname
);
1951 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1952 "Printer \'%s\' added by directory services.",
1958 else if (!p
->hostname
)
1961 * Hostname not set, so this must be a cached remote printer
1962 * that was created for a pending print job...
1965 cupsdSetString(&p
->hostname
, host
);
1966 cupsdSetString(&p
->uri
, uri
);
1967 cupsdSetString(&p
->device_uri
, uri
);
1974 * Use the short name for this shared printer.
1977 strlcpy(name
, resource
+ 10, sizeof(name
));
1980 else if (p
&& !p
->hostname
)
1983 * Hostname not set, so this must be a cached remote printer
1984 * that was created for a pending print job...
1987 cupsdSetString(&p
->hostname
, host
);
1988 cupsdSetString(&p
->uri
, uri
);
1989 cupsdSetString(&p
->device_uri
, uri
);
1996 * Printer doesn't exist; add it...
1999 p
= cupsdAddPrinter(name
);
2001 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2002 "Printer \'%s\' added by directory services.", name
);
2004 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2007 * Force the URI to point to the real server...
2010 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2012 cupsdSetString(&p
->hostname
, host
);
2013 cupsdSetString(&p
->uri
, uri
);
2014 cupsdSetString(&p
->device_uri
, uri
);
2022 * Update the state...
2026 p
->browse_time
= time(NULL
);
2028 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2032 * Grab the lease-duration for the browse data; anything less then 1
2033 * second or more than 1 week gets the default BrowseTimeout...
2036 i
= atoi(lease_duration
);
2037 if (i
< 1 || i
> 604800)
2040 p
->browse_expire
= p
->browse_time
+ i
;
2043 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2045 if (type
& CUPS_PRINTER_REJECTING
)
2047 type
&= ~CUPS_PRINTER_REJECTING
;
2055 else if (!p
->accepting
)
2061 if (p
->type
!= type
)
2067 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2069 cupsdSetString(&p
->location
, location
);
2073 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2075 cupsdSetString(&p
->info
, info
);
2080 if (!make_model
|| !make_model
[0])
2082 if (type
& CUPS_PRINTER_CLASS
)
2083 snprintf(local_make_model
, sizeof(local_make_model
),
2084 "Remote Class on %s", host
);
2086 snprintf(local_make_model
, sizeof(local_make_model
),
2087 "Remote Printer on %s", host
);
2090 snprintf(local_make_model
, sizeof(local_make_model
),
2091 "%s on %s", make_model
, host
);
2093 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2095 cupsdSetString(&p
->make_model
, local_make_model
);
2101 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2104 * See if we need to update the attributes...
2107 if (p
->num_options
!= num_attrs
)
2111 for (i
= 0; i
< num_attrs
; i
++)
2112 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2113 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2114 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2123 * Free the old options...
2126 cupsFreeOptions(p
->num_options
, p
->options
);
2129 p
->num_options
= num_attrs
;
2132 if (type
& CUPS_PRINTER_DELETE
)
2134 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2135 "%s \'%s\' deleted by directory services.",
2136 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2138 cupsdExpireSubscriptions(p
, NULL
);
2140 cupsdDeletePrinter(p
, 1);
2141 cupsdUpdateImplicitClasses();
2146 cupsdSetPrinterAttrs(p
);
2147 cupsdUpdateImplicitClasses();
2151 * See if we have a default printer... If not, make the first network
2152 * default printer the default.
2155 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2158 * Find the first network default printer and use it...
2161 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2163 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2164 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2173 * Do auto-classing if needed...
2176 process_implicit_classes(&write_printcap
);
2179 * Update the printcap file...
2183 cupsdWritePrintcap();
2189 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2192 static char * /* O - TXT record */
2193 dnssdBuildTxtRecord(
2194 int *txt_len
, /* O - TXT record length */
2195 cupsd_printer_t
*p
) /* I - Printer information */
2197 int i
, j
; /* Looping vars */
2198 char type_str
[32], /* Type to string buffer */
2199 state_str
[32], /* State to string buffer */
2200 rp_str
[1024], /* Queue name string buffer */
2201 air_str
[1024], /* auth-info-required string buffer */
2202 *keyvalue
[32][2]; /* Table of key/value pairs */
2206 * Load up the key value pairs...
2211 keyvalue
[i
][0] = "txtvers";
2212 keyvalue
[i
++][1] = "1";
2214 keyvalue
[i
][0] = "qtotal";
2215 keyvalue
[i
++][1] = "1";
2217 keyvalue
[i
][0] = "rp";
2218 keyvalue
[i
++][1] = rp_str
;
2219 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2220 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2222 keyvalue
[i
][0] = "ty";
2223 keyvalue
[i
++][1] = p
->make_model
;
2225 if (p
->location
&& *p
->location
!= '\0')
2227 keyvalue
[i
][0] = "note";
2228 keyvalue
[i
++][1] = p
->location
;
2231 keyvalue
[i
][0] = "product";
2232 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2234 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2235 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2237 keyvalue
[i
][0] = "printer-state";
2238 keyvalue
[i
++][1] = state_str
;
2240 keyvalue
[i
][0] = "printer-type";
2241 keyvalue
[i
++][1] = type_str
;
2243 keyvalue
[i
][0] = "Transparent";
2244 keyvalue
[i
++][1] = "T";
2246 keyvalue
[i
][0] = "Binary";
2247 keyvalue
[i
++][1] = "T";
2249 if ((p
->type
& CUPS_PRINTER_FAX
))
2251 keyvalue
[i
][0] = "Fax";
2252 keyvalue
[i
++][1] = "T";
2255 if ((p
->type
& CUPS_PRINTER_COLOR
))
2257 keyvalue
[i
][0] = "Color";
2258 keyvalue
[i
++][1] = "T";
2261 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2263 keyvalue
[i
][0] = "Duplex";
2264 keyvalue
[i
++][1] = "T";
2267 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2269 keyvalue
[i
][0] = "Staple";
2270 keyvalue
[i
++][1] = "T";
2273 if ((p
->type
& CUPS_PRINTER_COPIES
))
2275 keyvalue
[i
][0] = "Copies";
2276 keyvalue
[i
++][1] = "T";
2279 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2281 keyvalue
[i
][0] = "Collate";
2282 keyvalue
[i
++][1] = "T";
2285 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2287 keyvalue
[i
][0] = "Punch";
2288 keyvalue
[i
++][1] = "T";
2291 if ((p
->type
& CUPS_PRINTER_BIND
))
2293 keyvalue
[i
][0] = "Bind";
2294 keyvalue
[i
++][1] = "T";
2297 if ((p
->type
& CUPS_PRINTER_SORT
))
2299 keyvalue
[i
][0] = "Sort";
2300 keyvalue
[i
++][1] = "T";
2303 keyvalue
[i
][0] = "pdl";
2304 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2306 if (p
->num_auth_info_required
)
2308 char *air
= air_str
; /* Pointer into string */
2311 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2313 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2319 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2323 keyvalue
[i
][0] = "air";
2324 keyvalue
[i
++][1] = air
;
2328 * Then pack them into a proper txt record...
2331 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2336 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2341 dnssdDeregisterPrinter(
2342 cupsd_printer_t
*p
) /* I - Printer */
2344 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2347 * Closing the socket deregisters the service
2350 if (p
->dnssd_ipp_ref
)
2352 cupsdRemoveSelect(p
->dnssd_ipp_fd
);
2353 DNSServiceRefDeallocate(p
->dnssd_ipp_ref
);
2354 p
->dnssd_ipp_ref
= NULL
;
2355 p
->dnssd_ipp_fd
= -1;
2358 cupsdClearString(&p
->reg_name
);
2363 * p->txt_record is malloc'd, not _cupsStrAlloc'd...
2366 free(p
->txt_record
);
2367 p
->txt_record
= NULL
;
2373 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2374 * TXT record format.
2377 static char * /* O - TXT record */
2378 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2379 char *keyvalue
[][2], /* I - Table of key value pairs */
2380 int count
) /* I - Items in table */
2382 int i
; /* Looping var */
2383 int length
; /* Length of TXT record */
2384 int length2
; /* Length of value */
2385 char *txtRecord
; /* TXT record buffer */
2386 char *cursor
; /* Looping pointer */
2390 * Calculate the buffer size
2393 for (length
= i
= 0; i
< count
; i
++)
2394 length
+= 1 + strlen(keyvalue
[i
][0]) +
2395 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2398 * Allocate and fill it
2401 txtRecord
= malloc(length
);
2406 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2409 * Drop in the p-string style length byte followed by the data
2412 length
= strlen(keyvalue
[i
][0]);
2413 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2415 *cursor
++ = (unsigned char)(length
+ length2
);
2417 memcpy(cursor
, keyvalue
[i
][0], length
);
2424 memcpy(cursor
, keyvalue
[i
][1], length2
);
2435 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2439 dnssdRegisterCallback(
2440 DNSServiceRef sdRef
, /* I - DNS Service reference */
2441 DNSServiceFlags flags
, /* I - Reserved for future use */
2442 DNSServiceErrorType errorCode
, /* I - Error code */
2443 const char *name
, /* I - Service name */
2444 const char *regtype
, /* I - Service type */
2445 const char *domain
, /* I - Domain. ".local" for now */
2446 void *context
) /* I - User-defined context */
2450 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2451 "dnssdRegisterCallback(%s, %s)", name
, regtype
);
2455 cupsdLogMessage(CUPSD_LOG_ERROR
,
2456 "DNSServiceRegister failed with error %d", (int)errorCode
);
2463 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2464 * or update the broadcast contents.
2468 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2470 DNSServiceErrorType se
; /* dnssd errors */
2471 cupsd_listener_t
*lis
; /* Current listening socket */
2472 char *txt_record
, /* TXT record buffer */
2473 *name
; /* Service name */
2474 int txt_len
, /* TXT record length */
2475 port
; /* IPP port number */
2476 char str_buffer
[1024];
2477 /* C-string buffer */
2478 const char *computerName
; /* Computer name c-string ptr */
2479 const char *regtype
; /* Registration type */
2480 #ifdef HAVE_COREFOUNDATION_H
2481 CFStringRef computerNameRef
;/* Computer name CFString */
2482 CFStringEncoding nameEncoding
; /* Computer name encoding */
2483 CFMutableStringRef shortNameRef
; /* Mutable name string */
2484 CFIndex nameLength
; /* Name string length */
2486 int nameLength
; /* Name string length */
2487 #endif /* HAVE_COREFOUNDATION_H */
2490 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2491 !p
->dnssd_ipp_ref
? "new" : "update");
2494 * If per-printer sharing was just disabled make sure we're not
2495 * registered before returning.
2500 dnssdDeregisterPrinter(p
);
2505 * Get the computer name as a c-string...
2508 #ifdef HAVE_COREFOUNDATION_H
2509 computerName
= NULL
;
2510 if ((computerNameRef
= SCDynamicStoreCopyComputerName(NULL
, &nameEncoding
)))
2511 if ((computerName
= CFStringGetCStringPtr(computerNameRef
,
2512 kCFStringEncodingUTF8
)) == NULL
)
2513 if (CFStringGetCString(computerNameRef
, str_buffer
, sizeof(str_buffer
),
2514 kCFStringEncodingUTF8
))
2515 computerName
= str_buffer
;
2517 computerName
= ServerName
;
2518 #endif /* HAVE_COREFOUNDATION_H */
2521 * The registered name takes the form of "<printer-info> @ <computer name>"...
2526 cupsdSetStringf(&name
, "%s @ %s",
2527 (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
,
2530 cupsdSetString(&name
, (p
->info
&& strlen(p
->info
)) ? p
->info
: p
->name
);
2532 #ifdef HAVE_COREFOUNDATION_H
2533 if (computerNameRef
)
2534 CFRelease(computerNameRef
);
2535 #endif /* HAVE_COREFOUNDATION_H */
2538 * If an existing printer was renamed, unregister it and start over...
2541 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2542 dnssdDeregisterPrinter(p
);
2544 txt_len
= 0; /* anti-compiler-warning-code */
2545 txt_record
= dnssdBuildTxtRecord(&txt_len
, p
);
2547 if (!p
->dnssd_ipp_ref
)
2550 * Initial registration...
2553 cupsdSetString(&p
->reg_name
, name
);
2557 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
2559 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
2561 if (lis
->address
.addr
.sa_family
== AF_INET
)
2563 port
= ntohs(lis
->address
.ipv4
.sin_port
);
2566 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
2568 port
= ntohs(lis
->address
.ipv6
.sin6_port
);
2574 * Use the _fax subtype for fax queues...
2577 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? dnssdIPPFaxRegType
:
2580 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) type is \"%s\"",
2583 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, name
, regtype
,
2584 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2585 dnssdRegisterCallback
, p
);
2588 * In case the name is too long, try shortening the string one character
2592 if (se
== kDNSServiceErr_BadParam
)
2594 #ifdef HAVE_COREFOUNDATION_H
2595 if ((shortNameRef
= CFStringCreateMutable(NULL
, 0)) != NULL
)
2597 CFStringAppendCString(shortNameRef
, name
, kCFStringEncodingUTF8
);
2598 nameLength
= CFStringGetLength(shortNameRef
);
2600 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2602 CFStringDelete(shortNameRef
, CFRangeMake(--nameLength
, 1));
2603 if (CFStringGetCString(shortNameRef
, str_buffer
, sizeof(str_buffer
),
2604 kCFStringEncodingUTF8
))
2606 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
,
2607 regtype
, NULL
, NULL
, htons(port
),
2608 txt_len
, txt_record
,
2609 dnssdRegisterCallback
, p
);
2613 CFRelease(shortNameRef
);
2616 nameLength
= strlen(name
);
2617 while (se
== kDNSServiceErr_BadParam
&& nameLength
> 1)
2619 name
[--nameLength
] = '\0';
2620 se
= DNSServiceRegister(&p
->dnssd_ipp_ref
, 0, 0, str_buffer
, regtype
,
2621 NULL
, NULL
, htons(port
), txt_len
, txt_record
,
2622 dnssdRegisterCallback
, p
);
2624 #endif /* HAVE_COREFOUNDATION_H */
2627 if (se
== kDNSServiceErr_NoError
)
2629 p
->dnssd_ipp_fd
= DNSServiceRefSockFD(p
->dnssd_ipp_ref
);
2630 p
->txt_record
= txt_record
;
2631 p
->txt_len
= txt_len
;
2634 cupsdAddSelect(p
->dnssd_ipp_fd
, (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
,
2638 cupsdLogMessage(CUPSD_LOG_WARN
,
2639 "DNS-SD registration of \"%s\" failed with %d",
2642 else if (txt_len
!= p
->txt_len
|| memcmp(txt_record
, p
->txt_record
, txt_len
))
2645 * Update the existing registration...
2648 /* A TTL of 0 means use record's original value (Radar 3176248) */
2649 se
= DNSServiceUpdateRecord(p
->dnssd_ipp_ref
, NULL
, 0,
2650 txt_len
, txt_record
, 0);
2653 free(p
->txt_record
);
2655 p
->txt_record
= txt_record
;
2656 p
->txt_len
= txt_len
;
2663 cupsdClearString(&name
);
2665 #endif /* HAVE_DNSSD */
2669 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2673 process_implicit_classes(
2674 int *write_printcap
) /* O - Write printcap file? */
2676 int i
; /* Looping var */
2677 int update
; /* Update printer attributes? */
2678 char name
[IPP_MAX_NAME
], /* Name of printer */
2679 *hptr
; /* Pointer into hostname */
2680 cupsd_printer_t
*p
, /* Printer information */
2681 *pclass
, /* Printer class */
2682 *first
; /* First printer in class */
2683 int offset
, /* Offset of name */
2684 len
; /* Length of name */
2687 if (!ImplicitClasses
|| !Printers
)
2691 * Loop through all available printers and create classes as needed...
2694 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2695 update
= 0, pclass
= NULL
, first
= NULL
;
2697 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2700 * Skip implicit classes...
2703 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2710 * If len == 0, get the length of this printer name up to the "@"
2714 cupsArraySave(Printers
);
2717 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2718 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2721 * We have more than one printer with the same name; see if
2722 * we have a class, and if this printer is a member...
2725 if (pclass
&& strcasecmp(pclass
->name
, name
))
2728 cupsdSetPrinterAttrs(pclass
);
2734 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2737 * Need to add the class...
2740 pclass
= cupsdAddPrinter(name
);
2741 cupsArrayAdd(ImplicitPrinters
, pclass
);
2743 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2744 pclass
->accepting
= 1;
2745 pclass
->state
= IPP_PRINTER_IDLE
;
2747 cupsdSetString(&pclass
->location
, p
->location
);
2748 cupsdSetString(&pclass
->info
, p
->info
);
2750 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
2751 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
2756 *write_printcap
= 1;
2758 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2760 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2761 "Implicit class \'%s\' added by directory services.",
2767 for (i
= 0; i
< pclass
->num_printers
; i
++)
2768 if (pclass
->printers
[i
] == first
)
2771 if (i
>= pclass
->num_printers
)
2773 first
->in_implicit_class
= 1;
2774 cupsdAddPrinterToClass(pclass
, first
);
2780 for (i
= 0; i
< pclass
->num_printers
; i
++)
2781 if (pclass
->printers
[i
] == p
)
2784 if (i
>= pclass
->num_printers
)
2786 p
->in_implicit_class
= 1;
2787 cupsdAddPrinterToClass(pclass
, p
);
2794 * First time around; just get name length and mark it as first
2798 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2799 len
= hptr
- p
->name
;
2801 len
= strlen(p
->name
);
2803 strncpy(name
, p
->name
, len
);
2807 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2808 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2811 * Can't use same name as a local printer; add "Any" to the
2812 * front of the name, unless we have explicitly disabled
2813 * the "ImplicitAnyClasses"...
2816 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2819 * Add "Any" to the class name...
2822 strcpy(name
, "Any");
2823 strncpy(name
+ 3, p
->name
, len
);
2824 name
[len
+ 3] = '\0';
2830 * Don't create an implicit class if we have a local printer
2831 * with the same name...
2835 cupsArrayRestore(Printers
);
2843 cupsArrayRestore(Printers
);
2847 * Update the last printer class as needed...
2850 if (pclass
&& update
)
2851 cupsdSetPrinterAttrs(pclass
);
2856 * 'send_cups_browse()' - Send new browsing information using the CUPS
2861 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2863 int i
; /* Looping var */
2864 cups_ptype_t type
; /* Printer type */
2865 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2866 int bytes
; /* Length of packet */
2867 char packet
[1453], /* Browse data packet */
2868 uri
[1024], /* Printer URI */
2869 location
[1024], /* printer-location */
2870 info
[1024], /* printer-info */
2872 /* printer-make-and-model */
2873 cupsd_netif_t
*iface
; /* Network interface */
2877 * Figure out the printer type value...
2880 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2883 type
|= CUPS_PRINTER_REJECTING
;
2885 if (p
== DefaultPrinter
)
2886 type
|= CUPS_PRINTER_DEFAULT
;
2889 * Remove quotes from printer-info, printer-location, and
2890 * printer-make-and-model attributes...
2893 dequote(location
, p
->location
, sizeof(location
));
2894 dequote(info
, p
->info
, sizeof(info
));
2897 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2898 else if (p
->type
& CUPS_PRINTER_CLASS
)
2900 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2901 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2903 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2906 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2908 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2911 * Send a packet to each browse address...
2914 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2918 * Send the browse packet to one or more interfaces...
2921 if (!strcmp(b
->iface
, "*"))
2924 * Send to all local interfaces...
2929 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2931 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2934 * Only send to local, IPv4 interfaces...
2937 if (!iface
->is_local
|| !iface
->port
||
2938 iface
->address
.addr
.sa_family
!= AF_INET
)
2941 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2942 iface
->hostname
, iface
->port
,
2943 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2946 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2947 type
, p
->state
, uri
, location
, info
, make_model
,
2948 p
->browse_attrs
? p
->browse_attrs
: "");
2950 bytes
= strlen(packet
);
2952 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2953 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2954 iface
->name
, packet
);
2956 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2958 sendto(BrowseSocket
, packet
, bytes
, 0,
2959 (struct sockaddr
*)&(iface
->broadcast
),
2960 httpAddrLength(&(iface
->broadcast
)));
2963 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2966 * Send to the named interface using the IPv4 address...
2970 if (strcmp(b
->iface
, iface
->name
))
2975 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2978 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2982 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2983 iface
->hostname
, iface
->port
,
2984 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2987 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2988 type
, p
->state
, uri
, location
, info
, make_model
,
2989 p
->browse_attrs
? p
->browse_attrs
: "");
2991 bytes
= strlen(packet
);
2993 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2994 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2995 iface
->name
, packet
);
2997 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2999 sendto(BrowseSocket
, packet
, bytes
, 0,
3000 (struct sockaddr
*)&(iface
->broadcast
),
3001 httpAddrLength(&(iface
->broadcast
)));
3008 * Send the browse packet to the indicated address using
3009 * the default server name...
3012 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3013 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3014 p
->browse_attrs
? p
->browse_attrs
: "");
3016 bytes
= strlen(packet
);
3017 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3018 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3020 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3021 (struct sockaddr
*)&(b
->to
),
3022 httpAddrLength(&(b
->to
))) <= 0)
3025 * Unable to send browse packet, so remove this address from the
3029 cupsdLogMessage(CUPSD_LOG_ERROR
,
3030 "cupsdSendBrowseList: sendto failed for browser "
3032 (int)(b
- Browsers
+ 1), strerror(errno
));
3035 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3044 #ifdef HAVE_OPENLDAP
3046 * 'send_ldap_browse()' - Send LDAP printer registrations.
3050 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3052 int i
; /* Looping var... */
3053 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3054 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3055 LDAPMessage
*res
; /* Search result token */
3056 char *cn_value
[2], /* Change records */
3062 typestring
[255], /* String to hold printer-type */
3063 filter
[256], /* Search filter for possible UPDATEs */
3064 dn
[1024]; /* DN of the printer we are adding */
3065 int rc
; /* LDAP status */
3066 static const char * const objectClass_values
[] =
3067 { /* The 3 objectClass's we use in */
3068 "top", /* our LDAP entries */
3074 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3077 * Everything in ldap is ** so we fudge around it...
3080 sprintf(typestring
, "%u", p
->type
);
3082 cn_value
[0] = p
->name
;
3084 info
[0] = p
->info
? p
->info
: "Unknown";
3086 location
[0] = p
->location
? p
->location
: "Unknown";
3088 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3089 make_model
[1] = NULL
;
3090 type
[0] = typestring
;
3095 snprintf(filter
, sizeof(filter
),
3096 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3098 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3099 filter
, (char **)ldap_attrs
, 0, &res
);
3100 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3103 mods
[0].mod_type
= "cn";
3104 mods
[0].mod_values
= cn_value
;
3105 mods
[1].mod_type
= "printerDescription";
3106 mods
[1].mod_values
= info
;
3107 mods
[2].mod_type
= "printerURI";
3108 mods
[2].mod_values
= uri
;
3109 mods
[3].mod_type
= "printerLocation";
3110 mods
[3].mod_values
= location
;
3111 mods
[4].mod_type
= "printerMakeAndModel";
3112 mods
[4].mod_values
= make_model
;
3113 mods
[5].mod_type
= "printerType";
3114 mods
[5].mod_values
= type
;
3115 mods
[6].mod_type
= "objectClass";
3116 mods
[6].mod_values
= (char **)objectClass_values
;
3118 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3119 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3121 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3124 * Printer has already been registered, modify the current
3128 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3129 "send_ldap_browse: Replacing entry...");
3131 for (i
= 0; i
< 7; i
++)
3133 pmods
[i
] = mods
+ i
;
3134 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3138 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3139 cupsdLogMessage(CUPSD_LOG_ERROR
,
3140 "LDAP modify for %s failed with status %d: %s",
3141 p
->name
, rc
, ldap_err2string(rc
));
3146 * Printer has never been registered, add the current
3150 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3151 "send_ldap_browse: Adding entry...");
3153 for (i
= 0; i
< 7; i
++)
3155 pmods
[i
] = mods
+ i
;
3156 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3160 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3161 cupsdLogMessage(CUPSD_LOG_ERROR
,
3162 "LDAP add for %s failed with status %d: %s",
3163 p
->name
, rc
, ldap_err2string(rc
));
3166 #endif /* HAVE_OPENLDAP */
3171 * 'send_slp_browse()' - Register the specified printer with SLP.
3175 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3177 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3178 attrs
[8192], /* Printer attributes */
3179 finishings
[1024], /* Finishings to support */
3180 make_model
[IPP_MAX_NAME
* 2],
3181 /* Make and model, quoted */
3182 location
[IPP_MAX_NAME
* 2],
3183 /* Location, quoted */
3184 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3185 *src
, /* Pointer to original string */
3186 *dst
; /* Pointer to destination string */
3187 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3188 SLPError error
; /* SLP error, if any */
3191 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3195 * Make the SLP service URL that conforms to the IANA
3196 * 'printer:' template.
3199 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3201 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3204 * Figure out the finishings string...
3207 if (p
->type
& CUPS_PRINTER_STAPLE
)
3208 strcpy(finishings
, "staple");
3210 finishings
[0] = '\0';
3212 if (p
->type
& CUPS_PRINTER_BIND
)
3215 strlcat(finishings
, ",bind", sizeof(finishings
));
3217 strcpy(finishings
, "bind");
3220 if (p
->type
& CUPS_PRINTER_PUNCH
)
3223 strlcat(finishings
, ",punch", sizeof(finishings
));
3225 strcpy(finishings
, "punch");
3228 if (p
->type
& CUPS_PRINTER_COVER
)
3231 strlcat(finishings
, ",cover", sizeof(finishings
));
3233 strcpy(finishings
, "cover");
3236 if (p
->type
& CUPS_PRINTER_SORT
)
3239 strlcat(finishings
, ",sort", sizeof(finishings
));
3241 strcpy(finishings
, "sort");
3245 strcpy(finishings
, "none");
3248 * Quote any commas in the make and model, location, and info strings...
3251 for (src
= p
->make_model
, dst
= make_model
;
3252 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3254 if (*src
== ',' || *src
== '\\' || *src
== ')')
3263 strcpy(make_model
, "Unknown");
3265 for (src
= p
->location
, dst
= location
;
3266 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3268 if (*src
== ',' || *src
== '\\' || *src
== ')')
3277 strcpy(location
, "Unknown");
3279 for (src
= p
->info
, dst
= info
;
3280 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3282 if (*src
== ',' || *src
== '\\' || *src
== ')')
3291 strcpy(info
, "Unknown");
3294 * Get the authentication value...
3297 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3301 * Make the SLP attribute string list that conforms to
3302 * the IANA 'printer:' template.
3305 snprintf(attrs
, sizeof(attrs
),
3306 "(printer-uri-supported=%s),"
3307 "(uri-authentication-supported=%s>),"
3309 "(uri-security-supported=tls>),"
3311 "(uri-security-supported=none>),"
3312 #endif /* HAVE_SSL */
3313 "(printer-name=%s),"
3314 "(printer-location=%s),"
3315 "(printer-info=%s),"
3316 "(printer-more-info=%s),"
3317 "(printer-make-and-model=%s),"
3318 "(printer-type=%d),"
3319 "(charset-supported=utf-8),"
3320 "(natural-language-configured=%s),"
3321 "(natural-language-supported=de,en,es,fr,it),"
3322 "(color-supported=%s),"
3323 "(finishings-supported=%s),"
3324 "(sides-supported=one-sided%s),"
3325 "(multiple-document-jobs-supported=true)"
3326 "(ipp-versions-supported=1.0,1.1)",
3327 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3328 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3329 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3331 p
->type
& CUPS_PRINTER_DUPLEX
?
3332 ",two-sided-long-edge,two-sided-short-edge" : "");
3334 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3337 * Register the printer with the SLP server...
3340 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3341 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3343 if (error
!= SLP_OK
)
3344 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3350 * 'slp_attr_callback()' - SLP attribute callback
3353 static SLPBoolean
/* O - SLP_TRUE for success */
3355 SLPHandle hslp
, /* I - SLP handle */
3356 const char *attrlist
, /* I - Attribute list */
3357 SLPError errcode
, /* I - Parsing status for this attr */
3358 void *cookie
) /* I - Current printer */
3360 char *tmp
= 0; /* Temporary string */
3361 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3362 /* Current printer */
3365 (void)hslp
; /* anti-compiler-warning-code */
3368 * Bail if there was an error
3371 if (errcode
!= SLP_OK
)
3375 * Parse the attrlist to obtain things needed to build CUPS browse packet
3378 memset(p
, 0, sizeof(cupsd_printer_t
));
3380 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3382 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3384 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3386 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3387 p
->type
= atoi(tmp
);
3389 p
->type
= CUPS_PRINTER_REMOTE
;
3391 cupsdClearString(&tmp
);
3398 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3402 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3404 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3407 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3409 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3412 * Make the SLP service URL that conforms to the IANA
3413 * 'printer:' template.
3416 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3419 * Deregister the printer...
3422 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3428 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3431 static int /* O - 0 on success */
3432 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3433 const char *tag
, /* I - Name of attribute */
3434 char **valbuf
) /* O - Value */
3436 char *ptr1
, /* Pointer into string */
3440 cupsdClearString(valbuf
);
3442 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3444 ptr1
+= strlen(tag
);
3446 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3452 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3453 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3456 * Dequote the value...
3459 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3460 if (*ptr1
== '\\' && ptr1
[1])
3461 _cups_strcpy(ptr1
, ptr1
+ 1);
3472 * 'slp_reg_callback()' - Empty SLPRegReport.
3476 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3477 SLPError errcode
, /* I - Error code, if any */
3478 void *cookie
) /* I - App data */
3489 * 'slp_url_callback()' - SLP service url callback
3492 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3494 SLPHandle hslp
, /* I - SLP handle */
3495 const char *srvurl
, /* I - URL of service */
3496 unsigned short lifetime
, /* I - Life of service */
3497 SLPError errcode
, /* I - Existing error code */
3498 void *cookie
) /* I - Pointer to service list */
3500 slpsrvurl_t
*s
, /* New service entry */
3501 **head
; /* Pointer to head of entry */
3505 * Let the compiler know we won't be using these vars...
3512 * Bail if there was an error
3515 if (errcode
!= SLP_OK
)
3519 * Grab the head of the list...
3522 head
= (slpsrvurl_t
**)cookie
;
3525 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3528 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3532 * Copy the SLP service URL...
3535 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3538 * Link the SLP service URL into the head of the list
3548 #endif /* HAVE_LIBSLP */
3552 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3556 update_cups_browse(void)
3558 int i
; /* Looping var */
3559 int auth
; /* Authorization status */
3560 int len
; /* Length of name string */
3561 int bytes
; /* Number of bytes left */
3562 char packet
[1541], /* Broadcast packet */
3563 *pptr
; /* Pointer into packet */
3564 socklen_t srclen
; /* Length of source address */
3565 http_addr_t srcaddr
; /* Source address */
3566 char srcname
[1024]; /* Source hostname */
3567 unsigned address
[4]; /* Source address */
3568 unsigned type
; /* Printer type */
3569 unsigned state
; /* Printer state */
3570 char uri
[HTTP_MAX_URI
], /* Printer URI */
3571 host
[HTTP_MAX_URI
], /* Host portion of URI */
3572 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3573 info
[IPP_MAX_NAME
], /* Information string */
3574 location
[IPP_MAX_NAME
], /* Location string */
3575 make_model
[IPP_MAX_NAME
];/* Make and model string */
3576 int num_attrs
; /* Number of attributes */
3577 cups_option_t
*attrs
; /* Attributes */
3581 * Read a packet from the browse socket...
3584 srclen
= sizeof(srcaddr
);
3585 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3586 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3589 * "Connection refused" is returned under Linux if the destination port
3590 * or address is unreachable from a previous sendto(); check for the
3591 * error here and ignore it for now...
3594 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3596 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3598 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
3600 cupsdStopBrowsing();
3607 packet
[bytes
] = '\0';
3610 * If we're about to sleep, ignore incoming browse packets.
3617 * Figure out where it came from...
3621 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3623 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3624 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3625 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3626 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3629 #endif /* AF_INET6 */
3634 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3637 if (HostNameLookups
)
3638 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3640 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3642 len
= strlen(srcname
);
3650 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3653 * Access from localhost (127.0.0.1) is always allowed...
3661 * Do authorization checks on the domain/address...
3664 switch (BrowseACL
->order_type
)
3667 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
3670 case AUTH_ALLOW
: /* Order Deny,Allow */
3673 if (cupsdCheckAuth(address
, srcname
, len
,
3674 BrowseACL
->num_deny
, BrowseACL
->deny
))
3677 if (cupsdCheckAuth(address
, srcname
, len
,
3678 BrowseACL
->num_allow
, BrowseACL
->allow
))
3682 case AUTH_DENY
: /* Order Allow,Deny */
3685 if (cupsdCheckAuth(address
, srcname
, len
,
3686 BrowseACL
->num_allow
, BrowseACL
->allow
))
3689 if (cupsdCheckAuth(address
, srcname
, len
,
3690 BrowseACL
->num_deny
, BrowseACL
->deny
))
3699 if (auth
== AUTH_DENY
)
3701 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3702 "update_cups_browse: Refused %d bytes from %s", bytes
,
3707 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3708 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3715 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3717 cupsdLogMessage(CUPSD_LOG_WARN
,
3718 "update_cups_browse: Garbled browse packet - %s", packet
);
3722 strcpy(location
, "Location Unknown");
3723 strcpy(info
, "No Information Available");
3724 make_model
[0] = '\0';
3728 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3731 * Have extended information; can't use sscanf for it because not all
3732 * sscanf's allow empty strings with %[^\"]...
3735 for (i
= 0, pptr
++;
3736 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
3738 location
[i
] = *pptr
;
3746 while (*pptr
&& isspace(*pptr
& 255))
3751 for (i
= 0, pptr
++;
3752 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
3761 while (*pptr
&& isspace(*pptr
& 255))
3766 for (i
= 0, pptr
++;
3767 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
3769 make_model
[i
] = *pptr
;
3774 make_model
[i
] = '\0';
3777 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
3783 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
3784 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
3785 type
, state
, uri
, location
, info
, make_model
));
3788 * Pull the URI apart to see if this is a local or remote printer...
3791 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
3793 cupsFreeOptions(num_attrs
, attrs
);
3801 for (i
= 0; i
< NumRelays
; i
++)
3802 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
3803 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3804 (struct sockaddr
*)&(Relays
[i
].to
),
3805 httpAddrLength(&(Relays
[i
].to
))) <= 0)
3807 cupsdLogMessage(CUPSD_LOG_ERROR
,
3808 "update_cups_browse: sendto failed for relay %d - %s.",
3809 i
+ 1, strerror(errno
));
3810 cupsFreeOptions(num_attrs
, attrs
);
3815 * Process the browse data...
3818 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
3819 (ipp_pstate_t
)state
, location
, info
, make_model
,
3825 * 'update_lpd()' - Update the LPD configuration as needed.
3829 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
3834 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
3837 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
3840 char newfile
[1024]; /* New cups-lpd.N file */
3841 cups_file_t
*ofp
, /* Original file pointer */
3842 *nfp
; /* New file pointer */
3843 char line
[1024]; /* Line from file */
3846 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
3848 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
3850 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
3851 LPDConfigFile
+ 9, strerror(errno
));
3855 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
3857 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
3858 newfile
, strerror(errno
));
3864 * Copy all of the lines from the cups-lpd file...
3867 while (cupsFileGets(ofp
, line
, sizeof(line
)))
3871 cupsFilePrintf(nfp
, "%s\n", line
);
3872 snprintf(line
, sizeof(line
), "\tdisable = %s",
3873 onoff
? "no" : "yes");
3875 else if (strstr(line
, "disable ="))
3878 cupsFilePrintf(nfp
, "%s\n", line
);
3883 rename(newfile
, LPDConfigFile
+ 9);
3885 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
3888 * Enable/disable LPD via the launchctl command...
3891 char *argv
[5], /* Arguments for command */
3892 *envp
[MAX_ENV
]; /* Environment for command */
3893 int pid
; /* Process ID */
3896 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
3897 argv
[0] = (char *)"launchctl";
3898 argv
[1] = (char *)(onoff
? "load" : "unload");
3899 argv
[2] = (char *)"-w";
3900 argv
[3] = LPDConfigFile
+ 10;
3903 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1, &pid
);
3909 * 'update_polling()' - Read status messages from the poll daemons.
3913 update_polling(void)
3915 char *ptr
, /* Pointer to end of line in buffer */
3916 message
[1024]; /* Pointer to message text */
3917 int loglevel
; /* Log level for message */
3920 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
3921 message
, sizeof(message
))) != NULL
)
3922 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
3925 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
3928 * All polling processes have died; stop polling...
3931 cupsdLogMessage(CUPSD_LOG_ERROR
,
3932 "update_polling: all polling processes have exited!");
3939 * 'update_smb()' - Update the SMB configuration as needed.
3943 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
3948 if (!strncmp(SMBConfigFile
, "samba:///", 9))
3951 * Enable/disable SMB via the specified smb.conf config file...
3954 char newfile
[1024]; /* New smb.conf.N file */
3955 cups_file_t
*ofp
, /* Original file pointer */
3956 *nfp
; /* New file pointer */
3957 char line
[1024]; /* Line from file */
3958 int in_printers
; /* In [printers] section? */
3961 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
3963 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
3965 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
3966 SMBConfigFile
+ 8, strerror(errno
));
3970 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
3972 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
3973 newfile
, strerror(errno
));
3979 * Copy all of the lines from the smb.conf file...
3984 while (cupsFileGets(ofp
, line
, sizeof(line
)))
3986 if (in_printers
&& strstr(line
, "printable ="))
3987 snprintf(line
, sizeof(line
), " printable = %s",
3988 onoff
? "yes" : "no");
3990 cupsFilePrintf(nfp
, "%s\n", line
);
3993 in_printers
= !strcmp(line
, "[printers]");
3998 rename(newfile
, SMBConfigFile
+ 8);
4000 else if (!strncmp(SMBConfigFile
, "launchd:///", 11))
4003 * Enable/disable SMB via the launchctl command...
4006 char *argv
[5], /* Arguments for command */
4007 *envp
[MAX_ENV
]; /* Environment for command */
4008 int pid
; /* Process ID */
4011 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4012 argv
[0] = (char *)"launchctl";
4013 argv
[1] = (char *)(onoff
? "load" : "unload");
4014 argv
[2] = (char *)"-w";
4015 argv
[3] = SMBConfigFile
+ 10;
4018 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1, &pid
);
4024 * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".