~ubuntu-branches/ubuntu/utopic/nss-pam-ldapd/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/test_myldap.c

  • Committer: Bazaar Package Importer
  • Author(s): Arthur de Jong
  • Date: 2009-09-01 17:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090901170000-54hqcpmbxk32rzer
Tags: 0.7.0
* rename software to nss-pam-ldapd to indicate that PAM module is now a
  standard part of the software
* split into the binary packages libnss-ldapd, libpam-ldapd and nslcd
  (libpam-ldapd packaging used a patch for libpam-ldap by Steve Langasek)
  (closes: #535505)
* the configuration file name has been changed to /etc/nslcd.conf (package
  upgrade should migrate the configuration)
* updated Galician debconf translation by Marce Villarino (closes: #537424)
* patch by Petter Reinholdtsen to fix init script to start before autofs
  (closes: #544093)
* the default values for bind_timelimit and reconnect_maxsleeptime were
  lowered from 30 to 10 seconds (closes: #532874)
* upgrade to standards-version 3.8.3 (no changes needed)
* password hashes are no longer returned to non-root users (based on a patch
  by Alexander V. Chernikov)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   test_myldap.c - simple test for the myldap module
 
3
   This file is part of the nss-pam-ldapd library.
 
4
 
 
5
   Copyright (C) 2007 Arthur de Jong
 
6
 
 
7
   This library is free software; you can redistribute it and/or
 
8
   modify it under the terms of the GNU Lesser General Public
 
9
   License as published by the Free Software Foundation; either
 
10
   version 2.1 of the License, or (at your option) any later version.
 
11
 
 
12
   This library is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
   Lesser General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU Lesser General Public
 
18
   License along with this library; if not, write to the Free Software
 
19
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
20
   02110-1301 USA
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
#include <pthread.h>
 
28
#include <errno.h>
 
29
#include <assert.h>
 
30
#include <signal.h>
 
31
 
 
32
#include "nslcd/log.h"
 
33
#include "nslcd/cfg.h"
 
34
#include "nslcd/myldap.h"
 
35
 
 
36
struct worker_args {
 
37
  int id;
 
38
};
 
39
 
 
40
static const char *foo="";
 
41
 
 
42
/* this is a simple way to get this into an executable,
 
43
   we should probably read a valid config instead */
 
44
const char **base_get_var(int UNUSED(map)) {return NULL;}
 
45
int *scope_get_var(int UNUSED(map)) {return NULL;}
 
46
const char **filter_get_var(int UNUSED(map)) {return &foo;}
 
47
const char **attmap_get_var(int UNUSED(map),const char UNUSED(*name)) {return &foo;}
 
48
 
 
49
/* the maxium number of results to print (all results are retrieved) */
 
50
#define MAXRESULTS 10
 
51
 
 
52
/* This is a very basic search test, it performs a test to get certain
 
53
   entries from the database. It currently just prints out the DNs for
 
54
   the entries. */
 
55
static void test_search(void)
 
56
{
 
57
  MYLDAP_SESSION *session;
 
58
  MYLDAP_SEARCH *search;
 
59
  MYLDAP_ENTRY *entry;
 
60
  const char *attrs[] = { "uid", "cn", "gid", NULL };
 
61
  int i;
 
62
  int rc;
 
63
  /* initialize session */
 
64
  printf("test_myldap: test_search(): getting session...\n");
 
65
  session=myldap_create_session();
 
66
  assert(session!=NULL);
 
67
  /* perform search */
 
68
  printf("test_myldap: test_search(): doing search...\n");
 
69
  search=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
70
                       LDAP_SCOPE_SUBTREE,
 
71
                       "(objectclass=posixAccount)",
 
72
                       attrs);
 
73
  assert(search!=NULL);
 
74
  /* go over results */
 
75
  printf("test_myldap: test_search(): get results...\n");
 
76
  for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
 
77
  {
 
78
    if (i<MAXRESULTS)
 
79
      printf("test_myldap: test_search(): [%d] DN %s\n",i,myldap_get_dn(entry));
 
80
    else if (i==MAXRESULTS)
 
81
      printf("test_myldap: test_search(): ...\n");
 
82
  }
 
83
  printf("test_myldap: test_search(): %d entries returned: %s\n",i,ldap_err2string(rc));
 
84
  /* perform another search */
 
85
  printf("test_myldap: test_search(): doing search...\n");
 
86
  search=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
87
                       LDAP_SCOPE_SUBTREE,
 
88
                       "(objectclass=posixGroup)",
 
89
                       attrs);
 
90
  assert(search!=NULL);
 
91
  /* go over results */
 
92
  printf("test_myldap: test_search(): get results...\n");
 
93
  for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
 
94
  {
 
95
    if (i<MAXRESULTS)
 
96
      printf("test_myldap: test_search(): [%d] DN %s\n",i,myldap_get_dn(entry));
 
97
    else if (i==MAXRESULTS)
 
98
      printf("test_myldap: test_search(): ...\n");
 
99
  }
 
100
  printf("test_myldap: test_search(): %d entries returned: %s\n",i,ldap_err2string(rc));
 
101
  /* clean up */
 
102
  myldap_session_close(session);
 
103
}
 
104
 
 
105
static void test_get(void)
 
106
{
 
107
  MYLDAP_SESSION *session;
 
108
  MYLDAP_SEARCH *search1,*search2;
 
109
  MYLDAP_ENTRY *entry;
 
110
  const char *attrs1[] = { "cn", "userPassword", "memberUid", "gidNumber", "uniqueMember", NULL };
 
111
  const char *attrs2[] = { "uid", NULL };
 
112
  int rc;
 
113
  /* initialize session */
 
114
  printf("test_myldap: test_get(): getting session...\n");
 
115
  session=myldap_create_session();
 
116
  assert(session!=NULL);
 
117
  /* perform search */
 
118
  printf("test_myldap: test_get(): doing search...\n");
 
119
  search1=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
120
                        LDAP_SCOPE_SUBTREE,
 
121
                        "(&(|(objectClass=posixGroup)(objectClass=groupOfUniqueNames))(cn=testgroup2))",
 
122
                        attrs1);
 
123
  assert(search1!=NULL);
 
124
  /* get one entry */
 
125
  entry=myldap_get_entry(search1,&rc);
 
126
  assert(entry!=NULL);
 
127
  printf("test_myldap: test_get(): got DN %s\n",myldap_get_dn(entry));
 
128
  /* get some attribute values */
 
129
  (void)myldap_get_values(entry,"gidNumber");
 
130
  (void)myldap_get_values(entry,"userPassword");
 
131
  (void)myldap_get_values(entry,"memberUid");
 
132
  (void)myldap_get_values(entry,"uniqueMember");
 
133
  /* perform another search */
 
134
  printf("test_myldap: test_get(): doing get...\n");
 
135
  search2=myldap_search(session,"cn=Test User2,ou=people,dc=test,dc=tld",
 
136
                        LDAP_SCOPE_BASE,
 
137
                        "(objectclass=posixAccount)",
 
138
                        attrs2);
 
139
  assert(search2!=NULL);
 
140
  /* get one entry */
 
141
  entry=myldap_get_entry(search2,&rc);
 
142
  assert(entry!=NULL);
 
143
  printf("test_myldap: test_get(): got DN %s\n",myldap_get_dn(entry));
 
144
  /* test if searches are ok */
 
145
  assert(myldap_get_entry(search1,&rc)==NULL);
 
146
  assert(myldap_get_entry(search2,&rc)==NULL);
 
147
  /* clean up */
 
148
  myldap_session_close(session);
 
149
}
 
150
 
 
151
/* This search prints a number of attributes from a search */
 
152
static void test_get_values(void)
 
153
{
 
154
  MYLDAP_SESSION *session;
 
155
  MYLDAP_SEARCH *search;
 
156
  MYLDAP_ENTRY *entry;
 
157
  const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
 
158
  const char **vals;
 
159
  const char *rdnval;
 
160
  int i;
 
161
  /* initialize session */
 
162
  printf("test_myldap: test_get_values(): getting session...\n");
 
163
  session=myldap_create_session();
 
164
  assert(session!=NULL);
 
165
  /* perform search */
 
166
  search=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
167
                          LDAP_SCOPE_SUBTREE,
 
168
                          "(&(objectClass=posixAccount)(uid=*))",
 
169
                          attrs);
 
170
  assert(search!=NULL);
 
171
  /* go over results */
 
172
  for (i=0;(entry=myldap_get_entry(search,NULL))!=NULL;i++)
 
173
  {
 
174
    if (i<MAXRESULTS)
 
175
      printf("test_myldap: test_get_values(): [%d] DN %s\n",i,myldap_get_dn(entry));
 
176
    else if (i==MAXRESULTS)
 
177
      printf("test_myldap: test_get_values(): ...\n");
 
178
    /* try to get uid from attribute */
 
179
    vals=myldap_get_values(entry,"uidNumber");
 
180
    assert((vals!=NULL)&&(vals[0]!=NULL));
 
181
    if (i<MAXRESULTS)
 
182
      printf("test_myldap: test_get_values(): [%d] uidNumber=%s\n",i,vals[0]);
 
183
    /* try to get gid from attribute */
 
184
    vals=myldap_get_values(entry,"gidNumber");
 
185
    assert((vals!=NULL)&&(vals[0]!=NULL));
 
186
    if (i<MAXRESULTS)
 
187
      printf("test_myldap: test_get_values(): [%d] gidNumber=%s\n",i,vals[0]);
 
188
    /* write LDF_STRING(PASSWD_NAME) */
 
189
    vals=myldap_get_values(entry,"uid");
 
190
    assert((vals!=NULL)&&(vals[0]!=NULL));
 
191
    if (i<MAXRESULTS)
 
192
      printf("test_myldap: test_get_values(): [%d] uid=%s\n",i,vals[0]);
 
193
    /* get rdn values */
 
194
    rdnval=myldap_get_rdn_value(entry,"cn");
 
195
    if (i<MAXRESULTS)
 
196
      printf("test_myldap: test_get_values(): [%d] cdrdn=%s\n",i,rdnval==NULL?"NULL":rdnval);
 
197
    rdnval=myldap_get_rdn_value(entry,"uid");
 
198
    if (i<MAXRESULTS)
 
199
      printf("test_myldap: test_get_values(): [%d] uidrdn=%s\n",i,rdnval==NULL?"NULL":rdnval);
 
200
    /* check objectclass */
 
201
    assert(myldap_has_objectclass(entry,"posixAccount"));
 
202
  }
 
203
  /* clean up */
 
204
  myldap_session_close(session);
 
205
}
 
