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