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.
18 #include <ac/string.h>
19 #include <ac/syslog.h>
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
27 #include <sys/resource.h>
34 #include "ldapconfig.h"
37 #define GROUP_ERRORS 0x02
38 #define GROUP_REQUEST 0x04
39 #define GROUP_MEMBERS 0x08
40 #define GROUP_OWNER 0x10
43 #define ERRORS "errors"
44 #define REQUEST "request"
45 #define REQUESTS "requests"
46 #define MEMBERS "members"
48 #define OWNERS "owners"
51 char *vacationhost = NULL;
52 char *errorsfrom = NULL;
53 char *mailfrom = NULL;
55 char *ldaphost = LDAPHOST;
61 #define E_USERUNKNOWN 1
67 #define E_JOINMEMBERNOEMAIL 7
68 #define E_MEMBERNOEMAIL 8
70 #define E_NOMEMBERS 10
72 #define E_GROUPUNKNOWN 12
78 #define e_msg e_union.e_u_msg
79 #define e_loop e_union.e_u_loop
82 typedef struct groupto {
88 typedef struct baseinfo {
89 char *b_dn; /* dn to start searching at */
90 char b_rdnpref; /* give rdn's preference when searching? */
91 int b_search; /* ORed with the type of thing the address */
92 /* looks like (USER, GROUP_ERRORS, etc.) */
93 /* to see if this should be searched */
94 char *b_filter[3]; /* filter to apply - name substituted for %s */
95 /* (up to three of them) */
99 { "ou=People, o=University of Michigan, c=US",
101 "uid=%s", "cn=%s", NULL,
102 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
104 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
105 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
107 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
111 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
113 static char *attrs[] = { "objectClass", "title", "postaladdress",
114 "telephoneNumber", "mail", "description", "owner",
115 "errorsTo", "rfc822ErrorsTo", "requestsTo",
116 "rfc822RequestsTo", "joinable", "cn", "member",
117 "moderator", "onVacation", "uid",
118 "suppressNoEmailError", NULL };
122 static do_group_members();
123 static send_message();
124 static send_errors();
126 static do_ambiguous();
131 static unbind_and_exit();
134 static has_attributes();
135 static char **get_attributes_mail_dn();
136 static char *canonical();
146 int numto, ngroups, numerr, nargs;
149 extern int optind, errno;
152 if ( (myname = strrchr( argv[0], '/' )) == NULL )
153 myname = strdup( argv[0] );
155 myname = strdup( myname + 1 );
158 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
160 openlog( myname, OPENLOG_OPTIONS );
163 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
165 case 'd': /* turn on debugging */
166 debug = atoi( optarg );
169 case 'f': /* who it's from & where errors should go */
170 mailfrom = strdup( optarg );
171 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
172 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
173 sendmailargs[j+1] = mailfrom;
179 case 'h': /* hostname */
180 host = strdup( optarg );
181 hostlen = strlen(host);
184 case 'l': /* ldap host */
185 ldaphost = strdup( optarg );
188 /* mailer-daemon address - who we should */
189 case 'm': /* say errors come from */
190 errorsfrom = strdup( optarg );
193 case 'v': /* vacation host */
194 vacationhost = strdup( optarg );
198 syslog( LOG_ALERT, "unknown option" );
203 if ( mailfrom == NULL ) {
204 syslog( LOG_ALERT, "required argument -f not present" );
207 if ( errorsfrom == NULL ) {
208 syslog( LOG_ALERT, "required argument -m not present" );
211 if ( host == NULL ) {
212 syslog( LOG_ALERT, "required argument -h not present" );
216 if ( connect_to_x500() != 0 )
225 syslog( LOG_ALERT, "running as %d", geteuid() );
226 strcpy( buf, argv[0] );
227 for ( i = 1; i < argc; i++ ) {
229 strcat( buf, argv[i] );
232 syslog( LOG_ALERT, "args: (%s)", buf );
237 add_to( &tolist, &numto, sendmailargs );
239 ngroups = numerr = 0;
242 for ( i = optind; i < argc; i++ ) {
246 for ( j = 0; argv[i][j] != '\0'; j++ ) {
247 if ( argv[i][j] == '.' || argv[i][j] == '_' )
252 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
255 if ((strcasecmp(s, ERROR) == 0) ||
256 (strcasecmp(s, ERRORS) == 0)) {
259 } else if ((strcasecmp(s, REQUEST) == 0) ||
260 (strcasecmp(s, REQUESTS) == 0)) {
261 type = GROUP_REQUEST;
263 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
264 type = GROUP_MEMBERS;
266 } else if ((strcasecmp(s, OWNER) == 0) ||
267 (strcasecmp(s, OWNERS) == 0)) {
273 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
274 &errlist, &numerr, type );
278 * If we have both errors and successful deliveries to make or if
279 * if there are any groups to deliver to, we basically need to read
280 * the message twice. So, we have to put it in a tmp file.
283 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
288 if ( (fp = tmpfile()) == NULL ) {
289 syslog( LOG_ALERT, "could not open tmp file" );
290 unbind_and_exit( EX_TEMPFAIL );
293 /* copy the message to a temp file */
294 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
295 if ( fputs( buf, fp ) == EOF ) {
296 syslog( LOG_ALERT, "error writing tmpfile" );
297 unbind_and_exit( EX_TEMPFAIL );
301 if ( dup2( fileno( fp ), 0 ) == -1 ) {
302 syslog( LOG_ALERT, "could not dup2 tmpfile" );
303 unbind_and_exit( EX_TEMPFAIL );
309 /* deal with errors */
312 syslog( LOG_ALERT, "sending errors" );
314 (void) rewind( stdin );
315 send_errors( errlist, numerr );
318 (void) ldap_unbind( ld );
320 /* send to groups with errorsTo */
323 syslog( LOG_ALERT, "sending to groups with errorsto" );
325 (void) rewind( stdin );
326 send_group( togroups, ngroups );
329 /* send to expanded aliases and groups w/o errorsTo */
330 if ( numto > nargs ) {
332 syslog( LOG_ALERT, "sending to aliases and groups" );
334 (void) rewind( stdin );
335 send_message( tolist );
345 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
346 syslog( LOG_ALERT, "ldap_open failed" );
350 opt = MAIL500_MAXAMBIGUOUS;
351 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
352 opt = LDAP_DEREF_ALWAYS;
353 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
355 if ( ldap_simple_bind_s( ld, MAIL500_BINDDN, NULL ) != LDAP_SUCCESS ) {
356 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
370 for ( i = 0; a[i] != '\0'; i++ ) {
371 if ( a[i] != b[i] ) {
376 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
390 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
401 LDAPMessage *e, *res;
402 struct timeval timeout;
405 char realfilter[1024];
406 char **mail, **onvacation = NULL, **uid = NULL;
409 * Look up the name in X.500, add the appropriate addresses found
410 * to the to list, or to the err list in case of error. Groups are
411 * handled by the do_group routine, individuals are handled here.
412 * When looking up name, we follow the bases hierarchy, looking
413 * in base[0] first, then base[1], etc. For each base, there is
414 * a set of search filters to try, in order. If something goes
415 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
416 * If the b_rdnpref flag is set, then we give preference to entries
417 * that matched name because it's their rdn, otherwise not.
420 timeout.tv_sec = MAIL500_TIMEOUT;
422 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
423 if ( ! (base[b].b_search & type) ) {
426 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
427 char *format, *p, *s, *d;
431 for ( argc = 0; argc < 3; argc++ ) {
435 format = strdup( base[b].b_filter[f] );
436 for ( argc = 0, p = format; *p; p++ ) {
439 case 's': /* %s is the name */
443 case 'h': /* %h is the host */
450 "unknown format %c", *p );
458 /* three names ought to do... */
459 sprintf( filter, format, argv[0], argv[1], argv[2] );
461 for ( s = filter, d = realfilter; *s; s++, d++ ) {
470 rc = ldap_search_st( ld, base[b].b_dn,
471 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
474 /* some other trouble - try again later */
475 if ( rc != LDAP_SUCCESS &&
476 rc != LDAP_SIZELIMIT_EXCEEDED ) {
477 syslog( LOG_ALERT, "return 0x%x from X.500",
479 unbind_and_exit( EX_TEMPFAIL );
482 if ( (match = ldap_count_entries( ld, res )) != 0 )
492 /* trouble - try again later */
494 syslog( LOG_ALERT, "error parsing result from X.500" );
495 unbind_and_exit( EX_TEMPFAIL );
498 /* no matches - bounce with user unknown */
500 if ( type == USER ) {
501 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
503 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
508 /* more than one match - bounce with ambiguous user? */
510 LDAPMessage *next, *tmpres = NULL;
514 /* not giving rdn preference - bounce with ambiguous user */
515 if ( base[b].b_rdnpref == 0 ) {
516 add_error( err, nerr, E_AMBIGUOUS, name, res );
521 * giving rdn preference - see if any entries were matched
522 * because of their rdn. If so, collect them to deal with
523 * later (== 1 we deliver, > 1 we bounce).
526 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
527 next = ldap_next_entry( ld, e );
528 dn = ldap_get_dn( ld, e );
529 xdn = ldap_explode_dn( dn, 1 );
531 /* XXX bad, but how else can we do it? XXX */
532 if ( strcasecmp( xdn[0], name ) == 0 ) {
533 ldap_delete_result_entry( &res, e );
534 ldap_add_result_entry( &tmpres, e );
537 ldap_value_free( xdn );
541 /* nothing matched by rdn - go ahead and bounce */
542 if ( tmpres == NULL ) {
543 add_error( err, nerr, E_AMBIGUOUS, name, res );
546 /* more than one matched by rdn - bounce with rdn matches */
547 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
548 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
552 } else if ( match < 0 ) {
553 syslog( LOG_ALERT, "error parsing result from X.500" );
554 unbind_and_exit( EX_TEMPFAIL );
557 /* otherwise one matched by rdn - send to it */
563 * if we get this far, it means that we found a single match for
564 * name. for a user, we deliver to the mail attribute or bounce
565 * with address and phone if no mail attr. for a group, we
566 * deliver to all members or bounce to rfc822ErrorsTo if no members.
570 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
571 syslog( LOG_ALERT, "error parsing entry from X.500" );
572 unbind_and_exit( EX_TEMPFAIL );
575 dn = ldap_get_dn( ld, e );
577 if ( type == GROUP_ERRORS ) {
578 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
579 do_group_errors( e, dn, to, nto, err, nerr );
581 } else if ( type == GROUP_REQUEST ) {
582 /* sent to group-request - resend to [rfc822]RequestsTo attr */
583 do_group_request( e, dn, to, nto, err, nerr );
585 } else if ( type == GROUP_MEMBERS ) {
586 /* sent to group-members - expand */
587 do_group_members( e, dn, to, nto, togroups, ngroups, err,
590 } else if ( type == GROUP_OWNER ) {
591 /* sent to group-owner - resend to owner attr */
592 do_group_owner( e, dn, to, nto, err, nerr );
594 } else if ( isgroup( e ) ) {
596 * sent to group - resend from [rfc822]ErrorsTo if it's there,
597 * otherwise, expand the group
600 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
606 * sent to user - mail attribute => add it to the to list,
609 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
614 /* try to detect simple mail loops */
615 sprintf( buf, "%s@%s", name, host );
616 for ( i = 0; mail[i] != NULL; i++ ) {
618 * address is the same as the one we're
619 * sending to - mail loop. syslog the
620 * problem, bounce a message back to the
621 * sender (who else?), and delete the bogus
622 * addr from the list.
625 if ( (h = strchr( mail[i], '@' )) != NULL ) {
627 if ( strcasecmp( h, host ) == 0 ) {
629 "potential loop detected (%s)",
634 if ( mailcmp( buf, mail[i] ) == 0 ) {
636 "loop detected (%s)", mail[i] );
638 /* remove the bogus address */
639 for ( j = i; mail[j] != NULL; j++ ) {
644 if ( mail[0] != NULL ) {
645 add_to( to, nto, mail );
647 add_error( err, nerr, E_NOEMAIL, name, res );
650 ldap_value_free( mail );
652 add_error( err, nerr, E_NOEMAIL, name, res );
656 * If the user is on vacation, send a copy of the mail to
657 * the vacation server. The address is constructed from
658 * the vacationhost (set in a command line argument) and
659 * the uid (XXX this should be more general XXX).
662 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
663 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
668 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
669 sprintf( buf, "%s@%s", uid[0], vacationhost );
674 add_to( to, nto, vaddr );
677 "user without a uid on vacation (%s)",
683 if ( onvacation != NULL ) {
684 ldap_value_free( onvacation );
687 ldap_value_free( uid );
695 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
709 * If this group has an rfc822ErrorsTo attribute, we need to
710 * arrange for errors involving this group to go there, not
711 * to the sender. Since sendmail only has the concept of a
712 * single sender, we arrange for errors to go to groupname-errors,
713 * which we then handle specially when (if) it comes back to us
714 * by expanding to all the rfc822ErrorsTo addresses. If it has no
715 * rfc822ErrorsTo attribute, we call do_group_members() to expand
719 if ( group_loop( dn ) ) {
724 * check for moderated groups - if the group has a moderator
725 * attribute, we check to see if the from address is one of
726 * the moderator values. if so, continue on. if not, arrange
727 * to send the mail to the moderator(s). need to do this before
728 * we change the from below.
731 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
732 /* check if it came from any of the group's moderators */
733 for ( i = 0; moderator[i] != NULL; i++ ) {
734 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
738 /* not from the moderator? */
739 if ( moderator[i] == NULL ) {
740 add_to( to, nto, moderator );
741 ldap_value_free( moderator );
745 /* else from the moderator - fall through and deliver it */
748 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
749 add_group( dn, togroups, ngroups );
754 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
761 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
771 int i, rc, anymembers;
773 char **mail, **member, **joinable, **suppress;
775 LDAPMessage *ee, *res;
776 struct timeval timeout;
780 * if all has gone according to plan, we've already arranged for
781 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
782 * so all we have to do here is arrange to send to the
783 * rfc822Mailbox attribute, the member attribute, and anyone who
784 * has joined the group by setting memberOfGroup equal to the
788 /* add members in the group itself - mail attribute */
790 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
792 add_to( to, nto, mail );
794 ldap_value_free( mail );
797 /* add members in the group itself - member attribute */
798 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
799 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
801 for ( i = 0; member[i] != NULL; i++ ) {
802 if ( strcasecmp( dn, member[i] ) == 0 ) {
803 syslog( LOG_ALERT, "group (%s) contains itself",
807 add_member( dn, member[i], to, nto, togroups,
808 ngroups, err, nerr, suppress );
812 ldap_value_free( suppress );
814 ldap_value_free( member );
817 /* add members who have joined by setting memberOfGroup */
818 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
819 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
820 if ( ! anymembers ) {
821 add_error( err, nerr, E_NOMEMBERS, dn,
825 ldap_value_free( joinable );
828 ldap_value_free( joinable );
830 sprintf( filter, "(memberOfGroup=%s)", dn );
832 timeout.tv_sec = MAIL500_TIMEOUT;
835 /* for each subtree to look in... */
836 opt = MAIL500_MAXAMBIGUOUS;
837 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
838 for ( i = 0; base[i].b_dn != NULL; i++ ) {
839 /* find entries that have joined this group... */
840 rc = ldap_search_st( ld, base[i].b_dn,
841 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
844 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
845 rc == LDAP_TIMELIMIT_EXCEEDED ) {
847 "group search limit exceeded %d", rc );
848 unbind_and_exit( EX_TEMPFAIL );
851 if ( rc != LDAP_SUCCESS ) {
852 syslog( LOG_ALERT, "group search return 0x%x",
854 unbind_and_exit( EX_TEMPFAIL );
857 /* for each entry that has joined... */
858 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
859 ee = ldap_next_entry( ld, ee ) ) {
861 if ( isgroup( ee ) ) {
862 ndn = ldap_get_dn( ld, ee );
864 if ( do_group( e, ndn, to, nto,
865 togroups, ngroups, err, nerr )
868 "group loop (%s) (%s)",
877 /* add them to the to list */
878 if ( (mail = ldap_get_values( ld, ee, "mail" ))
880 add_to( to, nto, mail );
882 ldap_value_free( mail );
884 /* else generate a bounce */
886 ndn = ldap_get_dn( ld, ee );
888 add_error( err, nerr,
889 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
897 opt = MAIL500_MAXAMBIGUOUS;
898 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
901 if ( ! anymembers ) {
902 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
908 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr, suppress )
922 LDAPMessage *res, *e;
923 struct timeval timeout;
925 timeout.tv_sec = MAIL500_TIMEOUT;
927 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
928 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
929 if ( rc == LDAP_NO_SUCH_OBJECT ) {
930 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
934 syslog( LOG_ALERT, "member search return 0x%x", rc );
936 unbind_and_exit( EX_TEMPFAIL );
940 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
941 syslog( LOG_ALERT, "member search error parsing entry" );
943 unbind_and_exit( EX_TEMPFAIL );
945 ndn = ldap_get_dn( ld, e );
947 /* allow groups within groups */
948 if ( isgroup( e ) ) {
949 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
951 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
959 /* send to the member's mail attribute */
960 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
961 add_to( to, nto, mail );
963 ldap_value_free( mail );
965 /* else generate a bounce */
967 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
969 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
978 do_group_request( e, dn, to, nto, err, nerr )
988 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
989 "requestsTo" )) != NULL ) {
990 add_to( to, nto, requeststo );
992 ldap_value_free( requeststo );
994 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
1000 do_group_errors( e, dn, to, nto, err, nerr )
1010 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1011 "errorsTo" )) != NULL ) {
1012 add_to( to, nto, errorsto );
1014 ldap_value_free( errorsto );
1016 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1022 do_group_owner( e, dn, to, nto, err, nerr )
1032 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1033 add_to( to, nto, owner );
1034 ldap_value_free( owner );
1036 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1046 #ifndef HAVE_WAITPID
1047 WAITSTATUSTYPE status;
1054 strcpy( buf, to[0] );
1055 for ( i = 1; to[i] != NULL; i++ ) {
1057 strcat( buf, to[i] );
1060 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1064 if ( pid = fork() ) {
1066 waitpid( pid, (int *) NULL, 0 );
1068 wait4( pid, &status, WAIT_FLAGS, 0 );
1072 /* to includes sendmailargs */
1073 execv( MAIL500_SENDMAIL, to );
1075 syslog( LOG_ALERT, "execv failed" );
1077 exit( EX_TEMPFAIL );
1082 send_group( group, ngroup )
1090 #ifndef HAVE_WAITPID
1091 WAITSTATUSTYPE status;
1094 for ( i = 0; i < ngroup; i++ ) {
1095 (void) rewind( stdin );
1097 iargv[0] = MAIL500_SENDMAIL;
1099 iargv[2] = group[i].g_errorsto;
1100 iargv[3] = "-oMrX.500";
1107 add_to( &argv, &argc, iargv );
1108 add_to( &argv, &argc, group[i].g_members );
1114 strcpy( buf, argv[0] );
1115 for ( i = 1; i < argc; i++ ) {
1117 strcat( buf, argv[i] );
1120 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1124 if ( pid = fork() ) {
1126 waitpid( pid, (int *) NULL, 0 );
1128 wait4( pid, &status, WAIT_FLAGS, 0 );
1132 execv( MAIL500_SENDMAIL, argv );
1134 syslog( LOG_ALERT, "execv failed" );
1136 exit( EX_TEMPFAIL );
1144 send_errors( err, nerr )
1148 int pid, i, namelen;
1153 #ifndef HAVE_WAITPID
1154 WAITSTATUSTYPE status;
1157 argv[0] = MAIL500_SENDMAIL;
1158 argv[1] = "-oMrX.500";
1162 argv[5] = errorsfrom;
1169 strcpy( buf, argv[0] );
1170 for ( i = 1; argv[i] != NULL; i++ ) {
1172 strcat( buf, argv[i] );
1175 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1178 if ( pipe( fd ) == -1 ) {
1179 syslog( LOG_ALERT, "cannot create pipe" );
1180 exit( EX_TEMPFAIL );
1183 if ( pid = fork() ) {
1184 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1185 syslog( LOG_ALERT, "cannot fdopen pipe" );
1186 exit( EX_TEMPFAIL );
1189 fprintf( fp, "To: %s\n", mailfrom );
1190 fprintf( fp, "From: %s\n", errorsfrom );
1191 fprintf( fp, "Subject: undeliverable mail\n" );
1192 fprintf( fp, "\n" );
1193 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1194 for ( i = 0; i < nerr; i++ ) {
1195 namelen = strlen( err[i].e_addr );
1196 fprintf( fp, "\n" );
1198 switch ( err[i].e_code ) {
1200 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1203 case E_GROUPUNKNOWN:
1204 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1208 fprintf( fp, "%s: Group member does not exist\n",
1210 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1211 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1212 fprintf( fp, "then re-adding the person to the group.\n" );
1216 fprintf( fp, "%s: Group exists but has no request address\n",
1221 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1226 fprintf( fp, "%s: Group exists but has no owner\n",
1231 do_ambiguous( fp, &err[i], namelen );
1235 do_noemail( fp, &err[i], namelen );
1238 case E_MEMBERNOEMAIL:
1239 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1243 case E_JOINMEMBERNOEMAIL:
1244 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1249 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1250 err[i].e_addr, err[i].e_loop );
1254 fprintf( fp, "%s: Group has no members\n",
1259 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1260 unbind_and_exit( EX_TEMPFAIL );
1265 fprintf( fp, "\n------- The original message sent:\n\n" );
1267 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1273 waitpid( pid, (int *) NULL, 0 );
1275 wait4( pid, &status, WAIT_FLAGS, 0 );
1280 execv( MAIL500_SENDMAIL, argv );
1282 syslog( LOG_ALERT, "execv failed" );
1284 exit( EX_TEMPFAIL );
1291 do_noemail( fp, err, namelen )
1300 fprintf(fp, "%s: User has no email address registered.\n",
1302 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1303 namelen, " ", err->e_addr );
1306 dn = ldap_get_dn( ld, err->e_msg );
1307 ufn = ldap_explode_dn( dn, 1 );
1308 rdn = strdup( ufn[0] );
1309 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1310 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1312 for ( i = 0; vals[i]; i++ ) {
1313 last = strlen( vals[i] ) - 1;
1314 if ( isdigit( vals[i][last] ) ) {
1315 rdn = strdup( vals[i] );
1320 ldap_value_free( vals );
1323 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1326 ldap_value_free( ufn );
1328 /* titles or descriptions */
1329 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1330 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1332 fprintf( fp, "%*s No title or description registered\n",
1335 for ( i = 0; vals[i] != NULL; i++ ) {
1336 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1339 ldap_value_free( vals );
1342 /* postal address */
1343 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1345 fprintf( fp, "%*s No postal address registered\n", namelen,
1348 fprintf( fp, "%*s ", namelen, " " );
1349 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1350 if ( vals[0][i] == '$' ) {
1351 fprintf( fp, "\n%*s ", namelen, " " );
1352 while ( isspace( vals[0][i+1] ) )
1355 fprintf( fp, "%c", vals[0][i] );
1358 fprintf( fp, "\n" );
1360 ldap_value_free( vals );
1363 /* telephone number */
1364 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1366 fprintf( fp, "%*s No phone number registered\n", namelen,
1369 for ( i = 0; vals[i] != NULL; i++ ) {
1370 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1373 ldap_value_free( vals );
1379 do_ambiguous( fp, err, namelen )
1389 i = ldap_result2error( ld, err->e_msg, 0 );
1391 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1392 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1393 ldap_count_entries( ld, err->e_msg ) );
1395 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1396 e = ldap_next_entry( ld, e ) ) {
1397 dn = ldap_get_dn( ld, e );
1398 ufn = ldap_explode_dn( dn, 1 );
1399 rdn = strdup( ufn[0] );
1400 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1401 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1402 for ( i = 0; vals[i]; i++ ) {
1403 last = strlen( vals[i] ) - 1;
1404 if ( isdigit( vals[i][last] ) ) {
1405 rdn = strdup( vals[i] );
1410 ldap_value_free( vals );
1414 if ( isgroup( e ) ) {
1415 vals = ldap_get_values( ld, e, "description" );
1417 vals = ldap_get_values( ld, e, "title" );
1420 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1421 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1422 fprintf( fp, " %s\n", vals[i] );
1427 ldap_value_free( ufn );
1429 ldap_value_free( vals );
1434 count_values( list )
1439 for ( i = 0; list && list[i] != NULL; i++ )
1446 add_to( list, nlist, new )
1451 int i, nnew, oldnlist;
1453 nnew = count_values( new );
1456 if ( *list == NULL || *nlist == 0 ) {
1457 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1460 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1461 nnew * sizeof(char *) + sizeof(char *) );
1465 for ( i = 0; i < nnew; i++ )
1466 (*list)[i + oldnlist] = strdup( new[i] );
1467 (*list)[*nlist] = NULL;
1479 oclist = ldap_get_values( ld, e, "objectClass" );
1481 for ( i = 0; oclist[i] != NULL; i++ ) {
1482 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1483 ldap_value_free( oclist );
1487 ldap_value_free( oclist );
1493 add_error( err, nerr, code, addr, msg )
1501 *err = (Error *) malloc( sizeof(Error) );
1503 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1506 (*err)[*nerr].e_code = code;
1507 (*err)[*nerr].e_addr = strdup( addr );
1508 (*err)[*nerr].e_msg = msg;
1515 add_group( dn, list, nlist )
1523 for ( i = 0; i < *nlist; i++ ) {
1524 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1525 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1530 ufn = ldap_explode_dn( dn, 1 );
1531 namelen = strlen( ufn[0] );
1533 if ( *nlist == 0 ) {
1534 *list = (Group *) malloc( sizeof(Group) );
1536 *list = (Group *) realloc( *list, (*nlist + 1) *
1540 /* send errors to groupname-errors@host */
1541 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1543 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1544 (void) canonical( (*list)[*nlist].g_errorsto );
1546 /* send to groupname-members@host - make it a list for send_group */
1547 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1548 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1549 sizeof(MEMBERS) + hostlen + 2 );
1550 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1552 (void) canonical( (*list)[*nlist].g_members[0] );
1553 (*list)[*nlist].g_members[1] = NULL;
1555 /* save the group's dn so we can check for loops above */
1556 (*list)[*nlist].g_dn = strdup( dn );
1560 ldap_value_free( ufn );
1566 unbind_and_exit( rc )
1571 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1572 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1583 for ( ; *s != '\0'; s++ ) {
1596 static char **groups;
1599 for ( i = 0; i < ngroups; i++ ) {
1600 if ( strcmp( dn, groups[i] ) == 0 )
1605 groups = (char **) malloc( sizeof(char *) );
1607 groups = (char **) realloc( groups,
1608 (ngroups + 1) * sizeof(char *) );
1610 groups[ngroups++] = strdup( dn );
1616 has_attributes( e, attr1, attr2 )
1623 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1624 ldap_value_free( attr );
1628 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1629 ldap_value_free( attr );
1637 get_attributes_mail_dn( e, attr1, attr2 )
1640 char *attr2; /* this one is dn-valued */
1642 LDAPMessage *ee, *res;
1643 char **vals, **dnlist, **mail, **grname, **graddr;
1646 struct timeval timeout;
1648 dn = ldap_get_dn( ld, e );
1650 vals = ldap_get_values( ld, e, attr1 );
1651 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1654 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1655 timeout.tv_sec = MAIL500_TIMEOUT;
1656 timeout.tv_usec = 0;
1658 for ( i = 0; dnlist[i] != NULL; i++ ) {
1659 if ( (rc = ldap_search_st( ld, dnlist[i],
1660 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1661 &timeout, &res )) != LDAP_SUCCESS ) {
1662 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1663 unbind_and_exit( EX_TEMPFAIL );
1666 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1672 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1673 syslog( LOG_ALERT, "error parsing x500 entry" );
1677 if ( isgroup(ee) ) {
1680 grname = ldap_explode_dn( dnlist[i], 1 );
1682 /* groupname + host + @ + null */
1683 graddr[0] = (char *) malloc( strlen( grname[0] )
1684 + strlen( host ) + 2 );
1686 sprintf( graddr[0], "%s@%s", grname[0], host);
1687 (void) canonical( graddr[0] );
1689 add_to( &vals, &nto, graddr );
1692 ldap_value_free( grname );
1693 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1695 add_to( &vals, &nto, mail );
1697 ldap_value_free( mail );
1700 ldap_msgfree( res );