2 * "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $"
4 * Directory services routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
20 * Include necessary headers...
30 # ifdef HAVE_COREFOUNDATION
31 # include <CoreFoundation/CoreFoundation.h>
32 # endif /* HAVE_COREFOUNDATION */
33 # ifdef HAVE_SYSTEMCONFIGURATION
34 # include <SystemConfiguration/SystemConfiguration.h>
35 # endif /* HAVE_SYSTEMCONFIGURATION */
36 # endif /* __APPLE__ */
37 #endif /* HAVE_DNSSD */
44 static char *dequote(char *d
, const char *s
, int dlen
);
46 static int get_hostconfig(const char *name
);
47 #endif /* __APPLE__ */
48 static int is_local_queue(const char *uri
, char *host
, int hostlen
,
49 char *resource
, int resourcelen
);
50 static void process_browse_data(const char *uri
, const char *host
,
51 const char *resource
, cups_ptype_t type
,
52 ipp_pstate_t state
, const char *location
,
53 const char *info
, const char *make_model
,
54 int num_attrs
, cups_option_t
*attrs
);
55 static void process_implicit_classes(void);
56 static void send_cups_browse(cupsd_printer_t
*p
);
58 static LDAP
*ldap_connect(void);
59 static void ldap_reconnect(void);
60 static void ldap_disconnect(LDAP
*ld
);
61 static int ldap_search_rec(LDAP
*ld
, char *base
, int scope
,
62 char *filter
, char *attrs
[],
63 int attrsonly
, LDAPMessage
**res
);
64 static int ldap_getval_firststring(LDAP
*ld
, LDAPMessage
*entry
,
65 char *attr
, char *retval
,
66 unsigned long maxsize
);
67 static void ldap_freeres(LDAPMessage
*entry
);
68 static void send_ldap_ou(char *ou
, char *basedn
, char *descstring
);
69 static void send_ldap_browse(cupsd_printer_t
*p
);
70 static void ldap_dereg_printer(cupsd_printer_t
*p
);
71 static void ldap_dereg_ou(char *ou
, char *basedn
);
72 # ifdef HAVE_LDAP_REBIND_PROC
73 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
74 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
75 LDAP_CONST
char *refsp
,
80 static int ldap_rebind_proc(LDAP
*RebindLDAPHandle
,
86 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
87 # endif /* HAVE_LDAP_REBIND_PROC */
88 #endif /* HAVE_LDAP */
90 static void send_slp_browse(cupsd_printer_t
*p
);
91 #endif /* HAVE_LIBSLP */
92 static void update_cups_browse(void);
93 static void update_lpd(int onoff
);
94 static void update_polling(void);
95 static void update_smb(int onoff
);
99 static char *dnssdBuildTxtRecord(int *txt_len
, cupsd_printer_t
*p
,
101 static int dnssdComparePrinters(cupsd_printer_t
*a
, cupsd_printer_t
*b
);
102 static void dnssdDeregisterPrinter(cupsd_printer_t
*p
);
103 static char *dnssdPackTxtRecord(int *txt_len
, char *keyvalue
[][2],
105 static void dnssdRegisterCallback(DNSServiceRef sdRef
,
106 DNSServiceFlags flags
,
107 DNSServiceErrorType errorCode
,
108 const char *name
, const char *regtype
,
109 const char *domain
, void *context
);
110 static void dnssdRegisterPrinter(cupsd_printer_t
*p
);
111 static void dnssdUpdate(void);
112 #endif /* HAVE_DNSSD */
115 static const char * const ldap_attrs
[] =/* CUPS LDAP attributes */
117 "printerDescription",
119 "printerMakeAndModel",
124 #endif /* HAVE_LDAP */
132 * SLP service name for CUPS...
135 # define SLP_CUPS_SRVTYPE "service:printer"
136 # define SLP_CUPS_SRVLEN 15
140 * Printer service URL structure
143 typedef struct _slpsrvurl_s
/**** SLP URL list ****/
145 struct _slpsrvurl_s
*next
; /* Next URL in list */
146 char url
[HTTP_MAX_URI
];
155 static SLPBoolean
slp_attr_callback(SLPHandle hslp
, const char *attrlist
,
156 SLPError errcode
, void *cookie
);
157 static void slp_dereg_printer(cupsd_printer_t
*p
);
158 static int slp_get_attr(const char *attrlist
, const char *tag
,
160 static void slp_reg_callback(SLPHandle hslp
, SLPError errcode
,
162 static SLPBoolean
slp_url_callback(SLPHandle hslp
, const char *srvurl
,
163 unsigned short lifetime
,
164 SLPError errcode
, void *cookie
);
165 #endif /* HAVE_LIBSLP */
169 * 'cupsdDeregisterPrinter()' - Stop sending broadcast information for a
170 * local printer and remove any pending
171 * references to remote printers.
175 cupsdDeregisterPrinter(
176 cupsd_printer_t
*p
, /* I - Printer to register */
177 int removeit
) /* I - Printer being permanently removed */
180 * Only deregister if browsing is enabled and it's a local printer...
183 if (!Browsing
|| !p
->shared
||
184 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
188 * Announce the deletion...
191 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
193 cups_ptype_t savedtype
= p
->type
; /* Saved printer type */
195 p
->type
|= CUPS_PRINTER_DELETE
;
203 if (BrowseLocalProtocols
& BROWSE_SLP
)
204 slp_dereg_printer(p
);
205 #endif /* HAVE_LIBSLP */
208 if (BrowseLocalProtocols
& BROWSE_LDAP
)
209 ldap_dereg_printer(p
);
210 #endif /* HAVE_LDAP */
213 if (removeit
&& (BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
214 dnssdDeregisterPrinter(p
);
215 #endif /* HAVE_DNSSD */
220 * 'cupsdLoadRemoteCache()' - Load the remote printer cache.
224 cupsdLoadRemoteCache(void)
226 cups_file_t
*fp
; /* remote.cache file */
227 int linenum
; /* Current line number */
228 char line
[1024], /* Line from file */
229 *value
, /* Pointer to value */
230 *valueptr
, /* Pointer into value */
231 scheme
[32], /* Scheme portion of URI */
232 username
[64], /* Username portion of URI */
234 /* Hostname portion of URI */
235 resource
[HTTP_MAX_URI
];
236 /* Resource portion of URI */
237 int port
; /* Port number */
238 cupsd_printer_t
*p
; /* Current printer */
239 time_t now
; /* Current time */
243 * Don't load the cache if the remote protocols are disabled...
248 cupsdLogMessage(CUPSD_LOG_DEBUG
,
249 "cupsdLoadRemoteCache: Not loading remote cache.");
254 * Open the remote.cache file...
257 snprintf(line
, sizeof(line
), "%s/remote.cache", CacheDir
);
258 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
262 * Read printer configurations until we hit EOF...
269 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
272 * Decode the directive...
275 if (!strcasecmp(line
, "<Printer") ||
276 !strcasecmp(line
, "<DefaultPrinter"))
279 * <Printer name> or <DefaultPrinter name>
282 if (p
== NULL
&& value
)
285 * Add the printer and a base file type...
288 cupsdLogMessage(CUPSD_LOG_DEBUG
,
289 "cupsdLoadRemoteCache: Loading printer %s...", value
);
291 if ((p
= cupsdFindDest(value
)) != NULL
)
293 if (p
->type
& CUPS_PRINTER_CLASS
)
295 cupsdLogMessage(CUPSD_LOG_WARN
,
296 "Cached remote printer \"%s\" conflicts with "
304 p
= cupsdAddPrinter(value
);
307 p
->state
= IPP_PRINTER_IDLE
;
308 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
309 p
->browse_time
= now
;
310 p
->browse_expire
= now
+ BrowseTimeout
;
313 * Set the default printer as needed...
316 if (!strcasecmp(line
, "<DefaultPrinter"))
321 cupsdLogMessage(CUPSD_LOG_ERROR
,
322 "Syntax error on line %d of remote.cache.", linenum
);
326 else if (!strcasecmp(line
, "<Class") ||
327 !strcasecmp(line
, "<DefaultClass"))
330 * <Class name> or <DefaultClass name>
333 if (p
== NULL
&& value
)
336 * Add the printer and a base file type...
339 cupsdLogMessage(CUPSD_LOG_DEBUG
,
340 "cupsdLoadRemoteCache: Loading class %s...", value
);
342 if ((p
= cupsdFindDest(value
)) != NULL
)
343 p
->type
= CUPS_PRINTER_CLASS
;
345 p
= cupsdAddClass(value
);
348 p
->state
= IPP_PRINTER_IDLE
;
349 p
->type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
350 p
->browse_time
= now
;
351 p
->browse_expire
= now
+ BrowseTimeout
;
354 * Set the default printer as needed...
357 if (!strcasecmp(line
, "<DefaultClass"))
362 cupsdLogMessage(CUPSD_LOG_ERROR
,
363 "Syntax error on line %d of remote.cache.", linenum
);
367 else if (!strcasecmp(line
, "</Printer>") ||
368 !strcasecmp(line
, "</Class>"))
373 * Close out the current printer...
376 cupsdSetPrinterAttrs(p
);
382 cupsdLogMessage(CUPSD_LOG_ERROR
,
383 "Syntax error on line %d of remote.cache.", linenum
);
389 cupsdLogMessage(CUPSD_LOG_ERROR
,
390 "Syntax error on line %d of remote.cache.", linenum
);
393 else if (!strcasecmp(line
, "Info"))
396 cupsdSetString(&p
->info
, value
);
398 else if (!strcasecmp(line
, "MakeModel"))
401 cupsdSetString(&p
->make_model
, value
);
403 else if (!strcasecmp(line
, "Location"))
406 cupsdSetString(&p
->location
, value
);
408 else if (!strcasecmp(line
, "DeviceURI"))
412 httpSeparateURI(HTTP_URI_CODING_ALL
, value
, scheme
, sizeof(scheme
),
413 username
, sizeof(username
), host
, sizeof(host
), &port
,
414 resource
, sizeof(resource
));
416 cupsdSetString(&p
->hostname
, host
);
417 cupsdSetString(&p
->uri
, value
);
418 cupsdSetString(&p
->device_uri
, value
);
422 cupsdLogMessage(CUPSD_LOG_ERROR
,
423 "Syntax error on line %d of remote.cache.", linenum
);
427 else if (!strcasecmp(line
, "Option") && value
)
433 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
436 cupsdLogMessage(CUPSD_LOG_ERROR
,
437 "Syntax error on line %d of remote.cache.", linenum
);
440 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
442 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
446 else if (!strcasecmp(line
, "State"))
449 * Set the initial queue state...
452 if (value
&& !strcasecmp(value
, "idle"))
453 p
->state
= IPP_PRINTER_IDLE
;
454 else if (value
&& !strcasecmp(value
, "stopped"))
455 p
->state
= IPP_PRINTER_STOPPED
;
458 cupsdLogMessage(CUPSD_LOG_ERROR
,
459 "Syntax error on line %d of remote.cache.", linenum
);
463 else if (!strcasecmp(line
, "StateMessage"))
466 * Set the initial queue state message...
470 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
472 else if (!strcasecmp(line
, "Accepting"))
475 * Set the initial accepting state...
479 (!strcasecmp(value
, "yes") ||
480 !strcasecmp(value
, "on") ||
481 !strcasecmp(value
, "true")))
484 (!strcasecmp(value
, "no") ||
485 !strcasecmp(value
, "off") ||
486 !strcasecmp(value
, "false")))
490 cupsdLogMessage(CUPSD_LOG_ERROR
,
491 "Syntax error on line %d of remote.cache.", linenum
);
495 else if (!strcasecmp(line
, "Type"))
498 p
->type
= atoi(value
);
501 cupsdLogMessage(CUPSD_LOG_ERROR
,
502 "Syntax error on line %d of remote.cache.", linenum
);
506 else if (!strcasecmp(line
, "BrowseTime"))
510 time_t t
= atoi(value
);
512 if (t
> p
->browse_expire
)
513 p
->browse_expire
= t
;
517 cupsdLogMessage(CUPSD_LOG_ERROR
,
518 "Syntax error on line %d of remote.cache.", linenum
);
522 else if (!strcasecmp(line
, "JobSheets"))
525 * Set the initial job sheets...
530 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
535 cupsdSetString(&p
->job_sheets
[0], value
);
537 while (isspace(*valueptr
& 255))
542 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
547 cupsdSetString(&p
->job_sheets
[1], value
);
552 cupsdLogMessage(CUPSD_LOG_ERROR
,
553 "Syntax error on line %d of remote.cache.", linenum
);
557 else if (!strcasecmp(line
, "AllowUser"))
562 cupsdAddPrinterUser(p
, value
);
566 cupsdLogMessage(CUPSD_LOG_ERROR
,
567 "Syntax error on line %d of remote.cache.", linenum
);
571 else if (!strcasecmp(line
, "DenyUser"))
576 cupsdAddPrinterUser(p
, value
);
580 cupsdLogMessage(CUPSD_LOG_ERROR
,
581 "Syntax error on line %d of remote.cache.", linenum
);
588 * Something else we don't understand...
591 cupsdLogMessage(CUPSD_LOG_ERROR
,
592 "Unknown configuration directive %s on line %d of remote.cache.",
600 * Do auto-classing if needed...
603 process_implicit_classes();
608 * 'cupsdRegisterPrinter()' - Start sending broadcast information for a
609 * printer or update the broadcast contents.
613 cupsdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
615 if (!Browsing
|| !BrowseLocalProtocols
||
616 (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
620 /* if (BrowseLocalProtocols & BROWSE_SLP)
621 slpRegisterPrinter(p); */
622 #endif /* HAVE_LIBSLP */
625 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
626 dnssdRegisterPrinter(p
);
627 #endif /* HAVE_DNSSD */
632 * 'cupsdRestartPolling()' - Restart polling servers as needed.
636 cupsdRestartPolling(void)
638 int i
; /* Looping var */
639 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
642 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
644 kill(pollp
->pid
, SIGHUP
);
649 * 'cupsdSaveRemoteCache()' - Save the remote printer cache.
653 cupsdSaveRemoteCache(void)
655 int i
; /* Looping var */
656 cups_file_t
*fp
; /* printers.conf file */
657 char temp
[1024]; /* Temporary string */
658 cupsd_printer_t
*printer
; /* Current printer class */
659 time_t curtime
; /* Current time */
660 struct tm
*curdate
; /* Current date */
661 cups_option_t
*option
; /* Current option */
665 * Create the remote.cache file...
668 snprintf(temp
, sizeof(temp
), "%s/remote.cache", CacheDir
);
670 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
672 cupsdLogMessage(CUPSD_LOG_ERROR
,
673 "Unable to save remote.cache - %s", strerror(errno
));
677 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Saving remote.cache...");
680 * Restrict access to the file...
683 fchown(cupsFileNumber(fp
), getuid(), Group
);
684 fchmod(cupsFileNumber(fp
), ConfigFilePerm
);
687 * Write a small header to the file...
690 curtime
= time(NULL
);
691 curdate
= localtime(&curtime
);
692 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
694 cupsFilePuts(fp
, "# Remote cache file for " CUPS_SVERSION
"\n");
695 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
698 * Write each local printer known to the system...
701 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
703 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
706 * Skip local destinations...
709 if (!(printer
->type
& CUPS_PRINTER_DISCOVERED
))
713 * Write printers as needed...
716 if (printer
== DefaultPrinter
)
717 cupsFilePuts(fp
, "<Default");
719 cupsFilePutChar(fp
, '<');
721 if (printer
->type
& CUPS_PRINTER_CLASS
)
722 cupsFilePrintf(fp
, "Class %s>\n", printer
->name
);
724 cupsFilePrintf(fp
, "Printer %s>\n", printer
->name
);
726 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
728 cupsFilePrintf(fp
, "BrowseTime %d\n", (int)printer
->browse_expire
);
731 cupsFilePrintf(fp
, "Info %s\n", printer
->info
);
733 if (printer
->make_model
)
734 cupsFilePrintf(fp
, "MakeModel %s\n", printer
->make_model
);
736 if (printer
->location
)
737 cupsFilePrintf(fp
, "Location %s\n", printer
->location
);
739 if (printer
->device_uri
)
740 cupsFilePrintf(fp
, "DeviceURI %s\n", printer
->device_uri
);
742 if (printer
->state
== IPP_PRINTER_STOPPED
)
744 cupsFilePuts(fp
, "State Stopped\n");
745 cupsFilePrintf(fp
, "StateMessage %s\n", printer
->state_message
);
748 cupsFilePuts(fp
, "State Idle\n");
750 if (printer
->accepting
)
751 cupsFilePuts(fp
, "Accepting Yes\n");
753 cupsFilePuts(fp
, "Accepting No\n");
755 cupsFilePrintf(fp
, "JobSheets %s %s\n", printer
->job_sheets
[0],
756 printer
->job_sheets
[1]);
758 for (i
= 0; i
< printer
->num_users
; i
++)
759 cupsFilePrintf(fp
, "%sUser %s\n", printer
->deny_users
? "Deny" : "Allow",
762 for (i
= printer
->num_options
, option
= printer
->options
;
765 cupsFilePrintf(fp
, "Option %s %s\n", option
->name
, option
->value
);
767 if (printer
->type
& CUPS_PRINTER_CLASS
)
768 cupsFilePuts(fp
, "</Class>\n");
770 cupsFilePuts(fp
, "</Printer>\n");
778 * 'cupsdSendBrowseList()' - Send new browsing information as necessary.
782 cupsdSendBrowseList(void)
784 int count
; /* Number of dests to update */
785 cupsd_printer_t
*p
; /* Current printer */
786 time_t ut
, /* Minimum update time */
787 to
; /* Timeout time */
790 if (!Browsing
|| !Printers
)
794 * Compute the update and timeout times...
798 ut
= to
- BrowseInterval
;
801 * Figure out how many printers need an update...
804 if (BrowseInterval
> 0 && BrowseLocalProtocols
)
806 int max_count
; /* Maximum number to update */
810 * Throttle the number of printers we'll be updating this time
811 * around based on the number of queues that need updating and
812 * the maximum number of queues to update each second...
815 max_count
= 2 * cupsArrayCount(Printers
) / BrowseInterval
+ 1;
817 for (count
= 0, p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
818 count
< max_count
&& p
!= NULL
;
819 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
820 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) &&
821 p
->shared
&& p
->browse_time
< ut
)
825 * Loop through all of the printers and send local updates as needed...
829 p
= (cupsd_printer_t
*)cupsArrayFind(Printers
, BrowseNext
);
831 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
835 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
838 * Check for wraparound...
842 p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
846 else if ((p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)) ||
849 else if (p
->browse_time
< ut
)
852 * Need to send an update...
857 p
->browse_time
= time(NULL
);
859 if ((BrowseLocalProtocols
& BROWSE_CUPS
) && BrowseSocket
>= 0)
863 if (BrowseLocalProtocols
& BROWSE_SLP
)
865 #endif /* HAVE_LIBSLP */
868 if (BrowseLocalProtocols
& BROWSE_LDAP
)
870 #endif /* HAVE_LDAP */
875 * Save where we left off so that all printers get updated...
882 * Loop through all of the printers and timeout old printers as needed...
885 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
887 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
890 * If this is a remote queue, see if it needs to be timed out...
893 if ((p
->type
& CUPS_PRINTER_DISCOVERED
) &&
894 !(p
->type
& CUPS_PRINTER_IMPLICIT
) &&
895 p
->browse_expire
< to
)
897 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
898 "%s \'%s\' deleted by directory services (timeout).",
899 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
902 cupsdLogMessage(CUPSD_LOG_DEBUG
,
903 "Remote destination \"%s\" has timed out; "
907 cupsArraySave(Printers
);
908 cupsdDeletePrinter(p
, 1);
909 cupsArrayRestore(Printers
);
910 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
916 #ifdef HAVE_LDAP_REBIND_PROC
917 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
919 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
923 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
924 LDAP_CONST
char *refsp
,
932 * Bind to new LDAP server...
935 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
936 "ldap_rebind_proc: Rebind to %s", refsp
);
938 # if LDAP_API_VERSION > 3000
940 bval
.bv_val
= BrowseLDAPPassword
;
941 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
943 rc
= ldap_sasl_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
945 rc
= ldap_bind_s(RebindLDAPHandle
, BrowseLDAPBindDN
,
946 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
947 # endif /* LDAP_API_VERSION > 3000 */
952 # else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
955 * 'ldap_rebind_proc()' - Callback function for LDAP rebind
959 ldap_rebind_proc (LDAP
*RebindLDAPHandle
,
971 * Free current values...
974 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
975 "ldap_rebind_proc: Free values...");
980 if ( passwdp
&& *passwdp
) {
988 * Return credentials for LDAP referal...
991 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
992 "ldap_rebind_proc: Return necessary values...");
994 *dnp
= strdup(BrowseLDAPBindDN
);
995 *passwdp
= strdup(BrowseLDAPPassword
);
996 *authmethodp
= LDAP_AUTH_SIMPLE
;
1002 * Should never happen...
1005 cupsdLogMessage(CUPSD_LOG_ERROR
,
1006 "LDAP rebind has been called with wrong freeit value!");
1011 return (LDAP_SUCCESS
);
1013 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1014 #endif /* HAVE_LDAP_REBIND_PROC */
1019 * 'ldap_connect()' - Start new LDAP connection
1026 * Open LDAP handle...
1029 int rc
; /* LDAP API status */
1030 int version
= 3; /* LDAP version */
1031 struct berval bv
= {0, ""}; /* SASL bind value */
1032 LDAP
*TempBrowseLDAPHandle
=NULL
; /* Temporary LDAP Handle */
1033 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1034 int ldap_ssl
= 0; /* LDAP SSL indicator */
1035 int ssl_err
= 0; /* LDAP SSL error value */
1036 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1038 # ifdef HAVE_OPENLDAP
1039 # ifdef HAVE_LDAP_SSL
1042 * Set the certificate file to use for encrypted LDAP sessions...
1045 if (BrowseLDAPCACertFile
)
1047 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1048 "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
1049 BrowseLDAPCACertFile
);
1051 if ((rc
= ldap_set_option(NULL
, LDAP_OPT_X_TLS_CACERTFILE
,
1052 (void *)BrowseLDAPCACertFile
)) != LDAP_SUCCESS
)
1053 cupsdLogMessage(CUPSD_LOG_ERROR
,
1054 "Unable to set CA certificate file for LDAP "
1055 "connections: %d - %s", rc
, ldap_err2string(rc
));
1058 # endif /* HAVE_LDAP_SSL */
1060 * Initialize OPENLDAP connection...
1061 * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
1064 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1065 rc
= ldap_initialize(&TempBrowseLDAPHandle
, "ldapi:///");
1067 rc
= ldap_initialize(&TempBrowseLDAPHandle
, BrowseLDAPServer
);
1069 # else /* HAVE_OPENLDAP */
1071 int ldap_port
= 0; /* LDAP port */
1072 char ldap_protocol
[11], /* LDAP protocol */
1073 ldap_host
[255]; /* LDAP host */
1076 * Split LDAP URI into its components...
1079 if (! BrowseLDAPServer
)
1081 cupsdLogMessage(CUPSD_LOG_ERROR
,
1082 "BrowseLDAPServer not configured! Disable LDAP browsing!");
1083 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1084 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1088 sscanf(BrowseLDAPServer
, "%10[^:]://%254[^:/]:%d", ldap_protocol
, ldap_host
, &ldap_port
);
1090 if (strcmp(ldap_protocol
, "ldap") == 0) {
1092 } else if (strcmp(ldap_protocol
, "ldaps") == 0) {
1095 cupsdLogMessage(CUPSD_LOG_ERROR
,
1096 "unrecognised ldap protocol (%s)!", ldap_protocol
);
1097 cupsdLogMessage(CUPSD_LOG_ERROR
,
1098 "Disable LDAP browsing!");
1099 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1100 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1107 ldap_port
= LDAPS_PORT
;
1109 ldap_port
= LDAP_PORT
;
1112 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1113 "LDAP Connection Details: PROT:%s HOST:%s PORT:%d",
1114 ldap_protocol
, ldap_host
, ldap_port
);
1117 * Initialize LDAP connection...
1122 if ((TempBrowseLDAPHandle
= ldap_init(ldap_host
, ldap_port
)) == NULL
)
1123 rc
= LDAP_OPERATIONS_ERROR
;
1127 # ifdef HAVE_LDAP_SSL
1133 * Initialize SSL LDAP connection...
1135 if (BrowseLDAPCACertFile
)
1137 rc
= ldapssl_client_init(BrowseLDAPCACertFile
, (void *)NULL
);
1138 if (rc
!= LDAP_SUCCESS
) {
1139 cupsdLogMessage(CUPSD_LOG_ERROR
,
1140 "Failed to initialize LDAP SSL client!");
1141 rc
= LDAP_OPERATIONS_ERROR
;
1143 if ((TempBrowseLDAPHandle
= ldapssl_init(ldap_host
, ldap_port
, 1)) == NULL
)
1144 rc
= LDAP_OPERATIONS_ERROR
;
1151 cupsdLogMessage(CUPSD_LOG_ERROR
,
1152 "LDAP SSL certificate file/database not configured!");
1153 rc
= LDAP_OPERATIONS_ERROR
;
1156 # else /* HAVE_LDAP_SSL */
1159 * Return error, because client libraries doesn't support SSL
1162 cupsdLogMessage(CUPSD_LOG_ERROR
,
1163 "LDAP client libraries does not support TLS");
1164 rc
= LDAP_OPERATIONS_ERROR
;
1166 # endif /* HAVE_LDAP_SSL */
1168 # endif /* HAVE_OPENLDAP */
1171 * Check return code from LDAP initialize...
1174 if (rc
!= LDAP_SUCCESS
)
1176 if ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
))
1178 cupsdLogMessage(CUPSD_LOG_ERROR
,
1179 "Unable to initialize LDAP! Temporary disable LDAP browsing...");
1183 cupsdLogMessage(CUPSD_LOG_ERROR
,
1184 "Unable to initialize LDAP! Disable LDAP browsing!");
1185 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1186 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1189 ldap_disconnect(TempBrowseLDAPHandle
);
1190 TempBrowseLDAPHandle
= NULL
;
1194 * Upgrade LDAP version...
1197 else if (ldap_set_option(TempBrowseLDAPHandle
, LDAP_OPT_PROTOCOL_VERSION
,
1198 (const void *)&version
) != LDAP_SUCCESS
)
1200 cupsdLogMessage(CUPSD_LOG_ERROR
,
1201 "Unable to set LDAP protocol version %d! Disable LDAP browsing!",
1203 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1204 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1205 ldap_disconnect(TempBrowseLDAPHandle
);
1206 TempBrowseLDAPHandle
= NULL
;
1212 * Register LDAP rebind procedure...
1215 # ifdef HAVE_LDAP_REBIND_PROC
1216 # if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
1218 rc
= ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1219 if ( rc
!= LDAP_SUCCESS
)
1220 cupsdLogMessage(CUPSD_LOG_ERROR
,
1221 "Setting LDAP rebind function failed with status %d: %s",
1222 rc
, ldap_err2string(rc
));
1226 ldap_set_rebind_proc(TempBrowseLDAPHandle
, &ldap_rebind_proc
, (void *)NULL
);
1228 # endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
1229 # endif /* HAVE_LDAP_REBIND_PROC */
1232 * Start LDAP bind...
1235 # if LDAP_API_VERSION > 3000
1237 bval
.bv_val
= BrowseLDAPPassword
;
1238 bval
.bv_len
= (BrowseLDAPPassword
== NULL
) ? 0 : strlen(BrowseLDAPPassword
);
1240 if (!BrowseLDAPServer
|| !strcasecmp(BrowseLDAPServer
, "localhost"))
1241 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, NULL
, "EXTERNAL", &bv
, NULL
,
1244 rc
= ldap_sasl_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
, LDAP_SASL_SIMPLE
, &bval
, NULL
, NULL
, NULL
);
1246 rc
= ldap_bind_s(TempBrowseLDAPHandle
, BrowseLDAPBindDN
,
1247 BrowseLDAPPassword
, LDAP_AUTH_SIMPLE
);
1248 # endif /* LDAP_API_VERSION > 3000 */
1250 if (rc
!= LDAP_SUCCESS
)
1252 cupsdLogMessage(CUPSD_LOG_ERROR
,
1253 "LDAP bind failed with error %d: %s",
1254 rc
, ldap_err2string(rc
));
1255 # if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
1256 if (ldap_ssl
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1258 ssl_err
= PORT_GetError();
1260 cupsdLogMessage(CUPSD_LOG_ERROR
,
1261 "LDAP SSL error %d: %s",
1262 ssl_err
, ldapssl_err2string(ssl_err
));
1264 # endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
1265 ldap_disconnect(TempBrowseLDAPHandle
);
1266 TempBrowseLDAPHandle
= NULL
;
1270 cupsdLogMessage(CUPSD_LOG_INFO
,
1271 "LDAP connection established");
1275 return (TempBrowseLDAPHandle
);
1280 * 'ldap_reconnect()' - Reconnect to LDAP Server
1284 ldap_reconnect(void)
1286 LDAP
*TempBrowseLDAPHandle
= NULL
; /* Temp Handle to LDAP server */
1288 cupsdLogMessage(CUPSD_LOG_INFO
,
1289 "Try LDAP reconnect...");
1292 * Get a new LDAP Handle and replace the global Handle
1293 * if the new connection was successful
1296 TempBrowseLDAPHandle
= ldap_connect();
1298 if (TempBrowseLDAPHandle
!= NULL
)
1300 if (BrowseLDAPHandle
!= NULL
)
1302 ldap_disconnect(BrowseLDAPHandle
);
1304 BrowseLDAPHandle
= TempBrowseLDAPHandle
;
1310 * 'ldap_disconnect()' - Disconnect from LDAP Server
1314 ldap_disconnect(LDAP
*ld
) /* I - LDAP handle */
1316 int rc
; /* return code */
1319 * Close LDAP handle...
1322 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
1323 rc
= ldap_unbind_ext_s(ld
, NULL
, NULL
);
1325 rc
= ldap_unbind_s(ld
);
1326 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
1327 if (rc
!= LDAP_SUCCESS
)
1328 cupsdLogMessage(CUPSD_LOG_ERROR
,
1329 "Unbind from LDAP server failed with status %d: %s",
1330 rc
, ldap_err2string(rc
));
1332 #endif /* HAVE_LDAP */
1336 * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
1340 cupsdStartBrowsing(void)
1342 int val
; /* Socket option value */
1343 struct sockaddr_in addr
; /* Broadcast address */
1344 cupsd_printer_t
*p
; /* Current printer */
1349 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1352 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
)
1354 if (BrowseSocket
< 0)
1357 * Create the broadcast socket...
1360 if ((BrowseSocket
= socket(AF_INET
, SOCK_DGRAM
, 0)) < 0)
1362 cupsdLogMessage(CUPSD_LOG_ERROR
,
1363 "Unable to create broadcast socket - %s.",
1365 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1366 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1371 * Bind the socket to browse port...
1374 memset(&addr
, 0, sizeof(addr
));
1375 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
1376 addr
.sin_family
= AF_INET
;
1377 addr
.sin_port
= htons(BrowsePort
);
1379 if (bind(BrowseSocket
, (struct sockaddr
*)&addr
, sizeof(addr
)))
1381 cupsdLogMessage(CUPSD_LOG_ERROR
,
1382 "Unable to bind broadcast socket - %s.",
1386 closesocket(BrowseSocket
);
1388 close(BrowseSocket
);
1392 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1393 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1399 * Set the "broadcast" flag...
1403 if (setsockopt(BrowseSocket
, SOL_SOCKET
, SO_BROADCAST
, &val
, sizeof(val
)))
1405 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to set broadcast mode - %s.",
1409 closesocket(BrowseSocket
);
1411 close(BrowseSocket
);
1415 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
1416 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
1421 * Close the socket on exec...
1424 fcntl(BrowseSocket
, F_SETFD
, fcntl(BrowseSocket
, F_GETFD
) | FD_CLOEXEC
);
1427 * Finally, add the socket to the input selection set as needed...
1430 if (BrowseRemoteProtocols
& BROWSE_CUPS
)
1433 * We only listen if we want remote printers...
1436 cupsdAddSelect(BrowseSocket
, (cupsd_selfunc_t
)update_cups_browse
,
1444 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_DNSSD
)
1446 DNSServiceErrorType error
; /* Error from service creation */
1447 cupsd_listener_t
*lis
; /* Current listening socket */
1451 * First create a "master" connection for all registrations...
1454 if ((error
= DNSServiceCreateConnection(&DNSSDRef
))
1455 != kDNSServiceErr_NoError
)
1456 cupsdLogMessage(CUPSD_LOG_ERROR
,
1457 "Unable to create master DNS-SD reference: %d", error
);
1461 * Add the master connection to the select list...
1464 cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef
),
1465 (cupsd_selfunc_t
)dnssdUpdate
, NULL
, NULL
);
1468 * Then get the port we use for registrations. If we are not listening
1469 * on any non-local ports, there is no sense sharing local printers via
1475 for (lis
= (cupsd_listener_t
*)cupsArrayFirst(Listeners
);
1477 lis
= (cupsd_listener_t
*)cupsArrayNext(Listeners
))
1479 if (httpAddrLocalhost(&(lis
->address
)))
1482 if (lis
->address
.addr
.sa_family
== AF_INET
)
1484 DNSSDPort
= ntohs(lis
->address
.ipv4
.sin_port
);
1487 else if (lis
->address
.addr
.sa_family
== AF_INET6
)
1489 DNSSDPort
= ntohs(lis
->address
.ipv6
.sin6_port
);
1495 * Create an array to track the printers we share...
1498 if (BrowseRemoteProtocols
& BROWSE_DNSSD
)
1499 DNSSDPrinters
= cupsArrayNew((cups_array_func_t
)dnssdComparePrinters
,
1503 * Set the computer name and register the web interface...
1506 cupsdUpdateDNSSDName();
1509 #endif /* HAVE_DNSSD */
1512 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
)
1515 * Open SLP handle...
1518 if (SLPOpen("en", SLP_FALSE
, &BrowseSLPHandle
) != SLP_OK
)
1520 cupsdLogMessage(CUPSD_LOG_ERROR
,
1521 "Unable to open an SLP handle; disabling SLP browsing!");
1522 BrowseLocalProtocols
&= ~BROWSE_SLP
;
1523 BrowseRemoteProtocols
&= ~BROWSE_SLP
;
1526 BrowseSLPRefresh
= 0;
1529 BrowseSLPHandle
= NULL
;
1530 #endif /* HAVE_LIBSLP */
1533 if ((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
)
1537 cupsdLogMessage(CUPSD_LOG_ERROR
,
1538 "Need to set BrowseLDAPDN to use LDAP browsing!");
1539 BrowseLocalProtocols
&= ~BROWSE_LDAP
;
1540 BrowseRemoteProtocols
&= ~BROWSE_LDAP
;
1544 /* Open LDAP handle... */
1545 BrowseLDAPHandle
= ldap_connect();
1548 BrowseLDAPRefresh
= 0;
1550 #endif /* HAVE_LDAP */
1553 * Enable LPD and SMB printer sharing as needed through external programs...
1556 if (BrowseLocalProtocols
& BROWSE_LPD
)
1559 if (BrowseLocalProtocols
& BROWSE_SMB
)
1563 * Register the individual printers
1566 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1568 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1569 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1570 cupsdRegisterPrinter(p
);
1575 * 'cupsdStartPolling()' - Start polling servers as needed.
1579 cupsdStartPolling(void)
1581 int i
; /* Looping var */
1582 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1583 char polld
[1024]; /* Poll daemon path */
1584 char sport
[255]; /* Server port */
1585 char bport
[255]; /* Browser port */
1586 char interval
[255]; /* Poll interval */
1587 int statusfds
[2]; /* Status pipe */
1588 char *argv
[6]; /* Arguments */
1589 char *envp
[100]; /* Environment */
1593 * Don't do anything if we aren't polling...
1596 if (NumPolled
== 0 || BrowseSocket
< 0)
1599 PollStatusBuffer
= NULL
;
1604 * Setup string arguments for polld, port and interval options.
1607 snprintf(polld
, sizeof(polld
), "%s/daemon/cups-polld", ServerBin
);
1609 sprintf(bport
, "%d", BrowsePort
);
1612 sprintf(interval
, "%d", BrowseInterval
);
1614 strcpy(interval
, "30");
1616 argv
[0] = "cups-polld";
1622 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
1625 * Create a pipe that receives the status messages from each
1629 if (cupsdOpenPipe(statusfds
))
1631 cupsdLogMessage(CUPSD_LOG_ERROR
,
1632 "Unable to create polling status pipes - %s.",
1635 PollStatusBuffer
= NULL
;
1639 PollPipe
= statusfds
[0];
1640 PollStatusBuffer
= cupsdStatBufNew(PollPipe
, "[Poll]");
1643 * Run each polling daemon, redirecting stderr to the polling pipe...
1646 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1648 sprintf(sport
, "%d", pollp
->port
);
1650 argv
[1] = pollp
->hostname
;
1652 if (cupsdStartProcess(polld
, argv
, envp
, -1, -1, statusfds
[1], -1, -1,
1653 0, DefaultProfile
, &(pollp
->pid
)) < 0)
1655 cupsdLogMessage(CUPSD_LOG_ERROR
,
1656 "cupsdStartPolling: Unable to fork polling daemon - %s",
1662 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1663 "cupsdStartPolling: Started polling daemon for %s:%d, pid = %d",
1664 pollp
->hostname
, pollp
->port
, pollp
->pid
);
1667 close(statusfds
[1]);
1670 * Finally, add the pipe to the input selection set...
1673 cupsdAddSelect(PollPipe
, (cupsd_selfunc_t
)update_polling
, NULL
, NULL
);
1678 * 'cupsdStopBrowsing()' - Stop sending and receiving broadcast information.
1682 cupsdStopBrowsing(void)
1684 cupsd_printer_t
*p
; /* Current printer */
1687 if (!Browsing
|| !(BrowseLocalProtocols
| BrowseRemoteProtocols
))
1691 * De-register the individual printers
1694 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1696 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1697 if (!(p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
)))
1698 cupsdDeregisterPrinter(p
, 1);
1701 * Shut down browsing sockets...
1704 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_CUPS
) &&
1708 * Close the socket and remove it from the input selection set.
1712 closesocket(BrowseSocket
);
1714 close(BrowseSocket
);
1717 cupsdRemoveSelect(BrowseSocket
);
1722 if ((BrowseLocalProtocols
& BROWSE_DNSSD
) && DNSSDRef
)
1726 DNSServiceRefDeallocate(WebIFRef
);
1732 DNSServiceRefDeallocate(RemoteRef
);
1736 cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef
));
1738 DNSServiceRefDeallocate(DNSSDRef
);
1741 cupsArrayDelete(DNSSDPrinters
);
1742 DNSSDPrinters
= NULL
;
1746 #endif /* HAVE_DNSSD */
1749 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_SLP
) &&
1753 * Close SLP handle...
1756 SLPClose(BrowseSLPHandle
);
1757 BrowseSLPHandle
= NULL
;
1759 #endif /* HAVE_LIBSLP */
1762 if (((BrowseLocalProtocols
| BrowseRemoteProtocols
) & BROWSE_LDAP
) &&
1765 ldap_dereg_ou(ServerName
, BrowseLDAPDN
);
1766 ldap_disconnect(BrowseLDAPHandle
);
1767 BrowseLDAPHandle
= NULL
;
1769 #endif /* HAVE_OPENLDAP */
1772 * Disable LPD and SMB printer sharing as needed through external programs...
1775 if (BrowseLocalProtocols
& BROWSE_LPD
)
1778 if (BrowseLocalProtocols
& BROWSE_SMB
)
1784 * 'cupsdStopPolling()' - Stop polling servers as needed.
1788 cupsdStopPolling(void)
1790 int i
; /* Looping var */
1791 cupsd_dirsvc_poll_t
*pollp
; /* Current polling server */
1796 cupsdStatBufDelete(PollStatusBuffer
);
1799 cupsdRemoveSelect(PollPipe
);
1802 PollStatusBuffer
= NULL
;
1805 for (i
= 0, pollp
= Polled
; i
< NumPolled
; i
++, pollp
++)
1807 cupsdEndProcess(pollp
->pid
, 0);
1813 * 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
1817 cupsdUpdateDNSSDName(void)
1819 DNSServiceErrorType error
; /* Error from service creation */
1820 char webif
[1024]; /* Web interface share name */
1821 #ifdef HAVE_COREFOUNDATION_H
1822 CFStringRef nameRef
; /* Computer name CFString */
1823 char nameBuffer
[1024]; /* C-string buffer */
1824 CFStringEncoding nameEncoding
; /* Computer name encoding */
1825 #endif /* HAVE_COREFOUNDATION_H */
1829 * Only share the web interface and printers when non-local listening is
1837 * Get the computer name as a c-string...
1840 #ifdef HAVE_COREFOUNDATION_H
1841 cupsdClearString(&DNSSDName
);
1843 if ((nameRef
= SCDynamicStoreCopyComputerName(NULL
,
1844 &nameEncoding
)) != NULL
)
1846 if (CFStringGetCString(nameRef
, nameBuffer
, sizeof(nameBuffer
),
1847 kCFStringEncodingUTF8
))
1848 cupsdSetString(&DNSSDName
, nameBuffer
);
1854 cupsdSetString(&DNSSDName
, ServerName
);
1855 #endif /* HAVE_COREFOUNDATION_H */
1858 * Then (re)register the web interface...
1862 snprintf(webif
, sizeof(webif
), "CUPS @ %s", DNSSDName
);
1864 strlcpy(webif
, "CUPS Web Interface", sizeof(webif
));
1867 DNSServiceRefDeallocate(WebIFRef
);
1869 WebIFRef
= DNSSDRef
;
1870 if ((error
= DNSServiceRegister(&WebIFRef
,
1871 kDNSServiceFlagsShareConnection
,
1872 0, webif
, "_http._tcp", NULL
,
1873 NULL
, htons(DNSSDPort
), 7,
1874 "\006path=/", dnssdRegisterCallback
,
1875 NULL
)) != kDNSServiceErr_NoError
)
1876 cupsdLogMessage(CUPSD_LOG_ERROR
,
1877 "DNS-SD web interface registration failed: %d", error
);
1879 #endif /* HAVE_DNSSD */
1884 * 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
1888 cupsdUpdateLDAPBrowse(void)
1890 char uri
[HTTP_MAX_URI
], /* Printer URI */
1891 host
[HTTP_MAX_URI
], /* Hostname */
1892 resource
[HTTP_MAX_URI
], /* Resource path */
1893 location
[1024], /* Printer location */
1894 info
[1024], /* Printer information */
1895 make_model
[1024], /* Printer make and model */
1896 type_num
[30]; /* Printer type number */
1897 int type
; /* Printer type */
1898 int rc
; /* LDAP status */
1899 int limit
; /* Size limit */
1900 LDAPMessage
*res
, /* LDAP search results */
1901 *e
; /* Current entry from search */
1903 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "UpdateLDAPBrowse: %s", ServerName
);
1905 BrowseLDAPRefresh
= time(NULL
) + BrowseInterval
;
1908 * Reconnect if LDAP Handle is invalid...
1911 if (! BrowseLDAPHandle
)
1918 * Search for cups printers in LDAP directory...
1921 rc
= ldap_search_rec(BrowseLDAPHandle
, BrowseLDAPDN
, LDAP_SCOPE_SUBTREE
,
1922 "(objectclass=cupsPrinter)", (char **)ldap_attrs
, 0, &res
);
1925 * If ldap search was successfull then exit function
1926 * and temporary disable LDAP updates...
1929 if (rc
!= LDAP_SUCCESS
)
1931 if (BrowseLDAPUpdate
&& ((rc
== LDAP_SERVER_DOWN
) || (rc
== LDAP_CONNECT_ERROR
)))
1933 BrowseLDAPUpdate
= FALSE
;
1934 cupsdLogMessage(CUPSD_LOG_INFO
,
1935 "LDAP update temporary disabled");
1941 * If LDAP updates were disabled, we will reenable them...
1944 if (! BrowseLDAPUpdate
)
1946 BrowseLDAPUpdate
= TRUE
;
1947 cupsdLogMessage(CUPSD_LOG_INFO
,
1948 "LDAP update enabled");
1952 * Count LDAP entries and return if no entry exist...
1955 limit
= ldap_count_entries(BrowseLDAPHandle
, res
);
1956 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "LDAP search returned %d entries", limit
);
1964 * Loop through the available printers...
1967 for (e
= ldap_first_entry(BrowseLDAPHandle
, res
);
1969 e
= ldap_next_entry(BrowseLDAPHandle
, e
))
1972 * Get the required values from this entry...
1975 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
1976 "printerDescription", info
, sizeof(info
)) == -1)
1979 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
1980 "printerLocation", location
, sizeof(location
)) == -1)
1983 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
1984 "printerMakeAndModel", make_model
, sizeof(make_model
)) == -1)
1987 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
1988 "printerType", type_num
, sizeof(type_num
)) == -1)
1991 type
= atoi(type_num
);
1993 if (ldap_getval_firststring(BrowseLDAPHandle
, e
,
1994 "printerURI", uri
, sizeof(uri
)) == -1)
1998 * Process the entry as browse data...
2001 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2002 process_browse_data(uri
, host
, resource
, type
, IPP_PRINTER_IDLE
,
2003 location
, info
, make_model
, 0, NULL
);
2009 #endif /* HAVE_LDAP */
2014 * 'cupsdUpdateSLPBrowse()' - Get browsing information via SLP.
2018 cupsdUpdateSLPBrowse(void)
2020 slpsrvurl_t
*s
, /* Temporary list of service URLs */
2021 *next
; /* Next service in list */
2022 cupsd_printer_t p
; /* Printer information */
2023 const char *uri
; /* Pointer to printer URI */
2024 char host
[HTTP_MAX_URI
], /* Host portion of URI */
2025 resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2029 * Reset the refresh time...
2032 BrowseSLPRefresh
= time(NULL
) + BrowseInterval
;
2035 * Poll for remote printers using SLP...
2040 SLPFindSrvs(BrowseSLPHandle
, SLP_CUPS_SRVTYPE
, "", "",
2041 slp_url_callback
, &s
);
2044 * Loop through the list of available printers...
2050 * Save the "next" pointer...
2056 * Load a cupsd_printer_t structure with the SLP service attributes...
2059 SLPFindAttrs(BrowseSLPHandle
, s
->url
, "", "", slp_attr_callback
, &p
);
2062 * Process this printer entry...
2065 uri
= s
->url
+ SLP_CUPS_SRVLEN
+ 1;
2067 if (!strncmp(uri
, "http://", 7) || !strncmp(uri
, "ipp://", 6))
2070 * Pull the URI apart to see if this is a local or remote printer...
2073 if (!is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
2074 process_browse_data(uri
, host
, resource
, p
.type
, IPP_PRINTER_IDLE
,
2075 p
.location
, p
.info
, p
.make_model
, 0, NULL
);
2079 * Free this listing...
2082 cupsdClearString(&p
.info
);
2083 cupsdClearString(&p
.location
);
2084 cupsdClearString(&p
.make_model
);
2089 #endif /* HAVE_LIBSLP */
2093 * 'dequote()' - Remote quotes from a string.
2096 static char * /* O - Dequoted string */
2097 dequote(char *d
, /* I - Destination string */
2098 const char *s
, /* I - Source string */
2099 int dlen
) /* I - Destination length */
2101 char *dptr
; /* Pointer into destination */
2106 for (dptr
= d
, dlen
--; *s
&& dlen
> 0; s
++)
2124 * 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
2127 static char * /* O - TXT record */
2128 dnssdBuildTxtRecord(
2129 int *txt_len
, /* O - TXT record length */
2130 cupsd_printer_t
*p
, /* I - Printer information */
2131 int for_lpd
) /* I - 1 = LPD, 0 = IPP */
2133 int i
, j
; /* Looping vars */
2134 char type_str
[32], /* Type to string buffer */
2135 state_str
[32], /* State to string buffer */
2136 rp_str
[1024], /* Queue name string buffer */
2137 air_str
[1024], /* auth-info-required string buffer */
2138 *keyvalue
[32][2]; /* Table of key/value pairs */
2142 * Load up the key value pairs...
2147 keyvalue
[i
][0] = "txtvers";
2148 keyvalue
[i
++][1] = "1";
2150 keyvalue
[i
][0] = "qtotal";
2151 keyvalue
[i
++][1] = "1";
2153 keyvalue
[i
][0] = "rp";
2154 keyvalue
[i
++][1] = rp_str
;
2156 strlcpy(rp_str
, p
->name
, sizeof(rp_str
));
2158 snprintf(rp_str
, sizeof(rp_str
), "%s/%s",
2159 (p
->type
& CUPS_PRINTER_CLASS
) ? "classes" : "printers", p
->name
);
2161 keyvalue
[i
][0] = "ty";
2162 keyvalue
[i
++][1] = p
->make_model
;
2164 if (p
->location
&& *p
->location
!= '\0')
2166 keyvalue
[i
][0] = "note";
2167 keyvalue
[i
++][1] = p
->location
;
2170 keyvalue
[i
][0] = "priority";
2171 keyvalue
[i
++][1] = for_lpd
? "100" : "0";
2173 keyvalue
[i
][0] = "product";
2174 keyvalue
[i
++][1] = p
->product
? p
->product
: "Unknown";
2176 snprintf(type_str
, sizeof(type_str
), "0x%X", p
->type
| CUPS_PRINTER_REMOTE
);
2177 snprintf(state_str
, sizeof(state_str
), "%d", p
->state
);
2179 keyvalue
[i
][0] = "printer-state";
2180 keyvalue
[i
++][1] = state_str
;
2182 keyvalue
[i
][0] = "printer-type";
2183 keyvalue
[i
++][1] = type_str
;
2185 keyvalue
[i
][0] = "Transparent";
2186 keyvalue
[i
++][1] = "T";
2188 keyvalue
[i
][0] = "Binary";
2189 keyvalue
[i
++][1] = "T";
2191 if ((p
->type
& CUPS_PRINTER_FAX
))
2193 keyvalue
[i
][0] = "Fax";
2194 keyvalue
[i
++][1] = "T";
2197 if ((p
->type
& CUPS_PRINTER_COLOR
))
2199 keyvalue
[i
][0] = "Color";
2200 keyvalue
[i
++][1] = "T";
2203 if ((p
->type
& CUPS_PRINTER_DUPLEX
))
2205 keyvalue
[i
][0] = "Duplex";
2206 keyvalue
[i
++][1] = "T";
2209 if ((p
->type
& CUPS_PRINTER_STAPLE
))
2211 keyvalue
[i
][0] = "Staple";
2212 keyvalue
[i
++][1] = "T";
2215 if ((p
->type
& CUPS_PRINTER_COPIES
))
2217 keyvalue
[i
][0] = "Copies";
2218 keyvalue
[i
++][1] = "T";
2221 if ((p
->type
& CUPS_PRINTER_COLLATE
))
2223 keyvalue
[i
][0] = "Collate";
2224 keyvalue
[i
++][1] = "T";
2227 if ((p
->type
& CUPS_PRINTER_PUNCH
))
2229 keyvalue
[i
][0] = "Punch";
2230 keyvalue
[i
++][1] = "T";
2233 if ((p
->type
& CUPS_PRINTER_BIND
))
2235 keyvalue
[i
][0] = "Bind";
2236 keyvalue
[i
++][1] = "T";
2239 if ((p
->type
& CUPS_PRINTER_SORT
))
2241 keyvalue
[i
][0] = "Sort";
2242 keyvalue
[i
++][1] = "T";
2245 keyvalue
[i
][0] = "pdl";
2246 keyvalue
[i
++][1] = p
->pdl
? p
->pdl
: "application/postscript";
2248 if (p
->num_auth_info_required
)
2250 char *air
= air_str
; /* Pointer into string */
2253 for (j
= 0; j
< p
->num_auth_info_required
; j
++)
2255 if (air
>= (air_str
+ sizeof(air_str
) - 2))
2261 strlcpy(air
, p
->auth_info_required
[j
], sizeof(air_str
) - (air
- air_str
));
2265 keyvalue
[i
][0] = "air";
2266 keyvalue
[i
++][1] = air
;
2270 * Then pack them into a proper txt record...
2273 return (dnssdPackTxtRecord(txt_len
, keyvalue
, i
));
2278 * 'dnssdComparePrinters()' - Compare the registered names of two printers.
2281 static int /* O - Result of comparison */
2282 dnssdComparePrinters(cupsd_printer_t
*a
,/* I - First printer */
2283 cupsd_printer_t
*b
)/* I - Second printer */
2285 return (strcasecmp(a
->reg_name
, b
->reg_name
));
2290 * 'dnssdDeregisterPrinter()' - Stop sending broadcast information for a
2295 dnssdDeregisterPrinter(
2296 cupsd_printer_t
*p
) /* I - Printer */
2298 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdDeregisterPrinter(%s)", p
->name
);
2301 * Closing the socket deregisters the service
2306 DNSServiceRefDeallocate(p
->ipp_ref
);
2310 cupsArrayRemove(DNSSDPrinters
, p
);
2311 cupsdClearString(&p
->reg_name
);
2316 * p->ipp_txt is malloc'd, not _cupsStrAlloc'd...
2326 * 'dnssdPackTxtRecord()' - Pack an array of key/value pairs into the
2327 * TXT record format.
2330 static char * /* O - TXT record */
2331 dnssdPackTxtRecord(int *txt_len
, /* O - TXT record length */
2332 char *keyvalue
[][2], /* I - Table of key value pairs */
2333 int count
) /* I - Items in table */
2335 int i
; /* Looping var */
2336 int length
; /* Length of TXT record */
2337 int length2
; /* Length of value */
2338 char *txtRecord
; /* TXT record buffer */
2339 char *cursor
; /* Looping pointer */
2343 * Calculate the buffer size
2346 for (length
= i
= 0; i
< count
; i
++)
2347 length
+= 1 + strlen(keyvalue
[i
][0]) +
2348 (keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0);
2351 * Allocate and fill it
2354 txtRecord
= malloc(length
);
2359 for (cursor
= txtRecord
, i
= 0; i
< count
; i
++)
2362 * Drop in the p-string style length byte followed by the data
2365 length
= strlen(keyvalue
[i
][0]);
2366 length2
= keyvalue
[i
][1] ? 1 + strlen(keyvalue
[i
][1]) : 0;
2368 *cursor
++ = (unsigned char)(length
+ length2
);
2370 memcpy(cursor
, keyvalue
[i
][0], length
);
2377 memcpy(cursor
, keyvalue
[i
][1], length2
);
2388 * 'dnssdRegisterCallback()' - DNSServiceRegister callback.
2392 dnssdRegisterCallback(
2393 DNSServiceRef sdRef
, /* I - DNS Service reference */
2394 DNSServiceFlags flags
, /* I - Reserved for future use */
2395 DNSServiceErrorType errorCode
, /* I - Error code */
2396 const char *name
, /* I - Service name */
2397 const char *regtype
, /* I - Service type */
2398 const char *domain
, /* I - Domain. ".local" for now */
2399 void *context
) /* I - User-defined context */
2401 cupsd_printer_t
*p
= (cupsd_printer_t
*)context
;
2402 /* Current printer */
2405 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterCallback(%s, %s) for %s",
2406 name
, regtype
, p
? p
->name
: "Web Interface");
2410 cupsdLogMessage(CUPSD_LOG_ERROR
,
2411 "DNSServiceRegister failed with error %d", (int)errorCode
);
2414 else if (p
&& strcasecmp(name
, p
->reg_name
))
2416 cupsdLogMessage(CUPSD_LOG_INFO
, "Using service name \"%s\" for \"%s\"",
2419 cupsArrayRemove(DNSSDPrinters
, p
);
2420 cupsdSetString(&p
->reg_name
, name
);
2421 cupsArrayAdd(DNSSDPrinters
, p
);
2423 LastEvent
|= CUPSD_EVENT_PRINTER_MODIFIED
;
2429 * 'dnssdRegisterPrinter()' - Start sending broadcast information for a printer
2430 * or update the broadcast contents.
2434 dnssdRegisterPrinter(cupsd_printer_t
*p
)/* I - Printer */
2436 DNSServiceErrorType se
; /* dnssd errors */
2437 char *ipp_txt
, /* IPP TXT record buffer */
2438 *printer_txt
, /* LPD TXT record buffer */
2439 name
[1024], /* Service name */
2440 *nameptr
; /* Pointer into name */
2441 int ipp_len
, /* IPP TXT record length */
2442 printer_len
; /* LPD TXT record length */
2443 char resource
[1024]; /* Resource path for printer */
2444 const char *regtype
; /* Registration type */
2445 const char *domain
; /* Registration domain */
2446 cupsd_location_t
*location
, /* Printer location */
2447 *policy
; /* Operation policy for Print-Job */
2448 unsigned address
[4]; /* INADDR_ANY address */
2451 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "dnssdRegisterPrinter(%s) %s", p
->name
,
2452 !p
->ipp_ref
? "new" : "update");
2455 * If per-printer sharing was just disabled make sure we're not
2456 * registered before returning.
2461 dnssdDeregisterPrinter(p
);
2466 * The registered name takes the form of "<printer-info> @ <computer name>"...
2469 if (p
->info
&& strlen(p
->info
) > 0)
2472 snprintf(name
, sizeof(name
), "%s @ %s", p
->info
, DNSSDName
);
2474 strlcpy(name
, p
->info
, sizeof(name
));
2477 snprintf(name
, sizeof(name
), "%s @ %s", p
->name
, DNSSDName
);
2479 strlcpy(name
, p
->name
, sizeof(name
));
2482 * If an existing printer was renamed, unregister it and start over...
2485 if (p
->reg_name
&& strcmp(p
->reg_name
, name
))
2486 dnssdDeregisterPrinter(p
);
2490 cupsdSetString(&p
->reg_name
, name
);
2491 cupsArrayAdd(DNSSDPrinters
, p
);
2495 * If 'Allow printing from the Internet' is enabled (i.e. from any address)
2496 * let dnssd decide on the domain, otherwise restrict it to ".local".
2499 if (p
->type
& CUPS_PRINTER_CLASS
)
2500 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2502 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2504 address
[0] = address
[1] = address
[2] = address
[3] = 0;
2505 location
= cupsdFindBest(resource
, HTTP_POST
);
2506 policy
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2508 if ((location
&& !cupsdCheckAccess(address
, "", 0, location
)) ||
2509 (policy
&& !cupsdCheckAccess(address
, "", 0, policy
)))
2515 * Register IPP and (optionally) LPD...
2518 ipp_len
= 0; /* anti-compiler-warning-code */
2519 ipp_txt
= dnssdBuildTxtRecord(&ipp_len
, p
, 0);
2524 * Initial registration. Use the _fax subtype for fax queues...
2527 regtype
= (p
->type
& CUPS_PRINTER_FAX
) ? "_fax-ipp._tcp" :
2530 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2531 "Registering DNS-SD printer %s with name \"%s\", "
2532 "type \"%s\", and domain \"%s\"", p
->name
, name
, regtype
,
2533 domain
? domain
: "(null)");
2536 * Register the queue, dropping characters as needed until we succeed...
2539 nameptr
= name
+ strlen(name
);
2543 p
->ipp_ref
= DNSSDRef
;
2544 if ((se
= DNSServiceRegister(&p
->ipp_ref
, kDNSServiceFlagsShareConnection
,
2545 0, name
, regtype
, domain
, NULL
,
2546 htons(DNSSDPort
), ipp_len
, ipp_txt
,
2547 dnssdRegisterCallback
,
2548 p
)) == kDNSServiceErr_BadParam
)
2551 * Name is too long, drop trailing characters, taking into account
2557 while (nameptr
> name
&& (*nameptr
& 0xc0) == 0x80)
2564 while (se
== kDNSServiceErr_BadParam
&& nameptr
> name
);
2566 if (se
== kDNSServiceErr_NoError
)
2568 p
->ipp_txt
= ipp_txt
;
2569 p
->ipp_len
= ipp_len
;
2573 cupsdLogMessage(CUPSD_LOG_WARN
,
2574 "DNS-SD IPP registration of \"%s\" failed: %d",
2577 else if (ipp_len
!= p
->ipp_len
|| memcmp(ipp_txt
, p
->ipp_txt
, ipp_len
))
2580 * Update the existing registration...
2583 /* A TTL of 0 means use record's original value (Radar 3176248) */
2584 DNSServiceUpdateRecord(p
->ipp_ref
, NULL
, 0, ipp_len
, ipp_txt
, 0);
2589 p
->ipp_txt
= ipp_txt
;
2590 p
->ipp_len
= ipp_len
;
2597 if (BrowseLocalProtocols
& BROWSE_LPD
)
2599 printer_len
= 0; /* anti-compiler-warning-code */
2600 printer_txt
= dnssdBuildTxtRecord(&printer_len
, p
, 1);
2602 if (!p
->printer_ref
)
2605 * Initial registration...
2608 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2609 "Registering DNS-SD printer %s with name \"%s\", "
2610 "type \"_printer._tcp\", and domain \"%s\"", p
->name
,
2611 name
, domain
? domain
: "(null)");
2613 p
->printer_ref
= DNSSDRef
;
2614 if ((se
= DNSServiceRegister(&p
->printer_ref
,
2615 kDNSServiceFlagsShareConnection
,
2616 0, name
, "_printer._tcp", domain
, NULL
,
2617 htons(515), printer_len
, printer_txt
,
2618 dnssdRegisterCallback
,
2619 p
)) == kDNSServiceErr_NoError
)
2621 p
->printer_txt
= printer_txt
;
2622 p
->printer_len
= printer_len
;
2626 cupsdLogMessage(CUPSD_LOG_WARN
,
2627 "DNS-SD LPD registration of \"%s\" failed: %d",
2630 else if (printer_len
!= p
->printer_len
||
2631 memcmp(printer_txt
, p
->printer_txt
, printer_len
))
2634 * Update the existing registration...
2637 /* A TTL of 0 means use record's original value (Radar 3176248) */
2638 DNSServiceUpdateRecord(p
->printer_ref
, NULL
, 0, printer_len
,
2642 free(p
->printer_txt
);
2644 p
->printer_txt
= printer_txt
;
2645 p
->printer_len
= printer_len
;
2656 * 'dnssdUpdate()' - Handle DNS-SD queries.
2662 DNSServiceErrorType sdErr
; /* Service discovery error */
2665 if ((sdErr
= DNSServiceProcessResult(DNSSDRef
)) != kDNSServiceErr_NoError
)
2666 cupsdLogMessage(CUPSD_LOG_ERROR
,
2667 "DNS Service Discovery registration error %d!",
2670 #endif /* HAVE_DNSSD */
2675 * 'get_hostconfig()' - Get an /etc/hostconfig service setting.
2678 static int /* O - 1 for YES or AUTOMATIC, 0 for NO */
2679 get_hostconfig(const char *name
) /* I - Name of service */
2681 cups_file_t
*fp
; /* Hostconfig file */
2682 char line
[1024], /* Line from file */
2683 *ptr
; /* Pointer to value */
2684 int state
= 1; /* State of service */
2688 * Try opening the /etc/hostconfig file; if we can't open it, assume that
2689 * the service is enabled/auto.
2692 if ((fp
= cupsFileOpen("/etc/hostconfig", "r")) != NULL
)
2695 * Read lines from the file until we find the service...
2698 while (cupsFileGets(fp
, line
, sizeof(line
)))
2700 if (line
[0] == '#' || (ptr
= strchr(line
, '=')) == NULL
)
2705 if (!strcasecmp(line
, name
))
2708 * Found the service, see if it is set to "-NO-"...
2711 if (!strncasecmp(ptr
, "-NO-", 4))
2722 #endif /* __APPLE__ */
2726 * 'is_local_queue()' - Determine whether the URI points at a local queue.
2729 static int /* O - 1 = local, 0 = remote, -1 = bad URI */
2730 is_local_queue(const char *uri
, /* I - Printer URI */
2731 char *host
, /* O - Host string */
2732 int hostlen
, /* I - Length of host buffer */
2733 char *resource
, /* O - Resource string */
2734 int resourcelen
) /* I - Length of resource buffer */
2736 char scheme
[32], /* Scheme portion of URI */
2737 username
[HTTP_MAX_URI
]; /* Username portion of URI */
2738 int port
; /* Port portion of URI */
2739 cupsd_netif_t
*iface
; /* Network interface */
2743 * Pull the URI apart to see if this is a local or remote printer...
2746 if (httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
2747 username
, sizeof(username
), host
, hostlen
, &port
,
2748 resource
, resourcelen
) < HTTP_URI_OK
)
2751 DEBUG_printf(("host=\"%s\", ServerName=\"%s\"\n", host
, ServerName
));
2754 * Check for local server addresses...
2757 if (!strcasecmp(host
, ServerName
) && port
== LocalPort
)
2762 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
2764 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
2765 if (!strcasecmp(host
, iface
->hostname
) && port
== iface
->port
)
2769 * If we get here, the printer is remote...
2777 * 'process_browse_data()' - Process new browse data.
2781 process_browse_data(
2782 const char *uri
, /* I - URI of printer/class */
2783 const char *host
, /* I - Hostname */
2784 const char *resource
, /* I - Resource path */
2785 cups_ptype_t type
, /* I - Printer type */
2786 ipp_pstate_t state
, /* I - Printer state */
2787 const char *location
, /* I - Printer location */
2788 const char *info
, /* I - Printer information */
2789 const char *make_model
, /* I - Printer make and model */
2790 int num_attrs
, /* I - Number of attributes */
2791 cups_option_t
*attrs
) /* I - Attributes */
2793 int i
; /* Looping var */
2794 int update
; /* Update printer attributes? */
2795 char finaluri
[HTTP_MAX_URI
], /* Final URI for printer */
2796 name
[IPP_MAX_NAME
], /* Name of printer */
2797 newname
[IPP_MAX_NAME
], /* New name of printer */
2798 *hptr
, /* Pointer into hostname */
2799 *sptr
; /* Pointer into ServerName */
2800 const char *shortname
; /* Short queue name (queue) */
2801 char local_make_model
[IPP_MAX_NAME
];
2802 /* Local make and model */
2803 cupsd_printer_t
*p
; /* Printer information */
2804 const char *ipp_options
, /* ipp-options value */
2805 *lease_duration
; /* lease-duration value */
2806 int is_class
; /* Is this queue a class? */
2809 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2810 "process_browse_data(uri=\"%s\", host=\"%s\", "
2811 "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
2812 "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
2813 uri
, host
, resource
, type
, state
,
2814 location
? location
: "(nil)", info
? info
: "(nil)",
2815 make_model
? make_model
: "(nil)", num_attrs
, attrs
);
2818 * Determine if the URI contains any illegal characters in it...
2821 if (strncmp(uri
, "ipp://", 6) || !host
[0] ||
2822 (strncmp(resource
, "/printers/", 10) &&
2823 strncmp(resource
, "/classes/", 9)))
2825 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad printer URI in browse data: %s", uri
);
2829 if (strchr(resource
, '?') ||
2830 (!strncmp(resource
, "/printers/", 10) && strchr(resource
+ 10, '/')) ||
2831 (!strncmp(resource
, "/classes/", 9) && strchr(resource
+ 9, '/')))
2833 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad resource in browse data: %s",
2839 * OK, this isn't a local printer; add any remote options...
2842 ipp_options
= cupsGetOption("ipp-options", num_attrs
, attrs
);
2844 if (BrowseRemoteOptions
)
2846 if (BrowseRemoteOptions
[0] == '?')
2849 * Override server-supplied options...
2852 snprintf(finaluri
, sizeof(finaluri
), "%s%s", uri
, BrowseRemoteOptions
);
2854 else if (ipp_options
)
2857 * Combine the server and local options...
2860 snprintf(finaluri
, sizeof(finaluri
), "%s?%s+%s", uri
, ipp_options
,
2861 BrowseRemoteOptions
);
2866 * Just use the local options...
2869 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, BrowseRemoteOptions
);
2874 else if (ipp_options
)
2877 * Just use the server-supplied options...
2880 snprintf(finaluri
, sizeof(finaluri
), "%s?%s", uri
, ipp_options
);
2885 * See if we already have it listed in the Printers list, and add it if not...
2888 type
|= CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
;
2889 type
&= ~CUPS_PRINTER_IMPLICIT
;
2891 hptr
= strchr(host
, '.');
2892 sptr
= strchr(ServerName
, '.');
2893 is_class
= type
& CUPS_PRINTER_CLASS
;
2895 if (!ServerNameIsIP
&& sptr
!= NULL
&& hptr
!= NULL
)
2898 * Strip the common domain name components...
2901 while (hptr
!= NULL
)
2903 if (!strcasecmp(hptr
, sptr
))
2909 hptr
= strchr(hptr
+ 1, '.');
2916 * Remote destination is a class...
2919 if (!strncmp(resource
, "/classes/", 9))
2920 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 9, host
);
2924 shortname
= resource
+ 9;
2929 * Remote destination is a printer...
2932 if (!strncmp(resource
, "/printers/", 10))
2933 snprintf(name
, sizeof(name
), "%s@%s", resource
+ 10, host
);
2937 shortname
= resource
+ 10;
2941 *hptr
= '.'; /* Resource FQDN */
2943 if ((p
= cupsdFindDest(name
)) == NULL
&& BrowseShortNames
)
2946 * Long name doesn't exist, try short name...
2949 cupsdLogMessage(CUPSD_LOG_DEBUG
, "process_browse_data: %s not found...",
2952 if ((p
= cupsdFindDest(shortname
)) == NULL
)
2955 * Short name doesn't exist, use it for this shared queue.
2958 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "process_browse_data: %s not found...",
2960 strlcpy(name
, shortname
, sizeof(name
));
2965 * Short name exists...
2968 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2969 "process_browse_data: %s found, type=%x, hostname=%s...",
2970 shortname
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
2972 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2973 p
= NULL
; /* Don't replace implicit classes */
2974 else if (p
->hostname
&& strcasecmp(p
->hostname
, host
))
2977 * Short name exists but is for a different host. If this is a remote
2978 * queue, rename it and use the long name...
2981 if (p
->type
& CUPS_PRINTER_REMOTE
)
2983 cupsdLogMessage(CUPSD_LOG_DEBUG
,
2984 "Renamed remote %s \"%s\" to \"%s@%s\"...",
2985 is_class
? "class" : "printer", p
->name
, p
->name
,
2987 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
2988 "%s \'%s\' deleted by directory services.",
2989 is_class
? "Class" : "Printer", p
->name
);
2991 snprintf(newname
, sizeof(newname
), "%s@%s", p
->name
, p
->hostname
);
2992 cupsdRenamePrinter(p
, newname
);
2994 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
2995 "%s \'%s\' added by directory services.",
2996 is_class
? "Class" : "Printer", p
->name
);
3000 * Force creation with long name...
3008 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3009 "process_browse_data: %s found, type=%x, hostname=%s...",
3010 name
, p
->type
, p
->hostname
? p
->hostname
: "(nil)");
3015 * Queue doesn't exist; add it...
3019 p
= cupsdAddClass(name
);
3021 p
= cupsdAddPrinter(name
);
3023 cupsdClearString(&(p
->hostname
));
3025 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added remote %s \"%s\"...",
3026 is_class
? "class" : "printer", name
);
3028 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3029 "%s \'%s\' added by directory services.",
3030 is_class
? "Class" : "Printer", name
);
3033 * Force the URI to point to the real server...
3036 p
->type
= type
& ~CUPS_PRINTER_REJECTING
;
3039 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
3048 * Hostname not set, so this must be a cached remote printer
3049 * that was created for a pending print job...
3052 cupsdSetString(&p
->hostname
, host
);
3053 cupsdSetString(&p
->uri
, uri
);
3054 cupsdSetString(&p
->device_uri
, uri
);
3057 cupsdMarkDirty(CUPSD_DIRTY_REMOTE
);
3061 * Update the state...
3065 p
->browse_time
= time(NULL
);
3067 if ((lease_duration
= cupsGetOption("lease-duration", num_attrs
,
3071 * Grab the lease-duration for the browse data; anything less then 1
3072 * second or more than 1 week gets the default BrowseTimeout...
3075 i
= atoi(lease_duration
);
3076 if (i
< 1 || i
> 604800)
3079 p
->browse_expire
= p
->browse_time
+ i
;
3082 p
->browse_expire
= p
->browse_time
+ BrowseTimeout
;
3084 if (type
& CUPS_PRINTER_REJECTING
)
3086 type
&= ~CUPS_PRINTER_REJECTING
;
3094 else if (!p
->accepting
)
3100 if (p
->type
!= type
)
3106 if (location
&& (!p
->location
|| strcmp(p
->location
, location
)))
3108 cupsdSetString(&p
->location
, location
);
3112 if (info
&& (!p
->info
|| strcmp(p
->info
, info
)))
3114 cupsdSetString(&p
->info
, info
);
3117 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3120 if (!make_model
|| !make_model
[0])
3123 snprintf(local_make_model
, sizeof(local_make_model
),
3124 "Remote Class on %s", host
);
3126 snprintf(local_make_model
, sizeof(local_make_model
),
3127 "Remote Printer on %s", host
);
3130 snprintf(local_make_model
, sizeof(local_make_model
),
3131 "%s on %s", make_model
, host
);
3133 if (!p
->make_model
|| strcmp(p
->make_model
, local_make_model
))
3135 cupsdSetString(&p
->make_model
, local_make_model
);
3141 if (!update
&& !(type
& CUPS_PRINTER_DELETE
))
3144 * See if we need to update the attributes...
3147 if (p
->num_options
!= num_attrs
)
3151 for (i
= 0; i
< num_attrs
; i
++)
3152 if (strcmp(attrs
[i
].name
, p
->options
[i
].name
) ||
3153 (!attrs
[i
].value
!= !p
->options
[i
].value
) ||
3154 (attrs
[i
].value
&& strcmp(attrs
[i
].value
, p
->options
[i
].value
)))
3163 * Free the old options...
3166 cupsFreeOptions(p
->num_options
, p
->options
);
3169 p
->num_options
= num_attrs
;
3172 if (type
& CUPS_PRINTER_DELETE
)
3174 cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED
, p
, NULL
,
3175 "%s \'%s\' deleted by directory services.",
3176 is_class
? "Class" : "Printer", p
->name
);
3178 cupsdExpireSubscriptions(p
, NULL
);
3180 cupsdDeletePrinter(p
, 1);
3181 cupsdUpdateImplicitClasses();
3182 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3186 cupsdSetPrinterAttrs(p
);
3187 cupsdUpdateImplicitClasses();
3191 * See if we have a default printer... If not, make the first network
3192 * default printer the default.
3195 if (DefaultPrinter
== NULL
&& Printers
!= NULL
&& UseNetworkDefault
)
3198 * Find the first network default printer and use it...
3201 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3203 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3204 if (p
->type
& CUPS_PRINTER_DEFAULT
)
3207 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3213 * Do auto-classing if needed...
3216 process_implicit_classes();
3221 * 'process_implicit_classes()' - Create/update implicit classes as needed.
3225 process_implicit_classes(void)
3227 int i
; /* Looping var */
3228 int update
; /* Update printer attributes? */
3229 char name
[IPP_MAX_NAME
], /* Name of printer */
3230 *hptr
; /* Pointer into hostname */
3231 cupsd_printer_t
*p
, /* Printer information */
3232 *pclass
, /* Printer class */
3233 *first
; /* First printer in class */
3234 int offset
, /* Offset of name */
3235 len
; /* Length of name */
3238 if (!ImplicitClasses
|| !Printers
)
3242 * Loop through all available printers and create classes as needed...
3245 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
), len
= 0, offset
= 0,
3246 update
= 0, pclass
= NULL
, first
= NULL
;
3248 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3251 * Skip implicit classes...
3254 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3261 * If len == 0, get the length of this printer name up to the "@"
3265 cupsArraySave(Printers
);
3268 !strncasecmp(p
->name
, name
+ offset
, len
) &&
3269 (p
->name
[len
] == '\0' || p
->name
[len
] == '@'))
3272 * We have more than one printer with the same name; see if
3273 * we have a class, and if this printer is a member...
3276 if (pclass
&& strcasecmp(pclass
->name
, name
))
3279 cupsdSetPrinterAttrs(pclass
);
3285 if (!pclass
&& (pclass
= cupsdFindDest(name
)) == NULL
)
3288 * Need to add the class...
3291 pclass
= cupsdAddPrinter(name
);
3292 cupsArrayAdd(ImplicitPrinters
, pclass
);
3294 pclass
->type
|= CUPS_PRINTER_IMPLICIT
;
3295 pclass
->accepting
= 1;
3296 pclass
->state
= IPP_PRINTER_IDLE
;
3298 cupsdSetString(&pclass
->location
, p
->location
);
3299 cupsdSetString(&pclass
->info
, p
->info
);
3301 cupsdSetString(&pclass
->job_sheets
[0], p
->job_sheets
[0]);
3302 cupsdSetString(&pclass
->job_sheets
[1], p
->job_sheets
[1]);
3306 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
| CUPSD_DIRTY_REMOTE
);
3308 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Added implicit class \"%s\"...",
3310 cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED
, p
, NULL
,
3311 "Implicit class \'%s\' added by directory services.",
3317 for (i
= 0; i
< pclass
->num_printers
; i
++)
3318 if (pclass
->printers
[i
] == first
)
3321 if (i
>= pclass
->num_printers
)
3323 first
->in_implicit_class
= 1;
3324 cupsdAddPrinterToClass(pclass
, first
);
3330 for (i
= 0; i
< pclass
->num_printers
; i
++)
3331 if (pclass
->printers
[i
] == p
)
3334 if (i
>= pclass
->num_printers
)
3336 p
->in_implicit_class
= 1;
3337 cupsdAddPrinterToClass(pclass
, p
);
3344 * First time around; just get name length and mark it as first
3348 if ((hptr
= strchr(p
->name
, '@')) != NULL
)
3349 len
= hptr
- p
->name
;
3351 len
= strlen(p
->name
);
3353 strncpy(name
, p
->name
, len
);
3357 if ((first
= (hptr
? cupsdFindDest(name
) : p
)) != NULL
&&
3358 !(first
->type
& CUPS_PRINTER_IMPLICIT
))
3361 * Can't use same name as a local printer; add "Any" to the
3362 * front of the name, unless we have explicitly disabled
3363 * the "ImplicitAnyClasses"...
3366 if (ImplicitAnyClasses
&& len
< (sizeof(name
) - 4))
3369 * Add "Any" to the class name...
3372 strcpy(name
, "Any");
3373 strncpy(name
+ 3, p
->name
, len
);
3374 name
[len
+ 3] = '\0';
3380 * Don't create an implicit class if we have a local printer
3381 * with the same name...
3385 cupsArrayRestore(Printers
);
3393 cupsArrayRestore(Printers
);
3397 * Update the last printer class as needed...
3400 if (pclass
&& update
)
3401 cupsdSetPrinterAttrs(pclass
);
3406 * 'send_cups_browse()' - Send new browsing information using the CUPS
3411 send_cups_browse(cupsd_printer_t
*p
) /* I - Printer to send */
3413 int i
; /* Looping var */
3414 cups_ptype_t type
; /* Printer type */
3415 cupsd_dirsvc_addr_t
*b
; /* Browse address */
3416 int bytes
; /* Length of packet */
3417 char packet
[1453], /* Browse data packet */
3418 uri
[1024], /* Printer URI */
3419 location
[1024], /* printer-location */
3420 info
[1024], /* printer-info */
3422 /* printer-make-and-model */
3423 cupsd_netif_t
*iface
; /* Network interface */
3427 * Figure out the printer type value...
3430 type
= p
->type
| CUPS_PRINTER_REMOTE
;
3433 type
|= CUPS_PRINTER_REJECTING
;
3435 if (p
== DefaultPrinter
)
3436 type
|= CUPS_PRINTER_DEFAULT
;
3439 * Remove quotes from printer-info, printer-location, and
3440 * printer-make-and-model attributes...
3443 dequote(location
, p
->location
, sizeof(location
));
3444 dequote(info
, p
->info
, sizeof(info
));
3447 dequote(make_model
, p
->make_model
, sizeof(make_model
));
3448 else if (p
->type
& CUPS_PRINTER_CLASS
)
3450 if (p
->num_printers
> 0 && p
->printers
[0]->make_model
)
3451 strlcpy(make_model
, p
->printers
[0]->make_model
, sizeof(make_model
));
3453 strlcpy(make_model
, "Local Printer Class", sizeof(make_model
));
3456 strlcpy(make_model
, "Local Raw Printer", sizeof(make_model
));
3458 strlcpy(make_model
, "Local System V Printer", sizeof(make_model
));
3461 * Send a packet to each browse address...
3464 for (i
= NumBrowsers
, b
= Browsers
; i
> 0; i
--, b
++)
3468 * Send the browse packet to one or more interfaces...
3471 if (!strcmp(b
->iface
, "*"))
3474 * Send to all local interfaces...
3479 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
3481 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
3484 * Only send to local, IPv4 interfaces...
3487 if (!iface
->is_local
|| !iface
->port
||
3488 iface
->address
.addr
.sa_family
!= AF_INET
)
3491 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3492 iface
->hostname
, iface
->port
,
3493 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3496 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3497 type
, p
->state
, uri
, location
, info
, make_model
,
3498 p
->browse_attrs
? p
->browse_attrs
: "");
3500 bytes
= strlen(packet
);
3502 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3503 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3504 iface
->name
, packet
);
3506 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3508 sendto(BrowseSocket
, packet
, bytes
, 0,
3509 (struct sockaddr
*)&(iface
->broadcast
),
3510 httpAddrLength(&(iface
->broadcast
)));
3513 else if ((iface
= cupsdNetIFFind(b
->iface
)) != NULL
)
3516 * Send to the named interface using the IPv4 address...
3520 if (strcmp(b
->iface
, iface
->name
))
3525 else if (iface
->address
.addr
.sa_family
== AF_INET
&& iface
->port
)
3528 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
);
3532 httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
,
3533 iface
->hostname
, iface
->port
,
3534 (p
->type
& CUPS_PRINTER_CLASS
) ? "/classes/%s" :
3537 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3538 type
, p
->state
, uri
, location
, info
, make_model
,
3539 p
->browse_attrs
? p
->browse_attrs
: "");
3541 bytes
= strlen(packet
);
3543 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3544 "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes
,
3545 iface
->name
, packet
);
3547 iface
->broadcast
.ipv4
.sin_port
= htons(BrowsePort
);
3549 sendto(BrowseSocket
, packet
, bytes
, 0,
3550 (struct sockaddr
*)&(iface
->broadcast
),
3551 httpAddrLength(&(iface
->broadcast
)));
3558 * Send the browse packet to the indicated address using
3559 * the default server name...
3562 snprintf(packet
, sizeof(packet
), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
3563 type
, p
->state
, p
->uri
, location
, info
, make_model
,
3564 p
->browse_attrs
? p
->browse_attrs
: "");
3566 bytes
= strlen(packet
);
3567 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3568 "cupsdSendBrowseList: (%d bytes) %s", bytes
, packet
);
3570 if (sendto(BrowseSocket
, packet
, bytes
, 0,
3571 (struct sockaddr
*)&(b
->to
),
3572 httpAddrLength(&(b
->to
))) <= 0)
3575 * Unable to send browse packet, so remove this address from the
3579 cupsdLogMessage(CUPSD_LOG_ERROR
,
3580 "cupsdSendBrowseList: sendto failed for browser "
3582 (int)(b
- Browsers
+ 1), strerror(errno
));
3585 memmove(b
, b
+ 1, (i
- 1) * sizeof(cupsd_dirsvc_addr_t
));
3596 * 'ldap_search_rec()' - LDAP Search with reconnect
3600 ldap_search_rec(LDAP
*ld
, /* I - LDAP handler */
3601 char *base
, /* I - Base dn */
3602 int scope
, /* I - LDAP search scope */
3603 char *filter
, /* I - Filter string */
3604 char *attrs
[], /* I - Requested attributes */
3605 int attrsonly
, /* I - Return only attributes? */
3606 LDAPMessage
**res
) /* I - LDAP handler */
3608 int rc
; /* Return code */
3611 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3612 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
, NULL
,
3613 NULL
, LDAP_NO_LIMIT
, res
);
3615 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3616 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3619 * If we have a connection problem try again...
3622 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
3624 cupsdLogMessage(CUPSD_LOG_ERROR
,
3625 "LDAP search failed with status %d: %s",
3626 rc
, ldap_err2string(rc
));
3627 cupsdLogMessage(CUPSD_LOG_INFO
,
3628 "We try the LDAP search once again after reconnecting to "
3633 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3634 rc
= ldap_search_ext_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, NULL
,
3635 NULL
, NULL
, LDAP_NO_LIMIT
, res
);
3637 rc
= ldap_search_s(ld
, base
, scope
, filter
, attrs
, attrsonly
, res
);
3638 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3641 if (rc
== LDAP_NO_SUCH_OBJECT
)
3642 cupsdLogMessage(CUPSD_LOG_DEBUG
,
3643 "ldap_search_rec: LDAP entry/object not found");
3644 else if (rc
!= LDAP_SUCCESS
)
3645 cupsdLogMessage(CUPSD_LOG_ERROR
,
3646 "ldap_search_rec: LDAP search failed with status %d: %s",
3647 rc
, ldap_err2string(rc
));
3649 if (rc
!= LDAP_SUCCESS
)
3657 * 'ldap_freeres()' - Free LDAPMessage
3661 ldap_freeres(LDAPMessage
*entry
) /* I - LDAP handler */
3663 int rc
; /* Return value */
3666 rc
= ldap_msgfree(entry
);
3668 cupsdLogMessage(CUPSD_LOG_WARN
,
3669 "Can't free LDAPMessage!");
3671 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3672 "Freeing LDAPMessage was unnecessary");
3677 * 'ldap_getval_char()' - Get first LDAP value and convert to string
3681 ldap_getval_firststring(
3682 LDAP
*ld
, /* I - LDAP handler */
3683 LDAPMessage
*entry
, /* I - LDAP message or search result */
3684 char *attr
, /* I - the wanted attribute */
3685 char *retval
, /* O - String to return */
3686 unsigned long maxsize
) /* I - Max string size */
3688 char *dn
; /* LDAP DN */
3689 int rc
= 0; /* Return code */
3690 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3691 struct berval
**bval
; /* LDAP value array */
3692 unsigned long size
; /* String size */
3696 * Get value from LDAPMessage...
3699 if ((bval
= ldap_get_values_len(ld
, entry
, attr
)) == NULL
)
3702 dn
= ldap_get_dn(ld
, entry
);
3703 cupsdLogMessage(CUPSD_LOG_WARN
,
3704 "Failed to get LDAP value %s for %s!",
3712 * Check size and copy value into our string...
3716 if (size
< bval
[0]->bv_len
)
3719 dn
= ldap_get_dn(ld
, entry
);
3720 cupsdLogMessage(CUPSD_LOG_WARN
,
3721 "Attribute %s is too big! (dn: %s)",
3726 size
= bval
[0]->bv_len
;
3728 strlcpy(retval
, bval
[0]->bv_val
, size
);
3729 ldap_value_free_len(bval
);
3732 char **value
; /* LDAP value */
3735 * Get value from LDAPMessage...
3738 if ((value
= (char **)ldap_get_values(ld
, entry
, attr
)) == NULL
)
3741 dn
= ldap_get_dn(ld
, entry
);
3742 cupsdLogMessage(CUPSD_LOG_WARN
,
3743 "Failed to get LDAP value %s for %s!",
3749 strlcpy(retval
, *value
, maxsize
);
3750 ldap_value_free(value
);
3752 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3759 * 'send_ldap_ou()' - Send LDAP ou registrations.
3763 send_ldap_ou(char *ou
, /* I - Servername/ou to register */
3764 char *basedn
, /* I - Our base dn */
3765 char *descstring
) /* I - Description for ou */
3767 int i
; /* Looping var... */
3768 LDAPMod mods
[3]; /* The 3 attributes we will be adding */
3769 LDAPMod
*pmods
[4]; /* Pointers to the 3 attributes + NULL */
3770 LDAPMessage
*res
, /* Search result token */
3771 *e
; /* Current entry from search */
3772 int rc
; /* LDAP status */
3773 char dn
[1024], /* DN of the organizational unit we are adding */
3774 *desc
[2], /* Change records */
3776 char old_desc
[1024]; /* Old description */
3777 static const char * const objectClass_values
[] =
3778 { /* The 2 objectClass's we use in */
3779 "top", /* our LDAP entries */
3780 "organizationalUnit",
3783 static const char * const ou_attrs
[] =/* CUPS LDAP attributes */
3790 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: %s", ou
);
3793 * Reconnect if LDAP Handle is invalid...
3796 if (! BrowseLDAPHandle
)
3798 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3799 "send_ldap_ou: LDAP Handle is invalid. Try "
3806 * Prepare ldap search...
3809 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
3810 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_ou: dn=\"%s\"", dn
);
3814 desc
[0] = descstring
;
3817 mods
[0].mod_type
= "ou";
3818 mods
[0].mod_values
= ou_value
;
3819 mods
[1].mod_type
= "description";
3820 mods
[1].mod_values
= desc
;
3821 mods
[2].mod_type
= "objectClass";
3822 mods
[2].mod_values
= (char **)objectClass_values
;
3824 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
3825 (char **)ou_attrs
, 0, &res
);
3828 * If ldap search was not successfull then exit function...
3831 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
3835 * Check if we need to insert or update the LDAP entry...
3838 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
3839 rc
!= LDAP_NO_SUCH_OBJECT
)
3842 * Printserver has already been registered, check if
3843 * modification is required...
3846 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
3849 * Get the required values from this entry...
3852 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "description", old_desc
,
3853 sizeof(old_desc
)) == -1)
3857 * Check if modification is required...
3860 if ( strcmp(desc
[0], old_desc
) == 0 )
3863 * LDAP entry for the printer exists.
3864 * Printer has already been registered,
3865 * no modifications required...
3867 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3868 "send_ldap_ou: No updates required for %s", ou
);
3873 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3874 "send_ldap_ou: Replace entry for %s", ou
);
3876 for (i
= 0; i
< 3; i
++)
3878 pmods
[i
] = mods
+ i
;
3879 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
3883 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3884 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3885 NULL
)) != LDAP_SUCCESS
)
3887 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3888 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3890 cupsdLogMessage(CUPSD_LOG_ERROR
,
3891 "LDAP modify for %s failed with status %d: %s",
3892 ou
, rc
, ldap_err2string(rc
));
3893 if ( LDAP_SERVER_DOWN
== rc
)
3901 * Printserver has never been registered,
3902 * add registration...
3905 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3906 "send_ldap_ou: Add entry for %s", ou
);
3908 for (i
= 0; i
< 3; i
++)
3910 pmods
[i
] = mods
+ i
;
3911 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
3915 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
3916 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
3917 NULL
)) != LDAP_SUCCESS
)
3919 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
3920 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
3922 cupsdLogMessage(CUPSD_LOG_ERROR
,
3923 "LDAP add for %s failed with status %d: %s",
3924 ou
, rc
, ldap_err2string(rc
));
3925 if ( LDAP_SERVER_DOWN
== rc
)
3935 * 'send_ldap_browse()' - Send LDAP printer registrations.
3939 send_ldap_browse(cupsd_printer_t
*p
) /* I - Printer to register */
3941 int i
; /* Looping var... */
3942 LDAPMod mods
[7]; /* The 7 attributes we will be adding */
3943 LDAPMod
*pmods
[8]; /* Pointers to the 7 attributes + NULL */
3944 LDAPMessage
*res
, /* Search result token */
3945 *e
; /* Current entry from search */
3946 char *cn_value
[2], /* Change records */
3952 typestring
[255], /* String to hold printer-type */
3953 dn
[1024]; /* DN of the printer we are adding */
3954 int rc
; /* LDAP status */
3955 char old_uri
[HTTP_MAX_URI
], /* Printer URI */
3956 old_location
[1024], /* Printer location */
3957 old_info
[1024], /* Printer information */
3958 old_make_model
[1024], /* Printer make and model */
3959 old_type_string
[30]; /* Temporary type number */
3960 int old_type
; /* Printer type */
3961 static const char * const objectClass_values
[] =
3962 { /* The 3 objectClass's we use in */
3963 "top", /* our LDAP entries */
3970 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: %s", p
->name
);
3973 * Exit function if LDAP updates has been disabled...
3976 if (!BrowseLDAPUpdate
)
3978 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3979 "send_ldap_browse: Updates temporary disabled; "
3985 * Reconnect if LDAP Handle is invalid...
3988 if (!BrowseLDAPHandle
)
3990 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3991 "send_ldap_browse: LDAP Handle is invalid. Try "
3998 * Everything in ldap is ** so we fudge around it...
4001 sprintf(typestring
, "%u", p
->type
);
4003 cn_value
[0] = p
->name
;
4005 info
[0] = p
->info
? p
->info
: "Unknown";
4007 location
[0] = p
->location
? p
->location
: "Unknown";
4009 make_model
[0] = p
->make_model
? p
->make_model
: "Unknown";
4010 make_model
[1] = NULL
;
4011 type
[0] = typestring
;
4017 * Get ldap entry for printer ...
4020 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4022 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "send_ldap_browse: dn=\"%s\"", dn
);
4024 rc
= ldap_search_rec(BrowseLDAPHandle
, dn
, LDAP_SCOPE_BASE
, NULL
,
4025 (char **)ldap_attrs
, 0, &res
);
4028 * If ldap search was not successfull then exit function
4029 * and temporary disable LDAP updates...
4032 if (rc
!= LDAP_SUCCESS
&& rc
!= LDAP_NO_SUCH_OBJECT
)
4034 if (BrowseLDAPUpdate
&&
4035 (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
))
4037 BrowseLDAPUpdate
= FALSE
;
4038 cupsdLogMessage(CUPSD_LOG_INFO
,
4039 "LDAP update temporary disabled");
4046 * Fill modification array...
4049 mods
[0].mod_type
= "cn";
4050 mods
[0].mod_values
= cn_value
;
4051 mods
[1].mod_type
= "printerDescription";
4052 mods
[1].mod_values
= info
;
4053 mods
[2].mod_type
= "printerURI";
4054 mods
[2].mod_values
= uri
;
4055 mods
[3].mod_type
= "printerLocation";
4056 mods
[3].mod_values
= location
;
4057 mods
[4].mod_type
= "printerMakeAndModel";
4058 mods
[4].mod_values
= make_model
;
4059 mods
[5].mod_type
= "printerType";
4060 mods
[5].mod_values
= type
;
4061 mods
[6].mod_type
= "objectClass";
4062 mods
[6].mod_values
= (char **)objectClass_values
;
4065 * Check if we need to insert or update the LDAP entry...
4068 if (ldap_count_entries(BrowseLDAPHandle
, res
) > 0 &&
4069 rc
!= LDAP_NO_SUCH_OBJECT
)
4072 * Printer has already been registered, check if
4073 * modification is required...
4076 e
= ldap_first_entry(BrowseLDAPHandle
, res
);
4079 * Get the required values from this entry...
4082 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerDescription",
4083 old_info
, sizeof(old_info
)) == -1)
4086 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerLocation",
4087 old_location
, sizeof(old_location
)) == -1)
4090 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerMakeAndModel",
4091 old_make_model
, sizeof(old_make_model
)) == -1)
4094 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerType",
4095 old_type_string
, sizeof(old_type_string
)) == -1)
4098 old_type
= atoi(old_type_string
);
4100 if (ldap_getval_firststring(BrowseLDAPHandle
, e
, "printerURI", old_uri
,
4101 sizeof(old_uri
)) == -1)
4105 * Check if modification is required...
4108 if (!strcmp(info
[0], old_info
) && !strcmp(uri
[0], old_uri
) &&
4109 !strcmp(location
[0], old_location
) &&
4110 !strcmp(make_model
[0], old_make_model
) && p
->type
== old_type
)
4113 * LDAP entry for the printer exists. Printer has already been registered,
4114 * no modifications required...
4117 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4118 "send_ldap_browse: No updates required for %s", p
->name
);
4123 * LDAP entry for the printer exists. Printer has already been registered,
4124 * modify the current registration...
4127 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4128 "send_ldap_browse: Replace entry for %s", p
->name
);
4130 for (i
= 0; i
< 7; i
++)
4132 pmods
[i
] = mods
+ i
;
4133 pmods
[i
]->mod_op
= LDAP_MOD_REPLACE
;
4137 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4138 if ((rc
= ldap_modify_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4139 NULL
)) != LDAP_SUCCESS
)
4141 if ((rc
= ldap_modify_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4142 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4144 cupsdLogMessage(CUPSD_LOG_ERROR
,
4145 "LDAP modify for %s failed with status %d: %s",
4146 p
->name
, rc
, ldap_err2string(rc
));
4147 if (rc
== LDAP_SERVER_DOWN
)
4155 * No LDAP entry exists for the printer. Printer has never been registered,
4156 * add the current registration...
4159 send_ldap_ou(ServerName
, BrowseLDAPDN
, "CUPS Server");
4161 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4162 "send_ldap_browse: Add entry for %s", p
->name
);
4164 for (i
= 0; i
< 7; i
++)
4166 pmods
[i
] = mods
+ i
;
4167 pmods
[i
]->mod_op
= LDAP_MOD_ADD
;
4171 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4172 if ((rc
= ldap_add_ext_s(BrowseLDAPHandle
, dn
, pmods
, NULL
,
4173 NULL
)) != LDAP_SUCCESS
)
4175 if ((rc
= ldap_add_s(BrowseLDAPHandle
, dn
, pmods
)) != LDAP_SUCCESS
)
4176 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4178 cupsdLogMessage(CUPSD_LOG_ERROR
,
4179 "LDAP add for %s failed with status %d: %s",
4180 p
->name
, rc
, ldap_err2string(rc
));
4181 if (rc
== LDAP_SERVER_DOWN
)
4191 * 'ldap_dereg_printer()' - Delete printer from directory
4195 ldap_dereg_printer(cupsd_printer_t
*p
) /* I - Printer to deregister */
4197 char dn
[1024]; /* DN of the printer */
4198 int rc
; /* LDAP status */
4201 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: Remove entry for %s",
4205 * Reconnect if LDAP Handle is invalid...
4208 if (!BrowseLDAPHandle
)
4215 * Get dn for printer and delete LDAP entry...
4218 snprintf(dn
, sizeof(dn
), "cn=%s, ou=%s, %s", p
->name
, ServerName
,
4220 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_printer: dn=\"%s\"", dn
);
4222 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4223 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4224 NULL
)) != LDAP_SUCCESS
)
4226 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4227 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4229 cupsdLogMessage(CUPSD_LOG_WARN
,
4230 "LDAP delete for %s failed with status %d: %s",
4231 p
->name
, rc
, ldap_err2string(rc
));
4234 * If we had a connection problem (connection timed out, etc.)
4235 * we should reconnect and try again to delete the entry...
4238 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4240 cupsdLogMessage(CUPSD_LOG_INFO
,
4241 "Retry deleting LDAP entry for %s after a reconnect...", p
->name
);
4244 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4245 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4246 NULL
)) != LDAP_SUCCESS
)
4248 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4249 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4250 cupsdLogMessage(CUPSD_LOG_WARN
,
4251 "LDAP delete for %s failed with status %d: %s",
4252 p
->name
, rc
, ldap_err2string(rc
));
4259 ldap_dereg_ou(char *ou
, /* I - Organizational unit (servername) */
4260 char *basedn
) /* I - Dase dn */
4262 char dn
[1024]; /* DN of the printer */
4263 int rc
; /* LDAP status */
4266 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: Remove entry for %s", ou
);
4269 * Reconnect if LDAP Handle is invalid...
4272 if (!BrowseLDAPHandle
)
4279 * Get dn for printer and delete LDAP entry...
4282 snprintf(dn
, sizeof(dn
), "ou=%s, %s", ou
, basedn
);
4283 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "ldap_dereg_ou: dn=\"%s\"", dn
);
4285 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4286 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4287 NULL
)) != LDAP_SUCCESS
)
4289 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4290 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4292 cupsdLogMessage(CUPSD_LOG_WARN
,
4293 "LDAP delete for %s failed with status %d: %s",
4294 ou
, rc
, ldap_err2string(rc
));
4297 * If we had a connection problem (connection timed out, etc.)
4298 * we should reconnect and try again to delete the entry...
4301 if (rc
== LDAP_SERVER_DOWN
|| rc
== LDAP_CONNECT_ERROR
)
4303 cupsdLogMessage(CUPSD_LOG_INFO
,
4304 "Retry deleting LDAP entry for %s after a reconnect...", ou
);
4306 # if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
4307 if ((rc
= ldap_delete_ext_s(BrowseLDAPHandle
, dn
, NULL
,
4308 NULL
)) != LDAP_SUCCESS
)
4310 if ((rc
= ldap_delete_s(BrowseLDAPHandle
, dn
)) != LDAP_SUCCESS
)
4311 # endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
4312 cupsdLogMessage(CUPSD_LOG_WARN
,
4313 "LDAP delete for %s failed with status %d: %s",
4314 ou
, rc
, ldap_err2string(rc
));
4319 #endif /* HAVE_LDAP */
4324 * 'send_slp_browse()' - Register the specified printer with SLP.
4328 send_slp_browse(cupsd_printer_t
*p
) /* I - Printer to register */
4330 char srvurl
[HTTP_MAX_URI
], /* Printer service URI */
4331 attrs
[8192], /* Printer attributes */
4332 finishings
[1024], /* Finishings to support */
4333 make_model
[IPP_MAX_NAME
* 2],
4334 /* Make and model, quoted */
4335 location
[IPP_MAX_NAME
* 2],
4336 /* Location, quoted */
4337 info
[IPP_MAX_NAME
* 2], /* Info, quoted */
4338 *src
, /* Pointer to original string */
4339 *dst
; /* Pointer to destination string */
4340 ipp_attribute_t
*authentication
; /* uri-authentication-supported value */
4341 SLPError error
; /* SLP error, if any */
4344 cupsdLogMessage(CUPSD_LOG_DEBUG
, "send_slp_browse(%p = \"%s\")", p
,
4348 * Make the SLP service URL that conforms to the IANA
4349 * 'printer:' template.
4352 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4354 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Service URL = \"%s\"", srvurl
);
4357 * Figure out the finishings string...
4360 if (p
->type
& CUPS_PRINTER_STAPLE
)
4361 strcpy(finishings
, "staple");
4363 finishings
[0] = '\0';
4365 if (p
->type
& CUPS_PRINTER_BIND
)
4368 strlcat(finishings
, ",bind", sizeof(finishings
));
4370 strcpy(finishings
, "bind");
4373 if (p
->type
& CUPS_PRINTER_PUNCH
)
4376 strlcat(finishings
, ",punch", sizeof(finishings
));
4378 strcpy(finishings
, "punch");
4381 if (p
->type
& CUPS_PRINTER_COVER
)
4384 strlcat(finishings
, ",cover", sizeof(finishings
));
4386 strcpy(finishings
, "cover");
4389 if (p
->type
& CUPS_PRINTER_SORT
)
4392 strlcat(finishings
, ",sort", sizeof(finishings
));
4394 strcpy(finishings
, "sort");
4398 strcpy(finishings
, "none");
4401 * Quote any commas in the make and model, location, and info strings...
4404 for (src
= p
->make_model
, dst
= make_model
;
4405 src
&& *src
&& dst
< (make_model
+ sizeof(make_model
) - 2);)
4407 if (*src
== ',' || *src
== '\\' || *src
== ')')
4416 strcpy(make_model
, "Unknown");
4418 for (src
= p
->location
, dst
= location
;
4419 src
&& *src
&& dst
< (location
+ sizeof(location
) - 2);)
4421 if (*src
== ',' || *src
== '\\' || *src
== ')')
4430 strcpy(location
, "Unknown");
4432 for (src
= p
->info
, dst
= info
;
4433 src
&& *src
&& dst
< (info
+ sizeof(info
) - 2);)
4435 if (*src
== ',' || *src
== '\\' || *src
== ')')
4444 strcpy(info
, "Unknown");
4447 * Get the authentication value...
4450 authentication
= ippFindAttribute(p
->attrs
, "uri-authentication-supported",
4454 * Make the SLP attribute string list that conforms to
4455 * the IANA 'printer:' template.
4458 snprintf(attrs
, sizeof(attrs
),
4459 "(printer-uri-supported=%s),"
4460 "(uri-authentication-supported=%s>),"
4462 "(uri-security-supported=tls>),"
4464 "(uri-security-supported=none>),"
4465 #endif /* HAVE_SSL */
4466 "(printer-name=%s),"
4467 "(printer-location=%s),"
4468 "(printer-info=%s),"
4469 "(printer-more-info=%s),"
4470 "(printer-make-and-model=%s),"
4471 "(printer-type=%d),"
4472 "(charset-supported=utf-8),"
4473 "(natural-language-configured=%s),"
4474 "(natural-language-supported=de,en,es,fr,it),"
4475 "(color-supported=%s),"
4476 "(finishings-supported=%s),"
4477 "(sides-supported=one-sided%s),"
4478 "(multiple-document-jobs-supported=true)"
4479 "(ipp-versions-supported=1.0,1.1)",
4480 p
->uri
, authentication
->values
[0].string
.text
, p
->name
, location
,
4481 info
, p
->uri
, make_model
, p
->type
, DefaultLanguage
,
4482 p
->type
& CUPS_PRINTER_COLOR
? "true" : "false",
4484 p
->type
& CUPS_PRINTER_DUPLEX
?
4485 ",two-sided-long-edge,two-sided-short-edge" : "");
4487 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Attributes = \"%s\"", attrs
);
4490 * Register the printer with the SLP server...
4493 error
= SLPReg(BrowseSLPHandle
, srvurl
, BrowseTimeout
,
4494 SLP_CUPS_SRVTYPE
, attrs
, SLP_TRUE
, slp_reg_callback
, 0);
4496 if (error
!= SLP_OK
)
4497 cupsdLogMessage(CUPSD_LOG_ERROR
, "SLPReg of \"%s\" failed with status %d!", p
->name
,
4503 * 'slp_attr_callback()' - SLP attribute callback
4506 static SLPBoolean
/* O - SLP_TRUE for success */
4508 SLPHandle hslp
, /* I - SLP handle */
4509 const char *attrlist
, /* I - Attribute list */
4510 SLPError errcode
, /* I - Parsing status for this attr */
4511 void *cookie
) /* I - Current printer */
4513 char *tmp
= 0; /* Temporary string */
4514 cupsd_printer_t
*p
= (cupsd_printer_t
*)cookie
;
4515 /* Current printer */
4518 (void)hslp
; /* anti-compiler-warning-code */
4521 * Bail if there was an error
4524 if (errcode
!= SLP_OK
)
4528 * Parse the attrlist to obtain things needed to build CUPS browse packet
4531 memset(p
, 0, sizeof(cupsd_printer_t
));
4533 if (slp_get_attr(attrlist
, "(printer-location=", &(p
->location
)))
4535 if (slp_get_attr(attrlist
, "(printer-info=", &(p
->info
)))
4537 if (slp_get_attr(attrlist
, "(printer-make-and-model=", &(p
->make_model
)))
4539 if (!slp_get_attr(attrlist
, "(printer-type=", &tmp
))
4540 p
->type
= atoi(tmp
);
4542 p
->type
= CUPS_PRINTER_REMOTE
;
4544 cupsdClearString(&tmp
);
4551 * 'slp_dereg_printer()' - SLPDereg() the specified printer
4555 slp_dereg_printer(cupsd_printer_t
*p
) /* I - Printer */
4557 char srvurl
[HTTP_MAX_URI
]; /* Printer service URI */
4560 cupsdLogMessage(CUPSD_LOG_DEBUG
, "slp_dereg_printer: printer=\"%s\"", p
->name
);
4562 if (!(p
->type
& CUPS_PRINTER_REMOTE
))
4565 * Make the SLP service URL that conforms to the IANA
4566 * 'printer:' template.
4569 snprintf(srvurl
, sizeof(srvurl
), SLP_CUPS_SRVTYPE
":%s", p
->uri
);
4572 * Deregister the printer...
4575 SLPDereg(BrowseSLPHandle
, srvurl
, slp_reg_callback
, 0);
4581 * 'slp_get_attr()' - Get an attribute from an SLP registration.
4584 static int /* O - 0 on success */
4585 slp_get_attr(const char *attrlist
, /* I - Attribute list string */
4586 const char *tag
, /* I - Name of attribute */
4587 char **valbuf
) /* O - Value */
4589 char *ptr1
, /* Pointer into string */
4593 cupsdClearString(valbuf
);
4595 if ((ptr1
= strstr(attrlist
, tag
)) != NULL
)
4597 ptr1
+= strlen(tag
);
4599 if ((ptr2
= strchr(ptr1
,')')) != NULL
)
4605 *valbuf
= calloc(ptr2
- ptr1
+ 1, 1);
4606 strncpy(*valbuf
, ptr1
, ptr2
- ptr1
);
4609 * Dequote the value...
4612 for (ptr1
= *valbuf
; *ptr1
; ptr1
++)
4613 if (*ptr1
== '\\' && ptr1
[1])
4614 _cups_strcpy(ptr1
, ptr1
+ 1);
4625 * 'slp_reg_callback()' - Empty SLPRegReport.
4629 slp_reg_callback(SLPHandle hslp
, /* I - SLP handle */
4630 SLPError errcode
, /* I - Error code, if any */
4631 void *cookie
) /* I - App data */
4642 * 'slp_url_callback()' - SLP service url callback
4645 static SLPBoolean
/* O - TRUE = OK, FALSE = error */
4647 SLPHandle hslp
, /* I - SLP handle */
4648 const char *srvurl
, /* I - URL of service */
4649 unsigned short lifetime
, /* I - Life of service */
4650 SLPError errcode
, /* I - Existing error code */
4651 void *cookie
) /* I - Pointer to service list */
4653 slpsrvurl_t
*s
, /* New service entry */
4654 **head
; /* Pointer to head of entry */
4658 * Let the compiler know we won't be using these vars...
4665 * Bail if there was an error
4668 if (errcode
!= SLP_OK
)
4672 * Grab the head of the list...
4675 head
= (slpsrvurl_t
**)cookie
;
4678 * Allocate a *temporary* slpsrvurl_t to hold this entry.
4681 if ((s
= (slpsrvurl_t
*)calloc(1, sizeof(slpsrvurl_t
))) == NULL
)
4685 * Copy the SLP service URL...
4688 strlcpy(s
->url
, srvurl
, sizeof(s
->url
));
4691 * Link the SLP service URL into the head of the list
4701 #endif /* HAVE_LIBSLP */
4705 * 'update_cups_browse()' - Update the browse lists using the CUPS protocol.
4709 update_cups_browse(void)
4711 int i
; /* Looping var */
4712 int auth
; /* Authorization status */
4713 int len
; /* Length of name string */
4714 int bytes
; /* Number of bytes left */
4715 char packet
[1541], /* Broadcast packet */
4716 *pptr
; /* Pointer into packet */
4717 socklen_t srclen
; /* Length of source address */
4718 http_addr_t srcaddr
; /* Source address */
4719 char srcname
[1024]; /* Source hostname */
4720 unsigned address
[4]; /* Source address */
4721 unsigned type
; /* Printer type */
4722 unsigned state
; /* Printer state */
4723 char uri
[HTTP_MAX_URI
], /* Printer URI */
4724 host
[HTTP_MAX_URI
], /* Host portion of URI */
4725 resource
[HTTP_MAX_URI
], /* Resource portion of URI */
4726 info
[IPP_MAX_NAME
], /* Information string */
4727 location
[IPP_MAX_NAME
], /* Location string */
4728 make_model
[IPP_MAX_NAME
];/* Make and model string */
4729 int num_attrs
; /* Number of attributes */
4730 cups_option_t
*attrs
; /* Attributes */
4734 * Read a packet from the browse socket...
4737 srclen
= sizeof(srcaddr
);
4738 if ((bytes
= recvfrom(BrowseSocket
, packet
, sizeof(packet
) - 1, 0,
4739 (struct sockaddr
*)&srcaddr
, &srclen
)) < 0)
4742 * "Connection refused" is returned under Linux if the destination port
4743 * or address is unreachable from a previous sendto(); check for the
4744 * error here and ignore it for now...
4747 if (errno
!= ECONNREFUSED
&& errno
!= EAGAIN
)
4749 cupsdLogMessage(CUPSD_LOG_ERROR
, "Browse recv failed - %s.",
4751 cupsdLogMessage(CUPSD_LOG_ERROR
, "CUPS browsing turned off.");
4754 closesocket(BrowseSocket
);
4756 close(BrowseSocket
);
4759 cupsdRemoveSelect(BrowseSocket
);
4762 BrowseLocalProtocols
&= ~BROWSE_CUPS
;
4763 BrowseRemoteProtocols
&= ~BROWSE_CUPS
;
4769 packet
[bytes
] = '\0';
4772 * If we're about to sleep, ignore incoming browse packets.
4779 * Figure out where it came from...
4783 if (srcaddr
.addr
.sa_family
== AF_INET6
)
4785 address
[0] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[0]);
4786 address
[1] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[1]);
4787 address
[2] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[2]);
4788 address
[3] = ntohl(srcaddr
.ipv6
.sin6_addr
.s6_addr32
[3]);
4791 #endif /* AF_INET6 */
4796 address
[3] = ntohl(srcaddr
.ipv4
.sin_addr
.s_addr
);
4799 if (HostNameLookups
)
4800 httpAddrLookup(&srcaddr
, srcname
, sizeof(srcname
));
4802 httpAddrString(&srcaddr
, srcname
, sizeof(srcname
));
4804 len
= strlen(srcname
);
4812 if (httpAddrLocalhost(&srcaddr
) || !strcasecmp(srcname
, "localhost"))
4815 * Access from localhost (127.0.0.1) is always allowed...
4818 auth
= CUPSD_AUTH_ALLOW
;
4823 * Do authorization checks on the domain/address...
4826 switch (BrowseACL
->order_type
)
4829 auth
= CUPSD_AUTH_DENY
; /* anti-compiler-warning-code */
4832 case CUPSD_AUTH_ALLOW
: /* Order Deny,Allow */
4833 auth
= CUPSD_AUTH_ALLOW
;
4835 if (cupsdCheckAuth(address
, srcname
, len
,
4836 BrowseACL
->num_deny
, BrowseACL
->deny
))
4837 auth
= CUPSD_AUTH_DENY
;
4839 if (cupsdCheckAuth(address
, srcname
, len
,
4840 BrowseACL
->num_allow
, BrowseACL
->allow
))
4841 auth
= CUPSD_AUTH_ALLOW
;
4844 case CUPSD_AUTH_DENY
: /* Order Allow,Deny */
4845 auth
= CUPSD_AUTH_DENY
;
4847 if (cupsdCheckAuth(address
, srcname
, len
,
4848 BrowseACL
->num_allow
, BrowseACL
->allow
))
4849 auth
= CUPSD_AUTH_ALLOW
;
4851 if (cupsdCheckAuth(address
, srcname
, len
,
4852 BrowseACL
->num_deny
, BrowseACL
->deny
))
4853 auth
= CUPSD_AUTH_DENY
;
4859 auth
= CUPSD_AUTH_ALLOW
;
4861 if (auth
== CUPSD_AUTH_DENY
)
4863 cupsdLogMessage(CUPSD_LOG_DEBUG
,
4864 "update_cups_browse: Refused %d bytes from %s", bytes
,
4869 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
4870 "update_cups_browse: (%d bytes from %s) %s", bytes
,
4877 if (sscanf(packet
, "%x%x%1023s", &type
, &state
, uri
) < 3)
4879 cupsdLogMessage(CUPSD_LOG_WARN
,
4880 "update_cups_browse: Garbled browse packet - %s", packet
);
4884 strcpy(location
, "Location Unknown");
4885 strcpy(info
, "No Information Available");
4886 make_model
[0] = '\0';
4890 if ((pptr
= strchr(packet
, '\"')) != NULL
)
4893 * Have extended information; can't use sscanf for it because not all
4894 * sscanf's allow empty strings with %[^\"]...
4897 for (i
= 0, pptr
++;
4898 i
< (sizeof(location
) - 1) && *pptr
&& *pptr
!= '\"';
4900 location
[i
] = *pptr
;
4908 while (*pptr
&& isspace(*pptr
& 255))
4913 for (i
= 0, pptr
++;
4914 i
< (sizeof(info
) - 1) && *pptr
&& *pptr
!= '\"';
4923 while (*pptr
&& isspace(*pptr
& 255))
4928 for (i
= 0, pptr
++;
4929 i
< (sizeof(make_model
) - 1) && *pptr
&& *pptr
!= '\"';
4931 make_model
[i
] = *pptr
;
4936 make_model
[i
] = '\0';
4939 num_attrs
= cupsParseOptions(pptr
, num_attrs
, &attrs
);
4945 DEBUG_printf(("type=%x, state=%x, uri=\"%s\"\n"
4946 "location=\"%s\", info=\"%s\", make_model=\"%s\"\n",
4947 type
, state
, uri
, location
, info
, make_model
));
4950 * Pull the URI apart to see if this is a local or remote printer...
4953 if (is_local_queue(uri
, host
, sizeof(host
), resource
, sizeof(resource
)))
4955 cupsFreeOptions(num_attrs
, attrs
);
4963 for (i
= 0; i
< NumRelays
; i
++)
4964 if (cupsdCheckAuth(address
, srcname
, len
, 1, &(Relays
[i
].from
)))
4965 if (sendto(BrowseSocket
, packet
, bytes
, 0,
4966 (struct sockaddr
*)&(Relays
[i
].to
),
4967 httpAddrLength(&(Relays
[i
].to
))) <= 0)
4969 cupsdLogMessage(CUPSD_LOG_ERROR
,
4970 "update_cups_browse: sendto failed for relay %d - %s.",
4971 i
+ 1, strerror(errno
));
4972 cupsFreeOptions(num_attrs
, attrs
);
4977 * Process the browse data...
4980 process_browse_data(uri
, host
, resource
, (cups_ptype_t
)type
,
4981 (ipp_pstate_t
)state
, location
, info
, make_model
,
4987 * 'update_lpd()' - Update the LPD configuration as needed.
4991 update_lpd(int onoff
) /* - 1 = turn on, 0 = turn off */
4998 * Allow /etc/hostconfig CUPS_LPD service setting to override cupsd.conf
4999 * setting for backwards-compatibility.
5002 if (onoff
&& !get_hostconfig("CUPS_LPD"))
5004 #endif /* __APPLE__ */
5006 if (!strncmp(LPDConfigFile
, "xinetd:///", 10))
5009 * Enable/disable LPD via the xinetd.d config file for cups-lpd...
5012 char newfile
[1024]; /* New cups-lpd.N file */
5013 cups_file_t
*ofp
, /* Original file pointer */
5014 *nfp
; /* New file pointer */
5015 char line
[1024]; /* Line from file */
5018 snprintf(newfile
, sizeof(newfile
), "%s.N", LPDConfigFile
+ 9);
5020 if ((ofp
= cupsFileOpen(LPDConfigFile
+ 9, "r")) == NULL
)
5022 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5023 LPDConfigFile
+ 9, strerror(errno
));
5027 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5029 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5030 newfile
, strerror(errno
));
5036 * Copy all of the lines from the cups-lpd file...
5039 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5043 cupsFilePrintf(nfp
, "%s\n", line
);
5044 snprintf(line
, sizeof(line
), "\tdisable = %s",
5045 onoff
? "no" : "yes");
5047 else if (!strstr(line
, "disable ="))
5048 cupsFilePrintf(nfp
, "%s\n", line
);
5053 rename(newfile
, LPDConfigFile
+ 9);
5056 else if (!strncmp(LPDConfigFile
, "launchd:///", 11))
5059 * Enable/disable LPD via the launchctl command...
5062 char *argv
[5], /* Arguments for command */
5063 *envp
[MAX_ENV
]; /* Environment for command */
5064 int pid
; /* Process ID */
5067 cupsdLoadEnv(envp
, (int)(sizeof(envp
) / sizeof(envp
[0])));
5068 argv
[0] = (char *)"launchctl";
5069 argv
[1] = (char *)(onoff
? "load" : "unload");
5070 argv
[2] = (char *)"-w";
5071 argv
[3] = LPDConfigFile
+ 10;
5074 cupsdStartProcess("/bin/launchctl", argv
, envp
, -1, -1, -1, -1, -1, 1,
5077 #endif /* __APPLE__ */
5079 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown LPDConfigFile scheme!");
5084 * 'update_polling()' - Read status messages from the poll daemons.
5088 update_polling(void)
5090 char *ptr
, /* Pointer to end of line in buffer */
5091 message
[1024]; /* Pointer to message text */
5092 int loglevel
; /* Log level for message */
5095 while ((ptr
= cupsdStatBufUpdate(PollStatusBuffer
, &loglevel
,
5096 message
, sizeof(message
))) != NULL
)
5097 if (!strchr(PollStatusBuffer
->buffer
, '\n'))
5100 if (ptr
== NULL
&& !PollStatusBuffer
->bufused
)
5103 * All polling processes have died; stop polling...
5106 cupsdLogMessage(CUPSD_LOG_ERROR
,
5107 "update_polling: all polling processes have exited!");
5114 * 'update_smb()' - Update the SMB configuration as needed.
5118 update_smb(int onoff
) /* I - 1 = turn on, 0 = turn off */
5123 if (!strncmp(SMBConfigFile
, "samba:///", 9))
5126 * Enable/disable SMB via the specified smb.conf config file...
5129 char newfile
[1024]; /* New smb.conf.N file */
5130 cups_file_t
*ofp
, /* Original file pointer */
5131 *nfp
; /* New file pointer */
5132 char line
[1024]; /* Line from file */
5133 int in_printers
; /* In [printers] section? */
5136 snprintf(newfile
, sizeof(newfile
), "%s.N", SMBConfigFile
+ 8);
5138 if ((ofp
= cupsFileOpen(SMBConfigFile
+ 8, "r")) == NULL
)
5140 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open \"%s\" - %s",
5141 SMBConfigFile
+ 8, strerror(errno
));
5145 if ((nfp
= cupsFileOpen(newfile
, "w")) == NULL
)
5147 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create \"%s\" - %s",
5148 newfile
, strerror(errno
));
5154 * Copy all of the lines from the smb.conf file...
5159 while (cupsFileGets(ofp
, line
, sizeof(line
)))
5161 if (in_printers
&& strstr(line
, "printable ="))
5162 snprintf(line
, sizeof(line
), " printable = %s",
5163 onoff
? "yes" : "no");
5165 cupsFilePrintf(nfp
, "%s\n", line
);
5168 in_printers
= !strcmp(line
, "[printers]");
5173 rename(newfile
, SMBConfigFile
+ 8);
5176 cupsdLogMessage(CUPSD_LOG_INFO
, "Unknown SMBConfigFile scheme!");
5181 * End of "$Id: dirsvc.c 7676 2008-06-18 23:42:37Z mike $".