chiark / gitweb /
dimrngr: Avoid need for hkp housekeeping.
[gnupg2.git] / dirmngr / ldapserver.c
1 /* dirmngr.c - LDAP access
2    Copyright (C) 2008 g10 Code GmbH
3
4    This file is part of DirMngr.
5
6    DirMngr is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    DirMngr is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19    02110-1301, USA.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "dirmngr.h"
26 #include "ldapserver.h"
27
28 \f
29 /* Release the list of SERVERS.  As usual it is okay to call this
30    function with SERVERS passed as NULL.  */
31 void
32 ldapserver_list_free (ldap_server_t servers)
33 {
34   while (servers)
35     {
36       ldap_server_t tmp = servers->next;
37       xfree (servers->host);
38       xfree (servers->user);
39       if (servers->pass)
40         memset (servers->pass, 0, strlen (servers->pass));
41       xfree (servers->pass);
42       xfree (servers->base);
43       xfree (servers);
44       servers = tmp;
45     }
46 }
47
48
49 /* Parse a single LDAP server configuration line.  Returns the server
50    or NULL in case of errors.  The configuration line is assumed to be
51    colon seprated with these fields:
52
53    1. field: Hostname
54    2. field: Portnumber
55    3. field: Username
56    4. field: Password
57    5. field: Base DN
58
59    FILENAME and LINENO are used for diagnostic purposes only.
60 */
61 ldap_server_t
62 ldapserver_parse_one (char *line,
63                       const char *filename, unsigned int lineno)
64 {
65   char *p;
66   char *endp;
67   ldap_server_t server;
68   int fieldno;
69   int fail = 0;
70
71   /* Parse the colon separated fields.  */
72   server = xcalloc (1, sizeof *server);
73   for (fieldno = 1, p = line; p; p = endp, fieldno++ )
74     {
75       endp = strchr (p, ':');
76       if (endp)
77         *endp++ = '\0';
78       trim_spaces (p);
79       switch (fieldno)
80         {
81         case 1:
82           if (*p)
83             server->host = xstrdup (p);
84           else
85             {
86               log_error (_("%s:%u: no hostname given\n"),
87                          filename, lineno);
88               fail = 1;
89             }
90           break;
91
92         case 2:
93           if (*p)
94             server->port = atoi (p);
95           break;
96
97         case 3:
98           if (*p)
99             server->user = xstrdup (p);
100           break;
101
102         case 4:
103           if (*p && !server->user)
104             {
105               log_error (_("%s:%u: password given without user\n"),
106                          filename, lineno);
107               fail = 1;
108             }
109           else if (*p)
110             server->pass = xstrdup (p);
111           break;
112
113         case 5:
114           if (*p)
115             server->base = xstrdup (p);
116           break;
117
118         default:
119           /* (We silently ignore extra fields.) */
120           break;
121         }
122     }
123
124   if (fail)
125     {
126       log_info (_("%s:%u: skipping this line\n"), filename, lineno);
127       ldapserver_list_free (server);
128       server = NULL;
129     }
130
131   return server;
132 }