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