]> git.sur5r.net Git - openldap/blob - servers/slapd/suffixalias.c
More changes to let BDB build without LDBM.
[openldap] / servers / slapd / suffixalias.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1999-2000 The OpenLDAP Foundation, All Rights Reserved.
4  *
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file in the top level
6  * directory of this package.
7  */
8 /* Portions
9  * Copyright (c) 1998 Will Ballantyne, ITSD, Government of BC
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms are permitted
13  * provided that this notice is preserved and that due credit is given
14  * to ITSD, Government of BC. The name of ITSD
15  * may not be used to endorse or promote products derived from this
16  * software without specific prior written permission. This software
17  * is provided ``as is'' without express or implied warranty.
18  */
19
20 #include "portable.h"
21
22 #include <stdio.h>
23 #include <ac/string.h>
24 #include <ac/socket.h>
25 #include "slap.h"
26
27 /* 
28  * given a normalized uppercased dn (or root part),
29  * return an aliased dn if any of the alias suffixes match
30  */
31 char *suffix_alias(
32         Backend *be,
33         char *dn )
34 {
35         int     i, dnLength;
36
37         if(dn == NULL) return NULL;
38         if(be == NULL) return dn;
39
40         dnLength = strlen ( dn );
41
42         for ( i = 0;
43                 be->be_suffixAlias != NULL && be->be_suffixAlias[i] != NULL;
44                 i += 2 )
45         {
46                 int aliasLength = strlen (be->be_suffixAlias[i]);
47                 int diff = dnLength - aliasLength;
48
49                 if ( diff < 0 ) {
50                         /* alias is longer than dn */
51                         continue;
52                 } else if ( diff > 0 ) {
53                         if ( ! DN_SEPARATOR(dn[diff-1]) ) {
54                                 /* boundary is not at a DN separator */
55                                 continue;
56                         }
57                         /* At a DN Separator */
58                         /* XXX or an escaped separator... oh well */
59                 }
60
61                 if (!strcmp(be->be_suffixAlias[i], &dn[diff])) {
62                         char *oldDN = dn;
63                         dn = ch_malloc( diff + strlen(be->be_suffixAlias[i+1]) + 1 );
64                         strncpy( dn, oldDN, diff );
65                         strcpy( &dn[diff], be->be_suffixAlias[i+1] );
66 #ifdef NEW_LOGGING
67                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
68                                    "suffix_alias: converted \"%s\" to \"%s\"\n",
69                                    oldDN, dn ));
70 #else
71                         Debug( LDAP_DEBUG_ARGS,
72                                 "suffix_alias: converted \"%s\" to \"%s\"\n",
73                                 oldDN, dn, 0);
74 #endif
75
76                         free (oldDN);
77                         break;
78                 }
79         }
80
81         return dn;
82 }