2 /* $OpenLDAP: pkg/ldap/libraries/libldap/add.c,v 1.27.2.3 2008/02/11 23:26:41 kurt Exp $ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2008 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
16 /* Portions Copyright (c) 1990 Regents of the University of Michigan.
17 * All rights reserved.
24 #include <ac/socket.h>
25 #include <ac/string.h>
30 /* An LDAP Add Request/Response looks like this:
31 * AddRequest ::= [APPLICATION 8] SEQUENCE {
33 * attributes AttributeList }
35 * AttributeList ::= SEQUENCE OF attribute Attribute
37 * Attribute ::= PartialAttribute(WITH COMPONENTS {
39 * vals (SIZE(1..MAX))})
41 * PartialAttribute ::= SEQUENCE {
42 * type AttributeDescription,
43 * vals SET OF value AttributeValue }
45 * AttributeDescription ::= LDAPString
46 * -- Constrained to <attributedescription> [RFC4512]
48 * AttributeValue ::= OCTET STRING
50 * AddResponse ::= [APPLICATION 9] LDAPResult
55 * ldap_add - initiate an ldap add operation. Parameters:
58 * dn DN of the entry to add
59 * mods List of attributes for the entry. This is a null-
60 * terminated array of pointers to LDAPMod structures.
61 * only the type and values in the structures need be
65 * LDAPMod *attrs[] = {
66 * { 0, "cn", { "babs jensen", "babs", 0 } },
67 * { 0, "sn", { "jensen", 0 } },
68 * { 0, "objectClass", { "person", 0 } },
71 * msgid = ldap_add( ld, dn, attrs );
74 ldap_add( LDAP
*ld
, LDAP_CONST
char *dn
, LDAPMod
**attrs
)
79 rc
= ldap_add_ext( ld
, dn
, attrs
, NULL
, NULL
, &msgid
);
81 if ( rc
!= LDAP_SUCCESS
)
89 * ldap_add_ext - initiate an ldap extended add operation. Parameters:
92 * dn DN of the entry to add
93 * mods List of attributes for the entry. This is a null-
94 * terminated array of pointers to LDAPMod structures.
95 * only the type and values in the structures need be
97 * sctrl Server Controls
98 * cctrl Client Controls
99 * msgidp Message ID pointer
102 * LDAPMod *attrs[] = {
103 * { 0, "cn", { "babs jensen", "babs", 0 } },
104 * { 0, "sn", { "jensen", 0 } },
105 * { 0, "objectClass", { "person", 0 } },
108 * rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
115 LDAPControl
**sctrls
,
116 LDAPControl
**cctrls
,
123 Debug( LDAP_DEBUG_TRACE
, "ldap_add_ext\n", 0, 0, 0 );
124 assert( ld
!= NULL
);
125 assert( LDAP_VALID( ld
) );
126 assert( dn
!= NULL
);
127 assert( msgidp
!= NULL
);
129 /* check client controls */
130 rc
= ldap_int_client_controls( ld
, cctrls
);
131 if( rc
!= LDAP_SUCCESS
) return rc
;
133 /* create a message to send */
134 if ( (ber
= ldap_alloc_ber_with_options( ld
)) == NULL
) {
135 ld
->ld_errno
= LDAP_NO_MEMORY
;
139 LDAP_NEXT_MSGID(ld
, id
);
140 rc
= ber_printf( ber
, "{it{s{", /* '}}}' */
141 id
, LDAP_REQ_ADD
, dn
);
144 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
149 /* allow attrs to be NULL ("touch"; should fail...) */
151 /* for each attribute in the entry... */
152 for ( i
= 0; attrs
[i
] != NULL
; i
++ ) {
153 if ( ( attrs
[i
]->mod_op
& LDAP_MOD_BVALUES
) != 0 ) {
154 rc
= ber_printf( ber
, "{s[V]N}", attrs
[i
]->mod_type
,
155 attrs
[i
]->mod_bvalues
);
157 rc
= ber_printf( ber
, "{s[v]N}", attrs
[i
]->mod_type
,
158 attrs
[i
]->mod_values
);
161 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
168 if ( ber_printf( ber
, /*{{*/ "N}N}" ) == -1 ) {
169 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
174 /* Put Server Controls */
175 if( ldap_int_put_controls( ld
, sctrls
, ber
) != LDAP_SUCCESS
) {
180 if ( ber_printf( ber
, /*{*/ "N}" ) == -1 ) {
181 ld
->ld_errno
= LDAP_ENCODING_ERROR
;
186 /* send the message */
187 *msgidp
= ldap_send_initial_request( ld
, LDAP_REQ_ADD
, dn
, ber
, id
);
200 LDAPControl
**sctrls
,
201 LDAPControl
**cctrls
)
206 rc
= ldap_add_ext( ld
, dn
, attrs
, sctrls
, cctrls
, &msgid
);
208 if ( rc
!= LDAP_SUCCESS
)
211 if ( ldap_result( ld
, msgid
, LDAP_MSG_ALL
, (struct timeval
*) NULL
, &res
) == -1 || !res
)
212 return( ld
->ld_errno
);
214 return( ldap_result2error( ld
, res
, 1 ) );
218 ldap_add_s( LDAP
*ld
, LDAP_CONST
char *dn
, LDAPMod
**attrs
)
220 return ldap_add_ext_s( ld
, dn
, attrs
, NULL
, NULL
);