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 #if LDAP_VERSION < LDAP_VERSION3
35 /* quick fix until we have ldap_set_options */
36 #include "../libraries/libldap/ldap-int.h"
39 #include "ldapconfig.h"
42 #define GROUP_ERRORS 0x02
43 #define GROUP_REQUEST 0x04
44 #define GROUP_MEMBERS 0x08
45 #define GROUP_OWNER 0x10
48 #define ERRORS "errors"
49 #define REQUEST "request"
50 #define REQUESTS "requests"
51 #define MEMBERS "members"
53 #define OWNERS "owners"
56 char *vacationhost = NULL;
57 char *errorsfrom = NULL;
58 char *mailfrom = NULL;
60 char *ldaphost = LDAPHOST;
66 #define E_USERUNKNOWN 1
72 #define E_JOINMEMBERNOEMAIL 7
73 #define E_MEMBERNOEMAIL 8
75 #define E_NOMEMBERS 10
77 #define E_GROUPUNKNOWN 12
83 #define e_msg e_union.e_u_msg
84 #define e_loop e_union.e_u_loop
87 typedef struct groupto {
93 typedef struct baseinfo {
94 char *b_dn; /* dn to start searching at */
95 char b_rdnpref; /* give rdn's preference when searching? */
96 int b_search; /* ORed with the type of thing the address */
97 /* looks like (USER, GROUP_ERRORS, etc.) */
98 /* to see if this should be searched */
99 char *b_filter[3]; /* filter to apply - name substituted for %s */
100 /* (up to three of them) */
104 { "ou=People, o=University of Michigan, c=US",
106 "uid=%s", "cn=%s", NULL,
107 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
109 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
110 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
112 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
116 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
118 static char *attrs[] = { "objectClass", "title", "postaladdress",
119 "telephoneNumber", "mail", "description", "owner",
120 "errorsTo", "rfc822ErrorsTo", "requestsTo",
121 "rfc822RequestsTo", "joinable", "cn", "member",
122 "moderator", "onVacation", "uid",
123 "suppressNoEmailError", NULL };
127 static do_group_members();
128 static send_message();
129 static send_errors();
131 static do_ambiguous();
136 static unbind_and_exit();
139 static has_attributes();
140 static char **get_attributes_mail_dn();
141 static char *canonical();
151 int numto, ngroups, numerr, nargs;
154 extern int optind, errno;
157 if ( (myname = strrchr( argv[0], '/' )) == NULL )
158 myname = strdup( argv[0] );
160 myname = strdup( myname + 1 );
163 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
165 openlog( myname, OPENLOG_OPTIONS );
168 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
170 case 'd': /* turn on debugging */
171 debug = atoi( optarg );
174 case 'f': /* who it's from & where errors should go */
175 mailfrom = strdup( optarg );
176 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
177 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
178 sendmailargs[j+1] = mailfrom;
184 case 'h': /* hostname */
185 host = strdup( optarg );
186 hostlen = strlen(host);
189 case 'l': /* ldap host */
190 ldaphost = strdup( optarg );
193 /* mailer-daemon address - who we should */
194 case 'm': /* say errors come from */
195 errorsfrom = strdup( optarg );
198 case 'v': /* vacation host */
199 vacationhost = strdup( optarg );
203 syslog( LOG_ALERT, "unknown option" );
208 if ( mailfrom == NULL ) {
209 syslog( LOG_ALERT, "required argument -f not present" );
212 if ( errorsfrom == NULL ) {
213 syslog( LOG_ALERT, "required argument -m not present" );
216 if ( host == NULL ) {
217 syslog( LOG_ALERT, "required argument -h not present" );
221 if ( connect_to_x500() != 0 )
230 syslog( LOG_ALERT, "running as %d", geteuid() );
231 strcpy( buf, argv[0] );
232 for ( i = 1; i < argc; i++ ) {
234 strcat( buf, argv[i] );
237 syslog( LOG_ALERT, "args: (%s)", buf );
242 add_to( &tolist, &numto, sendmailargs );
244 ngroups = numerr = 0;
247 for ( i = optind; i < argc; i++ ) {
251 for ( j = 0; argv[i][j] != '\0'; j++ ) {
252 if ( argv[i][j] == '.' || argv[i][j] == '_' )
257 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
260 if ((strcasecmp(s, ERROR) == 0) ||
261 (strcasecmp(s, ERRORS) == 0)) {
264 } else if ((strcasecmp(s, REQUEST) == 0) ||
265 (strcasecmp(s, REQUESTS) == 0)) {
266 type = GROUP_REQUEST;
268 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
269 type = GROUP_MEMBERS;
271 } else if ((strcasecmp(s, OWNER) == 0) ||
272 (strcasecmp(s, OWNERS) == 0)) {
278 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
279 &errlist, &numerr, type );
283 * If we have both errors and successful deliveries to make or if
284 * if there are any groups to deliver to, we basically need to read
285 * the message twice. So, we have to put it in a tmp file.
288 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
293 if ( (fp = tmpfile()) == NULL ) {
294 syslog( LOG_ALERT, "could not open tmp file" );
295 unbind_and_exit( EX_TEMPFAIL );
298 /* copy the message to a temp file */
299 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
300 if ( fputs( buf, fp ) == EOF ) {
301 syslog( LOG_ALERT, "error writing tmpfile" );
302 unbind_and_exit( EX_TEMPFAIL );
306 if ( dup2( fileno( fp ), 0 ) == -1 ) {
307 syslog( LOG_ALERT, "could not dup2 tmpfile" );
308 unbind_and_exit( EX_TEMPFAIL );
314 /* deal with errors */
317 syslog( LOG_ALERT, "sending errors" );
319 (void) rewind( stdin );
320 send_errors( errlist, numerr );
323 (void) ldap_unbind( ld );
325 /* send to groups with errorsTo */
328 syslog( LOG_ALERT, "sending to groups with errorsto" );
330 (void) rewind( stdin );
331 send_group( togroups, ngroups );
334 /* send to expanded aliases and groups w/o errorsTo */
335 if ( numto > nargs ) {
337 syslog( LOG_ALERT, "sending to aliases and groups" );
339 (void) rewind( stdin );
340 send_message( tolist );
348 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
349 syslog( LOG_ALERT, "ldap_open failed" );
352 ld->ld_sizelimit = MAIL500_MAXAMBIGUOUS;
353 ld->ld_deref = LDAP_DEREF_ALWAYS;
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;
779 * if all has gone according to plan, we've already arranged for
780 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
781 * so all we have to do here is arrange to send to the
782 * rfc822Mailbox attribute, the member attribute, and anyone who
783 * has joined the group by setting memberOfGroup equal to the
787 /* add members in the group itself - mail attribute */
789 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
791 add_to( to, nto, mail );
793 ldap_value_free( mail );
796 /* add members in the group itself - member attribute */
797 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
798 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
800 for ( i = 0; member[i] != NULL; i++ ) {
801 if ( strcasecmp( dn, member[i] ) == 0 ) {
802 syslog( LOG_ALERT, "group (%s) contains itself",
806 add_member( dn, member[i], to, nto, togroups,
807 ngroups, err, nerr, suppress );
811 ldap_value_free( suppress );
813 ldap_value_free( member );
816 /* add members who have joined by setting memberOfGroup */
817 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
818 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
819 if ( ! anymembers ) {
820 add_error( err, nerr, E_NOMEMBERS, dn,
824 ldap_value_free( joinable );
827 ldap_value_free( joinable );
829 sprintf( filter, "(memberOfGroup=%s)", dn );
831 timeout.tv_sec = MAIL500_TIMEOUT;
834 /* for each subtree to look in... */
835 ld->ld_sizelimit = MAIL500_MAXGROUPMEMBERS;
836 for ( i = 0; base[i].b_dn != NULL; i++ ) {
837 /* find entries that have joined this group... */
838 rc = ldap_search_st( ld, base[i].b_dn,
839 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
842 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
843 rc == LDAP_TIMELIMIT_EXCEEDED ) {
845 "group search limit exceeded %d", rc );
846 unbind_and_exit( EX_TEMPFAIL );
849 if ( rc != LDAP_SUCCESS ) {
850 syslog( LOG_ALERT, "group search return 0x%x",
852 unbind_and_exit( EX_TEMPFAIL );
855 /* for each entry that has joined... */
856 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
857 ee = ldap_next_entry( ld, ee ) ) {
859 if ( isgroup( ee ) ) {
860 ndn = ldap_get_dn( ld, ee );
862 if ( do_group( e, ndn, to, nto,
863 togroups, ngroups, err, nerr )
866 "group loop (%s) (%s)",
875 /* add them to the to list */
876 if ( (mail = ldap_get_values( ld, ee, "mail" ))
878 add_to( to, nto, mail );
880 ldap_value_free( mail );
882 /* else generate a bounce */
884 ndn = ldap_get_dn( ld, ee );
886 add_error( err, nerr,
887 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
895 ld->ld_sizelimit = MAIL500_MAXAMBIGUOUS;
898 if ( ! anymembers ) {
899 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
905 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr, suppress )
919 LDAPMessage *res, *e;
920 struct timeval timeout;
922 timeout.tv_sec = MAIL500_TIMEOUT;
924 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
925 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
926 if ( rc == LDAP_NO_SUCH_OBJECT ) {
927 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
931 syslog( LOG_ALERT, "member search return 0x%x", rc );
933 unbind_and_exit( EX_TEMPFAIL );
937 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
938 syslog( LOG_ALERT, "member search error parsing entry" );
940 unbind_and_exit( EX_TEMPFAIL );
942 ndn = ldap_get_dn( ld, e );
944 /* allow groups within groups */
945 if ( isgroup( e ) ) {
946 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
948 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
956 /* send to the member's mail attribute */
957 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
958 add_to( to, nto, mail );
960 ldap_value_free( mail );
962 /* else generate a bounce */
964 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
966 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
975 do_group_request( e, dn, to, nto, err, nerr )
985 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
986 "requestsTo" )) != NULL ) {
987 add_to( to, nto, requeststo );
989 ldap_value_free( requeststo );
991 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
997 do_group_errors( e, dn, to, nto, err, nerr )
1007 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1008 "errorsTo" )) != NULL ) {
1009 add_to( to, nto, errorsto );
1011 ldap_value_free( errorsto );
1013 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1019 do_group_owner( e, dn, to, nto, err, nerr )
1029 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1030 add_to( to, nto, owner );
1031 ldap_value_free( owner );
1033 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1043 #ifndef HAVE_WAITPID
1044 WAITSTATUSTYPE status;
1051 strcpy( buf, to[0] );
1052 for ( i = 1; to[i] != NULL; i++ ) {
1054 strcat( buf, to[i] );
1057 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1061 if ( pid = fork() ) {
1063 waitpid( pid, (int *) NULL, 0 );
1065 wait4( pid, &status, WAIT_FLAGS, 0 );
1069 /* to includes sendmailargs */
1070 execv( MAIL500_SENDMAIL, to );
1072 syslog( LOG_ALERT, "execv failed" );
1074 exit( EX_TEMPFAIL );
1079 send_group( group, ngroup )
1087 #ifndef HAVE_WAITPID
1088 WAITSTATUSTYPE status;
1091 for ( i = 0; i < ngroup; i++ ) {
1092 (void) rewind( stdin );
1094 iargv[0] = MAIL500_SENDMAIL;
1096 iargv[2] = group[i].g_errorsto;
1097 iargv[3] = "-oMrX.500";
1104 add_to( &argv, &argc, iargv );
1105 add_to( &argv, &argc, group[i].g_members );
1111 strcpy( buf, argv[0] );
1112 for ( i = 1; i < argc; i++ ) {
1114 strcat( buf, argv[i] );
1117 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1121 if ( pid = fork() ) {
1123 waitpid( pid, (int *) NULL, 0 );
1125 wait4( pid, &status, WAIT_FLAGS, 0 );
1129 execv( MAIL500_SENDMAIL, argv );
1131 syslog( LOG_ALERT, "execv failed" );
1133 exit( EX_TEMPFAIL );
1141 send_errors( err, nerr )
1145 int pid, i, namelen;
1150 #ifndef HAVE_WAITPID
1151 WAITSTATUSTYPE status;
1154 argv[0] = MAIL500_SENDMAIL;
1155 argv[1] = "-oMrX.500";
1159 argv[5] = errorsfrom;
1166 strcpy( buf, argv[0] );
1167 for ( i = 1; argv[i] != NULL; i++ ) {
1169 strcat( buf, argv[i] );
1172 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1175 if ( pipe( fd ) == -1 ) {
1176 syslog( LOG_ALERT, "cannot create pipe" );
1177 exit( EX_TEMPFAIL );
1180 if ( pid = fork() ) {
1181 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1182 syslog( LOG_ALERT, "cannot fdopen pipe" );
1183 exit( EX_TEMPFAIL );
1186 fprintf( fp, "To: %s\n", mailfrom );
1187 fprintf( fp, "From: %s\n", errorsfrom );
1188 fprintf( fp, "Subject: undeliverable mail\n" );
1189 fprintf( fp, "\n" );
1190 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1191 for ( i = 0; i < nerr; i++ ) {
1192 namelen = strlen( err[i].e_addr );
1193 fprintf( fp, "\n" );
1195 switch ( err[i].e_code ) {
1197 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1200 case E_GROUPUNKNOWN:
1201 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1205 fprintf( fp, "%s: Group member does not exist\n",
1207 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1208 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1209 fprintf( fp, "then re-adding the person to the group.\n" );
1213 fprintf( fp, "%s: Group exists but has no request address\n",
1218 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1223 fprintf( fp, "%s: Group exists but has no owner\n",
1228 do_ambiguous( fp, &err[i], namelen );
1232 do_noemail( fp, &err[i], namelen );
1235 case E_MEMBERNOEMAIL:
1236 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1240 case E_JOINMEMBERNOEMAIL:
1241 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1246 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1247 err[i].e_addr, err[i].e_loop );
1251 fprintf( fp, "%s: Group has no members\n",
1256 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1257 unbind_and_exit( EX_TEMPFAIL );
1262 fprintf( fp, "\n------- The original message sent:\n\n" );
1264 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1270 waitpid( pid, (int *) NULL, 0 );
1272 wait4( pid, &status, WAIT_FLAGS, 0 );
1277 execv( MAIL500_SENDMAIL, argv );
1279 syslog( LOG_ALERT, "execv failed" );
1281 exit( EX_TEMPFAIL );
1288 do_noemail( fp, err, namelen )
1297 fprintf(fp, "%s: User has no email address registered.\n",
1299 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1300 namelen, " ", err->e_addr );
1303 dn = ldap_get_dn( ld, err->e_msg );
1304 ufn = ldap_explode_dn( dn, 1 );
1305 rdn = strdup( ufn[0] );
1306 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1307 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1309 for ( i = 0; vals[i]; i++ ) {
1310 last = strlen( vals[i] ) - 1;
1311 if ( isdigit( vals[i][last] ) ) {
1312 rdn = strdup( vals[i] );
1317 ldap_value_free( vals );
1320 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1323 ldap_value_free( ufn );
1325 /* titles or descriptions */
1326 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1327 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1329 fprintf( fp, "%*s No title or description registered\n",
1332 for ( i = 0; vals[i] != NULL; i++ ) {
1333 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1336 ldap_value_free( vals );
1339 /* postal address */
1340 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1342 fprintf( fp, "%*s No postal address registered\n", namelen,
1345 fprintf( fp, "%*s ", namelen, " " );
1346 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1347 if ( vals[0][i] == '$' ) {
1348 fprintf( fp, "\n%*s ", namelen, " " );
1349 while ( isspace( vals[0][i+1] ) )
1352 fprintf( fp, "%c", vals[0][i] );
1355 fprintf( fp, "\n" );
1357 ldap_value_free( vals );
1360 /* telephone number */
1361 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1363 fprintf( fp, "%*s No phone number registered\n", namelen,
1366 for ( i = 0; vals[i] != NULL; i++ ) {
1367 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1370 ldap_value_free( vals );
1376 do_ambiguous( fp, err, namelen )
1386 i = ldap_result2error( ld, err->e_msg, 0 );
1388 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1389 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1390 ldap_count_entries( ld, err->e_msg ) );
1392 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1393 e = ldap_next_entry( ld, e ) ) {
1394 dn = ldap_get_dn( ld, e );
1395 ufn = ldap_explode_dn( dn, 1 );
1396 rdn = strdup( ufn[0] );
1397 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1398 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1399 for ( i = 0; vals[i]; i++ ) {
1400 last = strlen( vals[i] ) - 1;
1401 if ( isdigit( vals[i][last] ) ) {
1402 rdn = strdup( vals[i] );
1407 ldap_value_free( vals );
1411 if ( isgroup( e ) ) {
1412 vals = ldap_get_values( ld, e, "description" );
1414 vals = ldap_get_values( ld, e, "title" );
1417 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1418 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1419 fprintf( fp, " %s\n", vals[i] );
1424 ldap_value_free( ufn );
1426 ldap_value_free( vals );
1431 count_values( list )
1436 for ( i = 0; list && list[i] != NULL; i++ )
1443 add_to( list, nlist, new )
1448 int i, nnew, oldnlist;
1450 nnew = count_values( new );
1453 if ( *list == NULL || *nlist == 0 ) {
1454 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1457 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1458 nnew * sizeof(char *) + sizeof(char *) );
1462 for ( i = 0; i < nnew; i++ )
1463 (*list)[i + oldnlist] = strdup( new[i] );
1464 (*list)[*nlist] = NULL;
1476 oclist = ldap_get_values( ld, e, "objectClass" );
1478 for ( i = 0; oclist[i] != NULL; i++ ) {
1479 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1480 ldap_value_free( oclist );
1484 ldap_value_free( oclist );
1490 add_error( err, nerr, code, addr, msg )
1498 *err = (Error *) malloc( sizeof(Error) );
1500 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1503 (*err)[*nerr].e_code = code;
1504 (*err)[*nerr].e_addr = strdup( addr );
1505 (*err)[*nerr].e_msg = msg;
1512 add_group( dn, list, nlist )
1520 for ( i = 0; i < *nlist; i++ ) {
1521 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1522 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1527 ufn = ldap_explode_dn( dn, 1 );
1528 namelen = strlen( ufn[0] );
1530 if ( *nlist == 0 ) {
1531 *list = (Group *) malloc( sizeof(Group) );
1533 *list = (Group *) realloc( *list, (*nlist + 1) *
1537 /* send errors to groupname-errors@host */
1538 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1540 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1541 (void) canonical( (*list)[*nlist].g_errorsto );
1543 /* send to groupname-members@host - make it a list for send_group */
1544 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1545 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1546 sizeof(MEMBERS) + hostlen + 2 );
1547 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1549 (void) canonical( (*list)[*nlist].g_members[0] );
1550 (*list)[*nlist].g_members[1] = NULL;
1552 /* save the group's dn so we can check for loops above */
1553 (*list)[*nlist].g_dn = strdup( dn );
1557 ldap_value_free( ufn );
1563 unbind_and_exit( rc )
1568 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1569 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1580 for ( ; *s != '\0'; s++ ) {
1593 static char **groups;
1596 for ( i = 0; i < ngroups; i++ ) {
1597 if ( strcmp( dn, groups[i] ) == 0 )
1602 groups = (char **) malloc( sizeof(char *) );
1604 groups = (char **) realloc( groups,
1605 (ngroups + 1) * sizeof(char *) );
1607 groups[ngroups++] = strdup( dn );
1613 has_attributes( e, attr1, attr2 )
1620 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1621 ldap_value_free( attr );
1625 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1626 ldap_value_free( attr );
1634 get_attributes_mail_dn( e, attr1, attr2 )
1637 char *attr2; /* this one is dn-valued */
1639 LDAPMessage *ee, *res;
1640 char **vals, **dnlist, **mail, **grname, **graddr;
1643 struct timeval timeout;
1645 dn = ldap_get_dn( ld, e );
1647 vals = ldap_get_values( ld, e, attr1 );
1648 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1651 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1652 timeout.tv_sec = MAIL500_TIMEOUT;
1653 timeout.tv_usec = 0;
1655 for ( i = 0; dnlist[i] != NULL; i++ ) {
1656 if ( (rc = ldap_search_st( ld, dnlist[i],
1657 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1658 &timeout, &res )) != LDAP_SUCCESS ) {
1659 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1660 unbind_and_exit( EX_TEMPFAIL );
1663 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1669 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1670 syslog( LOG_ALERT, "error parsing x500 entry" );
1674 if ( isgroup(ee) ) {
1677 grname = ldap_explode_dn( dnlist[i], 1 );
1679 /* groupname + host + @ + null */
1680 graddr[0] = (char *) malloc( strlen( grname[0] )
1681 + strlen( host ) + 2 );
1683 sprintf( graddr[0], "%s@%s", grname[0], host);
1684 (void) canonical( graddr[0] );
1686 add_to( &vals, &nto, graddr );
1689 ldap_value_free( grname );
1690 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1692 add_to( &vals, &nto, mail );
1694 ldap_value_free( mail );
1697 ldap_msgfree( res );