]> git.sur5r.net Git - openldap/blob - servers/slapd/suffixalias.c
Don't allow suffixAliases were alias and aliased dn our the same.
[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 "portable.h"
14
15 #include <stdio.h>
16 #include <string.h>
17 #include <ac/socket.h>          /* Get struct sockaddr for slap.h */
18 #include "slap.h"
19
20 /* 
21  * given a dn (or root part), return an aliased dn if any of the 
22  * alias suffixes match
23  */
24 char *suffixAlias (char *dn, Operation *op, Backend *be)
25 {
26         int     i, dnLength;
27
28         if(dn == NULL) return NULL;
29
30         dnLength = strlen ( dn );
31         op->o_suffix = NULL;
32         op->o_suffixAliased = NULL;
33         for ( i = 0;
34               be->be_suffixAlias != NULL && be->be_suffixAlias[i] != NULL;
35               i += 2) {
36                 int aliasLength = strlen (be->be_suffixAlias[i]);
37                 if (aliasLength > dnLength) {
38                         continue;
39                 }
40
41                 if (!strcasecmp(be->be_suffixAlias[i], 
42                                 dn + (dnLength - aliasLength))) {
43                         char *oldDN = dn;
44                         op->o_suffixAliased = ch_strdup ( be->be_suffixAlias[i] );
45                         dn = ch_malloc ( (dnLength - aliasLength) +
46                                           strlen (be->be_suffixAlias[ i+1 ]) + 1);
47                         strncpy (dn, oldDN, dnLength - aliasLength);
48                         strcpy  (dn + (dnLength - aliasLength), be->be_suffixAlias[ i+1 ]);
49                         op->o_suffix = ch_strdup (dn);
50                         Debug( LDAP_DEBUG_ARGS, "ALIAS: converted %s to %s", oldDN, dn, 0);
51                         free (oldDN);
52                         break;
53                 }
54         }
55         return dn;
56 }