]> git.sur5r.net Git - openldap/blob - clients/ud/edit.c
Some minor bugs for dntest ""
[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 #include "lutil.h"
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 #ifndef HAVE_SPAWNLP
134         int pid;
135         int status;
136 #endif
137         int rc;
138         
139 #ifdef DEBUG
140         if (debug & D_TRACE)
141                 printf("->load_editor()\n");
142 #endif
143
144         strcpy(entry_temp_file, LDAP_TMPDIR LDAP_DIRSEP "udXXXXXX");
145
146         {
147                 int tmpfd = mkstemp(entry_temp_file);
148
149                 if( tmpfd < 0 ) {
150                         perror("mkstemp");
151                         return -1;
152                 }
153
154                 if ((fp = fdopen(tmpfd, "w")) == NULL) {
155                         perror("fdopen");
156                         return(-1);
157                 }
158         }
159
160         fprintf(fp, "## Directory entry of %s\n", Entry.name);
161         fprintf(fp, "##\n");
162         fprintf(fp, "## Syntax is:\n");
163         fprintf(fp, "## <attribute-name>\n");
164         fprintf(fp, "##         <TAB> <value 1>\n");
165         fprintf(fp, "##         <TAB>   :  :\n");
166         fprintf(fp, "##         <TAB> <value N>\n");
167         fprintf(fp, "## Lines beginning with a hash mark are comments.\n");
168         fprintf(fp, "##\n");
169         fflush(fp);
170         if (isgroup())
171                 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_GROUP_MOD);
172         else
173                 rc = print_attrs_and_values(fp, Entry.attrs, ATTR_FLAG_PERSON_MOD);
174         fclose(fp);
175
176         if ( rc != 0 ) {
177             (void) unlink(entry_temp_file);
178             return( rc );
179         }
180
181         /* edit the temp file with the editor of choice */
182         if ((cp = getenv("EDITOR")) != NULL)
183                 editor = cp;
184         if (verbose) {
185                 char    *p;
186
187                 if (( p = strrchr( editor, *LDAP_DIRSEP )) == NULL ) {
188                         p = editor;
189                 } else {
190                         ++p;
191                 }
192                 printf("  Using %s as the editor...\n", p );
193 #ifndef HAVE_SPAWNLP
194                 sleep(2);
195 #endif
196         }
197 #ifdef HAVE_SPAWNLP
198         rc = _spawnlp( _P_WAIT, editor, editor, entry_temp_file, NULL );
199         if(rc != 0) {
200                 fatal("spawnlp");
201         }
202 #else
203         if ((pid = fork()) == 0) {      
204                 /* child - edit the Directory entry */
205                 (void) SIGNAL(SIGINT, SIG_IGN);
206                 (void) execlp(editor, editor, entry_temp_file, NULL);
207                 /*NOTREACHED*/
208                 (void) fatal(editor);   
209         }
210         else if (pid > 0) {
211                 /* parent - wait until the child proc is done editing */
212                 RETSIGTYPE (*handler)();
213                 handler = SIGNAL(SIGINT, SIG_IGN);
214                 (void) wait(&status);
215                 (void) SIGNAL(SIGINT, handler);
216         }
217         else {
218                 fatal("fork");
219                 /*NOTREACHED*/
220         }
221 #endif
222         return(0);
223 }
224
225 static int
226 print_attrs_and_values( FILE *fp, struct attribute *attrs, short int flag )
227 {
228         register int i, j;
229
230         for (i = 0; attrs[i].quipu_name != NULL; i++) {
231                 if (!modifiable(attrs[i].quipu_name,
232                         (short) (flag|ATTR_FLAG_MAY_EDIT)))
233                 {
234                         continue;
235                 }
236
237                 fprintf(fp, "%s\n", attrs[i].quipu_name);
238                 if ( attrs[i].number_of_values > MAX_VALUES ) {
239                         printf("  The %s attribute has more than %d values.\n",
240                                 attrs[i].quipu_name, MAX_VALUES );
241                         printf("  You cannot use the vedit command on this entry.  Sorry!\n" );
242                         return( -1 );
243                 }
244                 for (j = 0; j < attrs[i].number_of_values; j++)
245                         fprintf(fp, "\t%s\n", attrs[i].values[j]);
246         }
247         return( 0 );
248 }
249
250 static int
251 modifiable( char *s, short int flag )
252 {
253         register int i;
254
255         for (i = 0; attrlist[i].quipu_name != NULL; i++) {
256                 if (strcasecmp(s, attrlist[i].quipu_name))
257                         continue;
258                 if ((attrlist[i].flags & flag) == ATTR_FLAG_NONE)
259                         return(FALSE);
260                 return(TRUE);
261         }
262         /* should never be here */
263         return(FALSE);
264 }
265
266 static void
267 write_entry( void )
268 {
269         int i = 0, j, number_of_values = -1;
270
271         FILE *fp;
272         char *cp, line[LARGE_BUF_SIZE], *values[MAX_VALUES], **vp;
273
274         LDAPMod *mods[MAX_ATTRS + 1];
275         LDAPMod *modp = NULL;
276
277         /* parse the file and write the values to the Directory */
278         if ((fp = fopen(entry_temp_file, "r")) == NULL) {
279                 perror("fopen");
280                 return;
281         }
282         for (;;) {
283                 (void) fgets(line, sizeof(line), fp);
284                 if (feof(fp))
285                         break;
286                 line[strlen(line) - 1] = '\0';          /* kill newline */
287                 cp = line;
288                 if (*cp == '#')
289                         continue;
290                 if (isspace((unsigned char)*cp)) {      /* value */
291                         while (isspace((unsigned char)*cp))
292                                 cp++;
293                         values[number_of_values++] = strdup(cp);
294                         if ( number_of_values >= MAX_VALUES ) {
295                                 printf("  A maximum of %d values can be handled at one time.  Sorry!\n", MAX_VALUES );
296                                 return;
297                         }
298                         continue;
299                 }
300                 /* attribute */
301                 while (isspace((unsigned char)*cp))
302                         cp++;
303                 /*
304                  *  If the number of values is greater than zero, then we
305                  *  know that this is not the first time through this
306                  *  loop, and we also know that we have a little bit
307                  *  of work to do:
308                  *
309                  *      o The modify operation needs to be changed from
310                  *        a DELETE to a REPLACE
311                  *
312                  *      o The list of values pointer needs to be changed
313                  *        from NULL, to a NULL-terminated list of char
314                  *        pointers.
315                  */
316                 if (number_of_values > 0) {
317                         modp->mod_op = LDAP_MOD_REPLACE;
318                         if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values + 2))) == (char **) NULL) {
319                                 fatal("Malloc");
320                                 /*NOTREACHED*/
321                         }
322                         modp->mod_values = vp;
323                         for (j = 0; j < number_of_values; j++) {
324                                 *vp++ = strdup(values[j]);
325                                 (void) Free(values[j]);
326                         }
327                         *vp = NULL;
328                 }
329                 /*
330                  *  If there are no values, and there were no values to begin
331                  *  with, then there is nothing to do.
332                  */
333                 if ((number_of_values == 0) && (ovalues(modp->mod_type) == 0)) {
334 #ifdef DEBUG
335                         if (debug & D_MODIFY)
336                                 printf(" %s has zero values - skipping\n",
337                                                 modp->mod_type);
338 #endif
339                         (void) Free(modp->mod_type);
340                         modp->mod_type = strdup(cp);
341                         modp->mod_op = LDAP_MOD_DELETE;
342                         modp->mod_values = NULL;
343                         continue;
344                 }
345                 /*
346                  *  Fetch a new modify structure.
347                  *
348                  *  Assume a DELETE operation with no values.
349                  */
350                 if ((modp = (LDAPMod *) Malloc(sizeof(LDAPMod))) == NULL) {
351                         fatal("Malloc");
352                         /*NOTREACHED*/
353                 }
354                 modp->mod_values = NULL;
355                 modp->mod_type = strdup(cp);
356                 modp->mod_op = LDAP_MOD_DELETE;
357                 mods[i++] = modp;
358                 number_of_values = 0;
359         }
360         fclose(fp);
361
362         /* check the last one too */
363         if (number_of_values > 0) {
364                 modp->mod_op = LDAP_MOD_REPLACE;
365                 /*
366                  *  Fetch some value pointers.
367                  *
368                  *      number_of_values        To store the values
369                  *              1               For the NULL terminator
370                  *              1               In case we need it to store
371                  *                              the RDN as on of the values
372                  *                              of 'cn' in case it isn't there
373                  */
374                 if ((vp = (char **) Malloc(sizeof(char *) * (number_of_values +
375                                                  2))) == (char **) NULL) {
376                         fatal("Malloc");
377                         /*NOTREACHED*/
378                 }
379                 modp->mod_values = vp;
380                 for (j = 0; j < number_of_values; j++) {
381                         *vp++ = strdup(values[j]);
382                         (void) Free(values[j]);
383                 }
384                 *vp = NULL;
385         }
386         else if ((number_of_values == 0) && 
387                                 (ovalues(mods[i - 1]->mod_type) == 0)) {
388 #ifdef DEBUG
389                 if (debug & D_MODIFY)
390                         printf(" %s has zero values - skipping\n",
391                                         mods[i - 1]->mod_type);
392 #endif
393                 Free(mods[i - 1]->mod_type);
394                 Free(mods[i - 1]);
395                 i--;
396         }
397         mods[i] = (LDAPMod *) NULL;
398
399         /* 
400          * If one of the mods pointers is 'cn', be sure that the RDN is one
401          * of the values.
402          */
403         for (j = 0; j < i; j++) {
404                 if (strcasecmp("cn", mods[j]->mod_type))
405                         continue;
406                 /*
407                  *  True only if there WERE values, but the person deleted
408                  *  them all.
409                  */
410                 if (mods[j]->mod_values == NULL) {
411                         mods[j]->mod_op = LDAP_MOD_REPLACE;
412                         if ((vp = (char **) Malloc(sizeof(char *) * 2)) == (char **) NULL) {
413                                 fatal("Malloc");
414                                 /*NOTREACHED*/
415                         }
416                         mods[j]->mod_values = vp;
417                         *vp++ = strdup(Entry.name);
418                         *vp = NULL;
419                         break;
420                 }
421                 /*
422                  *  Be sure that one of the values of 'cn' is the RDN.
423                  */
424                 for (vp = mods[j]->mod_values; *vp != NULL; vp++) {
425                         if (strcasecmp(*vp, Entry.DN))
426                                 continue;
427                         break;
428                 }
429                 if (*vp == NULL) {
430                         *vp++ = strdup(Entry.name);
431                         *vp = NULL;
432                         break;
433                 }
434         }
435 #ifdef DEBUG
436         if (debug & D_MODIFY) {
437                 register int x, y;
438
439                 printf("  ld = 0x%x\n", ld);
440                 printf("  dn = [%s]\n", Entry.DN);
441                 for (x = 0; mods[x] != (LDAPMod *) NULL; x++) {
442                         printf("  mods[%d]->mod_op = %s\n", 
443                                         x, code_to_str(mods[x]->mod_op));
444                         printf("  mods[%d]->mod_type = %s\n", 
445                                                         x, mods[x]->mod_type);
446                         if (mods[x]->mod_values == NULL)
447                                 printf("  mods[%d]->mod_values = NULL\n", x);
448                         else {
449                                 for (y = 0; mods[x]->mod_values[y] != NULL; y++)
450                                    printf("  mods[%d]->mod_values[%1d] = %s\n", 
451                                                 x, y, mods[x]->mod_values[y]);
452                                 printf("  mods[%d]->mod_values[%1d] = NULL\n",
453                                                                         x, y);
454                         }
455                         printf("\n");
456                 }
457         }
458 #endif
459         if (ldap_modify_s(ld, Entry.DN, mods))
460                 mod_perror(ld);
461         else
462                 printf("  Modification complete.\n" );
463
464         /* clean up */
465         for (i = 0; mods[i] != NULL; i++)
466                 free_mod_struct(mods[i]);
467         return;
468 }
469
470 static int
471 ovalues( char *attr )
472 {
473         struct attribute *ap;
474
475         /*
476          *  Lookup the attribute with quipu_name 'attr' in the Entry
477          *  structure and return the number of values.
478          */
479         for (ap = Entry.attrs; ap->quipu_name != NULL; ap++) {
480                 if (!strcasecmp(ap->quipu_name, attr))
481                         return(ap->number_of_values);
482         }
483         /* should never get to this point unless 'attr' was something odd */
484         return(0);
485 }