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"
36 #ifndef MAIL500_BOUNCEFROM
37 #define MAIL500_BOUNCEFROM "<>"
41 #define GROUP_ERRORS 0x02
42 #define GROUP_REQUEST 0x04
43 #define GROUP_MEMBERS 0x08
44 #define GROUP_OWNER 0x10
47 #define ERRORS "errors"
48 #define REQUEST "request"
49 #define REQUESTS "requests"
50 #define MEMBERS "members"
52 #define OWNERS "owners"
55 char *vacationhost = NULL;
56 char *errorsfrom = NULL;
57 char *mailfrom = NULL;
59 char *ldaphost = LDAPHOST;
65 #define E_USERUNKNOWN 1
71 #define E_JOINMEMBERNOEMAIL 7
72 #define E_MEMBERNOEMAIL 8
74 #define E_NOMEMBERS 10
76 #define E_GROUPUNKNOWN 12
82 #define e_msg e_union.e_u_msg
83 #define e_loop e_union.e_u_loop
86 typedef struct groupto {
92 typedef struct baseinfo {
93 char *b_dn; /* dn to start searching at */
94 char b_rdnpref; /* give rdn's preference when searching? */
95 int b_search; /* ORed with the type of thing the address */
96 /* looks like (USER, GROUP_ERRORS, etc.) */
97 /* to see if this should be searched */
98 char *b_filter[3]; /* filter to apply - name substituted for %s */
99 /* (up to three of them) */
103 { "ou=People, o=University of Michigan, c=US",
105 "uid=%s", "cn=%s", NULL,
106 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
108 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
109 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
111 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
115 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
117 static char *attrs[] = { "objectClass", "title", "postaladdress",
118 "telephoneNumber", "mail", "description", "owner",
119 "errorsTo", "rfc822ErrorsTo", "requestsTo",
120 "rfc822RequestsTo", "joinable", "cn", "member",
121 "moderator", "onVacation", "uid",
122 "suppressNoEmailError", NULL };
126 static do_group_members();
127 static send_message();
128 static send_errors();
130 static do_ambiguous();
135 static unbind_and_exit();
138 static has_attributes();
139 static char **get_attributes_mail_dn();
140 static char *canonical();
150 int numto, ngroups, numerr, nargs;
153 extern int optind, errno;
156 if ( (myname = strrchr( argv[0], '/' )) == NULL )
157 myname = strdup( argv[0] );
159 myname = strdup( myname + 1 );
162 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
164 openlog( myname, OPENLOG_OPTIONS );
167 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
169 case 'd': /* turn on debugging */
170 debug = atoi( optarg );
173 case 'f': /* who it's from & where errors should go */
174 mailfrom = strdup( optarg );
175 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
176 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
177 sendmailargs[j+1] = mailfrom;
183 case 'h': /* hostname */
184 host = strdup( optarg );
185 hostlen = strlen(host);
188 case 'l': /* ldap host */
189 ldaphost = strdup( optarg );
192 /* mailer-daemon address - who we should */
193 case 'm': /* say errors come from */
194 errorsfrom = strdup( optarg );
197 case 'v': /* vacation host */
198 vacationhost = strdup( optarg );
202 syslog( LOG_ALERT, "unknown option" );
207 if ( mailfrom == NULL ) {
208 syslog( LOG_ALERT, "required argument -f not present" );
211 if ( errorsfrom == NULL ) {
212 syslog( LOG_ALERT, "required argument -m not present" );
215 if ( host == NULL ) {
216 syslog( LOG_ALERT, "required argument -h not present" );
220 if ( connect_to_x500() != 0 )
229 syslog( LOG_ALERT, "running as %d", geteuid() );
230 strcpy( buf, argv[0] );
231 for ( i = 1; i < argc; i++ ) {
233 strcat( buf, argv[i] );
236 syslog( LOG_ALERT, "args: (%s)", buf );
241 add_to( &tolist, &numto, sendmailargs );
243 ngroups = numerr = 0;
246 for ( i = optind; i < argc; i++ ) {
250 for ( j = 0; argv[i][j] != '\0'; j++ ) {
251 if ( argv[i][j] == '.' || argv[i][j] == '_' )
256 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
259 if ((strcasecmp(s, ERROR) == 0) ||
260 (strcasecmp(s, ERRORS) == 0)) {
263 } else if ((strcasecmp(s, REQUEST) == 0) ||
264 (strcasecmp(s, REQUESTS) == 0)) {
265 type = GROUP_REQUEST;
267 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
268 type = GROUP_MEMBERS;
270 } else if ((strcasecmp(s, OWNER) == 0) ||
271 (strcasecmp(s, OWNERS) == 0)) {
277 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
278 &errlist, &numerr, type );
282 * If we have both errors and successful deliveries to make or if
283 * if there are any groups to deliver to, we basically need to read
284 * the message twice. So, we have to put it in a tmp file.
287 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
292 if ( (fp = tmpfile()) == NULL ) {
293 syslog( LOG_ALERT, "could not open tmp file" );
294 unbind_and_exit( EX_TEMPFAIL );
297 /* copy the message to a temp file */
298 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
299 if ( fputs( buf, fp ) == EOF ) {
300 syslog( LOG_ALERT, "error writing tmpfile" );
301 unbind_and_exit( EX_TEMPFAIL );
305 if ( dup2( fileno( fp ), 0 ) == -1 ) {
306 syslog( LOG_ALERT, "could not dup2 tmpfile" );
307 unbind_and_exit( EX_TEMPFAIL );
313 /* deal with errors */
316 syslog( LOG_ALERT, "sending errors" );
318 (void) rewind( stdin );
319 send_errors( errlist, numerr );
322 (void) ldap_unbind( ld );
324 /* send to groups with errorsTo */
327 syslog( LOG_ALERT, "sending to groups with errorsto" );
329 (void) rewind( stdin );
330 send_group( togroups, ngroups );
333 /* send to expanded aliases and groups w/o errorsTo */
334 if ( numto > nargs ) {
336 syslog( LOG_ALERT, "sending to aliases and groups" );
338 (void) rewind( stdin );
339 send_message( tolist );
349 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
350 syslog( LOG_ALERT, "ldap_open failed" );
354 opt = MAIL500_MAXAMBIGUOUS;
355 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
356 opt = LDAP_DEREF_ALWAYS;
357 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
359 if ( ldap_simple_bind_s( ld, MAIL500_BINDDN, NULL ) != LDAP_SUCCESS ) {
360 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
374 for ( i = 0; a[i] != '\0'; i++ ) {
375 if ( a[i] != b[i] ) {
380 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
394 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
405 LDAPMessage *e, *res;
406 struct timeval timeout;
409 char realfilter[1024];
410 char **mail, **onvacation = NULL, **uid = NULL;
413 * Look up the name in X.500, add the appropriate addresses found
414 * to the to list, or to the err list in case of error. Groups are
415 * handled by the do_group routine, individuals are handled here.
416 * When looking up name, we follow the bases hierarchy, looking
417 * in base[0] first, then base[1], etc. For each base, there is
418 * a set of search filters to try, in order. If something goes
419 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
420 * If the b_rdnpref flag is set, then we give preference to entries
421 * that matched name because it's their rdn, otherwise not.
424 timeout.tv_sec = MAIL500_TIMEOUT;
426 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
427 if ( ! (base[b].b_search & type) ) {
430 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
431 char *format, *p, *s, *d;
435 for ( argc = 0; argc < 3; argc++ ) {
439 format = strdup( base[b].b_filter[f] );
440 for ( argc = 0, p = format; *p; p++ ) {
443 case 's': /* %s is the name */
447 case 'h': /* %h is the host */
454 "unknown format %c", *p );
462 /* three names ought to do... */
463 sprintf( filter, format, argv[0], argv[1], argv[2] );
465 for ( s = filter, d = realfilter; *s; s++, d++ ) {
474 rc = ldap_search_st( ld, base[b].b_dn,
475 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
478 /* some other trouble - try again later */
479 if ( rc != LDAP_SUCCESS &&
480 rc != LDAP_SIZELIMIT_EXCEEDED ) {
481 syslog( LOG_ALERT, "return 0x%x from X.500",
483 unbind_and_exit( EX_TEMPFAIL );
486 if ( (match = ldap_count_entries( ld, res )) != 0 )
496 /* trouble - try again later */
498 syslog( LOG_ALERT, "error parsing result from X.500" );
499 unbind_and_exit( EX_TEMPFAIL );
502 /* no matches - bounce with user unknown */
504 if ( type == USER ) {
505 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
507 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
512 /* more than one match - bounce with ambiguous user? */
514 LDAPMessage *next, *tmpres = NULL;
518 /* not giving rdn preference - bounce with ambiguous user */
519 if ( base[b].b_rdnpref == 0 ) {
520 add_error( err, nerr, E_AMBIGUOUS, name, res );
525 * giving rdn preference - see if any entries were matched
526 * because of their rdn. If so, collect them to deal with
527 * later (== 1 we deliver, > 1 we bounce).
530 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
531 next = ldap_next_entry( ld, e );
532 dn = ldap_get_dn( ld, e );
533 xdn = ldap_explode_dn( dn, 1 );
535 /* XXX bad, but how else can we do it? XXX */
536 if ( strcasecmp( xdn[0], name ) == 0 ) {
537 ldap_delete_result_entry( &res, e );
538 ldap_add_result_entry( &tmpres, e );
541 ldap_value_free( xdn );
545 /* nothing matched by rdn - go ahead and bounce */
546 if ( tmpres == NULL ) {
547 add_error( err, nerr, E_AMBIGUOUS, name, res );
550 /* more than one matched by rdn - bounce with rdn matches */
551 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
552 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
556 } else if ( match < 0 ) {
557 syslog( LOG_ALERT, "error parsing result from X.500" );
558 unbind_and_exit( EX_TEMPFAIL );
561 /* otherwise one matched by rdn - send to it */
567 * if we get this far, it means that we found a single match for
568 * name. for a user, we deliver to the mail attribute or bounce
569 * with address and phone if no mail attr. for a group, we
570 * deliver to all members or bounce to rfc822ErrorsTo if no members.
574 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
575 syslog( LOG_ALERT, "error parsing entry from X.500" );
576 unbind_and_exit( EX_TEMPFAIL );
579 dn = ldap_get_dn( ld, e );
581 if ( type == GROUP_ERRORS ) {
582 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
583 do_group_errors( e, dn, to, nto, err, nerr );
585 } else if ( type == GROUP_REQUEST ) {
586 /* sent to group-request - resend to [rfc822]RequestsTo attr */
587 do_group_request( e, dn, to, nto, err, nerr );
589 } else if ( type == GROUP_MEMBERS ) {
590 /* sent to group-members - expand */
591 do_group_members( e, dn, to, nto, togroups, ngroups, err,
594 } else if ( type == GROUP_OWNER ) {
595 /* sent to group-owner - resend to owner attr */
596 do_group_owner( e, dn, to, nto, err, nerr );
598 } else if ( isgroup( e ) ) {
600 * sent to group - resend from [rfc822]ErrorsTo if it's there,
601 * otherwise, expand the group
604 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
610 * sent to user - mail attribute => add it to the to list,
613 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
618 /* try to detect simple mail loops */
619 sprintf( buf, "%s@%s", name, host );
620 for ( i = 0; mail[i] != NULL; i++ ) {
622 * address is the same as the one we're
623 * sending to - mail loop. syslog the
624 * problem, bounce a message back to the
625 * sender (who else?), and delete the bogus
626 * addr from the list.
629 if ( (h = strchr( mail[i], '@' )) != NULL ) {
631 if ( strcasecmp( h, host ) == 0 ) {
633 "potential loop detected (%s)",
638 if ( mailcmp( buf, mail[i] ) == 0 ) {
640 "loop detected (%s)", mail[i] );
642 /* remove the bogus address */
643 for ( j = i; mail[j] != NULL; j++ ) {
648 if ( mail[0] != NULL ) {
649 add_to( to, nto, mail );
651 add_error( err, nerr, E_NOEMAIL, name, res );
654 ldap_value_free( mail );
656 add_error( err, nerr, E_NOEMAIL, name, res );
660 * If the user is on vacation, send a copy of the mail to
661 * the vacation server. The address is constructed from
662 * the vacationhost (set in a command line argument) and
663 * the uid (XXX this should be more general XXX).
666 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
667 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
672 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
673 sprintf( buf, "%s@%s", uid[0], vacationhost );
678 add_to( to, nto, vaddr );
681 "user without a uid on vacation (%s)",
687 if ( onvacation != NULL ) {
688 ldap_value_free( onvacation );
691 ldap_value_free( uid );
699 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
713 * If this group has an rfc822ErrorsTo attribute, we need to
714 * arrange for errors involving this group to go there, not
715 * to the sender. Since sendmail only has the concept of a
716 * single sender, we arrange for errors to go to groupname-errors,
717 * which we then handle specially when (if) it comes back to us
718 * by expanding to all the rfc822ErrorsTo addresses. If it has no
719 * rfc822ErrorsTo attribute, we call do_group_members() to expand
723 if ( group_loop( dn ) ) {
728 * check for moderated groups - if the group has a moderator
729 * attribute, we check to see if the from address is one of
730 * the moderator values. if so, continue on. if not, arrange
731 * to send the mail to the moderator(s). need to do this before
732 * we change the from below.
735 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
736 /* check if it came from any of the group's moderators */
737 for ( i = 0; moderator[i] != NULL; i++ ) {
738 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
742 /* not from the moderator? */
743 if ( moderator[i] == NULL ) {
744 add_to( to, nto, moderator );
745 ldap_value_free( moderator );
749 /* else from the moderator - fall through and deliver it */
752 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
753 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
754 add_group( dn, togroups, ngroups );
759 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
766 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
776 int i, rc, anymembers;
778 char **mail, **member, **joinable, **suppress;
780 LDAPMessage *ee, *res;
781 struct timeval timeout;
785 * if all has gone according to plan, we've already arranged for
786 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
787 * so all we have to do here is arrange to send to the
788 * rfc822Mailbox attribute, the member attribute, and anyone who
789 * has joined the group by setting memberOfGroup equal to the
793 /* add members in the group itself - mail attribute */
795 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
797 add_to( to, nto, mail );
799 ldap_value_free( mail );
802 /* add members in the group itself - member attribute */
803 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
804 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
806 for ( i = 0; member[i] != NULL; i++ ) {
807 if ( strcasecmp( dn, member[i] ) == 0 ) {
808 syslog( LOG_ALERT, "group (%s) contains itself",
812 add_member( dn, member[i], to, nto, togroups,
813 ngroups, err, nerr, suppress );
817 ldap_value_free( suppress );
819 ldap_value_free( member );
822 /* add members who have joined by setting memberOfGroup */
823 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
824 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
825 if ( ! anymembers ) {
826 add_error( err, nerr, E_NOMEMBERS, dn,
830 ldap_value_free( joinable );
833 ldap_value_free( joinable );
835 sprintf( filter, "(memberOfGroup=%s)", dn );
837 timeout.tv_sec = MAIL500_TIMEOUT;
840 /* for each subtree to look in... */
841 opt = MAIL500_MAXAMBIGUOUS;
842 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
843 for ( i = 0; base[i].b_dn != NULL; i++ ) {
844 /* find entries that have joined this group... */
845 rc = ldap_search_st( ld, base[i].b_dn,
846 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
849 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
850 rc == LDAP_TIMELIMIT_EXCEEDED ) {
852 "group search limit exceeded %d", rc );
853 unbind_and_exit( EX_TEMPFAIL );
856 if ( rc != LDAP_SUCCESS ) {
857 syslog( LOG_ALERT, "group search return 0x%x",
859 unbind_and_exit( EX_TEMPFAIL );
862 /* for each entry that has joined... */
863 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
864 ee = ldap_next_entry( ld, ee ) ) {
866 if ( isgroup( ee ) ) {
867 ndn = ldap_get_dn( ld, ee );
869 if ( do_group( e, ndn, to, nto,
870 togroups, ngroups, err, nerr )
873 "group loop (%s) (%s)",
882 /* add them to the to list */
883 if ( (mail = ldap_get_values( ld, ee, "mail" ))
885 add_to( to, nto, mail );
887 ldap_value_free( mail );
889 /* else generate a bounce */
891 ndn = ldap_get_dn( ld, ee );
893 add_error( err, nerr,
894 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
902 opt = MAIL500_MAXAMBIGUOUS;
903 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
906 if ( ! anymembers ) {
907 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
913 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr, suppress )
927 LDAPMessage *res, *e;
928 struct timeval timeout;
930 timeout.tv_sec = MAIL500_TIMEOUT;
932 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
933 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
934 if ( rc == LDAP_NO_SUCH_OBJECT ) {
935 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
939 syslog( LOG_ALERT, "member search return 0x%x", rc );
941 unbind_and_exit( EX_TEMPFAIL );
945 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
946 syslog( LOG_ALERT, "member search error parsing entry" );
948 unbind_and_exit( EX_TEMPFAIL );
950 ndn = ldap_get_dn( ld, e );
952 /* allow groups within groups */
953 if ( isgroup( e ) ) {
954 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
956 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
964 /* send to the member's mail attribute */
965 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
966 add_to( to, nto, mail );
968 ldap_value_free( mail );
970 /* else generate a bounce */
972 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
974 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
983 do_group_request( e, dn, to, nto, err, nerr )
993 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
994 "requestsTo" )) != NULL ) {
995 add_to( to, nto, requeststo );
997 ldap_value_free( requeststo );
999 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
1005 do_group_errors( e, dn, to, nto, err, nerr )
1015 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1016 "errorsTo" )) != NULL ) {
1017 add_to( to, nto, errorsto );
1019 ldap_value_free( errorsto );
1021 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1027 do_group_owner( e, dn, to, nto, err, nerr )
1037 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1038 add_to( to, nto, owner );
1039 ldap_value_free( owner );
1041 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1051 #ifndef HAVE_WAITPID
1052 WAITSTATUSTYPE status;
1059 strcpy( buf, to[0] );
1060 for ( i = 1; to[i] != NULL; i++ ) {
1062 strcat( buf, to[i] );
1065 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1069 if ( pid = fork() ) {
1071 waitpid( pid, (int *) NULL, 0 );
1073 wait4( pid, &status, WAIT_FLAGS, 0 );
1077 /* to includes sendmailargs */
1078 execv( MAIL500_SENDMAIL, to );
1080 syslog( LOG_ALERT, "execv failed" );
1082 exit( EX_TEMPFAIL );
1087 send_group( group, ngroup )
1095 #ifndef HAVE_WAITPID
1096 WAITSTATUSTYPE status;
1099 for ( i = 0; i < ngroup; i++ ) {
1100 (void) rewind( stdin );
1102 iargv[0] = MAIL500_SENDMAIL;
1104 iargv[2] = group[i].g_errorsto;
1105 iargv[3] = "-oMrX.500";
1112 add_to( &argv, &argc, iargv );
1113 add_to( &argv, &argc, group[i].g_members );
1119 strcpy( buf, argv[0] );
1120 for ( i = 1; i < argc; i++ ) {
1122 strcat( buf, argv[i] );
1125 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1129 if ( pid = fork() ) {
1131 waitpid( pid, (int *) NULL, 0 );
1133 wait4( pid, &status, WAIT_FLAGS, 0 );
1137 execv( MAIL500_SENDMAIL, argv );
1139 syslog( LOG_ALERT, "execv failed" );
1141 exit( EX_TEMPFAIL );
1149 send_errors( err, nerr )
1153 int pid, i, namelen;
1158 #ifndef HAVE_WAITPID
1159 WAITSTATUSTYPE status;
1162 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1163 mailfrom = errorsfrom;
1166 argv[0] = MAIL500_SENDMAIL;
1167 argv[1] = "-oMrX.500";
1171 argv[5] = MAIL500_BOUNCEFROM;
1178 strcpy( buf, argv[0] );
1179 for ( i = 1; argv[i] != NULL; i++ ) {
1181 strcat( buf, argv[i] );
1184 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1187 if ( pipe( fd ) == -1 ) {
1188 syslog( LOG_ALERT, "cannot create pipe" );
1189 exit( EX_TEMPFAIL );
1192 if ( pid = fork() ) {
1193 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1194 syslog( LOG_ALERT, "cannot fdopen pipe" );
1195 exit( EX_TEMPFAIL );
1198 fprintf( fp, "To: %s\n", mailfrom );
1199 fprintf( fp, "From: %s\n", errorsfrom );
1200 fprintf( fp, "Subject: undeliverable mail\n" );
1201 fprintf( fp, "\n" );
1202 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1203 for ( i = 0; i < nerr; i++ ) {
1204 namelen = strlen( err[i].e_addr );
1205 fprintf( fp, "\n" );
1207 switch ( err[i].e_code ) {
1209 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1212 case E_GROUPUNKNOWN:
1213 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1217 fprintf( fp, "%s: Group member does not exist\n",
1219 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1220 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1221 fprintf( fp, "then re-adding the person to the group.\n" );
1225 fprintf( fp, "%s: Group exists but has no request address\n",
1230 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1235 fprintf( fp, "%s: Group exists but has no owner\n",
1240 do_ambiguous( fp, &err[i], namelen );
1244 do_noemail( fp, &err[i], namelen );
1247 case E_MEMBERNOEMAIL:
1248 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1252 case E_JOINMEMBERNOEMAIL:
1253 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1258 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1259 err[i].e_addr, err[i].e_loop );
1263 fprintf( fp, "%s: Group has no members\n",
1268 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1269 unbind_and_exit( EX_TEMPFAIL );
1274 fprintf( fp, "\n------- The original message sent:\n\n" );
1276 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1282 waitpid( pid, (int *) NULL, 0 );
1284 wait4( pid, &status, WAIT_FLAGS, 0 );
1289 execv( MAIL500_SENDMAIL, argv );
1291 syslog( LOG_ALERT, "execv failed" );
1293 exit( EX_TEMPFAIL );
1300 do_noemail( fp, err, namelen )
1309 fprintf(fp, "%s: User has no email address registered.\n",
1311 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1312 namelen, " ", err->e_addr );
1315 dn = ldap_get_dn( ld, err->e_msg );
1316 ufn = ldap_explode_dn( dn, 1 );
1317 rdn = strdup( ufn[0] );
1318 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1319 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1321 for ( i = 0; vals[i]; i++ ) {
1322 last = strlen( vals[i] ) - 1;
1323 if ( isdigit( vals[i][last] ) ) {
1324 rdn = strdup( vals[i] );
1329 ldap_value_free( vals );
1332 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1335 ldap_value_free( ufn );
1337 /* titles or descriptions */
1338 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1339 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1341 fprintf( fp, "%*s No title or description registered\n",
1344 for ( i = 0; vals[i] != NULL; i++ ) {
1345 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1348 ldap_value_free( vals );
1351 /* postal address */
1352 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1354 fprintf( fp, "%*s No postal address registered\n", namelen,
1357 fprintf( fp, "%*s ", namelen, " " );
1358 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1359 if ( vals[0][i] == '$' ) {
1360 fprintf( fp, "\n%*s ", namelen, " " );
1361 while ( isspace( vals[0][i+1] ) )
1364 fprintf( fp, "%c", vals[0][i] );
1367 fprintf( fp, "\n" );
1369 ldap_value_free( vals );
1372 /* telephone number */
1373 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1375 fprintf( fp, "%*s No phone number registered\n", namelen,
1378 for ( i = 0; vals[i] != NULL; i++ ) {
1379 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1382 ldap_value_free( vals );
1388 do_ambiguous( fp, err, namelen )
1398 i = ldap_result2error( ld, err->e_msg, 0 );
1400 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1401 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1402 ldap_count_entries( ld, err->e_msg ) );
1404 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1405 e = ldap_next_entry( ld, e ) ) {
1406 dn = ldap_get_dn( ld, e );
1407 ufn = ldap_explode_dn( dn, 1 );
1408 rdn = strdup( ufn[0] );
1409 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1410 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1411 for ( i = 0; vals[i]; i++ ) {
1412 last = strlen( vals[i] ) - 1;
1413 if ( isdigit( vals[i][last] ) ) {
1414 rdn = strdup( vals[i] );
1419 ldap_value_free( vals );
1423 if ( isgroup( e ) ) {
1424 vals = ldap_get_values( ld, e, "description" );
1426 vals = ldap_get_values( ld, e, "title" );
1429 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1430 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1431 fprintf( fp, " %s\n", vals[i] );
1436 ldap_value_free( ufn );
1438 ldap_value_free( vals );
1443 count_values( list )
1448 for ( i = 0; list && list[i] != NULL; i++ )
1455 add_to( list, nlist, new )
1460 int i, nnew, oldnlist;
1462 nnew = count_values( new );
1465 if ( *list == NULL || *nlist == 0 ) {
1466 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1469 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1470 nnew * sizeof(char *) + sizeof(char *) );
1474 for ( i = 0; i < nnew; i++ )
1475 (*list)[i + oldnlist] = strdup( new[i] );
1476 (*list)[*nlist] = NULL;
1488 oclist = ldap_get_values( ld, e, "objectClass" );
1490 for ( i = 0; oclist[i] != NULL; i++ ) {
1491 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1492 ldap_value_free( oclist );
1496 ldap_value_free( oclist );
1502 add_error( err, nerr, code, addr, msg )
1510 *err = (Error *) malloc( sizeof(Error) );
1512 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1515 (*err)[*nerr].e_code = code;
1516 (*err)[*nerr].e_addr = strdup( addr );
1517 (*err)[*nerr].e_msg = msg;
1524 add_group( dn, list, nlist )
1532 for ( i = 0; i < *nlist; i++ ) {
1533 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1534 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1539 ufn = ldap_explode_dn( dn, 1 );
1540 namelen = strlen( ufn[0] );
1542 if ( *nlist == 0 ) {
1543 *list = (Group *) malloc( sizeof(Group) );
1545 *list = (Group *) realloc( *list, (*nlist + 1) *
1549 /* send errors to groupname-errors@host */
1550 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1552 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1553 (void) canonical( (*list)[*nlist].g_errorsto );
1555 /* send to groupname-members@host - make it a list for send_group */
1556 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1557 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1558 sizeof(MEMBERS) + hostlen + 2 );
1559 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1561 (void) canonical( (*list)[*nlist].g_members[0] );
1562 (*list)[*nlist].g_members[1] = NULL;
1564 /* save the group's dn so we can check for loops above */
1565 (*list)[*nlist].g_dn = strdup( dn );
1569 ldap_value_free( ufn );
1575 unbind_and_exit( rc )
1580 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1581 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1592 for ( ; *s != '\0'; s++ ) {
1605 static char **groups;
1608 for ( i = 0; i < ngroups; i++ ) {
1609 if ( strcmp( dn, groups[i] ) == 0 )
1614 groups = (char **) malloc( sizeof(char *) );
1616 groups = (char **) realloc( groups,
1617 (ngroups + 1) * sizeof(char *) );
1619 groups[ngroups++] = strdup( dn );
1625 has_attributes( e, attr1, attr2 )
1632 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1633 ldap_value_free( attr );
1637 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1638 ldap_value_free( attr );
1646 get_attributes_mail_dn( e, attr1, attr2 )
1649 char *attr2; /* this one is dn-valued */
1651 LDAPMessage *ee, *res;
1652 char **vals, **dnlist, **mail, **grname, **graddr;
1655 struct timeval timeout;
1657 dn = ldap_get_dn( ld, e );
1659 vals = ldap_get_values( ld, e, attr1 );
1660 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1663 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1664 timeout.tv_sec = MAIL500_TIMEOUT;
1665 timeout.tv_usec = 0;
1667 for ( i = 0; dnlist[i] != NULL; i++ ) {
1668 if ( (rc = ldap_search_st( ld, dnlist[i],
1669 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1670 &timeout, &res )) != LDAP_SUCCESS ) {
1671 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1672 unbind_and_exit( EX_TEMPFAIL );
1675 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1681 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1682 syslog( LOG_ALERT, "error parsing x500 entry" );
1686 if ( isgroup(ee) ) {
1689 grname = ldap_explode_dn( dnlist[i], 1 );
1691 /* groupname + host + @ + null */
1692 graddr[0] = (char *) malloc( strlen( grname[0] )
1693 + strlen( host ) + 2 );
1695 sprintf( graddr[0], "%s@%s", grname[0], host);
1696 (void) canonical( graddr[0] );
1698 add_to( &vals, &nto, graddr );
1701 ldap_value_free( grname );
1702 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1704 add_to( &vals, &nto, mail );
1706 ldap_value_free( mail );
1709 ldap_msgfree( res );