2 * "$Id: dirsvc.c 5663 2006-06-15 20:36:42Z 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
+ BrowseTimeout
;
221 * Set the default printer as needed...
224 if (!strcasecmp(line
, "<DefaultPrinter"))
229 cupsdLogMessage(CUPSD_LOG_ERROR
,
230 "Syntax error on line %d of remote.cache.", linenum
);
234 else if (!strcasecmp(line
, "<Class") ||
235 !strcasecmp(line
, "<DefaultClass"))
238 * <Class name> or <DefaultClass name>
241 if (p
== NULL
&& value
)
244 * Add the printer and a base file type...
247 cupsdLogMessage(CUPSD_LOG_DEBUG
,
248 "cupsdLoadRemoteCache: Loading class %s...", value
);
250 if ((p
= cupsdFindDest(value
)) != NULL
)
251 p
->type
= CUPS_PRINTER_CLASS
;
253 p
= cupsdAddClass(value
);
256 p
->state
= IPP_PRINTER_IDLE
;
257 p
->type
|= CUPS_PRINTER_REMOTE
;
258 p
->browse_time
= now
+ BrowseTimeout
;
261 * Set the default printer as needed...
264 if (!strcasecmp(line
, "<DefaultClass"))
269 cupsdLogMessage(CUPSD_LOG_ERROR
,
270 "Syntax error on line %d of remote.cache.", linenum
);
274 else if (!strcasecmp(line
, "</Printer>") ||
275 !strcasecmp(line
, "</Class>"))
280 * Close out the current printer...
283 cupsdSetPrinterAttrs(p
);
289 cupsdLogMessage(CUPSD_LOG_ERROR
,
290 "Syntax error on line %d of remote.cache.", linenum
);
296 cupsdLogMessage(CUPSD_LOG_ERROR
,
297 "Syntax error on line %d of remote.cache.", linenum
);
300 else if (!strcasecmp(line
, "Info"))
303 cupsdSetString(&p
->info
, value
);
305 else if (!strcasecmp(line
, "MakeModel"))
308 cupsdSetString(&p
->make_model
, value
);
310 else if (!strcasecmp(line
, "Location"))
313 cupsdSetString(&p
->location
, value
);
315 else if (!strcasecmp(line
, "DeviceURI"))
319 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
320 username
, sizeof(username
), host
, sizeof(host
), &port
,
321 resource
, sizeof(resource
));
323 cupsdSetString(&p
->hostname
, host
);
324 cupsdSetString(&p
->uri
, value
);
325 cupsdSetString(&p
->device_uri
, value
);
329 cupsdLogMessage(CUPSD_LOG_ERROR
,
330 "Syntax error on line %d of remote.cache.", linenum
);
334 else if (!strcasecmp(line
, "Option") && value
)
340 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
343 cupsdLogMessage(CUPSD_LOG_ERROR
,
344 "Syntax error on line %d of remote.cache.", linenum
);
347 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
349 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
353 else if (!strcasecmp(line
, "State"))
356 * Set the initial queue state...
359 if (value
&& !strcasecmp(value
, "idle"))
360 p
->state
= IPP_PRINTER_IDLE
;
361 else if (value
&& !strcasecmp(value
, "stopped"))
362 p
->state
= IPP_PRINTER_STOPPED
;
365 cupsdLogMessage(CUPSD_LOG_ERROR
,
366 "Syntax error on line %d of remote.cache.", linenum
);
370 else if (!strcasecmp(line
, "StateMessage"))
373 * Set the initial queue state message...
377 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
379 else if (!strcasecmp(line
, "Accepting"))
382 * Set the initial accepting state...
386 (!strcasecmp(value
, "yes") ||
387 !strcasecmp(value
, "on") ||
388 !strcasecmp(value
, "true")))
391 (!strcasecmp(value
, "no") ||
392 !strcasecmp(value
, "off") ||
393 !strcasecmp(value
, "false")))
397 cupsdLogMessage(CUPSD_LOG_ERROR
,
398 "Syntax error on line %d of remote.cache.", linenum
);
402 else if (!strcasecmp(line
, "Type"))
405 p
->type
= atoi(value
);
408 cupsdLogMessage(CUPSD_LOG_ERROR
,
409 "Syntax error on line %d of remote.cache.", linenum
);
413 else if (!strcasecmp(line
, "BrowseTime"))
417 time_t t
= atoi(value
);
419 if (t
> (now
+ BrowseInterval
))
424 cupsdLogMessage(CUPSD_LOG_ERROR
,
425 "Syntax error on line %d of remote.cache.", linenum
);
429 else if (!strcasecmp(line
, "JobSheets"))
432 * Set the initial job sheets...
437 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
442 cupsdSetString(&p
->job_sheets
[0], value
);
444 while (isspace(*valueptr
& 255))
449 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
454 cupsdSetString(&p
->job_sheets
[1], value
);
459 cupsdLogMessage(CUPSD_LOG_ERROR
,
460 "Syntax error on line %d of remote.cache.", linenum
);
464 else if (!strcasecmp(line
, "AllowUser"))
469 cupsdAddPrinterUser(p
, value
);
473 cupsdLogMessage(CUPSD_LOG_ERROR
,
474 "Syntax error on line %d of remote.cache.", linenum
);
478 else if (!strcasecmp(line
, "DenyUser"))
483 cupsdAddPrinterUser(p
, value
);
487 cupsdLogMessage(CUPSD_LOG_ERROR
,
488 "Syntax error on line %d of remote.cache.", linenum
);
495 * Something else we don't understand...
498 cupsdLogMessage(CUPSD_LOG_ERROR
,
499 "Unknown configuration directive %s on line %d of remote.cache.",
507 * Do auto-classing if needed...
510 process_implicit_classes();
515 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
519 cupsdSaveRemoteCache(void)
521 int i
; /* Looping var */
522 cups_file_t
*fp
; /* printers.conf file */
523 char temp
[1024]; /* Temporary string */
524 cupsd_printer_t
*printer
; /* Current printer class */
525 time_t curtime
; /* Current time */
526 struct tm
*curdate
; /* Current date */
527 cups_option_t
*option
; /* Current option */
531 * Create the remote.cache file...
534 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
536 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
538 cupsdLogMessage(CUPSD_LOG_ERROR
,
539 "Unable to save remote.cache - %s", strerror(errno
));
543 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
546 * Restrict access to the file...
549 fchown(cupsFileNumber(fp
), getuid(), Group
);
550 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
553 * Write a small header to the file...
556 curtime
= time(NULL
);
557 curdate
= localtime(&curtime
);
558 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
560 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
561 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
564 * Write each local printer known to the system...
567 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
569 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
572 * Skip local destinations...
575 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
579 * Write printers as needed...
582 if (printer
== DefaultPrinter
)
583 cupsFilePuts(fp
, "<Default");
585 cupsFilePutChar(fp
, '<');
587 if (printer
->type
& CUPS_PRINTER_CLASS
)
588 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
590 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
592 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
594 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_time
);
597 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
599 if (printer
->make_model
)
600 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
602 if (printer
->location
)
603 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
605 if (printer
->device_uri
)
606 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
608 if (printer
->state
== IPP_PRINTER_STOPPED
)
610 cupsFilePuts(fp
, "State Stopped\n");
611 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
614 cupsFilePuts(fp
, "State Idle\n");
616 if (printer
->accepting
)
617 cupsFilePuts(fp
, "Accepting Yes\n");
619 cupsFilePuts(fp
, "Accepting No\n");
621 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
622 printer
->job_sheets
[1]);
624 for (i
= 0; i
< printer
->num_users
; i
++)
625 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
628 for (i
= printer
->num_options
, option
= printer
->options
;
631 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
633 if (printer
->type
& CUPS_PRINTER_CLASS
)
634 cupsFilePuts(fp
, "</Class>\n");
636 cupsFilePuts(fp
, "</Printer>\n");
644 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
648 cupsdSendBrowseDelete(
649 cupsd_printer_t
*p
) /* I - Printer to delete */
652 * Only announce if browsing is enabled and this is a local queue...
655 if (!Browsing
|| !p
->shared
||
656 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
660 * First mark the printer for deletion...
663 p
->type
|= CUPS_PRINTER_DELETE
;
666 * Announce the deletion...
669 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
672 if ((BrowseLocalProtocols
& BROWSE_SLP
) && BrowseSLPHandle
)
673 slp_dereg_printer(p
);
674 #endif /* HAVE_LIBSLP */
679 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
683 cupsdSendBrowseList(void)
685 int count
; /* Number of dests to update */
686 cupsd_printer_t
*p
; /* Current printer */
687 time_t ut
, /* Minimum update time */
688 to
; /* Timeout time */
691 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
695 * Compute the update and timeout times...
699 ut
= to
- BrowseInterval
;
702 * Figure out how many printers need an update...
705 if (BrowseInterval
> 0)
707 int max_count
; /* Maximum number to update */
711 * Throttle the number of printers we'll be updating this time
712 * around based on the number of queues that need updating and
713 * the maximum number of queues to update each second...
716 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
718 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
719 count
< max_count
&& p
!= NULL
;
720 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
721 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
722 p
->shared
&& p
->browse_time
< ut
)
726 * Loop through all of the printers and send local updates as needed...
730 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
732 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
736 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
739 * Check for wraparound...
743 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
747 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
750 else if (p
->browse_time
< ut
)
753 * Need to send an update...
758 p
->browse_time
= time(NULL
);
760 if (BrowseLocalProtocols
& BROWSE_CUPS
)
764 if (BrowseLocalProtocols
& BROWSE_SLP
)
766 #endif /* HAVE_LIBSLP */
769 if (BrowseLocalProtocols
& BROWSE_LDAP
)
771 #endif /* HAVE_LDAP */
776 * Save where we left off so that all printers get updated...
783 * Loop through all of the printers and send local updates as needed...
786 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
788 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
791 * If this is a remote queue, see if it needs to be timed out...
794 if (p
->type
& CUPS_PRINTER_REMOTE
)
796 if (p
->browse_expire
< to
)
798 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
799 "%s \'%s\' deleted by directory services (timeout).",
800 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
803 cupsdLogMessage(CUPSD_LOG_DEBUG
,
804 "Remote destination \"%s\" has timed out; "
808 cupsArraySave(Printers
);
809 cupsdDeletePrinter(p
, 1);
810 cupsArrayRestore(Printers
);
818 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
822 cupsdStartBrowsing(void)
824 int val
; /* Socket option value */
825 struct sockaddr_in addr
; /* Broadcast address */
830 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
833 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
835 if (BrowseSocket
< 0)
838 * Create the broadcast socket...
841 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
843 cupsdLogMessage(CUPSD_LOG_ERROR
,
844 "cupsdStartBrowsing: Unable to create broadcast "
845 "socket - %s.", strerror(errno
));
846 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
847 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
852 * Bind the socket to browse port...
855 memset(&addr
, 0, sizeof(addr
));
856 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
857 addr
.sin_family
= AF_INET
;
858 addr
.sin_port
= htons(BrowsePort
);
860 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
862 cupsdLogMessage(CUPSD_LOG_ERROR
,
863 "cupsdStartBrowsing: Unable to bind broadcast "
864 "socket - %s.", strerror(errno
));
867 closesocket(BrowseSocket
);
873 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
874 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
880 * Set the "broadcast" flag...
884 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
886 cupsdLogMessage(CUPSD_LOG_ERROR
,
887 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
891 closesocket(BrowseSocket
);
897 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
898 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
903 * Close the socket on exec...
906 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
909 * Finally, add the socket to the input selection set...
912 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
913 "cupsdStartBrowsing: Adding fd %d to InputSet...",
916 FD_SET(BrowseSocket
, InputSet
);
922 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
928 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
930 cupsdLogMessage(CUPSD_LOG_ERROR
,
931 "Unable to open an SLP handle; disabling SLP browsing!");
932 BrowseLocalProtocols
&= ~BROWSE_SLP
;
933 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
936 BrowseSLPRefresh
= 0;
939 BrowseSLPHandle
= NULL
;
940 #endif /* HAVE_LIBSLP */
943 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
947 cupsdLogMessage(CUPSD_LOG_ERROR
,
948 "Need to set BrowseLDAPDN to use LDAP browsing!");
949 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
950 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
955 * Open LDAP handle...
958 int rc
; /* LDAP API status */
959 int version
= 3; /* LDAP version */
960 struct berval bv
= {0, ""}; /* SASL bind value */
964 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
967 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
968 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
970 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
972 if (rc
!= LDAP_SUCCESS
)
974 cupsdLogMessage(CUPSD_LOG_ERROR
,
975 "Unable to initialize LDAP; disabling LDAP browsing!");
976 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
977 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
979 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
980 (const void *)&version
) != LDAP_SUCCESS
)
982 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
983 BrowseLDAPHandle
= NULL
;
984 cupsdLogMessage(CUPSD_LOG_ERROR
,
985 "Unable to set LDAP protocol version; "
986 "disabling LDAP browsing!");
987 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
988 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
992 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
993 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
996 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
997 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
999 if (rc
!= LDAP_SUCCESS
)
1001 cupsdLogMessage(CUPSD_LOG_ERROR
,
1002 "Unable to bind to LDAP server; "
1003 "disabling LDAP browsing!");
1004 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1005 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1006 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1011 BrowseLDAPRefresh
= 0;
1013 #endif /* HAVE_OPENLDAP */
1018 * 'cupsdStartPolling()' - Start polling servers as needed.
1022 cupsdStartPolling(void)
1024 int i
; /* Looping var */
1025 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1026 char polld
[1024]; /* Poll daemon path */
1027 char sport
[10]; /* Server port */
1028 char bport
[10]; /* Browser port */
1029 char interval
[10]; /* Poll interval */
1030 int statusfds
[2]; /* Status pipe */
1031 char *argv
[6]; /* Arguments */
1032 char *envp
[100]; /* Environment */
1036 * Don't do anything if we aren't polling...
1042 PollStatusBuffer
= NULL
;
1047 * Setup string arguments for polld, port and interval options.
1050 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1052 sprintf(bport
, "%d", BrowsePort
);
1055 sprintf(interval
, "%d", BrowseInterval
);
1057 strcpy(interval
, "30");
1059 argv
[0] = "cups-polld";
1065 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1068 * Create a pipe that receives the status messages from each
1072 if (cupsdOpenPipe(statusfds
))
1074 cupsdLogMessage(CUPSD_LOG_ERROR
,
1075 "Unable to create polling status pipes - %s.",
1078 PollStatusBuffer
= NULL
;
1082 PollPipe
= statusfds
[0];
1083 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1086 * Run each polling daemon, redirecting stderr to the polling pipe...
1089 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1091 sprintf(sport
, "%d", pollp
->port
);
1093 argv
[1] = pollp
->hostname
;
1095 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1096 0, &(pollp
->pid
)) < 0)
1098 cupsdLogMessage(CUPSD_LOG_ERROR
,
1099 "cupsdStartPolling: Unable to fork polling daemon - %s",
1105 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1106 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1107 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1110 close(statusfds
[1]);
1113 * Finally, add the pipe to the input selection set...
1116 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1117 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1119 FD_SET(PollPipe
, InputSet
);
1124 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1128 cupsdStopBrowsing(void)
1130 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1133 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1137 * Close the socket and remove it from the input selection set.
1141 closesocket(BrowseSocket
);
1143 close(BrowseSocket
);
1146 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1147 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1150 FD_CLR(BrowseSocket
, InputSet
);
1155 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1159 * Close SLP handle...
1162 SLPClose(BrowseSLPHandle
);
1163 BrowseSLPHandle
= NULL
;
1165 #endif /* HAVE_LIBSLP */
1167 #ifdef HAVE_OPENLDAP
1168 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1171 ldap_unbind(BrowseLDAPHandle
);
1172 BrowseLDAPHandle
= NULL
;
1174 #endif /* HAVE_OPENLDAP */
1179 * 'cupsdStopPolling()' - Stop polling servers as needed.
1183 cupsdStopPolling(void)
1185 int i
; /* Looping var */
1186 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1191 cupsdStatBufDelete(PollStatusBuffer
);
1194 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1195 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1196 FD_CLR(PollPipe
, InputSet
);
1199 PollStatusBuffer
= NULL
;
1202 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1204 cupsdEndProcess(pollp
->pid
, 0);
1209 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1213 cupsdUpdateCUPSBrowse(void)
1215 int i
; /* Looping var */
1216 int auth
; /* Authorization status */
1217 int len
; /* Length of name string */
1218 int bytes
; /* Number of bytes left */
1219 char packet
[1541], /* Broadcast packet */
1220 *pptr
; /* Pointer into packet */
1221 socklen_t srclen
; /* Length of source address */
1222 http_addr_t srcaddr
; /* Source address */
1223 char srcname
[1024]; /* Source hostname */
1224 unsigned address
[4]; /* Source address */
1225 unsigned type
; /* Printer type */
1226 unsigned state
; /* Printer state */
1227 char uri
[HTTP_MAX_URI
], /* Printer URI */
1228 host
[HTTP_MAX_URI
], /* Host portion of URI */
1229 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1230 info
[IPP_MAX_NAME
], /* Information string */
1231 location
[IPP_MAX_NAME
], /* Location string */
1232 make_model
[IPP_MAX_NAME
];/* Make and model string */
1233 int num_attrs
; /* Number of attributes */
1234 cups_option_t
*attrs
; /* Attributes */
1238 * Read a packet from the browse socket...
1241 srclen
= sizeof(srcaddr
);
1242 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1243 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1246 * "Connection refused" is returned under Linux if the destination port
1247 * or address is unreachable from a previous sendto(); check for the
1248 * error here and ignore it for now...
1251 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1253 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1255 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1257 cupsdStopBrowsing();
1264 packet
[bytes
] = '\0';
1267 * If we're about to sleep, ignore incoming browse packets.
1274 * Figure out where it came from...
1278 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1280 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1281 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1282 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1283 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1286 #endif /* AF_INET6 */
1291 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1294 if (HostNameLookups
)
1295 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1297 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1299 len
= strlen(srcname
);
1307 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1310 * Access from localhost (127.0.0.1) is always allowed...
1318 * Do authorization checks on the domain/address...
1321 switch (BrowseACL
->order_type
)
1324 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1327 case AUTH_ALLOW
: /* Order Deny,Allow */
1330 if (cupsdCheckAuth(address
, srcname
, len
,
1331 BrowseACL
->num_deny
, BrowseACL
->deny
))
1334 if (cupsdCheckAuth(address
, srcname
, len
,
1335 BrowseACL
->num_allow
, BrowseACL
->allow
))
1339 case AUTH_DENY
: /* Order Allow,Deny */
1342 if (cupsdCheckAuth(address
, srcname
, len
,
1343 BrowseACL
->num_allow
, BrowseACL
->allow
))
1346 if (cupsdCheckAuth(address
, srcname
, len
,
1347 BrowseACL
->num_deny
, BrowseACL
->deny
))
1356 if (auth
== AUTH_DENY
)
1358 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1359 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1364 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1365 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1372 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1374 cupsdLogMessage(CUPSD_LOG_WARN
,
1375 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1379 strcpy(location
, "Location Unknown");
1380 strcpy(info
, "No Information Available");
1381 make_model
[0] = '\0';
1385 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1388 * Have extended information; can't use sscanf for it because not all
1389 * sscanf's allow empty strings with %[^\"]...
1392 for (i
= 0, pptr
++;
1393 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1395 location
[i
] = *pptr
;
1403 while (*pptr
&& isspace(*pptr
& 255))
1408 for (i
= 0, pptr
++;
1409 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1418 while (*pptr
&& isspace(*pptr
& 255))
1423 for (i
= 0, pptr
++;
1424 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1426 make_model
[i
] = *pptr
;
1431 make_model
[i
] = '\0';
1434 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1440 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1441 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1442 type
, state
, uri
, location
, info
, make_model
));
1445 * Pull the URI apart to see if this is a local or remote printer...
1448 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1450 cupsFreeOptions(num_attrs
, attrs
);
1458 for (i
= 0; i
< NumRelays
; i
++)
1459 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1460 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1461 (struct sockaddr
*)&(Relays
[i
].to
),
1462 sizeof(http_addr_t
)) <= 0)
1464 cupsdLogMessage(CUPSD_LOG_ERROR
,
1465 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1466 i
+ 1, strerror(errno
));
1467 cupsFreeOptions(num_attrs
, attrs
);
1472 * Process the browse data...
1475 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1476 (ipp_pstate_t
)state
, location
, info
, make_model
,
1481 #ifdef HAVE_OPENLDAP
1483 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1487 cupsdUpdateLDAPBrowse(void)
1489 char uri
[HTTP_MAX_URI
], /* Printer URI */
1490 host
[HTTP_MAX_URI
], /* Hostname */
1491 resource
[HTTP_MAX_URI
], /* Resource path */
1492 location
[1024], /* Printer location */
1493 info
[1024], /* Printer information */
1494 make_model
[1024], /* Printer make and model */
1495 **value
; /* Holds the returned data from LDAP */
1496 int type
; /* Printer type */
1497 int rc
; /* LDAP status */
1498 int limit
; /* Size limit */
1499 LDAPMessage
*res
, /* LDAP search results */
1500 *e
; /* Current entry from search */
1504 * Search for printers...
1507 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1509 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1511 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1512 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1513 if (rc
!= LDAP_SUCCESS
)
1515 cupsdLogMessage(CUPSD_LOG_ERROR
,
1516 "LDAP search returned error %d: %s", rc
,
1517 ldap_err2string(rc
));
1521 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1522 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1527 * Loop through the available printers...
1530 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1532 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1535 * Get the required values from this entry...
1538 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1539 "printerDescription")) == NULL
)
1542 strlcpy(info
, *value
, sizeof(info
));
1543 ldap_value_free(value
);
1545 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1546 "printerLocation")) == NULL
)
1549 strlcpy(location
, *value
, sizeof(location
));
1550 ldap_value_free(value
);
1552 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1553 "printerMakeAndModel")) == NULL
)
1556 strlcpy(make_model
, *value
, sizeof(make_model
));
1557 ldap_value_free(value
);
1559 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1560 "printerType")) == NULL
)
1563 type
= atoi(*value
);
1564 ldap_value_free(value
);
1566 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1567 "printerURI")) == NULL
)
1570 strlcpy(uri
, *value
, sizeof(uri
));
1571 ldap_value_free(value
);
1574 * Process the entry as browse data...
1577 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1578 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1579 location
, info
, make_model
, 0, NULL
);
1583 #endif /* HAVE_OPENLDAP */
1587 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1591 cupsdUpdatePolling(void)
1593 char *ptr
, /* Pointer to end of line in buffer */
1594 message
[1024]; /* Pointer to message text */
1595 int loglevel
; /* Log level for message */
1598 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1599 message
, sizeof(message
))) != NULL
)
1600 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1606 * All polling processes have died; stop polling...
1609 cupsdLogMessage(CUPSD_LOG_ERROR
,
1610 "cupsdUpdatePolling: all polling processes have exited!");
1618 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1622 cupsdUpdateSLPBrowse(void)
1624 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1625 *next
; /* Next service in list */
1626 cupsd_printer_t p
; /* Printer information */
1627 const char *uri
; /* Pointer to printer URI */
1628 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1629 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1633 * Reset the refresh time...
1636 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1639 * Poll for remote printers using SLP...
1644 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1645 slp_url_callback
, &s
);
1648 * Loop through the list of available printers...
1654 * Save the "next" pointer...
1660 * Load a cupsd_printer_t structure with the SLP service attributes...
1663 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1666 * Process this printer entry...
1669 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1671 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1674 * Pull the URI apart to see if this is a local or remote printer...
1677 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1678 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1679 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1683 * Free this listing...
1686 cupsdClearString(&p
.info
);
1687 cupsdClearString(&p
.location
);
1688 cupsdClearString(&p
.make_model
);
1693 #endif /* HAVE_LIBSLP */
1697 * 'dequote()' - Remote quotes from a string.
1700 static char * /* O - Dequoted string */
1701 dequote(char *d
, /* I - Destination string */
1702 const char *s
, /* I - Source string */
1703 int dlen
) /* I - Destination length */
1705 char *dptr
; /* Pointer into destination */
1710 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1727 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1730 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1731 is_local_queue(const char *uri
, /* I - Printer URI */
1732 char *host
, /* O - Host string */
1733 int hostlen
, /* I - Length of host buffer */
1734 char *resource
, /* O - Resource string */
1735 int resourcelen
) /* I - Length of resource buffer */
1737 char scheme
[32], /* Scheme portion of URI */
1738 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1739 int port
; /* Port portion of URI */
1740 cupsd_netif_t
*iface
; /* Network interface */
1744 * Pull the URI apart to see if this is a local or remote printer...
1747 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1748 username
, sizeof(username
), host
, hostlen
, &port
,
1749 resource
, resourcelen
) < HTTP_URI_OK
)
1752 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1755 * Check for local server addresses...
1758 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1763 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1765 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1766 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1770 * If we get here, the printer is remote...
1778 * 'process_browse_data()' - Process new browse data.
1782 process_browse_data(
1783 const char *uri
, /* I - URI of printer/class */
1784 const char *host
, /* I - Hostname */
1785 const char *resource
, /* I - Resource path */
1786 cups_ptype_t type
, /* I - Printer type */
1787 ipp_pstate_t state
, /* I - Printer state */
1788 const char *location
, /* I - Printer location */
1789 const char *info
, /* I - Printer information */
1790 const char *make_model
, /* I - Printer make and model */
1791 int num_attrs
, /* I - Number of attributes */
1792 cups_option_t
*attrs
) /* I - Attributes */
1794 int i
; /* Looping var */
1795 int update
; /* Update printer attributes? */
1796 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1797 name
[IPP_MAX_NAME
], /* Name of printer */
1798 newname
[IPP_MAX_NAME
], /* New name of printer */
1799 *hptr
, /* Pointer into hostname */
1800 *sptr
; /* Pointer into ServerName */
1801 char local_make_model
[IPP_MAX_NAME
];
1802 /* Local make and model */
1803 cupsd_printer_t
*p
; /* Printer information */
1804 const char *ipp_options
, /* ipp-options value */
1805 *lease_duration
; /* lease-duration value */
1809 * Determine if the URI contains any illegal characters in it...
1812 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1813 (strncmp(resource
, "/printers/", 10) &&
1814 strncmp(resource
, "/classes/", 9)))
1816 cupsdLogMessage(CUPSD_LOG_ERROR
,
1817 "process_browse_data: Bad printer URI in browse data: %s",
1822 if (strchr(resource
, '?') ||
1823 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1824 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1826 cupsdLogMessage(CUPSD_LOG_ERROR
,
1827 "process_browse_data: Bad resource in browse data: %s",
1833 * OK, this isn't a local printer; add any remote options...
1836 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1838 if (BrowseRemoteOptions
)
1840 if (BrowseRemoteOptions
[0] == '?')
1843 * Override server-supplied options...
1846 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1848 else if (ipp_options
)
1851 * Combine the server and local options...
1854 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1855 BrowseRemoteOptions
);
1860 * Just use the local options...
1863 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1868 else if (ipp_options
)
1871 * Just use the server-supplied options...
1874 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1879 * See if we already have it listed in the Printers list, and add it if not...
1882 type
|= CUPS_PRINTER_REMOTE
;
1883 type
&= ~CUPS_PRINTER_IMPLICIT
;
1885 hptr
= strchr(host
, '.');
1886 sptr
= strchr(ServerName
, '.');
1888 if (sptr
!= NULL
&& hptr
!= NULL
)
1891 * Strip the common domain name components...
1894 while (hptr
!= NULL
)
1896 if (!strcasecmp(hptr
, sptr
))
1902 hptr
= strchr(hptr
+ 1, '.');
1906 if (type
& CUPS_PRINTER_CLASS
)
1909 * Remote destination is a class...
1912 if (!strncmp(resource
, "/classes/", 9))
1913 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1917 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1919 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1921 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1924 * Nope, this isn't the same host; if the hostname isn't the local host,
1925 * add it to the other class and then find a class using the full host
1929 if (p
->type
& CUPS_PRINTER_REMOTE
)
1931 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1932 "Renamed remote class \"%s\" to \"%s@%s\"...",
1933 p
->name
, p
->name
, p
->hostname
);
1934 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1935 "Class \'%s\' deleted by directory services.",
1938 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1939 cupsdRenamePrinter(p
, newname
);
1941 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1942 "Class \'%s\' added by directory services.",
1948 else if (!p
->hostname
)
1951 * Hostname not set, so this must be a cached remote printer
1952 * that was created for a pending print job...
1955 cupsdSetString(&p
->hostname
, host
);
1956 cupsdSetString(&p
->uri
, uri
);
1957 cupsdSetString(&p
->device_uri
, uri
);
1964 * Use the short name for this shared class.
1967 strlcpy(name
, resource
+ 9, sizeof(name
));
1970 else if (p
&& !p
->hostname
)
1973 * Hostname not set, so this must be a cached remote printer
1974 * that was created for a pending print job...
1977 cupsdSetString(&p
->hostname
, host
);
1978 cupsdSetString(&p
->uri
, uri
);
1979 cupsdSetString(&p
->device_uri
, uri
);
1986 * Class doesn't exist; add it...
1989 p
= cupsdAddClass(name
);
1991 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
1993 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1994 "Class \'%s\' added by directory services.", name
);
1997 * Force the URI to point to the real server...
2000 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2002 cupsdSetString(&p
->uri
, uri
);
2003 cupsdSetString(&p
->device_uri
, uri
);
2004 cupsdSetString(&p
->hostname
, host
);
2012 * Remote destination is a printer...
2015 if (!strncmp(resource
, "/printers/", 10))
2016 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2020 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2022 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2024 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2027 * Nope, this isn't the same host; if the hostname isn't the local host,
2028 * add it to the other printer and then find a printer using the full host
2032 if (p
->type
& CUPS_PRINTER_REMOTE
)
2034 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2035 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2036 p
->name
, p
->name
, p
->hostname
);
2037 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2038 "Printer \'%s\' deleted by directory services.",
2041 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2042 cupsdRenamePrinter(p
, newname
);
2044 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2045 "Printer \'%s\' added by directory services.",
2051 else if (!p
->hostname
)
2054 * Hostname not set, so this must be a cached remote printer
2055 * that was created for a pending print job...
2058 cupsdSetString(&p
->hostname
, host
);
2059 cupsdSetString(&p
->uri
, uri
);
2060 cupsdSetString(&p
->device_uri
, uri
);
2067 * Use the short name for this shared printer.
2070 strlcpy(name
, resource
+ 10, sizeof(name
));
2073 else if (p
&& !p
->hostname
)
2076 * Hostname not set, so this must be a cached remote printer
2077 * that was created for a pending print job...
2080 cupsdSetString(&p
->hostname
, host
);
2081 cupsdSetString(&p
->uri
, uri
);
2082 cupsdSetString(&p
->device_uri
, uri
);
2089 * Printer doesn't exist; add it...
2092 p
= cupsdAddPrinter(name
);
2094 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2095 "Printer \'%s\' added by directory services.", name
);
2097 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2100 * Force the URI to point to the real server...
2103 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2105 cupsdSetString(&p
->hostname
, host
);
2106 cupsdSetString(&p
->uri
, uri
);
2107 cupsdSetString(&p
->device_uri
, uri
);
2114 * Update the state...
2118 p
->browse_time
= time(NULL
);
2120 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2124 * Grab the lease-duration for the browse data; anything less then 1
2125 * second or more than 1 week gets the default BrowseTimeout...
2128 i
= atoi(lease_duration
);
2129 if (i
< 1 || i
> 604800)
2132 p
->browse_expire
= p
->browse_time
+ i
;
2135 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2137 if (type
& CUPS_PRINTER_REJECTING
)
2139 type
&= ~CUPS_PRINTER_REJECTING
;
2147 else if (!p
->accepting
)
2153 if (p
->type
!= type
)
2159 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2161 cupsdSetString(&p
->location
, location
);
2165 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2167 cupsdSetString(&p
->info
, info
);
2171 if (!make_model
|| !make_model
[0])
2173 if (type
& CUPS_PRINTER_CLASS
)
2174 snprintf(local_make_model
, sizeof(local_make_model
),
2175 "Remote Class on %s", host
);
2177 snprintf(local_make_model
, sizeof(local_make_model
),
2178 "Remote Printer on %s", host
);
2181 snprintf(local_make_model
, sizeof(local_make_model
),
2182 "%s on %s", make_model
, host
);
2184 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2186 cupsdSetString(&p
->make_model
, local_make_model
);
2192 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2195 * See if we need to update the attributes...
2198 if (p
->num_options
!= num_attrs
)
2202 for (i
= 0; i
< num_attrs
; i
++)
2203 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2204 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2205 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2214 * Free the old options...
2217 cupsFreeOptions(p
->num_options
, p
->options
);
2220 p
->num_options
= num_attrs
;
2223 if (type
& CUPS_PRINTER_DELETE
)
2225 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2226 "%s \'%s\' deleted by directory services.",
2227 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2229 cupsdExpireSubscriptions(p
, NULL
);
2231 cupsdDeletePrinter(p
, 1);
2232 cupsdUpdateImplicitClasses();
2236 cupsdSetPrinterAttrs(p
);
2237 cupsdUpdateImplicitClasses();
2241 * See if we have a default printer... If not, make the first network
2242 * default printer the default.
2245 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2248 * Find the first network default printer and use it...
2251 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2253 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2254 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2262 * Do auto-classing if needed...
2265 process_implicit_classes();
2268 * Update the printcap file...
2271 cupsdWritePrintcap();
2276 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2280 process_implicit_classes(void)
2282 int i
; /* Looping var */
2283 int update
; /* Update printer attributes? */
2284 char name
[IPP_MAX_NAME
], /* Name of printer */
2285 *hptr
; /* Pointer into hostname */
2286 cupsd_printer_t
*p
, /* Printer information */
2287 *pclass
, /* Printer class */
2288 *first
; /* First printer in class */
2289 int offset
, /* Offset of name */
2290 len
; /* Length of name */
2293 if (!ImplicitClasses
|| !Printers
)
2297 * Loop through all available printers and create classes as needed...
2300 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2301 update
= 0, pclass
= NULL
, first
= NULL
;
2303 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2306 * Skip implicit classes...
2309 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2316 * If len == 0, get the length of this printer name up to the "@"
2320 cupsArraySave(Printers
);
2323 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2324 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2327 * We have more than one printer with the same name; see if
2328 * we have a class, and if this printer is a member...
2331 if (pclass
&& strcasecmp(pclass
->name
, name
))
2334 cupsdSetPrinterAttrs(pclass
);
2340 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2343 * Need to add the class...
2346 pclass
= cupsdAddPrinter(name
);
2347 cupsArrayAdd(ImplicitPrinters
, pclass
);
2349 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2350 pclass
->accepting
= 1;
2351 pclass
->state
= IPP_PRINTER_IDLE
;
2353 cupsdSetString(&pclass
->location
, p
->location
);
2354 cupsdSetString(&pclass
->info
, p
->info
);
2358 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2360 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2361 "Implicit class \'%s\' added by directory services.",
2367 for (i
= 0; i
< pclass
->num_printers
; i
++)
2368 if (pclass
->printers
[i
] == first
)
2371 if (i
>= pclass
->num_printers
)
2373 first
->in_implicit_class
= 1;
2374 cupsdAddPrinterToClass(pclass
, first
);
2380 for (i
= 0; i
< pclass
->num_printers
; i
++)
2381 if (pclass
->printers
[i
] == p
)
2384 if (i
>= pclass
->num_printers
)
2386 p
->in_implicit_class
= 1;
2387 cupsdAddPrinterToClass(pclass
, p
);
2394 * First time around; just get name length and mark it as first
2398 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2399 len
= hptr
- p
->name
;
2401 len
= strlen(p
->name
);
2403 strncpy(name
, p
->name
, len
);
2407 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2408 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2411 * Can't use same name as a local printer; add "Any" to the
2412 * front of the name, unless we have explicitly disabled
2413 * the "ImplicitAnyClasses"...
2416 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2419 * Add "Any" to the class name...
2422 strcpy(name
, "Any");
2423 strncpy(name
+ 3, p
->name
, len
);
2424 name
[len
+ 3] = '\0';
2430 * Don't create an implicit class if we have a local printer
2431 * with the same name...
2435 cupsArrayRestore(Printers
);
2443 cupsArrayRestore(Printers
);
2447 * Update the last printer class as needed...
2450 if (pclass
&& update
)
2451 cupsdSetPrinterAttrs(pclass
);
2456 * 'send_cups_browse()' - Send new browsing information using the CUPS
2461 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2463 int i
; /* Looping var */
2464 cups_ptype_t type
; /* Printer type */
2465 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2466 int bytes
; /* Length of packet */
2467 char packet
[1453], /* Browse data packet */
2468 uri
[1024], /* Printer URI */
2469 location
[1024], /* printer-location */
2470 info
[1024], /* printer-info */
2472 /* printer-make-and-model */
2473 cupsd_netif_t
*iface
; /* Network interface */
2477 * Figure out the printer type value...
2480 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2483 type
|= CUPS_PRINTER_REJECTING
;
2485 if (p
== DefaultPrinter
)
2486 type
|= CUPS_PRINTER_DEFAULT
;
2489 * Remove quotes from printer-info, printer-location, and
2490 * printer-make-and-model attributes...
2493 dequote(location
, p
->location
, sizeof(location
));
2494 dequote(info
, p
->info
, sizeof(info
));
2497 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2498 else if (p
->type
& CUPS_PRINTER_CLASS
)
2500 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2501 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2503 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2506 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2508 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2511 * Send a packet to each browse address...
2514 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2518 * Send the browse packet to one or more interfaces...
2521 if (!strcmp(b
->iface
, "*"))
2524 * Send to all local interfaces...
2529 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2531 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2534 * Only send to local, IPv4 interfaces...
2537 if (!iface
->is_local
|| !iface
->port
||
2538 iface
->address
.addr
.sa_family
!= AF_INET
)
2541 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2542 iface
->hostname
, iface
->port
,
2543 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2546 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2547 type
, p
->state
, uri
, location
, info
, make_model
,
2548 p
->browse_attrs
? p
->browse_attrs
: "");
2550 bytes
= strlen(packet
);
2552 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2553 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2554 iface
->name
, packet
);
2556 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2558 sendto(BrowseSocket
, packet
, bytes
, 0,
2559 (struct sockaddr
*)&(iface
->broadcast
),
2560 sizeof(struct sockaddr_in
));
2563 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2566 * Send to the named interface using the IPv4 address...
2570 if (strcmp(b
->iface
, iface
->name
))
2575 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2578 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2582 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2583 iface
->hostname
, iface
->port
,
2584 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2587 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2588 type
, p
->state
, uri
, location
, info
, make_model
,
2589 p
->browse_attrs
? p
->browse_attrs
: "");
2591 bytes
= strlen(packet
);
2593 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2594 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2595 iface
->name
, packet
);
2597 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2599 sendto(BrowseSocket
, packet
, bytes
, 0,
2600 (struct sockaddr
*)&(iface
->broadcast
),
2601 sizeof(struct sockaddr_in
));
2608 * Send the browse packet to the indicated address using
2609 * the default server name...
2612 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2613 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2614 p
->browse_attrs
? p
->browse_attrs
: "");
2616 bytes
= strlen(packet
);
2617 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2618 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2620 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2621 (struct sockaddr
*)&(b
->to
),
2622 sizeof(struct sockaddr_in
)) <= 0)
2625 * Unable to send browse packet, so remove this address from the
2629 cupsdLogMessage(CUPSD_LOG_ERROR
,
2630 "cupsdSendBrowseList: sendto failed for browser "
2632 (int)(b
- Browsers
+ 1), strerror(errno
));
2635 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2644 #ifdef HAVE_OPENLDAP
2646 * 'send_ldap_browse()' - Send LDAP printer registrations.
2650 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2652 int i
; /* Looping var... */
2653 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
2654 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
2655 LDAPMessage
*res
; /* Search result token */
2656 char *cn_value
[2], /* Change records */
2662 typestring
[255], /* String to hold printer-type */
2663 filter
[256], /* Search filter for possible UPDATEs */
2664 dn
[1024]; /* DN of the printer we are adding */
2665 int rc
; /* LDAP status */
2666 static const char * const objectClass_values
[] =
2667 { /* The 3 objectClass's we use in */
2668 "top", /* our LDAP entries */
2674 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
2677 * Everything in ldap is ** so we fudge around it...
2680 sprintf(typestring
, "%u", p
->type
);
2682 cn_value
[0] = p
->name
;
2684 info
[0] = p
->info
? p
->info
: "Unknown";
2686 location
[0] = p
->location
? p
->location
: "Unknown";
2688 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
2689 make_model
[1] = NULL
;
2690 type
[0] = typestring
;
2695 snprintf(filter
, sizeof(filter
),
2696 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
2698 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2699 filter
, (char **)ldap_attrs
, 0, &res
);
2700 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
2703 mods
[0].mod_type
= "cn";
2704 mods
[0].mod_values
= cn_value
;
2705 mods
[1].mod_type
= "printerDescription";
2706 mods
[1].mod_values
= info
;
2707 mods
[2].mod_type
= "printerURI";
2708 mods
[2].mod_values
= uri
;
2709 mods
[3].mod_type
= "printerLocation";
2710 mods
[3].mod_values
= location
;
2711 mods
[4].mod_type
= "printerMakeAndModel";
2712 mods
[4].mod_values
= make_model
;
2713 mods
[5].mod_type
= "printerType";
2714 mods
[5].mod_values
= type
;
2715 mods
[6].mod_type
= "objectClass";
2716 mods
[6].mod_values
= (char **)objectClass_values
;
2718 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
2719 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
2721 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
2724 * Printer has already been registered, modify the current
2728 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2729 "send_ldap_browse: Replacing entry...");
2731 for (i
= 0; i
< 7; i
++)
2733 pmods
[i
] = mods
+ i
;
2734 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2738 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2739 cupsdLogMessage(CUPSD_LOG_ERROR
,
2740 "LDAP modify for %s failed with status %d: %s",
2741 p
->name
, rc
, ldap_err2string(rc
));
2746 * Printer has never been registered, add the current
2750 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2751 "send_ldap_browse: Adding entry...");
2753 for (i
= 0; i
< 7; i
++)
2755 pmods
[i
] = mods
+ i
;
2756 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
2760 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2761 cupsdLogMessage(CUPSD_LOG_ERROR
,
2762 "LDAP add for %s failed with status %d: %s",
2763 p
->name
, rc
, ldap_err2string(rc
));
2766 #endif /* HAVE_OPENLDAP */
2771 * 'send_slp_browse()' - Register the specified printer with SLP.
2775 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2777 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
2778 attrs
[8192], /* Printer attributes */
2779 finishings
[1024], /* Finishings to support */
2780 make_model
[IPP_MAX_NAME
* 2],
2781 /* Make and model, quoted */
2782 location
[IPP_MAX_NAME
* 2],
2783 /* Location, quoted */
2784 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
2785 *src
, /* Pointer to original string */
2786 *dst
; /* Pointer to destination string */
2787 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
2788 SLPError error
; /* SLP error, if any */
2791 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
2795 * Make the SLP service URL that conforms to the IANA
2796 * 'printer:' template.
2799 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2801 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
2804 * Figure out the finishings string...
2807 if (p
->type
& CUPS_PRINTER_STAPLE
)
2808 strcpy(finishings
, "staple");
2810 finishings
[0] = '\0';
2812 if (p
->type
& CUPS_PRINTER_BIND
)
2815 strlcat(finishings
, ",bind", sizeof(finishings
));
2817 strcpy(finishings
, "bind");
2820 if (p
->type
& CUPS_PRINTER_PUNCH
)
2823 strlcat(finishings
, ",punch", sizeof(finishings
));
2825 strcpy(finishings
, "punch");
2828 if (p
->type
& CUPS_PRINTER_COVER
)
2831 strlcat(finishings
, ",cover", sizeof(finishings
));
2833 strcpy(finishings
, "cover");
2836 if (p
->type
& CUPS_PRINTER_SORT
)
2839 strlcat(finishings
, ",sort", sizeof(finishings
));
2841 strcpy(finishings
, "sort");
2845 strcpy(finishings
, "none");
2848 * Quote any commas in the make and model, location, and info strings...
2851 for (src
= p
->make_model
, dst
= make_model
;
2852 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
2854 if (*src
== ',' || *src
== '\\' || *src
== ')')
2863 strcpy(make_model
, "Unknown");
2865 for (src
= p
->location
, dst
= location
;
2866 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
2868 if (*src
== ',' || *src
== '\\' || *src
== ')')
2877 strcpy(location
, "Unknown");
2879 for (src
= p
->info
, dst
= info
;
2880 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
2882 if (*src
== ',' || *src
== '\\' || *src
== ')')
2891 strcpy(info
, "Unknown");
2894 * Get the authentication value...
2897 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
2901 * Make the SLP attribute string list that conforms to
2902 * the IANA 'printer:' template.
2905 snprintf(attrs
, sizeof(attrs
),
2906 "(printer-uri-supported=%s),"
2907 "(uri-authentication-supported=%s>),"
2909 "(uri-security-supported=tls>),"
2911 "(uri-security-supported=none>),"
2912 #endif /* HAVE_SSL */
2913 "(printer-name=%s),"
2914 "(printer-location=%s),"
2915 "(printer-info=%s),"
2916 "(printer-more-info=%s),"
2917 "(printer-make-and-model=%s),"
2918 "(printer-type=%d),"
2919 "(charset-supported=utf-8),"
2920 "(natural-language-configured=%s),"
2921 "(natural-language-supported=de,en,es,fr,it),"
2922 "(color-supported=%s),"
2923 "(finishings-supported=%s),"
2924 "(sides-supported=one-sided%s),"
2925 "(multiple-document-jobs-supported=true)"
2926 "(ipp-versions-supported=1.0,1.1)",
2927 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
2928 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
2929 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
2931 p
->type
& CUPS_PRINTER_DUPLEX
?
2932 ",two-sided-long-edge,two-sided-short-edge" : "");
2934 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
2937 * Register the printer with the SLP server...
2940 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
2941 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
2943 if (error
!= SLP_OK
)
2944 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
2950 * 'slp_attr_callback()' - SLP attribute callback
2953 static SLPBoolean
/* O - SLP_TRUE for success */
2955 SLPHandle hslp
, /* I - SLP handle */
2956 const char *attrlist
, /* I - Attribute list */
2957 SLPError errcode
, /* I - Parsing status for this attr */
2958 void *cookie
) /* I - Current printer */
2960 char *tmp
= 0; /* Temporary string */
2961 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2962 /* Current printer */
2965 (void)hslp
; /* anti-compiler-warning-code */
2968 * Bail if there was an error
2971 if (errcode
!= SLP_OK
)
2975 * Parse the attrlist to obtain things needed to build CUPS browse packet
2978 memset(p
, 0, sizeof(cupsd_printer_t
));
2980 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2982 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2984 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2986 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
2987 p
->type
= atoi(tmp
);
2989 p
->type
= CUPS_PRINTER_REMOTE
;
2991 cupsdClearString(&tmp
);
2998 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3002 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3004 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3007 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3009 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3012 * Make the SLP service URL that conforms to the IANA
3013 * 'printer:' template.
3016 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3019 * Deregister the printer...
3022 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3028 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3031 static int /* O - 0 on success */
3032 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3033 const char *tag
, /* I - Name of attribute */
3034 char **valbuf
) /* O - Value */
3036 char *ptr1
, /* Pointer into string */
3040 cupsdClearString(valbuf
);
3042 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3044 ptr1
+= strlen(tag
);
3046 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3052 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3053 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3056 * Dequote the value...
3059 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3060 if (*ptr1
== '\\' && ptr1
[1])
3061 _cups_strcpy(ptr1
, ptr1
+ 1);
3072 * 'slp_reg_callback()' - Empty SLPRegReport.
3076 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3077 SLPError errcode
, /* I - Error code, if any */
3078 void *cookie
) /* I - App data */
3089 * 'slp_url_callback()' - SLP service url callback
3092 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3094 SLPHandle hslp
, /* I - SLP handle */
3095 const char *srvurl
, /* I - URL of service */
3096 unsigned short lifetime
, /* I - Life of service */
3097 SLPError errcode
, /* I - Existing error code */
3098 void *cookie
) /* I - Pointer to service list */
3100 slpsrvurl_t
*s
, /* New service entry */
3101 **head
; /* Pointer to head of entry */
3105 * Let the compiler know we won't be using these vars...
3112 * Bail if there was an error
3115 if (errcode
!= SLP_OK
)
3119 * Grab the head of the list...
3122 head
= (slpsrvurl_t
**)cookie
;
3125 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3128 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3132 * Copy the SLP service URL...
3135 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3138 * Link the SLP service URL into the head of the list
3148 #endif /* HAVE_LIBSLP */
3152 * End of "$Id: dirsvc.c 5663 2006-06-15 20:36:42Z mike $".