2 * Copyright (c) 1994 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
17 #include <ac/stdlib.h>
19 #include <ac/signal.h>
20 #include <ac/string.h>
24 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_RESOURCE_H
27 #include <sys/resource.h>
36 #include "ldap_defaults.h"
39 static int load_editor( void );
40 static int modifiable( char *s, short flag );
41 static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short flag );
42 static int ovalues( char *attr );
43 static void write_entry( void );
45 static char *entry_temp_file;
51 LDAPMessage *mp; /* returned from find() */
52 char *dn, **rdns; /* distinguished name */
53 char name[MED_BUF_SIZE]; /* entry to modify */
57 printf("->edit(%s)\n", who);
60 * One must be bound in order to edit an entry.
62 if (bind_status == UD_NOT_BOUND) {
63 if (auth((char *) NULL, 1) < 0)
68 * First, decide what entry we are going to modify. If the
69 * user has not included a name on the modify command line,
70 * we will use the person who was last looked up with a find
71 * command. If there is no value there either, we don't know
74 * Once we know who to modify, be sure that they exist, and
79 printf(" Enter the name of the person or\n");
80 printf(" group whose entry you want to edit: ");
83 printf(" Edit whose entry? ");
85 fetch_buffer(name, sizeof(name), stdin);
91 if ((mp = find(who, TRUE)) == NULL) {
92 (void) ldap_msgfree(mp);
93 printf(" Could not locate \"%s\" in the Directory.\n", who);
96 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
97 rdns = ldap_explode_dn(dn, TRUE);
100 printf("\n Editing directory entry \"%s\"...\n", *rdns);
103 (void) ldap_msgfree(mp);
104 (void) ldap_value_free(rdns);
105 if (load_editor() < 0)
108 (void) unlink(entry_temp_file);
109 ldap_uncache_entry(ld, Entry.DN);
117 char *cp, *editor = UD_DEFAULT_EDITOR;
118 static char template[MED_BUF_SIZE];
127 printf("->load_editor()\n");
130 /* write the entry into a temp file */
131 (void) strcpy(template, "/tmp/udEdit.XXXXXX");
132 if ((entry_temp_file = mktemp(template)) == NULL) {
136 if ((fp = fopen(entry_temp_file, "w")) == NULL) {
140 fprintf(fp, "## Directory entry of %s\n", Entry.name);
142 fprintf(fp, "## Syntax is:\n");
143 fprintf(fp, "## <attribute-name>\n");
144 fprintf(fp, "## <TAB> <value 1>\n");
145 fprintf(fp, "## <TAB> : :\n");
146 fprintf(fp, "## <TAB> <value N>\n");
147 fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
151 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
153 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
157 (void) unlink(entry_temp_file);
161 /* edit the temp file with the editor of choice */
162 if ((cp = getenv("EDITOR")) != NULL)
167 if (( p = strrchr( editor, '/' )) == NULL ) {
172 printf(" Using %s as the editor...\n", p );
178 rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
183 if ((pid = fork()) == 0) {
184 /* child - edit the Directory entry */
185 (void) SIGNAL(SIGINT, SIG_IGN);
186 (void) execlp(editor, editor, entry_temp_file, NULL);
188 (void) fatal(editor);
191 /* parent - wait until the child proc is done editing */
192 RETSIGTYPE (*handler)();
193 handler = SIGNAL(SIGINT, SIG_IGN);
194 (void) wait(&status);
195 (void) SIGNAL(SIGINT, handler);
206 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
210 for (i = 0; attrs[i].quipu_name != NULL; i++) {
211 if (!modifiable(attrs[i].quipu_name,
212 (short) (flag|ATTR_FLAG_MAY_EDIT)))
217 fprintf(fp, "%s\n", attrs[i].quipu_name);
218 if ( attrs[i].number_of_values > MAX_VALUES ) {
219 printf(" The %s attribute has more than %d values.\n",
220 attrs[i].quipu_name, MAX_VALUES );
221 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
224 for (j = 0; j < attrs[i].number_of_values; j++)
225 fprintf(fp, "\t%s\n", attrs[i].values[j]);
231 modifiable( char *s, short int flag )
235 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
236 if (strcasecmp(s, attrlist[i].quipu_name))
238 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
242 /* should never be here */
249 int i = 0, j, number_of_values = -1;
252 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
254 LDAPMod *mods[MAX_ATTRS + 1];
255 LDAPMod *modp = NULL;
257 /* parse the file and write the values to the Directory */
258 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
263 (void) fgets(line, sizeof(line), fp);
266 line[strlen(line) - 1] = '\0'; /* kill newline */
270 if (isspace((unsigned char)*cp)) { /* value */
271 while (isspace((unsigned char)*cp))
273 values[number_of_values++] = strdup(cp);
274 if ( number_of_values >= MAX_VALUES ) {
275 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
281 while (isspace((unsigned char)*cp))
284 * If the number of values is greater than zero, then we
285 * know that this is not the first time through this
286 * loop, and we also know that we have a little bit
289 * o The modify operation needs to be changed from
290 * a DELETE to a REPLACE
292 * o The list of values pointer needs to be changed
293 * from NULL, to a NULL-terminated list of char
296 if (number_of_values > 0) {
297 modp->mod_op = LDAP_MOD_REPLACE;
298 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
302 modp->mod_values = vp;
303 for (j = 0; j < number_of_values; j++) {
304 *vp++ = strdup(values[j]);
305 (void) Free(values[j]);
310 * If there are no values, and there were no values to begin
311 * with, then there is nothing to do.
313 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
315 if (debug & D_MODIFY)
316 printf(" %s has zero values - skipping\n",
319 (void) Free(modp->mod_type);
320 modp->mod_type = strdup(cp);
321 modp->mod_op = LDAP_MOD_DELETE;
322 modp->mod_values = NULL;
326 * Fetch a new modify structure.
328 * Assume a DELETE operation with no values.
330 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
334 modp->mod_values = NULL;
335 modp->mod_type = strdup(cp);
336 modp->mod_op = LDAP_MOD_DELETE;
338 number_of_values = 0;
342 /* check the last one too */
343 if (number_of_values > 0) {
344 modp->mod_op = LDAP_MOD_REPLACE;
346 * Fetch some value pointers.
348 * number_of_values To store the values
349 * 1 For the NULL terminator
350 * 1 In case we need it to store
351 * the RDN as on of the values
352 * of 'cn' in case it isn't there
354 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
355 2))) == (char **) NULL) {
359 modp->mod_values = vp;
360 for (j = 0; j < number_of_values; j++) {
361 *vp++ = strdup(values[j]);
362 (void) Free(values[j]);
366 else if ((number_of_values == 0) &&
367 (ovalues(mods[i - 1]->mod_type) == 0)) {
369 if (debug & D_MODIFY)
370 printf(" %s has zero values - skipping\n",
371 mods[i - 1]->mod_type);
373 Free(mods[i - 1]->mod_type);
377 mods[i] = (LDAPMod *) NULL;
380 * If one of the mods pointers is 'cn', be sure that the RDN is one
383 for (j = 0; j < i; j++) {
384 if (strcasecmp("cn", mods[j]->mod_type))
387 * True only if there WERE values, but the person deleted
390 if (mods[j]->mod_values == NULL) {
391 mods[j]->mod_op = LDAP_MOD_REPLACE;
392 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
396 mods[j]->mod_values = vp;
397 *vp++ = strdup(Entry.name);
402 * Be sure that one of the values of 'cn' is the RDN.
404 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
405 if (strcasecmp(*vp, Entry.DN))
410 *vp++ = strdup(Entry.name);
416 if (debug & D_MODIFY) {
419 printf(" ld = 0x%x\n", ld);
420 printf(" dn = [%s]\n", Entry.DN);
421 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
422 printf(" mods[%d]->mod_op = %s\n",
423 x, code_to_str(mods[x]->mod_op));
424 printf(" mods[%d]->mod_type = %s\n",
425 x, mods[x]->mod_type);
426 if (mods[x]->mod_values == NULL)
427 printf(" mods[%d]->mod_values = NULL\n", x);
429 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
430 printf(" mods[%d]->mod_values[%1d] = %s\n",
431 x, y, mods[x]->mod_values[y]);
432 printf(" mods[%d]->mod_values[%1d] = NULL\n",
439 if (ldap_modify_s(ld, Entry.DN, mods))
442 printf(" Modification complete.\n" );
445 for (i = 0; mods[i] != NULL; i++)
446 free_mod_struct(mods[i]);
451 ovalues( char *attr )
453 struct attribute *ap;
456 * Lookup the attribute with quipu_name 'attr' in the Entry
457 * structure and return the number of values.
459 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
460 if (!strcasecmp(ap->quipu_name, attr))
461 return(ap->number_of_values);
463 /* should never get to this point unless 'attr' was something odd */