206
 
 
207
static void test_get_rdnvalues(void)
 
208
{
 
209
  MYLDAP_SESSION *session;
 
210
  MYLDAP_SEARCH *search;
 
211
  MYLDAP_ENTRY *entry;
 
212
  const char *attrs[] = { "cn", "uid", NULL };
 
213
  int rc;
 
214
  char buf[80];
 
215
  /* initialize session */
 
216
  printf("test_myldap: test_get_rdnvalues(): getting session...\n");
 
217
  session=myldap_create_session();
 
218
  assert(session!=NULL);
 
219
  /* perform search */
 
220
  printf("test_myldap: test_get_rdnvalues(): doing search...\n");
 
221
  search=myldap_search(session,"cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld",
 
222
                       LDAP_SCOPE_BASE,
 
223
                       "(objectClass=*)",
 
224
                       attrs);
 
225
  assert(search!=NULL);
 
226
  /* get one entry */
 
227
  entry=myldap_get_entry(search,&rc);
 
228
  assert(entry!=NULL);
 
229
  printf("test_myldap: test_get_rdnvalues(): got DN %s\n",myldap_get_dn(entry));
 
230
  /* get some values from DN */
 
231
  printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",myldap_get_rdn_value(entry,"uid"));
 
232
  printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",myldap_get_rdn_value(entry,"cn"));
 
233
  printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",myldap_get_rdn_value(entry,"uidNumber"));
 
234
  /* clean up */
 
235
  myldap_session_close(session);
 
236
  /* some tests */
 
237
  printf("test_myldap: test_get_rdnvalues(): DN.uid=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","uid",buf,sizeof(buf)));
 
