2 * "$Id: dirsvc.c 5305 2006-03-18 03:05:12Z 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 * cupsdUpdatePolling() - Read status messages from the poll daemons.
40 * cupsdUpdateSLPBrowse() - Get browsing information via SLP.
41 * dequote() - Remote quotes from a string.
42 * process_browse_data() - Process new browse data.
43 * process_implicit_classes() - Create/update implicit classes as needed.
44 * send_cups_browse() - Send new browsing information using the
46 * send_ldap_browse() - Send LDAP printer registrations.
47 * send_slp_browse() - Register the specified printer with SLP.
48 * slp_attr_callback() - SLP attribute callback
49 * slp_dereg_printer() - SLPDereg() the specified printer
50 * slp_get_attr() - Get an attribute from an SLP registration.
51 * slp_reg_callback() - Empty SLPRegReport.
52 * slp_url_callback() - SLP service url callback
56 * Include necessary headers...
67 static char *dequote(char *d
, const char *s
, int dlen
);
68 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
69 char *resource
, int resourcelen
);
70 static void process_browse_data(const char *uri
, const char *host
,
71 const char *resource
, cups_ptype_t type
,
72 ipp_pstate_t state
, const char *location
,
73 const char *info
, const char *make_model
,
74 int num_attrs
, cups_option_t
*attrs
);
75 static void process_implicit_classes(void);
76 static void send_cups_browse(cupsd_printer_t
*p
);
78 static void send_ldap_browse(cupsd_printer_t
*p
);
79 #endif /* HAVE_LDAP */
80 static void send_slp_browse(cupsd_printer_t
*p
);
84 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
88 "printerMakeAndModel",
93 #endif /* HAVE_OPENLDAP */
101 * SLP service name for CUPS...
104 # define SLP_CUPS_SRVTYPE "service:printer"
105 # define SLP_CUPS_SRVLEN 15
109 * Printer service URL structure
112 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
114 struct _slpsrvurl_s
*next
; /* Next URL in list */
115 char url
[HTTP_MAX_URI
];
124 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
125 SLPError errcode
, void *cookie
);
126 static void slp_dereg_printer(cupsd_printer_t
*p
);
127 static int slp_get_attr(const char *attrlist
, const char *tag
,
129 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
131 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
132 unsigned short lifetime
,
133 SLPError errcode
, void *cookie
);
134 #endif /* HAVE_LIBSLP */
138 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
142 cupsdLoadRemoteCache(void)
144 cups_file_t
*fp
; /* remote.cache file */
145 int linenum
; /* Current line number */
146 char line
[1024], /* Line from file */
147 *value
, /* Pointer to value */
148 *valueptr
, /* Pointer into value */
149 scheme
[32], /* Scheme portion of URI */
150 username
[64], /* Username portion of URI */
152 /* Hostname portion of URI */
153 resource
[HTTP_MAX_URI
];
154 /* Resource portion of URI */
155 int port
; /* Port number */
156 cupsd_printer_t
*p
; /* Current printer */
157 time_t now
; /* Current time */
161 * Open the remote.cache file...
164 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
165 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
169 * Read printer configurations until we hit EOF...
176 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
179 * Decode the directive...
182 if (!strcasecmp(line
, "<Printer") ||
183 !strcasecmp(line
, "<DefaultPrinter"))
186 * <Printer name> or <DefaultPrinter name>
189 if (p
== NULL
&& value
)
192 * Add the printer and a base file type...
195 cupsdLogMessage(CUPSD_LOG_DEBUG
,
196 "cupsdLoadRemoteCache: Loading printer %s...", value
);
198 if ((p
= cupsdFindDest(value
)) != NULL
)
200 if (p
->type
& CUPS_PRINTER_CLASS
)
202 cupsdLogMessage(CUPSD_LOG_WARN
,
203 "Cached remote printer \"%s\" conflicts with "
211 p
= cupsdAddPrinter(value
);
214 p
->state
= IPP_PRINTER_IDLE
;
215 p
->type
|= CUPS_PRINTER_REMOTE
;
216 p
->browse_time
= now
+ BrowseTimeout
;
219 * Set the default printer as needed...
222 if (!strcasecmp(line
, "<DefaultPrinter"))
227 cupsdLogMessage(CUPSD_LOG_ERROR
,
228 "Syntax error on line %d of remote.cache.", linenum
);
232 else if (!strcasecmp(line
, "<Class") ||
233 !strcasecmp(line
, "<DefaultClass"))
236 * <Class name> or <DefaultClass name>
239 if (p
== NULL
&& value
)
242 * Add the printer and a base file type...
245 cupsdLogMessage(CUPSD_LOG_DEBUG
,
246 "cupsdLoadRemoteCache: Loading class %s...", value
);
248 if ((p
= cupsdFindDest(value
)) != NULL
)
249 p
->type
= CUPS_PRINTER_CLASS
;
251 p
= cupsdAddClass(value
);
254 p
->state
= IPP_PRINTER_IDLE
;
255 p
->type
|= CUPS_PRINTER_REMOTE
;
256 p
->browse_time
= now
+ BrowseTimeout
;
259 * Set the default printer as needed...
262 if (!strcasecmp(line
, "<DefaultClass"))
267 cupsdLogMessage(CUPSD_LOG_ERROR
,
268 "Syntax error on line %d of remote.cache.", linenum
);
272 else if (!strcasecmp(line
, "</Printer>") ||
273 !strcasecmp(line
, "</Class>"))
278 * Close out the current printer...
281 cupsdSetPrinterAttrs(p
);
287 cupsdLogMessage(CUPSD_LOG_ERROR
,
288 "Syntax error on line %d of remote.cache.", linenum
);
294 cupsdLogMessage(CUPSD_LOG_ERROR
,
295 "Syntax error on line %d of remote.cache.", linenum
);
298 else if (!strcasecmp(line
, "Info"))
301 cupsdSetString(&p
->info
, value
);
303 else if (!strcasecmp(line
, "MakeModel"))
306 cupsdSetString(&p
->make_model
, value
);
308 else if (!strcasecmp(line
, "Location"))
311 cupsdSetString(&p
->location
, value
);
313 else if (!strcasecmp(line
, "DeviceURI"))
317 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
318 username
, sizeof(username
), host
, sizeof(host
), &port
,
319 resource
, sizeof(resource
));
321 cupsdSetString(&p
->hostname
, host
);
322 cupsdSetString(&p
->uri
, value
);
323 cupsdSetString(&p
->device_uri
, value
);
327 cupsdLogMessage(CUPSD_LOG_ERROR
,
328 "Syntax error on line %d of remote.cache.", linenum
);
332 else if (!strcasecmp(line
, "Option") && value
)
338 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
341 cupsdLogMessage(CUPSD_LOG_ERROR
,
342 "Syntax error on line %d of remote.cache.", linenum
);
345 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
347 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
351 else if (!strcasecmp(line
, "State"))
354 * Set the initial queue state...
357 if (value
&& !strcasecmp(value
, "idle"))
358 p
->state
= IPP_PRINTER_IDLE
;
359 else if (value
&& !strcasecmp(value
, "stopped"))
360 p
->state
= IPP_PRINTER_STOPPED
;
363 cupsdLogMessage(CUPSD_LOG_ERROR
,
364 "Syntax error on line %d of remote.cache.", linenum
);
368 else if (!strcasecmp(line
, "StateMessage"))
371 * Set the initial queue state message...
375 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
377 else if (!strcasecmp(line
, "Accepting"))
380 * Set the initial accepting state...
384 (!strcasecmp(value
, "yes") ||
385 !strcasecmp(value
, "on") ||
386 !strcasecmp(value
, "true")))
389 (!strcasecmp(value
, "no") ||
390 !strcasecmp(value
, "off") ||
391 !strcasecmp(value
, "false")))
395 cupsdLogMessage(CUPSD_LOG_ERROR
,
396 "Syntax error on line %d of remote.cache.", linenum
);
400 else if (!strcasecmp(line
, "Type"))
403 p
->type
= atoi(value
);
406 cupsdLogMessage(CUPSD_LOG_ERROR
,
407 "Syntax error on line %d of remote.cache.", linenum
);
411 else if (!strcasecmp(line
, "BrowseTime"))
415 time_t t
= atoi(value
);
417 if (t
> (now
+ BrowseInterval
))
422 cupsdLogMessage(CUPSD_LOG_ERROR
,
423 "Syntax error on line %d of remote.cache.", linenum
);
427 else if (!strcasecmp(line
, "JobSheets"))
430 * Set the initial job sheets...
435 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
440 cupsdSetString(&p
->job_sheets
[0], value
);
442 while (isspace(*valueptr
& 255))
447 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
452 cupsdSetString(&p
->job_sheets
[1], value
);
457 cupsdLogMessage(CUPSD_LOG_ERROR
,
458 "Syntax error on line %d of remote.cache.", linenum
);
462 else if (!strcasecmp(line
, "AllowUser"))
467 cupsdAddPrinterUser(p
, value
);
471 cupsdLogMessage(CUPSD_LOG_ERROR
,
472 "Syntax error on line %d of remote.cache.", linenum
);
476 else if (!strcasecmp(line
, "DenyUser"))
481 cupsdAddPrinterUser(p
, value
);
485 cupsdLogMessage(CUPSD_LOG_ERROR
,
486 "Syntax error on line %d of remote.cache.", linenum
);
493 * Something else we don't understand...
496 cupsdLogMessage(CUPSD_LOG_ERROR
,
497 "Unknown configuration directive %s on line %d of remote.cache.",
505 * Do auto-classing if needed...
508 process_implicit_classes();
513 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
517 cupsdSaveRemoteCache(void)
519 int i
; /* Looping var */
520 cups_file_t
*fp
; /* printers.conf file */
521 char temp
[1024]; /* Temporary string */
522 cupsd_printer_t
*printer
; /* Current printer class */
523 time_t curtime
; /* Current time */
524 struct tm
*curdate
; /* Current date */
525 cups_option_t
*option
; /* Current option */
529 * Create the remote.cache file...
532 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
534 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
536 cupsdLogMessage(CUPSD_LOG_ERROR
,
537 "Unable to save remote.cache - %s", strerror(errno
));
541 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving remote.cache...");
544 * Restrict access to the file...
547 fchown(cupsFileNumber(fp
), getuid(), Group
);
548 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
551 * Write a small header to the file...
554 curtime
= time(NULL
);
555 curdate
= localtime(&curtime
);
556 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
558 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
559 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
562 * Write each local printer known to the system...
565 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
567 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
570 * Skip local destinations...
573 if (!(printer
->type
& CUPS_PRINTER_REMOTE
))
577 * Write printers as needed...
580 if (printer
== DefaultPrinter
)
581 cupsFilePuts(fp
, "<Default");
583 cupsFilePutChar(fp
, '<');
585 if (printer
->type
& CUPS_PRINTER_CLASS
)
586 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
588 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
590 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
592 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_time
);
595 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
597 if (printer
->make_model
)
598 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
600 if (printer
->location
)
601 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
603 if (printer
->device_uri
)
604 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
606 if (printer
->state
== IPP_PRINTER_STOPPED
)
608 cupsFilePuts(fp
, "State Stopped\n");
609 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
612 cupsFilePuts(fp
, "State Idle\n");
614 if (printer
->accepting
)
615 cupsFilePuts(fp
, "Accepting Yes\n");
617 cupsFilePuts(fp
, "Accepting No\n");
619 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
620 printer
->job_sheets
[1]);
622 for (i
= 0; i
< printer
->num_users
; i
++)
623 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
626 for (i
= printer
->num_options
, option
= printer
->options
;
629 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
631 if (printer
->type
& CUPS_PRINTER_CLASS
)
632 cupsFilePuts(fp
, "</Class>\n");
634 cupsFilePuts(fp
, "</Printer>\n");
642 * 'cupsdSendBrowseDelete()' - Send a "browse delete" message for a printer.
646 cupsdSendBrowseDelete(
647 cupsd_printer_t
*p
) /* I - Printer to delete */
650 * Only announce if browsing is enabled and this is a local queue...
653 if (!Browsing
|| !p
->shared
||
654 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
658 * First mark the printer for deletion...
661 p
->type
|= CUPS_PRINTER_DELETE
;
664 * Announce the deletion...
667 if (BrowseLocalProtocols
& BROWSE_CUPS
)
670 if (BrowseLocalProtocols
& BROWSE_SLP
)
671 slp_dereg_printer(p
);
672 #endif /* HAVE_LIBSLP */
677 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
681 cupsdSendBrowseList(void)
683 int count
; /* Number of dests to update */
684 cupsd_printer_t
*p
; /* Current printer */
685 time_t ut
, /* Minimum update time */
686 to
; /* Timeout time */
689 if (!Browsing
|| !BrowseLocalProtocols
|| !Printers
)
693 * Compute the update and timeout times...
697 ut
= to
- BrowseInterval
;
700 * Figure out how many printers need an update...
703 if (BrowseInterval
> 0)
705 int max_count
; /* Maximum number to update */
709 * Throttle the number of printers we'll be updating this time
710 * around based on the number of queues that need updating and
711 * the maximum number of queues to update each second...
714 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
716 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
717 count
< max_count
&& p
!= NULL
;
718 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
719 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
720 p
->shared
&& p
->browse_time
< ut
)
724 * Loop through all of the printers and send local updates as needed...
728 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
730 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
734 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
737 * Check for wraparound...
741 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
745 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
748 else if (p
->browse_time
< ut
)
751 * Need to send an update...
756 p
->browse_time
= time(NULL
);
758 if (BrowseLocalProtocols
& BROWSE_CUPS
)
762 if (BrowseLocalProtocols
& BROWSE_SLP
)
764 #endif /* HAVE_LIBSLP */
767 if (BrowseLocalProtocols
& BROWSE_LDAP
)
769 #endif /* HAVE_LDAP */
774 * Save where we left off so that all printers get updated...
781 * Loop through all of the printers and send local updates as needed...
784 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
786 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
789 * If this is a remote queue, see if it needs to be timed out...
792 if (p
->type
& CUPS_PRINTER_REMOTE
)
794 if (p
->browse_expire
< to
)
796 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
797 "%s \'%s\' deleted by directory services (timeout).",
798 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
801 cupsdLogMessage(CUPSD_LOG_INFO
,
802 "Remote destination \"%s\" has timed out; "
806 cupsArraySave(Printers
);
807 cupsdDeletePrinter(p
, 1);
808 cupsArrayRestore(Printers
);
816 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
820 cupsdStartBrowsing(void)
822 int val
; /* Socket option value */
823 struct sockaddr_in addr
; /* Broadcast address */
828 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
831 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
833 if (BrowseSocket
< 0)
836 * Create the broadcast socket...
839 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
841 cupsdLogMessage(CUPSD_LOG_ERROR
,
842 "cupsdStartBrowsing: Unable to create broadcast "
843 "socket - %s.", strerror(errno
));
844 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
845 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
850 * Bind the socket to browse port...
853 memset(&addr
, 0, sizeof(addr
));
854 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
855 addr
.sin_family
= AF_INET
;
856 addr
.sin_port
= htons(BrowsePort
);
858 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
860 cupsdLogMessage(CUPSD_LOG_ERROR
,
861 "cupsdStartBrowsing: Unable to bind broadcast "
862 "socket - %s.", strerror(errno
));
865 closesocket(BrowseSocket
);
871 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
872 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
878 * Set the "broadcast" flag...
882 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
884 cupsdLogMessage(CUPSD_LOG_ERROR
,
885 "cupsdStartBrowsing: Unable to set broadcast mode - %s.",
889 closesocket(BrowseSocket
);
895 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
896 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
901 * Close the socket on exec...
904 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
907 * Finally, add the socket to the input selection set...
910 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
911 "cupsdStartBrowsing: Adding fd %d to InputSet...",
914 FD_SET(BrowseSocket
, InputSet
);
920 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
926 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
928 cupsdLogMessage(CUPSD_LOG_ERROR
,
929 "Unable to open an SLP handle; disabling SLP browsing!");
930 BrowseLocalProtocols
&= ~BROWSE_SLP
;
931 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
934 BrowseSLPRefresh
= 0;
936 #endif /* HAVE_LIBSLP */
939 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
943 cupsdLogMessage(CUPSD_LOG_ERROR
,
944 "Need to set BrowseLDAPDN to use LDAP browsing!");
945 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
946 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
951 * Open LDAP handle...
954 int rc
; /* LDAP API status */
955 int version
= 3; /* LDAP version */
956 struct berval bv
= {0, ""}; /* SASL bind value */
960 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
963 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
964 rc
= ldap_initialize(&BrowseLDAPHandle
, "ldapi:///");
966 rc
= ldap_initialize(&BrowseLDAPHandle
, BrowseLDAPServer
);
968 if (rc
!= LDAP_SUCCESS
)
970 cupsdLogMessage(CUPSD_LOG_ERROR
,
971 "Unable to initialize LDAP; disabling LDAP browsing!");
972 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
973 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
975 else if (ldap_set_option(BrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
976 (const void *)&version
) != LDAP_SUCCESS
)
978 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
979 BrowseLDAPHandle
= NULL
;
980 cupsdLogMessage(CUPSD_LOG_ERROR
,
981 "Unable to set LDAP protocol version; "
982 "disabling LDAP browsing!");
983 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
984 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
988 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
989 rc
= ldap_sasl_bind_s(BrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
992 rc
= ldap_bind_s(BrowseLDAPHandle
, BrowseLDAPBindDN
,
993 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
995 if (rc
!= LDAP_SUCCESS
)
997 cupsdLogMessage(CUPSD_LOG_ERROR
,
998 "Unable to bind to LDAP server; "
999 "disabling LDAP browsing!");
1000 ldap_unbind_ext(BrowseLDAPHandle
, NULL
, NULL
);
1001 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1002 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1007 BrowseLDAPRefresh
= 0;
1009 #endif /* HAVE_OPENLDAP */
1014 * 'cupsdStartPolling()' - Start polling servers as needed.
1018 cupsdStartPolling(void)
1020 int i
; /* Looping var */
1021 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1022 char polld
[1024]; /* Poll daemon path */
1023 char sport
[10]; /* Server port */
1024 char bport
[10]; /* Browser port */
1025 char interval
[10]; /* Poll interval */
1026 int statusfds
[2]; /* Status pipe */
1027 char *argv
[6]; /* Arguments */
1028 char *envp
[100]; /* Environment */
1032 * Don't do anything if we aren't polling...
1038 PollStatusBuffer
= NULL
;
1043 * Setup string arguments for polld, port and interval options.
1046 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1048 sprintf(bport
, "%d", BrowsePort
);
1051 sprintf(interval
, "%d", BrowseInterval
);
1053 strcpy(interval
, "30");
1055 argv
[0] = "cups-polld";
1061 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1064 * Create a pipe that receives the status messages from each
1068 if (cupsdOpenPipe(statusfds
))
1070 cupsdLogMessage(CUPSD_LOG_ERROR
,
1071 "Unable to create polling status pipes - %s.",
1074 PollStatusBuffer
= NULL
;
1078 PollPipe
= statusfds
[0];
1079 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1082 * Run each polling daemon, redirecting stderr to the polling pipe...
1085 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1087 sprintf(sport
, "%d", pollp
->port
);
1089 argv
[1] = pollp
->hostname
;
1091 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1,
1092 0, &(pollp
->pid
)) < 0)
1094 cupsdLogMessage(CUPSD_LOG_ERROR
,
1095 "cupsdStartPolling: Unable to fork polling daemon - %s",
1101 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1102 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1103 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1106 close(statusfds
[1]);
1109 * Finally, add the pipe to the input selection set...
1112 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1113 "cupsdStartPolling: Adding fd %d to InputSet...", PollPipe
);
1115 FD_SET(PollPipe
, InputSet
);
1120 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1124 cupsdStopBrowsing(void)
1126 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1129 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1133 * Close the socket and remove it from the input selection set.
1137 closesocket(BrowseSocket
);
1139 close(BrowseSocket
);
1142 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1143 "cupsdStopBrowsing: Removing fd %d from InputSet...",
1146 FD_CLR(BrowseSocket
, InputSet
);
1151 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1155 * Close SLP handle...
1158 SLPClose(BrowseSLPHandle
);
1159 BrowseSLPHandle
= NULL
;
1161 #endif /* HAVE_LIBSLP */
1164 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1167 ldap_unbind(BrowseLDAPHandle
);
1168 BrowseLDAPHandle
= NULL
;
1170 #endif /* HAVE_OPENLDAP */
1175 * 'cupsdStopPolling()' - Stop polling servers as needed.
1179 cupsdStopPolling(void)
1181 int i
; /* Looping var */
1182 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1187 cupsdStatBufDelete(PollStatusBuffer
);
1190 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1191 "cupsdStopPolling: removing fd %d from InputSet.", PollPipe
);
1192 FD_CLR(PollPipe
, InputSet
);
1195 PollStatusBuffer
= NULL
;
1198 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1200 cupsdEndProcess(pollp
->pid
, 0);
1205 * 'cupsdUpdateCUPSBrowse()' - Update the browse lists using the CUPS protocol.
1209 cupsdUpdateCUPSBrowse(void)
1211 int i
; /* Looping var */
1212 int auth
; /* Authorization status */
1213 int len
; /* Length of name string */
1214 int bytes
; /* Number of bytes left */
1215 char packet
[1541], /* Broadcast packet */
1216 *pptr
; /* Pointer into packet */
1217 socklen_t srclen
; /* Length of source address */
1218 http_addr_t srcaddr
; /* Source address */
1219 char srcname
[1024]; /* Source hostname */
1220 unsigned address
[4]; /* Source address */
1221 unsigned type
; /* Printer type */
1222 unsigned state
; /* Printer state */
1223 char uri
[HTTP_MAX_URI
], /* Printer URI */
1224 host
[HTTP_MAX_URI
], /* Host portion of URI */
1225 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
1226 info
[IPP_MAX_NAME
], /* Information string */
1227 location
[IPP_MAX_NAME
], /* Location string */
1228 make_model
[IPP_MAX_NAME
];/* Make and model string */
1229 int num_attrs
; /* Number of attributes */
1230 cups_option_t
*attrs
; /* Attributes */
1234 * Read a packet from the browse socket...
1237 srclen
= sizeof(srcaddr
);
1238 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
1239 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
1242 * "Connection refused" is returned under Linux if the destination port
1243 * or address is unreachable from a previous sendto(); check for the
1244 * error here and ignore it for now...
1247 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
1249 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
1251 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browsing turned off.");
1253 cupsdStopBrowsing();
1260 packet
[bytes
] = '\0';
1263 * If we're about to sleep, ignore incoming browse packets.
1270 * Figure out where it came from...
1274 if (srcaddr
.addr
.sa_family
== AF_INET6
)
1276 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
1277 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
1278 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
1279 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
1282 #endif /* AF_INET6 */
1287 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
1290 if (HostNameLookups
)
1291 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
1293 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
1295 len
= strlen(srcname
);
1303 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
1306 * Access from localhost (127.0.0.1) is always allowed...
1314 * Do authorization checks on the domain/address...
1317 switch (BrowseACL
->order_type
)
1320 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1323 case AUTH_ALLOW
: /* Order Deny,Allow */
1326 if (cupsdCheckAuth(address
, srcname
, len
,
1327 BrowseACL
->num_deny
, BrowseACL
->deny
))
1330 if (cupsdCheckAuth(address
, srcname
, len
,
1331 BrowseACL
->num_allow
, BrowseACL
->allow
))
1335 case AUTH_DENY
: /* Order Allow,Deny */
1338 if (cupsdCheckAuth(address
, srcname
, len
,
1339 BrowseACL
->num_allow
, BrowseACL
->allow
))
1342 if (cupsdCheckAuth(address
, srcname
, len
,
1343 BrowseACL
->num_deny
, BrowseACL
->deny
))
1352 if (auth
== AUTH_DENY
)
1354 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1355 "cupsdUpdateCUPSBrowse: Refused %d bytes from %s", bytes
,
1360 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1361 "cupsdUpdateCUPSBrowse: (%d bytes from %s) %s", bytes
,
1368 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
1370 cupsdLogMessage(CUPSD_LOG_WARN
,
1371 "cupsdUpdateCUPSBrowse: Garbled browse packet - %s", packet
);
1375 strcpy(location
, "Location Unknown");
1376 strcpy(info
, "No Information Available");
1377 make_model
[0] = '\0';
1381 if ((pptr
= strchr(packet
, '\"')) != NULL
)
1384 * Have extended information; can't use sscanf for it because not all
1385 * sscanf's allow empty strings with %[^\"]...
1388 for (i
= 0, pptr
++;
1389 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
1391 location
[i
] = *pptr
;
1399 while (*pptr
&& isspace(*pptr
& 255))
1404 for (i
= 0, pptr
++;
1405 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
1414 while (*pptr
&& isspace(*pptr
& 255))
1419 for (i
= 0, pptr
++;
1420 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
1422 make_model
[i
] = *pptr
;
1427 make_model
[i
] = '\0';
1430 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
1436 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
1437 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
1438 type
, state
, uri
, location
, info
, make_model
));
1441 * Pull the URI apart to see if this is a local or remote printer...
1444 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1446 cupsFreeOptions(num_attrs
, attrs
);
1454 for (i
= 0; i
< NumRelays
; i
++)
1455 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
1456 if (sendto(BrowseSocket
, packet
, bytes
, 0,
1457 (struct sockaddr
*)&(Relays
[i
].to
),
1458 sizeof(http_addr_t
)) <= 0)
1460 cupsdLogMessage(CUPSD_LOG_ERROR
,
1461 "cupsdUpdateCUPSBrowse: sendto failed for relay %d - %s.",
1462 i
+ 1, strerror(errno
));
1463 cupsFreeOptions(num_attrs
, attrs
);
1468 * Process the browse data...
1471 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
1472 (ipp_pstate_t
)state
, location
, info
, make_model
,
1477 #ifdef HAVE_OPENLDAP
1479 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1483 cupsdUpdateLDAPBrowse(void)
1485 char uri
[HTTP_MAX_URI
], /* Printer URI */
1486 host
[HTTP_MAX_URI
], /* Hostname */
1487 resource
[HTTP_MAX_URI
], /* Resource path */
1488 location
[1024], /* Printer location */
1489 info
[1024], /* Printer information */
1490 make_model
[1024], /* Printer make and model */
1491 **value
; /* Holds the returned data from LDAP */
1492 int type
; /* Printer type */
1493 int rc
; /* LDAP status */
1494 int limit
; /* Size limit */
1495 LDAPMessage
*res
, /* LDAP search results */
1496 *e
; /* Current entry from search */
1500 * Search for printers...
1503 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1505 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1507 rc
= ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1508 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1509 if (rc
!= LDAP_SUCCESS
)
1511 cupsdLogMessage(CUPSD_LOG_ERROR
,
1512 "LDAP search returned error %d: %s", rc
,
1513 ldap_err2string(rc
));
1517 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1518 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1523 * Loop through the available printers...
1526 if ((e
= ldap_first_entry(BrowseLDAPHandle
, res
)) == NULL
)
1528 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to get LDAP printer entry!");
1534 value
= ldap_get_values(BrowseLDAPHandle
, e
, "printerDescription");
1535 strlcpy(info
, *value
, sizeof(info
));
1536 ldap_value_free(value
);
1538 value
= ldap_get_values(BrowseLDAPHandle
, e
, "printerLocation");
1539 strlcpy(location
, *value
, sizeof(location
));
1540 ldap_value_free(value
);
1542 value
= ldap_get_values(BrowseLDAPHandle
, e
, "printerMakeAndModel");
1543 strlcpy(make_model
, *value
, sizeof(make_model
));
1544 ldap_value_free(value
);
1546 value
= ldap_get_values(BrowseLDAPHandle
, e
, "printerType");
1547 type
= atoi(*value
);
1548 ldap_value_free(value
);
1550 value
= ldap_get_values(BrowseLDAPHandle
, e
, "printerURI");
1551 strlcpy(uri
, *value
, sizeof(uri
));
1552 ldap_value_free(value
);
1554 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1555 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
1556 location
, info
, make_model
, 0, NULL
);
1558 e
= ldap_next_entry(BrowseLDAPHandle
, e
);
1561 #endif /* HAVE_OPENLDAP */
1565 * 'cupsdUpdatePolling()' - Read status messages from the poll daemons.
1569 cupsdUpdatePolling(void)
1571 char *ptr
, /* Pointer to end of line in buffer */
1572 message
[1024]; /* Pointer to message text */
1573 int loglevel
; /* Log level for message */
1576 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
1577 message
, sizeof(message
))) != NULL
)
1578 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
1584 * All polling processes have died; stop polling...
1587 cupsdLogMessage(CUPSD_LOG_ERROR
,
1588 "cupsdUpdatePolling: all polling processes have exited!");
1596 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
1600 cupsdUpdateSLPBrowse(void)
1602 slpsrvurl_t
*s
, /* Temporary list of service URLs */
1603 *next
; /* Next service in list */
1604 cupsd_printer_t p
; /* Printer information */
1605 const char *uri
; /* Pointer to printer URI */
1606 char host
[HTTP_MAX_URI
], /* Host portion of URI */
1607 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
1611 * Reset the refresh time...
1614 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
1617 * Poll for remote printers using SLP...
1622 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
1623 slp_url_callback
, &s
);
1626 * Loop through the list of available printers...
1632 * Save the "next" pointer...
1638 * Load a cupsd_printer_t structure with the SLP service attributes...
1641 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
1644 * Process this printer entry...
1647 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
1649 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
1652 * Pull the URI apart to see if this is a local or remote printer...
1655 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
1656 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
1657 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
1661 * Free this listing...
1664 cupsdClearString(&p
.info
);
1665 cupsdClearString(&p
.location
);
1666 cupsdClearString(&p
.make_model
);
1671 #endif /* HAVE_LIBSLP */
1675 * 'dequote()' - Remote quotes from a string.
1678 static char * /* O - Dequoted string */
1679 dequote(char *d
, /* I - Destination string */
1680 const char *s
, /* I - Source string */
1681 int dlen
) /* I - Destination length */
1683 char *dptr
; /* Pointer into destination */
1688 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
1705 * 'is_local_queue()' - Determine whether the URI points at a local queue.
1708 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
1709 is_local_queue(const char *uri
, /* I - Printer URI */
1710 char *host
, /* O - Host string */
1711 int hostlen
, /* I - Length of host buffer */
1712 char *resource
, /* O - Resource string */
1713 int resourcelen
) /* I - Length of resource buffer */
1715 char scheme
[32], /* Scheme portion of URI */
1716 username
[HTTP_MAX_URI
]; /* Username portion of URI */
1717 int port
; /* Port portion of URI */
1718 cupsd_netif_t
*iface
; /* Network interface */
1722 * Pull the URI apart to see if this is a local or remote printer...
1725 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
1726 username
, sizeof(username
), host
, hostlen
, &port
,
1727 resource
, resourcelen
) < HTTP_URI_OK
)
1730 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
1733 * Check for local server addresses...
1736 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
1741 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
1743 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
1744 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
1748 * If we get here, the printer is remote...
1756 * 'process_browse_data()' - Process new browse data.
1760 process_browse_data(
1761 const char *uri
, /* I - URI of printer/class */
1762 const char *host
, /* I - Hostname */
1763 const char *resource
, /* I - Resource path */
1764 cups_ptype_t type
, /* I - Printer type */
1765 ipp_pstate_t state
, /* I - Printer state */
1766 const char *location
, /* I - Printer location */
1767 const char *info
, /* I - Printer information */
1768 const char *make_model
, /* I - Printer make and model */
1769 int num_attrs
, /* I - Number of attributes */
1770 cups_option_t
*attrs
) /* I - Attributes */
1772 int i
; /* Looping var */
1773 int update
; /* Update printer attributes? */
1774 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
1775 name
[IPP_MAX_NAME
], /* Name of printer */
1776 newname
[IPP_MAX_NAME
], /* New name of printer */
1777 *hptr
, /* Pointer into hostname */
1778 *sptr
; /* Pointer into ServerName */
1779 char local_make_model
[IPP_MAX_NAME
];
1780 /* Local make and model */
1781 cupsd_printer_t
*p
; /* Printer information */
1782 const char *ipp_options
, /* ipp-options value */
1783 *lease_duration
; /* lease-duration value */
1787 * Determine if the URI contains any illegal characters in it...
1790 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
1791 (strncmp(resource
, "/printers/", 10) &&
1792 strncmp(resource
, "/classes/", 9)))
1794 cupsdLogMessage(CUPSD_LOG_ERROR
,
1795 "process_browse_data: Bad printer URI in browse data: %s",
1800 if (strchr(resource
, '?') ||
1801 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
1802 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
1804 cupsdLogMessage(CUPSD_LOG_ERROR
,
1805 "process_browse_data: Bad resource in browse data: %s",
1811 * OK, this isn't a local printer; add any remote options...
1814 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
1816 if (BrowseRemoteOptions
)
1818 if (BrowseRemoteOptions
[0] == '?')
1821 * Override server-supplied options...
1824 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
1826 else if (ipp_options
)
1829 * Combine the server and local options...
1832 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
1833 BrowseRemoteOptions
);
1838 * Just use the local options...
1841 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
1846 else if (ipp_options
)
1849 * Just use the server-supplied options...
1852 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
1857 * See if we already have it listed in the Printers list, and add it if not...
1860 type
|= CUPS_PRINTER_REMOTE
;
1861 type
&= ~CUPS_PRINTER_IMPLICIT
;
1863 hptr
= strchr(host
, '.');
1864 sptr
= strchr(ServerName
, '.');
1866 if (sptr
!= NULL
&& hptr
!= NULL
)
1869 * Strip the common domain name components...
1872 while (hptr
!= NULL
)
1874 if (!strcasecmp(hptr
, sptr
))
1880 hptr
= strchr(hptr
+ 1, '.');
1884 if (type
& CUPS_PRINTER_CLASS
)
1887 * Remote destination is a class...
1890 if (!strncmp(resource
, "/classes/", 9))
1891 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
1895 if ((p
= cupsdFindClass(name
)) == NULL
&& BrowseShortNames
)
1897 if ((p
= cupsdFindClass(resource
+ 9)) != NULL
)
1899 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
1902 * Nope, this isn't the same host; if the hostname isn't the local host,
1903 * add it to the other class and then find a class using the full host
1907 if (p
->type
& CUPS_PRINTER_REMOTE
)
1909 cupsdLogMessage(CUPSD_LOG_INFO
,
1910 "Renamed remote class \"%s\" to \"%s@%s\"...",
1911 p
->name
, p
->name
, p
->hostname
);
1912 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
1913 "Class \'%s\' deleted by directory services.",
1916 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
1917 cupsdRenamePrinter(p
, newname
);
1919 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1920 "Class \'%s\' added by directory services.",
1926 else if (!p
->hostname
)
1929 * Hostname not set, so this must be a cached remote printer
1930 * that was created for a pending print job...
1933 cupsdSetString(&p
->hostname
, host
);
1934 cupsdSetString(&p
->uri
, uri
);
1935 cupsdSetString(&p
->device_uri
, uri
);
1942 * Use the short name for this shared class.
1945 strlcpy(name
, resource
+ 9, sizeof(name
));
1948 else if (p
&& !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 * Class doesn't exist; add it...
1967 p
= cupsdAddClass(name
);
1969 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote class \"%s\"...", name
);
1971 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
1972 "Class \'%s\' added by directory services.", name
);
1975 * Force the URI to point to the real server...
1978 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
1980 cupsdSetString(&p
->uri
, uri
);
1981 cupsdSetString(&p
->device_uri
, uri
);
1982 cupsdSetString(&p
->hostname
, host
);
1990 * Remote destination is a printer...
1993 if (!strncmp(resource
, "/printers/", 10))
1994 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
1998 if ((p
= cupsdFindPrinter(name
)) == NULL
&& BrowseShortNames
)
2000 if ((p
= cupsdFindPrinter(resource
+ 10)) != NULL
)
2002 if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2005 * Nope, this isn't the same host; if the hostname isn't the local host,
2006 * add it to the other printer and then find a printer using the full host
2010 if (p
->type
& CUPS_PRINTER_REMOTE
)
2012 cupsdLogMessage(CUPSD_LOG_INFO
,
2013 "Renamed remote printer \"%s\" to \"%s@%s\"...",
2014 p
->name
, p
->name
, p
->hostname
);
2015 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2016 "Printer \'%s\' deleted by directory services.",
2019 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2020 cupsdRenamePrinter(p
, newname
);
2022 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2023 "Printer \'%s\' added by directory services.",
2029 else if (!p
->hostname
)
2032 * Hostname not set, so this must be a cached remote printer
2033 * that was created for a pending print job...
2036 cupsdSetString(&p
->hostname
, host
);
2037 cupsdSetString(&p
->uri
, uri
);
2038 cupsdSetString(&p
->device_uri
, uri
);
2045 * Use the short name for this shared printer.
2048 strlcpy(name
, resource
+ 10, sizeof(name
));
2051 else if (p
&& !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 * Printer doesn't exist; add it...
2070 p
= cupsdAddPrinter(name
);
2072 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2073 "Printer \'%s\' added by directory services.", name
);
2075 cupsdLogMessage(CUPSD_LOG_INFO
, "Added remote printer \"%s\"...", name
);
2078 * Force the URI to point to the real server...
2081 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
2083 cupsdSetString(&p
->hostname
, host
);
2084 cupsdSetString(&p
->uri
, uri
);
2085 cupsdSetString(&p
->device_uri
, uri
);
2092 * Update the state...
2096 p
->browse_time
= time(NULL
);
2098 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
2102 * Grab the lease-duration for the browse data; anything less then 1
2103 * second or more than 1 week gets the default BrowseTimeout...
2106 i
= atoi(lease_duration
);
2107 if (i
< 1 || i
> 604800)
2110 p
->browse_expire
= p
->browse_time
+ i
;
2113 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
2115 if (type
& CUPS_PRINTER_REJECTING
)
2117 type
&= ~CUPS_PRINTER_REJECTING
;
2125 else if (!p
->accepting
)
2131 if (p
->type
!= type
)
2137 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
2139 cupsdSetString(&p
->location
, location
);
2143 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
2145 cupsdSetString(&p
->info
, info
);
2149 if (!make_model
|| !make_model
[0])
2151 if (type
& CUPS_PRINTER_CLASS
)
2152 snprintf(local_make_model
, sizeof(local_make_model
),
2153 "Remote Class on %s", host
);
2155 snprintf(local_make_model
, sizeof(local_make_model
),
2156 "Remote Printer on %s", host
);
2159 snprintf(local_make_model
, sizeof(local_make_model
),
2160 "%s on %s", make_model
, host
);
2162 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
2164 cupsdSetString(&p
->make_model
, local_make_model
);
2170 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
2173 * See if we need to update the attributes...
2176 if (p
->num_options
!= num_attrs
)
2180 for (i
= 0; i
< num_attrs
; i
++)
2181 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
2182 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
2183 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
2192 * Free the old options...
2195 cupsFreeOptions(p
->num_options
, p
->options
);
2198 p
->num_options
= num_attrs
;
2201 if (type
& CUPS_PRINTER_DELETE
)
2203 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2204 "%s \'%s\' deleted by directory services.",
2205 (type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer", p
->name
);
2207 cupsdExpireSubscriptions(p
, NULL
);
2209 cupsdDeletePrinter(p
, 1);
2210 cupsdUpdateImplicitClasses();
2214 cupsdSetPrinterAttrs(p
);
2215 cupsdUpdateImplicitClasses();
2219 * See if we have a default printer... If not, make the first network
2220 * default printer the default.
2223 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
2226 * Find the first network default printer and use it...
2229 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2231 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2232 if (p
->type
& CUPS_PRINTER_DEFAULT
)
2240 * Do auto-classing if needed...
2243 process_implicit_classes();
2246 * Update the printcap file...
2249 cupsdWritePrintcap();
2254 * 'process_implicit_classes()' - Create/update implicit classes as needed.
2258 process_implicit_classes(void)
2260 int i
; /* Looping var */
2261 int update
; /* Update printer attributes? */
2262 char name
[IPP_MAX_NAME
], /* Name of printer */
2263 *hptr
; /* Pointer into hostname */
2264 cupsd_printer_t
*p
, /* Printer information */
2265 *pclass
, /* Printer class */
2266 *first
; /* First printer in class */
2267 int offset
, /* Offset of name */
2268 len
; /* Length of name */
2271 if (!ImplicitClasses
|| !Printers
)
2275 * Loop through all available printers and create classes as needed...
2278 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
2279 update
= 0, pclass
= NULL
, first
= NULL
;
2281 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2284 * Skip implicit classes...
2287 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2294 * If len == 0, get the length of this printer name up to the "@"
2298 cupsArraySave(Printers
);
2301 !strncasecmp(p
->name
, name
+ offset
, len
) &&
2302 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
2305 * We have more than one printer with the same name; see if
2306 * we have a class, and if this printer is a member...
2309 if (pclass
&& strcasecmp(pclass
->name
, name
))
2312 cupsdSetPrinterAttrs(pclass
);
2318 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
2321 * Need to add the class...
2324 pclass
= cupsdAddPrinter(name
);
2325 cupsArrayAdd(ImplicitPrinters
, pclass
);
2327 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
2328 pclass
->accepting
= 1;
2329 pclass
->state
= IPP_PRINTER_IDLE
;
2331 cupsdSetString(&pclass
->location
, p
->location
);
2332 cupsdSetString(&pclass
->info
, p
->info
);
2336 cupsdLogMessage(CUPSD_LOG_INFO
, "Added implicit class \"%s\"...",
2338 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2339 "Implicit class \'%s\' added by directory services.",
2345 for (i
= 0; i
< pclass
->num_printers
; i
++)
2346 if (pclass
->printers
[i
] == first
)
2349 if (i
>= pclass
->num_printers
)
2351 first
->in_implicit_class
= 1;
2352 cupsdAddPrinterToClass(pclass
, first
);
2358 for (i
= 0; i
< pclass
->num_printers
; i
++)
2359 if (pclass
->printers
[i
] == p
)
2362 if (i
>= pclass
->num_printers
)
2364 p
->in_implicit_class
= 1;
2365 cupsdAddPrinterToClass(pclass
, p
);
2372 * First time around; just get name length and mark it as first
2376 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
2377 len
= hptr
- p
->name
;
2379 len
= strlen(p
->name
);
2381 strncpy(name
, p
->name
, len
);
2385 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
2386 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
2389 * Can't use same name as a local printer; add "Any" to the
2390 * front of the name, unless we have explicitly disabled
2391 * the "ImplicitAnyClasses"...
2394 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
2397 * Add "Any" to the class name...
2400 strcpy(name
, "Any");
2401 strncpy(name
+ 3, p
->name
, len
);
2402 name
[len
+ 3] = '\0';
2408 * Don't create an implicit class if we have a local printer
2409 * with the same name...
2413 cupsArrayRestore(Printers
);
2421 cupsArrayRestore(Printers
);
2425 * Update the last printer class as needed...
2428 if (pclass
&& update
)
2429 cupsdSetPrinterAttrs(pclass
);
2434 * 'send_cups_browse()' - Send new browsing information using the CUPS
2439 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
2441 int i
; /* Looping var */
2442 cups_ptype_t type
; /* Printer type */
2443 cupsd_dirsvc_addr_t
*b
; /* Browse address */
2444 int bytes
; /* Length of packet */
2445 char packet
[1453], /* Browse data packet */
2446 uri
[1024], /* Printer URI */
2447 location
[1024], /* printer-location */
2448 info
[1024], /* printer-info */
2450 /* printer-make-and-model */
2451 cupsd_netif_t
*iface
; /* Network interface */
2455 * Figure out the printer type value...
2458 type
= p
->type
| CUPS_PRINTER_REMOTE
;
2461 type
|= CUPS_PRINTER_REJECTING
;
2463 if (p
== DefaultPrinter
)
2464 type
|= CUPS_PRINTER_DEFAULT
;
2467 * Remove quotes from printer-info, printer-location, and
2468 * printer-make-and-model attributes...
2471 dequote(location
, p
->location
, sizeof(location
));
2472 dequote(info
, p
->info
, sizeof(info
));
2473 dequote(make_model
, p
->make_model
? p
->make_model
: "Unknown",
2474 sizeof(make_model
));
2477 * Send a packet to each browse address...
2480 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
2484 * Send the browse packet to one or more interfaces...
2487 if (!strcmp(b
->iface
, "*"))
2490 * Send to all local interfaces...
2495 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2497 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2500 * Only send to local, IPv4 interfaces...
2503 if (!iface
->is_local
|| !iface
->port
||
2504 iface
->address
.addr
.sa_family
!= AF_INET
)
2507 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2508 iface
->hostname
, iface
->port
,
2509 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2512 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2513 type
, p
->state
, uri
, location
, info
, make_model
,
2514 p
->browse_attrs
? p
->browse_attrs
: "");
2516 bytes
= strlen(packet
);
2518 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2519 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2520 iface
->name
, packet
);
2522 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2524 sendto(BrowseSocket
, packet
, bytes
, 0,
2525 (struct sockaddr
*)&(iface
->broadcast
),
2526 sizeof(struct sockaddr_in
));
2529 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
2532 * Send to the named interface using the IPv4 address...
2536 if (strcmp(b
->iface
, iface
->name
))
2541 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
2544 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
2548 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
2549 iface
->hostname
, iface
->port
,
2550 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s%s" :
2553 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2554 type
, p
->state
, uri
, location
, info
, make_model
,
2555 p
->browse_attrs
? p
->browse_attrs
: "");
2557 bytes
= strlen(packet
);
2559 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2560 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
2561 iface
->name
, packet
);
2563 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
2565 sendto(BrowseSocket
, packet
, bytes
, 0,
2566 (struct sockaddr
*)&(iface
->broadcast
),
2567 sizeof(struct sockaddr_in
));
2574 * Send the browse packet to the indicated address using
2575 * the default server name...
2578 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
2579 type
, p
->state
, p
->uri
, location
, info
, make_model
,
2580 p
->browse_attrs
? p
->browse_attrs
: "");
2582 bytes
= strlen(packet
);
2583 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2584 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
2586 if (sendto(BrowseSocket
, packet
, bytes
, 0,
2587 (struct sockaddr
*)&(b
->to
),
2588 sizeof(struct sockaddr_in
)) <= 0)
2591 * Unable to send browse packet, so remove this address from the
2595 cupsdLogMessage(CUPSD_LOG_ERROR
,
2596 "cupsdSendBrowseList: sendto failed for browser "
2598 (int)(b
- Browsers
+ 1), strerror(errno
));
2601 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
2610 #ifdef HAVE_OPENLDAP
2612 * 'send_ldap_browse()' - Send LDAP printer registrations.
2616 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2618 int i
; /* Looping var... */
2619 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
2620 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
2621 LDAPMessage
*res
; /* Search result token */
2622 char *cn_value
[2], /* Change records */
2628 typestring
[255], /* String to hold printer-type */
2629 filter
[256], /* Search filter for possible UPDATEs */
2630 dn
[1024]; /* DN of the printer we are adding */
2631 int rc
; /* LDAP status */
2632 static const char * const objectClass_values
[] =
2633 { /* The 3 objectClass's we use in */
2634 "top", /* our LDAP entries */
2640 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s\n", p
->name
);
2643 * Everything in ldap is ** so we fudge around it...
2646 sprintf(typestring
, "%u", p
->type
);
2648 cn_value
[0] = p
->info
;
2652 location
[0] = p
->location
;
2654 make_model
[0] = p
->make_model
;
2655 make_model
[1] = NULL
;
2656 type
[0] = typestring
;
2661 snprintf(filter
, sizeof(filter
),
2662 "(&(objectclass=cupsPrinter)(printerDescription~=%s))", p
->info
);
2664 ldap_search_s(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
2665 filter
, (char **)ldap_attrs
, 0, &res
);
2666 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: Searching \"%s\"",
2669 mods
[0].mod_type
= "cn";
2670 mods
[0].mod_values
= cn_value
;
2671 mods
[1].mod_type
= "printerDescription";
2672 mods
[1].mod_values
= info
;
2673 mods
[2].mod_type
= "printerURI";
2674 mods
[2].mod_values
= uri
;
2675 mods
[3].mod_type
= "printerLocation";
2676 mods
[3].mod_values
= location
;
2677 mods
[4].mod_type
= "printerMakeAndModel";
2678 mods
[4].mod_values
= make_model
;
2679 mods
[5].mod_type
= "printerType";
2680 mods
[5].mod_values
= type
;
2681 mods
[6].mod_type
= "objectClass";
2682 mods
[6].mod_values
= (char **)objectClass_values
;
2684 snprintf(dn
, sizeof(dn
), "cn=%s,ou=printers,%s", p
->info
, BrowseLDAPDN
);
2685 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
2687 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0)
2690 * Printer has already been registered, modify the current
2694 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2695 "send_ldap_browse: Replacing entry...");
2697 for (i
= 0; i
< 7; i
++)
2699 pmods
[i
] = mods
+ i
;
2700 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2704 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2705 cupsdLogMessage(CUPSD_LOG_ERROR
,
2706 "LDAP modify for %s failed with status %d: %s",
2707 p
->name
, rc
, ldap_err2string(rc
));
2712 * Printer has already been registered, modify the current
2716 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2717 "send_ldap_browse: Adding entry...");
2719 for (i
= 0; i
< 7; i
++)
2721 pmods
[i
] = mods
+ i
;
2722 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
2726 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
2727 cupsdLogMessage(CUPSD_LOG_ERROR
,
2728 "LDAP add for %s failed with status %d: %s",
2729 p
->name
, rc
, ldap_err2string(rc
));
2732 #endif /* HAVE_OPENLDAP */
2737 * 'send_slp_browse()' - Register the specified printer with SLP.
2741 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
2743 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
2744 attrs
[8192], /* Printer attributes */
2745 finishings
[1024], /* Finishings to support */
2746 make_model
[IPP_MAX_NAME
* 2],
2747 /* Make and model, quoted */
2748 location
[IPP_MAX_NAME
* 2],
2749 /* Location, quoted */
2750 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
2751 *src
, /* Pointer to original string */
2752 *dst
; /* Pointer to destination string */
2753 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
2754 SLPError error
; /* SLP error, if any */
2757 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
2761 * Make the SLP service URL that conforms to the IANA
2762 * 'printer:' template.
2765 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2767 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
2770 * Figure out the finishings string...
2773 if (p
->type
& CUPS_PRINTER_STAPLE
)
2774 strcpy(finishings
, "staple");
2776 finishings
[0] = '\0';
2778 if (p
->type
& CUPS_PRINTER_BIND
)
2781 strlcat(finishings
, ",bind", sizeof(finishings
));
2783 strcpy(finishings
, "bind");
2786 if (p
->type
& CUPS_PRINTER_PUNCH
)
2789 strlcat(finishings
, ",punch", sizeof(finishings
));
2791 strcpy(finishings
, "punch");
2794 if (p
->type
& CUPS_PRINTER_COVER
)
2797 strlcat(finishings
, ",cover", sizeof(finishings
));
2799 strcpy(finishings
, "cover");
2802 if (p
->type
& CUPS_PRINTER_SORT
)
2805 strlcat(finishings
, ",sort", sizeof(finishings
));
2807 strcpy(finishings
, "sort");
2811 strcpy(finishings
, "none");
2814 * Quote any commas in the make and model, location, and info strings...
2817 for (src
= p
->make_model
, dst
= make_model
;
2818 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
2820 if (*src
== ',' || *src
== '\\' || *src
== ')')
2829 strcpy(make_model
, "Unknown");
2831 for (src
= p
->location
, dst
= location
;
2832 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
2834 if (*src
== ',' || *src
== '\\' || *src
== ')')
2843 strcpy(location
, "Unknown");
2845 for (src
= p
->info
, dst
= info
;
2846 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
2848 if (*src
== ',' || *src
== '\\' || *src
== ')')
2857 strcpy(info
, "Unknown");
2860 * Get the authentication value...
2863 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
2867 * Make the SLP attribute string list that conforms to
2868 * the IANA 'printer:' template.
2871 snprintf(attrs
, sizeof(attrs
),
2872 "(printer-uri-supported=%s),"
2873 "(uri-authentication-supported=%s>),"
2875 "(uri-security-supported=tls>),"
2877 "(uri-security-supported=none>),"
2878 #endif /* HAVE_SSL */
2879 "(printer-name=%s),"
2880 "(printer-location=%s),"
2881 "(printer-info=%s),"
2882 "(printer-more-info=%s),"
2883 "(printer-make-and-model=%s),"
2884 "(printer-type=%d),"
2885 "(charset-supported=utf-8),"
2886 "(natural-language-configured=%s),"
2887 "(natural-language-supported=de,en,es,fr,it),"
2888 "(color-supported=%s),"
2889 "(finishings-supported=%s),"
2890 "(sides-supported=one-sided%s),"
2891 "(multiple-document-jobs-supported=true)"
2892 "(ipp-versions-supported=1.0,1.1)",
2893 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
2894 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
2895 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
2897 p
->type
& CUPS_PRINTER_DUPLEX
?
2898 ",two-sided-long-edge,two-sided-short-edge" : "");
2900 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
2903 * Register the printer with the SLP server...
2906 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
2907 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
2909 if (error
!= SLP_OK
)
2910 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
2916 * 'slp_attr_callback()' - SLP attribute callback
2919 static SLPBoolean
/* O - SLP_TRUE for success */
2921 SLPHandle hslp
, /* I - SLP handle */
2922 const char *attrlist
, /* I - Attribute list */
2923 SLPError errcode
, /* I - Parsing status for this attr */
2924 void *cookie
) /* I - Current printer */
2926 char *tmp
= 0; /* Temporary string */
2927 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
2928 /* Current printer */
2931 (void)hslp
; /* anti-compiler-warning-code */
2934 * Bail if there was an error
2937 if (errcode
!= SLP_OK
)
2941 * Parse the attrlist to obtain things needed to build CUPS browse packet
2944 memset(p
, 0, sizeof(cupsd_printer_t
));
2946 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
2948 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
2950 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
2952 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
2953 p
->type
= atoi(tmp
);
2955 p
->type
= CUPS_PRINTER_REMOTE
;
2957 cupsdClearString(&tmp
);
2964 * 'slp_dereg_printer()' - SLPDereg() the specified printer
2968 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
2970 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
2973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
2975 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
2978 * Make the SLP service URL that conforms to the IANA
2979 * 'printer:' template.
2982 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
2985 * Deregister the printer...
2988 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
2994 * 'slp_get_attr()' - Get an attribute from an SLP registration.
2997 static int /* O - 0 on success */
2998 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
2999 const char *tag
, /* I - Name of attribute */
3000 char **valbuf
) /* O - Value */
3002 char *ptr1
, /* Pointer into string */
3006 cupsdClearString(valbuf
);
3008 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
3010 ptr1
+= strlen(tag
);
3012 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
3018 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
3019 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
3022 * Dequote the value...
3025 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
3026 if (*ptr1
== '\\' && ptr1
[1])
3027 _cups_strcpy(ptr1
, ptr1
+ 1);
3038 * 'slp_reg_callback()' - Empty SLPRegReport.
3042 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
3043 SLPError errcode
, /* I - Error code, if any */
3044 void *cookie
) /* I - App data */
3055 * 'slp_url_callback()' - SLP service url callback
3058 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
3060 SLPHandle hslp
, /* I - SLP handle */
3061 const char *srvurl
, /* I - URL of service */
3062 unsigned short lifetime
, /* I - Life of service */
3063 SLPError errcode
, /* I - Existing error code */
3064 void *cookie
) /* I - Pointer to service list */
3066 slpsrvurl_t
*s
, /* New service entry */
3067 **head
; /* Pointer to head of entry */
3071 * Let the compiler know we won't be using these vars...
3078 * Bail if there was an error
3081 if (errcode
!= SLP_OK
)
3085 * Grab the head of the list...
3088 head
= (slpsrvurl_t
**)cookie
;
3091 * Allocate a *temporary* slpsrvurl_t to hold this entry.
3094 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
3098 * Copy the SLP service URL...
3101 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
3104 * Link the SLP service URL into the head of the list
3114 #endif /* HAVE_LIBSLP */
3118 * End of "$Id: dirsvc.c 5305 2006-03-18 03:05:12Z mike $".