chiark / gitweb /
dirmngr: New debug message on correctly initialized libdns.
[gnupg2.git] / dirmngr / w32-ldap-help.h
1 /* w32-ldap-help.h - Map utf8 based API into a wchar_t API.
2  * Copyright (C) 2010 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG 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, see <https://www.gnu.org/licenses/>.
18  */
19
20 #ifndef W32_LDAP_HELP_H
21 #define W32_LDAP_HELP_H
22
23 #ifndef HAVE_W32CE_SYSTEM
24 # error This is only required for W32CE.
25 #endif
26
27
28 static inline LDAP *
29 _dirmngr_ldap_init (const char *host, unsigned short port)
30 {
31   LDAP *ld;
32   wchar_t *whost = NULL;
33
34   if (host)
35     {
36       whost = utf8_to_wchar (host);
37       if (!whost)
38         return NULL;
39     }
40   ld = ldap_init (whost, port);
41   xfree (whost);
42   return ld;
43 }
44
45
46 static inline ULONG
47 _dirmngr_ldap_simple_bind_s (LDAP *ld, const char *user, const char *pass)
48 {
49   ULONG ret;
50   wchar_t *wuser, *wpass;
51
52   wuser = user? utf8_to_wchar (user) : NULL;
53   wpass = pass? utf8_to_wchar (pass) : NULL;
54   /* We can't easily map errnos to ldap_errno, thus we pass a NULL to
55      the function in the hope that the server will throw an error.  */
56   ret = ldap_simple_bind_s (ld, wuser, wpass);
57   xfree (wpass);
58   xfree (wuser);
59   return ret;
60 }
61
62
63 static inline ULONG
64 _dirmngr_ldap_search_st (LDAP *ld, const char *base, ULONG scope,
65                          const char *filter, char **attrs,
66                          ULONG attrsonly, struct timeval *timeout,
67                          LDAPMessage **res)
68 {
69   ULONG ret = LDAP_NO_MEMORY;
70   wchar_t *wbase = NULL;
71   wchar_t *wfilter = NULL;
72   wchar_t **wattrs = NULL;
73   int i;
74
75   if (base)
76     {
77       wbase = utf8_to_wchar (base);
78       if (!wbase)
79         goto leave;
80     }
81   if (filter)
82     {
83       wfilter = utf8_to_wchar (filter);
84       if (!wfilter)
85         goto leave;
86     }
87   if (attrs)
88     {
89       for (i=0; attrs[i]; i++)
90         ;
91       wattrs = xtrycalloc (i+1, sizeof *wattrs);
92       if (!wattrs)
93         goto leave;
94       for (i=0; attrs[i]; i++)
95         {
96           wattrs[i] = utf8_to_wchar (attrs[i]);
97           if (!wattrs[i])
98             goto leave;
99         }
100     }
101
102   ret = ldap_search_st (ld, wbase, scope, wfilter, wattrs, attrsonly,
103                         (struct l_timeval *)timeout, res);
104
105  leave:
106   if (wattrs)
107     {
108       for (i=0; wattrs[i]; i++)
109         xfree (wattrs[i]);
110       xfree (wattrs);
111     }
112   xfree (wfilter);
113   xfree (wbase);
114   return ret;
115 }
116
117
118 static inline char *
119 _dirmngr_ldap_first_attribute (LDAP *ld, LDAPMessage *msg, BerElement **elem)
120 {
121   wchar_t *wattr;
122   char *attr;
123
124   wattr = ldap_first_attribute (ld, msg, elem);
125   if (!wattr)
126     return NULL;
127   attr = wchar_to_utf8 (wattr);
128   ldap_memfree (wattr);
129   return attr;
130 }
131
132
133 static inline char *
134 _dirmngr_ldap_next_attribute (LDAP *ld, LDAPMessage *msg, BerElement *elem)
135 {
136   wchar_t *wattr;
137   char *attr;
138
139   wattr = ldap_next_attribute (ld, msg, elem);
140   if (!wattr)
141     return NULL;
142   attr = wchar_to_utf8 (wattr);
143   ldap_memfree (wattr);
144   return attr;
145 }
146
147 static inline BerValue **
148 _dirmngr_ldap_get_values_len (LDAP *ld, LDAPMessage *msg, const char *attr)
149 {
150   BerValue **ret;
151   wchar_t *wattr;
152
153   if (attr)
154     {
155       wattr = utf8_to_wchar (attr);
156       if (!wattr)
157         return NULL;
158     }
159   else
160     wattr = NULL;
161
162   ret = ldap_get_values_len (ld, msg, wattr);
163   xfree (wattr);
164
165   return ret;
166 }
167
168
169 #endif /*W32_LDAP_HELP_H*/