]> git.sur5r.net Git - openldap/blob - libraries/libldap/add.c
Remove LDAP_PORT dependencies so that ldap.conf defaults take over.
[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 "ldap-int.h"
21
22 /*
23  * ldap_add - initiate an ldap (and X.500) 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, char *dn, LDAPMod **attrs )
43 {
44         BerElement      *ber;
45         int             i, rc;
46
47         /*
48          * An add request looks like this:
49          *      AddRequest ::= SEQUENCE {
50          *              entry   DistinguishedName,
51          *              attrs   SEQUENCE OF SEQUENCE {
52          *                      type    AttributeType,
53          *                      values  SET OF AttributeValue
54          *              }
55          *      }
56          */
57
58         Debug( LDAP_DEBUG_TRACE, "ldap_add\n", 0, 0, 0 );
59
60         /* create a message to send */
61         if ( (ber = ldap_alloc_ber_with_options( ld )) == NULLBER ) {
62                 return( -1 );
63         }
64
65         if ( ber_printf( ber, "{it{s{", ++ld->ld_msgid, LDAP_REQ_ADD, dn )
66             == -1 ) {
67                 ld->ld_errno = LDAP_ENCODING_ERROR;
68                 ber_free( ber, 1 );
69                 return( -1 );
70         }
71
72         /* for each attribute in the entry... */
73         for ( i = 0; attrs[i] != NULL; i++ ) {
74                 if ( ( attrs[i]->mod_op & LDAP_MOD_BVALUES) != 0 ) {
75                         rc = ber_printf( ber, "{s[V]}", attrs[i]->mod_type,
76                             attrs[i]->mod_values );
77                 } else {
78                         rc = ber_printf( ber, "{s[v]}", attrs[i]->mod_type,
79                             attrs[i]->mod_values );
80                 }
81                 if ( rc == -1 ) {
82                         ld->ld_errno = LDAP_ENCODING_ERROR;
83                         ber_free( ber, 1 );
84                         return( -1 );
85                 }
86         }
87
88         if ( ber_printf( ber, "}}}" ) == -1 ) {
89                 ld->ld_errno = LDAP_ENCODING_ERROR;
90                 ber_free( ber, 1 );
91                 return( -1 );
92         }
93
94         /* send the message */
95         return( ldap_send_initial_request( ld, LDAP_REQ_ADD, dn, ber ));
96 }
97
98 int
99 ldap_add_s( LDAP *ld, char *dn, LDAPMod **attrs )
100 {
101         int             msgid;
102         LDAPMessage     *res;
103
104         if ( (msgid = ldap_add( ld, dn, attrs )) == -1 )
105                 return( ld->ld_errno );
106
107         if ( ldap_result( ld, msgid, 1, (struct timeval *) NULL, &res ) == -1 )
108                 return( ld->ld_errno );
109
110         return( ldap_result2error( ld, res, 1 ) );
111 }
112