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