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