Line data Source code
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 :
29 : /* Release the list of SERVERS. As usual it is okay to call this
30 : function with SERVERS passed as NULL. */
31 : void
32 0 : ldapserver_list_free (ldap_server_t servers)
33 : {
34 0 : while (servers)
35 : {
36 0 : ldap_server_t tmp = servers->next;
37 0 : xfree (servers->host);
38 0 : xfree (servers->user);
39 0 : if (servers->pass)
40 0 : memset (servers->pass, 0, strlen (servers->pass));
41 0 : xfree (servers->pass);
42 0 : xfree (servers->base);
43 0 : xfree (servers);
44 0 : servers = tmp;
45 : }
46 0 : }
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 0 : 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 0 : int fail = 0;
70 :
71 : /* Parse the colon separated fields. */
72 0 : server = xcalloc (1, sizeof *server);
73 0 : for (fieldno = 1, p = line; p; p = endp, fieldno++ )
74 : {
75 0 : endp = strchr (p, ':');
76 0 : if (endp)
77 0 : *endp++ = '\0';
78 0 : trim_spaces (p);
79 0 : switch (fieldno)
80 : {
81 : case 1:
82 0 : if (*p)
83 0 : server->host = xstrdup (p);
84 : else
85 : {
86 0 : log_error (_("%s:%u: no hostname given\n"),
87 : filename, lineno);
88 0 : fail = 1;
89 : }
90 0 : break;
91 :
92 : case 2:
93 0 : if (*p)
94 0 : server->port = atoi (p);
95 0 : break;
96 :
97 : case 3:
98 0 : if (*p)
99 0 : server->user = xstrdup (p);
100 0 : break;
101 :
102 : case 4:
103 0 : if (*p && !server->user)
104 : {
105 0 : log_error (_("%s:%u: password given without user\n"),
106 : filename, lineno);
107 0 : fail = 1;
108 : }
109 0 : else if (*p)
110 0 : server->pass = xstrdup (p);
111 0 : break;
112 :
113 : case 5:
114 0 : if (*p)
115 0 : server->base = xstrdup (p);
116 0 : break;
117 :
118 : default:
119 : /* (We silently ignore extra fields.) */
120 0 : break;
121 : }
122 : }
123 :
124 0 : if (fail)
125 : {
126 0 : log_info (_("%s:%u: skipping this line\n"), filename, lineno);
127 0 : ldapserver_list_free (server);
128 0 : server = NULL;
129 : }
130 :
131 0 : return server;
132 : }
|