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