]> git.sur5r.net Git - openldap/blob - servers/slapd/suffixalias.c
Update copyright statements
[openldap] / servers / slapd / suffixalias.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1999-2002 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 void suffix_alias(
32         Backend *be,
33         struct berval *dn )
34 {
35         int     i, dnLength;
36
37         if(dn == NULL || be == NULL || dn->bv_len == 0)
38                 return;
39
40         dnLength = dn->bv_len;
41
42         for ( i = 0;
43                 be->be_suffixAlias != NULL && be->be_suffixAlias[i] != NULL;
44                 i += 2 )
45         {
46                 int aliasLength = be->be_suffixAlias[i]->bv_len;
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->bv_val[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]->bv_val, &dn->bv_val[diff])) {
62                         char *oldDN = dn->bv_val;
63                         dn->bv_len = diff + be->be_suffixAlias[i+1]->bv_len;
64                         dn->bv_val = ch_malloc( dn->bv_len + 1 );
65                         strncpy( dn->bv_val, oldDN, diff );
66                         strcpy( &dn->bv_val[diff], be->be_suffixAlias[i+1]->bv_val );
67 #ifdef NEW_LOGGING
68                         LDAP_LOG(( "operation", LDAP_LEVEL_INFO,
69                                    "suffix_alias: converted \"%s\" to \"%s\"\n",
70                                    oldDN, dn->bv_val ));
71 #else
72                         Debug( LDAP_DEBUG_ARGS,
73                                 "suffix_alias: converted \"%s\" to \"%s\"\n",
74                                 oldDN, dn->bv_val, 0);
75 #endif
76
77                         free (oldDN);
78                         break;
79                 }
80         }
81 }