#include <stdlib.h>
#include <ctype.h>
-#ifdef _SQUID_MSWIN_ /* Native Windows port and MinGW */
+#ifdef _SQUID_MSWIN_ /* Native Windows port and MinGW */
#define snprintf _snprintf
#include <windows.h>
#define LDAP_OPT_X_TLS 0x6000
#endif
/* Some tricks to allow dynamic bind with ldap_start_tls_s entry point at
- run time.
+ * run time.
*/
#undef ldap_start_tls_s
#if LDAP_UNICODE
#define LDAP_START_TLS_S "ldap_start_tls_sW"
-typedef WINLDAPAPI ULONG (LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlW *, IN PLDAPControlW *);
+typedef WINLDAPAPI ULONG(LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlW *, IN PLDAPControlW *);
#else
#define LDAP_START_TLS_S "ldap_start_tls_sA"
-typedef WINLDAPAPI ULONG (LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlA *, IN PLDAPControlA *);
+typedef WINLDAPAPI ULONG(LDAPAPI * PFldap_start_tls_s) (IN PLDAP, OUT PULONG, OUT LDAPMessage **, IN PLDAPControlA *, IN PLDAPControlA *);
#endif /* LDAP_UNICODE */
PFldap_start_tls_s Win32_ldap_start_tls_s;
#define ldap_start_tls_s(l,s,c) Win32_ldap_start_tls_s(l,NULL,NULL,s,c)
#endif
#if defined(LDAP_API_VERSION) && LDAP_API_VERSION > 1823
-static int
+static int
squid_ldap_errno(LDAP * ld)
{
int err = 0;
ldap_get_option(ld, LDAP_OPT_ERROR_NUMBER, &err);
return err;
}
-static void
+static void
squid_ldap_set_aliasderef(LDAP * ld, int deref)
{
ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
}
-static void
+static void
squid_ldap_set_referrals(LDAP * ld, int referrals)
{
int *value = referrals ? LDAP_OPT_ON : LDAP_OPT_OFF;
ldap_set_option(ld, LDAP_OPT_REFERRALS, value);
}
static void
-squid_ldap_set_timelimit(LDAP *ld, int timelimit)
+squid_ldap_set_timelimit(LDAP * ld, int timelimit)
{
ldap_set_option(ld, LDAP_OPT_TIMELIMIT, &timelimit);
}
static void
-squid_ldap_set_connect_timeout(LDAP *ld, int timelimit)
+squid_ldap_set_connect_timeout(LDAP * ld, int timelimit)
{
#if defined(LDAP_OPT_NETWORK_TIMEOUT)
struct timeval tv;
ldap_set_option(ld, LDAP_X_OPT_CONNECT_TIMEOUT, &timelimit);
#endif
}
-static void
+static void
squid_ldap_memfree(char *p)
{
ldap_memfree(p);
}
+
#else
-static int
+static int
squid_ldap_errno(LDAP * ld)
{
return ld->ld_errno;
}
-static void
+static void
squid_ldap_set_aliasderef(LDAP * ld, int deref)
{
ld->ld_deref = deref;
}
-static void
+static void
squid_ldap_set_referrals(LDAP * ld, int referrals)
{
if (referrals)
ld->ld_options &= ~LDAP_OPT_REFERRALS;
}
static void
-squid_ldap_set_timelimit(LDAP *ld, int timelimit)
+squid_ldap_set_timelimit(LDAP * ld, int timelimit)
{
ld->ld_timelimit = timelimit;
}
static void
-squid_ldap_set_connect_timeout(LDAP *ld, int timelimit)
+squid_ldap_set_connect_timeout(LDAP * ld, int timelimit)
{
fprintf(stderr, "Connect timeouts not supported in your LDAP library\n");
}
-static void
+static void
squid_ldap_memfree(char *p)
{
free(p);
}
+
#endif
#ifdef LDAP_API_FEATURE_X_OPENLDAP
- #if LDAP_VENDOR_VERSION > 194
- #define HAS_URI_SUPPORT 1
- #endif
+#if LDAP_VENDOR_VERSION > 194
+#define HAS_URI_SUPPORT 1
+#endif
#endif
int
bindpasswd = value;
break;
case 'W':
- readSecret (value);
+ readSecret(value);
break;
case 'P':
persistent = !persistent;
}
if (!ldapServer)
- ldapServer = (char *)"localhost";
+ ldapServer = (char *) "localhost";
if (!basedn || !searchfilter) {
fprintf(stderr, "\n" PROGRAM_NAME " version " PROGRAM_VERSION "\n\n");
fprintf(stderr, "\tIf you need to bind as a user to perform searches then use the\n\t-D binddn -w bindpasswd or -D binddn -W secretfile options\n\n");
exit(1);
}
-
/* On Windows ldap_start_tls_s is available starting from Windows XP,
- so we need to bind at run-time with the function entry point
+ * so we need to bind at run-time with the function entry point
*/
#ifdef _SQUID_MSWIN_
if (use_tls) {
- HMODULE WLDAP32Handle;
+ HMODULE WLDAP32Handle;
WLDAP32Handle = GetModuleHandle("wldap32");
- if ((Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(WLDAP32Handle, LDAP_START_TLS_S)) == NULL) {
- fprintf( stderr, PROGRAM_NAME ": ERROR: TLS (-Z) not supported on this platform.\n");
+ if ((Win32_ldap_start_tls_s = (PFldap_start_tls_s) GetProcAddress(WLDAP32Handle, LDAP_START_TLS_S)) == NULL) {
+ fprintf(stderr, PROGRAM_NAME ": ERROR: TLS (-Z) not supported on this platform.\n");
exit(1);
- }
+ }
}
#endif
}
rfc1738_unescape(extension_dn);
}
-
while (!found && user && (group = strtok(NULL, " \n")) != NULL) {
rfc1738_unescape(group);
recover:
if (ld == NULL) {
#if HAS_URI_SUPPORT
- if (strstr(ldapServer, "://") != NULL) {
- rc = ldap_initialize( &ld, ldapServer );
- if( rc != LDAP_SUCCESS ) {
+ if (strstr(ldapServer, "://") != NULL) {
+ rc = ldap_initialize(&ld, ldapServer);
+ if (rc != LDAP_SUCCESS) {
fprintf(stderr, "\nUnable to connect to LDAPURI:%s\n", ldapServer);
break;
}
- } else
+ } else
#endif
#if NETSCAPE_SSL
if (sslpath) {
- if ( !sslinit && (ldapssl_client_init(sslpath, NULL) != LDAP_SUCCESS)) {
+ if (!sslinit && (ldapssl_client_init(sslpath, NULL) != LDAP_SUCCESS)) {
fprintf(stderr, "\nUnable to initialise SSL with cert path %s\n",
- sslpath);
+ sslpath);
exit(1);
} else {
sslinit++;
}
if ((ld = ldapssl_init(ldapServer, port, 1)) == NULL) {
fprintf(stderr, "\nUnable to connect to SSL LDAP server: %s port:%d\n",
- ldapServer, port);
+ ldapServer, port);
exit(1);
}
} else
#endif
if ((ld = ldap_init(ldapServer, port)) == NULL) {
- fprintf(stderr, "\nUnable to connect to LDAP server:%s port:%d\n",ldapServer, port);
+ fprintf(stderr, "\nUnable to connect to LDAP server:%s port:%d\n", ldapServer, port);
break;
}
-
if (connect_timeout)
squid_ldap_set_connect_timeout(ld, connect_timeout);
if (found)
printf("OK\n");
else {
-error:
+ error:
printf("ERR\n");
-}
+ }
if (ld != NULL) {
if (!persistent || (squid_ldap_errno(ld) != LDAP_SUCCESS && squid_ldap_errno(ld) != LDAP_INVALID_CREDENTIALS)) {
{
int n = 0;
while (size > 4 && *src) {
- switch(*src) {
+ switch (*src) {
case '*':
case '(':
case ')':
size -= 3;
if (size > 0) {
*escaped++ = '\\';
- snprintf(escaped, 3, "%02x", (unsigned char)*src++);
- escaped+=2;
+ snprintf(escaped, 3, "%02x", (unsigned char) *src++);
+ escaped += 2;
}
break;
default:
build_filter(char *filter, int size, const char *template, const char *user, const char *group)
{
int n;
- while(*template && size > 0) {
- switch(*template) {
+ while (*template && size > 0) {
+ switch (*template) {
case '%':
template++;
switch (*template) {
LDAPMessage *res = NULL;
LDAPMessage *entry;
int rc;
- char *searchattr[] = {(char *)LDAP_NO_ATTRS, NULL};
+ char *searchattr[] =
+ {(char *) LDAP_NO_ATTRS, NULL};
if (extension_dn && *extension_dn)
snprintf(searchbase, sizeof(searchbase), "%s,%s", extension_dn, basedn);
fprintf(stderr, PROGRAM_NAME " ERROR, Failed to construct LDAP search filter. filter=\"%s\", user=\"%s\", group=\"%s\"\n", filter, member, group);
return 1;
}
-
if (debug)
fprintf(stderr, "group filter '%s', searchbase '%s'\n", filter, searchbase);
}
static int
-searchLDAP(LDAP *ld, char *group, char *login, char *extension_dn)
+searchLDAP(LDAP * ld, char *group, char *login, char *extension_dn)
{
if (usersearchfilter) {
LDAPMessage *entry;
int rc;
char *userdn;
- char *searchattr[] = {(char *)LDAP_NO_ATTRS, NULL};
+ char *searchattr[] =
+ {(char *) LDAP_NO_ATTRS, NULL};
if (extension_dn && *extension_dn)
snprintf(searchbase, sizeof(searchbase), "%s,%s", extension_dn, userbasedn ? userbasedn : basedn);
else
}
-int readSecret(const char *filename)
+int
+readSecret(const char *filename)
{
- char buf[BUFSIZ];
- char *e=0;
- FILE *f;
-
- if(!(f=fopen(filename, "r"))) {
- fprintf(stderr, PROGRAM_NAME " ERROR: Can not read secret file %s\n", filename);
- return 1;
- }
+ char buf[BUFSIZ];
+ char *e = 0;
+ FILE *f;
- if( !fgets(buf, sizeof(buf)-1, f)) {
- fprintf(stderr, PROGRAM_NAME " ERROR: Secret file %s is empty\n", filename);
+ if (!(f = fopen(filename, "r"))) {
+ fprintf(stderr, PROGRAM_NAME " ERROR: Can not read secret file %s\n", filename);
+ return 1;
+ }
+ if (!fgets(buf, sizeof(buf) - 1, f)) {
+ fprintf(stderr, PROGRAM_NAME " ERROR: Secret file %s is empty\n", filename);
+ fclose(f);
+ return 1;
+ }
+ /* strip whitespaces on end */
+ if ((e = strrchr(buf, '\n')))
+ *e = 0;
+ if ((e = strrchr(buf, '\r')))
+ *e = 0;
+
+ bindpasswd = strdup(buf);
+ if (!bindpasswd) {
+ fprintf(stderr, PROGRAM_NAME " ERROR: can not allocate memory\n");
+ }
fclose(f);
- return 1;
- }
-
- /* strip whitespaces on end */
- if((e = strrchr(buf, '\n'))) *e = 0;
- if((e = strrchr(buf, '\r'))) *e = 0;
- bindpasswd = strdup(buf);
- if (!bindpasswd) {
- fprintf(stderr, PROGRAM_NAME " ERROR: can not allocate memory\n");
- }
-
- fclose(f);
-
- return 0;
+ return 0;
}