summaryrefslogtreecommitdiffstats
path: root/dirmngr/w32-ldap-help.h
blob: 566a346349f0f8c074027926d65bfa836e8e4c74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
50
51
52
53
54
55
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* w32-ldap-help.h - Map utf8 based API into a wchar_t API.
 * Copyright (C) 2010 Free Software Foundation, Inc.
 *
 * This file is part of GnuPG.
 *
 * GnuPG 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 3 of the License, or
 * (at your option) any later version.
 *
 * GnuPG 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef W32_LDAP_HELP_H
#define W32_LDAP_HELP_H

#ifndef HAVE_W32CE_SYSTEM
# error This is only required for W32CE.
#endif


static inline LDAP *
_dirmngr_ldap_init (const char *host, unsigned short port)
{
  LDAP *ld;
  wchar_t *whost = NULL;

  if (host)
    {
      whost = utf8_to_wchar (host);
      if (!whost)
        return NULL;
    }
  ld = ldap_init (whost, port);
  xfree (whost);
  return ld;
}


static inline ULONG
_dirmngr_ldap_simple_bind_s (LDAP *ld, const char *user, const char *pass)
{
  ULONG ret;
  wchar_t *wuser, *wpass;

  wuser = user? utf8_to_wchar (user) : NULL;
  wpass = pass? utf8_to_wchar (pass) : NULL;
  /* We can't easily map errnos to ldap_errno, thus we pass a NULL to
     the function in the hope that the server will throw an error.  */
  ret = ldap_simple_bind_s (ld, wuser, wpass);
  xfree (wpass);
  xfree (wuser);
  return ret;
}


static inline ULONG
_dirmngr_ldap_search_st (LDAP *ld, const char *base, ULONG scope,
                         const char *filter, char **attrs,
                         ULONG attrsonly, struct timeval *timeout,
                         LDAPMessage **res)
{
  ULONG ret = LDAP_NO_MEMORY;
  wchar_t *wbase = NULL;
  wchar_t *wfilter = NULL;
  wchar_t **wattrs = NULL;
  int i;

  if (base)
    {
      wbase = utf8_to_wchar (base);
      if (!wbase)
        goto leave;
    }
  if (filter)
    {
      wfilter = utf8_to_wchar (filter);
      if (!wfilter)
        goto leave;
    }
  if (attrs)
    {
      for (i=0; attrs[i]; i++)
        ;
      wattrs = xtrycalloc (i+1, sizeof *wattrs);
      if (!wattrs)
        goto leave;
      for (i=0; attrs[i]; i++)
        {
          wattrs[i] = utf8_to_wchar (attrs[i]);
          if (!wattrs[i])
            goto leave;
        }
    }

  ret = ldap_search_st (ld, wbase, scope, wfilter, wattrs, attrsonly,
                        (struct l_timeval *)timeout, res);

 leave:
  if (wattrs)
    {
      for (i=0; wattrs[i]; i++)
        xfree (wattrs[i]);
      xfree (wattrs);
    }
  xfree (wfilter);
  xfree (wbase);
  return ret;
}


static inline char *
_dirmngr_ldap_first_attribute (LDAP *ld, LDAPMessage *msg, BerElement **elem)
{
  wchar_t *wattr;
  char *attr;

  wattr = ldap_first_attribute (ld, msg, elem);
  if (!wattr)
    return NULL;
  attr = wchar_to_utf8 (wattr);
  ldap_memfree (wattr);
  return attr;
}


static inline char *
_dirmngr_ldap_next_attribute (LDAP *ld, LDAPMessage *msg, BerElement *elem)
{
  wchar_t *wattr;
  char *attr;

  wattr = ldap_next_attribute (ld, msg, elem);
  if (!wattr)
    return NULL;
  attr = wchar_to_utf8 (wattr);
  ldap_memfree (wattr);
  return attr;
}

static inline BerValue **
_dirmngr_ldap_get_values_len (LDAP *ld, LDAPMessage *msg, const char *attr)
{
  BerValue **ret;
  wchar_t *wattr;

  if (attr)
    {
      wattr = utf8_to_wchar (attr);
      if (!wattr)
        return NULL;
    }
  else
    wattr = NULL;

  ret = ldap_get_values_len (ld, msg, wattr);
  xfree (wattr);

  return ret;
}


#endif /*W32_LDAP_HELP_H*/