2010-06-09 18:53:51 +02:00
|
|
|
|
/* dirmngr.c - LDAP access
|
|
|
|
|
Copyright (C) 2008 g10 Code GmbH
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
This file is part of DirMngr.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
DirMngr is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
DirMngr is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
02110-1301, USA. */
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#include "dirmngr.h"
|
|
|
|
|
#include "ldapserver.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Release the list of SERVERS. As usual it is okay to call this
|
|
|
|
|
function with SERVERS passed as NULL. */
|
|
|
|
|
void
|
|
|
|
|
ldapserver_list_free (ldap_server_t servers)
|
|
|
|
|
{
|
|
|
|
|
while (servers)
|
|
|
|
|
{
|
|
|
|
|
ldap_server_t tmp = servers->next;
|
|
|
|
|
xfree (servers->host);
|
|
|
|
|
xfree (servers->user);
|
|
|
|
|
if (servers->pass)
|
|
|
|
|
memset (servers->pass, 0, strlen (servers->pass));
|
|
|
|
|
xfree (servers->pass);
|
|
|
|
|
xfree (servers->base);
|
|
|
|
|
xfree (servers);
|
|
|
|
|
servers = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Parse a single LDAP server configuration line. Returns the server
|
2015-03-19 11:14:52 +01:00
|
|
|
|
or NULL in case of errors. The configuration line is assumed to be
|
2019-11-07 17:41:19 +01:00
|
|
|
|
colon separated with these fields:
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
1. field: Hostname
|
|
|
|
|
2. field: Portnumber
|
2011-02-04 12:57:53 +01:00
|
|
|
|
3. field: Username
|
2010-06-09 18:53:51 +02:00
|
|
|
|
4. field: Password
|
|
|
|
|
5. field: Base DN
|
2019-11-09 11:29:59 +01:00
|
|
|
|
6. field: Flags
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
FILENAME and LINENO are used for diagnostic purposes only.
|
|
|
|
|
*/
|
|
|
|
|
ldap_server_t
|
|
|
|
|
ldapserver_parse_one (char *line,
|
|
|
|
|
const char *filename, unsigned int lineno)
|
|
|
|
|
{
|
|
|
|
|
char *p;
|
|
|
|
|
char *endp;
|
2019-11-09 11:29:59 +01:00
|
|
|
|
const char *s;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
ldap_server_t server;
|
|
|
|
|
int fieldno;
|
|
|
|
|
int fail = 0;
|
2019-11-09 11:29:59 +01:00
|
|
|
|
int i;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
|
|
|
|
|
/* Parse the colon separated fields. */
|
|
|
|
|
server = xcalloc (1, sizeof *server);
|
|
|
|
|
for (fieldno = 1, p = line; p; p = endp, fieldno++ )
|
|
|
|
|
{
|
|
|
|
|
endp = strchr (p, ':');
|
|
|
|
|
if (endp)
|
|
|
|
|
*endp++ = '\0';
|
|
|
|
|
trim_spaces (p);
|
|
|
|
|
switch (fieldno)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
if (*p)
|
|
|
|
|
server->host = xstrdup (p);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error (_("%s:%u: no hostname given\n"),
|
|
|
|
|
filename, lineno);
|
|
|
|
|
fail = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
case 2:
|
|
|
|
|
if (*p)
|
|
|
|
|
server->port = atoi (p);
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
case 3:
|
|
|
|
|
if (*p)
|
|
|
|
|
server->user = xstrdup (p);
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
case 4:
|
|
|
|
|
if (*p && !server->user)
|
|
|
|
|
{
|
2011-02-04 12:57:53 +01:00
|
|
|
|
log_error (_("%s:%u: password given without user\n"),
|
2010-06-09 18:53:51 +02:00
|
|
|
|
filename, lineno);
|
|
|
|
|
fail = 1;
|
|
|
|
|
}
|
|
|
|
|
else if (*p)
|
|
|
|
|
server->pass = xstrdup (p);
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
case 5:
|
|
|
|
|
if (*p)
|
|
|
|
|
server->base = xstrdup (p);
|
|
|
|
|
break;
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2019-11-09 11:29:59 +01:00
|
|
|
|
case 6:
|
|
|
|
|
{
|
|
|
|
|
char **flags = NULL;
|
|
|
|
|
|
|
|
|
|
flags = strtokenize (p, ",");
|
|
|
|
|
if (!flags)
|
|
|
|
|
log_fatal ("strtokenize failed: %s\n",
|
|
|
|
|
gpg_strerror (gpg_error_from_syserror ()));
|
|
|
|
|
|
|
|
|
|
for (i=0; (s = flags[i]); i++)
|
|
|
|
|
{
|
|
|
|
|
if (!*s)
|
|
|
|
|
;
|
|
|
|
|
else if (!ascii_strcasecmp (s, "ldaps"))
|
|
|
|
|
server->use_ldaps = 1;
|
|
|
|
|
else if (!ascii_strcasecmp (s, "ldap"))
|
|
|
|
|
server->use_ldaps = 0;
|
|
|
|
|
else
|
|
|
|
|
log_info (_("%s:%u: ignoring unknown flag '%s'\n"),
|
|
|
|
|
filename, lineno, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
xfree (flags);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
default:
|
|
|
|
|
/* (We silently ignore extra fields.) */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-04 12:57:53 +01:00
|
|
|
|
|
2010-06-09 18:53:51 +02:00
|
|
|
|
if (fail)
|
|
|
|
|
{
|
|
|
|
|
log_info (_("%s:%u: skipping this line\n"), filename, lineno);
|
|
|
|
|
ldapserver_list_free (server);
|
2014-12-22 12:16:46 +01:00
|
|
|
|
server = NULL;
|
2010-06-09 18:53:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return server;
|
|
|
|
|
}
|