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>
24 extern char *strdup (const char *);
25 extern int optind, errno;
28 #ifdef HAVE_SYS_PARAM_H
29 #include <sys/param.h>
32 #include <sys/resource.h>
39 #include "ldapconfig.h"
41 #ifndef MAIL500_BOUNCEFROM
42 #define MAIL500_BOUNCEFROM "<>"
46 #define GROUP_ERRORS 0x02
47 #define GROUP_REQUEST 0x04
48 #define GROUP_MEMBERS 0x08
49 #define GROUP_OWNER 0x10
52 #define ERRORS "errors"
53 #define REQUEST "request"
54 #define REQUESTS "requests"
55 #define MEMBERS "members"
57 #define OWNERS "owners"
60 char *vacationhost = NULL;
61 char *errorsfrom = NULL;
62 char *mailfrom = NULL;
64 char *ldaphost = NULL;
70 #define E_USERUNKNOWN 1
76 #define E_JOINMEMBERNOEMAIL 7
77 #define E_MEMBERNOEMAIL 8
79 #define E_NOMEMBERS 10
81 #define E_GROUPUNKNOWN 12
87 #define e_msg e_union.e_u_msg
88 #define e_loop e_union.e_u_loop
91 typedef struct groupto {
97 typedef struct baseinfo {
98 char *b_dn; /* dn to start searching at */
99 char b_rdnpref; /* give rdn's preference when searching? */
100 int b_search; /* ORed with the type of thing the address */
101 /* looks like (USER, GROUP_ERRORS, etc.) */
102 /* to see if this should be searched */
103 char *b_filter[3]; /* filter to apply - name substituted for %s */
104 /* (up to three of them) */
108 { "ou=People, o=University of Michigan, c=US",
110 "uid=%s", "cn=%s", NULL,
111 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
113 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
114 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
116 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
120 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
122 static char *attrs[] = { "objectClass", "title", "postaladdress",
123 "telephoneNumber", "mail", "description", "owner",
124 "errorsTo", "rfc822ErrorsTo", "requestsTo",
125 "rfc822RequestsTo", "joinable", "cn", "member",
126 "moderator", "onVacation", "uid",
127 "suppressNoEmailError", NULL };
129 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
130 static int do_group( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
131 static void do_group_members( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
132 static void send_message( char **to );
133 static void send_errors( Error *err, int nerr );
134 static void do_noemail( FILE *fp, Error *err, int namelen );
135 static void do_ambiguous( FILE *fp, Error *err, int namelen );
136 static void add_to( char ***list, int *nlist, char **new );
137 static int isgroup( LDAPMessage *e );
138 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
139 static void add_group( char *dn, Group **list, int *nlist );
140 static void unbind_and_exit( int rc );
141 static int group_loop( char *dn );
142 static void send_group( Group *group, int ngroup );
143 static int has_attributes( LDAPMessage *e, char *attr1, char *attr2 );
144 static char **get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 );
145 static char *canonical( char *s );
146 static int connect_to_x500( void );
148 void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
149 void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
150 void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
151 void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
154 main ( int argc, char **argv )
160 int numto, ngroups, numerr, nargs;
163 if ( (myname = strrchr( argv[0], '/' )) == NULL )
164 myname = strdup( argv[0] );
166 myname = strdup( myname + 1 );
169 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
171 openlog( myname, OPENLOG_OPTIONS );
174 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
176 case 'd': /* turn on debugging */
177 debug = atoi( optarg );
180 case 'f': /* who it's from & where errors should go */
181 mailfrom = strdup( optarg );
182 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
183 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
184 sendmailargs[j+1] = mailfrom;
190 case 'h': /* hostname */
191 host = strdup( optarg );
192 hostlen = strlen(host);
195 case 'l': /* ldap host */
196 ldaphost = strdup( optarg );
199 /* mailer-daemon address - who we should */
200 case 'm': /* say errors come from */
201 errorsfrom = strdup( optarg );
204 case 'v': /* vacation host */
205 vacationhost = strdup( optarg );
209 syslog( LOG_ALERT, "unknown option" );
214 if ( mailfrom == NULL ) {
215 syslog( LOG_ALERT, "required argument -f not present" );
218 if ( errorsfrom == NULL ) {
219 syslog( LOG_ALERT, "required argument -m not present" );
222 if ( host == NULL ) {
223 syslog( LOG_ALERT, "required argument -h not present" );
227 if ( connect_to_x500() != 0 )
236 syslog( LOG_ALERT, "running as %d", geteuid() );
237 strcpy( buf, argv[0] );
238 for ( i = 1; i < argc; i++ ) {
240 strcat( buf, argv[i] );
243 syslog( LOG_ALERT, "args: (%s)", buf );
248 add_to( &tolist, &numto, sendmailargs );
250 ngroups = numerr = 0;
253 for ( i = optind; i < argc; i++ ) {
257 for ( j = 0; argv[i][j] != '\0'; j++ ) {
258 if ( argv[i][j] == '.' || argv[i][j] == '_' )
263 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
266 if ((strcasecmp(s, ERROR) == 0) ||
267 (strcasecmp(s, ERRORS) == 0)) {
270 } else if ((strcasecmp(s, REQUEST) == 0) ||
271 (strcasecmp(s, REQUESTS) == 0)) {
272 type = GROUP_REQUEST;
274 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
275 type = GROUP_MEMBERS;
277 } else if ((strcasecmp(s, OWNER) == 0) ||
278 (strcasecmp(s, OWNERS) == 0)) {
284 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
285 &errlist, &numerr, type );
289 * If we have both errors and successful deliveries to make or if
290 * if there are any groups to deliver to, we basically need to read
291 * the message twice. So, we have to put it in a tmp file.
294 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
299 if ( (fp = tmpfile()) == NULL ) {
300 syslog( LOG_ALERT, "could not open tmp file" );
301 unbind_and_exit( EX_TEMPFAIL );
304 /* copy the message to a temp file */
305 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
306 if ( fputs( buf, fp ) == EOF ) {
307 syslog( LOG_ALERT, "error writing tmpfile" );
308 unbind_and_exit( EX_TEMPFAIL );
312 if ( dup2( fileno( fp ), 0 ) == -1 ) {
313 syslog( LOG_ALERT, "could not dup2 tmpfile" );
314 unbind_and_exit( EX_TEMPFAIL );
320 /* deal with errors */
323 syslog( LOG_ALERT, "sending errors" );
325 (void) rewind( stdin );
326 send_errors( errlist, numerr );
329 (void) ldap_unbind( ld );
331 /* send to groups with errorsTo */
334 syslog( LOG_ALERT, "sending to groups with errorsto" );
336 (void) rewind( stdin );
337 send_group( togroups, ngroups );
340 /* send to expanded aliases and groups w/o errorsTo */
341 if ( numto > nargs ) {
343 syslog( LOG_ALERT, "sending to aliases and groups" );
345 (void) rewind( stdin );
346 send_message( tolist );
353 connect_to_x500( void )
357 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
358 syslog( LOG_ALERT, "ldap_open failed" );
362 opt = MAIL500_MAXAMBIGUOUS;
363 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
364 opt = LDAP_DEREF_ALWAYS;
365 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
367 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
368 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
376 mailcmp( char *a, char *b )
380 for ( i = 0; a[i] != '\0'; i++ ) {
381 if ( a[i] != b[i] ) {
386 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
412 LDAPMessage *e, *res;
413 struct timeval timeout;
416 char realfilter[1024];
417 char **mail, **onvacation = NULL, **uid = NULL;
420 * Look up the name in X.500, add the appropriate addresses found
421 * to the to list, or to the err list in case of error. Groups are
422 * handled by the do_group routine, individuals are handled here.
423 * When looking up name, we follow the bases hierarchy, looking
424 * in base[0] first, then base[1], etc. For each base, there is
425 * a set of search filters to try, in order. If something goes
426 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
427 * If the b_rdnpref flag is set, then we give preference to entries
428 * that matched name because it's their rdn, otherwise not.
431 timeout.tv_sec = MAIL500_TIMEOUT;
433 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
434 if ( ! (base[b].b_search & type) ) {
437 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
438 char *format, *p, *s, *d;
442 for ( argc = 0; argc < 3; argc++ ) {
446 format = strdup( base[b].b_filter[f] );
447 for ( argc = 0, p = format; *p; p++ ) {
450 case 's': /* %s is the name */
454 case 'h': /* %h is the host */
461 "unknown format %c", *p );
469 /* three names ought to do... */
470 sprintf( filter, format, argv[0], argv[1], argv[2] );
472 for ( s = filter, d = realfilter; *s; s++, d++ ) {
481 rc = ldap_search_st( ld, base[b].b_dn,
482 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
485 /* some other trouble - try again later */
486 if ( rc != LDAP_SUCCESS &&
487 rc != LDAP_SIZELIMIT_EXCEEDED ) {
488 syslog( LOG_ALERT, "return 0x%x from X.500",
490 unbind_and_exit( EX_TEMPFAIL );
493 if ( (match = ldap_count_entries( ld, res )) != 0 )
503 /* trouble - try again later */
505 syslog( LOG_ALERT, "error parsing result from X.500" );
506 unbind_and_exit( EX_TEMPFAIL );
509 /* no matches - bounce with user unknown */
511 if ( type == USER ) {
512 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
514 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
519 /* more than one match - bounce with ambiguous user? */
521 LDAPMessage *next, *tmpres = NULL;
525 /* not giving rdn preference - bounce with ambiguous user */
526 if ( base[b].b_rdnpref == 0 ) {
527 add_error( err, nerr, E_AMBIGUOUS, name, res );
532 * giving rdn preference - see if any entries were matched
533 * because of their rdn. If so, collect them to deal with
534 * later (== 1 we deliver, > 1 we bounce).
537 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
538 next = ldap_next_entry( ld, e );
539 dn = ldap_get_dn( ld, e );
540 xdn = ldap_explode_dn( dn, 1 );
542 /* XXX bad, but how else can we do it? XXX */
543 if ( strcasecmp( xdn[0], name ) == 0 ) {
544 ldap_delete_result_entry( &res, e );
545 ldap_add_result_entry( &tmpres, e );
548 ldap_value_free( xdn );
552 /* nothing matched by rdn - go ahead and bounce */
553 if ( tmpres == NULL ) {
554 add_error( err, nerr, E_AMBIGUOUS, name, res );
557 /* more than one matched by rdn - bounce with rdn matches */
558 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
559 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
563 } else if ( match < 0 ) {
564 syslog( LOG_ALERT, "error parsing result from X.500" );
565 unbind_and_exit( EX_TEMPFAIL );
568 /* otherwise one matched by rdn - send to it */
574 * if we get this far, it means that we found a single match for
575 * name. for a user, we deliver to the mail attribute or bounce
576 * with address and phone if no mail attr. for a group, we
577 * deliver to all members or bounce to rfc822ErrorsTo if no members.
581 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
582 syslog( LOG_ALERT, "error parsing entry from X.500" );
583 unbind_and_exit( EX_TEMPFAIL );
586 dn = ldap_get_dn( ld, e );
588 if ( type == GROUP_ERRORS ) {
589 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
590 do_group_errors( e, dn, to, nto, err, nerr );
592 } else if ( type == GROUP_REQUEST ) {
593 /* sent to group-request - resend to [rfc822]RequestsTo attr */
594 do_group_request( e, dn, to, nto, err, nerr );
596 } else if ( type == GROUP_MEMBERS ) {
597 /* sent to group-members - expand */
598 do_group_members( e, dn, to, nto, togroups, ngroups, err,
601 } else if ( type == GROUP_OWNER ) {
602 /* sent to group-owner - resend to owner attr */
603 do_group_owner( e, dn, to, nto, err, nerr );
605 } else if ( isgroup( e ) ) {
607 * sent to group - resend from [rfc822]ErrorsTo if it's there,
608 * otherwise, expand the group
611 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
617 * sent to user - mail attribute => add it to the to list,
620 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
625 /* try to detect simple mail loops */
626 sprintf( buf, "%s@%s", name, host );
627 for ( i = 0; mail[i] != NULL; i++ ) {
629 * address is the same as the one we're
630 * sending to - mail loop. syslog the
631 * problem, bounce a message back to the
632 * sender (who else?), and delete the bogus
633 * addr from the list.
636 if ( (h = strchr( mail[i], '@' )) != NULL ) {
638 if ( strcasecmp( h, host ) == 0 ) {
640 "potential loop detected (%s)",
645 if ( mailcmp( buf, mail[i] ) == 0 ) {
647 "loop detected (%s)", mail[i] );
649 /* remove the bogus address */
650 for ( j = i; mail[j] != NULL; j++ ) {
655 if ( mail[0] != NULL ) {
656 add_to( to, nto, mail );
658 add_error( err, nerr, E_NOEMAIL, name, res );
661 ldap_value_free( mail );
663 add_error( err, nerr, E_NOEMAIL, name, res );
667 * If the user is on vacation, send a copy of the mail to
668 * the vacation server. The address is constructed from
669 * the vacationhost (set in a command line argument) and
670 * the uid (XXX this should be more general XXX).
673 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
674 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
679 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
680 sprintf( buf, "%s@%s", uid[0], vacationhost );
685 add_to( to, nto, vaddr );
688 "user without a uid on vacation (%s)",
694 if ( onvacation != NULL ) {
695 ldap_value_free( onvacation );
698 ldap_value_free( uid );
719 * If this group has an rfc822ErrorsTo attribute, we need to
720 * arrange for errors involving this group to go there, not
721 * to the sender. Since sendmail only has the concept of a
722 * single sender, we arrange for errors to go to groupname-errors,
723 * which we then handle specially when (if) it comes back to us
724 * by expanding to all the rfc822ErrorsTo addresses. If it has no
725 * rfc822ErrorsTo attribute, we call do_group_members() to expand
729 if ( group_loop( dn ) ) {
734 * check for moderated groups - if the group has a moderator
735 * attribute, we check to see if the from address is one of
736 * the moderator values. if so, continue on. if not, arrange
737 * to send the mail to the moderator(s). need to do this before
738 * we change the from below.
741 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
742 /* check if it came from any of the group's moderators */
743 for ( i = 0; moderator[i] != NULL; i++ ) {
744 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
748 /* not from the moderator? */
749 if ( moderator[i] == NULL ) {
750 add_to( to, nto, moderator );
751 ldap_value_free( moderator );
755 /* else from the moderator - fall through and deliver it */
758 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
759 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
760 add_group( dn, togroups, ngroups );
765 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
783 int i, rc, anymembers;
785 char **mail, **member, **joinable, **suppress;
787 LDAPMessage *ee, *res;
788 struct timeval timeout;
792 * if all has gone according to plan, we've already arranged for
793 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
794 * so all we have to do here is arrange to send to the
795 * rfc822Mailbox attribute, the member attribute, and anyone who
796 * has joined the group by setting memberOfGroup equal to the
800 /* add members in the group itself - mail attribute */
802 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
804 add_to( to, nto, mail );
806 ldap_value_free( mail );
809 /* add members in the group itself - member attribute */
810 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
811 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
813 for ( i = 0; member[i] != NULL; i++ ) {
814 if ( strcasecmp( dn, member[i] ) == 0 ) {
815 syslog( LOG_ALERT, "group (%s) contains itself",
819 add_member( dn, member[i], to, nto, togroups,
820 ngroups, err, nerr, suppress );
824 ldap_value_free( suppress );
826 ldap_value_free( member );
829 /* add members who have joined by setting memberOfGroup */
830 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
831 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
832 if ( ! anymembers ) {
833 add_error( err, nerr, E_NOMEMBERS, dn,
837 ldap_value_free( joinable );
840 ldap_value_free( joinable );
842 sprintf( filter, "(memberOfGroup=%s)", dn );
844 timeout.tv_sec = MAIL500_TIMEOUT;
847 /* for each subtree to look in... */
848 opt = MAIL500_MAXAMBIGUOUS;
849 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
850 for ( i = 0; base[i].b_dn != NULL; i++ ) {
851 /* find entries that have joined this group... */
852 rc = ldap_search_st( ld, base[i].b_dn,
853 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
856 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
857 rc == LDAP_TIMELIMIT_EXCEEDED ) {
859 "group search limit exceeded %d", rc );
860 unbind_and_exit( EX_TEMPFAIL );
863 if ( rc != LDAP_SUCCESS ) {
864 syslog( LOG_ALERT, "group search return 0x%x",
866 unbind_and_exit( EX_TEMPFAIL );
869 /* for each entry that has joined... */
870 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
871 ee = ldap_next_entry( ld, ee ) ) {
873 if ( isgroup( ee ) ) {
874 ndn = ldap_get_dn( ld, ee );
876 if ( do_group( e, ndn, to, nto,
877 togroups, ngroups, err, nerr )
880 "group loop (%s) (%s)",
889 /* add them to the to list */
890 if ( (mail = ldap_get_values( ld, ee, "mail" ))
892 add_to( to, nto, mail );
894 ldap_value_free( mail );
896 /* else generate a bounce */
898 ndn = ldap_get_dn( ld, ee );
900 add_error( err, nerr,
901 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
909 opt = MAIL500_MAXAMBIGUOUS;
910 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
913 if ( ! anymembers ) {
914 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
934 LDAPMessage *res, *e;
935 struct timeval timeout;
937 timeout.tv_sec = MAIL500_TIMEOUT;
939 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
940 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
941 if ( rc == LDAP_NO_SUCH_OBJECT ) {
942 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
946 syslog( LOG_ALERT, "member search return 0x%x", rc );
948 unbind_and_exit( EX_TEMPFAIL );
952 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
953 syslog( LOG_ALERT, "member search error parsing entry" );
955 unbind_and_exit( EX_TEMPFAIL );
957 ndn = ldap_get_dn( ld, e );
959 /* allow groups within groups */
960 if ( isgroup( e ) ) {
961 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
963 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
971 /* send to the member's mail attribute */
972 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
973 add_to( to, nto, mail );
975 ldap_value_free( mail );
977 /* else generate a bounce */
979 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
981 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
1000 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
1001 "requestsTo" )) != NULL ) {
1002 add_to( to, nto, requeststo );
1004 ldap_value_free( requeststo );
1006 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
1022 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1023 "errorsTo" )) != NULL ) {
1024 add_to( to, nto, errorsto );
1026 ldap_value_free( errorsto );
1028 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1044 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1045 add_to( to, nto, owner );
1046 ldap_value_free( owner );
1048 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1053 send_message( char **to )
1056 #ifndef HAVE_WAITPID
1057 WAITSTATUSTYPE status;
1064 strcpy( buf, to[0] );
1065 for ( i = 1; to[i] != NULL; i++ ) {
1067 strcat( buf, to[i] );
1070 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1074 if ( (pid = fork()) != 0 ) {
1076 waitpid( pid, (int *) NULL, 0 );
1078 wait4( pid, &status, WAIT_FLAGS, 0 );
1082 /* to includes sendmailargs */
1083 execv( MAIL500_SENDMAIL, to );
1085 syslog( LOG_ALERT, "execv failed" );
1087 exit( EX_TEMPFAIL );
1092 send_group( Group *group, int ngroup )
1098 #ifndef HAVE_WAITPID
1099 WAITSTATUSTYPE status;
1102 for ( i = 0; i < ngroup; i++ ) {
1103 (void) rewind( stdin );
1105 iargv[0] = MAIL500_SENDMAIL;
1107 iargv[2] = group[i].g_errorsto;
1108 iargv[3] = "-oMrX.500";
1115 add_to( &argv, &argc, iargv );
1116 add_to( &argv, &argc, group[i].g_members );
1122 strcpy( buf, argv[0] );
1123 for ( i = 1; i < argc; i++ ) {
1125 strcat( buf, argv[i] );
1128 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1132 if ( (pid = fork()) != 0 ) {
1134 waitpid( pid, (int *) NULL, 0 );
1136 wait4( pid, &status, WAIT_FLAGS, 0 );
1140 execv( MAIL500_SENDMAIL, argv );
1142 syslog( LOG_ALERT, "execv failed" );
1144 exit( EX_TEMPFAIL );
1150 send_errors( Error *err, int nerr )
1152 int pid, i, namelen;
1157 #ifndef HAVE_WAITPID
1158 WAITSTATUSTYPE status;
1161 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1162 mailfrom = errorsfrom;
1165 argv[0] = MAIL500_SENDMAIL;
1166 argv[1] = "-oMrX.500";
1170 argv[5] = MAIL500_BOUNCEFROM;
1177 strcpy( buf, argv[0] );
1178 for ( i = 1; argv[i] != NULL; i++ ) {
1180 strcat( buf, argv[i] );
1183 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1186 if ( pipe( fd ) == -1 ) {
1187 syslog( LOG_ALERT, "cannot create pipe" );
1188 exit( EX_TEMPFAIL );
1191 if ( (pid = fork()) != 0 ) {
1192 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1193 syslog( LOG_ALERT, "cannot fdopen pipe" );
1194 exit( EX_TEMPFAIL );
1197 fprintf( fp, "To: %s\n", mailfrom );
1198 fprintf( fp, "From: %s\n", errorsfrom );
1199 fprintf( fp, "Subject: undeliverable mail\n" );
1200 fprintf( fp, "\n" );
1201 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1202 for ( i = 0; i < nerr; i++ ) {
1203 namelen = strlen( err[i].e_addr );
1204 fprintf( fp, "\n" );
1206 switch ( err[i].e_code ) {
1208 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1211 case E_GROUPUNKNOWN:
1212 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1216 fprintf( fp, "%s: Group member does not exist\n",
1218 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1219 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1220 fprintf( fp, "then re-adding the person to the group.\n" );
1224 fprintf( fp, "%s: Group exists but has no request address\n",
1229 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1234 fprintf( fp, "%s: Group exists but has no owner\n",
1239 do_ambiguous( fp, &err[i], namelen );
1243 do_noemail( fp, &err[i], namelen );
1246 case E_MEMBERNOEMAIL:
1247 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1251 case E_JOINMEMBERNOEMAIL:
1252 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1257 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1258 err[i].e_addr, err[i].e_loop );
1262 fprintf( fp, "%s: Group has no members\n",
1267 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1268 unbind_and_exit( EX_TEMPFAIL );
1273 fprintf( fp, "\n------- The original message sent:\n\n" );
1275 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1281 waitpid( pid, (int *) NULL, 0 );
1283 wait4( pid, &status, WAIT_FLAGS, 0 );
1288 execv( MAIL500_SENDMAIL, argv );
1290 syslog( LOG_ALERT, "execv failed" );
1292 exit( EX_TEMPFAIL );
1297 do_noemail( FILE *fp, Error *err, int namelen )
1303 fprintf(fp, "%s: User has no email address registered.\n",
1305 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1306 namelen, " ", err->e_addr );
1309 dn = ldap_get_dn( ld, err->e_msg );
1310 ufn = ldap_explode_dn( dn, 1 );
1311 rdn = strdup( ufn[0] );
1312 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1313 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1315 for ( i = 0; vals[i]; i++ ) {
1316 last = strlen( vals[i] ) - 1;
1317 if ( isdigit( vals[i][last] ) ) {
1318 rdn = strdup( vals[i] );
1323 ldap_value_free( vals );
1326 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1329 ldap_value_free( ufn );
1331 /* titles or descriptions */
1332 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1333 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1335 fprintf( fp, "%*s No title or description registered\n",
1338 for ( i = 0; vals[i] != NULL; i++ ) {
1339 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1342 ldap_value_free( vals );
1345 /* postal address */
1346 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1348 fprintf( fp, "%*s No postal address registered\n", namelen,
1351 fprintf( fp, "%*s ", namelen, " " );
1352 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1353 if ( vals[0][i] == '$' ) {
1354 fprintf( fp, "\n%*s ", namelen, " " );
1355 while ( isspace( vals[0][i+1] ) )
1358 fprintf( fp, "%c", vals[0][i] );
1361 fprintf( fp, "\n" );
1363 ldap_value_free( vals );
1366 /* telephone number */
1367 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1369 fprintf( fp, "%*s No phone number registered\n", namelen,
1372 for ( i = 0; vals[i] != NULL; i++ ) {
1373 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1376 ldap_value_free( vals );
1382 do_ambiguous( FILE *fp, Error *err, int 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( char **list )
1438 for ( i = 0; list && list[i] != NULL; i++ )
1445 add_to( char ***list, int *nlist, char **new )
1447 int i, nnew, oldnlist;
1449 nnew = count_values( new );
1452 if ( *list == NULL || *nlist == 0 ) {
1453 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1456 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1457 nnew * sizeof(char *) + sizeof(char *) );
1461 for ( i = 0; i < nnew; i++ )
1462 (*list)[i + oldnlist] = strdup( new[i] );
1463 (*list)[*nlist] = NULL;
1467 isgroup( LDAPMessage *e )
1472 oclist = ldap_get_values( ld, e, "objectClass" );
1474 for ( i = 0; oclist[i] != NULL; i++ ) {
1475 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1476 ldap_value_free( oclist );
1480 ldap_value_free( oclist );
1486 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1489 *err = (Error *) malloc( sizeof(Error) );
1491 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1494 (*err)[*nerr].e_code = code;
1495 (*err)[*nerr].e_addr = strdup( addr );
1496 (*err)[*nerr].e_msg = msg;
1501 add_group( char *dn, Group **list, int *nlist )
1506 for ( i = 0; i < *nlist; i++ ) {
1507 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1508 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1513 ufn = ldap_explode_dn( dn, 1 );
1514 namelen = strlen( ufn[0] );
1516 if ( *nlist == 0 ) {
1517 *list = (Group *) malloc( sizeof(Group) );
1519 *list = (Group *) realloc( *list, (*nlist + 1) *
1523 /* send errors to groupname-errors@host */
1524 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1526 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1527 (void) canonical( (*list)[*nlist].g_errorsto );
1529 /* send to groupname-members@host - make it a list for send_group */
1530 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1531 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1532 sizeof(MEMBERS) + hostlen + 2 );
1533 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1535 (void) canonical( (*list)[*nlist].g_members[0] );
1536 (*list)[*nlist].g_members[1] = NULL;
1538 /* save the group's dn so we can check for loops above */
1539 (*list)[*nlist].g_dn = strdup( dn );
1543 ldap_value_free( ufn );
1547 unbind_and_exit( int rc )
1551 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1552 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1558 canonical( char *s )
1562 for ( ; *s != '\0'; s++ ) {
1571 group_loop( char *dn )
1574 static char **groups;
1577 for ( i = 0; i < ngroups; i++ ) {
1578 if ( strcmp( dn, groups[i] ) == 0 )
1583 groups = (char **) malloc( sizeof(char *) );
1585 groups = (char **) realloc( groups,
1586 (ngroups + 1) * sizeof(char *) );
1588 groups[ngroups++] = strdup( dn );
1594 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1598 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1599 ldap_value_free( attr );
1603 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1604 ldap_value_free( attr );
1612 get_attributes_mail_dn(
1615 char *attr2 /* this one is dn-valued */
1618 LDAPMessage *ee, *res;
1619 char **vals, **dnlist, **mail, **grname, **graddr;
1622 struct timeval timeout;
1624 dn = ldap_get_dn( ld, e );
1626 vals = ldap_get_values( ld, e, attr1 );
1627 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1630 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1631 timeout.tv_sec = MAIL500_TIMEOUT;
1632 timeout.tv_usec = 0;
1634 for ( i = 0; dnlist[i] != NULL; i++ ) {
1635 if ( (rc = ldap_search_st( ld, dnlist[i],
1636 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1637 &timeout, &res )) != LDAP_SUCCESS ) {
1638 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1639 unbind_and_exit( EX_TEMPFAIL );
1642 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1648 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1649 syslog( LOG_ALERT, "error parsing x500 entry" );
1653 if ( isgroup(ee) ) {
1656 grname = ldap_explode_dn( dnlist[i], 1 );
1658 /* groupname + host + @ + null */
1659 graddr[0] = (char *) malloc( strlen( grname[0] )
1660 + strlen( host ) + 2 );
1662 sprintf( graddr[0], "%s@%s", grname[0], host);
1663 (void) canonical( graddr[0] );
1665 add_to( &vals, &nto, graddr );
1668 ldap_value_free( grname );
1669 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1671 add_to( &vals, &nto, mail );
1673 ldap_value_free( mail );
1676 ldap_msgfree( res );