238
  printf("test_myldap: test_get_rdnvalues(): DN.cn=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","cn",buf,sizeof(buf)));
 
239
  printf("test_myldap: test_get_rdnvalues(): DN.uidNumber=%s\n",myldap_cpy_rdn_value("cn=Aka Ashbach+uid=aashbach,ou=lotsofpeople,dc=test,dc=tld","uidNumber",buf,sizeof(buf)));
 
240
}
 
241
 
 
242
/* this method tests to see if we can perform two searches within
 
243
   one session */
 
244
static void test_two_searches(void)
 
245
{
 
246
  MYLDAP_SESSION *session;
 
247
  MYLDAP_SEARCH *search1,*search2;
 
248
  MYLDAP_ENTRY *entry;
 
249
  const char *attrs[] = { "uidNumber", "cn", "gidNumber", "uid", "objectClass", NULL };
 
250
  const char **vals;
 
251
  /* initialize session */
 
252
  printf("test_myldap: test_two_searches(): getting session...\n");
 
253
  session=myldap_create_session();
 
254
  assert(session!=NULL);
 
255
  /* perform search1 */
 
256
  search1=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
257
                        LDAP_SCOPE_SUBTREE,
 
258
                        "(&(objectClass=posixAccount)(uid=*))",
 
259
                        attrs);
 
