2 * "$Id: dirsvc.c 5724 2006-07-12 19:42:35Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdLoadRemoteCache() - Load the remote printer cache.
27 * cupsdSaveRemoteCache() - Save the remote printer cache.
28 * cupsdSendBrowseDelete() - Send a "browse delete" message for a
30 * cupsdSendBrowseList() - Send new browsing information as necessary.
31 * cupsdStartBrowsing() - Start sending and receiving broadcast
33 * cupsdStartPolling() - Start polling servers as needed.
34 * cupsdStopBrowsing() - Stop sending and receiving broadcast
36 * cupsdStopPolling() - Stop polling servers as needed.
37 * cupsdUpdateCUPSBrowse() - Update the browse lists using the CUPS
39 * cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
40 * cupsdUpdatePolling() - Read status messages from the poll daemons.
41 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
42 * dequote() - Remote quotes from a string.
43 * process_browse_data() - Process new browse data.
44 * process_implicit_classes() - Create/update implicit classes as needed.
45 * send_cups_browse() - Send new browsing information using the
47 * send_ldap_browse() - Send LDAP printer registrations.
48 * send_slp_browse() - Register the specified printer with SLP.
49 * slp_attr_callback() - SLP attribute callback
50 * slp_dereg_printer() - SLPDereg() the specified printer
51 * slp_get_attr() - Get an attribute from an SLP registration.
52 * slp_reg_callback() - Empty SLPRegReport.
53 * slp_url_callback() - SLP service url callback
57 * Include necessary headers...
68 static char *dequote(char *d
, const char *s
, int dlen
);
69 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
70 char *resource
, int resourcelen
);
71 static void process_browse_data(const char *uri
, const char *host
,
72 const char *resource
, cups_ptype_t type
,
73 ipp_pstate_t state
, const char *location
,
74 const char *info
, const char *make_model
,
75 int num_attrs
, cups_option_t
*attrs
);
76 static void process_implicit_classes(void);
77 static void send_cups_browse(cupsd_printer_t
*p
);
79 static void send_ldap_browse(cupsd_printer_t
*p
);
80 #endif /* HAVE_LDAP */
82 static void send_slp_browse(cupsd_printer_t
*p
);
83 #endif /* HAVE_LIBSLP */
86 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
90 "printerMakeAndModel",
95 #endif /* HAVE_OPENLDAP */
103 * SLP service name for CUPS...
106 # define SLP_CUPS_SRVTYPE "service:printer"
107 # define SLP_CUPS_SRVLEN 15
111 * Printer service URL structure
114 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
116 struct _slpsrvurl_s
*next
; /* Next URL in list */
117 char url
[HTTP_MAX_URI
];
126 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
127 SLPError errcode
, void *cookie
);
128 static void slp_dereg_printer(cupsd_printer_t
*p
);
129 static int slp_get_attr(const char *attrlist
, const char *tag
,
131 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
133 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
134 unsigned short lifetime
,
135 SLPError errcode
, void *cookie
);
136 #endif /* HAVE_LIBSLP */
140 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
144 cupsdLoadRemoteCache(void)
146 cups_file_t
*fp
; /* remote.cache file */
147 int linenum
; /* Current line number */
148 char line
[1024], /* Line from file */
149 *value
, /* Pointer to value */
150 *valueptr
, /* Pointer into value */
151 scheme
[32], /* Scheme portion of URI */
152 username
[64], /* Username portion of URI */
154 /* Hostname portion of URI */
155 resource
[HTTP_MAX_URI
];
156 /* Resource portion of URI */
157 int port
; /* Port number */
158 cupsd_printer_t
*p
; /* Current printer */
159 time_t now
; /* Current time */
163 * Open the remote.cache file...
166 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
167 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
171 * Read printer configurations until we hit EOF...
178 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
181 * Decode the directive...
184 if (!strcasecmp(line
, "<Printer") ||
185 !strcasecmp(line
, "<DefaultPrinter"))
188 * <Printer name> or <DefaultPrinter name>
191 if (p
== NULL
&& value
)
194 * Add the printer and a base file type...
197 cupsdLogMessage(CUPSD_LOG_DEBUG
,
198 "cupsdLoadRemoteCache: Loading printer %s...", value
);
200 if ((p
= cupsdFindDest(value
)) != NULL
)
202 if (p
->type
& CUPS_PRINTER_CLASS
)
204 cupsdLogMessage(CUPSD_LOG_WARN
,
205 "Cached remote printer \"%s\" conflicts with "
213 p
= cupsdAddPrinter(value
);
216 p
->state
= IPP_PRINTER_IDLE
;
217 p
->type
|= CUPS_PRINTER_REMOTE
;
218 p
->browse_time
= now
;
219 p
->browse_expire
= now
+ BrowseTimeout
;
222 * Set the default printer as needed...
225 if (!strcasecmp(line
, "<DefaultPrinter"))
230 cupsdLogMessage(CUPSD_LOG_ERROR
,
231 "Syntax error on line %d of remote.cache.", linenum
);
235 else if (!strcasecmp(line
, "<Class") ||
236 !strcasecmp(line
, "<DefaultClass"))
239 * <Class name> or <DefaultClass name>
242 if (p
== NULL
&& value
)
245 * Add the printer and a base file type...
248 cupsdLogMessage(CUPSD_LOG_DEBUG
,
249 "cupsdLoadRemoteCache: Loading class %s...", value
);
251 if ((p
= cupsdFindDest(value
)) != NULL
)
252 p
->type
= CUPS_PRINTER_CLASS
;
254 p
= cupsdAddClass(value
);
257 p
->state
= IPP_PRINTER_IDLE
;
258 p
->type
|= CUPS_PRINTER_REMOTE
;
259 p
->browse_time
= now
;
260 p
->browse_expire
= now
+ BrowseTimeout
;
263 * Set the default printer as needed...
266 if (!strcasecmp(line
, "<DefaultClass"))
271 cupsdLogMessage(CUPSD_LOG_ERROR
,
272 "Syntax error on line %d of remote.cache.", linenum
);
276 else if (!strcasecmp(line
, "</Printer>") ||
277 !strcasecmp(line
, "</Class>"))
282 * Close out the current printer...
285 cupsdSetPrinterAttrs(p
);
291 cupsdLogMessage(CUPSD_LOG_ERROR
,
292 "Syntax error on line %d of remote.cache.", linenum
);
298 cupsdLogMessage(CUPSD_LOG_ERROR
,
299 "Syntax error on line %d of remote.cache.", linenum
);
302 else if (!strcasecmp(line
, "Info"))
305 cupsdSetString(&p
->info
, value
);
307 else if (!strcasecmp(line
, "MakeModel"))
310 cupsdSetString(&p
->make_model
, value
);
312 else if (!strcasecmp(line
, "Location"))
315 cupsdSetString(&p
->location
, value
);
317 else if (!strcasecmp(line
, "DeviceURI"))
321 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
322 username
, sizeof(username
), host
, sizeof(host
), &port
,
323 resource
, sizeof(resource
));
325 cupsdSetString(&p
->hostname
, host
);
326 cupsdSetString(&p
->uri
, value
);
327 cupsdSetString(&p
->device_uri
, value
);
331 cupsdLogMessage(CUPSD_LOG_ERROR
,
332 "Syntax error on line %d of remote.cache.", linenum
);
336 else if (!strcasecmp(line
, "Option") && value
)
342 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
345 cupsdLogMessage(CUPSD_LOG_ERROR
,
346 "Syntax error on line %d of remote.cache.", linenum
);
349 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
351 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
355 else if (!strcasecmp(line
, "State"))
358 * Set the initial queue state...
361 if (value
&& !strcasecmp(value
, "idle"))
362 p
->state
= IPP_PRINTER_IDLE
;
363 else if (value
&& !strcasecmp(value
, "stopped"))
364 p
->state
= IPP_PRINTER_STOPPED
;
367 cupsdLogMessage(CUPSD_LOG_ERROR
,
368 "Syntax error on line %d of remote.cache.", linenum
);
372 else if (!strcasecmp(line
, "StateMessage"))
375 * Set the initial queue state message...
379 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
381 else if (!strcasecmp(line
, "Accepting"))
384 * Set the initial accepting state...
388 (!strcasecmp(value
, "yes") ||
389 !strcasecmp(value
, "on") ||
390 !strcasecmp(value
, "true")))
393 (!strcasecmp(value
, "no") ||
394 !strcasecmp(value
, "off") ||
395 !strcasecmp(value
, "false")))
399 cupsdLogMessage(CUPSD_LOG_ERROR
,
400 "Syntax error on line %d of remote.cache.", linenum
);
404 else if (!strcasecmp(line
, "Type"))
407 p
->type
= atoi(value
);
410 cupsdLogMessage(CUPSD_LOG_ERROR
,
411 "Syntax error on line %d of remote.cache.", linenum
);
415 else if (!strcasecmp(line
, "BrowseTime"))
419 time_t t
= atoi(value
);
421 if (t
> p
->browse_expire
)
422 p
->browse_expire
= t
;
426 cupsdLogMessage(CUPSD_LOG_ERROR
,
427 "Syntax error on line %d of remote.cache.", linenum
);
431 else if (!strcasecmp(line
, "JobSheets"))
434 * Set the initial job sheets...
439 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
444 cupsdSetString(&p
->job_sheets
[0], value
);
446 while (isspace(*valueptr
& 255))
451 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
456 cupsdSetString(&p
->job_sheets
[1], value
);
461 cupsdLogMessage(CUPSD_LOG_ERROR
,
462 "Syntax error on line %d of remote.cache.", linenum
);
466 else if (!strcasecmp(line
, "AllowUser"))
471 cupsdAddPrinterUser(p
, value
);
475 cupsdLogMessage(CUPSD_LOG_ERROR
,
476 "Syntax error on line %d of remote.cache.", linenum
);
480 else if (!strcasecmp(line
, "DenyUser"))
485 cupsdAddPrinterUser(p
, value
);
489 cupsdLogMessage(CUPSD_LOG_ERROR
,
490 "Syntax error on line %d of remote.cache.", linenum
);
497 * Something else we don't understand...
500 cupsdLogMessage(CUPSD_LOG_ERROR
,
501 "Unknown configuration directive %s on line %d of remote.cache.",
509 * Do auto-classing if needed...
512 process_implicit_classes();
517 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
521 cupsdSaveRemoteCache(void)
523 int i
; /* Looping var */
524 cups_file_t
*fp
; /* printers.conf file */
525 char temp
[1024]; /* Temporary string */
526 cupsd_printer_t
*printer
; /* Current printer class */
527 time_t curtime
; /* Current time */
528 struct tm
*curdate
; /* Current date */
529 cups_option_t
*option
; /* Current option */
533 * Create the remote.cache file...
536 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
538 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
540 cupsdLogMessage(CUPSD_LOG_ERROR
,
541 "Unable to save remote.cache - %s", strerror(errno
));
545 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
548 * Restrict access to the file...
551 fchown(cupsFileNumber(fp
), getuid(), Group
);
552 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
555 * Write a small header to the file...
558 curtime
= time(NULL
);
559 curdate
= localtime(&curtime
);
560 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
562 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
563 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
566 * Write each local printer known to the system...
569 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
571 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
574 * Skip local destinations...
577 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
581 * Write printers as needed...
584 if (printer
== DefaultPrinter
)
585 cupsFilePuts(fp
, "<Default");
587 cupsFilePutChar(fp
, '<');
589 if (printer
->type
& CUPS_PRINTER_CLASS
)
590 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
592 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
594 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
596 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
599 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
601 if (printer
->make_model
)
602 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
604 if (printer
->location
)
605 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
607 if (printer
->device_uri
)
608 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
610 if (printer
->state
== IPP_PRINTER_STOPPED
)
612 cupsFilePuts(fp
, "State Stopped\n");
613 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
616 cupsFilePuts(fp
, "State Idle\n");
618 if (printer
->accepting
)
619 cupsFilePuts(fp
, "Accepting Yes\n");
621 cupsFilePuts(fp
, "Accepting No\n");
623 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
624 printer
->job_sheets
[1]);
626 for (i
= 0; i
< printer
->num_users
; i
++)
627 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
630 for (i
= printer
->num_options
, option
= printer
->options
;
633 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
635 if (printer
->type
& CUPS_PRINTER_CLASS
)
636 cupsFilePuts(fp
, "</Class>\n");
638 cupsFilePuts(fp
, "</Printer>\n");
646 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
650 cupsdSendBrowseDelete(
651 cupsd_printer_t
*p
) /* I - Printer to delete */
654 * Only announce if browsing is enabled and this is a local queue...
657 if (!Browsing
|| !p
->shared
||
658 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
662 * First mark the printer for deletion...
665 p
->type
|= CUPS_PRINTER_DELETE
;
668 * Announce the deletion...
671 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
674 if ((BrowseLocalProtocols
& BROWSE_SLP
) && BrowseSLPHandle
)
675 slp_dereg_printer(p
);
676 #endif /* HAVE_LIBSLP */
681 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
685 cupsdSendBrowseList(void)
687 int count
; /* Number of dests to update */
688 cupsd_printer_t
*p
; /* Current printer */
689 time_t ut
, /* Minimum update time */
690 to
; /* Timeout time */
693 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
697 * Compute the update and timeout times...
701 ut
= to
- BrowseInterval
;
704 * Figure out how many printers need an update...
707 if (BrowseInterval
> 0)
709 int max_count
; /* Maximum number to update */
713 * Throttle the number of printers we'll be updating this time
714 * around based on the number of queues that need updating and
715 * the maximum number of queues to update each second...
718 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
720 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
721 count
< max_count
&& p
!= NULL
;
722 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
723 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
724 p
->shared
&& p
->browse_time
< ut
)
728 * Loop through all of the printers and send local updates as needed...
732 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
734 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
738 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
741 * Check for wraparound...
745 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
749 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
752 else if (p
->browse_time
< ut
)
755 * Need to send an update...
760 p
->browse_time
= time(NULL
);
762 if (BrowseLocalProtocols
& BROWSE_CUPS
)
766 if (BrowseLocalProtocols
& BROWSE_SLP
)
768 #endif /* HAVE_LIBSLP */
771 if (BrowseLocalProtocols
& BROWSE_LDAP
)
773 #endif /* HAVE_LDAP */
778 * Save where we left off so that all printers get updated...
785 * Loop through all of the printers and send local updates as needed...
788 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
790 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
793 * If this is a remote queue, see if it needs to be timed out...
796 if (p
->type
& CUPS_PRINTER_REMOTE
)
798 if (p
->browse_expire
< to
)
800 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
801 "%s \'%s\' deleted by directory services (timeout).",
802 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
805 cupsdLogMessage(CUPSD_LOG_DEBUG
,
806 "Remote destination \"%s\" has timed out; "
810 cupsArraySave(Printers
);
811 cupsdDeletePrinter(p
, 1);
812 cupsArrayRestore(Printers
);
820 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
824 cupsdStartBrowsing(void)
826 int val
; /* Socket option value */
827 struct sockaddr_in addr
; /* Broadcast address */
832 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
835 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
837 if (BrowseSocket
< 0)
840 * Create the broadcast socket...
843 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
845 cupsdLogMessage(CUPSD_LOG_ERROR
,
846 "cupsdStartBrowsing: Unable to create broadcast "
847 "socket - %s.", strerror(errno
));
848 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
849 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
854 * Bind the socket to browse port...
857 memset(&addr
, 0, sizeof(addr
));
858 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
859 addr
.sin_family
= AF_INET
;
860 addr
.sin_port
= htons(BrowsePort
);
862 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
864 cupsdLogMessage(CUPSD_LOG_ERROR
,
865 "cupsdStartBrowsing: Unable to bind broadcast "
866 "socket - %s.", strerror(errno
));
869 closesocket(BrowseSocket
);
875 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
876 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
882 * Set the "broadcast" flag...
886 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
888 cupsdLogMessage(CUPSD_LOG_ERROR
,
889 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
893 closesocket(BrowseSocket
);
899 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
900 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
905 * Close the socket on exec...
908 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
911 * Finally, add the socket to the input selection set as needed...
914 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
917 * We only listen if we want remote printers...
920 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
921 "cupsdStartBrowsing: Adding fd %d to InputSet...",
924 FD_SET(BrowseSocket
, InputSet
);
931 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
937 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
939 cupsdLogMessage(CUPSD_LOG_ERROR
,
940 "Unable to open an SLP handle; disabling SLP browsing!");
941 BrowseLocalProtocols
&= ~BROWSE_SLP
;
942 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
945 BrowseSLPRefresh
= 0;
948 BrowseSLPHandle
= NULL
;
949 #endif /* HAVE_LIBSLP */
952 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
956 cupsdLogMessage(CUPSD_LOG_ERROR
,
957 "Need to set BrowseLDAPDN to use LDAP browsing!");
958 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
959 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
964 * Open LDAP handle...
967 int rc
; /* LDAP API status */
968 int version
= 3; /* LDAP version */
969 struct berval bv
= {0, ""}; /* SASL bind value */
973 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
976 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
977 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
979 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
981 if (rc
!= LDAP_SUCCESS
)
983 cupsdLogMessage(CUPSD_LOG_ERROR
,
984 "Unable to initialize LDAP; disabling LDAP browsing!");
985 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
986 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
988 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
989 (const void *)&version
) != LDAP_SUCCESS
)
991 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
992 BrowseLDAPHandle
= NULL
;
993 cupsdLogMessage(CUPSD_LOG_ERROR
,
994 "Unable to set LDAP protocol version; "
995 "disabling LDAP browsing!");
996 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
997 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1001 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1002 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1005 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
1006 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1008 if (rc
!= LDAP_SUCCESS
)
1010 cupsdLogMessage(CUPSD_LOG_ERROR
,
1011 "Unable to bind to LDAP server; "
1012 "disabling LDAP browsing!");
1013 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1014 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1015 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1020 BrowseLDAPRefresh
= 0;
1022 #endif /* HAVE_OPENLDAP */
1027 * 'cupsdStartPolling()' - Start polling servers as needed.
1031 cupsdStartPolling(void)
1033 int i
; /* Looping var */
1034 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1035 char polld
[1024]; /* Poll daemon path */
1036 char sport
[10]; /* Server port */
1037 char bport
[10]; /* Browser port */
1038 char interval
[10]; /* Poll interval */
1039 int statusfds
[2]; /* Status pipe */
1040 char *argv
[6]; /* Arguments */
1041 char *envp
[100]; /* Environment */
1045 * Don't do anything if we aren't polling...
1051 PollStatusBuffer
= NULL
;
1056 * Setup string arguments for polld, port and interval options.
1059 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1061 sprintf(bport
, "%d", BrowsePort
);
1064 sprintf(interval
, "%d", BrowseInterval
);
1066 strcpy(interval
, "30");
1068 argv
[0] = "cups-polld";
1074 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1077 * Create a pipe that receives the status messages from each
1081 if (cupsdOpenPipe(statusfds
))
1083 cupsdLogMessage(CUPSD_LOG_ERROR
,
1084 "Unable to create polling status pipes - %s.",
1087 PollStatusBuffer
= NULL
;
1091 PollPipe
= statusfds
[0];
1092 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1095 * Run each polling daemon, redirecting stderr to the polling pipe...
1098 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1100 sprintf(sport
, "%d", pollp
->port
);
1102 argv
[1] = pollp
->hostname
;
1104 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1105 0, &(pollp
->pid
)) < 0)
1107 cupsdLogMessage(CUPSD_LOG_ERROR
,
1108 "cupsdStartPolling: Unable to fork polling daemon - %s",
1114 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1115 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1116 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1119 close(statusfds
[1]);
1122 * Finally, add the pipe to the input selection set...
1125 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1126 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1128 FD_SET(PollPipe
, InputSet
);
1133 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1137 cupsdStopBrowsing(void)
1139 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1142 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1146 * Close the socket and remove it from the input selection set.
1150 closesocket(BrowseSocket
);
1152 close(BrowseSocket
);
1155 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1156 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1159 FD_CLR(BrowseSocket
, InputSet
);
1164 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1168 * Close SLP handle...
1171 SLPClose(BrowseSLPHandle
);
1172 BrowseSLPHandle
= NULL
;
1174 #endif /* HAVE_LIBSLP */
1176 #ifdef HAVE_OPENLDAP
1177 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1180 ldap_unbind(BrowseLDAPHandle
);
1181 BrowseLDAPHandle
= NULL
;
1183 #endif /* HAVE_OPENLDAP */
1188 * 'cupsdStopPolling()' - Stop polling servers as needed.
1192 cupsdStopPolling(void)
1194 int i
; /* Looping var */
1195 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1200 cupsdStatBufDelete(PollStatusBuffer
);
1203 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1204 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1205 FD_CLR(PollPipe
, InputSet
);
1208 PollStatusBuffer
= NULL
;
1211 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1213 cupsdEndProcess(pollp
->pid
, 0);
1218 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1222 cupsdUpdateCUPSBrowse(void)
1224 int i
; /* Looping var */
1225 int auth
; /* Authorization status */
1226 int len
; /* Length of name string */
1227 int bytes
; /* Number of bytes left */
1228 char packet
[1541], /* Broadcast packet */
1229 *pptr
; /* Pointer into packet */
1230 socklen_t srclen
; /* Length of source address */
1231 http_addr_t srcaddr
; /* Source address */
1232 char srcname
[1024]; /* Source hostname */
1233 unsigned address
[4]; /* Source address */
1234 unsigned type
; /* Printer type */
1235 unsigned state
; /* Printer state */
1236 char uri
[HTTP_MAX_URI
], /* Printer URI */
1237 host
[HTTP_MAX_URI
], /* Host portion of URI */
1238 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1239 info
[IPP_MAX_NAME
], /* Information string */
1240 location
[IPP_MAX_NAME
], /* Location string */
1241 make_model
[IPP_MAX_NAME
];/* Make and model string */
1242 int num_attrs
; /* Number of attributes */
1243 cups_option_t
*attrs
; /* Attributes */
1247 * Read a packet from the browse socket...
1250 srclen
= sizeof(srcaddr
);
1251 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1252 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1255 * "Connection refused" is returned under Linux if the destination port
1256 * or address is unreachable from a previous sendto(); check for the
1257 * error here and ignore it for now...
1260 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1262 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1264 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1266 cupsdStopBrowsing();
1273 packet
[bytes
] = '\0';
1276 * If we're about to sleep, ignore incoming browse packets.
1283 * Figure out where it came from...
1287 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1289 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1290 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1291 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1292 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1295 #endif /* AF_INET6 */
1300 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1303 if (HostNameLookups
)
1304 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1306 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1308 len
= strlen(srcname
);
1316 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1319 * Access from localhost (127.0.0.1) is always allowed...
1327 * Do authorization checks on the domain/address...
1330 switch (BrowseACL
->order_type
)
1333 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1336 case AUTH_ALLOW
: /* Order Deny,Allow */
1339 if (cupsdCheckAuth(address
, srcname
, len
,
1340 BrowseACL
->num_deny
, BrowseACL
->deny
))
1343 if (cupsdCheckAuth(address
, srcname
, len
,
1344 BrowseACL
->num_allow
, BrowseACL
->allow
))
1348 case AUTH_DENY
: /* Order Allow,Deny */
1351 if (cupsdCheckAuth(address
, srcname
, len
,
1352 BrowseACL
->num_allow
, BrowseACL
->allow
))
1355 if (cupsdCheckAuth(address
, srcname
, len
,
1356 BrowseACL
->num_deny
, BrowseACL
->deny
))
1365 if (auth
== AUTH_DENY
)
1367 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1368 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1373 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1374 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1381 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1383 cupsdLogMessage(CUPSD_LOG_WARN
,
1384 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1388 strcpy(location
, "Location Unknown");
1389 strcpy(info
, "No Information Available");
1390 make_model
[0] = '\0';
1394 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1397 * Have extended information; can't use sscanf for it because not all
1398 * sscanf's allow empty strings with %[^\"]...
1401 for (i
= 0, pptr
++;
1402 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1404 location
[i
] = *pptr
;
1412 while (*pptr
&& isspace(*pptr
& 255))
1417 for (i
= 0, pptr
++;
1418 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1427 while (*pptr
&& isspace(*pptr
& 255))
1432 for (i
= 0, pptr
++;
1433 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1435 make_model
[i
] = *pptr
;
1440 make_model
[i
] = '\0';
1443 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1449 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1450 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1451 type
, state
, uri
, location
, info
, make_model
));
1454 * Pull the URI apart to see if this is a local or remote printer...
1457 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1459 cupsFreeOptions(num_attrs
, attrs
);
1467 for (i
= 0; i
< NumRelays
; i
++)
1468 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1469 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1470 (struct sockaddr
*)&(Relays
[i
].to
),
1471 sizeof(http_addr_t
)) <= 0)
1473 cupsdLogMessage(CUPSD_LOG_ERROR
,
1474 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1475 i
+ 1, strerror(errno
));
1476 cupsFreeOptions(num_attrs
, attrs
);
1481 * Process the browse data...
1484 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1485 (ipp_pstate_t
)state
, location
, info
, make_model
,
1490 #ifdef HAVE_OPENLDAP
1492 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1496 cupsdUpdateLDAPBrowse(void)
1498 char uri
[HTTP_MAX_URI
], /* Printer URI */
1499 host
[HTTP_MAX_URI
], /* Hostname */
1500 resource
[HTTP_MAX_URI
], /* Resource path */
1501 location
[1024], /* Printer location */
1502 info
[1024], /* Printer information */
1503 make_model
[1024], /* Printer make and model */
1504 **value
; /* Holds the returned data from LDAP */
1505 int type
; /* Printer type */
1506 int rc
; /* LDAP status */
1507 int limit
; /* Size limit */
1508 LDAPMessage
*res
, /* LDAP search results */
1509 *e
; /* Current entry from search */
1513 * Search for printers...
1516 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1518 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1520 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1521 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1522 if (rc
!= LDAP_SUCCESS
)
1524 cupsdLogMessage(CUPSD_LOG_ERROR
,
1525 "LDAP search returned error %d: %s", rc
,
1526 ldap_err2string(rc
));
1530 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1531 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1536 * Loop through the available printers...
1539 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1541 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1544 * Get the required values from this entry...
1547 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1548 "printerDescription")) == NULL
)
1551 strlcpy(info
, *value
, sizeof(info
));
1552 ldap_value_free(value
);
1554 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1555 "printerLocation")) == NULL
)
1558 strlcpy(location
, *value
, sizeof(location
));
1559 ldap_value_free(value
);
1561 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1562 "printerMakeAndModel")) == NULL
)
1565 strlcpy(make_model
, *value
, sizeof(make_model
));
1566 ldap_value_free(value
);
1568 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1569 "printerType")) == NULL
)
1572 type
= atoi(*value
);
1573 ldap_value_free(value
);
1575 if ((value
= ldap_get_values(BrowseLDAPHandle
, e
,
1576 "printerURI")) == NULL
)
1579 strlcpy(uri
, *value
, sizeof(uri
));
1580 ldap_value_free(value
);
1583 * Process the entry as browse data...
1586 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1587 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1588 location
, info
, make_model
, 0, NULL
);
1592 #endif /* HAVE_OPENLDAP */
1596 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1600 cupsdUpdatePolling(void)
1602 char *ptr
, /* Pointer to end of line in buffer */
1603 message
[1024]; /* Pointer to message text */
1604 int loglevel
; /* Log level for message */
1607 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1608 message
, sizeof(message
))) != NULL
)
1609 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1615 * All polling processes have died; stop polling...
1618 cupsdLogMessage(CUPSD_LOG_ERROR
,
1619 "cupsdUpdatePolling: all polling processes have exited!");
1627 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1631 cupsdUpdateSLPBrowse(void)
1633 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1634 *next
; /* Next service in list */
1635 cupsd_printer_t p
; /* Printer information */
1636 const char *uri
; /* Pointer to printer URI */
1637 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1638 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1642 * Reset the refresh time...
1645 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1648 * Poll for remote printers using SLP...
1653 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1654 slp_url_callback
, &s
);
1657 * Loop through the list of available printers...
1663 * Save the "next" pointer...
1669 * Load a cupsd_printer_t structure with the SLP service attributes...
1672 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1675 * Process this printer entry...
1678 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1680 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1683 * Pull the URI apart to see if this is a local or remote printer...
1686 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1687 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1688 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1692 * Free this listing...
1695 cupsdClearString(&p
.info
);
1696 cupsdClearString(&p
.location
);
1697 cupsdClearString(&p
.make_model
);
1702 #endif /* HAVE_LIBSLP */
1706 * 'dequote()' - Remote quotes from a string.
1709 static char * /* O - Dequoted string */
1710 dequote(char *d
, /* I - Destination string */
1711 const char *s
, /* I - Source string */
1712 int dlen
) /* I - Destination length */
1714 char *dptr
; /* Pointer into destination */
1719 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1736 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1739 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1740 is_local_queue(const char *uri
, /* I - Printer URI */
1741 char *host
, /* O - Host string */
1742 int hostlen
, /* I - Length of host buffer */
1743 char *resource
, /* O - Resource string */
1744 int resourcelen
) /* I - Length of resource buffer */
1746 char scheme
[32], /* Scheme portion of URI */
1747 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1748 int port
; /* Port portion of URI */
1749 cupsd_netif_t
*iface
; /* Network interface */
1753 * Pull the URI apart to see if this is a local or remote printer...
1756 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1757 username
, sizeof(username
), host
, hostlen
, &port
,
1758 resource
, resourcelen
) < HTTP_URI_OK
)
1761 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1764 * Check for local server addresses...
1767 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1772 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1774 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1775 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1779 * If we get here, the printer is remote...
1787 * 'process_browse_data()' - Process new browse data.
1791 process_browse_data(
1792 const char *uri
, /* I - URI of printer/class */
1793 const char *host
, /* I - Hostname */
1794 const char *resource
, /* I - Resource path */
1795 cups_ptype_t type
, /* I - Printer type */
1796 ipp_pstate_t state
, /* I - Printer state */
1797 const char *location
, /* I - Printer location */
1798 const char *info
, /* I - Printer information */
1799 const char *make_model
, /* I - Printer make and model */
1800 int num_attrs
, /* I - Number of attributes */
1801 cups_option_t
*attrs
) /* I - Attributes */
1803 int i
; /* Looping var */
1804 int update
; /* Update printer attributes? */
1805 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1806 name
[IPP_MAX_NAME
], /* Name of printer */
1807 newname
[IPP_MAX_NAME
], /* New name of printer */
1808 *hptr
, /* Pointer into hostname */
1809 *sptr
; /* Pointer into ServerName */
1810 char local_make_model
[IPP_MAX_NAME
];
1811 /* Local make and model */
1812 cupsd_printer_t
*p
; /* Printer information */
1813 const char *ipp_options
, /* ipp-options value */
1814 *lease_duration
; /* lease-duration value */
1818 * Determine if the URI contains any illegal characters in it...
1821 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1822 (strncmp(resource
, "/printers/", 10) &&
1823 strncmp(resource
, "/classes/", 9)))
1825 cupsdLogMessage(CUPSD_LOG_ERROR
,
1826 "process_browse_data: Bad printer URI in browse data: %s",
1831 if (strchr(resource
, '?') ||
1832 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1833 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1835 cupsdLogMessage(CUPSD_LOG_ERROR
,
1836 "process_browse_data: Bad resource in browse data: %s",
1842 * OK, this isn't a local printer; add any remote options...
1845 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1847 if (BrowseRemoteOptions
)
1849 if (BrowseRemoteOptions
[0] == '?')
1852 * Override server-supplied options...
1855 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1857 else if (ipp_options
)
1860 * Combine the server and local options...
1863 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1864 BrowseRemoteOptions
);
1869 * Just use the local options...
1872 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1877 else if (ipp_options
)
1880 * Just use the server-supplied options...
1883 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1888 * See if we already have it listed in the Printers list, and add it if not...
1891 type
|= CUPS_PRINTER_REMOTE
;
1892 type
&= ~CUPS_PRINTER_IMPLICIT
;
1894 hptr
= strchr(host
, '.');
1895 sptr
= strchr(ServerName
, '.');
1897 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
1900 * Strip the common domain name components...
1903 while (hptr
!= NULL
)
1905 if (!strcasecmp(hptr
, sptr
))
1911 hptr
= strchr(hptr
+ 1, '.');
1915 if (type
& CUPS_PRINTER_CLASS
)
1918 * Remote destination is a class...
1921 if (!strncmp(resource
, "/classes/", 9))
1922 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1926 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1928 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1930 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1933 * Nope, this isn't the same host; if the hostname isn't the local host,
1934 * add it to the other class and then find a class using the full host
1938 if (p
->type
& CUPS_PRINTER_REMOTE
)
1940 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1941 "Renamed remote class \"%s\" to \"%s@%s\"...",
1942 p
->name
, p
->name
, p
->hostname
);
1943 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1944 "Class \'%s\' deleted by directory services.",
1947 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1948 cupsdRenamePrinter(p
, newname
);
1950 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1951 "Class \'%s\' added by directory services.",
1957 else if (!p
->hostname
)
1960 * Hostname not set, so this must be a cached remote printer
1961 * that was created for a pending print job...
1964 cupsdSetString(&p
->hostname
, host
);
1965 cupsdSetString(&p
->uri
, uri
);
1966 cupsdSetString(&p
->device_uri
, uri
);
1973 * Use the short name for this shared class.
1976 strlcpy(name
, resource
+ 9, sizeof(name
));
1979 else if (p
&& !p
->hostname
)
1982 * Hostname not set, so this must be a cached remote printer
1983 * that was created for a pending print job...
1986 cupsdSetString(&p
->hostname
, host
);
1987 cupsdSetString(&p
->uri
, uri
);
1988 cupsdSetString(&p
->device_uri
, uri
);
1995 * Class doesn't exist; add it...
1998 p
= cupsdAddClass(name
);
2000 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote class \"%s\"...", name
);
2002 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2003 "Class \'%s\' added by directory services.", name
);
2006 * Force the URI to point to the real server...
2009 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2011 cupsdSetString(&p
->uri
, uri
);
2012 cupsdSetString(&p
->device_uri
, uri
);
2013 cupsdSetString(&p
->hostname
, host
);
2021 * Remote destination is a printer...
2024 if (!strncmp(resource
, "/printers/", 10))
2025 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2029 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2031 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2033 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2036 * Nope, this isn't the same host; if the hostname isn't the local host,
2037 * add it to the other printer and then find a printer using the full host
2041 if (p
->type
& CUPS_PRINTER_REMOTE
)
2043 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2044 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2045 p
->name
, p
->name
, p
->hostname
);
2046 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2047 "Printer \'%s\' deleted by directory services.",
2050 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2051 cupsdRenamePrinter(p
, newname
);
2053 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2054 "Printer \'%s\' added by directory services.",
2060 else if (!p
->hostname
)
2063 * Hostname not set, so this must be a cached remote printer
2064 * that was created for a pending print job...
2067 cupsdSetString(&p
->hostname
, host
);
2068 cupsdSetString(&p
->uri
, uri
);
2069 cupsdSetString(&p
->device_uri
, uri
);
2076 * Use the short name for this shared printer.
2079 strlcpy(name
, resource
+ 10, sizeof(name
));
2082 else if (p
&& !p
->hostname
)
2085 * Hostname not set, so this must be a cached remote printer
2086 * that was created for a pending print job...
2089 cupsdSetString(&p
->hostname
, host
);
2090 cupsdSetString(&p
->uri
, uri
);
2091 cupsdSetString(&p
->device_uri
, uri
);
2098 * Printer doesn't exist; add it...
2101 p
= cupsdAddPrinter(name
);
2103 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2104 "Printer \'%s\' added by directory services.", name
);
2106 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote printer \"%s\"...", name
);
2109 * Force the URI to point to the real server...
2112 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2114 cupsdSetString(&p
->hostname
, host
);
2115 cupsdSetString(&p
->uri
, uri
);
2116 cupsdSetString(&p
->device_uri
, uri
);
2123 * Update the state...
2127 p
->browse_time
= time(NULL
);
2129 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2133 * Grab the lease-duration for the browse data; anything less then 1
2134 * second or more than 1 week gets the default BrowseTimeout...
2137 i
= atoi(lease_duration
);
2138 if (i
< 1 || i
> 604800)
2141 p
->browse_expire
= p
->browse_time
+ i
;
2144 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2146 if (type
& CUPS_PRINTER_REJECTING
)
2148 type
&= ~CUPS_PRINTER_REJECTING
;
2156 else if (!p
->accepting
)
2162 if (p
->type
!= type
)
2168 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2170 cupsdSetString(&p
->location
, location
);
2174 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2176 cupsdSetString(&p
->info
, info
);
2180 if (!make_model
|| !make_model
[0])
2182 if (type
& CUPS_PRINTER_CLASS
)
2183 snprintf(local_make_model
, sizeof(local_make_model
),
2184 "Remote Class on %s", host
);
2186 snprintf(local_make_model
, sizeof(local_make_model
),
2187 "Remote Printer on %s", host
);
2190 snprintf(local_make_model
, sizeof(local_make_model
),
2191 "%s on %s", make_model
, host
);
2193 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2195 cupsdSetString(&p
->make_model
, local_make_model
);
2201 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2204 * See if we need to update the attributes...
2207 if (p
->num_options
!= num_attrs
)
2211 for (i
= 0; i
< num_attrs
; i
++)
2212 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2213 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2214 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2223 * Free the old options...
2226 cupsFreeOptions(p
->num_options
, p
->options
);
2229 p
->num_options
= num_attrs
;
2232 if (type
& CUPS_PRINTER_DELETE
)
2234 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2235 "%s \'%s\' deleted by directory services.",
2236 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2238 cupsdExpireSubscriptions(p
, NULL
);
2240 cupsdDeletePrinter(p
, 1);
2241 cupsdUpdateImplicitClasses();
2245 cupsdSetPrinterAttrs(p
);
2246 cupsdUpdateImplicitClasses();
2250 * See if we have a default printer... If not, make the first network
2251 * default printer the default.
2254 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2257 * Find the first network default printer and use it...
2260 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2262 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2263 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2271 * Do auto-classing if needed...
2274 process_implicit_classes();
2277 * Update the printcap file...
2280 cupsdWritePrintcap();
2285 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2289 process_implicit_classes(void)
2291 int i
; /* Looping var */
2292 int update
; /* Update printer attributes? */
2293 char name
[IPP_MAX_NAME
], /* Name of printer */
2294 *hptr
; /* Pointer into hostname */
2295 cupsd_printer_t
*p
, /* Printer information */
2296 *pclass
, /* Printer class */
2297 *first
; /* First printer in class */
2298 int offset
, /* Offset of name */
2299 len
; /* Length of name */
2302 if (!ImplicitClasses
|| !Printers
)
2306 * Loop through all available printers and create classes as needed...
2309 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2310 update
= 0, pclass
= NULL
, first
= NULL
;
2312 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2315 * Skip implicit classes...
2318 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2325 * If len == 0, get the length of this printer name up to the "@"
2329 cupsArraySave(Printers
);
2332 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2333 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2336 * We have more than one printer with the same name; see if
2337 * we have a class, and if this printer is a member...
2340 if (pclass
&& strcasecmp(pclass
->name
, name
))
2343 cupsdSetPrinterAttrs(pclass
);
2349 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2352 * Need to add the class...
2355 pclass
= cupsdAddPrinter(name
);
2356 cupsArrayAdd(ImplicitPrinters
, pclass
);
2358 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2359 pclass
->accepting
= 1;
2360 pclass
->state
= IPP_PRINTER_IDLE
;
2362 cupsdSetString(&pclass
->location
, p
->location
);
2363 cupsdSetString(&pclass
->info
, p
->info
);
2367 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
2369 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2370 "Implicit class \'%s\' added by directory services.",
2376 for (i
= 0; i
< pclass
->num_printers
; i
++)
2377 if (pclass
->printers
[i
] == first
)
2380 if (i
>= pclass
->num_printers
)
2382 first
->in_implicit_class
= 1;
2383 cupsdAddPrinterToClass(pclass
, first
);
2389 for (i
= 0; i
< pclass
->num_printers
; i
++)
2390 if (pclass
->printers
[i
] == p
)
2393 if (i
>= pclass
->num_printers
)
2395 p
->in_implicit_class
= 1;
2396 cupsdAddPrinterToClass(pclass
, p
);
2403 * First time around; just get name length and mark it as first
2407 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2408 len
= hptr
- p
->name
;
2410 len
= strlen(p
->name
);
2412 strncpy(name
, p
->name
, len
);
2416 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2417 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2420 * Can't use same name as a local printer; add "Any" to the
2421 * front of the name, unless we have explicitly disabled
2422 * the "ImplicitAnyClasses"...
2425 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2428 * Add "Any" to the class name...
2431 strcpy(name
, "Any");
2432 strncpy(name
+ 3, p
->name
, len
);
2433 name
[len
+ 3] = '\0';
2439 * Don't create an implicit class if we have a local printer
2440 * with the same name...
2444 cupsArrayRestore(Printers
);
2452 cupsArrayRestore(Printers
);
2456 * Update the last printer class as needed...
2459 if (pclass
&& update
)
2460 cupsdSetPrinterAttrs(pclass
);
2465 * 'send_cups_browse()' - Send new browsing information using the CUPS
2470 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2472 int i
; /* Looping var */
2473 cups_ptype_t type
; /* Printer type */
2474 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2475 int bytes
; /* Length of packet */
2476 char packet
[1453], /* Browse data packet */
2477 uri
[1024], /* Printer URI */
2478 location
[1024], /* printer-location */
2479 info
[1024], /* printer-info */
2481 /* printer-make-and-model */
2482 cupsd_netif_t
*iface
; /* Network interface */
2486 * Figure out the printer type value...
2489 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2492 type
|= CUPS_PRINTER_REJECTING
;
2494 if (p
== DefaultPrinter
)
2495 type
|= CUPS_PRINTER_DEFAULT
;
2498 * Remove quotes from printer-info, printer-location, and
2499 * printer-make-and-model attributes...
2502 dequote(location
, p
->location
, sizeof(location
));
2503 dequote(info
, p
->info
, sizeof(info
));
2506 dequote(make_model
, p
->make_model
, sizeof(make_model
));
2507 else if (p
->type
& CUPS_PRINTER_CLASS
)
2509 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
2510 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
2512 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
2515 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
2517 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
2520 * Send a packet to each browse address...
2523 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2527 * Send the browse packet to one or more interfaces...
2530 if (!strcmp(b
->iface
, "*"))
2533 * Send to all local interfaces...
2538 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2540 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2543 * Only send to local, IPv4 interfaces...
2546 if (!iface
->is_local
|| !iface
->port
||
2547 iface
->address
.addr
.sa_family
!= AF_INET
)
2550 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2551 iface
->hostname
, iface
->port
,
2552 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
2555 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2556 type
, p
->state
, uri
, location
, info
, make_model
,
2557 p
->browse_attrs
? p
->browse_attrs
: "");
2559 bytes
= strlen(packet
);
2561 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2562 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2563 iface
->name
, packet
);
2565 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2567 sendto(BrowseSocket
, packet
, bytes
, 0,
2568 (struct sockaddr
*)&(iface
->broadcast
),
2569 sizeof(struct sockaddr_in
));
2572 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2575 * Send to the named interface using the IPv4 address...
2579 if (strcmp(b
->iface
, iface
->name
))
2584 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2587 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2591 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2592 iface
->hostname
, iface
->port
,
2593 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2596 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2597 type
, p
->state
, uri
, location
, info
, make_model
,
2598 p
->browse_attrs
? p
->browse_attrs
: "");
2600 bytes
= strlen(packet
);
2602 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2603 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2604 iface
->name
, packet
);
2606 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2608 sendto(BrowseSocket
, packet
, bytes
, 0,
2609 (struct sockaddr
*)&(iface
->broadcast
),
2610 sizeof(struct sockaddr_in
));
2617 * Send the browse packet to the indicated address using
2618 * the default server name...
2621 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2622 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2623 p
->browse_attrs
? p
->browse_attrs
: "");
2625 bytes
= strlen(packet
);
2626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2627 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2629 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2630 (struct sockaddr
*)&(b
->to
),
2631 sizeof(struct sockaddr_in
)) <= 0)
2634 * Unable to send browse packet, so remove this address from the
2638 cupsdLogMessage(CUPSD_LOG_ERROR
,
2639 "cupsdSendBrowseList: sendto failed for browser "
2641 (int)(b
- Browsers
+ 1), strerror(errno
));
2644 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2653 #ifdef HAVE_OPENLDAP
2655 * 'send_ldap_browse()' - Send LDAP printer registrations.
2659 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2661 int i
; /* Looping var... */
2662 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
2663 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
2664 LDAPMessage
*res
; /* Search result token */
2665 char *cn_value
[2], /* Change records */
2671 typestring
[255], /* String to hold printer-type */
2672 filter
[256], /* Search filter for possible UPDATEs */
2673 dn
[1024]; /* DN of the printer we are adding */
2674 int rc
; /* LDAP status */
2675 static const char * const objectClass_values
[] =
2676 { /* The 3 objectClass's we use in */
2677 "top", /* our LDAP entries */
2683 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
2686 * Everything in ldap is ** so we fudge around it...
2689 sprintf(typestring
, "%u", p
->type
);
2691 cn_value
[0] = p
->name
;
2693 info
[0] = p
->info
? p
->info
: "Unknown";
2695 location
[0] = p
->location
? p
->location
: "Unknown";
2697 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
2698 make_model
[1] = NULL
;
2699 type
[0] = typestring
;
2704 snprintf(filter
, sizeof(filter
),
2705 "(&(objectclass=cupsPrinter)(printerURI=%s))", p
->uri
);
2707 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2708 filter
, (char **)ldap_attrs
, 0, &res
);
2709 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
2712 mods
[0].mod_type
= "cn";
2713 mods
[0].mod_values
= cn_value
;
2714 mods
[1].mod_type
= "printerDescription";
2715 mods
[1].mod_values
= info
;
2716 mods
[2].mod_type
= "printerURI";
2717 mods
[2].mod_values
= uri
;
2718 mods
[3].mod_type
= "printerLocation";
2719 mods
[3].mod_values
= location
;
2720 mods
[4].mod_type
= "printerMakeAndModel";
2721 mods
[4].mod_values
= make_model
;
2722 mods
[5].mod_type
= "printerType";
2723 mods
[5].mod_values
= type
;
2724 mods
[6].mod_type
= "objectClass";
2725 mods
[6].mod_values
= (char **)objectClass_values
;
2727 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->name
, BrowseLDAPDN
);
2728 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
2730 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
2733 * Printer has already been registered, modify the current
2737 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2738 "send_ldap_browse: Replacing entry...");
2740 for (i
= 0; i
< 7; i
++)
2742 pmods
[i
] = mods
+ i
;
2743 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2747 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2748 cupsdLogMessage(CUPSD_LOG_ERROR
,
2749 "LDAP modify for %s failed with status %d: %s",
2750 p
->name
, rc
, ldap_err2string(rc
));
2755 * Printer has never been registered, add the current
2759 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2760 "send_ldap_browse: Adding entry...");
2762 for (i
= 0; i
< 7; i
++)
2764 pmods
[i
] = mods
+ i
;
2765 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
2769 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2770 cupsdLogMessage(CUPSD_LOG_ERROR
,
2771 "LDAP add for %s failed with status %d: %s",
2772 p
->name
, rc
, ldap_err2string(rc
));
2775 #endif /* HAVE_OPENLDAP */
2780 * 'send_slp_browse()' - Register the specified printer with SLP.
2784 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2786 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
2787 attrs
[8192], /* Printer attributes */
2788 finishings
[1024], /* Finishings to support */
2789 make_model
[IPP_MAX_NAME
* 2],
2790 /* Make and model, quoted */
2791 location
[IPP_MAX_NAME
* 2],
2792 /* Location, quoted */
2793 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
2794 *src
, /* Pointer to original string */
2795 *dst
; /* Pointer to destination string */
2796 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
2797 SLPError error
; /* SLP error, if any */
2800 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
2804 * Make the SLP service URL that conforms to the IANA
2805 * 'printer:' template.
2808 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2810 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
2813 * Figure out the finishings string...
2816 if (p
->type
& CUPS_PRINTER_STAPLE
)
2817 strcpy(finishings
, "staple");
2819 finishings
[0] = '\0';
2821 if (p
->type
& CUPS_PRINTER_BIND
)
2824 strlcat(finishings
, ",bind", sizeof(finishings
));
2826 strcpy(finishings
, "bind");
2829 if (p
->type
& CUPS_PRINTER_PUNCH
)
2832 strlcat(finishings
, ",punch", sizeof(finishings
));
2834 strcpy(finishings
, "punch");
2837 if (p
->type
& CUPS_PRINTER_COVER
)
2840 strlcat(finishings
, ",cover", sizeof(finishings
));
2842 strcpy(finishings
, "cover");
2845 if (p
->type
& CUPS_PRINTER_SORT
)
2848 strlcat(finishings
, ",sort", sizeof(finishings
));
2850 strcpy(finishings
, "sort");
2854 strcpy(finishings
, "none");
2857 * Quote any commas in the make and model, location, and info strings...
2860 for (src
= p
->make_model
, dst
= make_model
;
2861 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
2863 if (*src
== ',' || *src
== '\\' || *src
== ')')
2872 strcpy(make_model
, "Unknown");
2874 for (src
= p
->location
, dst
= location
;
2875 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
2877 if (*src
== ',' || *src
== '\\' || *src
== ')')
2886 strcpy(location
, "Unknown");
2888 for (src
= p
->info
, dst
= info
;
2889 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
2891 if (*src
== ',' || *src
== '\\' || *src
== ')')
2900 strcpy(info
, "Unknown");
2903 * Get the authentication value...
2906 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
2910 * Make the SLP attribute string list that conforms to
2911 * the IANA 'printer:' template.
2914 snprintf(attrs
, sizeof(attrs
),
2915 "(printer-uri-supported=%s),"
2916 "(uri-authentication-supported=%s>),"
2918 "(uri-security-supported=tls>),"
2920 "(uri-security-supported=none>),"
2921 #endif /* HAVE_SSL */
2922 "(printer-name=%s),"
2923 "(printer-location=%s),"
2924 "(printer-info=%s),"
2925 "(printer-more-info=%s),"
2926 "(printer-make-and-model=%s),"
2927 "(printer-type=%d),"
2928 "(charset-supported=utf-8),"
2929 "(natural-language-configured=%s),"
2930 "(natural-language-supported=de,en,es,fr,it),"
2931 "(color-supported=%s),"
2932 "(finishings-supported=%s),"
2933 "(sides-supported=one-sided%s),"
2934 "(multiple-document-jobs-supported=true)"
2935 "(ipp-versions-supported=1.0,1.1)",
2936 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
2937 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
2938 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
2940 p
->type
& CUPS_PRINTER_DUPLEX
?
2941 ",two-sided-long-edge,two-sided-short-edge" : "");
2943 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
2946 * Register the printer with the SLP server...
2949 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
2950 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
2952 if (error
!= SLP_OK
)
2953 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
2959 * 'slp_attr_callback()' - SLP attribute callback
2962 static SLPBoolean
/* O - SLP_TRUE for success */
2964 SLPHandle hslp
, /* I - SLP handle */
2965 const char *attrlist
, /* I - Attribute list */
2966 SLPError errcode
, /* I - Parsing status for this attr */
2967 void *cookie
) /* I - Current printer */
2969 char *tmp
= 0; /* Temporary string */
2970 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2971 /* Current printer */
2974 (void)hslp
; /* anti-compiler-warning-code */
2977 * Bail if there was an error
2980 if (errcode
!= SLP_OK
)
2984 * Parse the attrlist to obtain things needed to build CUPS browse packet
2987 memset(p
, 0, sizeof(cupsd_printer_t
));
2989 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2991 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2993 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2995 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
2996 p
->type
= atoi(tmp
);
2998 p
->type
= CUPS_PRINTER_REMOTE
;
3000 cupsdClearString(&tmp
);
3007 * 'slp_dereg_printer()' - SLPDereg() the specified printer
3011 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
3013 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
3016 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
3018 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
3021 * Make the SLP service URL that conforms to the IANA
3022 * 'printer:' template.
3025 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
3028 * Deregister the printer...
3031 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
3037 * 'slp_get_attr()' - Get an attribute from an SLP registration.
3040 static int /* O - 0 on success */
3041 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
3042 const char *tag
, /* I - Name of attribute */
3043 char **valbuf
) /* O - Value */
3045 char *ptr1
, /* Pointer into string */
3049 cupsdClearString(valbuf
);
3051 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3053 ptr1
+= strlen(tag
);
3055 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3061 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3062 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3065 * Dequote the value...
3068 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3069 if (*ptr1
== '\\' && ptr1
[1])
3070 _cups_strcpy(ptr1
, ptr1
+ 1);
3081 * 'slp_reg_callback()' - Empty SLPRegReport.
3085 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3086 SLPError errcode
, /* I - Error code, if any */
3087 void *cookie
) /* I - App data */
3098 * 'slp_url_callback()' - SLP service url callback
3101 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3103 SLPHandle hslp
, /* I - SLP handle */
3104 const char *srvurl
, /* I - URL of service */
3105 unsigned short lifetime
, /* I - Life of service */
3106 SLPError errcode
, /* I - Existing error code */
3107 void *cookie
) /* I - Pointer to service list */
3109 slpsrvurl_t
*s
, /* New service entry */
3110 **head
; /* Pointer to head of entry */
3114 * Let the compiler know we won't be using these vars...
3121 * Bail if there was an error
3124 if (errcode
!= SLP_OK
)
3128 * Grab the head of the list...
3131 head
= (slpsrvurl_t
**)cookie
;
3134 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3137 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3141 * Copy the SLP service URL...
3144 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3147 * Link the SLP service URL into the head of the list
3157 #endif /* HAVE_LIBSLP */
3161 * End of "$Id: dirsvc.c 5724 2006-07-12 19:42:35Z mike $".