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.
18 #include <ac/signal.h>
19 #include <ac/string.h>
23 #include <ac/unistd.h>
25 #ifdef HAVE_SYS_RESOURCE_H
26 #include <sys/resource.h>
34 #include <ldapconfig.h>
37 static int load_editor( void );
38 static int modifiable( char *s, short flag );
39 static int print_attrs_and_values( FILE *fp, struct attribute *attrs, short flag );
40 static int ovalues( char *attr );
41 static void write_entry( void );
43 static char *entry_temp_file;
49 LDAPMessage *mp; /* returned from find() */
50 char *dn, **rdns; /* distinguished name */
51 char name[MED_BUF_SIZE]; /* entry to modify */
55 printf("->edit(%s)\n", who);
58 * One must be bound in order to edit an entry.
60 if (bind_status == UD_NOT_BOUND) {
61 if (auth((char *) NULL, 1) < 0)
66 * First, decide what entry we are going to modify. If the
67 * user has not included a name on the modify command line,
68 * we will use the person who was last looked up with a find
69 * command. If there is no value there either, we don't know
72 * Once we know who to modify, be sure that they exist, and
77 printf(" Enter the name of the person or\n");
78 printf(" group whose entry you want to edit: ");
81 printf(" Edit whose entry? ");
83 fetch_buffer(name, sizeof(name), stdin);
89 if ((mp = find(who, TRUE)) == NULL) {
90 (void) ldap_msgfree(mp);
91 printf(" Could not locate \"%s\" in the Directory.\n", who);
94 dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
95 rdns = ldap_explode_dn(dn, TRUE);
98 printf("\n Editing directory entry \"%s\"...\n", *rdns);
101 (void) ldap_msgfree(mp);
102 (void) ldap_value_free(rdns);
103 if (load_editor() < 0)
106 (void) unlink(entry_temp_file);
107 ldap_uncache_entry(ld, Entry.DN);
115 char *cp, *editor = UD_DEFAULT_EDITOR;
116 static char template[MED_BUF_SIZE];
125 printf("->load_editor()\n");
128 /* write the entry into a temp file */
129 (void) strcpy(template, "/tmp/udEdit.XXXXXX");
130 if ((entry_temp_file = mktemp(template)) == NULL) {
134 if ((fp = fopen(entry_temp_file, "w")) == NULL) {
138 fprintf(fp, "## Directory entry of %s\n", Entry.name);
140 fprintf(fp, "## Syntax is:\n");
141 fprintf(fp, "## <attribute-name>\n");
142 fprintf(fp, "## <TAB> <value 1>\n");
143 fprintf(fp, "## <TAB> : :\n");
144 fprintf(fp, "## <TAB> <value N>\n");
145 fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
149 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
151 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
155 (void) unlink(entry_temp_file);
159 /* edit the temp file with the editor of choice */
160 if ((cp = getenv("EDITOR")) != NULL)
165 if (( p = strrchr( editor, '/' )) == NULL ) {
170 printf(" Using %s as the editor...\n", p );
176 rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
181 if ((pid = fork()) == 0) {
182 /* child - edit the Directory entry */
183 (void) SIGNAL(SIGINT, SIG_IGN);
184 (void) execlp(editor, editor, entry_temp_file, NULL);
186 (void) fatal(editor);
189 /* parent - wait until the child proc is done editing */
191 handler = SIGNAL(SIGINT, SIG_IGN);
192 (void) wait(&status);
193 (void) SIGNAL(SIGINT, handler);
204 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
208 for (i = 0; attrs[i].quipu_name != NULL; i++) {
209 if (!modifiable(attrs[i].quipu_name,
210 (short) (flag|ATTR_FLAG_MAY_EDIT)))
215 fprintf(fp, "%s\n", attrs[i].quipu_name);
216 if ( attrs[i].number_of_values > MAX_VALUES ) {
217 printf(" The %s attribute has more than %d values.\n",
218 attrs[i].quipu_name, MAX_VALUES );
219 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
222 for (j = 0; j < attrs[i].number_of_values; j++)
223 fprintf(fp, "\t%s\n", attrs[i].values[j]);
229 modifiable( char *s, short int flag )
233 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
234 if (strcasecmp(s, attrlist[i].quipu_name))
236 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
240 /* should never be here */
247 int i = 0, j, number_of_values = -1;
250 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
252 LDAPMod *mods[MAX_ATTRS + 1];
253 LDAPMod *modp = NULL;
255 /* parse the file and write the values to the Directory */
256 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
261 (void) fgets(line, sizeof(line), fp);
264 line[strlen(line) - 1] = '\0'; /* kill newline */
268 if (isspace(*cp)) { /* value */
271 values[number_of_values++] = strdup(cp);
272 if ( number_of_values >= MAX_VALUES ) {
273 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
282 * If the number of values is greater than zero, then we
283 * know that this is not the first time through this
284 * loop, and we also know that we have a little bit
287 * o The modify operation needs to be changed from
288 * a DELETE to a REPLACE
290 * o The list of values pointer needs to be changed
291 * from NULL, to a NULL-terminated list of char
294 if (number_of_values > 0) {
295 modp->mod_op = LDAP_MOD_REPLACE;
296 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
300 modp->mod_values = vp;
301 for (j = 0; j < number_of_values; j++) {
302 *vp++ = strdup(values[j]);
303 (void) Free(values[j]);
308 * If there are no values, and there were no values to begin
309 * with, then there is nothing to do.
311 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
313 if (debug & D_MODIFY)
314 printf(" %s has zero values - skipping\n",
317 (void) Free(modp->mod_type);
318 modp->mod_type = strdup(cp);
319 modp->mod_op = LDAP_MOD_DELETE;
320 modp->mod_values = NULL;
324 * Fetch a new modify structure.
326 * Assume a DELETE operation with no values.
328 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
332 modp->mod_values = NULL;
333 modp->mod_type = strdup(cp);
334 modp->mod_op = LDAP_MOD_DELETE;
336 number_of_values = 0;
340 /* check the last one too */
341 if (number_of_values > 0) {
342 modp->mod_op = LDAP_MOD_REPLACE;
344 * Fetch some value pointers.
346 * number_of_values To store the values
347 * 1 For the NULL terminator
348 * 1 In case we need it to store
349 * the RDN as on of the values
350 * of 'cn' in case it isn't there
352 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
353 2))) == (char **) NULL) {
357 modp->mod_values = vp;
358 for (j = 0; j < number_of_values; j++) {
359 *vp++ = strdup(values[j]);
360 (void) Free(values[j]);
364 else if ((number_of_values == 0) &&
365 (ovalues(mods[i - 1]->mod_type) == 0)) {
367 if (debug & D_MODIFY)
368 printf(" %s has zero values - skipping\n",
369 mods[i - 1]->mod_type);
371 Free(mods[i - 1]->mod_type);
375 mods[i] = (LDAPMod *) NULL;
378 * If one of the mods pointers is 'cn', be sure that the RDN is one
381 for (j = 0; j < i; j++) {
382 if (strcasecmp("cn", mods[j]->mod_type))
385 * True only if there WERE values, but the person deleted
388 if (mods[j]->mod_values == NULL) {
389 mods[j]->mod_op = LDAP_MOD_REPLACE;
390 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
394 mods[j]->mod_values = vp;
395 *vp++ = strdup(Entry.name);
400 * Be sure that one of the values of 'cn' is the RDN.
402 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
403 if (strcasecmp(*vp, Entry.DN))
408 *vp++ = strdup(Entry.name);
414 if (debug & D_MODIFY) {
417 printf(" ld = 0x%x\n", ld);
418 printf(" dn = [%s]\n", Entry.DN);
419 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
420 printf(" mods[%d]->mod_op = %s\n",
421 x, code_to_str(mods[x]->mod_op));
422 printf(" mods[%d]->mod_type = %s\n",
423 x, mods[x]->mod_type);
424 if (mods[x]->mod_values == NULL)
425 printf(" mods[%d]->mod_values = NULL\n", x);
427 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
428 printf(" mods[%d]->mod_values[%1d] = %s\n",
429 x, y, mods[x]->mod_values[y]);
430 printf(" mods[%d]->mod_values[%1d] = NULL\n",
437 if (ldap_modify_s(ld, Entry.DN, mods))
440 printf(" Modification complete.\n" );
443 for (i = 0; mods[i] != NULL; i++)
444 free_mod_struct(mods[i]);
449 ovalues( char *attr )
451 struct attribute *ap;
454 * Lookup the attribute with quipu_name 'attr' in the Entry
455 * structure and return the number of values.
457 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
458 if (!strcasecmp(ap->quipu_name, attr))
459 return(ap->number_of_values);
461 /* should never get to this point unless 'attr' was something odd */