3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
22 #include <ac/stdlib.h>
24 #include <ac/signal.h>
25 #include <ac/string.h>
29 #include <ac/unistd.h>
31 #ifdef HAVE_SYS_RESOURCE_H
32 #include <sys/resource.h>
45 #include "ldap_defaults.h"
48 static int load_editor( void );
49 static int modifiable( char *s, short flag );
50 static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short flag );
51 static int ovalues( char *attr );
52 static void write_entry( void );
54 static char *entry_temp_file;
60 LDAPMessage *mp; /* returned from find() */
61 char *dn, **rdns; /* distinguished name */
62 char name[MED_BUF_SIZE]; /* entry to modify */
66 printf("->edit(%s)\n", who);
69 * One must be bound in order to edit an entry.
71 if (bind_status == UD_NOT_BOUND) {
72 if (auth((char *) NULL, 1) < 0)
77 * First, decide what entry we are going to modify. If the
78 * user has not included a name on the modify command line,
79 * we will use the person who was last looked up with a find
80 * command. If there is no value there either, we don't know
83 * Once we know who to modify, be sure that they exist, and
88 printf(" Enter the name of the person or\n");
89 printf(" group whose entry you want to edit: ");
92 printf(" Edit whose entry? ");
94 fetch_buffer(name, sizeof(name), stdin);
100 if ((mp = find(who, TRUE)) == NULL) {
101 (void) ldap_msgfree(mp);
102 printf(" Could not locate \"%s\" in the Directory.\n", who);
105 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
106 rdns = ldap_explode_dn(dn, TRUE);
109 printf("\n Editing directory entry \"%s\"...\n", *rdns);
112 (void) ldap_msgfree(mp);
113 (void) ldap_value_free(rdns);
114 if (load_editor() < 0)
117 (void) unlink(entry_temp_file);
118 ldap_uncache_entry(ld, Entry.DN);
126 char *cp, *editor = UD_DEFAULT_EDITOR;
127 static char template[MED_BUF_SIZE];
136 printf("->load_editor()\n");
139 /* write the entry into a temp file */
140 (void) strcpy(template, "/tmp/udEdit.XXXXXX");
141 if ((entry_temp_file = mktemp(template)) == NULL) {
145 if ((fp = fopen(entry_temp_file, "w")) == NULL) {
149 fprintf(fp, "## Directory entry of %s\n", Entry.name);
151 fprintf(fp, "## Syntax is:\n");
152 fprintf(fp, "## <attribute-name>\n");
153 fprintf(fp, "## <TAB> <value 1>\n");
154 fprintf(fp, "## <TAB> : :\n");
155 fprintf(fp, "## <TAB> <value N>\n");
156 fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
160 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
162 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
166 (void) unlink(entry_temp_file);
170 /* edit the temp file with the editor of choice */
171 if ((cp = getenv("EDITOR")) != NULL)
176 if (( p = strrchr( editor, '/' )) == NULL ) {
181 printf(" Using %s as the editor...\n", p );
187 rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
192 if ((pid = fork()) == 0) {
193 /* child - edit the Directory entry */
194 (void) SIGNAL(SIGINT, SIG_IGN);
195 (void) execlp(editor, editor, entry_temp_file, NULL);
197 (void) fatal(editor);
200 /* parent - wait until the child proc is done editing */
201 RETSIGTYPE (*handler)();
202 handler = SIGNAL(SIGINT, SIG_IGN);
203 (void) wait(&status);
204 (void) SIGNAL(SIGINT, handler);
215 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
219 for (i = 0; attrs[i].quipu_name != NULL; i++) {
220 if (!modifiable(attrs[i].quipu_name,
221 (short) (flag|ATTR_FLAG_MAY_EDIT)))
226 fprintf(fp, "%s\n", attrs[i].quipu_name);
227 if ( attrs[i].number_of_values > MAX_VALUES ) {
228 printf(" The %s attribute has more than %d values.\n",
229 attrs[i].quipu_name, MAX_VALUES );
230 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
233 for (j = 0; j < attrs[i].number_of_values; j++)
234 fprintf(fp, "\t%s\n", attrs[i].values[j]);
240 modifiable( char *s, short int flag )
244 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
245 if (strcasecmp(s, attrlist[i].quipu_name))
247 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
251 /* should never be here */
258 int i = 0, j, number_of_values = -1;
261 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
263 LDAPMod *mods[MAX_ATTRS + 1];
264 LDAPMod *modp = NULL;
266 /* parse the file and write the values to the Directory */
267 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
272 (void) fgets(line, sizeof(line), fp);
275 line[strlen(line) - 1] = '\0'; /* kill newline */
279 if (isspace((unsigned char)*cp)) { /* value */
280 while (isspace((unsigned char)*cp))
282 values[number_of_values++] = strdup(cp);
283 if ( number_of_values >= MAX_VALUES ) {
284 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
290 while (isspace((unsigned char)*cp))
293 * If the number of values is greater than zero, then we
294 * know that this is not the first time through this
295 * loop, and we also know that we have a little bit
298 * o The modify operation needs to be changed from
299 * a DELETE to a REPLACE
301 * o The list of values pointer needs to be changed
302 * from NULL, to a NULL-terminated list of char
305 if (number_of_values > 0) {
306 modp->mod_op = LDAP_MOD_REPLACE;
307 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
311 modp->mod_values = vp;
312 for (j = 0; j < number_of_values; j++) {
313 *vp++ = strdup(values[j]);
314 (void) Free(values[j]);
319 * If there are no values, and there were no values to begin
320 * with, then there is nothing to do.
322 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
324 if (debug & D_MODIFY)
325 printf(" %s has zero values - skipping\n",
328 (void) Free(modp->mod_type);
329 modp->mod_type = strdup(cp);
330 modp->mod_op = LDAP_MOD_DELETE;
331 modp->mod_values = NULL;
335 * Fetch a new modify structure.
337 * Assume a DELETE operation with no values.
339 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
343 modp->mod_values = NULL;
344 modp->mod_type = strdup(cp);
345 modp->mod_op = LDAP_MOD_DELETE;
347 number_of_values = 0;
351 /* check the last one too */
352 if (number_of_values > 0) {
353 modp->mod_op = LDAP_MOD_REPLACE;
355 * Fetch some value pointers.
357 * number_of_values To store the values
358 * 1 For the NULL terminator
359 * 1 In case we need it to store
360 * the RDN as on of the values
361 * of 'cn' in case it isn't there
363 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
364 2))) == (char **) NULL) {
368 modp->mod_values = vp;
369 for (j = 0; j < number_of_values; j++) {
370 *vp++ = strdup(values[j]);
371 (void) Free(values[j]);
375 else if ((number_of_values == 0) &&
376 (ovalues(mods[i - 1]->mod_type) == 0)) {
378 if (debug & D_MODIFY)
379 printf(" %s has zero values - skipping\n",
380 mods[i - 1]->mod_type);
382 Free(mods[i - 1]->mod_type);
386 mods[i] = (LDAPMod *) NULL;
389 * If one of the mods pointers is 'cn', be sure that the RDN is one
392 for (j = 0; j < i; j++) {
393 if (strcasecmp("cn", mods[j]->mod_type))
396 * True only if there WERE values, but the person deleted
399 if (mods[j]->mod_values == NULL) {
400 mods[j]->mod_op = LDAP_MOD_REPLACE;
401 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
405 mods[j]->mod_values = vp;
406 *vp++ = strdup(Entry.name);
411 * Be sure that one of the values of 'cn' is the RDN.
413 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
414 if (strcasecmp(*vp, Entry.DN))
419 *vp++ = strdup(Entry.name);
425 if (debug & D_MODIFY) {
428 printf(" ld = 0x%x\n", ld);
429 printf(" dn = [%s]\n", Entry.DN);
430 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
431 printf(" mods[%d]->mod_op = %s\n",
432 x, code_to_str(mods[x]->mod_op));
433 printf(" mods[%d]->mod_type = %s\n",
434 x, mods[x]->mod_type);
435 if (mods[x]->mod_values == NULL)
436 printf(" mods[%d]->mod_values = NULL\n", x);
438 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
439 printf(" mods[%d]->mod_values[%1d] = %s\n",
440 x, y, mods[x]->mod_values[y]);
441 printf(" mods[%d]->mod_values[%1d] = NULL\n",
448 if (ldap_modify_s(ld, Entry.DN, mods))
451 printf(" Modification complete.\n" );
454 for (i = 0; mods[i] != NULL; i++)
455 free_mod_struct(mods[i]);
460 ovalues( char *attr )
462 struct attribute *ap;
465 * Lookup the attribute with quipu_name 'attr' in the Entry
466 * structure and return the number of values.
468 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
469 if (!strcasecmp(ap->quipu_name, attr))
470 return(ap->number_of_values);
472 /* should never get to this point unless 'attr' was something odd */