260
  assert(search1!=NULL);
 
261
  /* get a result from search1 */
 
262
  entry=myldap_get_entry(search1,NULL);
 
263
  assert(entry!=NULL);
 
264
  printf("test_myldap: test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry));
 
265
  vals=myldap_get_values(entry,"cn");
 
266
  assert((vals!=NULL)&&(vals[0]!=NULL));
 
267
  printf("test_myldap: test_two_searches(): [search1] cn=%s\n",vals[0]);
 
268
  /* start a second search */
 
269
  search2=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
270
                        LDAP_SCOPE_SUBTREE,
 
271
                        "(&(objectclass=posixGroup)(gidNumber=*))",
 
272
                        attrs);
 
273
  assert(search2!=NULL);
 
274
  /* get a result from search2 */
 
275
  entry=myldap_get_entry(search2,NULL);
 
276
  assert(entry!=NULL);
 
277
  printf("test_myldap: test_two_searches(): [search2] DN %s\n",myldap_get_dn(entry));
 
278
  vals=myldap_get_values(entry,"cn");
 
279
  assert((vals!=NULL)&&(vals[0]!=NULL));
 
280
  printf("test_myldap: test_two_searches(): [search2] cn=%s\n",vals[0]);
 
281
  /* get another result from search1 */
 
282
  entry=myldap_get_entry(search1,NULL);
 
283
  assert(entry!=NULL);
 
284
  printf("test_myldap: test_two_searches(): [search1] DN %s\n",myldap_get_dn(entry));
 
285
  vals=myldap_get_values(entry,"cn");
 
286
  assert((vals!=NULL)&&(vals[0]!=NULL));
 
287
  printf("test_myldap: test_two_searches(): [search1] cn=%s\n",vals[0]);
 
288
  /* stop search1 */
 
289
  myldap_search_close(search1);
 
290
  /* get another result from search2 */
 
291
  entry=myldap_get_entry(search2,NULL);
 
292
  assert(entry!=NULL);
 
293
  printf("test_myldap: test_two_searches(): [search2] DN %s\n",myldap_get_dn(entry));
 
294
  vals=myldap_get_values(entry,"cn");
 
295
  assert((vals!=NULL)&&(vals[0]!=NULL));
 
296
  printf("test_myldap: test_two_searches(): [search2] cn=%s\n",vals[0]);
 
297
  /* clean up */
 
298
  myldap_session_close(session);
 
299
}
 
300
 
 
301
/* perform a simple search */
 
302
static void *worker(void *arg)
 
303
{
 
304
  MYLDAP_SESSION *session;
 
305
  MYLDAP_SEARCH *search;
 
306
  MYLDAP_ENTRY *entry;
 
307
  const char *attrs[] = { "uid", "cn", "gid", NULL };
 
308
  struct worker_args *args=(struct worker_args *)arg;
 
309
  int i;
 
310
  int rc;
 
311
  /* initialize session */
 
312
  session=myldap_create_session();
 
313
  assert(session!=NULL);
 
314
  /* perform search */
 
315
  search=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
316
                       LDAP_SCOPE_SUBTREE,
 
317
                       "(objectclass=posixAccount)",
 
318
                       attrs);
 
319
  assert(search!=NULL);
 
320
  /* go over results */
 
321
  for (i=0;(entry=myldap_get_entry(search,&rc))!=NULL;i++)
 
322
  {
 
323
    if (i<MAXRESULTS)
 
324
      printf("test_myldap: test_threads(): [worker %d] [%d] DN %s\n",args->id,i,myldap_get_dn(entry));
 
325
    else if (i==MAXRESULTS)
 
326
      printf("test_myldap: test_threads(): [worker %d] ...\n",args->id);
 
327
  }
 
328
  printf("test_myldap: test_threads(): [worker %d] DONE: %s\n",args->id,ldap_err2string(rc));
 
329
  assert(rc==LDAP_SUCCESS);
 
330
  /* clean up */
 
