/*
- * "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $"
+ * "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $"
*
- * Directory services routines for the Common UNIX Printing System (CUPS).
+ * Directory services routines for the CUPS scheduler.
*
- * Copyright 2007-2008 by Apple Inc.
+ * Copyright 2007-2011 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
* cupsdRestartPolling() - Restart polling servers as needed.
* cupsdSaveRemoteCache() - Save the remote printer cache.
* cupsdSendBrowseList() - Send new browsing information as necessary.
+ * ldap_rebind_proc() - Callback function for LDAP rebind
+ * ldap_connect() - Start new LDAP connection
+ * ldap_reconnect() - Reconnect to LDAP Server
+ * ldap_disconnect() - Disconnect from LDAP Server
* cupsdStartBrowsing() - Start sending and receiving broadcast
* information.
* cupsdStartPolling() - Start polling servers as needed.
* cupsdStopBrowsing() - Stop sending and receiving broadcast
* information.
* cupsdStopPolling() - Stop polling servers as needed.
- * cupsdUpdateDNSSDBrowse() - Handle DNS-SD queries.
* cupsdUpdateDNSSDName() - Update the computer name we use for
* browsing...
* cupsdUpdateLDAPBrowse() - Scan for new printers via LDAP...
* cupsdUpdateSLPBrowse() - Get browsing information via SLP.
* dequote() - Remote quotes from a string.
+ * dnssdAddAlias() - Add a DNS-SD alias name.
* dnssdBuildTxtRecord() - Build a TXT record from printer info.
* dnssdComparePrinters() - Compare the registered names of two printers.
* dnssdDeregisterPrinter() - Stop sending broadcast information for a
* dnssdRegisterCallback() - DNSServiceRegister callback.
* dnssdRegisterPrinter() - Start sending broadcast information for a
* printer or update the broadcast contents.
+ * dnssdStop() - Stop all DNS-SD registrations.
+ * dnssdUpdate() - Handle DNS-SD queries.
+ * get_auth_info_required() - Get the auth-info-required value to advertise.
* get_hostconfig() - Get an /etc/hostconfig service setting.
* is_local_queue() - Determine whether the URI points at a local
* queue.
* process_implicit_classes() - Create/update implicit classes as needed.
* send_cups_browse() - Send new browsing information using the CUPS
* protocol.
+ * ldap_search_rec() - LDAP Search with reconnect
+ * ldap_freeres() - Free LDAPMessage
+ * ldap_getval_char() - Get first LDAP value and convert to string
+ * send_ldap_ou() - Send LDAP ou registrations.
* send_ldap_browse() - Send LDAP printer registrations.
+ * ldap_dereg_printer() - Delete printer from directory
+ * ldap_dereg_ou() - Remove the organizational unit.
* send_slp_browse() - Register the specified printer with SLP.
* slp_attr_callback() - SLP attribute callback
* slp_dereg_printer() - SLPDereg() the specified printer
*/
static char *dequote(char *d, const char *s, int dlen);
+static char *get_auth_info_required(cupsd_printer_t *p, char *buffer,
+ size_t bufsize);
#ifdef __APPLE__
static int get_hostconfig(const char *name);
#endif /* __APPLE__ */
static void process_implicit_classes(void);
static void send_cups_browse(cupsd_printer_t *p);
#ifdef HAVE_LDAP
+static LDAP *ldap_connect(void);
+static LDAP *ldap_reconnect(void);
+static void ldap_disconnect(LDAP *ld);
+static int ldap_search_rec(LDAP *ld, char *base, int scope,
+ char *filter, char *attrs[],
+ int attrsonly, LDAPMessage **res);
+static int ldap_getval_firststring(LDAP *ld, LDAPMessage *entry,
+ char *attr, char *retval,
+ unsigned long maxsize);
+static void ldap_freeres(LDAPMessage *entry);
+static void send_ldap_ou(char *ou, char *basedn, char *descstring);
static void send_ldap_browse(cupsd_printer_t *p);
+static void ldap_dereg_printer(cupsd_printer_t *p);
+static void ldap_dereg_ou(char *ou, char *basedn);
+# ifdef HAVE_LDAP_REBIND_PROC
+# if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
+static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
+ LDAP_CONST char *refsp,
+ ber_tag_t request,
+ ber_int_t msgid,
+ void *params);
+# else
+static int ldap_rebind_proc(LDAP *RebindLDAPHandle,
+ char **dnp,
+ char **passwdp,
+ int *authmethodp,
+ int freeit,
+ void *arg);
+# endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
+# endif /* HAVE_LDAP_REBIND_PROC */
#endif /* HAVE_LDAP */
#ifdef HAVE_LIBSLP
static void send_slp_browse(cupsd_printer_t *p);
#ifdef HAVE_DNSSD
+# ifdef HAVE_COREFOUNDATION
+static void dnssdAddAlias(const void *key, const void *value,
+ void *context);
+# endif /* HAVE_COREFOUNDATION */
static char *dnssdBuildTxtRecord(int *txt_len, cupsd_printer_t *p,
int for_lpd);
static int dnssdComparePrinters(cupsd_printer_t *a, cupsd_printer_t *b);
const char *name, const char *regtype,
const char *domain, void *context);
static void dnssdRegisterPrinter(cupsd_printer_t *p);
+static void dnssdStop(void);
+static void dnssdUpdate(void);
#endif /* HAVE_DNSSD */
-#ifdef HAVE_OPENLDAP
+#ifdef HAVE_LDAP
static const char * const ldap_attrs[] =/* CUPS LDAP attributes */
{
"printerDescription",
"printerURI",
NULL
};
-#endif /* HAVE_OPENLDAP */
+#endif /* HAVE_LDAP */
#ifdef HAVE_LIBSLP
/*
* Only deregister if browsing is enabled and it's a local printer...
*/
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "cupsdDeregisterPrinter(p=%p(%s), removeit=%d)", p, p->name,
+ removeit);
+
if (!Browsing || !p->shared ||
- (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)))
return;
/*
slp_dereg_printer(p);
#endif /* HAVE_LIBSLP */
+#ifdef HAVE_LDAP
+ if (BrowseLocalProtocols & BROWSE_LDAP)
+ ldap_dereg_printer(p);
+#endif /* HAVE_LDAP */
+
#ifdef HAVE_DNSSD
if (removeit && (BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
dnssdDeregisterPrinter(p);
void
cupsdLoadRemoteCache(void)
{
+ int i; /* Looping var */
cups_file_t *fp; /* remote.cache file */
int linenum; /* Current line number */
- char line[1024], /* Line from file */
+ char line[4096], /* Line from file */
*value, /* Pointer to value */
*valueptr, /* Pointer into value */
scheme[32], /* Scheme portion of URI */
p = NULL;
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!p)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
+ }
+ else if (!strcasecmp(line, "UUID"))
+ {
+ if (value && !strncmp(value, "urn:uuid:", 9))
+ cupsdSetString(&(p->uuid), value);
+ else
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Bad UUID on line %d of remote.cache.", linenum);
}
else if (!strcasecmp(line, "Info"))
{
cupsdSetString(&p->hostname, host);
cupsdSetString(&p->uri, value);
- cupsdSetString(&p->device_uri, value);
+ cupsdSetDeviceURI(p, value);
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "Option") && value)
{
&(p->options));
}
}
+ else if (!strcasecmp(line, "Reason"))
+ {
+ if (value)
+ {
+ for (i = 0 ; i < p->num_reasons; i ++)
+ if (!strcmp(value, p->reasons[i]))
+ break;
+
+ if (i >= p->num_reasons &&
+ p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
+ {
+ p->reasons[p->num_reasons] = _cupsStrAlloc(value);
+ p->num_reasons ++;
+ }
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Syntax error on line %d of remote.cache.", linenum);
+ }
else if (!strcasecmp(line, "State"))
{
/*
if (value && !strcasecmp(value, "idle"))
p->state = IPP_PRINTER_IDLE;
else if (value && !strcasecmp(value, "stopped"))
+ {
p->state = IPP_PRINTER_STOPPED;
+ cupsdSetPrinterReasons(p, "+paused");
+ }
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "StateMessage"))
{
!strcasecmp(value, "false")))
p->accepting = 0;
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "Type"))
{
if (value)
p->type = atoi(value);
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "BrowseTime"))
{
p->browse_expire = t;
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "JobSheets"))
{
for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
if (*valueptr)
- *valueptr++ = '\0';
+ *valueptr = '\0';
cupsdSetString(&p->job_sheets[1], value);
}
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "AllowUser"))
{
if (value)
{
p->deny_users = 0;
- cupsdAddPrinterUser(p, value);
+ cupsdAddString(&(p->users), value);
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else if (!strcasecmp(line, "DenyUser"))
{
if (value)
{
p->deny_users = 1;
- cupsdAddPrinterUser(p, value);
+ cupsdAddString(&(p->users), value);
}
else
- {
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of remote.cache.", linenum);
- break;
- }
}
else
{
void
cupsdRegisterPrinter(cupsd_printer_t *p)/* I - Printer */
{
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "cupsdRegisterPrinter(p=%p(%s))", p,
+ p->name);
+
if (!Browsing || !BrowseLocalProtocols ||
- (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)))
return;
#ifdef HAVE_LIBSLP
{
int i; /* Looping var */
cups_file_t *fp; /* printers.conf file */
- char temp[1024]; /* Temporary string */
+ char temp[1024], /* Temporary string */
+ value[2048], /* Value string */
+ *name; /* Current user name */
cupsd_printer_t *printer; /* Current printer class */
time_t curtime; /* Current time */
struct tm *curdate; /* Current date */
else
cupsFilePrintf(fp, "Printer %s>\n", printer->name);
- cupsFilePrintf(fp, "Type %d\n", printer->type);
-
cupsFilePrintf(fp, "BrowseTime %d\n", (int)printer->browse_expire);
- if (printer->info)
- cupsFilePrintf(fp, "Info %s\n", printer->info);
+ cupsFilePrintf(fp, "UUID %s\n", printer->uuid);
- if (printer->make_model)
- cupsFilePrintf(fp, "MakeModel %s\n", printer->make_model);
+ if (printer->info)
+ cupsFilePutConf(fp, "Info", printer->info);
if (printer->location)
- cupsFilePrintf(fp, "Location %s\n", printer->location);
+ cupsFilePutConf(fp, "Location", printer->location);
+
+ if (printer->make_model)
+ cupsFilePutConf(fp, "MakeModel", printer->make_model);
- if (printer->device_uri)
- cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
+ cupsFilePutConf(fp, "DeviceURI", printer->device_uri);
if (printer->state == IPP_PRINTER_STOPPED)
- {
cupsFilePuts(fp, "State Stopped\n");
- cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
- }
else
cupsFilePuts(fp, "State Idle\n");
+ for (i = 0; i < printer->num_reasons; i ++)
+ cupsFilePutConf(fp, "Reason", printer->reasons[i]);
+
+ cupsFilePrintf(fp, "Type %d\n", printer->type);
+
if (printer->accepting)
cupsFilePuts(fp, "Accepting Yes\n");
else
cupsFilePuts(fp, "Accepting No\n");
- cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
- printer->job_sheets[1]);
+ snprintf(value, sizeof(value), "%s %s", printer->job_sheets[0],
+ printer->job_sheets[1]);
+ cupsFilePutConf(fp, "JobSheets", value);
- for (i = 0; i < printer->num_users; i ++)
- cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
- printer->users[i]);
+ for (name = (char *)cupsArrayFirst(printer->users);
+ name;
+ name = (char *)cupsArrayNext(printer->users))
+ cupsFilePutConf(fp, printer->deny_users ? "DenyUser" : "AllowUser", name);
for (i = printer->num_options, option = printer->options;
i > 0;
i --, option ++)
- cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
+ {
+ snprintf(value, sizeof(value), "%s %s", option->name, option->value);
+ cupsFilePutConf(fp, "Option", value);
+ }
if (printer->type & CUPS_PRINTER_CLASS)
cupsFilePuts(fp, "</Class>\n");
to; /* Timeout time */
- if (!Browsing || !BrowseLocalProtocols || !Printers)
+ if (!Browsing || !Printers)
return;
/*
* Figure out how many printers need an update...
*/
- if (BrowseInterval > 0)
+ if (BrowseInterval > 0 && BrowseLocalProtocols)
{
int max_count; /* Maximum number to update */
for (count = 0, p = (cupsd_printer_t *)cupsArrayFirst(Printers);
count < max_count && p != NULL;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
- if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)) &&
p->shared && p->browse_time < ut)
count ++;
if (!p)
break;
- else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
+ else if ((p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)) ||
!p->shared)
continue;
else if (p->browse_time < ut)
}
/*
- * Loop through all of the printers and send local updates as needed...
+ * Loop through all of the printers and timeout old printers as needed...
*/
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
* If this is a remote queue, see if it needs to be timed out...
*/
- if (p->type & CUPS_PRINTER_DISCOVERED)
+ if ((p->type & CUPS_PRINTER_DISCOVERED) &&
+ !(p->type & CUPS_PRINTER_IMPLICIT) &&
+ p->browse_expire < to)
{
- if (p->browse_expire < to)
- {
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "%s \'%s\' deleted by directory services (timeout).",
- (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
- p->name);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "%s \'%s\' deleted by directory services (timeout).",
+ (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
+ p->name);
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Remote destination \"%s\" has timed out; "
- "deleting it...",
- p->name);
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Remote destination \"%s\" has timed out; "
+ "deleting it...",
+ p->name);
- cupsArraySave(Printers);
- cupsdDeletePrinter(p, 1);
- cupsArrayRestore(Printers);
- cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
- }
+ cupsArraySave(Printers);
+ cupsdDeletePrinter(p, 1);
+ cupsArrayRestore(Printers);
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
}
}
}
+#ifdef HAVE_LDAP_REBIND_PROC
+# if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
/*
- * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
+ * 'ldap_rebind_proc()' - Callback function for LDAP rebind
*/
-void
-cupsdStartBrowsing(void)
+static int /* O - Result code */
+ldap_rebind_proc(
+ LDAP *RebindLDAPHandle, /* I - LDAP handle */
+ LDAP_CONST char *refsp, /* I - ??? */
+ ber_tag_t request, /* I - ??? */
+ ber_int_t msgid, /* I - ??? */
+ void *params) /* I - ??? */
{
- int val; /* Socket option value */
- struct sockaddr_in addr; /* Broadcast address */
- cupsd_printer_t *p; /* Current printer */
+ int rc; /* Result code */
+# if LDAP_API_VERSION > 3000
+ struct berval bval; /* Bind value */
+# endif /* LDAP_API_VERSION > 3000 */
+ /*
+ * Bind to new LDAP server...
+ */
- BrowseNext = NULL;
-
- if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
- return;
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_rebind_proc: Rebind to %s", refsp);
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
- {
- if (BrowseSocket < 0)
- {
- /*
- * Create the broadcast socket...
- */
+# if LDAP_API_VERSION > 3000
+ bval.bv_val = BrowseLDAPPassword;
+ bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword);
- if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to create broadcast socket - %s.",
- strerror(errno));
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
+ rc = ldap_sasl_bind_s(RebindLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE,
+ &bval, NULL, NULL, NULL);
+# else
+ rc = ldap_bind_s(RebindLDAPHandle, BrowseLDAPBindDN, BrowseLDAPPassword,
+ LDAP_AUTH_SIMPLE);
+# endif /* LDAP_API_VERSION > 3000 */
- /*
- * Bind the socket to browse port...
- */
+ return (rc);
+}
- memset(&addr, 0, sizeof(addr));
- addr.sin_addr.s_addr = htonl(INADDR_ANY);
- addr.sin_family = AF_INET;
- addr.sin_port = htons(BrowsePort);
- if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to bind broadcast socket - %s.",
- strerror(errno));
+# else /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
+/*
+ * 'ldap_rebind_proc()' - Callback function for LDAP rebind
+ */
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+static int /* O - Result code */
+ldap_rebind_proc(
+ LDAP *RebindLDAPHandle, /* I - LDAP handle */
+ char **dnp, /* I - ??? */
+ char **passwdp, /* I - ??? */
+ int *authmethodp, /* I - ??? */
+ int freeit, /* I - ??? */
+ void *arg) /* I - ??? */
+{
+ switch (freeit)
+ {
+ case 1:
+ /*
+ * Free current values...
+ */
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_rebind_proc: Free values...");
- /*
- * Set the "broadcast" flag...
- */
+ if (dnp && *dnp)
+ free(*dnp);
- val = 1;
- if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
- {
- cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
- strerror(errno));
+ if (passwdp && *passwdp)
+ free(*passwdp);
+ break;
-#ifdef WIN32
- closesocket(BrowseSocket);
-#else
- close(BrowseSocket);
-#endif /* WIN32 */
+ case 0:
+ /*
+ * Return credentials for LDAP referal...
+ */
- BrowseSocket = -1;
- BrowseLocalProtocols &= ~BROWSE_CUPS;
- BrowseRemoteProtocols &= ~BROWSE_CUPS;
- return;
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "ldap_rebind_proc: Return necessary values...");
- /*
- * Close the socket on exec...
- */
+ *dnp = strdup(BrowseLDAPBindDN);
+ *passwdp = strdup(BrowseLDAPPassword);
+ *authmethodp = LDAP_AUTH_SIMPLE;
+ break;
- fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
+ default:
+ /*
+ * Should never happen...
+ */
- /*
- * Finally, add the socket to the input selection set as needed...
- */
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP rebind has been called with wrong freeit value!");
+ break;
+ }
- if (BrowseRemoteProtocols & BROWSE_CUPS)
- {
- /*
- * We only listen if we want remote printers...
- */
+ return (LDAP_SUCCESS);
+}
+# endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
+#endif /* HAVE_LDAP_REBIND_PROC */
- cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
- NULL, NULL);
- }
- }
- else
- BrowseSocket = -1;
-#ifdef HAVE_DNSSD
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
- {
- DNSServiceErrorType error; /* Error from service creation */
- cupsd_listener_t *lis; /* Current listening socket */
+#ifdef HAVE_LDAP
+/*
+ * 'ldap_connect()' - Start new LDAP connection
+ */
+static LDAP * /* O - LDAP handle */
+ldap_connect(void)
+{
+ int rc; /* LDAP API status */
+ int version = 3; /* LDAP version */
+ struct berval bv = {0, ""}; /* SASL bind value */
+ LDAP *TempBrowseLDAPHandle=NULL;
+ /* Temporary LDAP Handle */
+# if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
+ int ldap_ssl = 0; /* LDAP SSL indicator */
+ int ssl_err = 0; /* LDAP SSL error value */
+# endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
+
+
+# ifdef HAVE_OPENLDAP
+# ifdef HAVE_LDAP_SSL
+ /*
+ * Set the certificate file to use for encrypted LDAP sessions...
+ */
- /*
- * First create a "master" connection for all registrations...
- */
+ if (BrowseLDAPCACertFile)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "ldap_connect: Setting CA certificate file \"%s\"",
+ BrowseLDAPCACertFile);
- if ((error = DNSServiceCreateConnection(&DNSSDRef))
- != kDNSServiceErr_NoError)
+ if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
+ (void *)BrowseLDAPCACertFile)) != LDAP_SUCCESS)
cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to create master DNS-SD reference: %d", error);
- else
- {
- /*
- * Add the master connection to the select list...
- */
-
- cupsdAddSelect(DNSServiceRefSockFD(DNSSDRef),
- (cupsd_selfunc_t)cupsdUpdateDNSSDBrowse, NULL, NULL);
+ "Unable to set CA certificate file for LDAP "
+ "connections: %d - %s", rc, ldap_err2string(rc));
+ }
+# endif /* HAVE_LDAP_SSL */
- /*
- * Then get the port we use for registrations. If we are not listening
- * on any non-local ports, there is no sense sharing local printers via
- * Bonjour...
- */
+ /*
+ * Initialize OPENLDAP connection...
+ * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
+ */
- DNSSDPort = 0;
+ if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
+ rc = ldap_initialize(&TempBrowseLDAPHandle, "ldapi:///");
+ else
+ rc = ldap_initialize(&TempBrowseLDAPHandle, BrowseLDAPServer);
- for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
- lis;
- lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
- {
- if (httpAddrLocalhost(&(lis->address)))
- continue;
+# else /* HAVE_OPENLDAP */
- if (lis->address.addr.sa_family == AF_INET)
- {
- DNSSDPort = ntohs(lis->address.ipv4.sin_port);
- break;
- }
- else if (lis->address.addr.sa_family == AF_INET6)
- {
- DNSSDPort = ntohs(lis->address.ipv6.sin6_port);
- break;
- }
- }
+ int ldap_port = 0; /* LDAP port */
+ char ldap_protocol[11], /* LDAP protocol */
+ ldap_host[255]; /* LDAP host */
- /*
- * Create an array to track the printers we share...
- */
+ /*
+ * Split LDAP URI into its components...
+ */
- if (BrowseRemoteProtocols & BROWSE_DNSSD)
- DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters,
- NULL);
+ if (!BrowseLDAPServer)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "BrowseLDAPServer not configured!");
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Disabling LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ return (NULL);
+ }
- /*
- * Set the computer name and register the web interface...
- */
+ sscanf(BrowseLDAPServer, "%10[^:]://%254[^:/]:%d", ldap_protocol, ldap_host,
+ &ldap_port);
- cupsdUpdateDNSSDName();
- }
+ if (!strcmp(ldap_protocol, "ldap"))
+ ldap_ssl = 0;
+ else if (!strcmp(ldap_protocol, "ldaps"))
+ ldap_ssl = 1;
+ else
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unrecognized LDAP protocol (%s)!",
+ ldap_protocol);
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Disabling LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ return (NULL);
}
-#endif /* HAVE_DNSSD */
-#ifdef HAVE_LIBSLP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
+ if (ldap_port == 0)
{
- /*
- * Open SLP handle...
- */
+ if (ldap_ssl)
+ ldap_port = LDAPS_PORT;
+ else
+ ldap_port = LDAP_PORT;
+ }
- if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to open an SLP handle; disabling SLP browsing!");
- BrowseLocalProtocols &= ~BROWSE_SLP;
- BrowseRemoteProtocols &= ~BROWSE_SLP;
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "ldap_connect: PROT:%s HOST:%s PORT:%d",
+ ldap_protocol, ldap_host, ldap_port);
- BrowseSLPRefresh = 0;
+ /*
+ * Initialize LDAP connection...
+ */
+
+ if (!ldap_ssl)
+ {
+ if ((TempBrowseLDAPHandle = ldap_init(ldap_host, ldap_port)) == NULL)
+ rc = LDAP_OPERATIONS_ERROR;
+ else
+ rc = LDAP_SUCCESS;
+
+# ifdef HAVE_LDAP_SSL
}
else
- BrowseSLPHandle = NULL;
-#endif /* HAVE_LIBSLP */
-
-#ifdef HAVE_OPENLDAP
- if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP)
{
- if (!BrowseLDAPDN)
+ /*
+ * Initialize SSL LDAP connection...
+ */
+
+ if (BrowseLDAPCACertFile)
+ {
+ rc = ldapssl_client_init(BrowseLDAPCACertFile, (void *)NULL);
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Failed to initialize LDAP SSL client!");
+ rc = LDAP_OPERATIONS_ERROR;
+ }
+ else
+ {
+ if ((TempBrowseLDAPHandle = ldapssl_init(ldap_host, ldap_port,
+ 1)) == NULL)
+ rc = LDAP_OPERATIONS_ERROR;
+ else
+ rc = LDAP_SUCCESS;
+ }
+ }
+ else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
- "Need to set BrowseLDAPDN to use LDAP browsing!");
- BrowseLocalProtocols &= ~BROWSE_LDAP;
- BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ "LDAP SSL certificate file/database not configured!");
+ rc = LDAP_OPERATIONS_ERROR;
}
+
+# else /* HAVE_LDAP_SSL */
+
+ /*
+ * Return error, because client libraries doesn't support SSL
+ */
+
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP client libraries do not support SSL");
+ rc = LDAP_OPERATIONS_ERROR;
+
+# endif /* HAVE_LDAP_SSL */
+ }
+# endif /* HAVE_OPENLDAP */
+
+ /*
+ * Check return code from LDAP initialize...
+ */
+
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to initialize LDAP!");
+
+ if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Temporarily disabling LDAP browsing...");
else
{
- /*
- * Open LDAP handle...
- */
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Disabling LDAP browsing!");
+
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ }
+
+ ldap_disconnect(TempBrowseLDAPHandle);
+
+ return (NULL);
+ }
+
+ /*
+ * Upgrade LDAP version...
+ */
+
+ if (ldap_set_option(TempBrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION,
+ (const void *)&version) != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set LDAP protocol version %d!",
+ version);
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Disabling LDAP browsing!");
- int rc; /* LDAP API status */
- int version = 3; /* LDAP version */
- struct berval bv = {0, ""}; /* SASL bind value */
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ ldap_disconnect(TempBrowseLDAPHandle);
+ return (NULL);
+ }
+
+ /*
+ * Register LDAP rebind procedure...
+ */
+
+# ifdef HAVE_LDAP_REBIND_PROC
+# if defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000)
+
+ rc = ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc,
+ (void *)NULL);
+ if (rc != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Setting LDAP rebind function failed with status %d: %s",
+ rc, ldap_err2string(rc));
+
+# else
+
+ ldap_set_rebind_proc(TempBrowseLDAPHandle, &ldap_rebind_proc, (void *)NULL);
+
+# endif /* defined(LDAP_API_FEATURE_X_OPENLDAP) && (LDAP_API_VERSION > 2000) */
+# endif /* HAVE_LDAP_REBIND_PROC */
+
+ /*
+ * Start LDAP bind...
+ */
+
+# if LDAP_API_VERSION > 3000
+ struct berval bval;
+ bval.bv_val = BrowseLDAPPassword;
+ bval.bv_len = (BrowseLDAPPassword == NULL) ? 0 : strlen(BrowseLDAPPassword);
+
+ if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
+ rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL,
+ NULL, NULL);
+ else
+ rc = ldap_sasl_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN, LDAP_SASL_SIMPLE, &bval, NULL, NULL, NULL);
+
+# else
+ rc = ldap_bind_s(TempBrowseLDAPHandle, BrowseLDAPBindDN,
+ BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
+# endif /* LDAP_API_VERSION > 3000 */
+
+ if (rc != LDAP_SUCCESS)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR, "LDAP bind failed with error %d: %s",
+ rc, ldap_err2string(rc));
+# if defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP)
+ if (ldap_ssl && (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR))
+ {
+ ssl_err = PORT_GetError();
+ if (ssl_err != 0)
+ cupsdLogMessage(CUPSD_LOG_ERROR, "LDAP SSL error %d: %s", ssl_err,
+ ldapssl_err2string(ssl_err));
+ }
+# endif /* defined(HAVE_LDAP_SSL) && defined (HAVE_MOZILLA_LDAP) */
+
+ ldap_disconnect(TempBrowseLDAPHandle);
+
+ return (NULL);
+ }
+
+ cupsdLogMessage(CUPSD_LOG_INFO, "LDAP connection established");
+
+ return (TempBrowseLDAPHandle);
+}
+
+
+/*
+ * 'ldap_reconnect()' - Reconnect to LDAP Server
+ */
+
+static LDAP * /* O - New LDAP handle */
+ldap_reconnect(void)
+{
+ LDAP *TempBrowseLDAPHandle = NULL; /* Temp Handle to LDAP server */
+
+
+ /*
+ * Get a new LDAP Handle and replace the global Handle
+ * if the new connection was successful.
+ */
+
+ cupsdLogMessage(CUPSD_LOG_INFO, "Try LDAP reconnect...");
+
+ TempBrowseLDAPHandle = ldap_connect();
+
+ if (TempBrowseLDAPHandle != NULL)
+ {
+ if (BrowseLDAPHandle != NULL)
+ ldap_disconnect(BrowseLDAPHandle);
+
+ BrowseLDAPHandle = TempBrowseLDAPHandle;
+ }
+
+ return (BrowseLDAPHandle);
+}
+
+
+/*
+ * 'ldap_disconnect()' - Disconnect from LDAP Server
+ */
+
+static void
+ldap_disconnect(LDAP *ld) /* I - LDAP handle */
+{
+ int rc; /* Return code */
+
+
+ /*
+ * Close LDAP handle...
+ */
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ rc = ldap_unbind_ext_s(ld, NULL, NULL);
+# else
+ rc = ldap_unbind_s(ld);
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+
+ if (rc != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unbind from LDAP server failed with status %d: %s",
+ rc, ldap_err2string(rc));
+}
+#endif /* HAVE_LDAP */
+
+
+/*
+ * 'cupsdStartBrowsing()' - Start sending and receiving broadcast information.
+ */
+
+void
+cupsdStartBrowsing(void)
+{
+ int val; /* Socket option value */
+ struct sockaddr_in addr; /* Broadcast address */
+ cupsd_printer_t *p; /* Current printer */
+
+
+ BrowseNext = NULL;
+
+ if (!Browsing || !(BrowseLocalProtocols | BrowseRemoteProtocols))
+ return;
+
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_CUPS)
+ {
+ if (BrowseSocket < 0)
+ {
/*
- * Set the certificate file to use for encrypted LDAP sessions...
+ * Create the broadcast socket...
*/
- if (BrowseLDAPCACertFile)
+ if ((BrowseSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "cupsdStartBrowsing: Setting CA certificate file \"%s\"",
- BrowseLDAPCACertFile);
-
- if ((rc = ldap_set_option(NULL, LDAP_OPT_X_TLS_CACERTFILE,
- (void *)BrowseLDAPCACertFile))
- != LDAP_SUCCESS)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to set CA certificate file for LDAP "
- "connections: %d - %s", rc, ldap_err2string(rc));
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create broadcast socket - %s.",
+ strerror(errno));
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
}
+ }
+ if (BrowseSocket >= 0)
+ {
/*
- * LDAP stuff currently only supports ldapi EXTERNAL SASL binds...
+ * Bind the socket to browse port...
*/
- if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
- rc = ldap_initialize(&BrowseLDAPHandle, "ldapi:///");
- else
- rc = ldap_initialize(&BrowseLDAPHandle, BrowseLDAPServer);
+ memset(&addr, 0, sizeof(addr));
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(BrowsePort);
- if (rc != LDAP_SUCCESS)
+ if (bind(BrowseSocket, (struct sockaddr *)&addr, sizeof(addr)))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to initialize LDAP; disabling LDAP browsing!");
- BrowseLocalProtocols &= ~BROWSE_LDAP;
- BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ "Unable to bind broadcast socket - %s.",
+ strerror(errno));
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
}
- else if (ldap_set_option(BrowseLDAPHandle, LDAP_OPT_PROTOCOL_VERSION,
- (const void *)&version) != LDAP_SUCCESS)
+ }
+
+ if (BrowseSocket >= 0)
+ {
+ /*
+ * Set the "broadcast" flag...
+ */
+
+ val = 1;
+ if (setsockopt(BrowseSocket, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)))
{
- ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
- BrowseLDAPHandle = NULL;
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to set LDAP protocol version; "
- "disabling LDAP browsing!");
- BrowseLocalProtocols &= ~BROWSE_LDAP;
- BrowseRemoteProtocols &= ~BROWSE_LDAP;
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to set broadcast mode - %s.",
+ strerror(errno));
+
+#ifdef WIN32
+ closesocket(BrowseSocket);
+#else
+ close(BrowseSocket);
+#endif /* WIN32 */
+
+ BrowseSocket = -1;
+ BrowseLocalProtocols &= ~BROWSE_CUPS;
+ BrowseRemoteProtocols &= ~BROWSE_CUPS;
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
}
- else
+ }
+
+ if (BrowseSocket >= 0)
+ {
+ /*
+ * Close the socket on exec...
+ */
+
+ fcntl(BrowseSocket, F_SETFD, fcntl(BrowseSocket, F_GETFD) | FD_CLOEXEC);
+
+ /*
+ * Finally, add the socket to the input selection set as needed...
+ */
+
+ if (BrowseRemoteProtocols & BROWSE_CUPS)
{
- if (!BrowseLDAPServer || !strcasecmp(BrowseLDAPServer, "localhost"))
- rc = ldap_sasl_bind_s(BrowseLDAPHandle, NULL, "EXTERNAL", &bv, NULL,
- NULL, NULL);
- else
- rc = ldap_bind_s(BrowseLDAPHandle, BrowseLDAPBindDN,
- BrowseLDAPPassword, LDAP_AUTH_SIMPLE);
+ /*
+ * We only listen if we want remote printers...
+ */
- if (rc != LDAP_SUCCESS)
- {
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "Unable to bind to LDAP server; "
- "disabling LDAP browsing!");
- ldap_unbind_ext(BrowseLDAPHandle, NULL, NULL);
- BrowseLocalProtocols &= ~BROWSE_LDAP;
- BrowseRemoteProtocols &= ~BROWSE_LDAP;
- }
+ cupsdAddSelect(BrowseSocket, (cupsd_selfunc_t)update_cups_browse,
+ NULL, NULL);
+ }
+ }
+ }
+ else
+ BrowseSocket = -1;
+
+#ifdef HAVE_DNSSD
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_DNSSD)
+ {
+ DNSServiceErrorType error; /* Error from service creation */
+ cupsd_listener_t *lis; /* Current listening socket */
+
+
+ /*
+ * First create a "master" connection for all registrations...
+ */
+
+ if ((error = DNSServiceCreateConnection(&DNSSDRef))
+ != kDNSServiceErr_NoError)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to create master DNS-SD reference: %d", error);
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
+ }
+ else
+ {
+ /*
+ * Add the master connection to the select list...
+ */
+
+ int fd = DNSServiceRefSockFD(DNSSDRef);
+
+ fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
+
+ cupsdAddSelect(fd, (cupsd_selfunc_t)dnssdUpdate, NULL, NULL);
+
+ /*
+ * Then get the port we use for registrations. If we are not listening
+ * on any non-local ports, there is no sense sharing local printers via
+ * Bonjour...
+ */
+
+ DNSSDPort = 0;
+
+ for (lis = (cupsd_listener_t *)cupsArrayFirst(Listeners);
+ lis;
+ lis = (cupsd_listener_t *)cupsArrayNext(Listeners))
+ {
+ if (httpAddrLocalhost(&(lis->address)))
+ continue;
+
+ DNSSDPort = _httpAddrPort(&(lis->address));
+ break;
}
+
+ /*
+ * Create an array to track the printers we share...
+ */
+
+ if (BrowseRemoteProtocols & BROWSE_DNSSD)
+ DNSSDPrinters = cupsArrayNew((cups_array_func_t)dnssdComparePrinters,
+ NULL);
+
+ /*
+ * Set the computer name and register the web interface...
+ */
+
+ cupsdUpdateDNSSDName();
+ }
+ }
+#endif /* HAVE_DNSSD */
+
+#ifdef HAVE_LIBSLP
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_SLP)
+ {
+ /*
+ * Open SLP handle...
+ */
+
+ if (SLPOpen("en", SLP_FALSE, &BrowseSLPHandle) != SLP_OK)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to open an SLP handle; disabling SLP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_SLP;
+ BrowseRemoteProtocols &= ~BROWSE_SLP;
+ BrowseSLPHandle = NULL;
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
+ }
+
+ BrowseSLPRefresh = 0;
+ }
+ else
+ BrowseSLPHandle = NULL;
+#endif /* HAVE_LIBSLP */
+
+#ifdef HAVE_LDAP
+ if ((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP)
+ {
+ if (!BrowseLDAPDN)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Need to set BrowseLDAPDN to use LDAP browsing!");
+ BrowseLocalProtocols &= ~BROWSE_LDAP;
+ BrowseRemoteProtocols &= ~BROWSE_LDAP;
+
+ if (FatalErrors & CUPSD_FATAL_BROWSE)
+ cupsdEndProcess(getpid(), 0);
+ }
+ else
+ {
+ /*
+ * Open LDAP handle...
+ */
+
+ if ((BrowseLDAPHandle = ldap_connect()) == NULL &&
+ (FatalErrors & CUPSD_FATAL_BROWSE))
+ cupsdEndProcess(getpid(), 0);
}
BrowseLDAPRefresh = 0;
}
-#endif /* HAVE_OPENLDAP */
+#endif /* HAVE_LDAP */
/*
* Enable LPD and SMB printer sharing as needed through external programs...
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
- if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)))
cupsdRegisterPrinter(p);
}
argv[1] = pollp->hostname;
if (cupsdStartProcess(polld, argv, envp, -1, -1, statusfds[1], -1, -1,
- 0, DefaultProfile, &(pollp->pid)) < 0)
+ 0, DefaultProfile, NULL, &(pollp->pid)) < 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"cupsdStartPolling: Unable to fork polling daemon - %s",
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
- if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
+ if (!(p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT |
+ CUPS_PRINTER_SCANNER)))
cupsdDeregisterPrinter(p, 1);
/*
#ifdef HAVE_DNSSD
if ((BrowseLocalProtocols & BROWSE_DNSSD) && DNSSDRef)
- {
- if (WebIFRef)
- {
- DNSServiceRefDeallocate(WebIFRef);
- WebIFRef = NULL;
- }
-
- if (RemoteRef)
- {
- DNSServiceRefDeallocate(RemoteRef);
- RemoteRef = NULL;
- }
-
- cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef));
-
- DNSServiceRefDeallocate(DNSSDRef);
- DNSSDRef = NULL;
-
- cupsArrayDelete(DNSSDPrinters);
- DNSSDPrinters = NULL;
-
- DNSSDPort = 0;
- }
+ dnssdStop();
#endif /* HAVE_DNSSD */
#ifdef HAVE_LIBSLP
}
#endif /* HAVE_LIBSLP */
-#ifdef HAVE_OPENLDAP
+#ifdef HAVE_LDAP
if (((BrowseLocalProtocols | BrowseRemoteProtocols) & BROWSE_LDAP) &&
BrowseLDAPHandle)
{
- ldap_unbind(BrowseLDAPHandle);
+ ldap_dereg_ou(ServerName, BrowseLDAPDN);
+ ldap_disconnect(BrowseLDAPHandle);
BrowseLDAPHandle = NULL;
}
#endif /* HAVE_OPENLDAP */
#ifdef HAVE_DNSSD
-/*
- * 'cupsdUpdateDNSSDBrowse()' - Handle DNS-SD queries.
- */
-
-void
-cupsdUpdateDNSSDBrowse(void)
-{
- DNSServiceErrorType sdErr; /* Service discovery error */
-
-
- if ((sdErr = DNSServiceProcessResult(DNSSDRef)) != kDNSServiceErr_NoError)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "DNS Service Discovery registration error %d!",
- sdErr);
-}
-
-
/*
* 'cupsdUpdateDNSSDName()' - Update the computer name we use for browsing...
*/
{
DNSServiceErrorType error; /* Error from service creation */
char webif[1024]; /* Web interface share name */
-#ifdef HAVE_COREFOUNDATION_H
- CFStringRef nameRef; /* Computer name CFString */
+# ifdef HAVE_SYSTEMCONFIGURATION
+ SCDynamicStoreRef sc; /* Context for dynamic store */
+ CFDictionaryRef btmm; /* Back-to-My-Mac domains */
+ CFStringEncoding nameEncoding; /* Encoding of computer name */
+ CFStringRef nameRef; /* Host name CFString */
char nameBuffer[1024]; /* C-string buffer */
- CFStringEncoding nameEncoding; /* Computer name encoding */
-#endif /* HAVE_COREFOUNDATION_H */
+# endif /* HAVE_SYSTEMCONFIGURATION */
/*
* enabled...
*/
+
if (!DNSSDPort)
return;
* Get the computer name as a c-string...
*/
-#ifdef HAVE_COREFOUNDATION_H
- cupsdClearString(&DNSSDName);
+# ifdef HAVE_SYSTEMCONFIGURATION
+ sc = SCDynamicStoreCreate(kCFAllocatorDefault, CFSTR("cupsd"), NULL, NULL);
- if ((nameRef = SCDynamicStoreCopyComputerName(NULL,
- &nameEncoding)) != NULL)
+ if (sc)
{
- if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
- kCFStringEncodingUTF8))
- cupsdSetString(&DNSSDName, nameBuffer);
+ /*
+ * Get the computer name from the dynamic store...
+ */
- CFRelease(nameRef);
- }
+ cupsdClearString(&DNSSDComputerName);
-#else
- cupsdSetString(&DNSSDName, ServerName);
-#endif /* HAVE_COREFOUNDATION_H */
+ if ((nameRef = SCDynamicStoreCopyComputerName(sc, &nameEncoding)) != NULL)
+ {
+ if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
+ kCFStringEncodingUTF8))
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Dynamic store computer name is \"%s\".", nameBuffer);
+ cupsdSetString(&DNSSDComputerName, nameBuffer);
+ }
+
+ CFRelease(nameRef);
+ }
+
+ if (!DNSSDComputerName)
+ {
+ /*
+ * Use the ServerName instead...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Using ServerName \"%s\" as computer name.", ServerName);
+ cupsdSetString(&DNSSDComputerName, ServerName);
+ }
+
+ /*
+ * Get the local hostname from the dynamic store...
+ */
+
+ cupsdClearString(&DNSSDHostName);
+
+ if ((nameRef = SCDynamicStoreCopyLocalHostName(sc)) != NULL)
+ {
+ if (CFStringGetCString(nameRef, nameBuffer, sizeof(nameBuffer),
+ kCFStringEncodingUTF8))
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Dynamic store host name is \"%s\".", nameBuffer);
+ cupsdSetString(&DNSSDHostName, nameBuffer);
+ }
+
+ CFRelease(nameRef);
+ }
+
+ if (!DNSSDHostName)
+ {
+ /*
+ * Use the ServerName instead...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Using ServerName \"%s\" as host name.", ServerName);
+ cupsdSetString(&DNSSDHostName, ServerName);
+ }
+
+ /*
+ * Get any Back-to-My-Mac domains and add them as aliases...
+ */
+
+ cupsdFreeAliases(DNSSDAlias);
+ DNSSDAlias = NULL;
+
+ btmm = SCDynamicStoreCopyValue(sc, CFSTR("Setup:/Network/BackToMyMac"));
+ if (btmm && CFGetTypeID(btmm) == CFDictionaryGetTypeID())
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "%d Back to My Mac aliases to add.",
+ (int)CFDictionaryGetCount(btmm));
+ CFDictionaryApplyFunction(btmm, dnssdAddAlias, NULL);
+ }
+ else if (btmm)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Bad Back to My Mac data in dynamic store!");
+ else
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "No Back to My Mac aliases to add.");
+
+ if (btmm)
+ CFRelease(btmm);
+
+ CFRelease(sc);
+ }
+ else
+# endif /* HAVE_SYSTEMCONFIGURATION */
+ {
+ cupsdSetString(&DNSSDComputerName, ServerName);
+ cupsdSetString(&DNSSDHostName, ServerName);
+ }
/*
- * Then (re)register the web interface...
+ * Then (re)register the web interface if enabled...
*/
- if (DNSSDName)
- snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDName);
- else
- strlcpy(webif, "CUPS Web Interface", sizeof(webif));
+ if (BrowseWebIF)
+ {
+ if (DNSSDComputerName)
+ snprintf(webif, sizeof(webif), "CUPS @ %s", DNSSDComputerName);
+ else
+ strlcpy(webif, "CUPS Web Interface", sizeof(webif));
- if (WebIFRef)
- DNSServiceRefDeallocate(WebIFRef);
+ if (WebIFRef)
+ DNSServiceRefDeallocate(WebIFRef);
- WebIFRef = DNSSDRef;
- if ((error = DNSServiceRegister(&WebIFRef,
- kDNSServiceFlagsShareConnection,
- 0, webif, "_http._tcp", NULL,
- NULL, htons(DNSSDPort), 7,
- "\006path=/", dnssdRegisterCallback,
- NULL)) != kDNSServiceErr_NoError)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "DNS-SD web interface registration failed: %d", error);
+ WebIFRef = DNSSDRef;
+ if ((error = DNSServiceRegister(&WebIFRef,
+ kDNSServiceFlagsShareConnection,
+ 0, webif, "_http._tcp", NULL,
+ NULL, htons(DNSSDPort), 7,
+ "\006path=/", dnssdRegisterCallback,
+ NULL)) != kDNSServiceErr_NoError)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNS-SD web interface registration failed: %d", error);
+ }
}
#endif /* HAVE_DNSSD */
-#ifdef HAVE_OPENLDAP
+#ifdef HAVE_LDAP
/*
* 'cupsdUpdateLDAPBrowse()' - Scan for new printers via LDAP...
*/
location[1024], /* Printer location */
info[1024], /* Printer information */
make_model[1024], /* Printer make and model */
- **value; /* Holds the returned data from LDAP */
+ type_num[30]; /* Printer type number */
int type; /* Printer type */
int rc; /* LDAP status */
int limit; /* Size limit */
LDAPMessage *res, /* LDAP search results */
*e; /* Current entry from search */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
+
+ BrowseLDAPRefresh = time(NULL) + BrowseInterval;
/*
- * Search for printers...
+ * Reconnect if LDAP Handle is invalid...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "UpdateLDAPBrowse: %s", ServerName);
+ if (! BrowseLDAPHandle)
+ {
+ ldap_reconnect();
+ return;
+ }
- BrowseLDAPRefresh = time(NULL) + BrowseInterval;
+ /*
+ * Search for cups printers in LDAP directory...
+ */
+
+ rc = ldap_search_rec(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
+ "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
+
+ /*
+ * If ldap search was successfull then exit function
+ * and temporary disable LDAP updates...
+ */
- rc = ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
- "(objectclass=cupsPrinter)", (char **)ldap_attrs, 0, &res);
if (rc != LDAP_SUCCESS)
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "LDAP search returned error %d: %s", rc,
- ldap_err2string(rc));
+ if (BrowseLDAPUpdate && ((rc == LDAP_SERVER_DOWN) || (rc == LDAP_CONNECT_ERROR)))
+ {
+ BrowseLDAPUpdate = FALSE;
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "LDAP update temporary disabled");
+ }
return;
}
+ /*
+ * If LDAP updates were disabled, we will reenable them...
+ */
+
+ if (! BrowseLDAPUpdate)
+ {
+ BrowseLDAPUpdate = TRUE;
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "LDAP update enabled");
+ }
+
+ /*
+ * Count LDAP entries and return if no entry exist...
+ */
+
limit = ldap_count_entries(BrowseLDAPHandle, res);
cupsdLogMessage(CUPSD_LOG_DEBUG2, "LDAP search returned %d entries", limit);
if (limit < 1)
+ {
+ ldap_freeres(res);
return;
+ }
/*
* Loop through the available printers...
* Get the required values from this entry...
*/
- if ((value = ldap_get_values(BrowseLDAPHandle, e,
- "printerDescription")) == NULL)
+ if (ldap_getval_firststring(BrowseLDAPHandle, e,
+ "printerDescription", info, sizeof(info)) == -1)
continue;
- strlcpy(info, *value, sizeof(info));
- ldap_value_free(value);
-
- if ((value = ldap_get_values(BrowseLDAPHandle, e,
- "printerLocation")) == NULL)
+ if (ldap_getval_firststring(BrowseLDAPHandle, e,
+ "printerLocation", location, sizeof(location)) == -1)
continue;
- strlcpy(location, *value, sizeof(location));
- ldap_value_free(value);
-
- if ((value = ldap_get_values(BrowseLDAPHandle, e,
- "printerMakeAndModel")) == NULL)
+ if (ldap_getval_firststring(BrowseLDAPHandle, e,
+ "printerMakeAndModel", make_model, sizeof(make_model)) == -1)
continue;
- strlcpy(make_model, *value, sizeof(make_model));
- ldap_value_free(value);
-
- if ((value = ldap_get_values(BrowseLDAPHandle, e,
- "printerType")) == NULL)
+ if (ldap_getval_firststring(BrowseLDAPHandle, e,
+ "printerType", type_num, sizeof(type_num)) == -1)
continue;
- type = atoi(*value);
- ldap_value_free(value);
+ type = atoi(type_num);
- if ((value = ldap_get_values(BrowseLDAPHandle, e,
- "printerURI")) == NULL)
+ if (ldap_getval_firststring(BrowseLDAPHandle, e,
+ "printerURI", uri, sizeof(uri)) == -1)
continue;
- strlcpy(uri, *value, sizeof(uri));
- ldap_value_free(value);
-
/*
* Process the entry as browse data...
*/
location, info, make_model, 0, NULL);
}
+
+ ldap_freeres(res);
}
-#endif /* HAVE_OPENLDAP */
+#endif /* HAVE_LDAP */
#ifdef HAVE_LIBSLP
#ifdef HAVE_DNSSD
+# ifdef HAVE_COREFOUNDATION
+/*
+ * 'dnssdAddAlias()' - Add a DNS-SD alias name.
+ */
+
+static void
+dnssdAddAlias(const void *key, /* I - Key */
+ const void *value, /* I - Value (domain) */
+ void *context) /* I - Unused */
+{
+ char valueStr[1024], /* Domain string */
+ hostname[1024]; /* Complete hostname */
+
+
+ (void)context;
+
+ if (CFGetTypeID((CFStringRef)value) == CFStringGetTypeID() &&
+ CFStringGetCString((CFStringRef)value, valueStr, sizeof(valueStr),
+ kCFStringEncodingUTF8))
+ {
+ snprintf(hostname, sizeof(hostname), "%s.%s", DNSSDHostName, valueStr);
+ if (!DNSSDAlias)
+ DNSSDAlias = cupsArrayNew(NULL, NULL);
+
+ cupsdAddAlias(DNSSDAlias, hostname);
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added Back to My Mac ServerAlias %s",
+ hostname);
+ }
+ else
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Bad Back to My Mac domain in dynamic store!");
+}
+# endif /* HAVE_COREFOUNDATION */
+
+
/*
* 'dnssdBuildTxtRecord()' - Build a TXT record from printer info.
*/
cupsd_printer_t *p, /* I - Printer information */
int for_lpd) /* I - 1 = LPD, 0 = IPP */
{
- int i, j; /* Looping vars */
- char type_str[32], /* Type to string buffer */
+ int i; /* Looping var */
+ char admin_hostname[256], /* .local hostname for admin page */
+ adminurl_str[256], /* URL for the admin page */
+ type_str[32], /* Type to string buffer */
state_str[32], /* State to string buffer */
rp_str[1024], /* Queue name string buffer */
air_str[1024], /* auth-info-required string buffer */
(p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers", p->name);
keyvalue[i ][0] = "ty";
- keyvalue[i++][1] = p->make_model;
+ keyvalue[i++][1] = p->make_model ? p->make_model : "Unknown";
- if (p->location && *p->location != '\0')
- {
- keyvalue[i ][0] = "note";
- keyvalue[i++][1] = p->location;
- }
+ snprintf(admin_hostname, sizeof(admin_hostname), "%s.local.", DNSSDHostName);
+ httpAssembleURIf(HTTP_URI_CODING_ALL, adminurl_str, sizeof(adminurl_str),
+ "http", NULL, admin_hostname, DNSSDPort, "/%s/%s",
+ (p->type & CUPS_PRINTER_CLASS) ? "classes" : "printers",
+ p->name);
+ keyvalue[i ][0] = "adminurl";
+ keyvalue[i++][1] = adminurl_str;
+
+ keyvalue[i ][0] = "note";
+ keyvalue[i++][1] = p->location ? p->location : "";
keyvalue[i ][0] = "priority";
keyvalue[i++][1] = for_lpd ? "100" : "0";
keyvalue[i ][0] = "product";
- keyvalue[i++][1] = p->product ? p->product : "Unknown";
+ keyvalue[i++][1] = p->pc && p->pc->product ? p->pc->product : "Unknown";
- snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
- snprintf(state_str, sizeof(state_str), "%d", p->state);
-
- keyvalue[i ][0] = "printer-state";
- keyvalue[i++][1] = state_str;
-
- keyvalue[i ][0] = "printer-type";
- keyvalue[i++][1] = type_str;
-
- keyvalue[i ][0] = "Transparent";
- keyvalue[i++][1] = "T";
-
- keyvalue[i ][0] = "Binary";
- keyvalue[i++][1] = "T";
+ keyvalue[i ][0] = "pdl";
+ keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
- if ((p->type & CUPS_PRINTER_FAX))
+ if (get_auth_info_required(p, air_str, sizeof(air_str)))
{
- keyvalue[i ][0] = "Fax";
- keyvalue[i++][1] = "T";
+ keyvalue[i ][0] = "air";
+ keyvalue[i++][1] = air_str;
}
- if ((p->type & CUPS_PRINTER_COLOR))
- {
- keyvalue[i ][0] = "Color";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "UUID";
+ keyvalue[i++][1] = p->uuid + 9;
- if ((p->type & CUPS_PRINTER_DUPLEX))
- {
- keyvalue[i ][0] = "Duplex";
- keyvalue[i++][1] = "T";
- }
+#ifdef HAVE_SSL
+ keyvalue[i ][0] = "TLS";
+ keyvalue[i++][1] = "1.2";
+#endif /* HAVE_SSL */
- if ((p->type & CUPS_PRINTER_STAPLE))
- {
- keyvalue[i ][0] = "Staple";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Transparent";
+ keyvalue[i++][1] = "F";
- if ((p->type & CUPS_PRINTER_COPIES))
- {
- keyvalue[i ][0] = "Copies";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Binary";
+ keyvalue[i++][1] = "F";
- if ((p->type & CUPS_PRINTER_COLLATE))
- {
- keyvalue[i ][0] = "Collate";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Fax";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_FAX) ? "T" : "F";
- if ((p->type & CUPS_PRINTER_PUNCH))
- {
- keyvalue[i ][0] = "Punch";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Color";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLOR) ? "T" : "F";
- if ((p->type & CUPS_PRINTER_BIND))
- {
- keyvalue[i ][0] = "Bind";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Duplex";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_DUPLEX) ? "T" : "F";
- if ((p->type & CUPS_PRINTER_SORT))
- {
- keyvalue[i ][0] = "Sort";
- keyvalue[i++][1] = "T";
- }
+ keyvalue[i ][0] = "Staple";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_STAPLE) ? "T" : "F";
- keyvalue[i ][0] = "pdl";
- keyvalue[i++][1] = p->pdl ? p->pdl : "application/postscript";
+ keyvalue[i ][0] = "Copies";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_COPIES) ? "T" : "F";
- if (p->num_auth_info_required)
- {
- char *air = air_str; /* Pointer into string */
+ keyvalue[i ][0] = "Collate";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_COLLATE) ? "T" : "F";
+ keyvalue[i ][0] = "Punch";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_PUNCH) ? "T" : "F";
- for (j = 0; j < p->num_auth_info_required; j ++)
- {
- if (air >= (air_str + sizeof(air_str) - 2))
- break;
+ keyvalue[i ][0] = "Bind";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_BIND) ? "T" : "F";
- if (j)
- *air++ = ',';
+ keyvalue[i ][0] = "Sort";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_SORT) ? "T" : "F";
- strlcpy(air, p->auth_info_required[j], sizeof(air_str) - (air - air_str));
- air += strlen(air);
- }
+ keyvalue[i ][0] = "Scan";
+ keyvalue[i++][1] = (p->type & CUPS_PRINTER_MFP) ? "T" : "F";
- keyvalue[i ][0] = "air";
- keyvalue[i++][1] = air;
- }
+ snprintf(type_str, sizeof(type_str), "0x%X", p->type | CUPS_PRINTER_REMOTE);
+ snprintf(state_str, sizeof(state_str), "%d", p->state);
+
+ keyvalue[i ][0] = "printer-state";
+ keyvalue[i++][1] = state_str;
+
+ keyvalue[i ][0] = "printer-type";
+ keyvalue[i++][1] = type_str;
/*
* Then pack them into a proper txt record...
p->ipp_ref = NULL;
}
- cupsArrayRemove(DNSSDPrinters, p);
- cupsdClearString(&p->reg_name);
-
if (p->ipp_txt)
{
/*
free(p->ipp_txt);
p->ipp_txt = NULL;
}
+
+ if (p->printer_ref)
+ {
+ DNSServiceRefDeallocate(p->printer_ref);
+ p->printer_ref = NULL;
+ }
+
+ if (p->printer_txt)
+ {
+ /*
+ * p->printer_txt is malloc'd, not _cupsStrAlloc'd...
+ */
+
+ free(p->printer_txt);
+ p->printer_txt = NULL;
+ }
+
+ /*
+ * Remove the printer from the array of DNS-SD printers, then clear the
+ * registered name...
+ */
+
+ cupsArrayRemove(DNSSDPrinters, p);
+ cupsdClearString(&p->reg_name);
}
* Calculate the buffer size
*/
+ if (count <= 0)
+ return (NULL);
+
for (length = i = 0; i < count; i++)
length += 1 + strlen(keyvalue[i][0]) +
(keyvalue[i][1] ? 1 + strlen(keyvalue[i][1]) : 0);
/* Current printer */
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s",
- name, regtype, p ? p->name : "Web Interface");
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterCallback(%s, %s) for %s (%s)",
+ name, regtype, p ? p->name : "Web Interface",
+ p ? (p->reg_name ? p->reg_name : "(null)") : "NA");
if (errorCode)
{
"DNSServiceRegister failed with error %d", (int)errorCode);
return;
}
- else if (p && strcasecmp(name, p->reg_name))
+ else if (p && (!p->reg_name || strcasecmp(name, p->reg_name)))
{
cupsdLogMessage(CUPSD_LOG_INFO, "Using service name \"%s\" for \"%s\"",
name, p->name);
name[1024], /* Service name */
*nameptr; /* Pointer into name */
int ipp_len, /* IPP TXT record length */
- printer_len; /* LPD TXT record length */
- char resource[1024]; /* Resource path for printer */
+ printer_len, /* LPD TXT record length */
+ printer_port; /* LPD port number */
const char *regtype; /* Registration type */
- const char *domain; /* Registration domain */
- cupsd_location_t *location, /* Printer location */
- *policy; /* Operation policy for Print-Job */
- unsigned address[4]; /* INADDR_ANY address */
cupsdLogMessage(CUPSD_LOG_DEBUG2, "dnssdRegisterPrinter(%s) %s", p->name,
if (p->info && strlen(p->info) > 0)
{
- if (DNSSDName)
- snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDName);
+ if (DNSSDComputerName)
+ snprintf(name, sizeof(name), "%s @ %s", p->info, DNSSDComputerName);
else
strlcpy(name, p->info, sizeof(name));
}
- else if (DNSSDName)
- snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDName);
+ else if (DNSSDComputerName)
+ snprintf(name, sizeof(name), "%s @ %s", p->name, DNSSDComputerName);
else
strlcpy(name, p->name, sizeof(name));
}
/*
- * If 'Allow printing from the Internet' is enabled (i.e. from any address)
- * let dnssd decide on the domain, otherwise restrict it to ".local".
+ * Register IPP and (optionally) LPD...
*/
- if (p->type & CUPS_PRINTER_CLASS)
- snprintf(resource, sizeof(resource), "/classes/%s", p->name);
- else
- snprintf(resource, sizeof(resource), "/printers/%s", p->name);
+ ipp_len = 0; /* anti-compiler-warning-code */
+ ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
+
+ if (p->ipp_ref &&
+ (ipp_len != p->ipp_len || memcmp(ipp_txt, p->ipp_txt, ipp_len)))
+ {
+ /*
+ * Update the existing registration...
+ */
- address[0] = address[1] = address[2] = address[3] = 0;
- location = cupsdFindBest(resource, HTTP_POST);
- policy = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
+ /* A TTL of 0 means use record's original value (Radar 3176248) */
+ if ((se = DNSServiceUpdateRecord(p->ipp_ref, NULL, 0, ipp_len, ipp_txt,
+ 0)) == kDNSServiceErr_NoError)
+ {
+ if (p->ipp_txt)
+ free(p->ipp_txt);
- if ((location && !cupsdCheckAccess(address, "", 0, location)) ||
- (policy && !cupsdCheckAccess(address, "", 0, policy)))
- domain = "local.";
- else
- domain = NULL;
+ p->ipp_txt = ipp_txt;
+ p->ipp_len = ipp_len;
+ ipp_txt = NULL;
+ }
+ else
+ {
+ /*
+ * Failed to update record, lets close this reference and move on...
+ */
- /*
- * Register IPP and (optionally) LPD...
- */
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to update IPP DNS-SD record for %s - %d", p->name,
+ se);
- ipp_len = 0; /* anti-compiler-warning-code */
- ipp_txt = dnssdBuildTxtRecord(&ipp_len, p, 0);
+ DNSServiceRefDeallocate(p->ipp_ref);
+ p->ipp_ref = NULL;
+ }
+ }
if (!p->ipp_ref)
{
/*
- * Initial registration. Use the _fax subtype for fax queues...
+ * Initial registration. Use the _fax-ipp regtype for fax queues...
*/
- regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" :
- "_ipp._tcp,_cups";
+ regtype = (p->type & CUPS_PRINTER_FAX) ? "_fax-ipp._tcp" : DNSSDRegType;
cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Registering DNS-SD printer %s with name \"%s\", "
- "type \"%s\", and domain \"%s\"", p->name, name, regtype,
- domain ? domain : "(null)");
+ "Registering DNS-SD printer %s with name \"%s\" and "
+ "type \"%s\"", p->name, name, regtype);
/*
* Register the queue, dropping characters as needed until we succeed...
{
p->ipp_ref = DNSSDRef;
if ((se = DNSServiceRegister(&p->ipp_ref, kDNSServiceFlagsShareConnection,
- 0, name, regtype, domain, NULL,
+ 0, name, regtype, NULL, NULL,
htons(DNSSDPort), ipp_len, ipp_txt,
dnssdRegisterCallback,
p)) == kDNSServiceErr_BadParam)
"DNS-SD IPP registration of \"%s\" failed: %d",
p->name, se);
}
- else if (ipp_len != p->ipp_len || memcmp(ipp_txt, p->ipp_txt, ipp_len))
- {
- /*
- * Update the existing registration...
- */
-
- /* A TTL of 0 means use record's original value (Radar 3176248) */
- DNSServiceUpdateRecord(p->ipp_ref, NULL, 0, ipp_len, ipp_txt, 0);
-
- if (p->ipp_txt)
- free(p->ipp_txt);
-
- p->ipp_txt = ipp_txt;
- p->ipp_len = ipp_len;
- ipp_txt = NULL;
- }
if (ipp_txt)
free(ipp_txt);
if (BrowseLocalProtocols & BROWSE_LPD)
{
- printer_len = 0; /* anti-compiler-warning-code */
- printer_txt = dnssdBuildTxtRecord(&printer_len, p, 1);
+ printer_len = 0; /* anti-compiler-warning-code */
+ printer_port = 515;
+ printer_txt = dnssdBuildTxtRecord(&printer_len, p, 1);
+ }
+ else
+ {
+ printer_len = 0;
+ printer_port = 0;
+ printer_txt = NULL;
+ }
+
+ if (p->printer_ref &&
+ (printer_len != p->printer_len ||
+ memcmp(printer_txt, p->printer_txt, printer_len)))
+ {
+ /*
+ * Update the existing registration...
+ */
- if (!p->printer_ref)
+ /* A TTL of 0 means use record's original value (Radar 3176248) */
+ if ((se = DNSServiceUpdateRecord(p->printer_ref, NULL, 0, printer_len,
+ printer_txt,
+ 0)) == kDNSServiceErr_NoError)
{
- /*
- * Initial registration...
- */
-
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Registering DNS-SD printer %s with name \"%s\", "
- "type \"_printer._tcp\", and domain \"%s\"", p->name,
- name, domain ? domain : "(null)");
+ if (p->printer_txt)
+ free(p->printer_txt);
- p->printer_ref = DNSSDRef;
- if ((se = DNSServiceRegister(&p->printer_ref,
- kDNSServiceFlagsShareConnection,
- 0, name, "_printer._tcp", domain, NULL,
- htons(515), printer_len, printer_txt,
- dnssdRegisterCallback,
- p)) == kDNSServiceErr_NoError)
- {
- p->printer_txt = printer_txt;
- p->printer_len = printer_len;
- printer_txt = NULL;
- }
- else
- cupsdLogMessage(CUPSD_LOG_WARN,
- "DNS-SD LPD registration of \"%s\" failed: %d",
- p->name, se);
+ p->printer_txt = printer_txt;
+ p->printer_len = printer_len;
+ printer_txt = NULL;
}
- else if (printer_len != p->printer_len ||
- memcmp(printer_txt, p->printer_txt, printer_len))
+ else
{
/*
- * Update the existing registration...
+ * Failed to update record, lets close this reference and move on...
*/
- /* A TTL of 0 means use record's original value (Radar 3176248) */
- DNSServiceUpdateRecord(p->printer_ref, NULL, 0, printer_len,
- printer_txt, 0);
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "Unable to update LPD DNS-SD record for %s - %d",
+ p->name, se);
- if (p->printer_txt)
- free(p->printer_txt);
+ DNSServiceRefDeallocate(p->printer_ref);
+ p->printer_ref = NULL;
+ }
+ }
+
+ if (!p->printer_ref)
+ {
+ /*
+ * Initial registration...
+ */
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Registering DNS-SD printer %s with name \"%s\" and "
+ "type \"_printer._tcp\"", p->name, name);
+
+ p->printer_ref = DNSSDRef;
+ if ((se = DNSServiceRegister(&p->printer_ref,
+ kDNSServiceFlagsShareConnection,
+ 0, name, "_printer._tcp", NULL, NULL,
+ htons(printer_port), printer_len, printer_txt,
+ dnssdRegisterCallback,
+ p)) == kDNSServiceErr_NoError)
+ {
p->printer_txt = printer_txt;
p->printer_len = printer_len;
printer_txt = NULL;
}
+ else
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "DNS-SD LPD registration of \"%s\" failed: %d",
+ p->name, se);
+ }
+
+ if (printer_txt)
+ free(printer_txt);
+}
+
+
+/*
+ * 'dnssdStop()' - Stop all DNS-SD registrations.
+ */
+
+static void
+dnssdStop(void)
+{
+ cupsd_printer_t *p; /* Current printer */
+
+
+ /*
+ * De-register the individual printers
+ */
+
+ for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
+ p;
+ p = (cupsd_printer_t *)cupsArrayNext(Printers))
+ dnssdDeregisterPrinter(p);
+
+ /*
+ * Shutdown the rest of the service refs...
+ */
+
+ if (WebIFRef)
+ {
+ DNSServiceRefDeallocate(WebIFRef);
+ WebIFRef = NULL;
+ }
+
+ if (RemoteRef)
+ {
+ DNSServiceRefDeallocate(RemoteRef);
+ RemoteRef = NULL;
+ }
+
+ cupsdRemoveSelect(DNSServiceRefSockFD(DNSSDRef));
+
+ DNSServiceRefDeallocate(DNSSDRef);
+ DNSSDRef = NULL;
+
+ cupsArrayDelete(DNSSDPrinters);
+ DNSSDPrinters = NULL;
+
+ DNSSDPort = 0;
+}
+
+
+/*
+ * 'dnssdUpdate()' - Handle DNS-SD queries.
+ */
+
+static void
+dnssdUpdate(void)
+{
+ DNSServiceErrorType sdErr; /* Service discovery error */
- if (printer_txt)
- free(printer_txt);
+
+ if ((sdErr = DNSServiceProcessResult(DNSSDRef)) != kDNSServiceErr_NoError)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "DNS Service Discovery registration error %d!",
+ sdErr);
+ dnssdStop();
}
}
#endif /* HAVE_DNSSD */
+/*
+ * 'get_auth_info_required()' - Get the auth-info-required value to advertise.
+ */
+
+static char * /* O - String or NULL if none */
+get_auth_info_required(
+ cupsd_printer_t *p, /* I - Printer */
+ char *buffer, /* I - Value buffer */
+ size_t bufsize) /* I - Size of value buffer */
+{
+ cupsd_location_t *auth; /* Pointer to authentication element */
+ char resource[1024]; /* Printer/class resource path */
+
+
+ /*
+ * If auth-info-required is set for this printer, return that...
+ */
+
+ if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
+ {
+ int i; /* Looping var */
+ char *bufptr; /* Pointer into buffer */
+
+ for (i = 0, bufptr = buffer; i < p->num_auth_info_required; i ++)
+ {
+ if (bufptr >= (buffer + bufsize - 2))
+ break;
+
+ if (i)
+ *bufptr++ = ',';
+
+ strlcpy(bufptr, p->auth_info_required[i], bufsize - (bufptr - buffer));
+ bufptr += strlen(bufptr);
+ }
+
+ return (buffer);
+ }
+
+ /*
+ * Figure out the authentication data requirements to advertise...
+ */
+
+ if (p->type & CUPS_PRINTER_CLASS)
+ snprintf(resource, sizeof(resource), "/classes/%s", p->name);
+ else
+ snprintf(resource, sizeof(resource), "/printers/%s", p->name);
+
+ if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
+ auth->type == CUPSD_AUTH_NONE)
+ auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
+
+ if (auth)
+ {
+ int auth_type; /* Authentication type */
+
+ if ((auth_type = auth->type) == CUPSD_AUTH_DEFAULT)
+ auth_type = DefaultAuthType;
+
+ switch (auth_type)
+ {
+ case CUPSD_AUTH_NONE :
+ return (NULL);
+
+ case CUPSD_AUTH_NEGOTIATE :
+ strlcpy(buffer, "negotiate", bufsize);
+ break;
+
+ default :
+ strlcpy(buffer, "username,password", bufsize);
+ break;
+ }
+
+ return (buffer);
+ }
+
+ return ("none");
+}
+
+
#ifdef __APPLE__
/*
* 'get_hostconfig()' - Get an /etc/hostconfig service setting.
newname[IPP_MAX_NAME], /* New name of printer */
*hptr, /* Pointer into hostname */
*sptr; /* Pointer into ServerName */
+ const char *shortname; /* Short queue name (queue) */
char local_make_model[IPP_MAX_NAME];
/* Local make and model */
cupsd_printer_t *p; /* Printer information */
const char *ipp_options, /* ipp-options value */
- *lease_duration; /* lease-duration value */
+ *lease_duration, /* lease-duration value */
+ *uuid; /* uuid value */
+ int is_class; /* Is this queue a class? */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "process_browse_data(uri=\"%s\", host=\"%s\", "
+ "resource=\"%s\", type=%x, state=%d, location=\"%s\", "
+ "info=\"%s\", make_model=\"%s\", num_attrs=%d, attrs=%p)",
+ uri, host, resource, type, state,
+ location ? location : "(nil)", info ? info : "(nil)",
+ make_model ? make_model : "(nil)", num_attrs, attrs);
/*
* Determine if the URI contains any illegal characters in it...
(strncmp(resource, "/printers/", 10) &&
strncmp(resource, "/classes/", 9)))
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "process_browse_data: Bad printer URI in browse data: %s",
- uri);
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Bad printer URI in browse data: %s", uri);
return;
}
(!strncmp(resource, "/printers/", 10) && strchr(resource + 10, '/')) ||
(!strncmp(resource, "/classes/", 9) && strchr(resource + 9, '/')))
{
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "process_browse_data: Bad resource in browse data: %s",
+ cupsdLogMessage(CUPSD_LOG_ERROR, "Bad resource in browse data: %s",
resource);
return;
}
* See if we already have it listed in the Printers list, and add it if not...
*/
- type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
- type &= ~CUPS_PRINTER_IMPLICIT;
- update = 0;
- hptr = strchr(host, '.');
- sptr = strchr(ServerName, '.');
+ type |= CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED;
+ type &= ~CUPS_PRINTER_IMPLICIT;
+ update = 0;
+ hptr = strchr(host, '.');
+ sptr = strchr(ServerName, '.');
+ is_class = type & CUPS_PRINTER_CLASS;
+ uuid = cupsGetOption("uuid", num_attrs, attrs);
if (!ServerNameIsIP && sptr != NULL && hptr != NULL)
{
}
}
- if (type & CUPS_PRINTER_CLASS)
+ if (is_class)
{
/*
* Remote destination is a class...
else
return;
- if (hptr && !*hptr)
- *hptr = '.'; /* Resource FQDN */
-
- if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
- {
- if ((p = cupsdFindDest(resource + 9)) != NULL)
- {
- if (p->hostname && strcasecmp(p->hostname, host))
- {
- /*
- * Nope, this isn't the same host; if the hostname isn't the local host,
- * add it to the other class and then find a class using the full host
- * name...
- */
+ shortname = resource + 9;
+ }
+ else
+ {
+ /*
+ * Remote destination is a printer...
+ */
- if (p->type & CUPS_PRINTER_REMOTE)
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Renamed remote class \"%s\" to \"%s@%s\"...",
- p->name, p->name, p->hostname);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "Class \'%s\' deleted by directory services.",
- p->name);
-
- snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
- cupsdRenamePrinter(p, newname);
-
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.",
- p->name);
- }
+ if (!strncmp(resource, "/printers/", 10))
+ snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
+ else
+ return;
- p = NULL;
- }
- else if (!p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ shortname = resource + 10;
+ }
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
- }
- else
- {
- /*
- * Use the short name for this shared class.
- */
+ if (hptr && !*hptr)
+ *hptr = '.'; /* Resource FQDN */
- strlcpy(name, resource + 9, sizeof(name));
- }
- }
- else if (p && !p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
+ {
+ /*
+ * Long name doesn't exist, try short name...
+ */
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "process_browse_data: %s not found...",
+ name);
- if (!p)
+ if ((p = cupsdFindDest(shortname)) == NULL)
{
/*
- * Class doesn't exist; add it...
- */
-
- p = cupsdAddClass(name);
-
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote class \"%s\"...", name);
-
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Class \'%s\' added by directory services.", name);
-
- /*
- * Force the URI to point to the real server...
+ * Short name doesn't exist, use it for this shared queue.
*/
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- cupsdSetString(&p->hostname, host);
-
- update = 1;
-
- cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "process_browse_data: %s not found...",
+ shortname);
+ strlcpy(name, shortname, sizeof(name));
}
- }
- else
- {
- /*
- * Remote destination is a printer...
- */
-
- if (!strncmp(resource, "/printers/", 10))
- snprintf(name, sizeof(name), "%s@%s", resource + 10, host);
else
- return;
+ {
+ /*
+ * Short name exists...
+ */
- if (hptr && !*hptr)
- *hptr = '.'; /* Resource FQDN */
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "process_browse_data: %s found, type=%x, hostname=%s...",
+ shortname, p->type, p->hostname ? p->hostname : "(nil)");
- if ((p = cupsdFindDest(name)) == NULL && BrowseShortNames)
- {
- if ((p = cupsdFindDest(resource + 10)) != NULL)
+ if (p->type & CUPS_PRINTER_IMPLICIT)
+ p = NULL; /* Don't replace implicit classes */
+ else if (p->hostname && strcasecmp(p->hostname, host))
{
- if (p->hostname && strcasecmp(p->hostname, host))
- {
- /*
- * Nope, this isn't the same host; if the hostname isn't the local host,
- * add it to the other printer and then find a printer using the full host
- * name...
- */
-
- if (p->type & CUPS_PRINTER_REMOTE)
- {
- cupsdLogMessage(CUPSD_LOG_DEBUG,
- "Renamed remote printer \"%s\" to \"%s@%s\"...",
- p->name, p->name, p->hostname);
- cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
- "Printer \'%s\' deleted by directory services.",
- p->name);
-
- snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
- cupsdRenamePrinter(p, newname);
-
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.",
- p->name);
- }
+ /*
+ * Short name exists but is for a different host. If this is a remote
+ * queue, rename it and use the long name...
+ */
- p = NULL;
- }
- else if (!p->hostname)
+ if (p->type & CUPS_PRINTER_REMOTE)
{
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "Renamed remote %s \"%s\" to \"%s@%s\"...",
+ is_class ? "class" : "printer", p->name, p->name,
+ p->hostname);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
+ "%s \'%s\' deleted by directory services.",
+ is_class ? "Class" : "Printer", p->name);
+
+ snprintf(newname, sizeof(newname), "%s@%s", p->name, p->hostname);
+ cupsdRenamePrinter(p, newname);
+
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "%s \'%s\' added by directory services.",
+ is_class ? "Class" : "Printer", p->name);
+ }
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
- }
- else
- {
/*
- * Use the short name for this shared printer.
+ * Force creation with long name...
*/
- strlcpy(name, resource + 10, sizeof(name));
+ p = NULL;
}
}
- else if (p && !p->hostname)
- {
- /*
- * Hostname not set, so this must be a cached remote printer
- * that was created for a pending print job...
- */
+ }
+ else if (p)
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "process_browse_data: %s found, type=%x, hostname=%s...",
+ name, p->type, p->hostname ? p->hostname : "(nil)");
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
- update = 1;
- }
+ if (!p)
+ {
+ /*
+ * Queue doesn't exist; add it...
+ */
+
+ if (is_class)
+ p = cupsdAddClass(name);
+ else
+ p = cupsdAddPrinter(name);
if (!p)
- {
- /*
- * Printer doesn't exist; add it...
- */
+ return;
- p = cupsdAddPrinter(name);
+ cupsdClearString(&(p->hostname));
- cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
- "Printer \'%s\' added by directory services.", name);
+ cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote %s \"%s\"...",
+ is_class ? "class" : "printer", name);
- cupsdLogMessage(CUPSD_LOG_DEBUG, "Added remote printer \"%s\"...", name);
+ cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, p, NULL,
+ "%s \'%s\' added by directory services.",
+ is_class ? "Class" : "Printer", name);
- /*
- * Force the URI to point to the real server...
- */
+ /*
+ * Force the URI to point to the real server...
+ */
- p->type = type & ~CUPS_PRINTER_REJECTING;
- p->accepting = 1;
- cupsdSetString(&p->hostname, host);
- cupsdSetString(&p->uri, uri);
- cupsdSetString(&p->device_uri, uri);
+ p->type = type & ~CUPS_PRINTER_REJECTING;
+ p->accepting = 1;
- update = 1;
+ cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
+ }
- cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP | CUPSD_DIRTY_REMOTE);
- }
+ if (!p->hostname)
+ {
+ /*
+ * Hostname not set, so this must be a cached remote printer
+ * that was created for a pending print job...
+ */
+
+ cupsdSetString(&p->hostname, host);
+ cupsdSetString(&p->uri, uri);
+ cupsdSetString(&p->device_uri, uri);
+ update = 1;
+
+ cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
}
/*
update = 1;
}
+ if (uuid && strcmp(p->uuid, uuid))
+ {
+ cupsdSetString(&p->uuid, uuid);
+ update = 1;
+ }
+
if (location && (!p->location || strcmp(p->location, location)))
{
cupsdSetString(&p->location, location);
if (!make_model || !make_model[0])
{
- if (type & CUPS_PRINTER_CLASS)
+ if (is_class)
snprintf(local_make_model, sizeof(local_make_model),
"Remote Class on %s", host);
else
{
cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, p, NULL,
"%s \'%s\' deleted by directory services.",
- (type & CUPS_PRINTER_CLASS) ? "Class" : "Printer", p->name);
+ is_class ? "Class" : "Printer", p->name);
cupsdExpireSubscriptions(p, NULL);
else
len = strlen(p->name);
+ if (len >= sizeof(name))
+ {
+ /*
+ * If the printer name length somehow is greater than we normally allow,
+ * skip this printer...
+ */
+
+ len = 0;
+ cupsArrayRestore(Printers);
+ continue;
+ }
+
strncpy(name, p->name, len);
name[len] = '\0';
offset = 0;
uri[1024], /* Printer URI */
location[1024], /* printer-location */
info[1024], /* printer-info */
- make_model[1024];
+ make_model[1024],
/* printer-make-and-model */
+ air[1024]; /* auth-info-required */
cupsd_netif_t *iface; /* Network interface */
else
strlcpy(make_model, "Local System V Printer", sizeof(make_model));
+ if (get_auth_info_required(p, packet, sizeof(packet)))
+ snprintf(air, sizeof(air), " auth-info-required=%s", packet);
+ else
+ air[0] = '\0';
+
/*
* Send a packet to each browse address...
*/
iface->address.addr.sa_family != AF_INET)
continue;
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet),
+ "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "", air, p->uuid);
+
+ bytes = strlen(packet);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
+
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ httpAddrLength(&(iface->broadcast)));
+ }
+ }
+ else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
+ {
+ /*
+ * Send to the named interface using the IPv4 address...
+ */
+
+ while (iface)
+ if (strcmp(b->iface, iface->name))
+ {
+ iface = NULL;
+ break;
+ }
+ else if (iface->address.addr.sa_family == AF_INET && iface->port)
+ break;
+ else
+ iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
+
+ if (iface)
+ {
+ httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
+ iface->hostname, iface->port,
+ (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
+ "/printers/%s",
+ p->name);
+ snprintf(packet, sizeof(packet),
+ "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
+ type, p->state, uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "", air, p->uuid);
+
+ bytes = strlen(packet);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
+ iface->name, packet);
+
+ iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+
+ sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(iface->broadcast),
+ httpAddrLength(&(iface->broadcast)));
+ }
+ }
+ }
+ else
+ {
+ /*
+ * Send the browse packet to the indicated address using
+ * the default server name...
+ */
+
+ snprintf(packet, sizeof(packet),
+ "%x %x %s \"%s\" \"%s\" \"%s\" %s%s uuid=%s\n",
+ type, p->state, p->uri, location, info, make_model,
+ p->browse_attrs ? p->browse_attrs : "", air, p->uuid);
+
+ bytes = strlen(packet);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
+
+ if (sendto(BrowseSocket, packet, bytes, 0,
+ (struct sockaddr *)&(b->to),
+ httpAddrLength(&(b->to))) <= 0)
+ {
+ /*
+ * Unable to send browse packet, so remove this address from the
+ * list...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "cupsdSendBrowseList: sendto failed for browser "
+ "%d - %s.",
+ (int)(b - Browsers + 1), strerror(errno));
+
+ if (i > 1)
+ memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+
+ b --;
+ NumBrowsers --;
+ }
+ }
+}
+
+
+#ifdef HAVE_LDAP
+/*
+ * 'ldap_search_rec()' - LDAP Search with reconnect
+ */
+
+static int /* O - Return code */
+ldap_search_rec(LDAP *ld, /* I - LDAP handler */
+ char *base, /* I - Base dn */
+ int scope, /* I - LDAP search scope */
+ char *filter, /* I - Filter string */
+ char *attrs[], /* I - Requested attributes */
+ int attrsonly, /* I - Return only attributes? */
+ LDAPMessage **res) /* I - LDAP handler */
+{
+ int rc; /* Return code */
+ LDAP *ldr; /* LDAP handler after reconnect */
+
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ rc = ldap_search_ext_s(ld, base, scope, filter, attrs, attrsonly, NULL, NULL,
+ NULL, LDAP_NO_LIMIT, res);
+# else
+ rc = ldap_search_s(ld, base, scope, filter, attrs, attrsonly, res);
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+
+ /*
+ * If we have a connection problem try again...
+ */
+
+ if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP search failed with status %d: %s",
+ rc, ldap_err2string(rc));
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "We try the LDAP search once again after reconnecting to "
+ "the server");
+ ldap_freeres(*res);
+ ldr = ldap_reconnect();
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ rc = ldap_search_ext_s(ldr, base, scope, filter, attrs, attrsonly, NULL,
+ NULL, NULL, LDAP_NO_LIMIT, res);
+# else
+ rc = ldap_search_s(ldr, base, scope, filter, attrs, attrsonly, res);
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ }
+
+ if (rc == LDAP_NO_SUCH_OBJECT)
+ cupsdLogMessage(CUPSD_LOG_DEBUG,
+ "ldap_search_rec: LDAP entry/object not found");
+ else if (rc != LDAP_SUCCESS)
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "ldap_search_rec: LDAP search failed with status %d: %s",
+ rc, ldap_err2string(rc));
+
+ if (rc != LDAP_SUCCESS)
+ ldap_freeres(*res);
+
+ return (rc);
+}
+
+
+/*
+ * 'ldap_freeres()' - Free LDAPMessage
+ */
+
+static void
+ldap_freeres(LDAPMessage *entry) /* I - LDAP handler */
+{
+ int rc; /* Return value */
+
+
+ rc = ldap_msgfree(entry);
+ if (rc == -1)
+ cupsdLogMessage(CUPSD_LOG_WARN, "Can't free LDAPMessage!");
+ else if (rc == 0)
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "Freeing LDAPMessage was unnecessary");
+}
+
+
+/*
+ * 'ldap_getval_char()' - Get first LDAP value and convert to string
+ */
+
+static int /* O - Return code */
+ldap_getval_firststring(
+ LDAP *ld, /* I - LDAP handler */
+ LDAPMessage *entry, /* I - LDAP message or search result */
+ char *attr, /* I - the wanted attribute */
+ char *retval, /* O - String to return */
+ unsigned long maxsize) /* I - Max string size */
+{
+ char *dn; /* LDAP DN */
+ int rc = 0; /* Return code */
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ struct berval **bval; /* LDAP value array */
+ unsigned long size; /* String size */
+
+
+ /*
+ * Get value from LDAPMessage...
+ */
+
+ if ((bval = ldap_get_values_len(ld, entry, attr)) == NULL)
+ {
+ rc = -1;
+ dn = ldap_get_dn(ld, entry);
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "Failed to get LDAP value %s for %s!",
+ attr, dn);
+ ldap_memfree(dn);
+ }
+ else
+ {
+ /*
+ * Check size and copy value into our string...
+ */
+
+ size = maxsize;
+ if (size < (bval[0]->bv_len + 1))
+ {
+ rc = -1;
+ dn = ldap_get_dn(ld, entry);
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "Attribute %s is too big! (dn: %s)",
+ attr, dn);
+ ldap_memfree(dn);
+ }
+ else
+ size = bval[0]->bv_len + 1;
+
+ strlcpy(retval, bval[0]->bv_val, size);
+ ldap_value_free_len(bval);
+ }
+# else
+ char **value; /* LDAP value */
+
+ /*
+ * Get value from LDAPMessage...
+ */
+
+ if ((value = (char **)ldap_get_values(ld, entry, attr)) == NULL)
+ {
+ rc = -1;
+ dn = ldap_get_dn(ld, entry);
+ cupsdLogMessage(CUPSD_LOG_WARN, "Failed to get LDAP value %s for %s!",
+ attr, dn);
+ ldap_memfree(dn);
+ }
+ else
+ {
+ strlcpy(retval, *value, maxsize);
+ ldap_value_free(value);
+ }
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+
+ return (rc);
+}
+
+
+/*
+ * 'send_ldap_ou()' - Send LDAP ou registrations.
+ */
+
+static void
+send_ldap_ou(char *ou, /* I - Servername/ou to register */
+ char *basedn, /* I - Our base dn */
+ char *descstring) /* I - Description for ou */
+{
+ int i; /* Looping var... */
+ LDAPMod mods[3]; /* The 3 attributes we will be adding */
+ LDAPMod *pmods[4]; /* Pointers to the 3 attributes + NULL */
+ LDAPMessage *res, /* Search result token */
+ *e; /* Current entry from search */
+ int rc; /* LDAP status */
+ int rcmod; /* LDAP status for modifications */
+ char dn[1024], /* DN of the organizational unit we are adding */
+ *desc[2], /* Change records */
+ *ou_value[2];
+ char old_desc[1024]; /* Old description */
+ static const char * const objectClass_values[] =
+ { /* The 2 objectClass's we use in */
+ "top", /* our LDAP entries */
+ "organizationalUnit",
+ NULL
+ };
+ static const char * const ou_attrs[] =/* CUPS LDAP attributes */
+ {
+ "description",
+ NULL
+ };
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: %s", ou);
+
+ /*
+ * Reconnect if LDAP Handle is invalid...
+ */
+
+ if (!BrowseLDAPHandle)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_ou: LDAP Handle is invalid. Try reconnecting...");
+ ldap_reconnect();
+ return;
+ }
+
+ /*
+ * Prepare ldap search...
+ */
+
+ snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_ou: dn=\"%s\"", dn);
+
+ ou_value[0] = ou;
+ ou_value[1] = NULL;
+ desc[0] = descstring;
+ desc[1] = NULL;
+
+ mods[0].mod_type = "ou";
+ mods[0].mod_values = ou_value;
+ mods[1].mod_type = "description";
+ mods[1].mod_values = desc;
+ mods[2].mod_type = "objectClass";
+ mods[2].mod_values = (char **)objectClass_values;
- bytes = strlen(packet);
+ rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL,
+ (char **)ou_attrs, 0, &res);
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
+ /*
+ * If ldap search was not successfull then exit function...
+ */
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+ if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT)
+ return;
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- httpAddrLength(&(iface->broadcast)));
- }
- }
- else if ((iface = cupsdNetIFFind(b->iface)) != NULL)
- {
- /*
- * Send to the named interface using the IPv4 address...
- */
+ /*
+ * Check if we need to insert or update the LDAP entry...
+ */
- while (iface)
- if (strcmp(b->iface, iface->name))
- {
- iface = NULL;
- break;
- }
- else if (iface->address.addr.sa_family == AF_INET && iface->port)
- break;
- else
- iface = (cupsd_netif_t *)cupsArrayNext(NetIFList);
+ if (ldap_count_entries(BrowseLDAPHandle, res) > 0 &&
+ rc != LDAP_NO_SUCH_OBJECT)
+ {
+ /*
+ * Printserver has already been registered, check if
+ * modification is required...
+ */
- if (iface)
- {
- httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
- iface->hostname, iface->port,
- (p->type & CUPS_PRINTER_CLASS) ? "/classes/%s" :
- "/printers/%s",
- p->name);
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
+ e = ldap_first_entry(BrowseLDAPHandle, res);
- bytes = strlen(packet);
+ /*
+ * Get the required values from this entry...
+ */
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes to \"%s\") %s", bytes,
- iface->name, packet);
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "description", old_desc,
+ sizeof(old_desc)) == -1)
+ old_desc[0] = '\0';
- iface->broadcast.ipv4.sin_port = htons(BrowsePort);
+ /*
+ * Check if modification is required...
+ */
- sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(iface->broadcast),
- httpAddrLength(&(iface->broadcast)));
- }
- }
- }
- else
+ if ( strcmp(desc[0], old_desc) == 0 )
{
/*
- * Send the browse packet to the indicated address using
- * the default server name...
+ * LDAP entry for the printer exists.
+ * Printer has already been registered,
+ * no modifications required...
*/
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_ou: No updates required for %s", ou);
+ }
+ else
+ {
- snprintf(packet, sizeof(packet), "%x %x %s \"%s\" \"%s\" \"%s\" %s\n",
- type, p->state, p->uri, location, info, make_model,
- p->browse_attrs ? p->browse_attrs : "");
-
- bytes = strlen(packet);
cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "cupsdSendBrowseList: (%d bytes) %s", bytes, packet);
+ "send_ldap_ou: Replace entry for %s", ou);
- if (sendto(BrowseSocket, packet, bytes, 0,
- (struct sockaddr *)&(b->to),
- httpAddrLength(&(b->to))) <= 0)
+ for (i = 0; i < 3; i ++)
{
- /*
- * Unable to send browse packet, so remove this address from the
- * list...
- */
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_REPLACE;
+ }
+ pmods[i] = NULL;
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rcmod = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rcmod = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP modify for %s failed with status %d: %s",
+ ou, rcmod, ldap_err2string(rcmod));
+ if (rcmod == LDAP_SERVER_DOWN)
+ ldap_reconnect();
+ }
+ }
+ }
+ else
+ {
+ /*
+ * Printserver has never been registered,
+ * add registration...
+ */
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "cupsdSendBrowseList: sendto failed for browser "
- "%d - %s.",
- (int)(b - Browsers + 1), strerror(errno));
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_ou: Add entry for %s", ou);
- if (i > 1)
- memmove(b, b + 1, (i - 1) * sizeof(cupsd_dirsvc_addr_t));
+ for (i = 0; i < 3; i ++)
+ {
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_ADD;
+ }
+ pmods[i] = NULL;
- b --;
- NumBrowsers --;
- }
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rcmod = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rcmod = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP add for %s failed with status %d: %s",
+ ou, rcmod, ldap_err2string(rcmod));
+ if (rcmod == LDAP_SERVER_DOWN)
+ ldap_reconnect();
}
+ }
+
+ if (rc == LDAP_SUCCESS)
+ ldap_freeres(res);
}
-#ifdef HAVE_OPENLDAP
/*
* 'send_ldap_browse()' - Send LDAP printer registrations.
*/
int i; /* Looping var... */
LDAPMod mods[7]; /* The 7 attributes we will be adding */
LDAPMod *pmods[8]; /* Pointers to the 7 attributes + NULL */
- LDAPMessage *res; /* Search result token */
+ LDAPMessage *res, /* Search result token */
+ *e; /* Current entry from search */
char *cn_value[2], /* Change records */
*uri[2],
*info[2],
*make_model[2],
*type[2],
typestring[255], /* String to hold printer-type */
- filter[256], /* Search filter for possible UPDATEs */
dn[1024]; /* DN of the printer we are adding */
int rc; /* LDAP status */
+ int rcmod; /* LDAP status for modifications */
+ char old_uri[HTTP_MAX_URI], /* Printer URI */
+ old_location[1024], /* Printer location */
+ old_info[1024], /* Printer information */
+ old_make_model[1024], /* Printer make and model */
+ old_type_string[30]; /* Temporary type number */
+ int old_type; /* Printer type */
static const char * const objectClass_values[] =
{ /* The 3 objectClass's we use in */
"top", /* our LDAP entries */
NULL
};
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s\n", p->name);
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: %s", p->name);
+
+ /*
+ * Exit function if LDAP updates has been disabled...
+ */
+
+ if (!BrowseLDAPUpdate)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Updates temporary disabled; "
+ "skipping...");
+ return;
+ }
+
+ /*
+ * Reconnect if LDAP Handle is invalid...
+ */
+
+ if (!BrowseLDAPHandle)
+ {
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: LDAP Handle is invalid. Try "
+ "reconnecting...");
+ ldap_reconnect();
+ return;
+ }
/*
* Everything in ldap is ** so we fudge around it...
uri[0] = p->uri;
uri[1] = NULL;
- snprintf(filter, sizeof(filter),
- "(&(objectclass=cupsPrinter)(printerURI=%s))", p->uri);
+ /*
+ * Get ldap entry for printer ...
+ */
+
+ snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName,
+ BrowseLDAPDN);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
+
+ rc = ldap_search_rec(BrowseLDAPHandle, dn, LDAP_SCOPE_BASE, NULL,
+ (char **)ldap_attrs, 0, &res);
+
+ /*
+ * If ldap search was not successfull then exit function
+ * and temporary disable LDAP updates...
+ */
- ldap_search_s(BrowseLDAPHandle, BrowseLDAPDN, LDAP_SCOPE_SUBTREE,
- filter, (char **)ldap_attrs, 0, &res);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: Searching \"%s\"",
- filter);
+ if (rc != LDAP_SUCCESS && rc != LDAP_NO_SUCH_OBJECT)
+ {
+ if (BrowseLDAPUpdate &&
+ (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR))
+ {
+ BrowseLDAPUpdate = FALSE;
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "LDAP update temporary disabled");
+ }
+
+ return;
+ }
+
+ /*
+ * Fill modification array...
+ */
- mods[0].mod_type = "cn";
+ mods[0].mod_type = "cn";
mods[0].mod_values = cn_value;
- mods[1].mod_type = "printerDescription";
+ mods[1].mod_type = "printerDescription";
mods[1].mod_values = info;
- mods[2].mod_type = "printerURI";
+ mods[2].mod_type = "printerURI";
mods[2].mod_values = uri;
- mods[3].mod_type = "printerLocation";
+ mods[3].mod_type = "printerLocation";
mods[3].mod_values = location;
- mods[4].mod_type = "printerMakeAndModel";
+ mods[4].mod_type = "printerMakeAndModel";
mods[4].mod_values = make_model;
- mods[5].mod_type = "printerType";
+ mods[5].mod_type = "printerType";
mods[5].mod_values = type;
- mods[6].mod_type = "objectClass";
+ mods[6].mod_type = "objectClass";
mods[6].mod_values = (char **)objectClass_values;
- snprintf(dn, sizeof(dn), "cn=%s,ou=printers,%s", p->name, BrowseLDAPDN);
- cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_ldap_browse: dn=\"%s\"", dn);
+ /*
+ * Check if we need to insert or update the LDAP entry...
+ */
- if (ldap_count_entries(BrowseLDAPHandle, res) > 0)
+ if (ldap_count_entries(BrowseLDAPHandle, res) > 0 &&
+ rc != LDAP_NO_SUCH_OBJECT)
{
/*
- * Printer has already been registered, modify the current
- * registration...
+ * Printer has already been registered, check if
+ * modification is required...
*/
- cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "send_ldap_browse: Replacing entry...");
+ e = ldap_first_entry(BrowseLDAPHandle, res);
- for (i = 0; i < 7; i ++)
+ /*
+ * Get the required values from this entry...
+ */
+
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerDescription",
+ old_info, sizeof(old_info)) == -1)
+ old_info[0] = '\0';
+
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerLocation",
+ old_location, sizeof(old_location)) == -1)
+ old_info[0] = '\0';
+
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerMakeAndModel",
+ old_make_model, sizeof(old_make_model)) == -1)
+ old_info[0] = '\0';
+
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerType",
+ old_type_string, sizeof(old_type_string)) == -1)
+ old_info[0] = '\0';
+
+ old_type = atoi(old_type_string);
+
+ if (ldap_getval_firststring(BrowseLDAPHandle, e, "printerURI", old_uri,
+ sizeof(old_uri)) == -1)
+ old_info[0] = '\0';
+
+ /*
+ * Check if modification is required...
+ */
+
+ if (!strcmp(info[0], old_info) && !strcmp(uri[0], old_uri) &&
+ !strcmp(location[0], old_location) &&
+ !strcmp(make_model[0], old_make_model) && p->type == old_type)
{
- pmods[i] = mods + i;
- pmods[i]->mod_op = LDAP_MOD_REPLACE;
+ /*
+ * LDAP entry for the printer exists. Printer has already been registered,
+ * no modifications required...
+ */
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: No updates required for %s", p->name);
}
- pmods[i] = NULL;
+ else
+ {
+ /*
+ * LDAP entry for the printer exists. Printer has already been registered,
+ * modify the current registration...
+ */
- if ((rc = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
- cupsdLogMessage(CUPSD_LOG_ERROR,
- "LDAP modify for %s failed with status %d: %s",
- p->name, rc, ldap_err2string(rc));
+ cupsdLogMessage(CUPSD_LOG_DEBUG2,
+ "send_ldap_browse: Replace entry for %s", p->name);
+
+ for (i = 0; i < 7; i ++)
+ {
+ pmods[i] = mods + i;
+ pmods[i]->mod_op = LDAP_MOD_REPLACE;
+ }
+ pmods[i] = NULL;
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rcmod = ldap_modify_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rcmod = ldap_modify_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
+ cupsdLogMessage(CUPSD_LOG_ERROR,
+ "LDAP modify for %s failed with status %d: %s",
+ p->name, rcmod, ldap_err2string(rcmod));
+ if (rcmod == LDAP_SERVER_DOWN)
+ ldap_reconnect();
+ }
+ }
}
else
{
/*
- * Printer has never been registered, add the current
- * registration...
+ * No LDAP entry exists for the printer. Printer has never been registered,
+ * add the current registration...
*/
+ send_ldap_ou(ServerName, BrowseLDAPDN, "CUPS Server");
+
cupsdLogMessage(CUPSD_LOG_DEBUG2,
- "send_ldap_browse: Adding entry...");
+ "send_ldap_browse: Add entry for %s", p->name);
for (i = 0; i < 7; i ++)
{
}
pmods[i] = NULL;
- if ((rc = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rcmod = ldap_add_ext_s(BrowseLDAPHandle, dn, pmods, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rcmod = ldap_add_s(BrowseLDAPHandle, dn, pmods)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
cupsdLogMessage(CUPSD_LOG_ERROR,
"LDAP add for %s failed with status %d: %s",
- p->name, rc, ldap_err2string(rc));
+ p->name, rcmod, ldap_err2string(rcmod));
+ if (rcmod == LDAP_SERVER_DOWN)
+ ldap_reconnect();
+ }
}
+
+ if (rc == LDAP_SUCCESS)
+ ldap_freeres(res);
}
-#endif /* HAVE_OPENLDAP */
+
+
+/*
+ * 'ldap_dereg_printer()' - Delete printer from directory
+ */
+
+static void
+ldap_dereg_printer(cupsd_printer_t *p) /* I - Printer to deregister */
+{
+ char dn[1024]; /* DN of the printer */
+ int rc; /* LDAP status */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: Remove entry for %s",
+ p->name);
+
+ /*
+ * Reconnect if LDAP Handle is invalid...
+ */
+
+ if (!BrowseLDAPHandle)
+ {
+ ldap_reconnect();
+ return;
+ }
+
+ /*
+ * Get dn for printer and delete LDAP entry...
+ */
+
+ snprintf(dn, sizeof(dn), "cn=%s, ou=%s, %s", p->name, ServerName,
+ BrowseLDAPDN);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_printer: dn=\"%s\"", dn);
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "LDAP delete for %s failed with status %d: %s",
+ p->name, rc, ldap_err2string(rc));
+
+ /*
+ * If we had a connection problem (connection timed out, etc.)
+ * we should reconnect and try again to delete the entry...
+ */
+
+ if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
+ {
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "Retry deleting LDAP entry for %s after a reconnect...", p->name);
+ ldap_reconnect();
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "LDAP delete for %s failed with status %d: %s",
+ p->name, rc, ldap_err2string(rc));
+ }
+ }
+}
+
+
+/*
+ * 'ldap_dereg_ou()' - Remove the organizational unit.
+ */
+
+static void
+ldap_dereg_ou(char *ou, /* I - Organizational unit (servername) */
+ char *basedn) /* I - Dase dn */
+{
+ char dn[1024]; /* DN of the printer */
+ int rc; /* LDAP status */
+
+
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: Remove entry for %s", ou);
+
+ /*
+ * Reconnect if LDAP Handle is invalid...
+ */
+
+ if (!BrowseLDAPHandle)
+ {
+ ldap_reconnect();
+ return;
+ }
+
+ /*
+ * Get dn for printer and delete LDAP entry...
+ */
+
+ snprintf(dn, sizeof(dn), "ou=%s, %s", ou, basedn);
+ cupsdLogMessage(CUPSD_LOG_DEBUG2, "ldap_dereg_ou: dn=\"%s\"", dn);
+
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ {
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "LDAP delete for %s failed with status %d: %s",
+ ou, rc, ldap_err2string(rc));
+
+ /*
+ * If we had a connection problem (connection timed out, etc.)
+ * we should reconnect and try again to delete the entry...
+ */
+
+ if (rc == LDAP_SERVER_DOWN || rc == LDAP_CONNECT_ERROR)
+ {
+ cupsdLogMessage(CUPSD_LOG_INFO,
+ "Retry deleting LDAP entry for %s after a reconnect...", ou);
+ ldap_reconnect();
+# if defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000
+ if ((rc = ldap_delete_ext_s(BrowseLDAPHandle, dn, NULL,
+ NULL)) != LDAP_SUCCESS)
+# else
+ if ((rc = ldap_delete_s(BrowseLDAPHandle, dn)) != LDAP_SUCCESS)
+# endif /* defined(HAVE_OPENLDAP) && LDAP_API_VERSION > 3000 */
+ cupsdLogMessage(CUPSD_LOG_WARN,
+ "LDAP delete for %s failed with status %d: %s",
+ ou, rc, ldap_err2string(rc));
+ }
+ }
+}
+#endif /* HAVE_LDAP */
#ifdef HAVE_LIBSLP
case CUPSD_AUTH_ALLOW : /* Order Deny,Allow */
auth = CUPSD_AUTH_ALLOW;
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
+ if (cupsdCheckAuth(address, srcname, len, BrowseACL->deny))
auth = CUPSD_AUTH_DENY;
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
+ if (cupsdCheckAuth(address, srcname, len, BrowseACL->allow))
auth = CUPSD_AUTH_ALLOW;
break;
case CUPSD_AUTH_DENY : /* Order Allow,Deny */
auth = CUPSD_AUTH_DENY;
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_allow, BrowseACL->allow))
+ if (cupsdCheckAuth(address, srcname, len, BrowseACL->allow))
auth = CUPSD_AUTH_ALLOW;
- if (cupsdCheckAuth(address, srcname, len,
- BrowseACL->num_deny, BrowseACL->deny))
+ if (cupsdCheckAuth(address, srcname, len, BrowseACL->deny))
auth = CUPSD_AUTH_DENY;
break;
}
*/
for (i = 0; i < NumRelays; i ++)
- if (cupsdCheckAuth(address, srcname, len, 1, &(Relays[i].from)))
+ if (cupsdCheckAuth(address, srcname, len, Relays[i].from))
if (sendto(BrowseSocket, packet, bytes, 0,
(struct sockaddr *)&(Relays[i].to),
httpAddrLength(&(Relays[i].to))) <= 0)
argv[4] = NULL;
cupsdStartProcess("/bin/launchctl", argv, envp, -1, -1, -1, -1, -1, 1,
- NULL, &pid);
+ NULL, NULL, &pid);
}
#endif /* __APPLE__ */
else
while ((ptr = cupsdStatBufUpdate(PollStatusBuffer, &loglevel,
message, sizeof(message))) != NULL)
+ {
+ if (loglevel == CUPSD_LOG_INFO)
+ cupsdLogMessage(CUPSD_LOG_INFO, "%s", message);
+
if (!strchr(PollStatusBuffer->buffer, '\n'))
break;
+ }
if (ptr == NULL && !PollStatusBuffer->bufused)
{
/*
- * End of "$Id: dirsvc.c 7003 2007-10-01 23:10:13Z mike $".
+ * End of "$Id: dirsvc.c 7933 2008-09-11 00:44:58Z mike $".
*/