2 * "$Id: dirsvc.c 6090 2006-11-14 16:35:27Z 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 * Don't load the cache if the CUPS remote protocol is disabled...
166 if (!(BrowseRemoteProtocols
& BROWSE_CUPS
))
168 cupsdLogMessage(CUPSD_LOG_DEBUG
,
169 "cupsdLoadRemoteCache: Not loading remote cache.");
174 * Open the remote.cache file...
177 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
178 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
182 * Read printer configurations until we hit EOF...
189 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
192 * Decode the directive...
195 if (!strcasecmp(line
, "<Printer") ||
196 !strcasecmp(line
, "<DefaultPrinter"))
199 * <Printer name> or <DefaultPrinter name>
202 if (p
== NULL
&& value
)
205 * Add the printer and a base file type...
208 cupsdLogMessage(CUPSD_LOG_DEBUG
,
209 "cupsdLoadRemoteCache: Loading printer %s...", value
);
211 if ((p
= cupsdFindDest(value
)) != NULL
)
213 if (p
->type
& CUPS_PRINTER_CLASS
)
215 cupsdLogMessage(CUPSD_LOG_WARN
,
216 "Cached remote printer \"%s\" conflicts with "
224 p
= cupsdAddPrinter(value
);
227 p
->state
= IPP_PRINTER_IDLE
;
228 p
->type
|= CUPS_PRINTER_REMOTE
;
229 p
->browse_time
= now
;
230 p
->browse_expire
= now
+ BrowseTimeout
;
233 * Set the default printer as needed...
236 if (!strcasecmp(line
, "<DefaultPrinter"))
241 cupsdLogMessage(CUPSD_LOG_ERROR
,
242 "Syntax error on line %d of remote.cache.", linenum
);
246 else if (!strcasecmp(line
, "<Class") ||
247 !strcasecmp(line
, "<DefaultClass"))
250 * <Class name> or <DefaultClass name>
253 if (p
== NULL
&& value
)
256 * Add the printer and a base file type...
259 cupsdLogMessage(CUPSD_LOG_DEBUG
,
260 "cupsdLoadRemoteCache: Loading class %s...", value
);
262 if ((p
= cupsdFindDest(value
)) != NULL
)
263 p
->type
= CUPS_PRINTER_CLASS
;
265 p
= cupsdAddClass(value
);
268 p
->state
= IPP_PRINTER_IDLE
;
269 p
->type
|= CUPS_PRINTER_REMOTE
;
270 p
->browse_time
= now
;
271 p
->browse_expire
= now
+ BrowseTimeout
;
274 * Set the default printer as needed...
277 if (!strcasecmp(line
, "<DefaultClass"))
282 cupsdLogMessage(CUPSD_LOG_ERROR
,
283 "Syntax error on line %d of remote.cache.", linenum
);
287 else if (!strcasecmp(line
, "</Printer>") ||
288 !strcasecmp(line
, "</Class>"))
293 * Close out the current printer...
296 cupsdSetPrinterAttrs(p
);
302 cupsdLogMessage(CUPSD_LOG_ERROR
,
303 "Syntax error on line %d of remote.cache.", linenum
);
309 cupsdLogMessage(CUPSD_LOG_ERROR
,
310 "Syntax error on line %d of remote.cache.", linenum
);
313 else if (!strcasecmp(line
, "Info"))
316 cupsdSetString(&p
->info
, value
);
318 else if (!strcasecmp(line
, "MakeModel"))
321 cupsdSetString(&p
->make_model
, value
);
323 else if (!strcasecmp(line
, "Location"))
326 cupsdSetString(&p
->location
, value
);
328 else if (!strcasecmp(line
, "DeviceURI"))
332 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
333 username
, sizeof(username
), host
, sizeof(host
), &port
,
334 resource
, sizeof(resource
));
336 cupsdSetString(&p
->hostname
, host
);
337 cupsdSetString(&p
->uri
, value
);
338 cupsdSetString(&p
->device_uri
, value
);
342 cupsdLogMessage(CUPSD_LOG_ERROR
,
343 "Syntax error on line %d of remote.cache.", linenum
);
347 else if (!strcasecmp(line
, "Option") && value
)
353 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
356 cupsdLogMessage(CUPSD_LOG_ERROR
,
357 "Syntax error on line %d of remote.cache.", linenum
);
360 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
362 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
366 else if (!strcasecmp(line
, "State"))
369 * Set the initial queue state...
372 if (value
&& !strcasecmp(value
, "idle"))
373 p
->state
= IPP_PRINTER_IDLE
;
374 else if (value
&& !strcasecmp(value
, "stopped"))
375 p
->state
= IPP_PRINTER_STOPPED
;
378 cupsdLogMessage(CUPSD_LOG_ERROR
,
379 "Syntax error on line %d of remote.cache.", linenum
);
383 else if (!strcasecmp(line
, "StateMessage"))
386 * Set the initial queue state message...
390 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
392 else if (!strcasecmp(line
, "Accepting"))
395 * Set the initial accepting state...
399 (!strcasecmp(value
, "yes") ||
400 !strcasecmp(value
, "on") ||
401 !strcasecmp(value
, "true")))
404 (!strcasecmp(value
, "no") ||
405 !strcasecmp(value
, "off") ||
406 !strcasecmp(value
, "false")))
410 cupsdLogMessage(CUPSD_LOG_ERROR
,
411 "Syntax error on line %d of remote.cache.", linenum
);
415 else if (!strcasecmp(line
, "Type"))
418 p
->type
= atoi(value
);
421 cupsdLogMessage(CUPSD_LOG_ERROR
,
422 "Syntax error on line %d of remote.cache.", linenum
);
426 else if (!strcasecmp(line
, "BrowseTime"))
430 time_t t
= atoi(value
);
432 if (t
> p
->browse_expire
)
433 p
->browse_expire
= t
;
437 cupsdLogMessage(CUPSD_LOG_ERROR
,
438 "Syntax error on line %d of remote.cache.", linenum
);
442 else if (!strcasecmp(line
, "JobSheets"))
445 * Set the initial job sheets...
450 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
455 cupsdSetString(&p
->job_sheets
[0], value
);
457 while (isspace(*valueptr
& 255))
462 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
467 cupsdSetString(&p
->job_sheets
[1], value
);
472 cupsdLogMessage(CUPSD_LOG_ERROR
,
473 "Syntax error on line %d of remote.cache.", linenum
);
477 else if (!strcasecmp(line
, "AllowUser"))
482 cupsdAddPrinterUser(p
, value
);
486 cupsdLogMessage(CUPSD_LOG_ERROR
,
487 "Syntax error on line %d of remote.cache.", linenum
);
491 else if (!strcasecmp(line
, "DenyUser"))
496 cupsdAddPrinterUser(p
, value
);
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Syntax error on line %d of remote.cache.", linenum
);
508 * Something else we don't understand...
511 cupsdLogMessage(CUPSD_LOG_ERROR
,
512 "Unknown configuration directive %s on line %d of remote.cache.",
520 * Do auto-classing if needed...
523 process_implicit_classes();
528 * 'cupsdRestartPolling()' - Restart polling servers as needed.
532 cupsdRestartPolling(void)
534 int i
; /* Looping var */
535 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
538 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
540 kill(pollp
->pid
, SIGHUP
);
545 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
549 cupsdSaveRemoteCache(void)
551 int i
; /* Looping var */
552 cups_file_t
*fp
; /* printers.conf file */
553 char temp
[1024]; /* Temporary string */
554 cupsd_printer_t
*printer
; /* Current printer class */
555 time_t curtime
; /* Current time */
556 struct tm
*curdate
; /* Current date */
557 cups_option_t
*option
; /* Current option */
561 * Create the remote.cache file...
564 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
566 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
568 cupsdLogMessage(CUPSD_LOG_ERROR
,
569 "Unable to save remote.cache - %s", strerror(errno
));
573 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
576 * Restrict access to the file...
579 fchown(cupsFileNumber(fp
), getuid(), Group
);
580 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
583 * Write a small header to the file...
586 curtime
= time(NULL
);
587 curdate
= localtime(&curtime
);
588 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
590 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
591 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
594 * Write each local printer known to the system...
597 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
599 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
602 * Skip local destinations...
605 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
609 * Write printers as needed...
612 if (printer
== DefaultPrinter
)
613 cupsFilePuts(fp
, "<Default");
615 cupsFilePutChar(fp
, '<');
617 if (printer
->type
& CUPS_PRINTER_CLASS
)
618 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
620 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
622 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
624 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
627 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
629 if (printer
->make_model
)
630 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
632 if (printer
->location
)
633 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
635 if (printer
->device_uri
)
636 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
638 if (printer
->state
== IPP_PRINTER_STOPPED
)
640 cupsFilePuts(fp
, "State Stopped\n");
641 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
644 cupsFilePuts(fp
, "State Idle\n");
646 if (printer
->accepting
)
647 cupsFilePuts(fp
, "Accepting Yes\n");
649 cupsFilePuts(fp
, "Accepting No\n");
651 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
652 printer
->job_sheets
[1]);
654 for (i
= 0; i
< printer
->num_users
; i
++)
655 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
658 for (i
= printer
->num_options
, option
= printer
->options
;
661 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
663 if (printer
->type
& CUPS_PRINTER_CLASS
)
664 cupsFilePuts(fp
, "</Class>\n");
666 cupsFilePuts(fp
, "</Printer>\n");
674 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
678 cupsdSendBrowseDelete(
679 cupsd_printer_t
*p
) /* I - Printer to delete */
682 * Only announce if browsing is enabled and this is a local queue...
685 if (!Browsing
|| !p
->shared
||
686 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
690 * First mark the printer for deletion...
693 p
->type
|= CUPS_PRINTER_DELETE
;
696 * Announce the deletion...
699 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
702 if ((BrowseLocalProtocols
& BROWSE_SLP
) && BrowseSLPHandle
)
703 slp_dereg_printer(p
);
704 #endif /* HAVE_LIBSLP */
709 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
713 cupsdSendBrowseList(void)
715 int count
; /* Number of dests to update */
716 cupsd_printer_t
*p
; /* Current printer */
717 time_t ut
, /* Minimum update time */
718 to
; /* Timeout time */
721 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
725 * Compute the update and timeout times...
729 ut
= to
- BrowseInterval
;
732 * Figure out how many printers need an update...
735 if (BrowseInterval
> 0)
737 int max_count
; /* Maximum number to update */
741 * Throttle the number of printers we'll be updating this time
742 * around based on the number of queues that need updating and
743 * the maximum number of queues to update each second...
746 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
748 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
749 count
< max_count
&& p
!= NULL
;
750 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
751 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
752 p
->shared
&& p
->browse_time
< ut
)
756 * Loop through all of the printers and send local updates as needed...
760 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
762 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
766 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
769 * Check for wraparound...
773 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
777 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
780 else if (p
->browse_time
< ut
)
783 * Need to send an update...
788 p
->browse_time
= time(NULL
);
790 if (BrowseLocalProtocols
& BROWSE_CUPS
)
794 if (BrowseLocalProtocols
& BROWSE_SLP
)
796 #endif /* HAVE_LIBSLP */
799 if (BrowseLocalProtocols
& BROWSE_LDAP
)
801 #endif /* HAVE_LDAP */
806 * Save where we left off so that all printers get updated...
813 * Loop through all of the printers and send local updates as needed...
816 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
818 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
821 * If this is a remote queue, see if it needs to be timed out...
824 if (p
->type
& CUPS_PRINTER_REMOTE
)
826 if (p
->browse_expire
< to
)
828 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
829 "%s \'%s\' deleted by directory services (timeout).",
830 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
833 cupsdLogMessage(CUPSD_LOG_DEBUG
,
834 "Remote destination \"%s\" has timed out; "
838 cupsArraySave(Printers
);
839 cupsdDeletePrinter(p
, 1);
840 cupsArrayRestore(Printers
);
848 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
852 cupsdStartBrowsing(void)
854 int val
; /* Socket option value */
855 struct sockaddr_in addr
; /* Broadcast address */
860 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
863 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
865 if (BrowseSocket
< 0)
868 * Create the broadcast socket...
871 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
873 cupsdLogMessage(CUPSD_LOG_ERROR
,
874 "cupsdStartBrowsing: Unable to create broadcast "
875 "socket - %s.", strerror(errno
));
876 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
877 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
882 * Bind the socket to browse port...
885 memset(&addr
, 0, sizeof(addr
));
886 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
887 addr
.sin_family
= AF_INET
;
888 addr
.sin_port
= htons(BrowsePort
);
890 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
892 cupsdLogMessage(CUPSD_LOG_ERROR
,
893 "cupsdStartBrowsing: Unable to bind broadcast "
894 "socket - %s.", strerror(errno
));
897 closesocket(BrowseSocket
);
903 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
904 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
910 * Set the "broadcast" flag...
914 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
916 cupsdLogMessage(CUPSD_LOG_ERROR
,
917 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
921 closesocket(BrowseSocket
);
927 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
928 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
933 * Close the socket on exec...
936 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
939 * Finally, add the socket to the input selection set as needed...
942 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
945 * We only listen if we want remote printers...
948 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
949 "cupsdStartBrowsing: Adding fd %d to InputSet...",
952 FD_SET(BrowseSocket
, InputSet
);
959 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
965 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
967 cupsdLogMessage(CUPSD_LOG_ERROR
,
968 "Unable to open an SLP handle; disabling SLP browsing!");
969 BrowseLocalProtocols
&= ~BROWSE_SLP
;
970 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
973 BrowseSLPRefresh
= 0;
976 BrowseSLPHandle
= NULL
;
977 #endif /* HAVE_LIBSLP */
980 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
984 cupsdLogMessage(CUPSD_LOG_ERROR
,
985 "Need to set BrowseLDAPDN to use LDAP browsing!");
986 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
987 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
992 * Open LDAP handle...
995 int rc
; /* LDAP API status */
996 int version
= 3; /* LDAP version */
997 struct berval bv
= {0, ""}; /* SASL bind value */
1001 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1004 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1005 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
1007 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
1009 if (rc
!= LDAP_SUCCESS
)
1011 cupsdLogMessage(CUPSD_LOG_ERROR
,
1012 "Unable to initialize LDAP; disabling LDAP browsing!");
1013 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1014 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1016 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1017 (const void *)&version
) != LDAP_SUCCESS
)
1019 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1020 BrowseLDAPHandle
= NULL
;
1021 cupsdLogMessage(CUPSD_LOG_ERROR
,
1022 "Unable to set LDAP protocol version; "
1023 "disabling LDAP browsing!");
1024 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1025 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1029 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1030 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1033 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1034 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1036 if (rc
!= LDAP_SUCCESS
)
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "Unable to bind to LDAP server; "
1040 "disabling LDAP browsing!");
1041 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1042 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1043 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1048 BrowseLDAPRefresh
= 0;
1050 #endif /* HAVE_OPENLDAP */
1055 * 'cupsdStartPolling()' - Start polling servers as needed.
1059 cupsdStartPolling(void)
1061 int i
; /* Looping var */
1062 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1063 char polld
[1024]; /* Poll daemon path */
1064 char sport
[255]; /* Server port */
1065 char bport
[255]; /* Browser port */
1066 char interval
[255]; /* Poll interval */
1067 int statusfds
[2]; /* Status pipe */
1068 char *argv
[6]; /* Arguments */
1069 char *envp
[100]; /* Environment */
1073 * Don't do anything if we aren't polling...
1076 if (NumPolled
== 0 || BrowseSocket
< 0)
1079 PollStatusBuffer
= NULL
;
1084 * Setup string arguments for polld, port and interval options.
1087 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1089 sprintf(bport
, "%d", BrowsePort
);
1092 sprintf(interval
, "%d", BrowseInterval
);
1094 strcpy(interval
, "30");
1096 argv
[0] = "cups-polld";
1102 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1105 * Create a pipe that receives the status messages from each
1109 if (cupsdOpenPipe(statusfds
))
1111 cupsdLogMessage(CUPSD_LOG_ERROR
,
1112 "Unable to create polling status pipes - %s.",
1115 PollStatusBuffer
= NULL
;
1119 PollPipe
= statusfds
[0];
1120 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1123 * Run each polling daemon, redirecting stderr to the polling pipe...
1126 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1128 sprintf(sport
, "%d", pollp
->port
);
1130 argv
[1] = pollp
->hostname
;
1132 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1133 0, &(pollp
->pid
)) < 0)
1135 cupsdLogMessage(CUPSD_LOG_ERROR
,
1136 "cupsdStartPolling: Unable to fork polling daemon - %s",
1142 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1143 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1144 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1147 close(statusfds
[1]);
1150 * Finally, add the pipe to the input selection set...
1153 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1154 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1156 FD_SET(PollPipe
, InputSet
);
1161 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1165 cupsdStopBrowsing(void)
1167 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1170 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1174 * Close the socket and remove it from the input selection set.
1178 closesocket(BrowseSocket
);
1180 close(BrowseSocket
);
1183 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1184 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1187 FD_CLR(BrowseSocket
, InputSet
);
1192 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1196 * Close SLP handle...
1199 SLPClose(BrowseSLPHandle
);
1200 BrowseSLPHandle
= NULL
;
1202 #endif /* HAVE_LIBSLP */
1204 #ifdef HAVE_OPENLDAP
1205 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1208 ldap_unbind(BrowseLDAPHandle
);
1209 BrowseLDAPHandle
= NULL
;
1211 #endif /* HAVE_OPENLDAP */
1216 * 'cupsdStopPolling()' - Stop polling servers as needed.
1220 cupsdStopPolling(void)
1222 int i
; /* Looping var */
1223 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1228 cupsdStatBufDelete(PollStatusBuffer
);
1231 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1232 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1233 FD_CLR(PollPipe
, InputSet
);
1236 PollStatusBuffer
= NULL
;
1239 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1241 cupsdEndProcess(pollp
->pid
, 0);
1246 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1250 cupsdUpdateCUPSBrowse(void)
1252 int i
; /* Looping var */
1253 int auth
; /* Authorization status */
1254 int len
; /* Length of name string */
1255 int bytes
; /* Number of bytes left */
1256 char packet
[1541], /* Broadcast packet */
1257 *pptr
; /* Pointer into packet */
1258 socklen_t srclen
; /* Length of source address */
1259 http_addr_t srcaddr
; /* Source address */
1260 char srcname
[1024]; /* Source hostname */
1261 unsigned address
[4]; /* Source address */
1262 unsigned type
; /* Printer type */
1263 unsigned state
; /* Printer state */
1264 char uri
[HTTP_MAX_URI
], /* Printer URI */
1265 host
[HTTP_MAX_URI
], /* Host portion of URI */
1266 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1267 info
[IPP_MAX_NAME
], /* Information string */
1268 location
[IPP_MAX_NAME
], /* Location string */
1269 make_model
[IPP_MAX_NAME
];/* Make and model string */
1270 int num_attrs
; /* Number of attributes */
1271 cups_option_t
*attrs
; /* Attributes */
1275 * Read a packet from the browse socket...
1278 srclen
= sizeof(srcaddr
);
1279 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1280 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1283 * "Connection refused" is returned under Linux if the destination port
1284 * or address is unreachable from a previous sendto(); check for the
1285 * error here and ignore it for now...
1288 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1290 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1292 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1294 cupsdStopBrowsing();
1301 packet
[bytes
] = '\0';
1304 * If we're about to sleep, ignore incoming browse packets.
1311 * Figure out where it came from...
1315 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1317 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1318 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1319 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1320 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1323 #endif /* AF_INET6 */
1328 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1331 if (HostNameLookups
)
1332 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1334 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1336 len
= strlen(srcname
);
1344 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1347 * Access from localhost (127.0.0.1) is always allowed...
1355 * Do authorization checks on the domain/address...
1358 switch (BrowseACL
->order_type
)
1361 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1364 case AUTH_ALLOW
: /* Order Deny,Allow */
1367 if (cupsdCheckAuth(address
, srcname
, len
,
1368 BrowseACL
->num_deny
, BrowseACL
->deny
))
1371 if (cupsdCheckAuth(address
, srcname
, len
,
1372 BrowseACL
->num_allow
, BrowseACL
->allow
))
1376 case AUTH_DENY
: /* Order Allow,Deny */
1379 if (cupsdCheckAuth(address
, srcname
, len
,
1380 BrowseACL
->num_allow
, BrowseACL
->allow
))
1383 if (cupsdCheckAuth(address
, srcname
, len
,
1384 BrowseACL
->num_deny
, BrowseACL
->deny
))
1393 if (auth
== AUTH_DENY
)
1395 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1396 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1401 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1402 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1409 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1411 cupsdLogMessage(CUPSD_LOG_WARN
,
1412 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1416 strcpy(location
, "Location Unknown");
1417 strcpy(info
, "No Information Available");
1418 make_model
[0] = '\0';
1422 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1425 * Have extended information; can't use sscanf for it because not all
1426 * sscanf's allow empty strings with %[^\"]...
1429 for (i
= 0, pptr
++;
1430 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1432 location
[i
] = *pptr
;
1440 while (*pptr
&& isspace(*pptr
& 255))
1445 for (i
= 0, pptr
++;
1446 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1455 while (*pptr
&& isspace(*pptr
& 255))
1460 for (i
= 0, pptr
++;
1461 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1463 make_model
[i
] = *pptr
;
1468 make_model
[i
] = '\0';
1471 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1477 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1478 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1479 type
, state
, uri
, location
, info
, make_model
));
1482 * Pull the URI apart to see if this is a local or remote printer...
1485 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1487 cupsFreeOptions(num_attrs
, attrs
);
1495 for (i
= 0; i
< NumRelays
; i
++)
1496 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1497 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1498 (struct sockaddr
*)&(Relays
[i
].to
),
1499 httpAddrLength(&(Relays
[i
].to
))) <= 0)
1501 cupsdLogMessage(CUPSD_LOG_ERROR
,
1502 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1503 i
+ 1, strerror(errno
));
1504 cupsFreeOptions(num_attrs
, attrs
);
1509 * Process the browse data...
1512 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1513 (ipp_pstate_t
)state
, location
, info
, make_model
,
1518 #ifdef HAVE_OPENLDAP
1520 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1524 cupsdUpdateLDAPBrowse(void)
1526 char uri
[HTTP_MAX_URI
], /* Printer URI */
1527 host
[HTTP_MAX_URI
], /* Hostname */
1528 resource
[HTTP_MAX_URI
], /* Resource path */
1529 location
[1024], /* Printer location */
1530 info
[1024], /* Printer information */
1531 make_model
[1024], /* Printer make and model */
1532 **value
; /* Holds the returned data from LDAP */
1533 int type
; /* Printer type */
1534 int rc
; /* LDAP status */
1535 int limit
; /* Size limit */
1536 LDAPMessage
*res
, /* LDAP search results */
1537 *e
; /* Current entry from search */
1541 * Search for printers...
1544 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1546 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1548 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1549 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1550 if (rc
!= LDAP_SUCCESS
)
1552 cupsdLogMessage(CUPSD_LOG_ERROR
,
1553 "LDAP search returned error %d: %s", rc
,
1554 ldap_err2string(rc
));
1558 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1559 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1564 * Loop through the available printers...
1567 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1569 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1572 * Get the required values from this entry...
1575 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1576 "printerDescription")) == NULL
)
1579 strlcpy(info
, *value
, sizeof(info
));
1580 ldap_value_free(value
);
1582 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1583 "printerLocation")) == NULL
)
1586 strlcpy(location
, *value
, sizeof(location
));
1587 ldap_value_free(value
);
1589 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1590 "printerMakeAndModel")) == NULL
)
1593 strlcpy(make_model
, *value
, sizeof(make_model
));
1594 ldap_value_free(value
);
1596 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1597 "printerType")) == NULL
)
1600 type
= atoi(*value
);
1601 ldap_value_free(value
);
1603 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1604 "printerURI")) == NULL
)
1607 strlcpy(uri
, *value
, sizeof(uri
));
1608 ldap_value_free(value
);
1611 * Process the entry as browse data...
1614 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1615 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1616 location
, info
, make_model
, 0, NULL
);
1620 #endif /* HAVE_OPENLDAP */
1624 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1628 cupsdUpdatePolling(void)
1630 char *ptr
, /* Pointer to end of line in buffer */
1631 message
[1024]; /* Pointer to message text */
1632 int loglevel
; /* Log level for message */
1635 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1636 message
, sizeof(message
))) != NULL
)
1637 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1640 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
1643 * All polling processes have died; stop polling...
1646 cupsdLogMessage(CUPSD_LOG_ERROR
,
1647 "cupsdUpdatePolling: all polling processes have exited!");
1655 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1659 cupsdUpdateSLPBrowse(void)
1661 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1662 *next
; /* Next service in list */
1663 cupsd_printer_t p
; /* Printer information */
1664 const char *uri
; /* Pointer to printer URI */
1665 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1666 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1670 * Reset the refresh time...
1673 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1676 * Poll for remote printers using SLP...
1681 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1682 slp_url_callback
, &s
);
1685 * Loop through the list of available printers...
1691 * Save the "next" pointer...
1697 * Load a cupsd_printer_t structure with the SLP service attributes...
1700 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1703 * Process this printer entry...
1706 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1708 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1711 * Pull the URI apart to see if this is a local or remote printer...
1714 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1715 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1716 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1720 * Free this listing...
1723 cupsdClearString(&p
.info
);
1724 cupsdClearString(&p
.location
);
1725 cupsdClearString(&p
.make_model
);
1730 #endif /* HAVE_LIBSLP */
1734 * 'dequote()' - Remote quotes from a string.
1737 static char * /* O - Dequoted string */
1738 dequote(char *d
, /* I - Destination string */
1739 const char *s
, /* I - Source string */
1740 int dlen
) /* I - Destination length */
1742 char *dptr
; /* Pointer into destination */
1747 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1764 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1767 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1768 is_local_queue(const char *uri
, /* I - Printer URI */
1769 char *host
, /* O - Host string */
1770 int hostlen
, /* I - Length of host buffer */
1771 char *resource
, /* O - Resource string */
1772 int resourcelen
) /* I - Length of resource buffer */
1774 char scheme
[32], /* Scheme portion of URI */
1775 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1776 int port
; /* Port portion of URI */
1777 cupsd_netif_t
*iface
; /* Network interface */
1781 * Pull the URI apart to see if this is a local or remote printer...
1784 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1785 username
, sizeof(username
), host
, hostlen
, &port
,
1786 resource
, resourcelen
) < HTTP_URI_OK
)
1789 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1792 * Check for local server addresses...
1795 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1800 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1802 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1803 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1807 * If we get here, the printer is remote...
1815 * 'process_browse_data()' - Process new browse data.
1819 process_browse_data(
1820 const char *uri
, /* I - URI of printer/class */
1821 const char *host
, /* I - Hostname */
1822 const char *resource
, /* I - Resource path */
1823 cups_ptype_t type
, /* I - Printer type */
1824 ipp_pstate_t state
, /* I - Printer state */
1825 const char *location
, /* I - Printer location */
1826 const char *info
, /* I - Printer information */
1827 const char *make_model
, /* I - Printer make and model */
1828 int num_attrs
, /* I - Number of attributes */
1829 cups_option_t
*attrs
) /* I - Attributes */
1831 int i
; /* Looping var */
1832 int update
; /* Update printer attributes? */
1833 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1834 name
[IPP_MAX_NAME
], /* Name of printer */
1835 newname
[IPP_MAX_NAME
], /* New name of printer */
1836 *hptr
, /* Pointer into hostname */
1837 *sptr
; /* Pointer into ServerName */
1838 char local_make_model
[IPP_MAX_NAME
];
1839 /* Local make and model */
1840 cupsd_printer_t
*p
; /* Printer information */
1841 const char *ipp_options
, /* ipp-options value */
1842 *lease_duration
; /* lease-duration value */
1846 * Determine if the URI contains any illegal characters in it...
1849 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1850 (strncmp(resource
, "/printers/", 10) &&
1851 strncmp(resource
, "/classes/", 9)))
1853 cupsdLogMessage(CUPSD_LOG_ERROR
,
1854 "process_browse_data: Bad printer URI in browse data: %s",
1859 if (strchr(resource
, '?') ||
1860 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1861 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1863 cupsdLogMessage(CUPSD_LOG_ERROR
,
1864 "process_browse_data: Bad resource in browse data: %s",
1870 * OK, this isn't a local printer; add any remote options...
1873 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1875 if (BrowseRemoteOptions
)
1877 if (BrowseRemoteOptions
[0] == '?')
1880 * Override server-supplied options...
1883 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1885 else if (ipp_options
)
1888 * Combine the server and local options...
1891 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1892 BrowseRemoteOptions
);
1897 * Just use the local options...
1900 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1905 else if (ipp_options
)
1908 * Just use the server-supplied options...
1911 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1916 * See if we already have it listed in the Printers list, and add it if not...
1919 type
|= CUPS_PRINTER_REMOTE
;
1920 type
&= ~CUPS_PRINTER_IMPLICIT
;
1922 hptr
= strchr(host
, '.');
1923 sptr
= strchr(ServerName
, '.');
1925 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1928 * Strip the common domain name components...
1931 while (hptr
!= NULL
)
1933 if (!strcasecmp(hptr
, sptr
))
1939 hptr
= strchr(hptr
+ 1, '.');
1943 if (type
& CUPS_PRINTER_CLASS
)
1946 * Remote destination is a class...
1949 if (!strncmp(resource
, "/classes/", 9))
1950 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1955 *hptr
= '.'; /* Resource FQDN */
1957 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1959 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1961 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1964 * Nope, this isn't the same host; if the hostname isn't the local host,
1965 * add it to the other class and then find a class using the full host
1969 if (p
->type
& CUPS_PRINTER_REMOTE
)
1971 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1972 "Renamed remote class \"%s\" to \"%s@%s\"...",
1973 p
->name
, p
->name
, p
->hostname
);
1974 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1975 "Class \'%s\' deleted by directory services.",
1978 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1979 cupsdRenamePrinter(p
, newname
);
1981 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1982 "Class \'%s\' added by directory services.",
1988 else if (!p
->hostname
)
1991 * Hostname not set, so this must be a cached remote printer
1992 * that was created for a pending print job...
1995 cupsdSetString(&p
->hostname
, host
);
1996 cupsdSetString(&p
->uri
, uri
);
1997 cupsdSetString(&p
->device_uri
, uri
);
2004 * Use the short name for this shared class.
2007 strlcpy(name
, resource
+ 9, sizeof(name
));
2010 else if (p
&& !p
->hostname
)
2013 * Hostname not set, so this must be a cached remote printer
2014 * that was created for a pending print job...
2017 cupsdSetString(&p
->hostname
, host
);
2018 cupsdSetString(&p
->uri
, uri
);
2019 cupsdSetString(&p
->device_uri
, uri
);
2026 * Class doesn't exist; add it...
2029 p
= cupsdAddClass(name
);
2031 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2033 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2034 "Class \'%s\' added by directory services.", name
);
2037 * Force the URI to point to the real server...
2040 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2042 cupsdSetString(&p
->uri
, uri
);
2043 cupsdSetString(&p
->device_uri
, uri
);
2044 cupsdSetString(&p
->hostname
, host
);
2052 * Remote destination is a printer...
2055 if (!strncmp(resource
, "/printers/", 10))
2056 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2061 *hptr
= '.'; /* Resource FQDN */
2063 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2065 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2067 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2070 * Nope, this isn't the same host; if the hostname isn't the local host,
2071 * add it to the other printer and then find a printer using the full host
2075 if (p
->type
& CUPS_PRINTER_REMOTE
)
2077 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2078 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2079 p
->name
, p
->name
, p
->hostname
);
2080 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2081 "Printer \'%s\' deleted by directory services.",
2084 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2085 cupsdRenamePrinter(p
, newname
);
2087 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2088 "Printer \'%s\' added by directory services.",
2094 else if (!p
->hostname
)
2097 * Hostname not set, so this must be a cached remote printer
2098 * that was created for a pending print job...
2101 cupsdSetString(&p
->hostname
, host
);
2102 cupsdSetString(&p
->uri
, uri
);
2103 cupsdSetString(&p
->device_uri
, uri
);
2110 * Use the short name for this shared printer.
2113 strlcpy(name
, resource
+ 10, sizeof(name
));
2116 else if (p
&& !p
->hostname
)
2119 * Hostname not set, so this must be a cached remote printer
2120 * that was created for a pending print job...
2123 cupsdSetString(&p
->hostname
, host
);
2124 cupsdSetString(&p
->uri
, uri
);
2125 cupsdSetString(&p
->device_uri
, uri
);
2132 * Printer doesn't exist; add it...
2135 p
= cupsdAddPrinter(name
);
2137 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2138 "Printer \'%s\' added by directory services.", name
);
2140 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2143 * Force the URI to point to the real server...
2146 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2148 cupsdSetString(&p
->hostname
, host
);
2149 cupsdSetString(&p
->uri
, uri
);
2150 cupsdSetString(&p
->device_uri
, uri
);
2157 * Update the state...
2161 p
->browse_time
= time(NULL
);
2163 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2167 * Grab the lease-duration for the browse data; anything less then 1
2168 * second or more than 1 week gets the default BrowseTimeout...
2171 i
= atoi(lease_duration
);
2172 if (i
< 1 || i
> 604800)
2175 p
->browse_expire
= p
->browse_time
+ i
;
2178 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2180 if (type
& CUPS_PRINTER_REJECTING
)
2182 type
&= ~CUPS_PRINTER_REJECTING
;
2190 else if (!p
->accepting
)
2196 if (p
->type
!= type
)
2202 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2204 cupsdSetString(&p
->location
, location
);
2208 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2210 cupsdSetString(&p
->info
, info
);
2214 if (!make_model
|| !make_model
[0])
2216 if (type
& CUPS_PRINTER_CLASS
)
2217 snprintf(local_make_model
, sizeof(local_make_model
),
2218 "Remote Class on %s", host
);
2220 snprintf(local_make_model
, sizeof(local_make_model
),
2221 "Remote Printer on %s", host
);
2224 snprintf(local_make_model
, sizeof(local_make_model
),
2225 "%s on %s", make_model
, host
);
2227 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2229 cupsdSetString(&p
->make_model
, local_make_model
);
2235 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2238 * See if we need to update the attributes...
2241 if (p
->num_options
!= num_attrs
)
2245 for (i
= 0; i
< num_attrs
; i
++)
2246 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2247 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2248 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2257 * Free the old options...
2260 cupsFreeOptions(p
->num_options
, p
->options
);
2263 p
->num_options
= num_attrs
;
2266 if (type
& CUPS_PRINTER_DELETE
)
2268 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2269 "%s \'%s\' deleted by directory services.",
2270 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2272 cupsdExpireSubscriptions(p
, NULL
);
2274 cupsdDeletePrinter(p
, 1);
2275 cupsdUpdateImplicitClasses();
2279 cupsdSetPrinterAttrs(p
);
2280 cupsdUpdateImplicitClasses();
2284 * See if we have a default printer... If not, make the first network
2285 * default printer the default.
2288 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2291 * Find the first network default printer and use it...
2294 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2296 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2297 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2305 * Do auto-classing if needed...
2308 process_implicit_classes();
2311 * Update the printcap file...
2314 cupsdWritePrintcap();
2319 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2323 process_implicit_classes(void)
2325 int i
; /* Looping var */
2326 int update
; /* Update printer attributes? */
2327 char name
[IPP_MAX_NAME
], /* Name of printer */
2328 *hptr
; /* Pointer into hostname */
2329 cupsd_printer_t
*p
, /* Printer information */
2330 *pclass
, /* Printer class */
2331 *first
; /* First printer in class */
2332 int offset
, /* Offset of name */
2333 len
; /* Length of name */
2336 if (!ImplicitClasses
|| !Printers
)
2340 * Loop through all available printers and create classes as needed...
2343 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2344 update
= 0, pclass
= NULL
, first
= NULL
;
2346 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2349 * Skip implicit classes...
2352 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2359 * If len == 0, get the length of this printer name up to the "@"
2363 cupsArraySave(Printers
);
2366 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2367 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2370 * We have more than one printer with the same name; see if
2371 * we have a class, and if this printer is a member...
2374 if (pclass
&& strcasecmp(pclass
->name
, name
))
2377 cupsdSetPrinterAttrs(pclass
);
2383 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2386 * Need to add the class...
2389 pclass
= cupsdAddPrinter(name
);
2390 cupsArrayAdd(ImplicitPrinters
, pclass
);
2392 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2393 pclass
->accepting
= 1;
2394 pclass
->state
= IPP_PRINTER_IDLE
;
2396 cupsdSetString(&pclass
->location
, p
->location
);
2397 cupsdSetString(&pclass
->info
, p
->info
);
2401 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2403 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2404 "Implicit class \'%s\' added by directory services.",
2410 for (i
= 0; i
< pclass
->num_printers
; i
++)
2411 if (pclass
->printers
[i
] == first
)
2414 if (i
>= pclass
->num_printers
)
2416 first
->in_implicit_class
= 1;
2417 cupsdAddPrinterToClass(pclass
, first
);
2423 for (i
= 0; i
< pclass
->num_printers
; i
++)
2424 if (pclass
->printers
[i
] == p
)
2427 if (i
>= pclass
->num_printers
)
2429 p
->in_implicit_class
= 1;
2430 cupsdAddPrinterToClass(pclass
, p
);
2437 * First time around; just get name length and mark it as first
2441 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2442 len
= hptr
- p
->name
;
2444 len
= strlen(p
->name
);
2446 strncpy(name
, p
->name
, len
);
2450 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2451 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2454 * Can't use same name as a local printer; add "Any" to the
2455 * front of the name, unless we have explicitly disabled
2456 * the "ImplicitAnyClasses"...
2459 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2462 * Add "Any" to the class name...
2465 strcpy(name
, "Any");
2466 strncpy(name
+ 3, p
->name
, len
);
2467 name
[len
+ 3] = '\0';
2473 * Don't create an implicit class if we have a local printer
2474 * with the same name...
2478 cupsArrayRestore(Printers
);
2486 cupsArrayRestore(Printers
);
2490 * Update the last printer class as needed...
2493 if (pclass
&& update
)
2494 cupsdSetPrinterAttrs(pclass
);
2499 * 'send_cups_browse()' - Send new browsing information using the CUPS
2504 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2506 int i
; /* Looping var */
2507 cups_ptype_t type
; /* Printer type */
2508 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2509 int bytes
; /* Length of packet */
2510 char packet
[1453], /* Browse data packet */
2511 uri
[1024], /* Printer URI */
2512 location
[1024], /* printer-location */
2513 info
[1024], /* printer-info */
2515 /* printer-make-and-model */
2516 cupsd_netif_t
*iface
; /* Network interface */
2520 * Figure out the printer type value...
2523 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2526 type
|= CUPS_PRINTER_REJECTING
;
2528 if (p
== DefaultPrinter
)
2529 type
|= CUPS_PRINTER_DEFAULT
;
2532 * Remove quotes from printer-info, printer-location, and
2533 * printer-make-and-model attributes...
2536 dequote(location
, p
->location
, sizeof(location
));
2537 dequote(info
, p
->info
, sizeof(info
));
2540 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2541 else if (p
->type
& CUPS_PRINTER_CLASS
)
2543 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2544 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2546 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2549 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2551 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2554 * Send a packet to each browse address...
2557 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2561 * Send the browse packet to one or more interfaces...
2564 if (!strcmp(b
->iface
, "*"))
2567 * Send to all local interfaces...
2572 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2574 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2577 * Only send to local, IPv4 interfaces...
2580 if (!iface
->is_local
|| !iface
->port
||
2581 iface
->address
.addr
.sa_family
!= AF_INET
)
2584 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2585 iface
->hostname
, iface
->port
,
2586 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2589 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2590 type
, p
->state
, uri
, location
, info
, make_model
,
2591 p
->browse_attrs
? p
->browse_attrs
: "");
2593 bytes
= strlen(packet
);
2595 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2596 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2597 iface
->name
, packet
);
2599 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2601 sendto(BrowseSocket
, packet
, bytes
, 0,
2602 (struct sockaddr
*)&(iface
->broadcast
),
2603 httpAddrLength(&(iface
->broadcast
)));
2606 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2609 * Send to the named interface using the IPv4 address...
2613 if (strcmp(b
->iface
, iface
->name
))
2618 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2621 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2625 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2626 iface
->hostname
, iface
->port
,
2627 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2630 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2631 type
, p
->state
, uri
, location
, info
, make_model
,
2632 p
->browse_attrs
? p
->browse_attrs
: "");
2634 bytes
= strlen(packet
);
2636 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2637 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2638 iface
->name
, packet
);
2640 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2642 sendto(BrowseSocket
, packet
, bytes
, 0,
2643 (struct sockaddr
*)&(iface
->broadcast
),
2644 httpAddrLength(&(iface
->broadcast
)));
2651 * Send the browse packet to the indicated address using
2652 * the default server name...
2655 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2656 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2657 p
->browse_attrs
? p
->browse_attrs
: "");
2659 bytes
= strlen(packet
);
2660 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2661 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2663 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2664 (struct sockaddr
*)&(b
->to
),
2665 httpAddrLength(&(b
->to
))) <= 0)
2668 * Unable to send browse packet, so remove this address from the
2672 cupsdLogMessage(CUPSD_LOG_ERROR
,
2673 "cupsdSendBrowseList: sendto failed for browser "
2675 (int)(b
- Browsers
+ 1), strerror(errno
));
2678 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2687 #ifdef HAVE_OPENLDAP
2689 * 'send_ldap_browse()' - Send LDAP printer registrations.
2693 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2695 int i
; /* Looping var... */
2696 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
2697 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
2698 LDAPMessage
*res
; /* Search result token */
2699 char *cn_value
[2], /* Change records */
2705 typestring
[255], /* String to hold printer-type */
2706 filter
[256], /* Search filter for possible UPDATEs */
2707 dn
[1024]; /* DN of the printer we are adding */
2708 int rc
; /* LDAP status */
2709 static const char * const objectClass_values
[] =
2710 { /* The 3 objectClass's we use in */
2711 "top", /* our LDAP entries */
2717 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
2720 * Everything in ldap is ** so we fudge around it...
2723 sprintf(typestring
, "%u", p
->type
);
2725 cn_value
[0] = p
->name
;
2727 info
[0] = p
->info
? p
->info
: "Unknown";
2729 location
[0] = p
->location
? p
->location
: "Unknown";
2731 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
2732 make_model
[1] = NULL
;
2733 type
[0] = typestring
;
2738 snprintf(filter
, sizeof(filter
),
2739 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
2741 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2742 filter
, (char **)ldap_attrs
, 0, &res
);
2743 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
2746 mods
[0].mod_type
= "cn";
2747 mods
[0].mod_values
= cn_value
;
2748 mods
[1].mod_type
= "printerDescription";
2749 mods
[1].mod_values
= info
;
2750 mods
[2].mod_type
= "printerURI";
2751 mods
[2].mod_values
= uri
;
2752 mods
[3].mod_type
= "printerLocation";
2753 mods
[3].mod_values
= location
;
2754 mods
[4].mod_type
= "printerMakeAndModel";
2755 mods
[4].mod_values
= make_model
;
2756 mods
[5].mod_type
= "printerType";
2757 mods
[5].mod_values
= type
;
2758 mods
[6].mod_type
= "objectClass";
2759 mods
[6].mod_values
= (char **)objectClass_values
;
2761 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
2762 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
2764 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
2767 * Printer has already been registered, modify the current
2771 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2772 "send_ldap_browse: Replacing entry...");
2774 for (i
= 0; i
< 7; i
++)
2776 pmods
[i
] = mods
+ i
;
2777 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2781 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2782 cupsdLogMessage(CUPSD_LOG_ERROR
,
2783 "LDAP modify for %s failed with status %d: %s",
2784 p
->name
, rc
, ldap_err2string(rc
));
2789 * Printer has never been registered, add the current
2793 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2794 "send_ldap_browse: Adding entry...");
2796 for (i
= 0; i
< 7; i
++)
2798 pmods
[i
] = mods
+ i
;
2799 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
2803 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2804 cupsdLogMessage(CUPSD_LOG_ERROR
,
2805 "LDAP add for %s failed with status %d: %s",
2806 p
->name
, rc
, ldap_err2string(rc
));
2809 #endif /* HAVE_OPENLDAP */
2814 * 'send_slp_browse()' - Register the specified printer with SLP.
2818 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2820 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
2821 attrs
[8192], /* Printer attributes */
2822 finishings
[1024], /* Finishings to support */
2823 make_model
[IPP_MAX_NAME
* 2],
2824 /* Make and model, quoted */
2825 location
[IPP_MAX_NAME
* 2],
2826 /* Location, quoted */
2827 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
2828 *src
, /* Pointer to original string */
2829 *dst
; /* Pointer to destination string */
2830 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
2831 SLPError error
; /* SLP error, if any */
2834 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
2838 * Make the SLP service URL that conforms to the IANA
2839 * 'printer:' template.
2842 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2844 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
2847 * Figure out the finishings string...
2850 if (p
->type
& CUPS_PRINTER_STAPLE
)
2851 strcpy(finishings
, "staple");
2853 finishings
[0] = '\0';
2855 if (p
->type
& CUPS_PRINTER_BIND
)
2858 strlcat(finishings
, ",bind", sizeof(finishings
));
2860 strcpy(finishings
, "bind");
2863 if (p
->type
& CUPS_PRINTER_PUNCH
)
2866 strlcat(finishings
, ",punch", sizeof(finishings
));
2868 strcpy(finishings
, "punch");
2871 if (p
->type
& CUPS_PRINTER_COVER
)
2874 strlcat(finishings
, ",cover", sizeof(finishings
));
2876 strcpy(finishings
, "cover");
2879 if (p
->type
& CUPS_PRINTER_SORT
)
2882 strlcat(finishings
, ",sort", sizeof(finishings
));
2884 strcpy(finishings
, "sort");
2888 strcpy(finishings
, "none");
2891 * Quote any commas in the make and model, location, and info strings...
2894 for (src
= p
->make_model
, dst
= make_model
;
2895 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
2897 if (*src
== ',' || *src
== '\\' || *src
== ')')
2906 strcpy(make_model
, "Unknown");
2908 for (src
= p
->location
, dst
= location
;
2909 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
2911 if (*src
== ',' || *src
== '\\' || *src
== ')')
2920 strcpy(location
, "Unknown");
2922 for (src
= p
->info
, dst
= info
;
2923 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
2925 if (*src
== ',' || *src
== '\\' || *src
== ')')
2934 strcpy(info
, "Unknown");
2937 * Get the authentication value...
2940 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
2944 * Make the SLP attribute string list that conforms to
2945 * the IANA 'printer:' template.
2948 snprintf(attrs
, sizeof(attrs
),
2949 "(printer-uri-supported=%s),"
2950 "(uri-authentication-supported=%s>),"
2952 "(uri-security-supported=tls>),"
2954 "(uri-security-supported=none>),"
2955 #endif /* HAVE_SSL */
2956 "(printer-name=%s),"
2957 "(printer-location=%s),"
2958 "(printer-info=%s),"
2959 "(printer-more-info=%s),"
2960 "(printer-make-and-model=%s),"
2961 "(printer-type=%d),"
2962 "(charset-supported=utf-8),"
2963 "(natural-language-configured=%s),"
2964 "(natural-language-supported=de,en,es,fr,it),"
2965 "(color-supported=%s),"
2966 "(finishings-supported=%s),"
2967 "(sides-supported=one-sided%s),"
2968 "(multiple-document-jobs-supported=true)"
2969 "(ipp-versions-supported=1.0,1.1)",
2970 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
2971 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
2972 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
2974 p
->type
& CUPS_PRINTER_DUPLEX
?
2975 ",two-sided-long-edge,two-sided-short-edge" : "");
2977 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
2980 * Register the printer with the SLP server...
2983 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
2984 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
2986 if (error
!= SLP_OK
)
2987 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
2993 * 'slp_attr_callback()' - SLP attribute callback
2996 static SLPBoolean
/* O - SLP_TRUE for success */
2998 SLPHandle hslp
, /* I - SLP handle */
2999 const char *attrlist
, /* I - Attribute list */
3000 SLPError errcode
, /* I - Parsing status for this attr */
3001 void *cookie
) /* I - Current printer */
3003 char *tmp
= 0; /* Temporary string */
3004 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
3005 /* Current printer */
3008 (void)hslp
; /* anti-compiler-warning-code */
3011 * Bail if there was an error
3014 if (errcode
!= SLP_OK
)
3018 * Parse the attrlist to obtain things needed to build CUPS browse packet
3021 memset(p
, 0, sizeof(cupsd_printer_t
));
3023 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
3025 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
3027 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
3029 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
3030 p
->type
= atoi(tmp
);
3032 p
->type
= CUPS_PRINTER_REMOTE
;
3034 cupsdClearString(&tmp
);
3041 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3045 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3047 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3050 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3052 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3055 * Make the SLP service URL that conforms to the IANA
3056 * 'printer:' template.
3059 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3062 * Deregister the printer...
3065 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3071 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3074 static int /* O - 0 on success */
3075 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3076 const char *tag
, /* I - Name of attribute */
3077 char **valbuf
) /* O - Value */
3079 char *ptr1
, /* Pointer into string */
3083 cupsdClearString(valbuf
);
3085 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3087 ptr1
+= strlen(tag
);
3089 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3095 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3096 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3099 * Dequote the value...
3102 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3103 if (*ptr1
== '\\' && ptr1
[1])
3104 _cups_strcpy(ptr1
, ptr1
+ 1);
3115 * 'slp_reg_callback()' - Empty SLPRegReport.
3119 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3120 SLPError errcode
, /* I - Error code, if any */
3121 void *cookie
) /* I - App data */
3132 * 'slp_url_callback()' - SLP service url callback
3135 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3137 SLPHandle hslp
, /* I - SLP handle */
3138 const char *srvurl
, /* I - URL of service */
3139 unsigned short lifetime
, /* I - Life of service */
3140 SLPError errcode
, /* I - Existing error code */
3141 void *cookie
) /* I - Pointer to service list */
3143 slpsrvurl_t
*s
, /* New service entry */
3144 **head
; /* Pointer to head of entry */
3148 * Let the compiler know we won't be using these vars...
3155 * Bail if there was an error
3158 if (errcode
!= SLP_OK
)
3162 * Grab the head of the list...
3165 head
= (slpsrvurl_t
**)cookie
;
3168 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3171 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3175 * Copy the SLP service URL...
3178 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3181 * Link the SLP service URL into the head of the list
3191 #endif /* HAVE_LIBSLP */
3195 * End of "$Id: dirsvc.c 6090 2006-11-14 16:35:27Z mike $".