331
  myldap_session_close(session);
 
332
  return 0;
 
333
}
 
334
 
 
335
/* thread ids of all running threads */
 
336
#define NUM_THREADS 5
 
337
pthread_t my_threads[NUM_THREADS];
 
338
 
 
339
static void test_threads(void)
 
340
{
 
341
  int i;
 
342
  struct worker_args args[NUM_THREADS];
 
343
  /* start worker threads */
 
344
  for (i=0;i<NUM_THREADS;i++)
 
345
  {
 
346
    args[i].id=i;
 
347
    assert(pthread_create(&my_threads[i],NULL,worker,&(args[i]))==0);
 
348
  }
 
349
  /* wait for all threads to die */
 
350
  for (i=0;i<NUM_THREADS;i++)
 
351
  {
 
352
    assert(pthread_join(my_threads[i],NULL)==0);
 
353
  }
 
354
}
 
355
 
 
356
static void test_connections(void)
 
357
{
 
358
  MYLDAP_SESSION *session;
 
359
  MYLDAP_SEARCH *search;
 
360
  const char *attrs[] = { "uid", "cn", "gid", NULL };
 
361
  char *old_uris[NSS_LDAP_CONFIG_URI_MAX+1];
 
362
  int i;
 
363
  /* save the old URIs */
 
364
  for (i=0;i<(NSS_LDAP_CONFIG_URI_MAX+1);i++)
 
365
  {
 
366
    old_uris[i]=nslcd_cfg->ldc_uris[i].uri;
 
367
    nslcd_cfg->ldc_uris[i].uri=NULL;
 
368
  }
 
369
  /* set new URIs */
 
370
  i=0;
 
371
  nslcd_cfg->ldc_uris[i++].uri="ldapi://%2fdev%2fnull/";
 
372
  nslcd_cfg->ldc_uris[i++].uri="ldap://10.10.10.10/";
 
373
  nslcd_cfg->ldc_uris[i++].uri="ldapi://%2fdev%2fnonexistent/";
 
374
  nslcd_cfg->ldc_uris[i++].uri="ldap://nosuchhost/";
 
375
  nslcd_cfg->ldc_uris[i++].uri=NULL;
 
376
  /* initialize session */
 
377
  printf("test_myldap: test_connections(): getting session...\n");
 
378
  session=myldap_create_session();
 
379
  assert(session!=NULL);
 
380
  /* perform search */
 
381
  printf("test_myldap: test_connections(): doing search...\n");
 
382
  search=myldap_search(session,nslcd_cfg->ldc_bases[0],
 
383
                       LDAP_SCOPE_SUBTREE,
 
384
                       "(objectclass=posixAccount)",
 
385
                       attrs);
 
386
  assert(search==NULL);
 
387
  /* clean up */
 
388
  myldap_session_close(session);
 
389
  /* restore the old URIs */
 
390
  for (i=0;i<(NSS_LDAP_CONFIG_URI_MAX+1);i++)
 
391
    nslcd_cfg->ldc_uris[i].uri=old_uris[i];
 
392
}
 
393
 
 
394
/* the main program... */
 
395
int main(int argc,char *argv[])
 
396
{
 
397
  char *srcdir;
 
398
  char fname[100];
 
399
  struct sigaction act;
 
400
  /* build the name of the file */
 
401
  srcdir=getenv("srcdir");
 
402
  if (srcdir==NULL)
 
403
    srcdir=".";
 
404
  snprintf(fname,sizeof(fname),"%s/nslcd-test.conf",srcdir);
 
405
  fname[sizeof(fname)-1]='\0';
 
406
  /* initialize configuration */
 
407
  cfg_init(fname);
 
408
  /* partially initialize logging */
 
409
  log_setdefaultloglevel(LOG_DEBUG);
 
410
  /* ignore SIGPIPE */
 
411
  memset(&act,0,sizeof(struct sigaction));
 
412
  act.sa_handler=SIG_IGN;
 
413
  sigemptyset(&act.sa_mask);
 
414
  act.sa_flags=SA_RESTART|SA_NOCLDSTOP;
 
415
  assert(sigaction(SIGPIPE,&act,NULL)==0);
 
416
  /* do tests */
 
417
  test_search();
 
418
  test_get();
 
419
  test_get_values();
 
420
  test_get_rdnvalues();
 
421
  test_two_searches();
 
422
  test_threads();
 
423
  test_connections();
 
424
  return 0;
 
425
}