]> git.sur5r.net Git - openldap/blob - libraries/libldap/add.c
strlen returns size_t not int.
[openldap] / libraries / libldap / add.c
1 /*
2  * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*  Portions
6  *  Copyright (c) 1990 Regents of the University of Michigan.
7  *  All rights reserved.
8  *
9  *  add.c
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include <ac/socket.h>
17 #include <ac/string.h>
18 #include <ac/time.h>
19
20 #include "ldap-int.h"
21
22 /*
23  * ldap_add - initiate an ldap add operation.  Parameters:
24  *
25  *      ld              LDAP descriptor
26  *      dn              DN of the entry to add
27  *      mods            List of attributes for the entry.  This is a null-
28  *                      terminated array of pointers to LDAPMod structures.
29  *                      only the type and values in the structures need be
30  *                      filled in.
31  *
32  * Example:
33  *      LDAPMod *attrs[] = { 
34  *                      { 0, "cn", { "babs jensen", "babs", 0 } },
35  *                      { 0, "sn", { "jensen", 0 } },
36  *                      { 0, "objectClass", { "person", 0 } },
37  *                      0
38  *              }
39  *      msgid = ldap_add( ld, dn, attrs );
40  */
41 int
42 ldap_add( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
43 {
44         int rc;
45         int msgid;
46
47         rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
48
49         if ( rc != LDAP_SUCCESS )
50                 return -1;
51
52         return msgid;
53 }
54
55
56 /*
57  * ldap_add_ext - initiate an ldap extended add operation.  Parameters:
58  *
59  *      ld              LDAP descriptor
60  *      dn              DN of the entry to add
61  *      mods            List of attributes for the entry.  This is a null-
62  *                      terminated array of pointers to LDAPMod structures.
63  *                      only the type and values in the structures need be
64  *                      filled in.
65  *      sctrl   Server Controls
66  *      cctrl   Client Controls
67  *      msgidp  Message ID pointer
68  *
69  * Example:
70  *      LDAPMod *attrs[] = { 
71  *                      { 0, "cn", { "babs jensen", "babs", 0 } },
72  *                      { 0, "sn", { "jensen", 0 } },
73  *                      { 0, "objectClass", { "person", 0 } },
74  *                      0
75  *              }
76  *      rc = ldap_add_ext( ld, dn, attrs, NULL, NULL, &msgid );
77  */
78 int
79 ldap_add_ext( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs,
80         LDAPControl **sctrls,
81         LDAPControl **cctrls,
82         int     *msgidp )
83 {
84         BerElement      *ber;
85         int             i, rc;
86
87         /*
88          * An add request looks like this:
89          *      AddRequest ::= SEQUENCE {
90          *              entry   DistinguishedName,
91          *              attrs   SEQUENCE OF SEQUENCE {
92          *                      type    AttributeType,
93          *                      values  SET OF AttributeValue
94          *              }
95          *      }
96          */
97
98         Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
99
100         /* create a message to send */
101         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
102                 ld->ld_errno = LDAP_NO_MEMORY;
103                 return ld->ld_errno;
104         }
105
106         rc = ber_printf( ber, "{it{s{", /* leave open '}}}' */
107                 ++ld->ld_msgid, LDAP_REQ_ADD, dn );
108
109         if ( rc == -1 ) {
110                 ld->ld_errno = LDAP_ENCODING_ERROR;
111                 ber_free( ber, 1 );
112                 return ld->ld_errno;
113         }
114
115         /* for each attribute in the entry... */
116         for ( i = 0; attrs[i] != NULL; i++ ) {
117                 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
118                         rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
119                             attrs[i]->mod_values );
120                 } else {
121                         rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
122                             attrs[i]->mod_values );
123                 }
124                 if ( rc == -1 ) {
125                         ld->ld_errno = LDAP_ENCODING_ERROR;
126                         ber_free( ber, 1 );
127                         return ld->ld_errno;
128                 }
129         }
130
131         /* close '{{' */
132         if ( ber_printf( ber, "}}" ) == -1 ) {
133                 ld->ld_errno = LDAP_ENCODING_ERROR;
134                 ber_free( ber, 1 );
135                 return ld->ld_errno;
136         }
137
138         /* Put Server Controls */
139         if( ldap_int_put_controls( ld, sctrls, ber ) != LDAP_SUCCESS ) {
140                 ber_free( ber, 1 );
141                 return ld->ld_errno;
142         }
143
144         /* close '{' */
145         if ( ber_printf( ber, "}" ) == -1 ) {
146                 ld->ld_errno = LDAP_ENCODING_ERROR;
147                 ber_free( ber, 1 );
148                 return ld->ld_errno;
149         }
150
151         /* send the message */
152         *msgidp = ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber );
153
154         if(*msgidp < 0)
155                 return ld->ld_errno;
156
157         return LDAP_SUCCESS;
158 }
159
160 int
161 ldap_add_ext_s(
162         LDAP *ld,
163         LDAP_CONST char *dn,
164         LDAPMod **attrs,
165         LDAPControl **sctrls,
166         LDAPControl **cctrls )
167 {
168         int             msgid, rc;
169         LDAPMessage     *res;
170
171         rc = ldap_add_ext( ld, dn, attrs, sctrls, cctrls, &msgid );
172
173         if ( rc != LDAP_SUCCESS )
174                 return( rc );
175
176         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
177                 return( ld->ld_errno );
178
179         return( ldap_result2error( ld, res, 1 ) );
180 }
181
182 int
183 ldap_add_s( LDAP *ld, LDAP_CONST char *dn, LDAPMod **attrs )
184 {
185         return ldap_add_ext_s( ld, dn, attrs, NULL, NULL );
186 }
187