]> git.sur5r.net Git - openldap/blob - contrib/slapi-plugins/addrdnvalues/addrdnvalues.c
fix copyrights
[openldap] / contrib / slapi-plugins / addrdnvalues / addrdnvalues.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 2003-2004 PADL Software Pty Ltd.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted only as authorized by the OpenLDAP
8  * Public License.
9  *
10  * A copy of this license is available in the file LICENSE in the
11  * top-level directory of the distribution or, alternatively, at
12  * <http://www.OpenLDAP.org/license.html>.
13  */
14
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <ldap.h>
19 #include <lber.h>
20
21 #include <slapi-plugin.h>
22
23 int addrdnvalues_preop_init(Slapi_PBlock *pb);
24
25 static Slapi_PluginDesc pluginDescription = {
26         "addrdnvalues-plugin",
27         "PADL",
28         "1.0",
29         "RDN values addition plugin"
30 };
31
32 static int addrdnvalues_preop_add(Slapi_PBlock *pb)
33 {
34         int rc;
35         Slapi_Entry *e;
36         char *szDN;
37         LDAPDN dn;
38         int i;
39
40         if (slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e) != 0) {
41                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_add",
42                                 "Error retrieving target entry\n");
43                 return -1;
44         }
45
46         szDN = slapi_entry_get_dn(e);
47         rc = ldap_str2dn(szDN, &dn, LDAP_DN_FORMAT_LDAPV3);
48         if (rc != LDAP_SUCCESS) {
49                 slapi_send_ldap_result(pb, LDAP_OTHER, NULL,
50                         "Failed to parse distinguished name", 0, NULL);
51                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_add",
52                         "Failed to parse distinguished name: %s\n",
53                         ldap_err2string(rc));
54                 return -1;
55         }
56
57         if (dn[0] != NULL) {
58                 LDAPRDN rdn = dn[0];
59
60                 for (i = 0; rdn[i] != NULL; i++) {
61                         LDAPAVA *ava = &rdn[0][i];
62                         struct berval *vals[2];
63                         Slapi_Attr *a = NULL;
64
65                         /* 0 means attr exists */
66                         if (slapi_entry_attr_find(e, ava->la_attr.bv_val, &a) == 0 &&
67                             a != NULL &&
68                             slapi_attr_value_find(a, &ava->la_value) == 0)
69                         {
70                                 /* RDN in entry */
71                                 continue;
72                         } /* else RDN not in entry */
73
74                         vals[0] = &ava->la_value;
75                         vals[1] = NULL;
76
77                         slapi_entry_attr_merge(e, ava->la_attr.bv_val, vals);
78                 }
79         }
80
81         ldap_dnfree(dn);
82
83         return 0;
84 }
85
86 int addrdnvalues_preop_init(Slapi_PBlock *pb)
87 {
88         if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03) != 0 ||
89             slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &pluginDescription) != 0 ||
90             slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN, (void *)addrdnvalues_preop_add) != 0) {
91                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_init",
92                                 "Error registering %s\n", pluginDescription.spd_description);
93                 return -1;
94         }
95
96         return 0;
97 }
98