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/string.h>
20 #include <ac/syslog.h>
23 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
31 #ifdef HAVE_SYS_RESOURCE_H
32 #include <sys/resource.h>
40 #include "ldapconfig.h"
42 #ifndef MAIL500_BOUNCEFROM
43 #define MAIL500_BOUNCEFROM "<>"
47 #define GROUP_ERRORS 0x02
48 #define GROUP_REQUEST 0x04
49 #define GROUP_MEMBERS 0x08
50 #define GROUP_OWNER 0x10
53 #define ERRORS "errors"
54 #define REQUEST "request"
55 #define REQUESTS "requests"
56 #define MEMBERS "members"
58 #define OWNERS "owners"
61 char *vacationhost = NULL;
62 char *errorsfrom = NULL;
63 char *mailfrom = NULL;
65 char *ldaphost = NULL;
71 #define E_USERUNKNOWN 1
77 #define E_JOINMEMBERNOEMAIL 7
78 #define E_MEMBERNOEMAIL 8
80 #define E_NOMEMBERS 10
82 #define E_GROUPUNKNOWN 12
88 #define e_msg e_union.e_u_msg
89 #define e_loop e_union.e_u_loop
92 typedef struct groupto {
98 typedef struct baseinfo {
99 char *b_dn; /* dn to start searching at */
100 char b_rdnpref; /* give rdn's preference when searching? */
101 int b_search; /* ORed with the type of thing the address */
102 /* looks like (USER, GROUP_ERRORS, etc.) */
103 /* to see if this should be searched */
104 char *b_filter[3]; /* filter to apply - name substituted for %s */
105 /* (up to three of them) */
109 { "ou=People, o=University of Michigan, c=US",
111 "uid=%s", "cn=%s", NULL,
112 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
114 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
115 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
117 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
121 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
123 static char *attrs[] = { "objectClass", "title", "postaladdress",
124 "telephoneNumber", "mail", "description", "owner",
125 "errorsTo", "rfc822ErrorsTo", "requestsTo",
126 "rfc822RequestsTo", "joinable", "cn", "member",
127 "moderator", "onVacation", "uid",
128 "suppressNoEmailError", NULL };
130 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
131 static int do_group( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
132 static void do_group_members( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
133 static void send_message( char **to );
134 static void send_errors( Error *err, int nerr );
135 static void do_noemail( FILE *fp, Error *err, int namelen );
136 static void do_ambiguous( FILE *fp, Error *err, int namelen );
137 static void add_to( char ***list, int *nlist, char **new );
138 static int isgroup( LDAPMessage *e );
139 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
140 static void add_group( char *dn, Group **list, int *nlist );
141 static void unbind_and_exit( int rc );
142 static int group_loop( char *dn );
143 static void send_group( Group *group, int ngroup );
144 static int has_attributes( LDAPMessage *e, char *attr1, char *attr2 );
145 static char **get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 );
146 static char *canonical( char *s );
147 static int connect_to_x500( void );
149 static void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
150 static void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
151 static void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
152 static void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
155 main ( int argc, char **argv )
161 int numto, ngroups, numerr, nargs;
164 if ( (myname = strrchr( argv[0], '/' )) == NULL )
165 myname = strdup( argv[0] );
167 myname = strdup( myname + 1 );
170 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
172 openlog( myname, OPENLOG_OPTIONS );
175 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
177 case 'd': /* turn on debugging */
178 debug = atoi( optarg );
181 case 'f': /* who it's from & where errors should go */
182 mailfrom = strdup( optarg );
183 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
184 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
185 sendmailargs[j+1] = mailfrom;
191 case 'h': /* hostname */
192 host = strdup( optarg );
193 hostlen = strlen(host);
196 case 'l': /* ldap host */
197 ldaphost = strdup( optarg );
200 /* mailer-daemon address - who we should */
201 case 'm': /* say errors come from */
202 errorsfrom = strdup( optarg );
205 case 'v': /* vacation host */
206 vacationhost = strdup( optarg );
210 syslog( LOG_ALERT, "unknown option" );
215 if ( mailfrom == NULL ) {
216 syslog( LOG_ALERT, "required argument -f not present" );
219 if ( errorsfrom == NULL ) {
220 syslog( LOG_ALERT, "required argument -m not present" );
223 if ( host == NULL ) {
224 syslog( LOG_ALERT, "required argument -h not present" );
228 if ( connect_to_x500() != 0 )
237 syslog( LOG_ALERT, "running as %d", geteuid() );
238 strcpy( buf, argv[0] );
239 for ( i = 1; i < argc; i++ ) {
241 strcat( buf, argv[i] );
244 syslog( LOG_ALERT, "args: (%s)", buf );
249 add_to( &tolist, &numto, sendmailargs );
251 ngroups = numerr = 0;
254 for ( i = optind; i < argc; i++ ) {
258 for ( j = 0; argv[i][j] != '\0'; j++ ) {
259 if ( argv[i][j] == '.' || argv[i][j] == '_' )
264 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
267 if ((strcasecmp(s, ERROR) == 0) ||
268 (strcasecmp(s, ERRORS) == 0)) {
271 } else if ((strcasecmp(s, REQUEST) == 0) ||
272 (strcasecmp(s, REQUESTS) == 0)) {
273 type = GROUP_REQUEST;
275 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
276 type = GROUP_MEMBERS;
278 } else if ((strcasecmp(s, OWNER) == 0) ||
279 (strcasecmp(s, OWNERS) == 0)) {
285 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
286 &errlist, &numerr, type );
290 * If we have both errors and successful deliveries to make or if
291 * if there are any groups to deliver to, we basically need to read
292 * the message twice. So, we have to put it in a tmp file.
295 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
300 if ( (fp = tmpfile()) == NULL ) {
301 syslog( LOG_ALERT, "could not open tmp file" );
302 unbind_and_exit( EX_TEMPFAIL );
305 /* copy the message to a temp file */
306 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
307 if ( fputs( buf, fp ) == EOF ) {
308 syslog( LOG_ALERT, "error writing tmpfile" );
309 unbind_and_exit( EX_TEMPFAIL );
313 if ( dup2( fileno( fp ), 0 ) == -1 ) {
314 syslog( LOG_ALERT, "could not dup2 tmpfile" );
315 unbind_and_exit( EX_TEMPFAIL );
321 /* deal with errors */
324 syslog( LOG_ALERT, "sending errors" );
326 (void) rewind( stdin );
327 send_errors( errlist, numerr );
330 (void) ldap_unbind( ld );
332 /* send to groups with errorsTo */
335 syslog( LOG_ALERT, "sending to groups with errorsto" );
337 (void) rewind( stdin );
338 send_group( togroups, ngroups );
341 /* send to expanded aliases and groups w/o errorsTo */
342 if ( numto > nargs ) {
344 syslog( LOG_ALERT, "sending to aliases and groups" );
346 (void) rewind( stdin );
347 send_message( tolist );
354 connect_to_x500( void )
358 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
359 syslog( LOG_ALERT, "ldap_open failed" );
363 opt = MAIL500_MAXAMBIGUOUS;
364 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
365 opt = LDAP_DEREF_ALWAYS;
366 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
368 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
369 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
377 mailcmp( char *a, char *b )
381 for ( i = 0; a[i] != '\0'; i++ ) {
382 if ( a[i] != b[i] ) {
387 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
413 LDAPMessage *e, *res;
414 struct timeval timeout;
417 char realfilter[1024];
418 char **mail, **onvacation = NULL, **uid = NULL;
421 * Look up the name in X.500, add the appropriate addresses found
422 * to the to list, or to the err list in case of error. Groups are
423 * handled by the do_group routine, individuals are handled here.
424 * When looking up name, we follow the bases hierarchy, looking
425 * in base[0] first, then base[1], etc. For each base, there is
426 * a set of search filters to try, in order. If something goes
427 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
428 * If the b_rdnpref flag is set, then we give preference to entries
429 * that matched name because it's their rdn, otherwise not.
432 timeout.tv_sec = MAIL500_TIMEOUT;
434 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
435 if ( ! (base[b].b_search & type) ) {
438 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
439 char *format, *p, *s, *d;
443 for ( argc = 0; argc < 3; argc++ ) {
447 format = strdup( base[b].b_filter[f] );
448 for ( argc = 0, p = format; *p; p++ ) {
451 case 's': /* %s is the name */
455 case 'h': /* %h is the host */
462 "unknown format %c", *p );
470 /* three names ought to do... */
471 sprintf( filter, format, argv[0], argv[1], argv[2] );
473 for ( s = filter, d = realfilter; *s; s++, d++ ) {
482 rc = ldap_search_st( ld, base[b].b_dn,
483 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
486 /* some other trouble - try again later */
487 if ( rc != LDAP_SUCCESS &&
488 rc != LDAP_SIZELIMIT_EXCEEDED ) {
489 syslog( LOG_ALERT, "return 0x%x from X.500",
491 unbind_and_exit( EX_TEMPFAIL );
494 if ( (match = ldap_count_entries( ld, res )) != 0 )
504 /* trouble - try again later */
506 syslog( LOG_ALERT, "error parsing result from X.500" );
507 unbind_and_exit( EX_TEMPFAIL );
510 /* no matches - bounce with user unknown */
512 if ( type == USER ) {
513 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
515 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
520 /* more than one match - bounce with ambiguous user? */
522 LDAPMessage *next, *tmpres = NULL;
526 /* not giving rdn preference - bounce with ambiguous user */
527 if ( base[b].b_rdnpref == 0 ) {
528 add_error( err, nerr, E_AMBIGUOUS, name, res );
533 * giving rdn preference - see if any entries were matched
534 * because of their rdn. If so, collect them to deal with
535 * later (== 1 we deliver, > 1 we bounce).
538 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
539 next = ldap_next_entry( ld, e );
540 dn = ldap_get_dn( ld, e );
541 xdn = ldap_explode_dn( dn, 1 );
543 /* XXX bad, but how else can we do it? XXX */
544 if ( strcasecmp( xdn[0], name ) == 0 ) {
545 ldap_delete_result_entry( &res, e );
546 ldap_add_result_entry( &tmpres, e );
549 ldap_value_free( xdn );
553 /* nothing matched by rdn - go ahead and bounce */
554 if ( tmpres == NULL ) {
555 add_error( err, nerr, E_AMBIGUOUS, name, res );
558 /* more than one matched by rdn - bounce with rdn matches */
559 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
560 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
564 } else if ( match < 0 ) {
565 syslog( LOG_ALERT, "error parsing result from X.500" );
566 unbind_and_exit( EX_TEMPFAIL );
569 /* otherwise one matched by rdn - send to it */
575 * if we get this far, it means that we found a single match for
576 * name. for a user, we deliver to the mail attribute or bounce
577 * with address and phone if no mail attr. for a group, we
578 * deliver to all members or bounce to rfc822ErrorsTo if no members.
582 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
583 syslog( LOG_ALERT, "error parsing entry from X.500" );
584 unbind_and_exit( EX_TEMPFAIL );
587 dn = ldap_get_dn( ld, e );
589 if ( type == GROUP_ERRORS ) {
590 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
591 do_group_errors( e, dn, to, nto, err, nerr );
593 } else if ( type == GROUP_REQUEST ) {
594 /* sent to group-request - resend to [rfc822]RequestsTo attr */
595 do_group_request( e, dn, to, nto, err, nerr );
597 } else if ( type == GROUP_MEMBERS ) {
598 /* sent to group-members - expand */
599 do_group_members( e, dn, to, nto, togroups, ngroups, err,
602 } else if ( type == GROUP_OWNER ) {
603 /* sent to group-owner - resend to owner attr */
604 do_group_owner( e, dn, to, nto, err, nerr );
606 } else if ( isgroup( e ) ) {
608 * sent to group - resend from [rfc822]ErrorsTo if it's there,
609 * otherwise, expand the group
612 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
618 * sent to user - mail attribute => add it to the to list,
621 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
626 /* try to detect simple mail loops */
627 sprintf( buf, "%s@%s", name, host );
628 for ( i = 0; mail[i] != NULL; i++ ) {
630 * address is the same as the one we're
631 * sending to - mail loop. syslog the
632 * problem, bounce a message back to the
633 * sender (who else?), and delete the bogus
634 * addr from the list.
637 if ( (h = strchr( mail[i], '@' )) != NULL ) {
639 if ( strcasecmp( h, host ) == 0 ) {
641 "potential loop detected (%s)",
646 if ( mailcmp( buf, mail[i] ) == 0 ) {
648 "loop detected (%s)", mail[i] );
650 /* remove the bogus address */
651 for ( j = i; mail[j] != NULL; j++ ) {
656 if ( mail[0] != NULL ) {
657 add_to( to, nto, mail );
659 add_error( err, nerr, E_NOEMAIL, name, res );
662 ldap_value_free( mail );
664 add_error( err, nerr, E_NOEMAIL, name, res );
668 * If the user is on vacation, send a copy of the mail to
669 * the vacation server. The address is constructed from
670 * the vacationhost (set in a command line argument) and
671 * the uid (XXX this should be more general XXX).
674 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
675 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
680 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
681 sprintf( buf, "%s@%s", uid[0], vacationhost );
686 add_to( to, nto, vaddr );
689 "user without a uid on vacation (%s)",
695 if ( onvacation != NULL ) {
696 ldap_value_free( onvacation );
699 ldap_value_free( uid );
720 * If this group has an rfc822ErrorsTo attribute, we need to
721 * arrange for errors involving this group to go there, not
722 * to the sender. Since sendmail only has the concept of a
723 * single sender, we arrange for errors to go to groupname-errors,
724 * which we then handle specially when (if) it comes back to us
725 * by expanding to all the rfc822ErrorsTo addresses. If it has no
726 * rfc822ErrorsTo attribute, we call do_group_members() to expand
730 if ( group_loop( dn ) ) {
735 * check for moderated groups - if the group has a moderator
736 * attribute, we check to see if the from address is one of
737 * the moderator values. if so, continue on. if not, arrange
738 * to send the mail to the moderator(s). need to do this before
739 * we change the from below.
742 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
743 /* check if it came from any of the group's moderators */
744 for ( i = 0; moderator[i] != NULL; i++ ) {
745 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
749 /* not from the moderator? */
750 if ( moderator[i] == NULL ) {
751 add_to( to, nto, moderator );
752 ldap_value_free( moderator );
756 /* else from the moderator - fall through and deliver it */
759 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
760 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
761 add_group( dn, togroups, ngroups );
766 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
784 int i, rc, anymembers;
786 char **mail, **member, **joinable, **suppress;
788 LDAPMessage *ee, *res;
789 struct timeval timeout;
793 * if all has gone according to plan, we've already arranged for
794 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
795 * so all we have to do here is arrange to send to the
796 * rfc822Mailbox attribute, the member attribute, and anyone who
797 * has joined the group by setting memberOfGroup equal to the
801 /* add members in the group itself - mail attribute */
803 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
805 add_to( to, nto, mail );
807 ldap_value_free( mail );
810 /* add members in the group itself - member attribute */
811 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
812 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
814 for ( i = 0; member[i] != NULL; i++ ) {
815 if ( strcasecmp( dn, member[i] ) == 0 ) {
816 syslog( LOG_ALERT, "group (%s) contains itself",
820 add_member( dn, member[i], to, nto, togroups,
821 ngroups, err, nerr, suppress );
825 ldap_value_free( suppress );
827 ldap_value_free( member );
830 /* add members who have joined by setting memberOfGroup */
831 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
832 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
833 if ( ! anymembers ) {
834 add_error( err, nerr, E_NOMEMBERS, dn,
838 ldap_value_free( joinable );
841 ldap_value_free( joinable );
843 sprintf( filter, "(memberOfGroup=%s)", dn );
845 timeout.tv_sec = MAIL500_TIMEOUT;
848 /* for each subtree to look in... */
849 opt = MAIL500_MAXAMBIGUOUS;
850 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
851 for ( i = 0; base[i].b_dn != NULL; i++ ) {
852 /* find entries that have joined this group... */
853 rc = ldap_search_st( ld, base[i].b_dn,
854 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
857 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
858 rc == LDAP_TIMELIMIT_EXCEEDED ) {
860 "group search limit exceeded %d", rc );
861 unbind_and_exit( EX_TEMPFAIL );
864 if ( rc != LDAP_SUCCESS ) {
865 syslog( LOG_ALERT, "group search return 0x%x",
867 unbind_and_exit( EX_TEMPFAIL );
870 /* for each entry that has joined... */
871 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
872 ee = ldap_next_entry( ld, ee ) ) {
874 if ( isgroup( ee ) ) {
875 ndn = ldap_get_dn( ld, ee );
877 if ( do_group( e, ndn, to, nto,
878 togroups, ngroups, err, nerr )
881 "group loop (%s) (%s)",
890 /* add them to the to list */
891 if ( (mail = ldap_get_values( ld, ee, "mail" ))
893 add_to( to, nto, mail );
895 ldap_value_free( mail );
897 /* else generate a bounce */
899 ndn = ldap_get_dn( ld, ee );
901 add_error( err, nerr,
902 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
910 opt = MAIL500_MAXAMBIGUOUS;
911 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
914 if ( ! anymembers ) {
915 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
935 LDAPMessage *res, *e;
936 struct timeval timeout;
938 timeout.tv_sec = MAIL500_TIMEOUT;
940 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
941 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
942 if ( rc == LDAP_NO_SUCH_OBJECT ) {
943 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
947 syslog( LOG_ALERT, "member search return 0x%x", rc );
949 unbind_and_exit( EX_TEMPFAIL );
953 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
954 syslog( LOG_ALERT, "member search error parsing entry" );
956 unbind_and_exit( EX_TEMPFAIL );
958 ndn = ldap_get_dn( ld, e );
960 /* allow groups within groups */
961 if ( isgroup( e ) ) {
962 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
964 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
972 /* send to the member's mail attribute */
973 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
974 add_to( to, nto, mail );
976 ldap_value_free( mail );
978 /* else generate a bounce */
980 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
982 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
1001 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
1002 "requestsTo" )) != NULL ) {
1003 add_to( to, nto, requeststo );
1005 ldap_value_free( requeststo );
1007 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
1023 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1024 "errorsTo" )) != NULL ) {
1025 add_to( to, nto, errorsto );
1027 ldap_value_free( errorsto );
1029 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1045 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1046 add_to( to, nto, owner );
1047 ldap_value_free( owner );
1049 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1054 send_message( char **to )
1057 #ifndef HAVE_WAITPID
1058 WAITSTATUSTYPE status;
1065 strcpy( buf, to[0] );
1066 for ( i = 1; to[i] != NULL; i++ ) {
1068 strcat( buf, to[i] );
1071 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1075 if ( (pid = fork()) != 0 ) {
1077 waitpid( pid, (int *) NULL, 0 );
1079 wait4( pid, &status, WAIT_FLAGS, 0 );
1083 /* to includes sendmailargs */
1084 execv( MAIL500_SENDMAIL, to );
1086 syslog( LOG_ALERT, "execv failed" );
1088 exit( EX_TEMPFAIL );
1093 send_group( Group *group, int ngroup )
1099 #ifndef HAVE_WAITPID
1100 WAITSTATUSTYPE status;
1103 for ( i = 0; i < ngroup; i++ ) {
1104 (void) rewind( stdin );
1106 iargv[0] = MAIL500_SENDMAIL;
1108 iargv[2] = group[i].g_errorsto;
1109 iargv[3] = "-oMrX.500";
1116 add_to( &argv, &argc, iargv );
1117 add_to( &argv, &argc, group[i].g_members );
1123 strcpy( buf, argv[0] );
1124 for ( i = 1; i < argc; i++ ) {
1126 strcat( buf, argv[i] );
1129 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1133 if ( (pid = fork()) != 0 ) {
1135 waitpid( pid, (int *) NULL, 0 );
1137 wait4( pid, &status, WAIT_FLAGS, 0 );
1141 execv( MAIL500_SENDMAIL, argv );
1143 syslog( LOG_ALERT, "execv failed" );
1145 exit( EX_TEMPFAIL );
1151 send_errors( Error *err, int 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()) != 0 ) {
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 );
1298 do_noemail( FILE *fp, Error *err, int namelen )
1304 fprintf(fp, "%s: User has no email address registered.\n",
1306 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1307 namelen, " ", err->e_addr );
1310 dn = ldap_get_dn( ld, err->e_msg );
1311 ufn = ldap_explode_dn( dn, 1 );
1312 rdn = strdup( ufn[0] );
1313 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1314 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1316 for ( i = 0; vals[i]; i++ ) {
1317 last = strlen( vals[i] ) - 1;
1318 if ( isdigit( vals[i][last] ) ) {
1319 rdn = strdup( vals[i] );
1324 ldap_value_free( vals );
1327 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1330 ldap_value_free( ufn );
1332 /* titles or descriptions */
1333 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1334 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1336 fprintf( fp, "%*s No title or description registered\n",
1339 for ( i = 0; vals[i] != NULL; i++ ) {
1340 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1343 ldap_value_free( vals );
1346 /* postal address */
1347 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1349 fprintf( fp, "%*s No postal address registered\n", namelen,
1352 fprintf( fp, "%*s ", namelen, " " );
1353 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1354 if ( vals[0][i] == '$' ) {
1355 fprintf( fp, "\n%*s ", namelen, " " );
1356 while ( isspace( vals[0][i+1] ) )
1359 fprintf( fp, "%c", vals[0][i] );
1362 fprintf( fp, "\n" );
1364 ldap_value_free( vals );
1367 /* telephone number */
1368 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1370 fprintf( fp, "%*s No phone number registered\n", namelen,
1373 for ( i = 0; vals[i] != NULL; i++ ) {
1374 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1377 ldap_value_free( vals );
1383 do_ambiguous( FILE *fp, Error *err, int namelen )
1390 i = ldap_result2error( ld, err->e_msg, 0 );
1392 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1393 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1394 ldap_count_entries( ld, err->e_msg ) );
1396 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1397 e = ldap_next_entry( ld, e ) ) {
1398 dn = ldap_get_dn( ld, e );
1399 ufn = ldap_explode_dn( dn, 1 );
1400 rdn = strdup( ufn[0] );
1401 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1402 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1403 for ( i = 0; vals[i]; i++ ) {
1404 last = strlen( vals[i] ) - 1;
1405 if ( isdigit( vals[i][last] ) ) {
1406 rdn = strdup( vals[i] );
1411 ldap_value_free( vals );
1415 if ( isgroup( e ) ) {
1416 vals = ldap_get_values( ld, e, "description" );
1418 vals = ldap_get_values( ld, e, "title" );
1421 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1422 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1423 fprintf( fp, " %s\n", vals[i] );
1428 ldap_value_free( ufn );
1430 ldap_value_free( vals );
1435 count_values( char **list )
1439 for ( i = 0; list && list[i] != NULL; i++ )
1446 add_to( char ***list, int *nlist, char **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;
1468 isgroup( LDAPMessage *e )
1473 oclist = ldap_get_values( ld, e, "objectClass" );
1475 for ( i = 0; oclist[i] != NULL; i++ ) {
1476 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1477 ldap_value_free( oclist );
1481 ldap_value_free( oclist );
1487 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1490 *err = (Error *) malloc( sizeof(Error) );
1492 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1495 (*err)[*nerr].e_code = code;
1496 (*err)[*nerr].e_addr = strdup( addr );
1497 (*err)[*nerr].e_msg = msg;
1502 add_group( char *dn, Group **list, int *nlist )
1507 for ( i = 0; i < *nlist; i++ ) {
1508 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1509 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1514 ufn = ldap_explode_dn( dn, 1 );
1515 namelen = strlen( ufn[0] );
1517 if ( *nlist == 0 ) {
1518 *list = (Group *) malloc( sizeof(Group) );
1520 *list = (Group *) realloc( *list, (*nlist + 1) *
1524 /* send errors to groupname-errors@host */
1525 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1527 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1528 (void) canonical( (*list)[*nlist].g_errorsto );
1530 /* send to groupname-members@host - make it a list for send_group */
1531 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1532 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1533 sizeof(MEMBERS) + hostlen + 2 );
1534 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1536 (void) canonical( (*list)[*nlist].g_members[0] );
1537 (*list)[*nlist].g_members[1] = NULL;
1539 /* save the group's dn so we can check for loops above */
1540 (*list)[*nlist].g_dn = strdup( dn );
1544 ldap_value_free( ufn );
1548 unbind_and_exit( int rc )
1552 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1553 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1559 canonical( char *s )
1563 for ( ; *s != '\0'; s++ ) {
1572 group_loop( char *dn )
1575 static char **groups;
1578 for ( i = 0; i < ngroups; i++ ) {
1579 if ( strcmp( dn, groups[i] ) == 0 )
1584 groups = (char **) malloc( sizeof(char *) );
1586 groups = (char **) realloc( groups,
1587 (ngroups + 1) * sizeof(char *) );
1589 groups[ngroups++] = strdup( dn );
1595 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1599 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1600 ldap_value_free( attr );
1604 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1605 ldap_value_free( attr );
1613 get_attributes_mail_dn(
1616 char *attr2 /* this one is dn-valued */
1619 LDAPMessage *ee, *res;
1620 char **vals, **dnlist, **mail, **grname, **graddr;
1623 struct timeval timeout;
1625 dn = ldap_get_dn( ld, e );
1627 vals = ldap_get_values( ld, e, attr1 );
1628 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1631 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1632 timeout.tv_sec = MAIL500_TIMEOUT;
1633 timeout.tv_usec = 0;
1635 for ( i = 0; dnlist[i] != NULL; i++ ) {
1636 if ( (rc = ldap_search_st( ld, dnlist[i],
1637 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1638 &timeout, &res )) != LDAP_SUCCESS ) {
1639 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1640 unbind_and_exit( EX_TEMPFAIL );
1643 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1649 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1650 syslog( LOG_ALERT, "error parsing x500 entry" );
1654 if ( isgroup(ee) ) {
1657 grname = ldap_explode_dn( dnlist[i], 1 );
1659 /* groupname + host + @ + null */
1660 graddr[0] = (char *) malloc( strlen( grname[0] )
1661 + strlen( host ) + 2 );
1663 sprintf( graddr[0], "%s@%s", grname[0], host);
1664 (void) canonical( graddr[0] );
1666 add_to( &vals, &nto, graddr );
1669 ldap_value_free( grname );
1670 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1672 add_to( &vals, &nto, mail );
1674 ldap_value_free( mail );
1677 ldap_msgfree( res );