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 <sys/resource.h>
27 #include <ldapconfig.h>
30 extern struct entry Entry;
34 extern LDAPMessage *find();
36 static int load_editor();
37 static int modifiable();
38 static int print_attrs_and_values();
40 static int write_entry();
42 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 */
54 extern int bind_status;
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)
108 (void) write_entry();
109 (void) unlink(entry_temp_file);
110 ldap_uncache_entry(ld, Entry.DN);
117 char *cp, *editor = UD_DEFAULT_EDITOR;
118 static char template[MED_BUF_SIZE];
119 extern char * mktemp();
120 extern int isgroup(), fatal();
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 );
176 if ((pid = fork()) == 0) {
177 /* child - edit the Directory entry */
178 (void) SIGNAL(SIGINT, SIG_IGN);
179 (void) execlp(editor, editor, entry_temp_file, NULL);
181 (void) fatal(editor);
184 /* parent - wait until the child proc is done editing */
185 handler = SIGNAL(SIGINT, SIG_IGN);
186 (void) wait(&status);
187 (void) SIGNAL(SIGINT, handler);
196 static int print_attrs_and_values(fp, attrs, flag)
198 struct attribute attrs[];
203 for (i = 0; attrs[i].quipu_name != NULL; i++) {
204 if (!modifiable(attrs[i].quipu_name, flag|ATTR_FLAG_MAY_EDIT))
206 fprintf(fp, "%s\n", attrs[i].quipu_name);
207 if ( attrs[i].number_of_values > MAX_VALUES ) {
208 printf(" The %s attribute has more than %d values.\n",
209 attrs[i].quipu_name, MAX_VALUES );
210 printf(" You cannot use the vedit command on this entry. Sorry!\n" );
213 for (j = 0; j < attrs[i].number_of_values; j++)
214 fprintf(fp, "\t%s\n", attrs[i].values[j]);
219 static modifiable(s, flag)
224 extern struct attribute attrlist[];
226 for (i = 0; attrlist[i].quipu_name != NULL; i++) {
227 if (strcasecmp(s, attrlist[i].quipu_name))
229 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
233 /* should never be here */
239 int i = 0, j, number_of_values = -1;
242 char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
244 LDAPMod *mods[MAX_ATTRS + 1];
245 LDAPMod *modp = NULL;
247 extern char * code_to_str();
248 extern void free_mod_struct();
250 /* parse the file and write the values to the Directory */
251 if ((fp = fopen(entry_temp_file, "r")) == NULL) {
256 (void) fgets(line, sizeof(line), fp);
259 line[strlen(line) - 1] = '\0'; /* kill newline */
263 if (isspace(*cp)) { /* value */
266 values[number_of_values++] = strdup(cp);
267 if ( number_of_values >= MAX_VALUES ) {
268 printf(" A maximum of %d values can be handled at one time. Sorry!\n", MAX_VALUES );
277 * If the number of values is greater than zero, then we
278 * know that this is not the first time through this
279 * loop, and we also know that we have a little bit
282 * o The modify operation needs to be changed from
283 * a DELETE to a REPLACE
285 * o The list of values pointer needs to be changed
286 * from NULL, to a NULL-terminated list of char
289 if (number_of_values > 0) {
290 modp->mod_op = LDAP_MOD_REPLACE;
291 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
295 modp->mod_values = vp;
296 for (j = 0; j < number_of_values; j++) {
297 *vp++ = strdup(values[j]);
298 (void) Free(values[j]);
303 * If there are no values, and there were no values to begin
304 * with, then there is nothing to do.
306 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
308 if (debug & D_MODIFY)
309 printf(" %s has zero values - skipping\n",
312 (void) Free(modp->mod_type);
313 modp->mod_type = strdup(cp);
314 modp->mod_op = LDAP_MOD_DELETE;
315 modp->mod_values = NULL;
319 * Fetch a new modify structure.
321 * Assume a DELETE operation with no values.
323 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
327 modp->mod_values = NULL;
328 modp->mod_type = strdup(cp);
329 modp->mod_op = LDAP_MOD_DELETE;
331 number_of_values = 0;
335 /* check the last one too */
336 if (number_of_values > 0) {
337 modp->mod_op = LDAP_MOD_REPLACE;
339 * Fetch some value pointers.
341 * number_of_values To store the values
342 * 1 For the NULL terminator
343 * 1 In case we need it to store
344 * the RDN as on of the values
345 * of 'cn' in case it isn't there
347 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
348 2))) == (char **) NULL) {
352 modp->mod_values = vp;
353 for (j = 0; j < number_of_values; j++) {
354 *vp++ = strdup(values[j]);
355 (void) Free(values[j]);
359 else if ((number_of_values == 0) &&
360 (ovalues(mods[i - 1]->mod_type) == 0)) {
362 if (debug & D_MODIFY)
363 printf(" %s has zero values - skipping\n",
364 mods[i - 1]->mod_type);
366 Free(mods[i - 1]->mod_type);
370 mods[i] = (LDAPMod *) NULL;
373 * If one of the mods pointers is 'cn', be sure that the RDN is one
376 for (j = 0; j < i; j++) {
377 if (strcasecmp("cn", mods[j]->mod_type))
380 * True only if there WERE values, but the person deleted
383 if (mods[j]->mod_values == NULL) {
384 mods[j]->mod_op = LDAP_MOD_REPLACE;
385 if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
389 mods[j]->mod_values = vp;
390 *vp++ = strdup(Entry.name);
395 * Be sure that one of the values of 'cn' is the RDN.
397 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
398 if (strcasecmp(*vp, Entry.DN))
403 *vp++ = strdup(Entry.name);
409 if (debug & D_MODIFY) {
412 printf(" ld = 0x%x\n", ld);
413 printf(" dn = [%s]\n", Entry.DN);
414 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
415 printf(" mods[%d]->mod_op = %s\n",
416 x, code_to_str(mods[x]->mod_op));
417 printf(" mods[%d]->mod_type = %s\n",
418 x, mods[x]->mod_type);
419 if (mods[x]->mod_values == NULL)
420 printf(" mods[%d]->mod_values = NULL\n", x);
422 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
423 printf(" mods[%d]->mod_values[%1d] = %s\n",
424 x, y, mods[x]->mod_values[y]);
425 printf(" mods[%d]->mod_values[%1d] = NULL\n",
432 if (ldap_modify_s(ld, Entry.DN, mods))
435 printf(" Modification complete.\n" );
438 for (i = 0; mods[i] != NULL; i++)
439 free_mod_struct(mods[i]);
446 struct attribute *ap;
449 * Lookup the attribute with quipu_name 'attr' in the Entry
450 * structure and return the number of values.
452 for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
453 if (!strcasecmp(ap->quipu_name, attr))
454 return(ap->number_of_values);
456 /* should never get to this point unless 'attr' was something odd */