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>
35 #include <ldapconfig.h>
38 static int load_editor( void );
39 static int modifiable( char *s, short flag );
40 static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short flag );
41 static int ovalues( char *attr );
42 static void write_entry( void );
44 static char *entry_temp_file;
50 LDAPMessage *mp; /* returned from find() */
51 char *dn, **rdns; /* distinguished name */
52 char name[MED_BUF_SIZE]; /* entry to modify */
56 printf("->edit(%s)\n", who);
59 * One must be bound in order to edit an entry.
61 if (bind_status == UD_NOT_BOUND) {
62 if (auth((char *) NULL, 1) < 0)
67 * First, decide what entry we are going to modify. If the
68 * user has not included a name on the modify command line,
69 * we will use the person who was last looked up with a find
70 * command. If there is no value there either, we don't know
73 * Once we know who to modify, be sure that they exist, and
78 printf(" Enter the name of the person or\n");
79 printf(" group whose entry you want to edit: ");
82 printf(" Edit whose entry? ");
84 fetch_buffer(name, sizeof(name), stdin);
90 if ((mp = find(who, TRUE)) == NULL) {
91 (void) ldap_msgfree(mp);
92 printf(" Could not locate \"%s\" in the Directory.\n", who);
95 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
96 rdns = ldap_explode_dn(dn, TRUE);
99 printf("\n Editing directory entry \"%s\"...\n", *rdns);
102 (void) ldap_msgfree(mp);
103 (void) ldap_value_free(rdns);
104 if (load_editor() < 0)
107 (void) unlink(entry_temp_file);
108 ldap_uncache_entry(ld, Entry.DN);
116 char *cp, *editor = UD_DEFAULT_EDITOR;
117 static char template[MED_BUF_SIZE];
126 printf("->load_editor()\n");
129 /* write the entry into a temp file */
130 (void) strcpy(template, "/tmp/udEdit.XXXXXX");
131 if ((entry_temp_file = mktemp(template)) == NULL) {
135 if ((fp = fopen(entry_temp_file, "w")) == NULL) {
139 fprintf(fp, "## Directory entry of %s\n", Entry.name);
141 fprintf(fp, "## Syntax is:\n");
142 fprintf(fp, "## <attribute-name>\n");
143 fprintf(fp, "## <TAB> <value 1>\n");
144 fprintf(fp, "## <TAB> : :\n");
145 fprintf(fp, "## <TAB> <value N>\n");
146 fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
150 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
152 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
156 (void) unlink(entry_temp_file);
160 /* edit the temp file with the editor of choice */
161 if ((cp = getenv("EDITOR")) != NULL)
166 if (( p = strrchr( editor, '/' )) == NULL ) {
171 printf(" Using %s as the editor...\n", p );
177 rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
182 if ((pid = fork()) == 0) {
183 /* child - edit the Directory entry */
184 (void) SIGNAL(SIGINT, SIG_IGN);
185 (void) execlp(editor, editor, entry_temp_file, NULL);
187 (void) fatal(editor);
190 /* parent - wait until the child proc is done editing */
191 RETSIGTYPE (*handler)();
192 handler = SIGNAL(SIGINT, SIG_IGN);
193 (void) wait(&status);
194 (void) SIGNAL(SIGINT, handler);
205 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
209 for (i = 0; attrs[i].quipu_name != NULL; i++) {
210 if (!modifiable(attrs[i].quipu_name,
211 (short) (flag|ATTR_FLAG_MAY_EDIT)))
216 fprintf(fp, "%s\n", attrs[i].quipu_name);
217 if ( attrs[i].number_of_values > MAX_VALUES ) {
218 printf(" The %s attribute has more than %d values.\n",
219 attrs[i].quipu_name, MAX_VALUES );
220 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
223 for (j = 0; j < attrs[i].number_of_values; j++)
224 fprintf(fp, "\t%s\n", attrs[i].values[j]);
230 modifiable( char *s, short int flag )
234 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
235 if (strcasecmp(s, attrlist[i].quipu_name))
237 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
241 /* should never be here */
248 int i = 0, j, number_of_values = -1;
251 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
253 LDAPMod *mods[MAX_ATTRS + 1];
254 LDAPMod *modp = NULL;
256 /* parse the file and write the values to the Directory */
257 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
262 (void) fgets(line, sizeof(line), fp);
265 line[strlen(line) - 1] = '\0'; /* kill newline */
269 if (isspace((unsigned char)*cp)) { /* value */
270 while (isspace((unsigned char)*cp))
272 values[number_of_values++] = strdup(cp);
273 if ( number_of_values >= MAX_VALUES ) {
274 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
280 while (isspace((unsigned char)*cp))
283 * If the number of values is greater than zero, then we
284 * know that this is not the first time through this
285 * loop, and we also know that we have a little bit
288 * o The modify operation needs to be changed from
289 * a DELETE to a REPLACE
291 * o The list of values pointer needs to be changed
292 * from NULL, to a NULL-terminated list of char
295 if (number_of_values > 0) {
296 modp->mod_op = LDAP_MOD_REPLACE;
297 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
301 modp->mod_values = vp;
302 for (j = 0; j < number_of_values; j++) {
303 *vp++ = strdup(values[j]);
304 (void) Free(values[j]);
309 * If there are no values, and there were no values to begin
310 * with, then there is nothing to do.
312 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
314 if (debug & D_MODIFY)
315 printf(" %s has zero values - skipping\n",
318 (void) Free(modp->mod_type);
319 modp->mod_type = strdup(cp);
320 modp->mod_op = LDAP_MOD_DELETE;
321 modp->mod_values = NULL;
325 * Fetch a new modify structure.
327 * Assume a DELETE operation with no values.
329 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
333 modp->mod_values = NULL;
334 modp->mod_type = strdup(cp);
335 modp->mod_op = LDAP_MOD_DELETE;
337 number_of_values = 0;
341 /* check the last one too */
342 if (number_of_values > 0) {
343 modp->mod_op = LDAP_MOD_REPLACE;
345 * Fetch some value pointers.
347 * number_of_values To store the values
348 * 1 For the NULL terminator
349 * 1 In case we need it to store
350 * the RDN as on of the values
351 * of 'cn' in case it isn't there
353 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
354 2))) == (char **) NULL) {
358 modp->mod_values = vp;
359 for (j = 0; j < number_of_values; j++) {
360 *vp++ = strdup(values[j]);
361 (void) Free(values[j]);
365 else if ((number_of_values == 0) &&
366 (ovalues(mods[i - 1]->mod_type) == 0)) {
368 if (debug & D_MODIFY)
369 printf(" %s has zero values - skipping\n",
370 mods[i - 1]->mod_type);
372 Free(mods[i - 1]->mod_type);
376 mods[i] = (LDAPMod *) NULL;
379 * If one of the mods pointers is 'cn', be sure that the RDN is one
382 for (j = 0; j < i; j++) {
383 if (strcasecmp("cn", mods[j]->mod_type))
386 * True only if there WERE values, but the person deleted
389 if (mods[j]->mod_values == NULL) {
390 mods[j]->mod_op = LDAP_MOD_REPLACE;
391 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
395 mods[j]->mod_values = vp;
396 *vp++ = strdup(Entry.name);
401 * Be sure that one of the values of 'cn' is the RDN.
403 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
404 if (strcasecmp(*vp, Entry.DN))
409 *vp++ = strdup(Entry.name);
415 if (debug & D_MODIFY) {
418 printf(" ld = 0x%x\n", ld);
419 printf(" dn = [%s]\n", Entry.DN);
420 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
421 printf(" mods[%d]->mod_op = %s\n",
422 x, code_to_str(mods[x]->mod_op));
423 printf(" mods[%d]->mod_type = %s\n",
424 x, mods[x]->mod_type);
425 if (mods[x]->mod_values == NULL)
426 printf(" mods[%d]->mod_values = NULL\n", x);
428 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
429 printf(" mods[%d]->mod_values[%1d] = %s\n",
430 x, y, mods[x]->mod_values[y]);
431 printf(" mods[%d]->mod_values[%1d] = NULL\n",
438 if (ldap_modify_s(ld, Entry.DN, mods))
441 printf(" Modification complete.\n" );
444 for (i = 0; mods[i] != NULL; i++)
445 free_mod_struct(mods[i]);
450 ovalues( char *attr )
452 struct attribute *ap;
455 * Lookup the attribute with quipu_name 'attr' in the Entry
456 * structure and return the number of values.
458 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
459 if (!strcasecmp(ap->quipu_name, attr))
460 return(ap->number_of_values);
462 /* should never get to this point unless 'attr' was something odd */