3 * Copyright (c) 1994 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
20 #include <ac/signal.h>
21 #include <ac/string.h>
25 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_RESOURCE_H
28 #include <sys/resource.h>
37 #include "ldap_defaults.h"
40 static int load_editor( void );
41 static int modifiable( char *s, short flag );
42 static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short flag );
43 static int ovalues( char *attr );
44 static void write_entry( void );
46 static char *entry_temp_file;
52 LDAPMessage *mp; /* returned from find() */
53 char *dn, **rdns; /* distinguished name */
54 char name[MED_BUF_SIZE]; /* entry to modify */
58 printf("->edit(%s)\n", who);
61 * One must be bound in order to edit an entry.
63 if (bind_status == UD_NOT_BOUND) {
64 if (auth((char *) NULL, 1) < 0)
69 * First, decide what entry we are going to modify. If the
70 * user has not included a name on the modify command line,
71 * we will use the person who was last looked up with a find
72 * command. If there is no value there either, we don't know
75 * Once we know who to modify, be sure that they exist, and
80 printf(" Enter the name of the person or\n");
81 printf(" group whose entry you want to edit: ");
84 printf(" Edit whose entry? ");
86 fetch_buffer(name, sizeof(name), stdin);
92 if ((mp = find(who, TRUE)) == NULL) {
93 (void) ldap_msgfree(mp);
94 printf(" Could not locate \"%s\" in the Directory.\n", who);
97 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
98 rdns = ldap_explode_dn(dn, TRUE);
101 printf("\n Editing directory entry \"%s\"...\n", *rdns);
104 (void) ldap_msgfree(mp);
105 (void) ldap_value_free(rdns);
106 if (load_editor() < 0)
109 (void) unlink(entry_temp_file);
110 ldap_uncache_entry(ld, Entry.DN);
118 char *cp, *editor = UD_DEFAULT_EDITOR;
119 static char template[MED_BUF_SIZE];
128 printf("->load_editor()\n");
131 /* write the entry into a temp file */
132 (void) strcpy(template, "/tmp/udEdit.XXXXXX");
133 if ((entry_temp_file = mktemp(template)) == NULL) {
137 if ((fp = fopen(entry_temp_file, "w")) == NULL) {
141 fprintf(fp, "## Directory entry of %s\n", Entry.name);
143 fprintf(fp, "## Syntax is:\n");
144 fprintf(fp, "## <attribute-name>\n");
145 fprintf(fp, "## <TAB> <value 1>\n");
146 fprintf(fp, "## <TAB> : :\n");
147 fprintf(fp, "## <TAB> <value N>\n");
148 fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
152 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
154 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
158 (void) unlink(entry_temp_file);
162 /* edit the temp file with the editor of choice */
163 if ((cp = getenv("EDITOR")) != NULL)
168 if (( p = strrchr( editor, '/' )) == NULL ) {
173 printf(" Using %s as the editor...\n", p );
179 rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
184 if ((pid = fork()) == 0) {
185 /* child - edit the Directory entry */
186 (void) SIGNAL(SIGINT, SIG_IGN);
187 (void) execlp(editor, editor, entry_temp_file, NULL);
189 (void) fatal(editor);
192 /* parent - wait until the child proc is done editing */
193 RETSIGTYPE (*handler)();
194 handler = SIGNAL(SIGINT, SIG_IGN);
195 (void) wait(&status);
196 (void) SIGNAL(SIGINT, handler);
207 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
211 for (i = 0; attrs[i].quipu_name != NULL; i++) {
212 if (!modifiable(attrs[i].quipu_name,
213 (short) (flag|ATTR_FLAG_MAY_EDIT)))
218 fprintf(fp, "%s\n", attrs[i].quipu_name);
219 if ( attrs[i].number_of_values > MAX_VALUES ) {
220 printf(" The %s attribute has more than %d values.\n",
221 attrs[i].quipu_name, MAX_VALUES );
222 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
225 for (j = 0; j < attrs[i].number_of_values; j++)
226 fprintf(fp, "\t%s\n", attrs[i].values[j]);
232 modifiable( char *s, short int flag )
236 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
237 if (strcasecmp(s, attrlist[i].quipu_name))
239 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
243 /* should never be here */
250 int i = 0, j, number_of_values = -1;
253 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
255 LDAPMod *mods[MAX_ATTRS + 1];
256 LDAPMod *modp = NULL;
258 /* parse the file and write the values to the Directory */
259 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
264 (void) fgets(line, sizeof(line), fp);
267 line[strlen(line) - 1] = '\0'; /* kill newline */
271 if (isspace((unsigned char)*cp)) { /* value */
272 while (isspace((unsigned char)*cp))
274 values[number_of_values++] = strdup(cp);
275 if ( number_of_values >= MAX_VALUES ) {
276 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
282 while (isspace((unsigned char)*cp))
285 * If the number of values is greater than zero, then we
286 * know that this is not the first time through this
287 * loop, and we also know that we have a little bit
290 * o The modify operation needs to be changed from
291 * a DELETE to a REPLACE
293 * o The list of values pointer needs to be changed
294 * from NULL, to a NULL-terminated list of char
297 if (number_of_values > 0) {
298 modp->mod_op = LDAP_MOD_REPLACE;
299 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
303 modp->mod_values = vp;
304 for (j = 0; j < number_of_values; j++) {
305 *vp++ = strdup(values[j]);
306 (void) Free(values[j]);
311 * If there are no values, and there were no values to begin
312 * with, then there is nothing to do.
314 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
316 if (debug & D_MODIFY)
317 printf(" %s has zero values - skipping\n",
320 (void) Free(modp->mod_type);
321 modp->mod_type = strdup(cp);
322 modp->mod_op = LDAP_MOD_DELETE;
323 modp->mod_values = NULL;
327 * Fetch a new modify structure.
329 * Assume a DELETE operation with no values.
331 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
335 modp->mod_values = NULL;
336 modp->mod_type = strdup(cp);
337 modp->mod_op = LDAP_MOD_DELETE;
339 number_of_values = 0;
343 /* check the last one too */
344 if (number_of_values > 0) {
345 modp->mod_op = LDAP_MOD_REPLACE;
347 * Fetch some value pointers.
349 * number_of_values To store the values
350 * 1 For the NULL terminator
351 * 1 In case we need it to store
352 * the RDN as on of the values
353 * of 'cn' in case it isn't there
355 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
356 2))) == (char **) NULL) {
360 modp->mod_values = vp;
361 for (j = 0; j < number_of_values; j++) {
362 *vp++ = strdup(values[j]);
363 (void) Free(values[j]);
367 else if ((number_of_values == 0) &&
368 (ovalues(mods[i - 1]->mod_type) == 0)) {
370 if (debug & D_MODIFY)
371 printf(" %s has zero values - skipping\n",
372 mods[i - 1]->mod_type);
374 Free(mods[i - 1]->mod_type);
378 mods[i] = (LDAPMod *) NULL;
381 * If one of the mods pointers is 'cn', be sure that the RDN is one
384 for (j = 0; j < i; j++) {
385 if (strcasecmp("cn", mods[j]->mod_type))
388 * True only if there WERE values, but the person deleted
391 if (mods[j]->mod_values == NULL) {
392 mods[j]->mod_op = LDAP_MOD_REPLACE;
393 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
397 mods[j]->mod_values = vp;
398 *vp++ = strdup(Entry.name);
403 * Be sure that one of the values of 'cn' is the RDN.
405 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
406 if (strcasecmp(*vp, Entry.DN))
411 *vp++ = strdup(Entry.name);
417 if (debug & D_MODIFY) {
420 printf(" ld = 0x%x\n", ld);
421 printf(" dn = [%s]\n", Entry.DN);
422 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
423 printf(" mods[%d]->mod_op = %s\n",
424 x, code_to_str(mods[x]->mod_op));
425 printf(" mods[%d]->mod_type = %s\n",
426 x, mods[x]->mod_type);
427 if (mods[x]->mod_values == NULL)
428 printf(" mods[%d]->mod_values = NULL\n", x);
430 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
431 printf(" mods[%d]->mod_values[%1d] = %s\n",
432 x, y, mods[x]->mod_values[y]);
433 printf(" mods[%d]->mod_values[%1d] = NULL\n",
440 if (ldap_modify_s(ld, Entry.DN, mods))
443 printf(" Modification complete.\n" );
446 for (i = 0; mods[i] != NULL; i++)
447 free_mod_struct(mods[i]);
452 ovalues( char *attr )
454 struct attribute *ap;
457 * Lookup the attribute with quipu_name 'attr' in the Entry
458 * structure and return the number of values.
460 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
461 if (!strcasecmp(ap->quipu_name, attr))
462 return(ap->number_of_values);
464 /* should never get to this point unless 'attr' was something odd */