File: tclLdapModify.c

package info (click to toggle)
libtcl-ldap 1.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 180 kB
  • ctags: 305
  • sloc: ansic: 1,435; tcl: 161; makefile: 57
file content (214 lines) | stat: -rw-r--r-- 5,157 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright (c) 1994 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 */

/* Tcl_LdapModifyCmd - simple function to modify an entry using LDAP */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <lber.h>
#include <ldap.h>
#include "tcl.h"
#define safe_realloc(ptr,size) (ptr == NULL ? malloc(size) : realloc(ptr,size))

static int freepmods _ANSI_ARGS_ ((LDAPMod ** pmods));
static int addmodifyop _ANSI_ARGS_ ((LDAPMod *** pmodsp, char *buf));
static int new;

extern int ldcnt;
extern LDAP *ldprime[1024];

int Tcl_LdapModifyCmd (dummy, interp, argc, argv)
     ClientData dummy;
     Tcl_Interp *interp;
     int argc;
     char **argv;
{
    char *entrydn, *buf;
    int not, verbose, rc, linenum, conn;
    LDAPMod **pmods;
    int localArgc;
    char **localArgv;

    rc = not = verbose = 0;
    pmods = NULL;

    new = (strcmp (argv[0], "LdapAdd") == 0);

    if (argc < 4) {
	Tcl_AppendResult (interp, "Too few arguments! Usage: \n", (char *) NULL);
	Tcl_AppendResult (interp, argv[0], " LdapConn ", (char *) NULL);
	Tcl_AppendResult (interp, "DN [LIST of attr=value pairs]", (char *) NULL);
	return TCL_ERROR;
    }

    conn = atoi (argv[1]);
    entrydn = argv[2];

    if ((Tcl_SplitList (interp, argv[3], &localArgc, &localArgv)) != TCL_OK) {
	Tcl_AppendResult (interp, "No LIST argument passed!", (char *) NULL);
	return TCL_ERROR;
    }

    ldprime[conn]->ld_deref = LDAP_DEREF_NEVER;		/* this seems prudent */

    for (linenum = 0; linenum < localArgc; linenum++) {
	buf = localArgv[linenum];
	if ((addmodifyop (&pmods, buf)) != TCL_OK) {
	    Tcl_AppendResult (interp, "Problem with data!", (char *) NULL);
	    return TCL_ERROR;
	}
    }

    if (pmods != NULL) {
	if (new) {
	    if ((ldap_add_s (ldprime[conn], entrydn, pmods)) != LDAP_SUCCESS) {
		Tcl_AppendResult (interp, "Unable to add entry!", (char *) NULL);
		freepmods (pmods);
		return TCL_ERROR;
	    }
	} else {
	    if ((ldap_modify_s (ldprime[conn], entrydn, pmods)) != LDAP_SUCCESS) {
		Tcl_AppendResult (interp, "Unable to change entry!", (char *) NULL);
		freepmods (pmods);
		return TCL_ERROR;
	    }
	}
	freepmods (pmods);
    }

    return TCL_OK;
}				/* End of main function Tcl_LdapModify */

static int addmodifyop (pmodsp, buf)
     LDAPMod ***pmodsp;
     char *buf;
{
    char *p, *q, *attr, *value;
    LDAPMod **pmods;
    int i, j, modop;
    struct berval *bvp;

    pmods = *pmodsp;

    if ((p = strchr (buf, '=')) == NULL) {
	value = NULL;
	p = buf + strlen (buf);
    } else {
	*p++ = '\0';
	value = p;
    }

    for (attr = buf; *attr != '\0' && isspace (*attr); ++attr) {;
    }
    for (q = p - 1; q > attr && isspace (*q); --q) {
	*q = '\0';
    }

    if (value != NULL) {
	while (isspace (*value)) {
	    ++value;
	}
	for (q = value + strlen (value) - 1; q > value && isspace (*q); --q) {
	    *q = '\0';
	}
    } else {
	if (new) {
	    return TCL_ERROR;
	}
    }

    switch (*attr) {
    case '-':
	modop = LDAP_MOD_DELETE;
	++attr;
	break;
    case '+':
	modop = LDAP_MOD_ADD;
	++attr;
	break;
    default:
	modop = LDAP_MOD_REPLACE;
    }

    modop |= LDAP_MOD_BVALUES;

    i = 0;
    if (pmods != NULL) {
	for (; pmods[i] != NULL; ++i) {
	    if (strcasecmp (pmods[i]->mod_type, attr) == 0 && pmods[i]->mod_op
		== modop) {
		break;
	    }
	}
    }

    if (pmods == NULL || pmods[i] == NULL) {
	if ((pmods = (LDAPMod **) safe_realloc (pmods, (i + 2) *
		    sizeof (LDAPMod *))) == NULL) {
	    return TCL_ERROR;
	}
	*pmodsp = pmods;
	pmods[i + 1] = NULL;
	if ((pmods[i] = (LDAPMod *) calloc (1, sizeof (LDAPMod))) == NULL) {
	    return TCL_ERROR;
	}
	pmods[i]->mod_op = modop;
	if ((pmods[i]->mod_type = strdup (attr)) == NULL) {
	    return TCL_ERROR;
	}
    }

    if (value != NULL) {
	j = 0;
	if (pmods[i]->mod_bvalues != NULL) {
	    for (; pmods[i]->mod_bvalues[j] != NULL; ++j) {;
	    }
	}
	if ((pmods[i]->mod_bvalues =
		(struct berval **) safe_realloc (pmods[i]->mod_bvalues,
		    (j + 2) * sizeof (struct berval *))) == NULL) {
	    return TCL_ERROR;
	}
	pmods[i]->mod_bvalues[j + 1] = NULL;
	if ((bvp = (struct berval *) malloc (sizeof (struct berval))) == NULL) {
	    return TCL_ERROR;
	}
	pmods[i]->mod_bvalues[j] = bvp;

	bvp->bv_len = strlen (value);
	if ((bvp->bv_val = strdup (value)) == NULL) {
	    return TCL_ERROR;
	}
    }
    return TCL_OK;
}				/* End addmodifyop */

static int freepmods (pmods)
     LDAPMod **pmods;
{
    int i;

    for (i = 0; pmods[i] != NULL; ++i) {
	if (pmods[i]->mod_bvalues != NULL) {
	    ber_bvecfree (pmods[i]->mod_bvalues);
	}
    }
    free (pmods);
    return (0);
}				/* End of freepmods */