ldapserver.c 2.94 KB
Newer Older
Werner Koch's avatar
Werner Koch committed
1 2
/* dirmngr.c - LDAP access
   Copyright (C) 2008 g10 Code GmbH
3

Werner Koch's avatar
Werner Koch committed
4
   This file is part of DirMngr.
5

Werner Koch's avatar
Werner Koch committed
6 7 8 9
   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.
10

Werner Koch's avatar
Werner Koch committed
11 12 13 14
   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.
15

Werner Koch's avatar
Werner Koch committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   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
50
   or NULL in case of errors.  The configuration line is assumed to be
Werner Koch's avatar
Werner Koch committed
51 52 53 54
   colon seprated with these fields:

   1. field: Hostname
   2. field: Portnumber
55
   3. field: Username
Werner Koch's avatar
Werner Koch committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
   4. field: Password
   5. field: Base DN

   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;
  ldap_server_t server;
  int fieldno;
  int fail = 0;

  /* 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;
91

Werner Koch's avatar
Werner Koch committed
92 93 94 95
	case 2:
	  if (*p)
	    server->port = atoi (p);
	  break;
96

Werner Koch's avatar
Werner Koch committed
97 98 99 100
	case 3:
	  if (*p)
	    server->user = xstrdup (p);
	  break;
101

Werner Koch's avatar
Werner Koch committed
102 103 104
	case 4:
	  if (*p && !server->user)
	    {
105
	      log_error (_("%s:%u: password given without user\n"),
Werner Koch's avatar
Werner Koch committed
106 107 108 109 110 111
			 filename, lineno);
	      fail = 1;
	    }
	  else if (*p)
	    server->pass = xstrdup (p);
	  break;
112

Werner Koch's avatar
Werner Koch committed
113 114 115 116
	case 5:
	  if (*p)
	    server->base = xstrdup (p);
	  break;
117

Werner Koch's avatar
Werner Koch committed
118 119 120 121 122
	default:
	  /* (We silently ignore extra fields.) */
	  break;
	}
    }
123

Werner Koch's avatar
Werner Koch committed
124 125 126 127
  if (fail)
    {
      log_info (_("%s:%u: skipping this line\n"), filename, lineno);
      ldapserver_list_free (server);
128
      server = NULL;
Werner Koch's avatar
Werner Koch committed
129 130 131 132
    }

  return server;
}