2 * "$Id: dirsvc.c 6003 2006-10-02 16:26:04Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdLoadRemoteCache() - Load the remote printer cache.
27 * cupsdSaveRemoteCache() - Save the remote printer cache.
28 * cupsdSendBrowseDelete() - Send a "browse delete" message for a
30 * cupsdSendBrowseList() - Send new browsing information as necessary.
31 * cupsdStartBrowsing() - Start sending and receiving broadcast
33 * cupsdStartPolling() - Start polling servers as needed.
34 * cupsdStopBrowsing() - Stop sending and receiving broadcast
36 * cupsdStopPolling() - Stop polling servers as needed.
37 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
39 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
40 * cupsdUpdatePolling() - Read status messages from the poll daemons.
41 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
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
57 * Include necessary headers...
68 static char *dequote(char *d
, const char *s
, int dlen
);
69 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
70 char *resource
, int resourcelen
);
71 static void process_browse_data(const char *uri
, const char *host
,
72 const char *resource
, cups_ptype_t type
,
73 ipp_pstate_t state
, const char *location
,
74 const char *info
, const char *make_model
,
75 int num_attrs
, cups_option_t
*attrs
);
76 static void process_implicit_classes(void);
77 static void send_cups_browse(cupsd_printer_t
*p
);
79 static void send_ldap_browse(cupsd_printer_t
*p
);
80 #endif /* HAVE_LDAP */
82 static void send_slp_browse(cupsd_printer_t
*p
);
83 #endif /* HAVE_LIBSLP */
86 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
90 "printerMakeAndModel",
95 #endif /* HAVE_OPENLDAP */
103 * SLP service name for CUPS...
106 # define SLP_CUPS_SRVTYPE "service:printer"
107 # define SLP_CUPS_SRVLEN 15
111 * Printer service URL structure
114 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
116 struct _slpsrvurl_s
*next
; /* Next URL in list */
117 char url
[HTTP_MAX_URI
];
126 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
127 SLPError errcode
, void *cookie
);
128 static void slp_dereg_printer(cupsd_printer_t
*p
);
129 static int slp_get_attr(const char *attrlist
, const char *tag
,
131 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
133 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
134 unsigned short lifetime
,
135 SLPError errcode
, void *cookie
);
136 #endif /* HAVE_LIBSLP */
140 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
144 cupsdLoadRemoteCache(void)
146 cups_file_t
*fp
; /* remote.cache file */
147 int linenum
; /* Current line number */
148 char line
[1024], /* Line from file */
149 *value
, /* Pointer to value */
150 *valueptr
, /* Pointer into value */
151 scheme
[32], /* Scheme portion of URI */
152 username
[64], /* Username portion of URI */
154 /* Hostname portion of URI */
155 resource
[HTTP_MAX_URI
];
156 /* Resource portion of URI */
157 int port
; /* Port number */
158 cupsd_printer_t
*p
; /* Current printer */
159 time_t now
; /* Current time */
163 * Open the remote.cache file...
166 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
167 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
171 * Read printer configurations until we hit EOF...
178 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
181 * Decode the directive...
184 if (!strcasecmp(line
, "<Printer") ||
185 !strcasecmp(line
, "<DefaultPrinter"))
188 * <Printer name> or <DefaultPrinter name>
191 if (p
== NULL
&& value
)
194 * Add the printer and a base file type...
197 cupsdLogMessage(CUPSD_LOG_DEBUG
,
198 "cupsdLoadRemoteCache: Loading printer %s...", value
);
200 if ((p
= cupsdFindDest(value
)) != NULL
)
202 if (p
->type
& CUPS_PRINTER_CLASS
)
204 cupsdLogMessage(CUPSD_LOG_WARN
,
205 "Cached remote printer \"%s\" conflicts with "
213 p
= cupsdAddPrinter(value
);
216 p
->state
= IPP_PRINTER_IDLE
;
217 p
->type
|= CUPS_PRINTER_REMOTE
;
218 p
->browse_time
= now
;
219 p
->browse_expire
= now
+ BrowseTimeout
;
222 * Set the default printer as needed...
225 if (!strcasecmp(line
, "<DefaultPrinter"))
230 cupsdLogMessage(CUPSD_LOG_ERROR
,
231 "Syntax error on line %d of remote.cache.", linenum
);
235 else if (!strcasecmp(line
, "<Class") ||
236 !strcasecmp(line
, "<DefaultClass"))
239 * <Class name> or <DefaultClass name>
242 if (p
== NULL
&& value
)
245 * Add the printer and a base file type...
248 cupsdLogMessage(CUPSD_LOG_DEBUG
,
249 "cupsdLoadRemoteCache: Loading class %s...", value
);
251 if ((p
= cupsdFindDest(value
)) != NULL
)
252 p
->type
= CUPS_PRINTER_CLASS
;
254 p
= cupsdAddClass(value
);
257 p
->state
= IPP_PRINTER_IDLE
;
258 p
->type
|= CUPS_PRINTER_REMOTE
;
259 p
->browse_time
= now
;
260 p
->browse_expire
= now
+ BrowseTimeout
;
263 * Set the default printer as needed...
266 if (!strcasecmp(line
, "<DefaultClass"))
271 cupsdLogMessage(CUPSD_LOG_ERROR
,
272 "Syntax error on line %d of remote.cache.", linenum
);
276 else if (!strcasecmp(line
, "</Printer>") ||
277 !strcasecmp(line
, "</Class>"))
282 * Close out the current printer...
285 cupsdSetPrinterAttrs(p
);
291 cupsdLogMessage(CUPSD_LOG_ERROR
,
292 "Syntax error on line %d of remote.cache.", linenum
);
298 cupsdLogMessage(CUPSD_LOG_ERROR
,
299 "Syntax error on line %d of remote.cache.", linenum
);
302 else if (!strcasecmp(line
, "Info"))
305 cupsdSetString(&p
->info
, value
);
307 else if (!strcasecmp(line
, "MakeModel"))
310 cupsdSetString(&p
->make_model
, value
);
312 else if (!strcasecmp(line
, "Location"))
315 cupsdSetString(&p
->location
, value
);
317 else if (!strcasecmp(line
, "DeviceURI"))
321 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
322 username
, sizeof(username
), host
, sizeof(host
), &port
,
323 resource
, sizeof(resource
));
325 cupsdSetString(&p
->hostname
, host
);
326 cupsdSetString(&p
->uri
, value
);
327 cupsdSetString(&p
->device_uri
, value
);
331 cupsdLogMessage(CUPSD_LOG_ERROR
,
332 "Syntax error on line %d of remote.cache.", linenum
);
336 else if (!strcasecmp(line
, "Option") && value
)
342 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
345 cupsdLogMessage(CUPSD_LOG_ERROR
,
346 "Syntax error on line %d of remote.cache.", linenum
);
349 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
351 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
355 else if (!strcasecmp(line
, "State"))
358 * Set the initial queue state...
361 if (value
&& !strcasecmp(value
, "idle"))
362 p
->state
= IPP_PRINTER_IDLE
;
363 else if (value
&& !strcasecmp(value
, "stopped"))
364 p
->state
= IPP_PRINTER_STOPPED
;
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Syntax error on line %d of remote.cache.", linenum
);
372 else if (!strcasecmp(line
, "StateMessage"))
375 * Set the initial queue state message...
379 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
381 else if (!strcasecmp(line
, "Accepting"))
384 * Set the initial accepting state...
388 (!strcasecmp(value
, "yes") ||
389 !strcasecmp(value
, "on") ||
390 !strcasecmp(value
, "true")))
393 (!strcasecmp(value
, "no") ||
394 !strcasecmp(value
, "off") ||
395 !strcasecmp(value
, "false")))
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Syntax error on line %d of remote.cache.", linenum
);
404 else if (!strcasecmp(line
, "Type"))
407 p
->type
= atoi(value
);
410 cupsdLogMessage(CUPSD_LOG_ERROR
,
411 "Syntax error on line %d of remote.cache.", linenum
);
415 else if (!strcasecmp(line
, "BrowseTime"))
419 time_t t
= atoi(value
);
421 if (t
> p
->browse_expire
)
422 p
->browse_expire
= t
;
426 cupsdLogMessage(CUPSD_LOG_ERROR
,
427 "Syntax error on line %d of remote.cache.", linenum
);
431 else if (!strcasecmp(line
, "JobSheets"))
434 * Set the initial job sheets...
439 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
444 cupsdSetString(&p
->job_sheets
[0], value
);
446 while (isspace(*valueptr
& 255))
451 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
456 cupsdSetString(&p
->job_sheets
[1], value
);
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "Syntax error on line %d of remote.cache.", linenum
);
466 else if (!strcasecmp(line
, "AllowUser"))
471 cupsdAddPrinterUser(p
, value
);
475 cupsdLogMessage(CUPSD_LOG_ERROR
,
476 "Syntax error on line %d of remote.cache.", linenum
);
480 else if (!strcasecmp(line
, "DenyUser"))
485 cupsdAddPrinterUser(p
, value
);
489 cupsdLogMessage(CUPSD_LOG_ERROR
,
490 "Syntax error on line %d of remote.cache.", linenum
);
497 * Something else we don't understand...
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Unknown configuration directive %s on line %d of remote.cache.",
509 * Do auto-classing if needed...
512 process_implicit_classes();
517 * 'cupsdRestartPolling()' - Restart polling servers as needed.
521 cupsdRestartPolling(void)
523 int i
; /* Looping var */
524 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
527 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
529 kill(pollp
->pid
, SIGHUP
);
534 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
538 cupsdSaveRemoteCache(void)
540 int i
; /* Looping var */
541 cups_file_t
*fp
; /* printers.conf file */
542 char temp
[1024]; /* Temporary string */
543 cupsd_printer_t
*printer
; /* Current printer class */
544 time_t curtime
; /* Current time */
545 struct tm
*curdate
; /* Current date */
546 cups_option_t
*option
; /* Current option */
550 * Create the remote.cache file...
553 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
555 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
557 cupsdLogMessage(CUPSD_LOG_ERROR
,
558 "Unable to save remote.cache - %s", strerror(errno
));
562 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
565 * Restrict access to the file...
568 fchown(cupsFileNumber(fp
), getuid(), Group
);
569 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
572 * Write a small header to the file...
575 curtime
= time(NULL
);
576 curdate
= localtime(&curtime
);
577 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
579 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
580 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
583 * Write each local printer known to the system...
586 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
588 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
591 * Skip local destinations...
594 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
598 * Write printers as needed...
601 if (printer
== DefaultPrinter
)
602 cupsFilePuts(fp
, "<Default");
604 cupsFilePutChar(fp
, '<');
606 if (printer
->type
& CUPS_PRINTER_CLASS
)
607 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
609 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
611 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
613 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
616 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
618 if (printer
->make_model
)
619 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
621 if (printer
->location
)
622 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
624 if (printer
->device_uri
)
625 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
627 if (printer
->state
== IPP_PRINTER_STOPPED
)
629 cupsFilePuts(fp
, "State Stopped\n");
630 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
633 cupsFilePuts(fp
, "State Idle\n");
635 if (printer
->accepting
)
636 cupsFilePuts(fp
, "Accepting Yes\n");
638 cupsFilePuts(fp
, "Accepting No\n");
640 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
641 printer
->job_sheets
[1]);
643 for (i
= 0; i
< printer
->num_users
; i
++)
644 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
647 for (i
= printer
->num_options
, option
= printer
->options
;
650 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
652 if (printer
->type
& CUPS_PRINTER_CLASS
)
653 cupsFilePuts(fp
, "</Class>\n");
655 cupsFilePuts(fp
, "</Printer>\n");
663 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
667 cupsdSendBrowseDelete(
668 cupsd_printer_t
*p
) /* I - Printer to delete */
671 * Only announce if browsing is enabled and this is a local queue...
674 if (!Browsing
|| !p
->shared
||
675 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
679 * First mark the printer for deletion...
682 p
->type
|= CUPS_PRINTER_DELETE
;
685 * Announce the deletion...
688 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
691 if ((BrowseLocalProtocols
& BROWSE_SLP
) && BrowseSLPHandle
)
692 slp_dereg_printer(p
);
693 #endif /* HAVE_LIBSLP */
698 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
702 cupsdSendBrowseList(void)
704 int count
; /* Number of dests to update */
705 cupsd_printer_t
*p
; /* Current printer */
706 time_t ut
, /* Minimum update time */
707 to
; /* Timeout time */
710 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
714 * Compute the update and timeout times...
718 ut
= to
- BrowseInterval
;
721 * Figure out how many printers need an update...
724 if (BrowseInterval
> 0)
726 int max_count
; /* Maximum number to update */
730 * Throttle the number of printers we'll be updating this time
731 * around based on the number of queues that need updating and
732 * the maximum number of queues to update each second...
735 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
737 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
738 count
< max_count
&& p
!= NULL
;
739 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
740 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
741 p
->shared
&& p
->browse_time
< ut
)
745 * Loop through all of the printers and send local updates as needed...
749 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
751 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
755 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
758 * Check for wraparound...
762 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
766 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
769 else if (p
->browse_time
< ut
)
772 * Need to send an update...
777 p
->browse_time
= time(NULL
);
779 if (BrowseLocalProtocols
& BROWSE_CUPS
)
783 if (BrowseLocalProtocols
& BROWSE_SLP
)
785 #endif /* HAVE_LIBSLP */
788 if (BrowseLocalProtocols
& BROWSE_LDAP
)
790 #endif /* HAVE_LDAP */
795 * Save where we left off so that all printers get updated...
802 * Loop through all of the printers and send local updates as needed...
805 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
807 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
810 * If this is a remote queue, see if it needs to be timed out...
813 if (p
->type
& CUPS_PRINTER_REMOTE
)
815 if (p
->browse_expire
< to
)
817 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
818 "%s \'%s\' deleted by directory services (timeout).",
819 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
822 cupsdLogMessage(CUPSD_LOG_DEBUG
,
823 "Remote destination \"%s\" has timed out; "
827 cupsArraySave(Printers
);
828 cupsdDeletePrinter(p
, 1);
829 cupsArrayRestore(Printers
);
837 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
841 cupsdStartBrowsing(void)
843 int val
; /* Socket option value */
844 struct sockaddr_in addr
; /* Broadcast address */
849 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
852 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
854 if (BrowseSocket
< 0)
857 * Create the broadcast socket...
860 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
862 cupsdLogMessage(CUPSD_LOG_ERROR
,
863 "cupsdStartBrowsing: Unable to create broadcast "
864 "socket - %s.", strerror(errno
));
865 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
866 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
871 * Bind the socket to browse port...
874 memset(&addr
, 0, sizeof(addr
));
875 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
876 addr
.sin_family
= AF_INET
;
877 addr
.sin_port
= htons(BrowsePort
);
879 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
881 cupsdLogMessage(CUPSD_LOG_ERROR
,
882 "cupsdStartBrowsing: Unable to bind broadcast "
883 "socket - %s.", strerror(errno
));
886 closesocket(BrowseSocket
);
892 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
893 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
899 * Set the "broadcast" flag...
903 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
905 cupsdLogMessage(CUPSD_LOG_ERROR
,
906 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
910 closesocket(BrowseSocket
);
916 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
917 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
922 * Close the socket on exec...
925 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
928 * Finally, add the socket to the input selection set as needed...
931 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
934 * We only listen if we want remote printers...
937 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
938 "cupsdStartBrowsing: Adding fd %d to InputSet...",
941 FD_SET(BrowseSocket
, InputSet
);
948 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
954 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
956 cupsdLogMessage(CUPSD_LOG_ERROR
,
957 "Unable to open an SLP handle; disabling SLP browsing!");
958 BrowseLocalProtocols
&= ~BROWSE_SLP
;
959 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
962 BrowseSLPRefresh
= 0;
965 BrowseSLPHandle
= NULL
;
966 #endif /* HAVE_LIBSLP */
969 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
973 cupsdLogMessage(CUPSD_LOG_ERROR
,
974 "Need to set BrowseLDAPDN to use LDAP browsing!");
975 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
976 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
981 * Open LDAP handle...
984 int rc
; /* LDAP API status */
985 int version
= 3; /* LDAP version */
986 struct berval bv
= {0, ""}; /* SASL bind value */
990 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
993 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
994 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
996 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
998 if (rc
!= LDAP_SUCCESS
)
1000 cupsdLogMessage(CUPSD_LOG_ERROR
,
1001 "Unable to initialize LDAP; disabling LDAP browsing!");
1002 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1003 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1005 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1006 (const void *)&version
) != LDAP_SUCCESS
)
1008 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1009 BrowseLDAPHandle
= NULL
;
1010 cupsdLogMessage(CUPSD_LOG_ERROR
,
1011 "Unable to set LDAP protocol version; "
1012 "disabling LDAP browsing!");
1013 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1014 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1018 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1019 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1022 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1023 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1025 if (rc
!= LDAP_SUCCESS
)
1027 cupsdLogMessage(CUPSD_LOG_ERROR
,
1028 "Unable to bind to LDAP server; "
1029 "disabling LDAP browsing!");
1030 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1031 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1032 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1037 BrowseLDAPRefresh
= 0;
1039 #endif /* HAVE_OPENLDAP */
1044 * 'cupsdStartPolling()' - Start polling servers as needed.
1048 cupsdStartPolling(void)
1050 int i
; /* Looping var */
1051 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1052 char polld
[1024]; /* Poll daemon path */
1053 char sport
[255]; /* Server port */
1054 char bport
[255]; /* Browser port */
1055 char interval
[255]; /* Poll interval */
1056 int statusfds
[2]; /* Status pipe */
1057 char *argv
[6]; /* Arguments */
1058 char *envp
[100]; /* Environment */
1062 * Don't do anything if we aren't polling...
1065 if (NumPolled
== 0 || BrowseSocket
< 0)
1068 PollStatusBuffer
= NULL
;
1073 * Setup string arguments for polld, port and interval options.
1076 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1078 sprintf(bport
, "%d", BrowsePort
);
1081 sprintf(interval
, "%d", BrowseInterval
);
1083 strcpy(interval
, "30");
1085 argv
[0] = "cups-polld";
1091 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1094 * Create a pipe that receives the status messages from each
1098 if (cupsdOpenPipe(statusfds
))
1100 cupsdLogMessage(CUPSD_LOG_ERROR
,
1101 "Unable to create polling status pipes - %s.",
1104 PollStatusBuffer
= NULL
;
1108 PollPipe
= statusfds
[0];
1109 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1112 * Run each polling daemon, redirecting stderr to the polling pipe...
1115 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1117 sprintf(sport
, "%d", pollp
->port
);
1119 argv
[1] = pollp
->hostname
;
1121 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1122 0, &(pollp
->pid
)) < 0)
1124 cupsdLogMessage(CUPSD_LOG_ERROR
,
1125 "cupsdStartPolling: Unable to fork polling daemon - %s",
1131 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1132 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1133 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1136 close(statusfds
[1]);
1139 * Finally, add the pipe to the input selection set...
1142 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1143 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1145 FD_SET(PollPipe
, InputSet
);
1150 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1154 cupsdStopBrowsing(void)
1156 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1159 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1163 * Close the socket and remove it from the input selection set.
1167 closesocket(BrowseSocket
);
1169 close(BrowseSocket
);
1172 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1173 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1176 FD_CLR(BrowseSocket
, InputSet
);
1181 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1185 * Close SLP handle...
1188 SLPClose(BrowseSLPHandle
);
1189 BrowseSLPHandle
= NULL
;
1191 #endif /* HAVE_LIBSLP */
1193 #ifdef HAVE_OPENLDAP
1194 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1197 ldap_unbind(BrowseLDAPHandle
);
1198 BrowseLDAPHandle
= NULL
;
1200 #endif /* HAVE_OPENLDAP */
1205 * 'cupsdStopPolling()' - Stop polling servers as needed.
1209 cupsdStopPolling(void)
1211 int i
; /* Looping var */
1212 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1217 cupsdStatBufDelete(PollStatusBuffer
);
1220 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1221 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1222 FD_CLR(PollPipe
, InputSet
);
1225 PollStatusBuffer
= NULL
;
1228 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1230 cupsdEndProcess(pollp
->pid
, 0);
1235 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1239 cupsdUpdateCUPSBrowse(void)
1241 int i
; /* Looping var */
1242 int auth
; /* Authorization status */
1243 int len
; /* Length of name string */
1244 int bytes
; /* Number of bytes left */
1245 char packet
[1541], /* Broadcast packet */
1246 *pptr
; /* Pointer into packet */
1247 socklen_t srclen
; /* Length of source address */
1248 http_addr_t srcaddr
; /* Source address */
1249 char srcname
[1024]; /* Source hostname */
1250 unsigned address
[4]; /* Source address */
1251 unsigned type
; /* Printer type */
1252 unsigned state
; /* Printer state */
1253 char uri
[HTTP_MAX_URI
], /* Printer URI */
1254 host
[HTTP_MAX_URI
], /* Host portion of URI */
1255 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1256 info
[IPP_MAX_NAME
], /* Information string */
1257 location
[IPP_MAX_NAME
], /* Location string */
1258 make_model
[IPP_MAX_NAME
];/* Make and model string */
1259 int num_attrs
; /* Number of attributes */
1260 cups_option_t
*attrs
; /* Attributes */
1264 * Read a packet from the browse socket...
1267 srclen
= sizeof(srcaddr
);
1268 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1269 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1272 * "Connection refused" is returned under Linux if the destination port
1273 * or address is unreachable from a previous sendto(); check for the
1274 * error here and ignore it for now...
1277 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1279 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1281 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1283 cupsdStopBrowsing();
1290 packet
[bytes
] = '\0';
1293 * If we're about to sleep, ignore incoming browse packets.
1300 * Figure out where it came from...
1304 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1306 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1307 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1308 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1309 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1312 #endif /* AF_INET6 */
1317 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1320 if (HostNameLookups
)
1321 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1323 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1325 len
= strlen(srcname
);
1333 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1336 * Access from localhost (127.0.0.1) is always allowed...
1344 * Do authorization checks on the domain/address...
1347 switch (BrowseACL
->order_type
)
1350 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1353 case AUTH_ALLOW
: /* Order Deny,Allow */
1356 if (cupsdCheckAuth(address
, srcname
, len
,
1357 BrowseACL
->num_deny
, BrowseACL
->deny
))
1360 if (cupsdCheckAuth(address
, srcname
, len
,
1361 BrowseACL
->num_allow
, BrowseACL
->allow
))
1365 case AUTH_DENY
: /* Order Allow,Deny */
1368 if (cupsdCheckAuth(address
, srcname
, len
,
1369 BrowseACL
->num_allow
, BrowseACL
->allow
))
1372 if (cupsdCheckAuth(address
, srcname
, len
,
1373 BrowseACL
->num_deny
, BrowseACL
->deny
))
1382 if (auth
== AUTH_DENY
)
1384 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1385 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1390 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1391 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1398 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1400 cupsdLogMessage(CUPSD_LOG_WARN
,
1401 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1405 strcpy(location
, "Location Unknown");
1406 strcpy(info
, "No Information Available");
1407 make_model
[0] = '\0';
1411 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1414 * Have extended information; can't use sscanf for it because not all
1415 * sscanf's allow empty strings with %[^\"]...
1418 for (i
= 0, pptr
++;
1419 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1421 location
[i
] = *pptr
;
1429 while (*pptr
&& isspace(*pptr
& 255))
1434 for (i
= 0, pptr
++;
1435 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1444 while (*pptr
&& isspace(*pptr
& 255))
1449 for (i
= 0, pptr
++;
1450 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1452 make_model
[i
] = *pptr
;
1457 make_model
[i
] = '\0';
1460 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1466 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1467 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1468 type
, state
, uri
, location
, info
, make_model
));
1471 * Pull the URI apart to see if this is a local or remote printer...
1474 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1476 cupsFreeOptions(num_attrs
, attrs
);
1484 for (i
= 0; i
< NumRelays
; i
++)
1485 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1486 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1487 (struct sockaddr
*)&(Relays
[i
].to
),
1488 httpAddrLength(&(Relays
[i
].to
))) <= 0)
1490 cupsdLogMessage(CUPSD_LOG_ERROR
,
1491 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1492 i
+ 1, strerror(errno
));
1493 cupsFreeOptions(num_attrs
, attrs
);
1498 * Process the browse data...
1501 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1502 (ipp_pstate_t
)state
, location
, info
, make_model
,
1507 #ifdef HAVE_OPENLDAP
1509 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1513 cupsdUpdateLDAPBrowse(void)
1515 char uri
[HTTP_MAX_URI
], /* Printer URI */
1516 host
[HTTP_MAX_URI
], /* Hostname */
1517 resource
[HTTP_MAX_URI
], /* Resource path */
1518 location
[1024], /* Printer location */
1519 info
[1024], /* Printer information */
1520 make_model
[1024], /* Printer make and model */
1521 **value
; /* Holds the returned data from LDAP */
1522 int type
; /* Printer type */
1523 int rc
; /* LDAP status */
1524 int limit
; /* Size limit */
1525 LDAPMessage
*res
, /* LDAP search results */
1526 *e
; /* Current entry from search */
1530 * Search for printers...
1533 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1535 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1537 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1538 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1539 if (rc
!= LDAP_SUCCESS
)
1541 cupsdLogMessage(CUPSD_LOG_ERROR
,
1542 "LDAP search returned error %d: %s", rc
,
1543 ldap_err2string(rc
));
1547 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1548 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1553 * Loop through the available printers...
1556 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1558 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1561 * Get the required values from this entry...
1564 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1565 "printerDescription")) == NULL
)
1568 strlcpy(info
, *value
, sizeof(info
));
1569 ldap_value_free(value
);
1571 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1572 "printerLocation")) == NULL
)
1575 strlcpy(location
, *value
, sizeof(location
));
1576 ldap_value_free(value
);
1578 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1579 "printerMakeAndModel")) == NULL
)
1582 strlcpy(make_model
, *value
, sizeof(make_model
));
1583 ldap_value_free(value
);
1585 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1586 "printerType")) == NULL
)
1589 type
= atoi(*value
);
1590 ldap_value_free(value
);
1592 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1593 "printerURI")) == NULL
)
1596 strlcpy(uri
, *value
, sizeof(uri
));
1597 ldap_value_free(value
);
1600 * Process the entry as browse data...
1603 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1604 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1605 location
, info
, make_model
, 0, NULL
);
1609 #endif /* HAVE_OPENLDAP */
1613 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1617 cupsdUpdatePolling(void)
1619 char *ptr
, /* Pointer to end of line in buffer */
1620 message
[1024]; /* Pointer to message text */
1621 int loglevel
; /* Log level for message */
1624 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1625 message
, sizeof(message
))) != NULL
)
1626 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1629 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
1632 * All polling processes have died; stop polling...
1635 cupsdLogMessage(CUPSD_LOG_ERROR
,
1636 "cupsdUpdatePolling: all polling processes have exited!");
1644 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1648 cupsdUpdateSLPBrowse(void)
1650 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1651 *next
; /* Next service in list */
1652 cupsd_printer_t p
; /* Printer information */
1653 const char *uri
; /* Pointer to printer URI */
1654 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1655 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1659 * Reset the refresh time...
1662 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1665 * Poll for remote printers using SLP...
1670 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1671 slp_url_callback
, &s
);
1674 * Loop through the list of available printers...
1680 * Save the "next" pointer...
1686 * Load a cupsd_printer_t structure with the SLP service attributes...
1689 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1692 * Process this printer entry...
1695 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1697 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1700 * Pull the URI apart to see if this is a local or remote printer...
1703 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1704 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1705 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1709 * Free this listing...
1712 cupsdClearString(&p
.info
);
1713 cupsdClearString(&p
.location
);
1714 cupsdClearString(&p
.make_model
);
1719 #endif /* HAVE_LIBSLP */
1723 * 'dequote()' - Remote quotes from a string.
1726 static char * /* O - Dequoted string */
1727 dequote(char *d
, /* I - Destination string */
1728 const char *s
, /* I - Source string */
1729 int dlen
) /* I - Destination length */
1731 char *dptr
; /* Pointer into destination */
1736 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1753 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1756 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1757 is_local_queue(const char *uri
, /* I - Printer URI */
1758 char *host
, /* O - Host string */
1759 int hostlen
, /* I - Length of host buffer */
1760 char *resource
, /* O - Resource string */
1761 int resourcelen
) /* I - Length of resource buffer */
1763 char scheme
[32], /* Scheme portion of URI */
1764 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1765 int port
; /* Port portion of URI */
1766 cupsd_netif_t
*iface
; /* Network interface */
1770 * Pull the URI apart to see if this is a local or remote printer...
1773 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1774 username
, sizeof(username
), host
, hostlen
, &port
,
1775 resource
, resourcelen
) < HTTP_URI_OK
)
1778 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1781 * Check for local server addresses...
1784 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1789 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1791 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1792 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1796 * If we get here, the printer is remote...
1804 * 'process_browse_data()' - Process new browse data.
1808 process_browse_data(
1809 const char *uri
, /* I - URI of printer/class */
1810 const char *host
, /* I - Hostname */
1811 const char *resource
, /* I - Resource path */
1812 cups_ptype_t type
, /* I - Printer type */
1813 ipp_pstate_t state
, /* I - Printer state */
1814 const char *location
, /* I - Printer location */
1815 const char *info
, /* I - Printer information */
1816 const char *make_model
, /* I - Printer make and model */
1817 int num_attrs
, /* I - Number of attributes */
1818 cups_option_t
*attrs
) /* I - Attributes */
1820 int i
; /* Looping var */
1821 int update
; /* Update printer attributes? */
1822 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1823 name
[IPP_MAX_NAME
], /* Name of printer */
1824 newname
[IPP_MAX_NAME
], /* New name of printer */
1825 *hptr
, /* Pointer into hostname */
1826 *sptr
; /* Pointer into ServerName */
1827 char local_make_model
[IPP_MAX_NAME
];
1828 /* Local make and model */
1829 cupsd_printer_t
*p
; /* Printer information */
1830 const char *ipp_options
, /* ipp-options value */
1831 *lease_duration
; /* lease-duration value */
1835 * Determine if the URI contains any illegal characters in it...
1838 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1839 (strncmp(resource
, "/printers/", 10) &&
1840 strncmp(resource
, "/classes/", 9)))
1842 cupsdLogMessage(CUPSD_LOG_ERROR
,
1843 "process_browse_data: Bad printer URI in browse data: %s",
1848 if (strchr(resource
, '?') ||
1849 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1850 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1852 cupsdLogMessage(CUPSD_LOG_ERROR
,
1853 "process_browse_data: Bad resource in browse data: %s",
1859 * OK, this isn't a local printer; add any remote options...
1862 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1864 if (BrowseRemoteOptions
)
1866 if (BrowseRemoteOptions
[0] == '?')
1869 * Override server-supplied options...
1872 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1874 else if (ipp_options
)
1877 * Combine the server and local options...
1880 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1881 BrowseRemoteOptions
);
1886 * Just use the local options...
1889 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1894 else if (ipp_options
)
1897 * Just use the server-supplied options...
1900 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1905 * See if we already have it listed in the Printers list, and add it if not...
1908 type
|= CUPS_PRINTER_REMOTE
;
1909 type
&= ~CUPS_PRINTER_IMPLICIT
;
1911 hptr
= strchr(host
, '.');
1912 sptr
= strchr(ServerName
, '.');
1914 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1917 * Strip the common domain name components...
1920 while (hptr
!= NULL
)
1922 if (!strcasecmp(hptr
, sptr
))
1928 hptr
= strchr(hptr
+ 1, '.');
1932 if (type
& CUPS_PRINTER_CLASS
)
1935 * Remote destination is a class...
1938 if (!strncmp(resource
, "/classes/", 9))
1939 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1944 *hptr
= '.'; /* Resource FQDN */
1946 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1948 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1950 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1953 * Nope, this isn't the same host; if the hostname isn't the local host,
1954 * add it to the other class and then find a class using the full host
1958 if (p
->type
& CUPS_PRINTER_REMOTE
)
1960 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1961 "Renamed remote class \"%s\" to \"%s@%s\"...",
1962 p
->name
, p
->name
, p
->hostname
);
1963 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1964 "Class \'%s\' deleted by directory services.",
1967 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1968 cupsdRenamePrinter(p
, newname
);
1970 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1971 "Class \'%s\' added by directory services.",
1977 else if (!p
->hostname
)
1980 * Hostname not set, so this must be a cached remote printer
1981 * that was created for a pending print job...
1984 cupsdSetString(&p
->hostname
, host
);
1985 cupsdSetString(&p
->uri
, uri
);
1986 cupsdSetString(&p
->device_uri
, uri
);
1993 * Use the short name for this shared class.
1996 strlcpy(name
, resource
+ 9, sizeof(name
));
1999 else if (p
&& !p
->hostname
)
2002 * Hostname not set, so this must be a cached remote printer
2003 * that was created for a pending print job...
2006 cupsdSetString(&p
->hostname
, host
);
2007 cupsdSetString(&p
->uri
, uri
);
2008 cupsdSetString(&p
->device_uri
, uri
);
2015 * Class doesn't exist; add it...
2018 p
= cupsdAddClass(name
);
2020 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2022 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2023 "Class \'%s\' added by directory services.", name
);
2026 * Force the URI to point to the real server...
2029 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2031 cupsdSetString(&p
->uri
, uri
);
2032 cupsdSetString(&p
->device_uri
, uri
);
2033 cupsdSetString(&p
->hostname
, host
);
2041 * Remote destination is a printer...
2044 if (!strncmp(resource
, "/printers/", 10))
2045 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2050 *hptr
= '.'; /* Resource FQDN */
2052 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2054 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2056 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2059 * Nope, this isn't the same host; if the hostname isn't the local host,
2060 * add it to the other printer and then find a printer using the full host
2064 if (p
->type
& CUPS_PRINTER_REMOTE
)
2066 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2067 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2068 p
->name
, p
->name
, p
->hostname
);
2069 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2070 "Printer \'%s\' deleted by directory services.",
2073 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2074 cupsdRenamePrinter(p
, newname
);
2076 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2077 "Printer \'%s\' added by directory services.",
2083 else if (!p
->hostname
)
2086 * Hostname not set, so this must be a cached remote printer
2087 * that was created for a pending print job...
2090 cupsdSetString(&p
->hostname
, host
);
2091 cupsdSetString(&p
->uri
, uri
);
2092 cupsdSetString(&p
->device_uri
, uri
);
2099 * Use the short name for this shared printer.
2102 strlcpy(name
, resource
+ 10, sizeof(name
));
2105 else if (p
&& !p
->hostname
)
2108 * Hostname not set, so this must be a cached remote printer
2109 * that was created for a pending print job...
2112 cupsdSetString(&p
->hostname
, host
);
2113 cupsdSetString(&p
->uri
, uri
);
2114 cupsdSetString(&p
->device_uri
, uri
);
2121 * Printer doesn't exist; add it...
2124 p
= cupsdAddPrinter(name
);
2126 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2127 "Printer \'%s\' added by directory services.", name
);
2129 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2132 * Force the URI to point to the real server...
2135 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2137 cupsdSetString(&p
->hostname
, host
);
2138 cupsdSetString(&p
->uri
, uri
);
2139 cupsdSetString(&p
->device_uri
, uri
);
2146 * Update the state...
2150 p
->browse_time
= time(NULL
);
2152 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2156 * Grab the lease-duration for the browse data; anything less then 1
2157 * second or more than 1 week gets the default BrowseTimeout...
2160 i
= atoi(lease_duration
);
2161 if (i
< 1 || i
> 604800)
2164 p
->browse_expire
= p
->browse_time
+ i
;
2167 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2169 if (type
& CUPS_PRINTER_REJECTING
)
2171 type
&= ~CUPS_PRINTER_REJECTING
;
2179 else if (!p
->accepting
)
2185 if (p
->type
!= type
)
2191 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2193 cupsdSetString(&p
->location
, location
);
2197 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2199 cupsdSetString(&p
->info
, info
);
2203 if (!make_model
|| !make_model
[0])
2205 if (type
& CUPS_PRINTER_CLASS
)
2206 snprintf(local_make_model
, sizeof(local_make_model
),
2207 "Remote Class on %s", host
);
2209 snprintf(local_make_model
, sizeof(local_make_model
),
2210 "Remote Printer on %s", host
);
2213 snprintf(local_make_model
, sizeof(local_make_model
),
2214 "%s on %s", make_model
, host
);
2216 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2218 cupsdSetString(&p
->make_model
, local_make_model
);
2224 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2227 * See if we need to update the attributes...
2230 if (p
->num_options
!= num_attrs
)
2234 for (i
= 0; i
< num_attrs
; i
++)
2235 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2236 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2237 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2246 * Free the old options...
2249 cupsFreeOptions(p
->num_options
, p
->options
);
2252 p
->num_options
= num_attrs
;
2255 if (type
& CUPS_PRINTER_DELETE
)
2257 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2258 "%s \'%s\' deleted by directory services.",
2259 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2261 cupsdExpireSubscriptions(p
, NULL
);
2263 cupsdDeletePrinter(p
, 1);
2264 cupsdUpdateImplicitClasses();
2268 cupsdSetPrinterAttrs(p
);
2269 cupsdUpdateImplicitClasses();
2273 * See if we have a default printer... If not, make the first network
2274 * default printer the default.
2277 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2280 * Find the first network default printer and use it...
2283 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2285 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2286 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2294 * Do auto-classing if needed...
2297 process_implicit_classes();
2300 * Update the printcap file...
2303 cupsdWritePrintcap();
2308 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2312 process_implicit_classes(void)
2314 int i
; /* Looping var */
2315 int update
; /* Update printer attributes? */
2316 char name
[IPP_MAX_NAME
], /* Name of printer */
2317 *hptr
; /* Pointer into hostname */
2318 cupsd_printer_t
*p
, /* Printer information */
2319 *pclass
, /* Printer class */
2320 *first
; /* First printer in class */
2321 int offset
, /* Offset of name */
2322 len
; /* Length of name */
2325 if (!ImplicitClasses
|| !Printers
)
2329 * Loop through all available printers and create classes as needed...
2332 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2333 update
= 0, pclass
= NULL
, first
= NULL
;
2335 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2338 * Skip implicit classes...
2341 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2348 * If len == 0, get the length of this printer name up to the "@"
2352 cupsArraySave(Printers
);
2355 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2356 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2359 * We have more than one printer with the same name; see if
2360 * we have a class, and if this printer is a member...
2363 if (pclass
&& strcasecmp(pclass
->name
, name
))
2366 cupsdSetPrinterAttrs(pclass
);
2372 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2375 * Need to add the class...
2378 pclass
= cupsdAddPrinter(name
);
2379 cupsArrayAdd(ImplicitPrinters
, pclass
);
2381 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2382 pclass
->accepting
= 1;
2383 pclass
->state
= IPP_PRINTER_IDLE
;
2385 cupsdSetString(&pclass
->location
, p
->location
);
2386 cupsdSetString(&pclass
->info
, p
->info
);
2390 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2392 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2393 "Implicit class \'%s\' added by directory services.",
2399 for (i
= 0; i
< pclass
->num_printers
; i
++)
2400 if (pclass
->printers
[i
] == first
)
2403 if (i
>= pclass
->num_printers
)
2405 first
->in_implicit_class
= 1;
2406 cupsdAddPrinterToClass(pclass
, first
);
2412 for (i
= 0; i
< pclass
->num_printers
; i
++)
2413 if (pclass
->printers
[i
] == p
)
2416 if (i
>= pclass
->num_printers
)
2418 p
->in_implicit_class
= 1;
2419 cupsdAddPrinterToClass(pclass
, p
);
2426 * First time around; just get name length and mark it as first
2430 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2431 len
= hptr
- p
->name
;
2433 len
= strlen(p
->name
);
2435 strncpy(name
, p
->name
, len
);
2439 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2440 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2443 * Can't use same name as a local printer; add "Any" to the
2444 * front of the name, unless we have explicitly disabled
2445 * the "ImplicitAnyClasses"...
2448 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2451 * Add "Any" to the class name...
2454 strcpy(name
, "Any");
2455 strncpy(name
+ 3, p
->name
, len
);
2456 name
[len
+ 3] = '\0';
2462 * Don't create an implicit class if we have a local printer
2463 * with the same name...
2467 cupsArrayRestore(Printers
);
2475 cupsArrayRestore(Printers
);
2479 * Update the last printer class as needed...
2482 if (pclass
&& update
)
2483 cupsdSetPrinterAttrs(pclass
);
2488 * 'send_cups_browse()' - Send new browsing information using the CUPS
2493 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2495 int i
; /* Looping var */
2496 cups_ptype_t type
; /* Printer type */
2497 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2498 int bytes
; /* Length of packet */
2499 char packet
[1453], /* Browse data packet */
2500 uri
[1024], /* Printer URI */
2501 location
[1024], /* printer-location */
2502 info
[1024], /* printer-info */
2504 /* printer-make-and-model */
2505 cupsd_netif_t
*iface
; /* Network interface */
2509 * Figure out the printer type value...
2512 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2515 type
|= CUPS_PRINTER_REJECTING
;
2517 if (p
== DefaultPrinter
)
2518 type
|= CUPS_PRINTER_DEFAULT
;
2521 * Remove quotes from printer-info, printer-location, and
2522 * printer-make-and-model attributes...
2525 dequote(location
, p
->location
, sizeof(location
));
2526 dequote(info
, p
->info
, sizeof(info
));
2529 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2530 else if (p
->type
& CUPS_PRINTER_CLASS
)
2532 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2533 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2535 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2538 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2540 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2543 * Send a packet to each browse address...
2546 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2550 * Send the browse packet to one or more interfaces...
2553 if (!strcmp(b
->iface
, "*"))
2556 * Send to all local interfaces...
2561 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2563 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2566 * Only send to local, IPv4 interfaces...
2569 if (!iface
->is_local
|| !iface
->port
||
2570 iface
->address
.addr
.sa_family
!= AF_INET
)
2573 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2574 iface
->hostname
, iface
->port
,
2575 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2578 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2579 type
, p
->state
, uri
, location
, info
, make_model
,
2580 p
->browse_attrs
? p
->browse_attrs
: "");
2582 bytes
= strlen(packet
);
2584 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2585 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2586 iface
->name
, packet
);
2588 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2590 sendto(BrowseSocket
, packet
, bytes
, 0,
2591 (struct sockaddr
*)&(iface
->broadcast
),
2592 httpAddrLength(&(iface
->broadcast
)));
2595 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2598 * Send to the named interface using the IPv4 address...
2602 if (strcmp(b
->iface
, iface
->name
))
2607 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2610 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2614 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2615 iface
->hostname
, iface
->port
,
2616 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2619 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2620 type
, p
->state
, uri
, location
, info
, make_model
,
2621 p
->browse_attrs
? p
->browse_attrs
: "");
2623 bytes
= strlen(packet
);
2625 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2626 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2627 iface
->name
, packet
);
2629 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2631 sendto(BrowseSocket
, packet
, bytes
, 0,
2632 (struct sockaddr
*)&(iface
->broadcast
),
2633 httpAddrLength(&(iface
->broadcast
)));
2640 * Send the browse packet to the indicated address using
2641 * the default server name...
2644 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2645 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2646 p
->browse_attrs
? p
->browse_attrs
: "");
2648 bytes
= strlen(packet
);
2649 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2650 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2652 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2653 (struct sockaddr
*)&(b
->to
),
2654 httpAddrLength(&(b
->to
))) <= 0)
2657 * Unable to send browse packet, so remove this address from the
2661 cupsdLogMessage(CUPSD_LOG_ERROR
,
2662 "cupsdSendBrowseList: sendto failed for browser "
2664 (int)(b
- Browsers
+ 1), strerror(errno
));
2667 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2676 #ifdef HAVE_OPENLDAP
2678 * 'send_ldap_browse()' - Send LDAP printer registrations.
2682 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2684 int i
; /* Looping var... */
2685 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
2686 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
2687 LDAPMessage
*res
; /* Search result token */
2688 char *cn_value
[2], /* Change records */
2694 typestring
[255], /* String to hold printer-type */
2695 filter
[256], /* Search filter for possible UPDATEs */
2696 dn
[1024]; /* DN of the printer we are adding */
2697 int rc
; /* LDAP status */
2698 static const char * const objectClass_values
[] =
2699 { /* The 3 objectClass's we use in */
2700 "top", /* our LDAP entries */
2706 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
2709 * Everything in ldap is ** so we fudge around it...
2712 sprintf(typestring
, "%u", p
->type
);
2714 cn_value
[0] = p
->name
;
2716 info
[0] = p
->info
? p
->info
: "Unknown";
2718 location
[0] = p
->location
? p
->location
: "Unknown";
2720 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
2721 make_model
[1] = NULL
;
2722 type
[0] = typestring
;
2727 snprintf(filter
, sizeof(filter
),
2728 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
2730 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2731 filter
, (char **)ldap_attrs
, 0, &res
);
2732 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
2735 mods
[0].mod_type
= "cn";
2736 mods
[0].mod_values
= cn_value
;
2737 mods
[1].mod_type
= "printerDescription";
2738 mods
[1].mod_values
= info
;
2739 mods
[2].mod_type
= "printerURI";
2740 mods
[2].mod_values
= uri
;
2741 mods
[3].mod_type
= "printerLocation";
2742 mods
[3].mod_values
= location
;
2743 mods
[4].mod_type
= "printerMakeAndModel";
2744 mods
[4].mod_values
= make_model
;
2745 mods
[5].mod_type
= "printerType";
2746 mods
[5].mod_values
= type
;
2747 mods
[6].mod_type
= "objectClass";
2748 mods
[6].mod_values
= (char **)objectClass_values
;
2750 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
2751 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
2753 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
2756 * Printer has already been registered, modify the current
2760 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2761 "send_ldap_browse: Replacing entry...");
2763 for (i
= 0; i
< 7; i
++)
2765 pmods
[i
] = mods
+ i
;
2766 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2770 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2771 cupsdLogMessage(CUPSD_LOG_ERROR
,
2772 "LDAP modify for %s failed with status %d: %s",
2773 p
->name
, rc
, ldap_err2string(rc
));
2778 * Printer has never been registered, add the current
2782 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2783 "send_ldap_browse: Adding entry...");
2785 for (i
= 0; i
< 7; i
++)
2787 pmods
[i
] = mods
+ i
;
2788 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
2792 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2793 cupsdLogMessage(CUPSD_LOG_ERROR
,
2794 "LDAP add for %s failed with status %d: %s",
2795 p
->name
, rc
, ldap_err2string(rc
));
2798 #endif /* HAVE_OPENLDAP */
2803 * 'send_slp_browse()' - Register the specified printer with SLP.
2807 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2809 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
2810 attrs
[8192], /* Printer attributes */
2811 finishings
[1024], /* Finishings to support */
2812 make_model
[IPP_MAX_NAME
* 2],
2813 /* Make and model, quoted */
2814 location
[IPP_MAX_NAME
* 2],
2815 /* Location, quoted */
2816 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
2817 *src
, /* Pointer to original string */
2818 *dst
; /* Pointer to destination string */
2819 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
2820 SLPError error
; /* SLP error, if any */
2823 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
2827 * Make the SLP service URL that conforms to the IANA
2828 * 'printer:' template.
2831 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2833 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
2836 * Figure out the finishings string...
2839 if (p
->type
& CUPS_PRINTER_STAPLE
)
2840 strcpy(finishings
, "staple");
2842 finishings
[0] = '\0';
2844 if (p
->type
& CUPS_PRINTER_BIND
)
2847 strlcat(finishings
, ",bind", sizeof(finishings
));
2849 strcpy(finishings
, "bind");
2852 if (p
->type
& CUPS_PRINTER_PUNCH
)
2855 strlcat(finishings
, ",punch", sizeof(finishings
));
2857 strcpy(finishings
, "punch");
2860 if (p
->type
& CUPS_PRINTER_COVER
)
2863 strlcat(finishings
, ",cover", sizeof(finishings
));
2865 strcpy(finishings
, "cover");
2868 if (p
->type
& CUPS_PRINTER_SORT
)
2871 strlcat(finishings
, ",sort", sizeof(finishings
));
2873 strcpy(finishings
, "sort");
2877 strcpy(finishings
, "none");
2880 * Quote any commas in the make and model, location, and info strings...
2883 for (src
= p
->make_model
, dst
= make_model
;
2884 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
2886 if (*src
== ',' || *src
== '\\' || *src
== ')')
2895 strcpy(make_model
, "Unknown");
2897 for (src
= p
->location
, dst
= location
;
2898 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
2900 if (*src
== ',' || *src
== '\\' || *src
== ')')
2909 strcpy(location
, "Unknown");
2911 for (src
= p
->info
, dst
= info
;
2912 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
2914 if (*src
== ',' || *src
== '\\' || *src
== ')')
2923 strcpy(info
, "Unknown");
2926 * Get the authentication value...
2929 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
2933 * Make the SLP attribute string list that conforms to
2934 * the IANA 'printer:' template.
2937 snprintf(attrs
, sizeof(attrs
),
2938 "(printer-uri-supported=%s),"
2939 "(uri-authentication-supported=%s>),"
2941 "(uri-security-supported=tls>),"
2943 "(uri-security-supported=none>),"
2944 #endif /* HAVE_SSL */
2945 "(printer-name=%s),"
2946 "(printer-location=%s),"
2947 "(printer-info=%s),"
2948 "(printer-more-info=%s),"
2949 "(printer-make-and-model=%s),"
2950 "(printer-type=%d),"
2951 "(charset-supported=utf-8),"
2952 "(natural-language-configured=%s),"
2953 "(natural-language-supported=de,en,es,fr,it),"
2954 "(color-supported=%s),"
2955 "(finishings-supported=%s),"
2956 "(sides-supported=one-sided%s),"
2957 "(multiple-document-jobs-supported=true)"
2958 "(ipp-versions-supported=1.0,1.1)",
2959 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
2960 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
2961 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
2963 p
->type
& CUPS_PRINTER_DUPLEX
?
2964 ",two-sided-long-edge,two-sided-short-edge" : "");
2966 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
2969 * Register the printer with the SLP server...
2972 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
2973 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
2975 if (error
!= SLP_OK
)
2976 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
2982 * 'slp_attr_callback()' - SLP attribute callback
2985 static SLPBoolean
/* O - SLP_TRUE for success */
2987 SLPHandle hslp
, /* I - SLP handle */
2988 const char *attrlist
, /* I - Attribute list */
2989 SLPError errcode
, /* I - Parsing status for this attr */
2990 void *cookie
) /* I - Current printer */
2992 char *tmp
= 0; /* Temporary string */
2993 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2994 /* Current printer */
2997 (void)hslp
; /* anti-compiler-warning-code */
3000 * Bail if there was an error
3003 if (errcode
!= SLP_OK
)
3007 * Parse the attrlist to obtain things needed to build CUPS browse packet
3010 memset(p
, 0, sizeof(cupsd_printer_t
));
3012 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3014 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3016 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3018 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3019 p
->type
= atoi(tmp
);
3021 p
->type
= CUPS_PRINTER_REMOTE
;
3023 cupsdClearString(&tmp
);
3030 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3034 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3036 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3039 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3041 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3044 * Make the SLP service URL that conforms to the IANA
3045 * 'printer:' template.
3048 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3051 * Deregister the printer...
3054 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3060 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3063 static int /* O - 0 on success */
3064 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3065 const char *tag
, /* I - Name of attribute */
3066 char **valbuf
) /* O - Value */
3068 char *ptr1
, /* Pointer into string */
3072 cupsdClearString(valbuf
);
3074 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3076 ptr1
+= strlen(tag
);
3078 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3084 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3085 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3088 * Dequote the value...
3091 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3092 if (*ptr1
== '\\' && ptr1
[1])
3093 _cups_strcpy(ptr1
, ptr1
+ 1);
3104 * 'slp_reg_callback()' - Empty SLPRegReport.
3108 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3109 SLPError errcode
, /* I - Error code, if any */
3110 void *cookie
) /* I - App data */
3121 * 'slp_url_callback()' - SLP service url callback
3124 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3126 SLPHandle hslp
, /* I - SLP handle */
3127 const char *srvurl
, /* I - URL of service */
3128 unsigned short lifetime
, /* I - Life of service */
3129 SLPError errcode
, /* I - Existing error code */
3130 void *cookie
) /* I - Pointer to service list */
3132 slpsrvurl_t
*s
, /* New service entry */
3133 **head
; /* Pointer to head of entry */
3137 * Let the compiler know we won't be using these vars...
3144 * Bail if there was an error
3147 if (errcode
!= SLP_OK
)
3151 * Grab the head of the list...
3154 head
= (slpsrvurl_t
**)cookie
;
3157 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3160 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3164 * Copy the SLP service URL...
3167 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3170 * Link the SLP service URL into the head of the list
3180 #endif /* HAVE_LIBSLP */
3184 * End of "$Id: dirsvc.c 6003 2006-10-02 16:26:04Z mike $".