]> git.sur5r.net Git - openldap/blob - servers/slapd/suffixalias.c
merge with main branch
[openldap] / servers / slapd / suffixalias.c
1 /*
2  * Copyright (c) 1998 Will Ballantyne, ITSD, Government of BC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to ITSD, Government of BC. The name of ITSD
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include "slap.h"
16
17 /* 
18  * given a dn (or root part), return an aliased dn if any of the 
19  * alias suffixes match
20  */
21 char *suffixAlias ( dn, op, be )
22         char *dn;
23         Operation *op;
24         Backend *be;
25 {
26         int     i, dnLength;
27
28         dnLength = strlen ( dn );
29         op->o_suffix = NULL;
30         op->o_suffixAliased = NULL;
31         for ( i = 0;
32               be->be_suffixAlias != NULL && be->be_suffixAlias[i] != NULL;
33               i += 2) {
34                 int aliasLength = strlen (be->be_suffixAlias[i]);
35                 if (aliasLength > dnLength) {
36                         continue;
37                 }
38
39                 if (!strcasecmp(be->be_suffixAlias[i], 
40                                 dn + (dnLength - aliasLength))) {
41                         char *oldDN = dn;
42                         op->o_suffixAliased = strdup ( be->be_suffixAlias[i] );
43                         dn = ch_malloc ( (dnLength - aliasLength) +
44                                           strlen (be->be_suffixAlias[ i+1 ]) + 1);
45                         strncpy (dn, oldDN, dnLength - aliasLength);
46                         strcpy  (dn + (dnLength - aliasLength), be->be_suffixAlias[ i+1 ]);
47                         op->o_suffix = strdup (dn);
48                         Debug( LDAP_DEBUG_ARGS, "ALIAS: converted %s to %s", oldDN, dn, 0);
49                         free (oldDN);
50                         break;
51                 }
52         }
53         return dn;
54 }