2 * Copyright (c) 1990 Regents of the University of Michigan.
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.
19 #include <ac/socket.h>
20 #include <ac/string.h>
21 #include <ac/syslog.h>
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
29 #include <sys/resource.h>
35 #include <ldapconfig.h>
38 #define GROUP_ERRORS 1
39 #define GROUP_REQUEST 2
40 #define GROUP_MEMBERS 3
42 #define ERRORS "errors"
43 #define REQUEST "request"
44 #define MEMBERS "members"
47 char *errorsfrom = NULL;
48 char *mailfrom = NULL;
59 #define E_USERUNKNOWN 1
65 #define E_JOINMEMBERNOEMAIL 7
66 #define E_MEMBERNOEMAIL 8
68 #define E_JOINMEMBERNOFAXNUM 10
69 #define E_MEMBERNOFAXNUM 11
70 #define E_FAXTOEMAILMBR 12
76 typedef struct groupto {
82 typedef struct baseinfo {
83 char *b_dn; /* dn to start searching at */
84 char b_rdnpref; /* give rdn's preference when searching? */
85 char *b_filter[3]; /* filter to apply - name substituted for %s */
86 /* (up to three of them) */
90 { "ou=People, o=University of Michigan, c=US", 0,
91 "uid=%s", "cn=%s", NULL,
92 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
93 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
94 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
95 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
99 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
101 static char *attrs[] = { "objectClass", "title", "postaladdress",
102 "telephoneNumber", "mail", "description",
103 "errorsTo", "rfc822ErrorsTo", "requestsTo",
104 "rfc822RequestsTo", "joinable", "cn", "member",
105 "facsimileTelephoneNumber", NULL };
109 static do_group_members();
110 static send_message();
111 static send_errors();
112 static do_noemailorfax();
113 static do_ambiguous();
118 static unbind_and_exit();
121 static has_attributes();
122 static char **get_attributes_mail_dn();
123 static char *canonical();
133 int numto, ngroups, numerr, nargs;
136 extern int optind, errno;
139 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
141 case 'f': /* who it's from & where errors should go */
142 mailfrom = strdup( optarg );
143 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
144 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
145 sendmailargs[j+1] = mailfrom;
151 case 'h': /* hostname */
152 host = strdup( optarg );
153 hostlen = strlen(host);
156 /* mailer-daemon address - who we should */
157 case 'm': /* say errors come from */
158 errorsfrom = strdup( optarg );
162 syslog( LOG_ALERT, "unknown option" );
167 if ( (myname = strrchr( argv[0], '/' )) == NULL )
168 myname = strdup( argv[0] );
170 myname = strdup( myname + 1 );
171 if (!strcmp(myname, "mail500")) {
173 } else if (!strcmp(myname, "fax500")) {
176 /* I give up, I must be mail500 */
181 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
183 openlog( myname, OPENLOG_OPTIONS );
186 if ( mailfrom == NULL ) {
187 syslog( LOG_ALERT, "required argument -f not present" );
190 if ( errorsfrom == NULL ) {
191 syslog( LOG_ALERT, "required argument -m not present" );
194 if ( host == NULL ) {
195 syslog( LOG_ALERT, "required argument -h not present" );
199 if ( connect_to_x500() != 0 )
208 strcpy( buf, argv[0] );
209 for ( i = 1; i < argc; i++ ) {
211 strcat( buf, argv[i] );
214 syslog( LOG_ALERT, "args: (%s)", buf );
218 add_to( &tolist, &numto, sendmailargs );
220 ngroups = numerr = 0;
223 for ( i = optind; i < argc; i++ ) {
227 for ( j = 0; argv[i][j] != '\0'; j++ ) {
228 if ( argv[i][j] == '.' || argv[i][j] == '_' )
233 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
236 if ( strcmp( s, ERRORS ) == 0 ) {
239 } else if ( strcmp( s, REQUEST ) == 0 ) {
240 type = GROUP_REQUEST;
242 } else if ( strcmp( s, MEMBERS ) == 0 ) {
243 type = GROUP_MEMBERS;
248 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
249 &errlist, &numerr, type );
253 * If we have both errors and successful deliveries to make or if
254 * if there are any groups to deliver to, we basically need to read
255 * the message twice. So, we have to put it in a tmp file.
258 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
263 if ( (fp = tmpfile()) == NULL ) {
264 syslog( LOG_ALERT, "could not open tmp file" );
265 unbind_and_exit( EX_TEMPFAIL );
268 /* copy the message to a temp file */
269 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
270 if ( fputs( buf, fp ) == EOF ) {
271 syslog( LOG_ALERT, "error writing tmpfile" );
272 unbind_and_exit( EX_TEMPFAIL );
276 if ( dup2( fileno( fp ), 0 ) == -1 ) {
277 syslog( LOG_ALERT, "could not dup2 tmpfile" );
278 unbind_and_exit( EX_TEMPFAIL );
284 /* deal with errors */
286 (void) rewind( stdin );
287 send_errors( errlist, numerr );
290 (void) ldap_unbind( ld );
292 /* send to groups with errorsTo */
294 (void) rewind( stdin );
295 send_group( togroups, ngroups );
298 /* send to expanded aliases and groups w/o errorsTo */
299 if ( numto > nargs ) {
300 (void) rewind( stdin );
301 send_message( tolist );
309 int sizelimit = FAX_MAXAMBIGUOUS;
310 int deref = LDAP_DEREF_ALWAYS;
312 if ( (ld = ldap_open( NULL, 0 )) == NULL ) {
313 syslog( LOG_ALERT, "ldap_open failed" );
317 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
318 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
320 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
321 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
330 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
341 LDAPMessage *e, *res;
342 struct timeval timeout;
349 * Look up the name in X.500, add the appropriate addresses found
350 * to the to list, or to the err list in case of error. Groups are
351 * handled by the do_group routine, individuals are handled here.
352 * When looking up name, we follow the bases hierarchy, looking
353 * in base[0] first, then base[1], etc. For each base, there is
354 * a set of search filters to try, in order. If something goes
355 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
356 * If the b_rdnpref flag is set, then we give preference to entries
357 * that matched name because it's their rdn, otherwise not.
360 timeout.tv_sec = FAX_TIMEOUT;
362 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
363 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
364 char *format, *p, *argv[3];
367 for ( argc = 0; argc < 3; argc++ ) {
371 format = strdup( base[b].b_filter[f] );
372 for ( argc = 0, p = format; *p; p++ ) {
375 case 's': /* %s is the name */
379 case 'h': /* %h is the host */
386 "unknown format %c", *p );
394 /* three names ought to do... */
395 sprintf( filter, format, argv[0], argv[1], argv[2] );
399 rc = ldap_search_st( ld, base[b].b_dn,
400 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
403 /* some other trouble - try again later */
404 if ( rc != LDAP_SUCCESS &&
405 rc != LDAP_SIZELIMIT_EXCEEDED ) {
406 syslog( LOG_ALERT, "return %d from X.500", rc );
407 unbind_and_exit( EX_TEMPFAIL );
410 if ( (match = ldap_count_entries( ld, res )) != 0 )
420 /* trouble - try again later */
422 syslog( LOG_ALERT, "error parsing result from X.500" );
423 unbind_and_exit( EX_TEMPFAIL );
426 /* no matches - bounce with user unknown */
428 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
432 /* more than one match - bounce with ambiguous user? */
434 LDAPMessage *next, *tmpres = NULL;
438 /* not giving rdn preference - bounce with ambiguous user */
439 if ( base[b].b_rdnpref == 0 ) {
440 add_error( err, nerr, E_AMBIGUOUS, name, res );
445 * giving rdn preference - see if any entries were matched
446 * because of their rdn. If so, collect them to deal with
447 * later (== 1 we deliver, > 1 we bounce).
450 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
451 next = ldap_next_entry( ld, e );
452 dn = ldap_get_dn( ld, e );
453 xdn = ldap_explode_dn( dn, 1 );
455 /* XXX bad, but how else can we do it? XXX */
456 if ( strcasecmp( xdn[0], name ) == 0 ) {
457 ldap_delete_result_entry( &res, e );
458 ldap_add_result_entry( &tmpres, e );
461 ldap_value_free( xdn );
465 /* nothing matched by rdn - go ahead and bounce */
466 if ( tmpres == NULL ) {
467 add_error( err, nerr, E_AMBIGUOUS, name, res );
470 /* more than one matched by rdn - bounce with rdn matches */
471 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
472 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
476 } else if ( match < 0 ) {
477 syslog( LOG_ALERT, "error parsing result from X.500" );
478 unbind_and_exit( EX_TEMPFAIL );
481 /* otherwise one matched by rdn - send to it */
487 * if we get this far, it means that we found a single match for
488 * name. for a user, we deliver to the mail attribute or bounce
489 * with address and phone if no mail attr. for a group, we
490 * deliver to all members or bounce to rfc822ErrorsTo if no members.
494 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
495 syslog( LOG_ALERT, "error parsing entry from X.500" );
496 unbind_and_exit( EX_TEMPFAIL );
499 dn = ldap_get_dn( ld, e );
501 if ( type == GROUP_ERRORS ) {
502 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
503 do_group_errors( e, dn, to, nto, err, nerr );
505 } else if ( type == GROUP_REQUEST ) {
506 /* sent to group-request - resend to [rfc822]RequestsTo attr */
507 do_group_request( e, dn, to, nto, err, nerr );
509 } else if ( type == GROUP_MEMBERS ) {
510 /* sent to group-members - expand */
511 do_group_members( e, dn, to, nto, togroups, ngroups, err,
514 } else if ( isgroup( e ) ) {
516 * sent to group - resend from [rfc822]ErrorsTo if it's there,
517 * otherwise, expand the group
520 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
526 * sent to user - mail attribute => add it to the to list,
531 if ( (mail = ldap_get_values( ld, e,
532 "facsimileTelephoneNumber" )) != NULL ) {
537 fdn = ldap_get_dn( ld, e );
538 ufn = ldap_explode_dn( fdn, 1 );
539 /* Convert spaces to underscores for rp */
540 for (i = 0; ufn[0][i] != '\0'; i++) {
541 if (ufn[0][i] == ' ') {
545 *mail = faxtotpc(*mail, ufn[0]);
547 add_to(to, nto, mail);
552 ldap_value_free( ufn );
553 ldap_value_free( mail );
556 add_error( err, nerr, E_NOFAXNUM,
561 if ( (mail = ldap_get_values( ld, e, "mail" ))
563 add_to( to, nto, mail );
565 ldap_value_free( mail );
568 add_error( err, nerr, E_NOEMAIL,
581 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
592 * If this group has an rfc822ErrorsTo attribute, we need to
593 * arrange for errors involving this group to go there, not
594 * to the sender. Since sendmail only has the concept of a
595 * single sender, we arrange for errors to go to groupname-errors,
596 * which we then handle specially when (if) it comes back to us
597 * by expanding to all the rfc822ErrorsTo addresses. If it has no
598 * rfc822ErrorsTo attribute, we call do_group_members() to expand
602 if ( group_loop( dn ) ) {
606 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
607 add_group( dn, togroups, ngroups );
612 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
619 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
631 char **mail, **member, **joinable;
633 LDAPMessage *ee, *res;
634 struct timeval timeout;
637 * if all has gone according to plan, we've already arranged for
638 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
639 * so all we have to do here is arrange to send to the
640 * rfc822Mailbox attribute, the member attribute, and anyone who
641 * has joined the group by setting memberOfGroup equal to the
645 /* add members in the group itself - mail attribute */
646 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
648 * These are only email addresses - impossible
649 * to have a fax number
653 /* XXX How do we want to inform sender that the */
654 /* group they sent to has email-only members? */
657 add_to( to, nto, mail );
661 ldap_value_free( mail );
664 /* add members in the group itself - member attribute */
665 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
666 for ( i = 0; member[i] != NULL; i++ ) {
667 add_member( dn, member[i], to, nto, togroups,
668 ngroups, err, nerr );
671 ldap_value_free( member );
674 /* add members who have joined by setting memberOfGroup */
675 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
676 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
677 ldap_value_free( joinable );
680 ldap_value_free( joinable );
682 sprintf( filter, "(memberOfGroup=%s)", dn );
684 timeout.tv_sec = FAX_TIMEOUT;
687 /* for each subtree to look in... */
689 int sizelimit = FAX_MAXMEMBERS;
690 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
692 for ( i = 0; base[i].b_dn != NULL; i++ ) {
693 /* find entries that have joined this group... */
694 rc = ldap_search_st( ld, base[i].b_dn,
695 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
698 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
699 rc == LDAP_TIMELIMIT_EXCEEDED ) {
701 "group search limit exceeded %d", rc );
702 unbind_and_exit( EX_TEMPFAIL );
705 if ( rc != LDAP_SUCCESS ) {
706 syslog( LOG_ALERT, "group search bad return %d",
708 unbind_and_exit( EX_TEMPFAIL );
711 /* for each entry that has joined... */
712 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
713 ee = ldap_next_entry( ld, ee ) ) {
714 if ( isgroup( ee ) ) {
715 ndn = ldap_get_dn( ld, ee );
717 if ( do_group( e, ndn, to, nto,
718 togroups, ngroups, err, nerr )
721 "group loop (%s) (%s)",
730 /* add them to the to list */
733 if ( (mail = ldap_get_values( ld, ee,
734 "facsimileTelephoneNumber" ))
740 fdn = ldap_get_dn( ld, ee );
741 ufn = ldap_explode_dn( fdn, 1 );
746 for (i = 0; ufn[0][i] != '\0';
748 if (ufn[0][i] == ' ') {
752 *mail = faxtotpc(*mail, ufn[0]);
753 add_to(to, nto, mail);
755 ldap_value_free( mail );
758 ndn = ldap_get_dn( ld, ee );
760 add_error( err, nerr,
761 E_JOINMEMBERNOFAXNUM, ndn,
768 if ( (mail = ldap_get_values( ld, ee,
769 "mail" )) != NULL ) {
770 add_to( to, nto, mail );
772 ldap_value_free( mail );
774 /* else generate a bounce */
776 ndn = ldap_get_dn( ld, ee );
778 add_error( err, nerr,
779 E_JOINMEMBERNOEMAIL, ndn,
791 int sizelimit = FAX_MAXAMBIGUOUS;
792 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
799 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr )
812 LDAPMessage *res, *e;
813 struct timeval timeout;
815 timeout.tv_sec = FAX_TIMEOUT;
817 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
818 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
819 if ( rc == LDAP_NO_SUCH_OBJECT ) {
820 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
824 syslog( LOG_ALERT, "member search bad return %d", rc );
826 unbind_and_exit( EX_TEMPFAIL );
830 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
831 syslog( LOG_ALERT, "member search error parsing entry" );
833 unbind_and_exit( EX_TEMPFAIL );
835 ndn = ldap_get_dn( ld, e );
837 /* allow groups within groups */
838 if ( isgroup( e ) ) {
839 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
841 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
851 if ( (mail = ldap_get_values( ld, e,
852 "facsimileTelephoneNumber" )) != NULL ) {
856 fdn = ldap_get_dn( ld, e );
857 ufn = ldap_explode_dn( fdn, 1 );
858 /* Convert spaces to underscores for rp */
859 for (i = 0; ufn[0][i] != '\0'; i++) {
860 if (ufn[0][i] == ' ') {
864 *mail = faxtotpc(*mail, ufn[0]);
865 add_to(to, nto, mail);
867 ldap_value_free( mail );
870 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
874 /* send to the member's mail attribute */
875 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
876 add_to( to, nto, mail );
878 ldap_value_free( mail );
880 /* else generate a bounce */
882 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
890 do_group_request( e, dn, to, nto, err, nerr )
900 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
901 "requestsTo" )) != NULL ) {
902 add_to( to, nto, requeststo );
904 ldap_value_free( requeststo );
906 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
912 do_group_errors( e, dn, to, nto, err, nerr )
922 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
923 "errorsTo" )) != NULL ) {
924 add_to( to, nto, errorsto );
926 ldap_value_free( errorsto );
928 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
940 WAITSTATUSTYPE status;
945 if ( pid = fork() ) {
947 waitpid( pid, (int *) NULL, 0 );
949 wait4( pid, &status, WAIT_FLAGS, 0 );
953 /* to includes sendmailargs */
954 execv( FAX_SENDMAIL, to );
956 syslog( LOG_ALERT, "execv failed" );
963 send_group( group, ngroup )
972 WAITSTATUSTYPE status;
975 for ( i = 0; i < ngroup; i++ ) {
976 (void) rewind( stdin );
978 iargv[0] = FAX_SENDMAIL;
980 iargv[2] = group[i].g_errorsto;
981 iargv[3] = "-oMrX.500";
988 add_to( &argv, &argc, iargv );
989 add_to( &argv, &argc, group[i].g_members );
993 if ( pid = fork() ) {
995 waitpid( pid, (int *) NULL, 0 );
997 wait4( pid, &status, WAIT_FLAGS, 0 );
1001 execv( FAX_SENDMAIL, argv );
1003 syslog( LOG_ALERT, "execv failed" );
1005 exit( EX_TEMPFAIL );
1013 send_errors( err, nerr )
1021 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1023 if ( (fp = popen( buf, "w" )) == NULL ) {
1024 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1028 fprintf( fp, "To: %s\n", mailfrom );
1029 fprintf( fp, "From: %s\n", errorsfrom );
1030 fprintf( fp, "Subject: undeliverable mail\n" );
1031 fprintf( fp, "\n" );
1032 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1033 for ( i = 0; i < nerr; i++ ) {
1034 namelen = strlen( err[i].e_addr );
1035 fprintf( fp, "\n" );
1037 switch ( err[i].e_code ) {
1039 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1043 fprintf( fp, "%s: Group member does not exist\n",
1048 fprintf( fp, "%s: Group exists but has no request address\n",
1053 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1058 do_ambiguous( fp, &err[i], namelen );
1062 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1065 case E_MEMBERNOEMAIL:
1066 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1070 case E_JOINMEMBERNOEMAIL:
1071 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1076 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1079 case E_MEMBERNOFAXNUM:
1080 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1084 case E_JOINMEMBERNOFAXNUM:
1085 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1090 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1091 unbind_and_exit( EX_TEMPFAIL );
1096 fprintf( fp, "\n------- The original message sent:\n\n" );
1098 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1102 if ( pclose( fp ) == -1 ) {
1103 syslog( LOG_ALERT, "pclose failed" );
1104 unbind_and_exit( EX_TEMPFAIL );
1112 do_noemailorfax( fp, err, namelen, errtype )
1122 if (errtype == E_NOFAXNUM) {
1123 fprintf(fp, "%s: User has no facsimile number registered.\n",
1125 } else if (errtype == E_NOEMAIL) {
1126 fprintf(fp, "%s: User has no email address registered.\n",
1129 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1130 namelen, " ", err->e_addr );
1133 dn = ldap_get_dn( ld, err->e_msg );
1134 ufn = ldap_explode_dn( dn, 1 );
1135 rdn = strdup( ufn[0] );
1136 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1137 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1139 for ( i = 0; vals[i]; i++ ) {
1140 last = strlen( vals[i] ) - 1;
1141 if ( isdigit( vals[i][last] ) ) {
1142 rdn = strdup( vals[i] );
1147 ldap_value_free( vals );
1150 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1153 ldap_value_free( ufn );
1155 /* titles or descriptions */
1156 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1157 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1159 fprintf( fp, "%*s No title or description registered\n",
1162 for ( i = 0; vals[i] != NULL; i++ ) {
1163 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1166 ldap_value_free( vals );
1169 /* postal address */
1170 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1172 fprintf( fp, "%*s No postal address registered\n", namelen,
1175 fprintf( fp, "%*s ", namelen, " " );
1176 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1177 if ( vals[0][i] == '$' ) {
1178 fprintf( fp, "\n%*s ", namelen, " " );
1179 while ( isspace( vals[0][i+1] ) )
1182 fprintf( fp, "%c", vals[0][i] );
1185 fprintf( fp, "\n" );
1187 ldap_value_free( vals );
1190 /* telephone number */
1191 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1193 fprintf( fp, "%*s No phone number registered\n", namelen,
1196 for ( i = 0; vals[i] != NULL; i++ ) {
1197 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1200 ldap_value_free( vals );
1206 do_ambiguous( fp, err, namelen )
1216 i = ldap_result2error( ld, err->e_msg, 0 );
1218 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1219 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1220 ldap_count_entries( ld, err->e_msg ) );
1222 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1223 e = ldap_next_entry( ld, e ) ) {
1224 dn = ldap_get_dn( ld, e );
1225 ufn = ldap_explode_dn( dn, 1 );
1226 rdn = strdup( ufn[0] );
1227 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1228 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1229 for ( i = 0; vals[i]; i++ ) {
1230 last = strlen( vals[i] ) - 1;
1231 if ( isdigit( vals[i][last] ) ) {
1232 rdn = strdup( vals[i] );
1237 ldap_value_free( vals );
1241 if ( isgroup( e ) ) {
1242 vals = ldap_get_values( ld, e, "description" );
1244 vals = ldap_get_values( ld, e, "title" );
1247 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1248 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1249 fprintf( fp, " %s\n", vals[i] );
1254 ldap_value_free( ufn );
1256 ldap_value_free( vals );
1261 count_values( list )
1266 for ( i = 0; list && list[i] != NULL; i++ )
1273 add_to( list, nlist, new )
1278 int i, nnew, oldnlist;
1280 nnew = count_values( new );
1283 if ( *list == NULL || *nlist == 0 ) {
1284 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1287 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1288 nnew * sizeof(char *) + sizeof(char *) );
1292 for ( i = 0; i < nnew; i++ ) {
1293 (*list)[i + oldnlist] = strdup( new[i] );
1295 (*list)[*nlist] = NULL;
1307 oclist = ldap_get_values( ld, e, "objectClass" );
1309 for ( i = 0; oclist[i] != NULL; i++ ) {
1310 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1311 ldap_value_free( oclist );
1315 ldap_value_free( oclist );
1321 add_error( err, nerr, code, addr, msg )
1329 *err = (Error *) malloc( sizeof(Error) );
1331 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1334 (*err)[*nerr].e_code = code;
1335 (*err)[*nerr].e_addr = strdup( addr );
1336 (*err)[*nerr].e_msg = msg;
1343 add_group( dn, list, nlist )
1351 for ( i = 0; i < *nlist; i++ ) {
1352 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1353 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1358 ufn = ldap_explode_dn( dn, 1 );
1359 namelen = strlen( ufn[0] );
1361 if ( *nlist == 0 ) {
1362 *list = (Group *) malloc( sizeof(Group) );
1364 *list = (Group *) realloc( *list, (*nlist + 1) *
1368 /* send errors to groupname-errors@host */
1369 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1371 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1372 (void) canonical( (*list)[*nlist].g_errorsto );
1374 /* send to groupname-members@host - make it a list for send_group */
1375 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1376 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1377 sizeof(MEMBERS) + hostlen + 2 );
1378 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1380 (void) canonical( (*list)[*nlist].g_members[0] );
1381 (*list)[*nlist].g_members[1] = NULL;
1383 /* save the group's dn so we can check for loops above */
1384 (*list)[*nlist].g_dn = strdup( dn );
1388 ldap_value_free( ufn );
1394 unbind_and_exit( rc )
1399 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1400 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1411 for ( ; *s != '\0'; s++ ) {
1424 static char **groups;
1427 for ( i = 0; i < ngroups; i++ ) {
1428 if ( strcmp( dn, groups[i] ) == 0 )
1433 groups = (char **) malloc( sizeof(char *) );
1435 groups = (char **) realloc( groups,
1436 (ngroups + 1) * sizeof(char *) );
1438 groups[ngroups++] = strdup( dn );
1444 has_attributes( e, attr1, attr2 )
1451 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1452 ldap_value_free( attr );
1456 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1457 ldap_value_free( attr );
1465 get_attributes_mail_dn( e, attr1, attr2 )
1468 char *attr2; /* this one is dn-valued */
1470 LDAPMessage *ee, *res;
1471 char **vals, **dnlist, **mail;
1473 struct timeval timeout;
1475 vals = ldap_get_values( ld, e, attr1 );
1476 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1479 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1480 timeout.tv_sec = FAX_TIMEOUT;
1481 timeout.tv_usec = 0;
1483 for ( i = 0; dnlist[i] != NULL; i++ ) {
1484 if ( (rc = ldap_search_st( ld, dnlist[i],
1485 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1486 &timeout, &res )) != LDAP_SUCCESS ) {
1487 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1488 unbind_and_exit( EX_TEMPFAIL );
1491 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1497 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1498 syslog( LOG_ALERT, "error parsing x500 entry" );
1502 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1504 add_to( &vals, &nto, mail );
1506 ldap_value_free( mail );
1509 ldap_msgfree( res );