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