Bump to 2.4.3
[platform/upstream/gpg2.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 separated 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  * 6. field: Flags
59  *
60  * Flags are:
61  *
62  *   starttls  := Use STARTTLS with a default port of 389
63  *   ldaptls   := Tunnel LDAP trough a TLS tunnel with default port 636
64  *   plain     := Switch to plain unsecured LDAP.
65  *   (The last of these 3 flags is the effective one)
66  *   ntds      := Use Active Directory authentication
67  *   areconly  := Use option LDAP_OPT_AREC_EXCLUSIVE
68  *
69  * FILENAME and LINENO are used for diagnostic purposes only.
70  */
71 ldap_server_t
72 ldapserver_parse_one (const char *line,
73                       const char *filename, unsigned int lineno)
74 {
75   char *p;
76   const char *s;
77   ldap_server_t server;
78   int fieldno;
79   int fail = 0;
80   int i;
81   char **fields = NULL;
82
83   server = xtrycalloc (1, sizeof *server);
84   if (!server)
85     {
86       fail = 1;
87       goto leave;
88     }
89
90   fields = strtokenize (line, ":");
91   if (!fields)
92     {
93       fail = 1;
94       goto leave;
95     }
96
97   for (fieldno=0; (p = fields[fieldno]); fieldno++)
98     {
99       switch (fieldno)
100         {
101         case 0:
102           server->host = xtrystrdup (p);
103           if (!server->host)
104             fail = 1;
105           break;
106
107         case 1:
108           if (*p)
109             server->port = atoi (p);
110           break;
111
112         case 2:
113           server->user = xtrystrdup (p);
114           if (!server->user)
115             fail = 1;
116           break;
117
118         case 3:
119           if (*p && !server->user)
120             {
121               if (filename)
122                 log_error (_("%s:%u: password given without user\n"),
123                            filename, lineno);
124               else
125                 log_error ("ldap: password given without user ('%s')\n", line);
126               fail = 1;
127             }
128           else if (*p)
129             {
130               server->pass = xtrystrdup (p);
131               if (!server->pass)
132                 fail = 1;
133             }
134           break;
135
136         case 4:
137           if (*p)
138             {
139               server->base = xtrystrdup (p);
140               if (!server->base)
141                 fail = 1;;
142             }
143           break;
144
145         case 5:
146           {
147             char **flags = NULL;
148
149             flags = strtokenize (p, ",");
150             if (!flags)
151               {
152                 log_error ("strtokenize failed: %s\n",
153                            gpg_strerror (gpg_error_from_syserror ()));
154                 fail = 1;
155                 break;
156               }
157
158             for (i=0; (s = flags[i]); i++)
159               {
160                 if (!*s)
161                   ;
162                 else if (!ascii_strcasecmp (s, "starttls"))
163                   {
164                     server->starttls = 1;
165                     server->ldap_over_tls = 0;
166                   }
167                 else if (!ascii_strcasecmp (s, "ldaptls"))
168                   {
169                     server->starttls = 0;
170                     server->ldap_over_tls = 1;
171                   }
172                 else if (!ascii_strcasecmp (s, "plain"))
173                   {
174                     server->starttls = 0;
175                     server->ldap_over_tls = 0;
176                   }
177                 else if (!ascii_strcasecmp (s, "ntds"))
178                   {
179                     server->ntds = 1;
180                   }
181                 else if (!ascii_strcasecmp (s, "areconly"))
182                   {
183                     server->areconly = 1;
184                   }
185                 else
186                   {
187                     if (filename)
188                       log_info (_("%s:%u: ignoring unknown flag '%s'\n"),
189                                 filename, lineno, s);
190                     else
191                       log_info ("ldap: unknown flag '%s' ignored in (%s)\n",
192                                 s, line);
193                   }
194               }
195
196             xfree (flags);
197           }
198           break;
199
200         default:
201           /* (We silently ignore extra fields.) */
202           break;
203         }
204     }
205
206  leave:
207   if (fail)
208     {
209       if (filename)
210         log_info (_("%s:%u: skipping this line\n"), filename, lineno);
211       else
212         log_info ("ldap: error in server spec ('%s')\n", line);
213       ldapserver_list_free (server);
214       server = NULL;
215     }
216   xfree (fields);
217
218   return server;
219 }