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-2008 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 local
18 * printer and remove any pending references to
20 * cupsdLoadRemoteCache() - Load the remote printer cache.
21 * cupsdRegisterPrinter() - Start sending broadcast information for a
22 * printer or update the broadcast contents.
23 * cupsdRestartPolling() - Restart polling servers as needed.
24 * cupsdSaveRemoteCache() - Save the remote printer cache.
25 * cupsdSendBrowseList() - Send new browsing information as necessary.
26 * cupsdStartBrowsing() - Start sending and receiving broadcast
28 * cupsdStartPolling() - Start polling servers as needed.
29 * cupsdStopBrowsing() - Stop sending and receiving broadcast
31 * cupsdStopPolling() - Stop polling servers as needed.
32 * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
33 * cupsdUpdateDNSSDName() - Update the computer name we use for
35 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
36 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
37 * dequote() - Remote quotes from a string.
38 * dnssdBuildTxtRecord() - Build a TXT record from printer info.
39 * dnssdComparePrinters() - Compare the registered names of two printers.
40 * dnssdDeregisterPrinter() - Stop sending broadcast information for a
42 * dnssdPackTxtRecord() - Pack an array of key/value pairs into the TXT
44 * dnssdRegisterCallback() - DNSServiceRegister callback.
45 * dnssdRegisterPrinter() - Start sending broadcast information for a
46 * printer or update the broadcast contents.
47 * get_hostconfig() - Get an /etc/hostconfig service setting.
48 * is_local_queue() - Determine whether the URI points at a local
50 * process_browse_data() - Process new browse data.
51 * process_implicit_classes() - Create/update implicit classes as needed.
52 * send_cups_browse() - Send new browsing information using the CUPS
54 * send_ldap_browse() - Send LDAP printer registrations.
55 * send_slp_browse() - Register the specified printer with SLP.
56 * slp_attr_callback() - SLP attribute callback
57 * slp_dereg_printer() - SLPDereg() the specified printer
58 * slp_get_attr() - Get an attribute from an SLP registration.
59 * slp_reg_callback() - Empty SLPRegReport.
60 * slp_url_callback() - SLP service url callback
61 * update_cups_browse() - Update the browse lists using the CUPS
63 * update_lpd() - Update the LPD configuration as needed.
64 * update_polling() - Read status messages from the poll daemons.
65 * update_smb() - Update the SMB configuration as needed.
69 * Include necessary headers...
79 # ifdef HAVE_COREFOUNDATION
80 # include <CoreFoundation/CoreFoundation.h>
81 # endif /* HAVE_COREFOUNDATION */
82 # ifdef HAVE_SYSTEMCONFIGURATION
83 # include <SystemConfiguration/SystemConfiguration.h>
84 # endif /* HAVE_SYSTEMCONFIGURATION */
85 # endif /* __APPLE__ */
86 #endif /* HAVE_DNSSD */
93 static char *dequote(char *d
, const char *s
, int dlen
);
95 static int get_hostconfig(const char *name
);
96 #endif /* __APPLE__ */
97 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
98 char *resource
, int resourcelen
);
99 static void process_browse_data(const char *uri
, const char *host
,
100 const char *resource
, cups_ptype_t type
,
101 ipp_pstate_t state
, const char *location
,
102 const char *info
, const char *make_model
,
103 int num_attrs
, cups_option_t
*attrs
);
104 static void process_implicit_classes(void);
105 static void send_cups_browse(cupsd_printer_t
*p
);
107 static void send_ldap_browse(cupsd_printer_t
*p
);
108 #endif /* HAVE_LDAP */
110 static void send_slp_browse(cupsd_printer_t
*p
);
111 #endif /* HAVE_LIBSLP */
112 static void update_cups_browse(void);
113 static void update_lpd(int onoff
);
114 static void update_polling(void);
115 static void update_smb(int onoff
);
119 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
121 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
122 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
123 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
125 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
126 DNSServiceFlags flags
,
127 DNSServiceErrorType errorCode
,
128 const char *name
, const char *regtype
,
129 const char *domain
, void *context
);
130 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
131 #endif /* HAVE_DNSSD */
134 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
136 "printerDescription",
138 "printerMakeAndModel",
143 #endif /* HAVE_OPENLDAP */
151 * SLP service name for CUPS...
154 # define SLP_CUPS_SRVTYPE "service:printer"
155 # define SLP_CUPS_SRVLEN 15
159 * Printer service URL structure
162 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
164 struct _slpsrvurl_s
*next
; /* Next URL in list */
165 char url
[HTTP_MAX_URI
];
174 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
175 SLPError errcode
, void *cookie
);
176 static void slp_dereg_printer(cupsd_printer_t
*p
);
177 static int slp_get_attr(const char *attrlist
, const char *tag
,
179 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
181 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
182 unsigned short lifetime
,
183 SLPError errcode
, void *cookie
);
184 #endif /* HAVE_LIBSLP */
188 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
189 * local printer and remove any pending
190 * references to remote printers.
194 cupsdDeregisterPrinter(
195 cupsd_printer_t
*p
, /* I - Printer to register */
196 int removeit
) /* I - Printer being permanently removed */
199 * Only deregister if browsing is enabled and it's a local printer...
202 if (!Browsing
|| !p
->shared
||
203 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
207 * Announce the deletion...
210 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
212 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
214 p
->type
|= CUPS_PRINTER_DELETE
;
222 if (BrowseLocalProtocols
& BROWSE_SLP
)
223 slp_dereg_printer(p
);
224 #endif /* HAVE_LIBSLP */
227 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
228 dnssdDeregisterPrinter(p
);
229 #endif /* HAVE_DNSSD */
234 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
238 cupsdLoadRemoteCache(void)
240 cups_file_t
*fp
; /* remote.cache file */
241 int linenum
; /* Current line number */
242 char line
[1024], /* Line from file */
243 *value
, /* Pointer to value */
244 *valueptr
, /* Pointer into value */
245 scheme
[32], /* Scheme portion of URI */
246 username
[64], /* Username portion of URI */
248 /* Hostname portion of URI */
249 resource
[HTTP_MAX_URI
];
250 /* Resource portion of URI */
251 int port
; /* Port number */
252 cupsd_printer_t
*p
; /* Current printer */
253 time_t now
; /* Current time */
257 * Don't load the cache if the remote protocols are disabled...
262 cupsdLogMessage(CUPSD_LOG_DEBUG
,
263 "cupsdLoadRemoteCache: Not loading remote cache.");
268 * Open the remote.cache file...
271 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
272 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
276 * Read printer configurations until we hit EOF...
283 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
286 * Decode the directive...
289 if (!strcasecmp(line
, "<Printer") ||
290 !strcasecmp(line
, "<DefaultPrinter"))
293 * <Printer name> or <DefaultPrinter name>
296 if (p
== NULL
&& value
)
299 * Add the printer and a base file type...
302 cupsdLogMessage(CUPSD_LOG_DEBUG
,
303 "cupsdLoadRemoteCache: Loading printer %s...", value
);
305 if ((p
= cupsdFindDest(value
)) != NULL
)
307 if (p
->type
& CUPS_PRINTER_CLASS
)
309 cupsdLogMessage(CUPSD_LOG_WARN
,
310 "Cached remote printer \"%s\" conflicts with "
318 p
= cupsdAddPrinter(value
);
321 p
->state
= IPP_PRINTER_IDLE
;
322 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
323 p
->browse_time
= now
;
324 p
->browse_expire
= now
+ BrowseTimeout
;
327 * Set the default printer as needed...
330 if (!strcasecmp(line
, "<DefaultPrinter"))
335 cupsdLogMessage(CUPSD_LOG_ERROR
,
336 "Syntax error on line %d of remote.cache.", linenum
);
340 else if (!strcasecmp(line
, "<Class") ||
341 !strcasecmp(line
, "<DefaultClass"))
344 * <Class name> or <DefaultClass name>
347 if (p
== NULL
&& value
)
350 * Add the printer and a base file type...
353 cupsdLogMessage(CUPSD_LOG_DEBUG
,
354 "cupsdLoadRemoteCache: Loading class %s...", value
);
356 if ((p
= cupsdFindDest(value
)) != NULL
)
357 p
->type
= CUPS_PRINTER_CLASS
;
359 p
= cupsdAddClass(value
);
362 p
->state
= IPP_PRINTER_IDLE
;
363 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
364 p
->browse_time
= now
;
365 p
->browse_expire
= now
+ BrowseTimeout
;
368 * Set the default printer as needed...
371 if (!strcasecmp(line
, "<DefaultClass"))
376 cupsdLogMessage(CUPSD_LOG_ERROR
,
377 "Syntax error on line %d of remote.cache.", linenum
);
381 else if (!strcasecmp(line
, "</Printer>") ||
382 !strcasecmp(line
, "</Class>"))
387 * Close out the current printer...
390 cupsdSetPrinterAttrs(p
);
396 cupsdLogMessage(CUPSD_LOG_ERROR
,
397 "Syntax error on line %d of remote.cache.", linenum
);
403 cupsdLogMessage(CUPSD_LOG_ERROR
,
404 "Syntax error on line %d of remote.cache.", linenum
);
407 else if (!strcasecmp(line
, "Info"))
410 cupsdSetString(&p
->info
, value
);
412 else if (!strcasecmp(line
, "MakeModel"))
415 cupsdSetString(&p
->make_model
, value
);
417 else if (!strcasecmp(line
, "Location"))
420 cupsdSetString(&p
->location
, value
);
422 else if (!strcasecmp(line
, "DeviceURI"))
426 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
427 username
, sizeof(username
), host
, sizeof(host
), &port
,
428 resource
, sizeof(resource
));
430 cupsdSetString(&p
->hostname
, host
);
431 cupsdSetString(&p
->uri
, value
);
432 cupsdSetString(&p
->device_uri
, value
);
436 cupsdLogMessage(CUPSD_LOG_ERROR
,
437 "Syntax error on line %d of remote.cache.", linenum
);
441 else if (!strcasecmp(line
, "Option") && value
)
447 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
450 cupsdLogMessage(CUPSD_LOG_ERROR
,
451 "Syntax error on line %d of remote.cache.", linenum
);
454 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
456 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
460 else if (!strcasecmp(line
, "State"))
463 * Set the initial queue state...
466 if (value
&& !strcasecmp(value
, "idle"))
467 p
->state
= IPP_PRINTER_IDLE
;
468 else if (value
&& !strcasecmp(value
, "stopped"))
469 p
->state
= IPP_PRINTER_STOPPED
;
472 cupsdLogMessage(CUPSD_LOG_ERROR
,
473 "Syntax error on line %d of remote.cache.", linenum
);
477 else if (!strcasecmp(line
, "StateMessage"))
480 * Set the initial queue state message...
484 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
486 else if (!strcasecmp(line
, "Accepting"))
489 * Set the initial accepting state...
493 (!strcasecmp(value
, "yes") ||
494 !strcasecmp(value
, "on") ||
495 !strcasecmp(value
, "true")))
498 (!strcasecmp(value
, "no") ||
499 !strcasecmp(value
, "off") ||
500 !strcasecmp(value
, "false")))
504 cupsdLogMessage(CUPSD_LOG_ERROR
,
505 "Syntax error on line %d of remote.cache.", linenum
);
509 else if (!strcasecmp(line
, "Type"))
512 p
->type
= atoi(value
);
515 cupsdLogMessage(CUPSD_LOG_ERROR
,
516 "Syntax error on line %d of remote.cache.", linenum
);
520 else if (!strcasecmp(line
, "BrowseTime"))
524 time_t t
= atoi(value
);
526 if (t
> p
->browse_expire
)
527 p
->browse_expire
= t
;
531 cupsdLogMessage(CUPSD_LOG_ERROR
,
532 "Syntax error on line %d of remote.cache.", linenum
);
536 else if (!strcasecmp(line
, "JobSheets"))
539 * Set the initial job sheets...
544 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
549 cupsdSetString(&p
->job_sheets
[0], value
);
551 while (isspace(*valueptr
& 255))
556 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
561 cupsdSetString(&p
->job_sheets
[1], value
);
566 cupsdLogMessage(CUPSD_LOG_ERROR
,
567 "Syntax error on line %d of remote.cache.", linenum
);
571 else if (!strcasecmp(line
, "AllowUser"))
576 cupsdAddPrinterUser(p
, value
);
580 cupsdLogMessage(CUPSD_LOG_ERROR
,
581 "Syntax error on line %d of remote.cache.", linenum
);
585 else if (!strcasecmp(line
, "DenyUser"))
590 cupsdAddPrinterUser(p
, value
);
594 cupsdLogMessage(CUPSD_LOG_ERROR
,
595 "Syntax error on line %d of remote.cache.", linenum
);
602 * Something else we don't understand...
605 cupsdLogMessage(CUPSD_LOG_ERROR
,
606 "Unknown configuration directive %s on line %d of remote.cache.",
614 * Do auto-classing if needed...
617 process_implicit_classes();
622 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
623 * printer or update the broadcast contents.
627 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
629 if (!Browsing
|| !BrowseLocalProtocols
||
630 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
634 /* if (BrowseLocalProtocols & BROWSE_SLP)
635 slpRegisterPrinter(p); */
636 #endif /* HAVE_LIBSLP */
639 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
640 dnssdRegisterPrinter(p
);
641 #endif /* HAVE_DNSSD */
646 * 'cupsdRestartPolling()' - Restart polling servers as needed.
650 cupsdRestartPolling(void)
652 int i
; /* Looping var */
653 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
656 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
658 kill(pollp
->pid
, SIGHUP
);
663 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
667 cupsdSaveRemoteCache(void)
669 int i
; /* Looping var */
670 cups_file_t
*fp
; /* printers.conf file */
671 char temp
[1024]; /* Temporary string */
672 cupsd_printer_t
*printer
; /* Current printer class */
673 time_t curtime
; /* Current time */
674 struct tm
*curdate
; /* Current date */
675 cups_option_t
*option
; /* Current option */
679 * Create the remote.cache file...
682 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
684 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
686 cupsdLogMessage(CUPSD_LOG_ERROR
,
687 "Unable to save remote.cache - %s", strerror(errno
));
691 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
694 * Restrict access to the file...
697 fchown(cupsFileNumber(fp
), getuid(), Group
);
698 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
701 * Write a small header to the file...
704 curtime
= time(NULL
);
705 curdate
= localtime(&curtime
);
706 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
708 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
709 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
712 * Write each local printer known to the system...
715 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
717 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
720 * Skip local destinations...
723 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
727 * Write printers as needed...
730 if (printer
== DefaultPrinter
)
731 cupsFilePuts(fp
, "<Default");
733 cupsFilePutChar(fp
, '<');
735 if (printer
->type
& CUPS_PRINTER_CLASS
)
736 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
738 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
740 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
742 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
745 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
747 if (printer
->make_model
)
748 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
750 if (printer
->location
)
751 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
753 if (printer
->device_uri
)
754 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
756 if (printer
->state
== IPP_PRINTER_STOPPED
)
758 cupsFilePuts(fp
, "State Stopped\n");
759 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
762 cupsFilePuts(fp
, "State Idle\n");
764 if (printer
->accepting
)
765 cupsFilePuts(fp
, "Accepting Yes\n");
767 cupsFilePuts(fp
, "Accepting No\n");
769 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
770 printer
->job_sheets
[1]);
772 for (i
= 0; i
< printer
->num_users
; i
++)
773 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
776 for (i
= printer
->num_options
, option
= printer
->options
;
779 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
781 if (printer
->type
& CUPS_PRINTER_CLASS
)
782 cupsFilePuts(fp
, "</Class>\n");
784 cupsFilePuts(fp
, "</Printer>\n");
792 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
796 cupsdSendBrowseList(void)
798 int count
; /* Number of dests to update */
799 cupsd_printer_t
*p
; /* Current printer */
800 time_t ut
, /* Minimum update time */
801 to
; /* Timeout time */
804 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
808 * Compute the update and timeout times...
812 ut
= to
- BrowseInterval
;
815 * Figure out how many printers need an update...
818 if (BrowseInterval
> 0)
820 int max_count
; /* Maximum number to update */
824 * Throttle the number of printers we'll be updating this time
825 * around based on the number of queues that need updating and
826 * the maximum number of queues to update each second...
829 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
831 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
832 count
< max_count
&& p
!= NULL
;
833 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
834 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
835 p
->shared
&& p
->browse_time
< ut
)
839 * Loop through all of the printers and send local updates as needed...
843 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
845 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
849 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
852 * Check for wraparound...
856 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
860 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
863 else if (p
->browse_time
< ut
)
866 * Need to send an update...
871 p
->browse_time
= time(NULL
);
873 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
877 if (BrowseLocalProtocols
& BROWSE_SLP
)
879 #endif /* HAVE_LIBSLP */
882 if (BrowseLocalProtocols
& BROWSE_LDAP
)
884 #endif /* HAVE_LDAP */
889 * Save where we left off so that all printers get updated...
896 * Loop through all of the printers and send local updates as needed...
899 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
901 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
904 * If this is a remote queue, see if it needs to be timed out...
907 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
909 if (p
->browse_expire
< to
)
911 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
912 "%s \'%s\' deleted by directory services (timeout).",
913 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
916 cupsdLogMessage(CUPSD_LOG_DEBUG
,
917 "Remote destination \"%s\" has timed out; "
921 cupsArraySave(Printers
);
922 cupsdDeletePrinter(p
, 1);
923 cupsArrayRestore(Printers
);
924 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
932 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
936 cupsdStartBrowsing(void)
938 int val
; /* Socket option value */
939 struct sockaddr_in addr
; /* Broadcast address */
940 cupsd_printer_t
*p
; /* Current printer */
945 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
948 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
950 if (BrowseSocket
< 0)
953 * Create the broadcast socket...
956 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
958 cupsdLogMessage(CUPSD_LOG_ERROR
,
959 "Unable to create broadcast socket - %s.",
961 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
962 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
967 * Bind the socket to browse port...
970 memset(&addr
, 0, sizeof(addr
));
971 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
972 addr
.sin_family
= AF_INET
;
973 addr
.sin_port
= htons(BrowsePort
);
975 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
977 cupsdLogMessage(CUPSD_LOG_ERROR
,
978 "Unable to bind broadcast socket - %s.",
982 closesocket(BrowseSocket
);
988 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
989 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
995 * Set the "broadcast" flag...
999 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1001 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1005 closesocket(BrowseSocket
);
1007 close(BrowseSocket
);
1011 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1012 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1017 * Close the socket on exec...
1020 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1023 * Finally, add the socket to the input selection set as needed...
1026 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1029 * We only listen if we want remote printers...
1032 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1040 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1042 DNSServiceErrorType error
; /* Error from service creation */
1043 cupsd_listener_t
*lis
; /* Current listening socket */
1047 * First create a "master" connection for all registrations...
1050 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1051 != kDNSServiceErr_NoError
)
1052 cupsdLogMessage(CUPSD_LOG_ERROR
,
1053 "Unable to create master DNS-SD reference: %d", error
);
1057 * Add the master connection to the select list...
1060 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1061 (cupsd_selfunc_t
)cupsdUpdateDNSSDBrowse
, NULL
, NULL
);
1064 * Then get the port we use for registrations. If we are not listening
1065 * on any non-local ports, there is no sense sharing local printers via
1071 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1073 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1075 if (httpAddrLocalhost(&(lis
->address
)))
1078 if (lis
->address
.addr
.sa_family
== AF_INET
)
1080 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1083 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1085 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1091 * Create an array to track the printers we share...
1094 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1095 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1099 * Set the computer name and register the web interface...
1102 cupsdUpdateDNSSDName();
1105 #endif /* HAVE_DNSSD */
1108 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1111 * Open SLP handle...
1114 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1116 cupsdLogMessage(CUPSD_LOG_ERROR
,
1117 "Unable to open an SLP handle; disabling SLP browsing!");
1118 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1119 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1122 BrowseSLPRefresh
= 0;
1125 BrowseSLPHandle
= NULL
;
1126 #endif /* HAVE_LIBSLP */
1128 #ifdef HAVE_OPENLDAP
1129 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1133 cupsdLogMessage(CUPSD_LOG_ERROR
,
1134 "Need to set BrowseLDAPDN to use LDAP browsing!");
1135 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1136 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1141 * Open LDAP handle...
1144 int rc
; /* LDAP API status */
1145 int version
= 3; /* LDAP version */
1146 struct berval bv
= {0, ""}; /* SASL bind value */
1150 * Set the certificate file to use for encrypted LDAP sessions...
1153 if (BrowseLDAPCACertFile
)
1155 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1156 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1157 BrowseLDAPCACertFile
);
1159 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1160 (void *)BrowseLDAPCACertFile
))
1162 cupsdLogMessage(CUPSD_LOG_ERROR
,
1163 "Unable to set CA certificate file for LDAP "
1164 "connections: %d - %s", rc
, ldap_err2string(rc
));
1168 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1171 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1172 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1174 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1176 if (rc
!= LDAP_SUCCESS
)
1178 cupsdLogMessage(CUPSD_LOG_ERROR
,
1179 "Unable to initialize LDAP; disabling LDAP browsing!");
1180 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1181 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1183 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1184 (const void *)&version
) != LDAP_SUCCESS
)
1186 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1187 BrowseLDAPHandle
= NULL
;
1188 cupsdLogMessage(CUPSD_LOG_ERROR
,
1189 "Unable to set LDAP protocol version; "
1190 "disabling LDAP browsing!");
1191 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1192 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1196 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1197 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1200 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1201 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1203 if (rc
!= LDAP_SUCCESS
)
1205 cupsdLogMessage(CUPSD_LOG_ERROR
,
1206 "Unable to bind to LDAP server; "
1207 "disabling LDAP browsing!");
1208 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1209 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1210 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1215 BrowseLDAPRefresh
= 0;
1217 #endif /* HAVE_OPENLDAP */
1220 * Enable LPD and SMB printer sharing as needed through external programs...
1223 if (BrowseLocalProtocols
& BROWSE_LPD
)
1226 if (BrowseLocalProtocols
& BROWSE_SMB
)
1230 * Register the individual printers
1233 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1235 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1236 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1237 cupsdRegisterPrinter(p
);
1242 * 'cupsdStartPolling()' - Start polling servers as needed.
1246 cupsdStartPolling(void)
1248 int i
; /* Looping var */
1249 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1250 char polld
[1024]; /* Poll daemon path */
1251 char sport
[255]; /* Server port */
1252 char bport
[255]; /* Browser port */
1253 char interval
[255]; /* Poll interval */
1254 int statusfds
[2]; /* Status pipe */
1255 char *argv
[6]; /* Arguments */
1256 char *envp
[100]; /* Environment */
1260 * Don't do anything if we aren't polling...
1263 if (NumPolled
== 0 || BrowseSocket
< 0)
1266 PollStatusBuffer
= NULL
;
1271 * Setup string arguments for polld, port and interval options.
1274 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1276 sprintf(bport
, "%d", BrowsePort
);
1279 sprintf(interval
, "%d", BrowseInterval
);
1281 strcpy(interval
, "30");
1283 argv
[0] = "cups-polld";
1289 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1292 * Create a pipe that receives the status messages from each
1296 if (cupsdOpenPipe(statusfds
))
1298 cupsdLogMessage(CUPSD_LOG_ERROR
,
1299 "Unable to create polling status pipes - %s.",
1302 PollStatusBuffer
= NULL
;
1306 PollPipe
= statusfds
[0];
1307 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1310 * Run each polling daemon, redirecting stderr to the polling pipe...
1313 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1315 sprintf(sport
, "%d", pollp
->port
);
1317 argv
[1] = pollp
->hostname
;
1319 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1320 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1322 cupsdLogMessage(CUPSD_LOG_ERROR
,
1323 "cupsdStartPolling: Unable to fork polling daemon - %s",
1329 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1330 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1331 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1334 close(statusfds
[1]);
1337 * Finally, add the pipe to the input selection set...
1340 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1345 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1349 cupsdStopBrowsing(void)
1351 cupsd_printer_t
*p
; /* Current printer */
1354 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1358 * De-register the individual printers
1361 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1363 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1364 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1365 cupsdDeregisterPrinter(p
, 1);
1368 * Shut down browsing sockets...
1371 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1375 * Close the socket and remove it from the input selection set.
1379 closesocket(BrowseSocket
);
1381 close(BrowseSocket
);
1384 cupsdRemoveSelect(BrowseSocket
);
1389 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1393 DNSServiceRefDeallocate(WebIFRef
);
1399 DNSServiceRefDeallocate(RemoteRef
);
1403 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1405 DNSServiceRefDeallocate(DNSSDRef
);
1408 cupsArrayDelete(DNSSDPrinters
);
1409 DNSSDPrinters
= NULL
;
1413 #endif /* HAVE_DNSSD */
1416 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1420 * Close SLP handle...
1423 SLPClose(BrowseSLPHandle
);
1424 BrowseSLPHandle
= NULL
;
1426 #endif /* HAVE_LIBSLP */
1428 #ifdef HAVE_OPENLDAP
1429 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1432 ldap_unbind(BrowseLDAPHandle
);
1433 BrowseLDAPHandle
= NULL
;
1435 #endif /* HAVE_OPENLDAP */
1438 * Disable LPD and SMB printer sharing as needed through external programs...
1441 if (BrowseLocalProtocols
& BROWSE_LPD
)
1444 if (BrowseLocalProtocols
& BROWSE_SMB
)
1450 * 'cupsdStopPolling()' - Stop polling servers as needed.
1454 cupsdStopPolling(void)
1456 int i
; /* Looping var */
1457 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1462 cupsdStatBufDelete(PollStatusBuffer
);
1465 cupsdRemoveSelect(PollPipe
);
1468 PollStatusBuffer
= NULL
;
1471 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1473 cupsdEndProcess(pollp
->pid
, 0);
1479 * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
1483 cupsdUpdateDNSSDBrowse(void)
1485 DNSServiceErrorType sdErr
; /* Service discovery error */
1488 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
1489 cupsdLogMessage(CUPSD_LOG_ERROR
,
1490 "DNS Service Discovery registration error %d!",
1496 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1500 cupsdUpdateDNSSDName(void)
1502 DNSServiceErrorType error
; /* Error from service creation */
1503 char webif
[1024]; /* Web interface share name */
1504 #ifdef HAVE_COREFOUNDATION_H
1505 CFStringRef nameRef
; /* Computer name CFString */
1506 char nameBuffer
[1024]; /* C-string buffer */
1507 CFStringEncoding nameEncoding
; /* Computer name encoding */
1508 #endif /* HAVE_COREFOUNDATION_H */
1512 * Only share the web interface and printers when non-local listening is
1520 * Get the computer name as a c-string...
1523 #ifdef HAVE_COREFOUNDATION_H
1524 cupsdClearString(&DNSSDName
);
1526 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1527 &nameEncoding
)) != NULL
)
1529 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1530 kCFStringEncodingUTF8
))
1531 cupsdSetString(&DNSSDName
, nameBuffer
);
1537 cupsdSetString(&DNSSDName
, ServerName
);
1538 #endif /* HAVE_COREFOUNDATION_H */
1541 * Then (re)register the web interface...
1545 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1547 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1550 DNSServiceRefDeallocate(WebIFRef
);
1552 WebIFRef
= DNSSDRef
;
1553 if ((error
= DNSServiceRegister(&WebIFRef
,
1554 kDNSServiceFlagsShareConnection
,
1555 0, webif
, "_http._tcp", NULL
,
1556 NULL
, htons(DNSSDPort
), 7,
1557 "\006path=/", dnssdRegisterCallback
,
1558 NULL
)) != kDNSServiceErr_NoError
)
1559 cupsdLogMessage(CUPSD_LOG_ERROR
,
1560 "DNS-SD web interface registration failed: %d", error
);
1562 #endif /* HAVE_DNSSD */
1565 #ifdef HAVE_OPENLDAP
1567 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1571 cupsdUpdateLDAPBrowse(void)
1573 char uri
[HTTP_MAX_URI
], /* Printer URI */
1574 host
[HTTP_MAX_URI
], /* Hostname */
1575 resource
[HTTP_MAX_URI
], /* Resource path */
1576 location
[1024], /* Printer location */
1577 info
[1024], /* Printer information */
1578 make_model
[1024], /* Printer make and model */
1579 **value
; /* Holds the returned data from LDAP */
1580 int type
; /* Printer type */
1581 int rc
; /* LDAP status */
1582 int limit
; /* Size limit */
1583 LDAPMessage
*res
, /* LDAP search results */
1584 *e
; /* Current entry from search */
1588 * Search for printers...
1591 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1593 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1595 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1596 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1597 if (rc
!= LDAP_SUCCESS
)
1599 cupsdLogMessage(CUPSD_LOG_ERROR
,
1600 "LDAP search returned error %d: %s", rc
,
1601 ldap_err2string(rc
));
1605 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1606 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1611 * Loop through the available printers...
1614 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1616 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1619 * Get the required values from this entry...
1622 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1623 "printerDescription")) == NULL
)
1626 strlcpy(info
, *value
, sizeof(info
));
1627 ldap_value_free(value
);
1629 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1630 "printerLocation")) == NULL
)
1633 strlcpy(location
, *value
, sizeof(location
));
1634 ldap_value_free(value
);
1636 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1637 "printerMakeAndModel")) == NULL
)
1640 strlcpy(make_model
, *value
, sizeof(make_model
));
1641 ldap_value_free(value
);
1643 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1644 "printerType")) == NULL
)
1647 type
= atoi(*value
);
1648 ldap_value_free(value
);
1650 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1651 "printerURI")) == NULL
)
1654 strlcpy(uri
, *value
, sizeof(uri
));
1655 ldap_value_free(value
);
1658 * Process the entry as browse data...
1661 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1662 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1663 location
, info
, make_model
, 0, NULL
);
1667 #endif /* HAVE_OPENLDAP */
1672 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1676 cupsdUpdateSLPBrowse(void)
1678 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1679 *next
; /* Next service in list */
1680 cupsd_printer_t p
; /* Printer information */
1681 const char *uri
; /* Pointer to printer URI */
1682 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1683 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1687 * Reset the refresh time...
1690 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1693 * Poll for remote printers using SLP...
1698 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1699 slp_url_callback
, &s
);
1702 * Loop through the list of available printers...
1708 * Save the "next" pointer...
1714 * Load a cupsd_printer_t structure with the SLP service attributes...
1717 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1720 * Process this printer entry...
1723 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1725 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1728 * Pull the URI apart to see if this is a local or remote printer...
1731 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1732 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1733 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1737 * Free this listing...
1740 cupsdClearString(&p
.info
);
1741 cupsdClearString(&p
.location
);
1742 cupsdClearString(&p
.make_model
);
1747 #endif /* HAVE_LIBSLP */
1751 * 'dequote()' - Remote quotes from a string.
1754 static char * /* O - Dequoted string */
1755 dequote(char *d
, /* I - Destination string */
1756 const char *s
, /* I - Source string */
1757 int dlen
) /* I - Destination length */
1759 char *dptr
; /* Pointer into destination */
1764 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1782 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
1785 static char * /* O - TXT record */
1786 dnssdBuildTxtRecord(
1787 int *txt_len
, /* O - TXT record length */
1788 cupsd_printer_t
*p
, /* I - Printer information */
1789 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
1791 int i
, j
; /* Looping vars */
1792 char type_str
[32], /* Type to string buffer */
1793 state_str
[32], /* State to string buffer */
1794 rp_str
[1024], /* Queue name string buffer */
1795 air_str
[1024], /* auth-info-required string buffer */
1796 *keyvalue
[32][2]; /* Table of key/value pairs */
1800 * Load up the key value pairs...
1805 keyvalue
[i
][0] = "txtvers";
1806 keyvalue
[i
++][1] = "1";
1808 keyvalue
[i
][0] = "qtotal";
1809 keyvalue
[i
++][1] = "1";
1811 keyvalue
[i
][0] = "rp";
1812 keyvalue
[i
++][1] = rp_str
;
1814 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
1816 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
1817 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
1819 keyvalue
[i
][0] = "ty";
1820 keyvalue
[i
++][1] = p
->make_model
;
1822 if (p
->location
&& *p
->location
!= '\0')
1824 keyvalue
[i
][0] = "note";
1825 keyvalue
[i
++][1] = p
->location
;
1828 keyvalue
[i
][0] = "priority";
1829 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
1831 keyvalue
[i
][0] = "product";
1832 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
1834 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
1835 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
1837 keyvalue
[i
][0] = "printer-state";
1838 keyvalue
[i
++][1] = state_str
;
1840 keyvalue
[i
][0] = "printer-type";
1841 keyvalue
[i
++][1] = type_str
;
1843 keyvalue
[i
][0] = "Transparent";
1844 keyvalue
[i
++][1] = "T";
1846 keyvalue
[i
][0] = "Binary";
1847 keyvalue
[i
++][1] = "T";
1849 if ((p
->type
& CUPS_PRINTER_FAX
))
1851 keyvalue
[i
][0] = "Fax";
1852 keyvalue
[i
++][1] = "T";
1855 if ((p
->type
& CUPS_PRINTER_COLOR
))
1857 keyvalue
[i
][0] = "Color";
1858 keyvalue
[i
++][1] = "T";
1861 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
1863 keyvalue
[i
][0] = "Duplex";
1864 keyvalue
[i
++][1] = "T";
1867 if ((p
->type
& CUPS_PRINTER_STAPLE
))
1869 keyvalue
[i
][0] = "Staple";
1870 keyvalue
[i
++][1] = "T";
1873 if ((p
->type
& CUPS_PRINTER_COPIES
))
1875 keyvalue
[i
][0] = "Copies";
1876 keyvalue
[i
++][1] = "T";
1879 if ((p
->type
& CUPS_PRINTER_COLLATE
))
1881 keyvalue
[i
][0] = "Collate";
1882 keyvalue
[i
++][1] = "T";
1885 if ((p
->type
& CUPS_PRINTER_PUNCH
))
1887 keyvalue
[i
][0] = "Punch";
1888 keyvalue
[i
++][1] = "T";
1891 if ((p
->type
& CUPS_PRINTER_BIND
))
1893 keyvalue
[i
][0] = "Bind";
1894 keyvalue
[i
++][1] = "T";
1897 if ((p
->type
& CUPS_PRINTER_SORT
))
1899 keyvalue
[i
][0] = "Sort";
1900 keyvalue
[i
++][1] = "T";
1903 keyvalue
[i
][0] = "pdl";
1904 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
1906 if (p
->num_auth_info_required
)
1908 char *air
= air_str
; /* Pointer into string */
1911 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
1913 if (air
>= (air_str
+ sizeof(air_str
) - 2))
1919 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
1923 keyvalue
[i
][0] = "air";
1924 keyvalue
[i
++][1] = air
;
1928 * Then pack them into a proper txt record...
1931 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
1936 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
1939 static int /* O - Result of comparison */
1940 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
1941 cupsd_printer_t
*b
)/* I - Second printer */
1943 return (strcasecmp(a
->reg_name
, b
->reg_name
));
1948 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
1953 dnssdDeregisterPrinter(
1954 cupsd_printer_t
*p
) /* I - Printer */
1956 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
1959 * Closing the socket deregisters the service
1964 DNSServiceRefDeallocate(p
->ipp_ref
);
1968 cupsArrayRemove(DNSSDPrinters
, p
);
1969 cupsdClearString(&p
->reg_name
);
1974 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
1984 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
1985 * TXT record format.
1988 static char * /* O - TXT record */
1989 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
1990 char *keyvalue
[][2], /* I - Table of key value pairs */
1991 int count
) /* I - Items in table */
1993 int i
; /* Looping var */
1994 int length
; /* Length of TXT record */
1995 int length2
; /* Length of value */
1996 char *txtRecord
; /* TXT record buffer */
1997 char *cursor
; /* Looping pointer */
2001 * Calculate the buffer size
2004 for (length
= i
= 0; i
< count
; i
++)
2005 length
+= 1 + strlen(keyvalue
[i
][0]) +
2006 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2009 * Allocate and fill it
2012 txtRecord
= malloc(length
);
2017 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2020 * Drop in the p-string style length byte followed by the data
2023 length
= strlen(keyvalue
[i
][0]);
2024 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2026 *cursor
++ = (unsigned char)(length
+ length2
);
2028 memcpy(cursor
, keyvalue
[i
][0], length
);
2035 memcpy(cursor
, keyvalue
[i
][1], length2
);
2046 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2050 dnssdRegisterCallback(
2051 DNSServiceRef sdRef
, /* I - DNS Service reference */
2052 DNSServiceFlags flags
, /* I - Reserved for future use */
2053 DNSServiceErrorType errorCode
, /* I - Error code */
2054 const char *name
, /* I - Service name */
2055 const char *regtype
, /* I - Service type */
2056 const char *domain
, /* I - Domain. ".local" for now */
2057 void *context
) /* I - User-defined context */
2059 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2060 /* Current printer */
2063 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2064 name
, regtype
, p
? p
->name
: "Web Interface");
2068 cupsdLogMessage(CUPSD_LOG_ERROR
,
2069 "DNSServiceRegister failed with error %d", (int)errorCode
);
2072 else if (p
&& strcasecmp(name
, p
->reg_name
))
2074 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2077 cupsArrayRemove(DNSSDPrinters
, p
);
2078 cupsdSetString(&p
->reg_name
, name
);
2079 cupsArrayAdd(DNSSDPrinters
, p
);
2081 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2087 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2088 * or update the broadcast contents.
2092 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2094 DNSServiceErrorType se
; /* dnssd errors */
2095 char *ipp_txt
, /* IPP TXT record buffer */
2096 *printer_txt
, /* LPD TXT record buffer */
2097 name
[1024], /* Service name */
2098 *nameptr
; /* Pointer into name */
2099 int ipp_len
, /* IPP TXT record length */
2100 printer_len
; /* LPD TXT record length */
2101 char resource
[1024]; /* Resource path for printer */
2102 const char *regtype
; /* Registration type */
2103 const char *domain
; /* Registration domain */
2104 cupsd_location_t
*location
, /* Printer location */
2105 *policy
; /* Operation policy for Print-Job */
2106 unsigned address
[4]; /* INADDR_ANY address */
2109 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2110 !p
->ipp_ref
? "new" : "update");
2113 * If per-printer sharing was just disabled make sure we're not
2114 * registered before returning.
2119 dnssdDeregisterPrinter(p
);
2124 * The registered name takes the form of "<printer-info> @ <computer name>"...
2127 if (p
->info
&& strlen(p
->info
) > 0)
2130 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2132 strlcpy(name
, p
->info
, sizeof(name
));
2135 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2137 strlcpy(name
, p
->name
, sizeof(name
));
2140 * If an existing printer was renamed, unregister it and start over...
2143 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2144 dnssdDeregisterPrinter(p
);
2148 cupsdSetString(&p
->reg_name
, name
);
2149 cupsArrayAdd(DNSSDPrinters
, p
);
2153 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2154 * let dnssd decide on the domain, otherwise restrict it to ".local".
2157 if (p
->type
& CUPS_PRINTER_CLASS
)
2158 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2160 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2162 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2163 location
= cupsdFindBest(resource
, HTTP_POST
);
2164 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2166 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2167 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2173 * Register IPP and (optionally) LPD...
2176 ipp_len
= 0; /* anti-compiler-warning-code */
2177 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2182 * Initial registration. Use the _fax subtype for fax queues...
2185 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2188 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2189 "Registering DNS-SD printer %s with name \"%s\", "
2190 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2191 domain
? domain
: "(null)");
2194 * Register the queue, dropping characters as needed until we succeed...
2197 nameptr
= name
+ strlen(name
);
2201 p
->ipp_ref
= DNSSDRef
;
2202 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2203 0, name
, regtype
, domain
, NULL
,
2204 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2205 dnssdRegisterCallback
,
2206 p
)) == kDNSServiceErr_BadParam
)
2209 * Name is too long, drop trailing characters, taking into account
2215 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2222 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2224 if (se
== kDNSServiceErr_NoError
)
2226 p
->ipp_txt
= ipp_txt
;
2227 p
->ipp_len
= ipp_len
;
2231 cupsdLogMessage(CUPSD_LOG_WARN
,
2232 "DNS-SD IPP registration of \"%s\" failed: %d",
2235 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2238 * Update the existing registration...
2241 /* A TTL of 0 means use record's original value (Radar 3176248) */
2242 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2247 p
->ipp_txt
= ipp_txt
;
2248 p
->ipp_len
= ipp_len
;
2255 if (BrowseLocalProtocols
& BROWSE_LPD
)
2257 printer_len
= 0; /* anti-compiler-warning-code */
2258 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2260 if (!p
->printer_ref
)
2263 * Initial registration...
2266 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2267 "Registering DNS-SD printer %s with name \"%s\", "
2268 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2269 name
, domain
? domain
: "(null)");
2271 p
->printer_ref
= DNSSDRef
;
2272 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2273 kDNSServiceFlagsShareConnection
,
2274 0, name
, "_printer._tcp", domain
, NULL
,
2275 htons(515), printer_len
, printer_txt
,
2276 dnssdRegisterCallback
,
2277 p
)) == kDNSServiceErr_NoError
)
2279 p
->printer_txt
= printer_txt
;
2280 p
->printer_len
= printer_len
;
2284 cupsdLogMessage(CUPSD_LOG_WARN
,
2285 "DNS-SD LPD registration of \"%s\" failed: %d",
2288 else if (printer_len
!= p
->printer_len
||
2289 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2292 * Update the existing registration...
2295 /* A TTL of 0 means use record's original value (Radar 3176248) */
2296 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2300 free(p
->printer_txt
);
2302 p
->printer_txt
= printer_txt
;
2303 p
->printer_len
= printer_len
;
2311 #endif /* HAVE_DNSSD */
2316 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2319 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2320 get_hostconfig(const char *name
) /* I - Name of service */
2322 cups_file_t
*fp
; /* Hostconfig file */
2323 char line
[1024], /* Line from file */
2324 *ptr
; /* Pointer to value */
2325 int state
= 1; /* State of service */
2329 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2330 * the service is enabled/auto.
2333 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2336 * Read lines from the file until we find the service...
2339 while (cupsFileGets(fp
, line
, sizeof(line
)))
2341 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2346 if (!strcasecmp(line
, name
))
2349 * Found the service, see if it is set to "-NO-"...
2352 if (!strncasecmp(ptr
, "-NO-", 4))
2363 #endif /* __APPLE__ */
2367 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2370 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2371 is_local_queue(const char *uri
, /* I - Printer URI */
2372 char *host
, /* O - Host string */
2373 int hostlen
, /* I - Length of host buffer */
2374 char *resource
, /* O - Resource string */
2375 int resourcelen
) /* I - Length of resource buffer */
2377 char scheme
[32], /* Scheme portion of URI */
2378 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2379 int port
; /* Port portion of URI */
2380 cupsd_netif_t
*iface
; /* Network interface */
2384 * Pull the URI apart to see if this is a local or remote printer...
2387 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2388 username
, sizeof(username
), host
, hostlen
, &port
,
2389 resource
, resourcelen
) < HTTP_URI_OK
)
2392 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2395 * Check for local server addresses...
2398 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2403 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2405 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2406 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2410 * If we get here, the printer is remote...
2418 * 'process_browse_data()' - Process new browse data.
2422 process_browse_data(
2423 const char *uri
, /* I - URI of printer/class */
2424 const char *host
, /* I - Hostname */
2425 const char *resource
, /* I - Resource path */
2426 cups_ptype_t type
, /* I - Printer type */
2427 ipp_pstate_t state
, /* I - Printer state */
2428 const char *location
, /* I - Printer location */
2429 const char *info
, /* I - Printer information */
2430 const char *make_model
, /* I - Printer make and model */
2431 int num_attrs
, /* I - Number of attributes */
2432 cups_option_t
*attrs
) /* I - Attributes */
2434 int i
; /* Looping var */
2435 int update
; /* Update printer attributes? */
2436 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2437 name
[IPP_MAX_NAME
], /* Name of printer */
2438 newname
[IPP_MAX_NAME
], /* New name of printer */
2439 *hptr
, /* Pointer into hostname */
2440 *sptr
; /* Pointer into ServerName */
2441 char local_make_model
[IPP_MAX_NAME
];
2442 /* Local make and model */
2443 cupsd_printer_t
*p
; /* Printer information */
2444 const char *ipp_options
, /* ipp-options value */
2445 *lease_duration
; /* lease-duration value */
2449 * Determine if the URI contains any illegal characters in it...
2452 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2453 (strncmp(resource
, "/printers/", 10) &&
2454 strncmp(resource
, "/classes/", 9)))
2456 cupsdLogMessage(CUPSD_LOG_ERROR
,
2457 "process_browse_data: Bad printer URI in browse data: %s",
2462 if (strchr(resource
, '?') ||
2463 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2464 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2466 cupsdLogMessage(CUPSD_LOG_ERROR
,
2467 "process_browse_data: Bad resource in browse data: %s",
2473 * OK, this isn't a local printer; add any remote options...
2476 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2478 if (BrowseRemoteOptions
)
2480 if (BrowseRemoteOptions
[0] == '?')
2483 * Override server-supplied options...
2486 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2488 else if (ipp_options
)
2491 * Combine the server and local options...
2494 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2495 BrowseRemoteOptions
);
2500 * Just use the local options...
2503 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2508 else if (ipp_options
)
2511 * Just use the server-supplied options...
2514 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2519 * See if we already have it listed in the Printers list, and add it if not...
2522 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2523 type
&= ~CUPS_PRINTER_IMPLICIT
;
2525 hptr
= strchr(host
, '.');
2526 sptr
= strchr(ServerName
, '.');
2528 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2531 * Strip the common domain name components...
2534 while (hptr
!= NULL
)
2536 if (!strcasecmp(hptr
, sptr
))
2542 hptr
= strchr(hptr
+ 1, '.');
2546 if (type
& CUPS_PRINTER_CLASS
)
2549 * Remote destination is a class...
2552 if (!strncmp(resource
, "/classes/", 9))
2553 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2558 *hptr
= '.'; /* Resource FQDN */
2560 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2562 if ((p
= cupsdFindDest(resource
+ 9)) != NULL
)
2564 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2567 * Nope, this isn't the same host; if the hostname isn't the local host,
2568 * add it to the other class and then find a class using the full host
2572 if (p
->type
& CUPS_PRINTER_REMOTE
)
2574 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2575 "Renamed remote class \"%s\" to \"%s@%s\"...",
2576 p
->name
, p
->name
, p
->hostname
);
2577 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2578 "Class \'%s\' deleted by directory services.",
2581 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2582 cupsdRenamePrinter(p
, newname
);
2584 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2585 "Class \'%s\' added by directory services.",
2591 else if (!p
->hostname
)
2594 * Hostname not set, so this must be a cached remote printer
2595 * that was created for a pending print job...
2598 cupsdSetString(&p
->hostname
, host
);
2599 cupsdSetString(&p
->uri
, uri
);
2600 cupsdSetString(&p
->device_uri
, uri
);
2607 * Use the short name for this shared class.
2610 strlcpy(name
, resource
+ 9, sizeof(name
));
2613 else if (p
&& !p
->hostname
)
2616 * Hostname not set, so this must be a cached remote printer
2617 * that was created for a pending print job...
2620 cupsdSetString(&p
->hostname
, host
);
2621 cupsdSetString(&p
->uri
, uri
);
2622 cupsdSetString(&p
->device_uri
, uri
);
2629 * Class doesn't exist; add it...
2632 p
= cupsdAddClass(name
);
2634 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2636 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2637 "Class \'%s\' added by directory services.", name
);
2640 * Force the URI to point to the real server...
2643 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2645 cupsdSetString(&p
->uri
, uri
);
2646 cupsdSetString(&p
->device_uri
, uri
);
2647 cupsdSetString(&p
->hostname
, host
);
2651 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2657 * Remote destination is a printer...
2660 if (!strncmp(resource
, "/printers/", 10))
2661 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2666 *hptr
= '.'; /* Resource FQDN */
2668 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2670 if ((p
= cupsdFindDest(resource
+ 10)) != NULL
)
2672 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2675 * Nope, this isn't the same host; if the hostname isn't the local host,
2676 * add it to the other printer and then find a printer using the full host
2680 if (p
->type
& CUPS_PRINTER_REMOTE
)
2682 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2683 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2684 p
->name
, p
->name
, p
->hostname
);
2685 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2686 "Printer \'%s\' deleted by directory services.",
2689 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2690 cupsdRenamePrinter(p
, newname
);
2692 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2693 "Printer \'%s\' added by directory services.",
2699 else if (!p
->hostname
)
2702 * Hostname not set, so this must be a cached remote printer
2703 * that was created for a pending print job...
2706 cupsdSetString(&p
->hostname
, host
);
2707 cupsdSetString(&p
->uri
, uri
);
2708 cupsdSetString(&p
->device_uri
, uri
);
2715 * Use the short name for this shared printer.
2718 strlcpy(name
, resource
+ 10, sizeof(name
));
2721 else if (p
&& !p
->hostname
)
2724 * Hostname not set, so this must be a cached remote printer
2725 * that was created for a pending print job...
2728 cupsdSetString(&p
->hostname
, host
);
2729 cupsdSetString(&p
->uri
, uri
);
2730 cupsdSetString(&p
->device_uri
, uri
);
2737 * Printer doesn't exist; add it...
2740 p
= cupsdAddPrinter(name
);
2742 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2743 "Printer \'%s\' added by directory services.", name
);
2745 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2748 * Force the URI to point to the real server...
2751 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2753 cupsdSetString(&p
->hostname
, host
);
2754 cupsdSetString(&p
->uri
, uri
);
2755 cupsdSetString(&p
->device_uri
, uri
);
2759 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2764 * Update the state...
2768 p
->browse_time
= time(NULL
);
2770 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2774 * Grab the lease-duration for the browse data; anything less then 1
2775 * second or more than 1 week gets the default BrowseTimeout...
2778 i
= atoi(lease_duration
);
2779 if (i
< 1 || i
> 604800)
2782 p
->browse_expire
= p
->browse_time
+ i
;
2785 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2787 if (type
& CUPS_PRINTER_REJECTING
)
2789 type
&= ~CUPS_PRINTER_REJECTING
;
2797 else if (!p
->accepting
)
2803 if (p
->type
!= type
)
2809 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2811 cupsdSetString(&p
->location
, location
);
2815 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2817 cupsdSetString(&p
->info
, info
);
2820 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2823 if (!make_model
|| !make_model
[0])
2825 if (type
& CUPS_PRINTER_CLASS
)
2826 snprintf(local_make_model
, sizeof(local_make_model
),
2827 "Remote Class on %s", host
);
2829 snprintf(local_make_model
, sizeof(local_make_model
),
2830 "Remote Printer on %s", host
);
2833 snprintf(local_make_model
, sizeof(local_make_model
),
2834 "%s on %s", make_model
, host
);
2836 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2838 cupsdSetString(&p
->make_model
, local_make_model
);
2844 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2847 * See if we need to update the attributes...
2850 if (p
->num_options
!= num_attrs
)
2854 for (i
= 0; i
< num_attrs
; i
++)
2855 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2856 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2857 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2866 * Free the old options...
2869 cupsFreeOptions(p
->num_options
, p
->options
);
2872 p
->num_options
= num_attrs
;
2875 if (type
& CUPS_PRINTER_DELETE
)
2877 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2878 "%s \'%s\' deleted by directory services.",
2879 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2881 cupsdExpireSubscriptions(p
, NULL
);
2883 cupsdDeletePrinter(p
, 1);
2884 cupsdUpdateImplicitClasses();
2885 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2889 cupsdSetPrinterAttrs(p
);
2890 cupsdUpdateImplicitClasses();
2894 * See if we have a default printer... If not, make the first network
2895 * default printer the default.
2898 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2901 * Find the first network default printer and use it...
2904 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2906 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2907 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2910 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
2916 * Do auto-classing if needed...
2919 process_implicit_classes();
2924 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2928 process_implicit_classes(void)
2930 int i
; /* Looping var */
2931 int update
; /* Update printer attributes? */
2932 char name
[IPP_MAX_NAME
], /* Name of printer */
2933 *hptr
; /* Pointer into hostname */
2934 cupsd_printer_t
*p
, /* Printer information */
2935 *pclass
, /* Printer class */
2936 *first
; /* First printer in class */
2937 int offset
, /* Offset of name */
2938 len
; /* Length of name */
2941 if (!ImplicitClasses
|| !Printers
)
2945 * Loop through all available printers and create classes as needed...
2948 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2949 update
= 0, pclass
= NULL
, first
= NULL
;
2951 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2954 * Skip implicit classes...
2957 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2964 * If len == 0, get the length of this printer name up to the "@"
2968 cupsArraySave(Printers
);
2971 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2972 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2975 * We have more than one printer with the same name; see if
2976 * we have a class, and if this printer is a member...
2979 if (pclass
&& strcasecmp(pclass
->name
, name
))
2982 cupsdSetPrinterAttrs(pclass
);
2988 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2991 * Need to add the class...
2994 pclass
= cupsdAddPrinter(name
);
2995 cupsArrayAdd(ImplicitPrinters
, pclass
);
2997 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2998 pclass
->accepting
= 1;
2999 pclass
->state
= IPP_PRINTER_IDLE
;
3001 cupsdSetString(&pclass
->location
, p
->location
);
3002 cupsdSetString(&pclass
->info
, p
->info
);
3004 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3005 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3009 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3011 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3013 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3014 "Implicit class \'%s\' added by directory services.",
3020 for (i
= 0; i
< pclass
->num_printers
; i
++)
3021 if (pclass
->printers
[i
] == first
)
3024 if (i
>= pclass
->num_printers
)
3026 first
->in_implicit_class
= 1;
3027 cupsdAddPrinterToClass(pclass
, first
);
3033 for (i
= 0; i
< pclass
->num_printers
; i
++)
3034 if (pclass
->printers
[i
] == p
)
3037 if (i
>= pclass
->num_printers
)
3039 p
->in_implicit_class
= 1;
3040 cupsdAddPrinterToClass(pclass
, p
);
3047 * First time around; just get name length and mark it as first
3051 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3052 len
= hptr
- p
->name
;
3054 len
= strlen(p
->name
);
3056 strncpy(name
, p
->name
, len
);
3060 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3061 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3064 * Can't use same name as a local printer; add "Any" to the
3065 * front of the name, unless we have explicitly disabled
3066 * the "ImplicitAnyClasses"...
3069 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3072 * Add "Any" to the class name...
3075 strcpy(name
, "Any");
3076 strncpy(name
+ 3, p
->name
, len
);
3077 name
[len
+ 3] = '\0';
3083 * Don't create an implicit class if we have a local printer
3084 * with the same name...
3088 cupsArrayRestore(Printers
);
3096 cupsArrayRestore(Printers
);
3100 * Update the last printer class as needed...
3103 if (pclass
&& update
)
3104 cupsdSetPrinterAttrs(pclass
);
3109 * 'send_cups_browse()' - Send new browsing information using the CUPS
3114 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3116 int i
; /* Looping var */
3117 cups_ptype_t type
; /* Printer type */
3118 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3119 int bytes
; /* Length of packet */
3120 char packet
[1453], /* Browse data packet */
3121 uri
[1024], /* Printer URI */
3122 location
[1024], /* printer-location */
3123 info
[1024], /* printer-info */
3125 /* printer-make-and-model */
3126 cupsd_netif_t
*iface
; /* Network interface */
3130 * Figure out the printer type value...
3133 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3136 type
|= CUPS_PRINTER_REJECTING
;
3138 if (p
== DefaultPrinter
)
3139 type
|= CUPS_PRINTER_DEFAULT
;
3142 * Remove quotes from printer-info, printer-location, and
3143 * printer-make-and-model attributes...
3146 dequote(location
, p
->location
, sizeof(location
));
3147 dequote(info
, p
->info
, sizeof(info
));
3150 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3151 else if (p
->type
& CUPS_PRINTER_CLASS
)
3153 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3154 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3156 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3159 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3161 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3164 * Send a packet to each browse address...
3167 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3171 * Send the browse packet to one or more interfaces...
3174 if (!strcmp(b
->iface
, "*"))
3177 * Send to all local interfaces...
3182 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3184 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3187 * Only send to local, IPv4 interfaces...
3190 if (!iface
->is_local
|| !iface
->port
||
3191 iface
->address
.addr
.sa_family
!= AF_INET
)
3194 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3195 iface
->hostname
, iface
->port
,
3196 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3199 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3200 type
, p
->state
, uri
, location
, info
, make_model
,
3201 p
->browse_attrs
? p
->browse_attrs
: "");
3203 bytes
= strlen(packet
);
3205 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3206 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3207 iface
->name
, packet
);
3209 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3211 sendto(BrowseSocket
, packet
, bytes
, 0,
3212 (struct sockaddr
*)&(iface
->broadcast
),
3213 httpAddrLength(&(iface
->broadcast
)));
3216 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3219 * Send to the named interface using the IPv4 address...
3223 if (strcmp(b
->iface
, iface
->name
))
3228 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3231 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3235 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3236 iface
->hostname
, iface
->port
,
3237 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3240 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3241 type
, p
->state
, uri
, location
, info
, make_model
,
3242 p
->browse_attrs
? p
->browse_attrs
: "");
3244 bytes
= strlen(packet
);
3246 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3247 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3248 iface
->name
, packet
);
3250 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3252 sendto(BrowseSocket
, packet
, bytes
, 0,
3253 (struct sockaddr
*)&(iface
->broadcast
),
3254 httpAddrLength(&(iface
->broadcast
)));
3261 * Send the browse packet to the indicated address using
3262 * the default server name...
3265 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3266 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3267 p
->browse_attrs
? p
->browse_attrs
: "");
3269 bytes
= strlen(packet
);
3270 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3271 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3273 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3274 (struct sockaddr
*)&(b
->to
),
3275 httpAddrLength(&(b
->to
))) <= 0)
3278 * Unable to send browse packet, so remove this address from the
3282 cupsdLogMessage(CUPSD_LOG_ERROR
,
3283 "cupsdSendBrowseList: sendto failed for browser "
3285 (int)(b
- Browsers
+ 1), strerror(errno
));
3288 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3297 #ifdef HAVE_OPENLDAP
3299 * 'send_ldap_browse()' - Send LDAP printer registrations.
3303 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3305 int i
; /* Looping var... */
3306 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3307 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3308 LDAPMessage
*res
; /* Search result token */
3309 char *cn_value
[2], /* Change records */
3315 typestring
[255], /* String to hold printer-type */
3316 filter
[256], /* Search filter for possible UPDATEs */
3317 dn
[1024]; /* DN of the printer we are adding */
3318 int rc
; /* LDAP status */
3319 static const char * const objectClass_values
[] =
3320 { /* The 3 objectClass's we use in */
3321 "top", /* our LDAP entries */
3327 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
3330 * Everything in ldap is ** so we fudge around it...
3333 sprintf(typestring
, "%u", p
->type
);
3335 cn_value
[0] = p
->name
;
3337 info
[0] = p
->info
? p
->info
: "Unknown";
3339 location
[0] = p
->location
? p
->location
: "Unknown";
3341 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
3342 make_model
[1] = NULL
;
3343 type
[0] = typestring
;
3348 snprintf(filter
, sizeof(filter
),
3349 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
3351 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
3352 filter
, (char **)ldap_attrs
, 0, &res
);
3353 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
3356 mods
[0].mod_type
= "cn";
3357 mods
[0].mod_values
= cn_value
;
3358 mods
[1].mod_type
= "printerDescription";
3359 mods
[1].mod_values
= info
;
3360 mods
[2].mod_type
= "printerURI";
3361 mods
[2].mod_values
= uri
;
3362 mods
[3].mod_type
= "printerLocation";
3363 mods
[3].mod_values
= location
;
3364 mods
[4].mod_type
= "printerMakeAndModel";
3365 mods
[4].mod_values
= make_model
;
3366 mods
[5].mod_type
= "printerType";
3367 mods
[5].mod_values
= type
;
3368 mods
[6].mod_type
= "objectClass";
3369 mods
[6].mod_values
= (char **)objectClass_values
;
3371 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
3372 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
3374 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
3377 * Printer has already been registered, modify the current
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3382 "send_ldap_browse: Replacing entry...");
3384 for (i
= 0; i
< 7; i
++)
3386 pmods
[i
] = mods
+ i
;
3387 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3391 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3392 cupsdLogMessage(CUPSD_LOG_ERROR
,
3393 "LDAP modify for %s failed with status %d: %s",
3394 p
->name
, rc
, ldap_err2string(rc
));
3399 * Printer has never been registered, add the current
3403 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3404 "send_ldap_browse: Adding entry...");
3406 for (i
= 0; i
< 7; i
++)
3408 pmods
[i
] = mods
+ i
;
3409 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3413 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3414 cupsdLogMessage(CUPSD_LOG_ERROR
,
3415 "LDAP add for %s failed with status %d: %s",
3416 p
->name
, rc
, ldap_err2string(rc
));
3419 #endif /* HAVE_OPENLDAP */
3424 * 'send_slp_browse()' - Register the specified printer with SLP.
3428 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3430 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
3431 attrs
[8192], /* Printer attributes */
3432 finishings
[1024], /* Finishings to support */
3433 make_model
[IPP_MAX_NAME
* 2],
3434 /* Make and model, quoted */
3435 location
[IPP_MAX_NAME
* 2],
3436 /* Location, quoted */
3437 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
3438 *src
, /* Pointer to original string */
3439 *dst
; /* Pointer to destination string */
3440 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
3441 SLPError error
; /* SLP error, if any */
3444 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
3448 * Make the SLP service URL that conforms to the IANA
3449 * 'printer:' template.
3452 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3454 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
3457 * Figure out the finishings string...
3460 if (p
->type
& CUPS_PRINTER_STAPLE
)
3461 strcpy(finishings
, "staple");
3463 finishings
[0] = '\0';
3465 if (p
->type
& CUPS_PRINTER_BIND
)
3468 strlcat(finishings
, ",bind", sizeof(finishings
));
3470 strcpy(finishings
, "bind");
3473 if (p
->type
& CUPS_PRINTER_PUNCH
)
3476 strlcat(finishings
, ",punch", sizeof(finishings
));
3478 strcpy(finishings
, "punch");
3481 if (p
->type
& CUPS_PRINTER_COVER
)
3484 strlcat(finishings
, ",cover", sizeof(finishings
));
3486 strcpy(finishings
, "cover");
3489 if (p
->type
& CUPS_PRINTER_SORT
)
3492 strlcat(finishings
, ",sort", sizeof(finishings
));
3494 strcpy(finishings
, "sort");
3498 strcpy(finishings
, "none");
3501 * Quote any commas in the make and model, location, and info strings...
3504 for (src
= p
->make_model
, dst
= make_model
;
3505 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
3507 if (*src
== ',' || *src
== '\\' || *src
== ')')
3516 strcpy(make_model
, "Unknown");
3518 for (src
= p
->location
, dst
= location
;
3519 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
3521 if (*src
== ',' || *src
== '\\' || *src
== ')')
3530 strcpy(location
, "Unknown");
3532 for (src
= p
->info
, dst
= info
;
3533 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
3535 if (*src
== ',' || *src
== '\\' || *src
== ')')
3544 strcpy(info
, "Unknown");
3547 * Get the authentication value...
3550 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
3554 * Make the SLP attribute string list that conforms to
3555 * the IANA 'printer:' template.
3558 snprintf(attrs
, sizeof(attrs
),
3559 "(printer-uri-supported=%s),"
3560 "(uri-authentication-supported=%s>),"
3562 "(uri-security-supported=tls>),"
3564 "(uri-security-supported=none>),"
3565 #endif /* HAVE_SSL */
3566 "(printer-name=%s),"
3567 "(printer-location=%s),"
3568 "(printer-info=%s),"
3569 "(printer-more-info=%s),"
3570 "(printer-make-and-model=%s),"
3571 "(printer-type=%d),"
3572 "(charset-supported=utf-8),"
3573 "(natural-language-configured=%s),"
3574 "(natural-language-supported=de,en,es,fr,it),"
3575 "(color-supported=%s),"
3576 "(finishings-supported=%s),"
3577 "(sides-supported=one-sided%s),"
3578 "(multiple-document-jobs-supported=true)"
3579 "(ipp-versions-supported=1.0,1.1)",
3580 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
3581 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
3582 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
3584 p
->type
& CUPS_PRINTER_DUPLEX
?
3585 ",two-sided-long-edge,two-sided-short-edge" : "");
3587 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
3590 * Register the printer with the SLP server...
3593 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
3594 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
3596 if (error
!= SLP_OK
)
3597 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
3603 * 'slp_attr_callback()' - SLP attribute callback
3606 static SLPBoolean
/* O - SLP_TRUE for success */
3608 SLPHandle hslp
, /* I - SLP handle */
3609 const char *attrlist
, /* I - Attribute list */
3610 SLPError errcode
, /* I - Parsing status for this attr */
3611 void *cookie
) /* I - Current printer */
3613 char *tmp
= 0; /* Temporary string */
3614 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3615 /* Current printer */
3618 (void)hslp
; /* anti-compiler-warning-code */
3621 * Bail if there was an error
3624 if (errcode
!= SLP_OK
)
3628 * Parse the attrlist to obtain things needed to build CUPS browse packet
3631 memset(p
, 0, sizeof(cupsd_printer_t
));
3633 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3635 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3637 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3639 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3640 p
->type
= atoi(tmp
);
3642 p
->type
= CUPS_PRINTER_REMOTE
;
3644 cupsdClearString(&tmp
);
3651 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3655 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3657 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3660 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3662 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3665 * Make the SLP service URL that conforms to the IANA
3666 * 'printer:' template.
3669 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3672 * Deregister the printer...
3675 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3681 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3684 static int /* O - 0 on success */
3685 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3686 const char *tag
, /* I - Name of attribute */
3687 char **valbuf
) /* O - Value */
3689 char *ptr1
, /* Pointer into string */
3693 cupsdClearString(valbuf
);
3695 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3697 ptr1
+= strlen(tag
);
3699 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3705 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3706 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3709 * Dequote the value...
3712 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3713 if (*ptr1
== '\\' && ptr1
[1])
3714 _cups_strcpy(ptr1
, ptr1
+ 1);
3725 * 'slp_reg_callback()' - Empty SLPRegReport.
3729 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3730 SLPError errcode
, /* I - Error code, if any */
3731 void *cookie
) /* I - App data */
3742 * 'slp_url_callback()' - SLP service url callback
3745 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3747 SLPHandle hslp
, /* I - SLP handle */
3748 const char *srvurl
, /* I - URL of service */
3749 unsigned short lifetime
, /* I - Life of service */
3750 SLPError errcode
, /* I - Existing error code */
3751 void *cookie
) /* I - Pointer to service list */
3753 slpsrvurl_t
*s
, /* New service entry */
3754 **head
; /* Pointer to head of entry */
3758 * Let the compiler know we won't be using these vars...
3765 * Bail if there was an error
3768 if (errcode
!= SLP_OK
)
3772 * Grab the head of the list...
3775 head
= (slpsrvurl_t
**)cookie
;
3778 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3781 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3785 * Copy the SLP service URL...
3788 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3791 * Link the SLP service URL into the head of the list
3801 #endif /* HAVE_LIBSLP */
3805 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
3809 update_cups_browse(void)
3811 int i
; /* Looping var */
3812 int auth
; /* Authorization status */
3813 int len
; /* Length of name string */
3814 int bytes
; /* Number of bytes left */
3815 char packet
[1541], /* Broadcast packet */
3816 *pptr
; /* Pointer into packet */
3817 socklen_t srclen
; /* Length of source address */
3818 http_addr_t srcaddr
; /* Source address */
3819 char srcname
[1024]; /* Source hostname */
3820 unsigned address
[4]; /* Source address */
3821 unsigned type
; /* Printer type */
3822 unsigned state
; /* Printer state */
3823 char uri
[HTTP_MAX_URI
], /* Printer URI */
3824 host
[HTTP_MAX_URI
], /* Host portion of URI */
3825 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
3826 info
[IPP_MAX_NAME
], /* Information string */
3827 location
[IPP_MAX_NAME
], /* Location string */
3828 make_model
[IPP_MAX_NAME
];/* Make and model string */
3829 int num_attrs
; /* Number of attributes */
3830 cups_option_t
*attrs
; /* Attributes */
3834 * Read a packet from the browse socket...
3837 srclen
= sizeof(srcaddr
);
3838 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
3839 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
3842 * "Connection refused" is returned under Linux if the destination port
3843 * or address is unreachable from a previous sendto(); check for the
3844 * error here and ignore it for now...
3847 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
3849 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
3851 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
3854 closesocket(BrowseSocket
);
3856 close(BrowseSocket
);
3859 cupsdRemoveSelect(BrowseSocket
);
3862 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
3863 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
3869 packet
[bytes
] = '\0';
3872 * If we're about to sleep, ignore incoming browse packets.
3879 * Figure out where it came from...
3883 if (srcaddr
.addr
.sa_family
== AF_INET6
)
3885 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
3886 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
3887 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
3888 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
3891 #endif /* AF_INET6 */
3896 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
3899 if (HostNameLookups
)
3900 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
3902 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
3904 len
= strlen(srcname
);
3912 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
3915 * Access from localhost (127.0.0.1) is always allowed...
3918 auth
= CUPSD_AUTH_ALLOW
;
3923 * Do authorization checks on the domain/address...
3926 switch (BrowseACL
->order_type
)
3929 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
3932 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
3933 auth
= CUPSD_AUTH_ALLOW
;
3935 if (cupsdCheckAuth(address
, srcname
, len
,
3936 BrowseACL
->num_deny
, BrowseACL
->deny
))
3937 auth
= CUPSD_AUTH_DENY
;
3939 if (cupsdCheckAuth(address
, srcname
, len
,
3940 BrowseACL
->num_allow
, BrowseACL
->allow
))
3941 auth
= CUPSD_AUTH_ALLOW
;
3944 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
3945 auth
= CUPSD_AUTH_DENY
;
3947 if (cupsdCheckAuth(address
, srcname
, len
,
3948 BrowseACL
->num_allow
, BrowseACL
->allow
))
3949 auth
= CUPSD_AUTH_ALLOW
;
3951 if (cupsdCheckAuth(address
, srcname
, len
,
3952 BrowseACL
->num_deny
, BrowseACL
->deny
))
3953 auth
= CUPSD_AUTH_DENY
;
3959 auth
= CUPSD_AUTH_ALLOW
;
3961 if (auth
== CUPSD_AUTH_DENY
)
3963 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3964 "update_cups_browse: Refused %d bytes from %s", bytes
,
3969 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3970 "update_cups_browse: (%d bytes from %s) %s", bytes
,
3977 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
3979 cupsdLogMessage(CUPSD_LOG_WARN
,
3980 "update_cups_browse: Garbled browse packet - %s", packet
);
3984 strcpy(location
, "Location Unknown");
3985 strcpy(info
, "No Information Available");
3986 make_model
[0] = '\0';
3990 if ((pptr
= strchr(packet
, '\"')) != NULL
)
3993 * Have extended information; can't use sscanf for it because not all
3994 * sscanf's allow empty strings with %[^\"]...
3997 for (i
= 0, pptr
++;
3998 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4000 location
[i
] = *pptr
;
4008 while (*pptr
&& isspace(*pptr
& 255))
4013 for (i
= 0, pptr
++;
4014 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
4023 while (*pptr
&& isspace(*pptr
& 255))
4028 for (i
= 0, pptr
++;
4029 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
4031 make_model
[i
] = *pptr
;
4036 make_model
[i
] = '\0';
4039 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
4045 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
4046 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
4047 type
, state
, uri
, location
, info
, make_model
));
4050 * Pull the URI apart to see if this is a local or remote printer...
4053 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
4055 cupsFreeOptions(num_attrs
, attrs
);
4063 for (i
= 0; i
< NumRelays
; i
++)
4064 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
4065 if (sendto(BrowseSocket
, packet
, bytes
, 0,
4066 (struct sockaddr
*)&(Relays
[i
].to
),
4067 httpAddrLength(&(Relays
[i
].to
))) <= 0)
4069 cupsdLogMessage(CUPSD_LOG_ERROR
,
4070 "update_cups_browse: sendto failed for relay %d - %s.",
4071 i
+ 1, strerror(errno
));
4072 cupsFreeOptions(num_attrs
, attrs
);
4077 * Process the browse data...
4080 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
4081 (ipp_pstate_t
)state
, location
, info
, make_model
,
4087 * 'update_lpd()' - Update the LPD configuration as needed.
4091 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
4098 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
4099 * setting for backwards-compatibility.
4102 if (onoff
&& !get_hostconfig("CUPS_LPD"))
4104 #endif /* __APPLE__ */
4106 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
4109 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
4112 char newfile
[1024]; /* New cups-lpd.N file */
4113 cups_file_t
*ofp
, /* Original file pointer */
4114 *nfp
; /* New file pointer */
4115 char line
[1024]; /* Line from file */
4118 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
4120 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
4122 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4123 LPDConfigFile
+ 9, strerror(errno
));
4127 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4129 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4130 newfile
, strerror(errno
));
4136 * Copy all of the lines from the cups-lpd file...
4139 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4143 cupsFilePrintf(nfp
, "%s\n", line
);
4144 snprintf(line
, sizeof(line
), "\tdisable = %s",
4145 onoff
? "no" : "yes");
4147 else if (!strstr(line
, "disable ="))
4148 cupsFilePrintf(nfp
, "%s\n", line
);
4153 rename(newfile
, LPDConfigFile
+ 9);
4156 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
4159 * Enable/disable LPD via the launchctl command...
4162 char *argv
[5], /* Arguments for command */
4163 *envp
[MAX_ENV
]; /* Environment for command */
4164 int pid
; /* Process ID */
4167 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
4168 argv
[0] = (char *)"launchctl";
4169 argv
[1] = (char *)(onoff
? "load" : "unload");
4170 argv
[2] = (char *)"-w";
4171 argv
[3] = LPDConfigFile
+ 10;
4174 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
4177 #endif /* __APPLE__ */
4179 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
4184 * 'update_polling()' - Read status messages from the poll daemons.
4188 update_polling(void)
4190 char *ptr
, /* Pointer to end of line in buffer */
4191 message
[1024]; /* Pointer to message text */
4192 int loglevel
; /* Log level for message */
4195 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
4196 message
, sizeof(message
))) != NULL
)
4197 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
4200 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
4203 * All polling processes have died; stop polling...
4206 cupsdLogMessage(CUPSD_LOG_ERROR
,
4207 "update_polling: all polling processes have exited!");
4214 * 'update_smb()' - Update the SMB configuration as needed.
4218 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
4223 if (!strncmp(SMBConfigFile
, "samba:///", 9))
4226 * Enable/disable SMB via the specified smb.conf config file...
4229 char newfile
[1024]; /* New smb.conf.N file */
4230 cups_file_t
*ofp
, /* Original file pointer */
4231 *nfp
; /* New file pointer */
4232 char line
[1024]; /* Line from file */
4233 int in_printers
; /* In [printers] section? */
4236 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
4238 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
4240 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
4241 SMBConfigFile
+ 8, strerror(errno
));
4245 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
4247 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
4248 newfile
, strerror(errno
));
4254 * Copy all of the lines from the smb.conf file...
4259 while (cupsFileGets(ofp
, line
, sizeof(line
)))
4261 if (in_printers
&& strstr(line
, "printable ="))
4262 snprintf(line
, sizeof(line
), " printable = %s",
4263 onoff
? "yes" : "no");
4265 cupsFilePrintf(nfp
, "%s\n", line
);
4268 in_printers
= !strcmp(line
, "[printers]");
4273 rename(newfile
, SMBConfigFile
+ 8);
4276 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
4281 * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".