]> git.sur5r.net Git - openldap/blob - libraries/libldap/add.c
Initial revision
[openldap] / libraries / libldap / add.c
1 /*
2  *  Copyright (c) 1990 Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  add.c
6  */
7
8 #ifndef lint 
9 static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #ifdef MACOS
16 #include "macos.h"
17 #endif /* MACOS */
18
19 #if defined( DOS ) || defined( _WIN32 )
20 #include <malloc.h>
21 #include "msdos.h"
22 #endif /* DOS */
23
24 #if !defined( MACOS ) && !defined( DOS )
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #endif /* !MACOS && !DOS */
28
29 #include "lber.h"
30 #include "ldap.h"
31 #include "ldap-int.h"
32
33 /*
34  * ldap_add - initiate an ldap (and X.500) add operation.  Parameters:
35  *
36  *      ld              LDAP descriptor
37  *      dn              DN of the entry to add
38  *      mods            List of attributes for the entry.  This is a null-
39  *                      terminated array of pointers to LDAPMod structures.
40  *                      only the type and values in the structures need be
41  *                      filled in.
42  *
43  * Example:
44  *      LDAPMod *attrs[] = { 
45  *                      { 0, "cn", { "babs jensen", "babs", 0 } },
46  *                      { 0, "sn", { "jensen", 0 } },
47  *                      { 0, "objectClass", { "person", 0 } },
48  *                      0
49  *              }
50  *      msgid = ldap_add( ld, dn, attrs );
51  */
52 int
53 ldap_add( LDAP *ld, char *dn, LDAPMod **attrs )
54 {
55         BerElement      *ber;
56         int             i, rc;
57
58         /*
59          * An add request looks like this:
60          *      AddRequest ::= SEQUENCE {
61          *              entry   DistinguishedName,
62          *              attrs   SEQUENCE OF SEQUENCE {
63          *                      type    AttributeType,
64          *                      values  SET OF AttributeValue
65          *              }
66          *      }
67          */
68
69         Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
70
71         /* create a message to send */
72         if ( (ber = alloc_ber_with_options( ld )) == NULLBER ) {
73                 return( -1 );
74         }
75
76         if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_ADD, dn )
77             == -1 ) {
78                 ld->ld_errno = LDAP_ENCODING_ERROR;
79                 ber_free( ber, 1 );
80                 return( -1 );
81         }
82
83         /* for each attribute in the entry... */
84         for ( i = 0; attrs[i] != NULL; i++ ) {
85                 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
86                         rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
87                             attrs[i]->mod_values );
88                 } else {
89                         rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
90                             attrs[i]->mod_values );
91                 }
92                 if ( rc == -1 ) {
93                         ld->ld_errno = LDAP_ENCODING_ERROR;
94                         ber_free( ber, 1 );
95                         return( -1 );
96                 }
97         }
98
99         if ( ber_printf( ber, "}}}" ) == -1 ) {
100                 ld->ld_errno = LDAP_ENCODING_ERROR;
101                 ber_free( ber, 1 );
102                 return( -1 );
103         }
104
105         /* send the message */
106         return( send_initial_request( ld, LDAP_REQ_ADD, dn, ber ));
107 }
108
109 int
110 ldap_add_s( LDAP *ld, char *dn, LDAPMod **attrs )
111 {
112         int             msgid;
113         LDAPMessage     *res;
114
115         if ( (msgid = ldap_add( ld, dn, attrs )) == -1 )
116                 return( ld->ld_errno );
117
118         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
119                 return( ld->ld_errno );
120
121         return( ldap_result2error( ld, res, 1 ) );
122 }
123