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