]> git.sur5r.net Git - openldap/blob - contrib/slapi-plugins/addrdnvalues/addrdnvalues.c
Don't return API result codes on wire
[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 /* (C) Copyright PADL Software Pty Ltd. 2003
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that this notice is preserved
17  * and that due credit is given to PADL Software Pty Ltd. This software
18  * is provided ``as is'' without express or implied warranty.
19  */
20
21 #include <string.h>
22 #include <unistd.h>
23
24 #include <ldap.h>
25 #include <lber.h>
26
27 #include <slapi-plugin.h>
28
29 int addrdnvalues_preop_init(Slapi_PBlock *pb);
30
31 static Slapi_PluginDesc pluginDescription = {
32         "addrdnvalues-plugin",
33         "PADL",
34         "1.0",
35         "RDN values addition plugin"
36 };
37
38 static int addrdnvalues_preop_add(Slapi_PBlock *pb)
39 {
40         int rc;
41         Slapi_Entry *e;
42         char *szDN;
43         LDAPDN dn;
44         int i;
45
46         if (slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &e) != 0) {
47                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_add",
48                                 "Error retrieving target entry\n");
49                 return -1;
50         }
51
52         szDN = slapi_entry_get_dn(e);
53         rc = ldap_str2dn(szDN, &dn, LDAP_DN_FORMAT_LDAPV3);
54         if (rc != LDAP_SUCCESS) {
55                 slapi_send_ldap_result(pb, LDAP_OTHER, NULL,
56                         "Failed to parse distinguished name", 0, NULL);
57                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_add",
58                         "Failed to parse distinguished name: %s\n",
59                         ldap_err2string(rc));
60                 return -1;
61         }
62
63         if (dn[0] != NULL) {
64                 LDAPRDN rdn = dn[0];
65
66                 for (i = 0; rdn[i] != NULL; i++) {
67                         LDAPAVA *ava = &rdn[0][i];
68                         struct berval *vals[2];
69                         Slapi_Attr *a = NULL;
70
71                         /* 0 means attr exists */
72                         if (slapi_entry_attr_find(e, ava->la_attr.bv_val, &a) == 0 &&
73                             a != NULL &&
74                             slapi_attr_value_find(a, &ava->la_value) == 0)
75                         {
76                                 /* RDN in entry */
77                                 continue;
78                         } /* else RDN not in entry */
79
80                         vals[0] = &ava->la_value;
81                         vals[1] = NULL;
82
83                         slapi_entry_attr_merge(e, ava->la_attr.bv_val, vals);
84                 }
85         }
86
87         ldap_dnfree(dn);
88
89         return 0;
90 }
91
92 int addrdnvalues_preop_init(Slapi_PBlock *pb)
93 {
94         if (slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03) != 0 ||
95             slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, &pluginDescription) != 0 ||
96             slapi_pblock_set(pb, SLAPI_PLUGIN_PRE_ADD_FN, (void *)addrdnvalues_preop_add) != 0) {
97                 slapi_log_error(SLAPI_LOG_PLUGIN, "addrdnvalues_preop_init",
98                                 "Error registering %s\n", pluginDescription.spd_description);
99                 return -1;
100         }
101
102         return 0;
103 }
104