]> git.sur5r.net Git - openldap/blob - clients/ud/edit.c
merged with autoconf branch
[openldap] / clients / ud / edit.c
1 /*
2  * Copyright (c) 1994  Regents of the University of Michigan.
3  * All rights reserved.
4  *
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.
11  */
12
13 #include "portable.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include <ac/signal.h>
19 #include <ac/string.h>
20 #include <ac/time.h>
21 #include <ac/wait.h>
22
23 #include <sys/resource.h>
24
25 #include <lber.h>
26 #include <ldap.h>
27 #include <ldapconfig.h>
28 #include "ud.h"
29
30 extern struct entry Entry; 
31 extern int verbose;
32 extern LDAP *ld;
33
34 extern LDAPMessage *find();
35
36 static int load_editor();
37 static int modifiable();
38 static int print_attrs_and_values();
39 static int ovalues();
40 static int write_entry();
41
42 static char *entry_temp_file;
43
44 #ifdef DEBUG
45 extern int debug;
46 #endif
47
48 edit(who)
49 char *who;
50 {
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;
55
56 #ifdef DEBUG
57         if (debug & D_TRACE)
58                 printf("->edit(%s)\n", who);
59 #endif
60         /*
61          *  One must be bound in order to edit an entry.
62          */
63         if (bind_status == UD_NOT_BOUND) {
64                 if (auth((char *) NULL, 1) < 0)
65                         return;
66         }
67
68         /*
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
73          *  who to modify.
74          *
75          *  Once we know who to modify, be sure that they exist, and
76          *  parse out their DN.
77          */
78         if (who == NULL) {
79                 if (verbose) {
80                         printf("  Enter the name of the person or\n");
81                         printf("  group whose entry you want to edit: ");
82                 }
83                 else
84                         printf("  Edit whose entry? ");
85                 fflush(stdout);
86                 fetch_buffer(name, sizeof(name), stdin);
87                 if (name[0] != '\0')
88                         who = name;
89                 else
90                         return;
91         }
92         if ((mp = find(who, TRUE)) == NULL) {
93                 (void) ldap_msgfree(mp);
94                 printf("  Could not locate \"%s\" in the Directory.\n", who);
95                 return;
96         }
97         dn = ldap_get_dn(ld, ldap_first_entry(ld, mp));
98         rdns = ldap_explode_dn(dn, TRUE);
99         Free(dn);
100         if (verbose) {
101                 printf("\n  Editing directory entry \"%s\"...\n", *rdns);
102         }
103         parse_answer(mp);
104         (void) ldap_msgfree(mp);
105         (void) ldap_value_free(rdns);
106         if (load_editor() < 0)
107                 return;
108         (void) write_entry();
109         (void) unlink(entry_temp_file);
110         ldap_uncache_entry(ld, Entry.DN);
111         return;
112 }
113
114 static load_editor()
115 {
116         FILE *fp;
117         char *cp, *editor = UD_DEFAULT_EDITOR;
118         static char template[MED_BUF_SIZE];
119         extern char * mktemp();
120         extern int isgroup(), fatal();
121         int pid;
122         int status;
123         int rc;
124         void (*handler)();
125         
126 #ifdef DEBUG
127         if (debug & D_TRACE)
128                 printf("->load_editor()\n");
129 #endif
130
131         /* write the entry into a temp file */
132         (void) strcpy(template, "/tmp/udEdit.XXXXXX");
133         if ((entry_temp_file = mktemp(template)) == NULL) {
134                 perror("mktemp");
135                 return(-1);
136         }
137         if ((fp = fopen(entry_temp_file, "w")) == NULL) {
138                 perror("fopen");
139                 return(-1);
140         }
141         fprintf(fp, "## Directory entry of %s\n", Entry.name);
142         fprintf(fp, "##\n");
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");
149         fprintf(fp, "##\n");
150         fflush(fp);
151         if (isgroup())
152                 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
153         else
154                 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
155         fclose(fp);
156
157         if ( rc != 0 ) {
158             (void) unlink(entry_temp_file);
159             return( rc );
160         }
161
162         /* edit the temp file with the editor of choice */
163         if ((cp = getenv("EDITOR")) != NULL)
164                 editor = cp;
165         if (verbose) {
166                 char    *p;
167
168                 if (( p = strrchr( editor, '/' )) == NULL ) {
169                         p = editor;
170                 } else {
171                         ++p;
172                 }
173                 printf("  Using %s as the editor...\n", p );
174                 sleep(2);
175         }
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);
180                 /*NOTREACHED*/
181                 (void) fatal(editor);   
182         }
183         else if (pid > 0) {
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);
188         }
189         else {
190                 fatal("fork");
191                 /*NOTREACHED*/
192         }
193         return(0);
194 }
195
196 static int print_attrs_and_values(fp, attrs, flag)
197 FILE *fp;
198 struct attribute attrs[];
199 short flag;
200 {
201         register int i, j;
202
203         for (i = 0; attrs[i].quipu_name != NULL; i++) {
204                 if (!modifiable(attrs[i].quipu_name, flag|ATTR_FLAG_MAY_EDIT))
205                         continue;
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" );
211                         return( -1 );
212                 }
213                 for (j = 0; j < attrs[i].number_of_values; j++)
214                         fprintf(fp, "\t%s\n", attrs[i].values[j]);
215         }
216         return( 0 );
217 }
218
219 static modifiable(s, flag)
220 char *s;
221 short flag;
222 {
223         register int i;
224         extern struct attribute attrlist[];
225
226         for (i = 0; attrlist[i].quipu_name != NULL; i++) {
227                 if (strcasecmp(s, attrlist[i].quipu_name))
228                         continue;
229                 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
230                         return(FALSE);
231                 return(TRUE);
232         }
233         /* should never be here */
234         return(FALSE);
235 }
236
237 static write_entry()
238 {
239         int i = 0, j, number_of_values = -1;
240
241         FILE *fp;
242         char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
243
244         LDAPMod *mods[MAX_ATTRS + 1];
245         LDAPMod *modp = NULL;
246
247         extern char * code_to_str();
248         extern void free_mod_struct();
249
250         /* parse the file and write the values to the Directory */
251         if ((fp = fopen(entry_temp_file, "r")) == NULL) {
252                 perror("fopen");
253                 return;
254         }
255         for (;;) {
256                 (void) fgets(line, sizeof(line), fp);
257                 if (feof(fp))
258                         break;
259                 line[strlen(line) - 1] = '\0';          /* kill newline */
260                 cp = line;
261                 if (*cp == '#')
262                         continue;
263                 if (isspace(*cp)) {     /* value */
264                         while (isspace(*cp))
265                                 cp++;
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 );
269                                 return;
270                         }
271                         continue;
272                 }
273                 /* attribute */
274                 while (isspace(*cp))
275                         cp++;
276                 /*
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
280                  *  of work to do:
281                  *
282                  *      o The modify operation needs to be changed from
283                  *        a DELETE to a REPLACE
284                  *
285                  *      o The list of values pointer needs to be changed
286                  *        from NULL, to a NULL-terminated list of char
287                  *        pointers.
288                  */
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) {
292                                 fatal("Malloc");
293                                 /*NOTREACHED*/
294                         }
295                         modp->mod_values = vp;
296                         for (j = 0; j < number_of_values; j++) {
297                                 *vp++ = strdup(values[j]);
298                                 (void) Free(values[j]);
299                         }
300                         *vp = NULL;
301                 }
302                 /*
303                  *  If there are no values, and there were no values to begin
304                  *  with, then there is nothing to do.
305                  */
306                 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
307 #ifdef DEBUG
308                         if (debug & D_MODIFY)
309                                 printf(" %s has zero values - skipping\n",
310                                                 modp->mod_type);
311 #endif
312                         (void) Free(modp->mod_type);
313                         modp->mod_type = strdup(cp);
314                         modp->mod_op = LDAP_MOD_DELETE;
315                         modp->mod_values = NULL;
316                         continue;
317                 }
318                 /*
319                  *  Fetch a new modify structure.
320                  *
321                  *  Assume a DELETE operation with no values.
322                  */
323                 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
324                         fatal("Malloc");
325                         /*NOTREACHED*/
326                 }
327                 modp->mod_values = NULL;
328                 modp->mod_type = strdup(cp);
329                 modp->mod_op = LDAP_MOD_DELETE;
330                 mods[i++] = modp;
331                 number_of_values = 0;
332         }
333         fclose(fp);
334
335         /* check the last one too */
336         if (number_of_values > 0) {
337                 modp->mod_op = LDAP_MOD_REPLACE;
338                 /*
339                  *  Fetch some value pointers.
340                  *
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
346                  */
347                 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
348                                                  2))) == (char **) NULL) {
349                         fatal("Malloc");
350                         /*NOTREACHED*/
351                 }
352                 modp->mod_values = vp;
353                 for (j = 0; j < number_of_values; j++) {
354                         *vp++ = strdup(values[j]);
355                         (void) Free(values[j]);
356                 }
357                 *vp = NULL;
358         }
359         else if ((number_of_values == 0) && 
360                                 (ovalues(mods[i - 1]->mod_type) == 0)) {
361 #ifdef DEBUG
362                 if (debug & D_MODIFY)
363                         printf(" %s has zero values - skipping\n",
364                                         mods[i - 1]->mod_type);
365 #endif
366                 Free(mods[i - 1]->mod_type);
367                 Free(mods[i - 1]);
368                 i--;
369         }
370         mods[i] = (LDAPMod *) NULL;
371
372         /* 
373          * If one of the mods pointers is 'cn', be sure that the RDN is one
374          * of the values.
375          */
376         for (j = 0; j < i; j++) {
377                 if (strcasecmp("cn", mods[j]->mod_type))
378                         continue;
379                 /*
380                  *  True only if there WERE values, but the person deleted
381                  *  them all.
382                  */
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) {
386                                 fatal("Malloc");
387                                 /*NOTREACHED*/
388                         }
389                         mods[j]->mod_values = vp;
390                         *vp++ = strdup(Entry.name);
391                         *vp = NULL;
392                         break;
393                 }
394                 /*
395                  *  Be sure that one of the values of 'cn' is the RDN.
396                  */
397                 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
398                         if (strcasecmp(*vp, Entry.DN))
399                                 continue;
400                         break;
401                 }
402                 if (*vp == NULL) {
403                         *vp++ = strdup(Entry.name);
404                         *vp = NULL;
405                         break;
406                 }
407         }
408 #ifdef DEBUG
409         if (debug & D_MODIFY) {
410                 register int x, y;
411
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);
421                         else {
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",
426                                                                         x, y);
427                         }
428                         printf("\n");
429                 }
430         }
431 #endif
432         if (ldap_modify_s(ld, Entry.DN, mods))
433                 mod_perror(ld);
434         else
435                 printf("  Modification complete.\n" );
436
437         /* clean up */
438         for (i = 0; mods[i] != NULL; i++)
439                 free_mod_struct(mods[i]);
440         return;
441 }
442
443 static ovalues(attr)
444 char *attr;
445 {
446         struct attribute *ap;
447
448         /*
449          *  Lookup the attribute with quipu_name 'attr' in the Entry
450          *  structure and return the number of values.
451          */
452         for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
453                 if (!strcasecmp(ap->quipu_name, attr))
454                         return(ap->number_of_values);
455         }
456         /* should never get to this point unless 'attr' was something odd */
457         return(0);
458 }