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>
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
38 #include "ldapconfig.h"
40 #ifndef MAIL500_BOUNCEFROM
41 #define MAIL500_BOUNCEFROM "<>"
45 #define GROUP_ERRORS 0x02
46 #define GROUP_REQUEST 0x04
47 #define GROUP_MEMBERS 0x08
48 #define GROUP_OWNER 0x10
51 #define ERRORS "errors"
52 #define REQUEST "request"
53 #define REQUESTS "requests"
54 #define MEMBERS "members"
56 #define OWNERS "owners"
59 char *vacationhost = NULL;
60 char *errorsfrom = NULL;
61 char *mailfrom = NULL;
63 char *ldaphost = NULL;
69 #define E_USERUNKNOWN 1
75 #define E_JOINMEMBERNOEMAIL 7
76 #define E_MEMBERNOEMAIL 8
78 #define E_NOMEMBERS 10
80 #define E_GROUPUNKNOWN 12
86 #define e_msg e_union.e_u_msg
87 #define e_loop e_union.e_u_loop
90 typedef struct groupto {
96 typedef struct baseinfo {
97 char *b_dn; /* dn to start searching at */
98 char b_rdnpref; /* give rdn's preference when searching? */
99 int b_search; /* ORed with the type of thing the address */
100 /* looks like (USER, GROUP_ERRORS, etc.) */
101 /* to see if this should be searched */
102 char *b_filter[3]; /* filter to apply - name substituted for %s */
103 /* (up to three of them) */
107 { "ou=People, o=University of Michigan, c=US",
109 "uid=%s", "cn=%s", NULL,
110 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
112 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
113 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
115 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
119 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
121 static char *attrs[] = { "objectClass", "title", "postaladdress",
122 "telephoneNumber", "mail", "description", "owner",
123 "errorsTo", "rfc822ErrorsTo", "requestsTo",
124 "rfc822RequestsTo", "joinable", "cn", "member",
125 "moderator", "onVacation", "uid",
126 "suppressNoEmailError", NULL };
128 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
129 static int do_group( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
130 static void do_group_members( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
131 static void send_message( char **to );
132 static void send_errors( Error *err, int nerr );
133 static void do_noemail( FILE *fp, Error *err, int namelen );
134 static void do_ambiguous( FILE *fp, Error *err, int namelen );
135 static void add_to( char ***list, int *nlist, char **new );
136 static int isgroup( LDAPMessage *e );
137 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
138 static void add_group( char *dn, Group **list, int *nlist );
139 static void unbind_and_exit( int rc );
140 static int group_loop( char *dn );
141 static void send_group( Group *group, int ngroup );
142 static int has_attributes( LDAPMessage *e, char *attr1, char *attr2 );
143 static char **get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 );
144 static char *canonical( char *s );
145 static int connect_to_x500( void );
147 static void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
148 static void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
149 static void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
150 static void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
153 main ( int argc, char **argv )
159 int numto, ngroups, numerr, nargs;
162 if ( (myname = strrchr( argv[0], '/' )) == NULL )
163 myname = strdup( argv[0] );
165 myname = strdup( myname + 1 );
168 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
170 openlog( myname, OPENLOG_OPTIONS );
173 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
175 case 'd': /* turn on debugging */
176 debug = atoi( optarg );
179 case 'f': /* who it's from & where errors should go */
180 mailfrom = strdup( optarg );
181 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
182 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
183 sendmailargs[j+1] = mailfrom;
189 case 'h': /* hostname */
190 host = strdup( optarg );
191 hostlen = strlen(host);
194 case 'l': /* ldap host */
195 ldaphost = strdup( optarg );
198 /* mailer-daemon address - who we should */
199 case 'm': /* say errors come from */
200 errorsfrom = strdup( optarg );
203 case 'v': /* vacation host */
204 vacationhost = strdup( optarg );
208 syslog( LOG_ALERT, "unknown option" );
213 if ( mailfrom == NULL ) {
214 syslog( LOG_ALERT, "required argument -f not present" );
217 if ( errorsfrom == NULL ) {
218 syslog( LOG_ALERT, "required argument -m not present" );
221 if ( host == NULL ) {
222 syslog( LOG_ALERT, "required argument -h not present" );
226 if ( connect_to_x500() != 0 )
235 syslog( LOG_ALERT, "running as %d", geteuid() );
236 strcpy( buf, argv[0] );
237 for ( i = 1; i < argc; i++ ) {
239 strcat( buf, argv[i] );
242 syslog( LOG_ALERT, "args: (%s)", buf );
247 add_to( &tolist, &numto, sendmailargs );
249 ngroups = numerr = 0;
252 for ( i = optind; i < argc; i++ ) {
256 for ( j = 0; argv[i][j] != '\0'; j++ ) {
257 if ( argv[i][j] == '.' || argv[i][j] == '_' )
262 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
265 if ((strcasecmp(s, ERROR) == 0) ||
266 (strcasecmp(s, ERRORS) == 0)) {
269 } else if ((strcasecmp(s, REQUEST) == 0) ||
270 (strcasecmp(s, REQUESTS) == 0)) {
271 type = GROUP_REQUEST;
273 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
274 type = GROUP_MEMBERS;
276 } else if ((strcasecmp(s, OWNER) == 0) ||
277 (strcasecmp(s, OWNERS) == 0)) {
283 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
284 &errlist, &numerr, type );
288 * If we have both errors and successful deliveries to make or if
289 * if there are any groups to deliver to, we basically need to read
290 * the message twice. So, we have to put it in a tmp file.
293 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
298 if ( (fp = tmpfile()) == NULL ) {
299 syslog( LOG_ALERT, "could not open tmp file" );
300 unbind_and_exit( EX_TEMPFAIL );
303 /* copy the message to a temp file */
304 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
305 if ( fputs( buf, fp ) == EOF ) {
306 syslog( LOG_ALERT, "error writing tmpfile" );
307 unbind_and_exit( EX_TEMPFAIL );
311 if ( dup2( fileno( fp ), 0 ) == -1 ) {
312 syslog( LOG_ALERT, "could not dup2 tmpfile" );
313 unbind_and_exit( EX_TEMPFAIL );
319 /* deal with errors */
322 syslog( LOG_ALERT, "sending errors" );
324 (void) rewind( stdin );
325 send_errors( errlist, numerr );
328 (void) ldap_unbind( ld );
330 /* send to groups with errorsTo */
333 syslog( LOG_ALERT, "sending to groups with errorsto" );
335 (void) rewind( stdin );
336 send_group( togroups, ngroups );
339 /* send to expanded aliases and groups w/o errorsTo */
340 if ( numto > nargs ) {
342 syslog( LOG_ALERT, "sending to aliases and groups" );
344 (void) rewind( stdin );
345 send_message( tolist );
352 connect_to_x500( void )
356 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
357 syslog( LOG_ALERT, "ldap_open failed" );
361 opt = MAIL500_MAXAMBIGUOUS;
362 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
363 opt = LDAP_DEREF_ALWAYS;
364 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
366 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
367 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
375 mailcmp( char *a, char *b )
379 for ( i = 0; a[i] != '\0'; i++ ) {
380 if ( a[i] != b[i] ) {
385 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
411 LDAPMessage *e, *res;
412 struct timeval timeout;
415 char realfilter[1024];
416 char **mail, **onvacation = NULL, **uid = NULL;
419 * Look up the name in X.500, add the appropriate addresses found
420 * to the to list, or to the err list in case of error. Groups are
421 * handled by the do_group routine, individuals are handled here.
422 * When looking up name, we follow the bases hierarchy, looking
423 * in base[0] first, then base[1], etc. For each base, there is
424 * a set of search filters to try, in order. If something goes
425 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
426 * If the b_rdnpref flag is set, then we give preference to entries
427 * that matched name because it's their rdn, otherwise not.
430 timeout.tv_sec = MAIL500_TIMEOUT;
432 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
433 if ( ! (base[b].b_search & type) ) {
436 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
437 char *format, *p, *s, *d;
441 for ( argc = 0; argc < 3; argc++ ) {
445 format = strdup( base[b].b_filter[f] );
446 for ( argc = 0, p = format; *p; p++ ) {
449 case 's': /* %s is the name */
453 case 'h': /* %h is the host */
460 "unknown format %c", *p );
468 /* three names ought to do... */
469 sprintf( filter, format, argv[0], argv[1], argv[2] );
471 for ( s = filter, d = realfilter; *s; s++, d++ ) {
480 rc = ldap_search_st( ld, base[b].b_dn,
481 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
484 /* some other trouble - try again later */
485 if ( rc != LDAP_SUCCESS &&
486 rc != LDAP_SIZELIMIT_EXCEEDED ) {
487 syslog( LOG_ALERT, "return 0x%x from X.500",
489 unbind_and_exit( EX_TEMPFAIL );
492 if ( (match = ldap_count_entries( ld, res )) != 0 )
502 /* trouble - try again later */
504 syslog( LOG_ALERT, "error parsing result from X.500" );
505 unbind_and_exit( EX_TEMPFAIL );
508 /* no matches - bounce with user unknown */
510 if ( type == USER ) {
511 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
513 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
518 /* more than one match - bounce with ambiguous user? */
520 LDAPMessage *next, *tmpres = NULL;
524 /* not giving rdn preference - bounce with ambiguous user */
525 if ( base[b].b_rdnpref == 0 ) {
526 add_error( err, nerr, E_AMBIGUOUS, name, res );
531 * giving rdn preference - see if any entries were matched
532 * because of their rdn. If so, collect them to deal with
533 * later (== 1 we deliver, > 1 we bounce).
536 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
537 next = ldap_next_entry( ld, e );
538 dn = ldap_get_dn( ld, e );
539 xdn = ldap_explode_dn( dn, 1 );
541 /* XXX bad, but how else can we do it? XXX */
542 if ( strcasecmp( xdn[0], name ) == 0 ) {
543 ldap_delete_result_entry( &res, e );
544 ldap_add_result_entry( &tmpres, e );
547 ldap_value_free( xdn );
551 /* nothing matched by rdn - go ahead and bounce */
552 if ( tmpres == NULL ) {
553 add_error( err, nerr, E_AMBIGUOUS, name, res );
556 /* more than one matched by rdn - bounce with rdn matches */
557 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
558 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
562 } else if ( match < 0 ) {
563 syslog( LOG_ALERT, "error parsing result from X.500" );
564 unbind_and_exit( EX_TEMPFAIL );
567 /* otherwise one matched by rdn - send to it */
573 * if we get this far, it means that we found a single match for
574 * name. for a user, we deliver to the mail attribute or bounce
575 * with address and phone if no mail attr. for a group, we
576 * deliver to all members or bounce to rfc822ErrorsTo if no members.
580 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
581 syslog( LOG_ALERT, "error parsing entry from X.500" );
582 unbind_and_exit( EX_TEMPFAIL );
585 dn = ldap_get_dn( ld, e );
587 if ( type == GROUP_ERRORS ) {
588 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
589 do_group_errors( e, dn, to, nto, err, nerr );
591 } else if ( type == GROUP_REQUEST ) {
592 /* sent to group-request - resend to [rfc822]RequestsTo attr */
593 do_group_request( e, dn, to, nto, err, nerr );
595 } else if ( type == GROUP_MEMBERS ) {
596 /* sent to group-members - expand */
597 do_group_members( e, dn, to, nto, togroups, ngroups, err,
600 } else if ( type == GROUP_OWNER ) {
601 /* sent to group-owner - resend to owner attr */
602 do_group_owner( e, dn, to, nto, err, nerr );
604 } else if ( isgroup( e ) ) {
606 * sent to group - resend from [rfc822]ErrorsTo if it's there,
607 * otherwise, expand the group
610 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
616 * sent to user - mail attribute => add it to the to list,
619 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
624 /* try to detect simple mail loops */
625 sprintf( buf, "%s@%s", name, host );
626 for ( i = 0; mail[i] != NULL; i++ ) {
628 * address is the same as the one we're
629 * sending to - mail loop. syslog the
630 * problem, bounce a message back to the
631 * sender (who else?), and delete the bogus
632 * addr from the list.
635 if ( (h = strchr( mail[i], '@' )) != NULL ) {
637 if ( strcasecmp( h, host ) == 0 ) {
639 "potential loop detected (%s)",
644 if ( mailcmp( buf, mail[i] ) == 0 ) {
646 "loop detected (%s)", mail[i] );
648 /* remove the bogus address */
649 for ( j = i; mail[j] != NULL; j++ ) {
654 if ( mail[0] != NULL ) {
655 add_to( to, nto, mail );
657 add_error( err, nerr, E_NOEMAIL, name, res );
660 ldap_value_free( mail );
662 add_error( err, nerr, E_NOEMAIL, name, res );
666 * If the user is on vacation, send a copy of the mail to
667 * the vacation server. The address is constructed from
668 * the vacationhost (set in a command line argument) and
669 * the uid (XXX this should be more general XXX).
672 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
673 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
678 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
679 sprintf( buf, "%s@%s", uid[0], vacationhost );
684 add_to( to, nto, vaddr );
687 "user without a uid on vacation (%s)",
693 if ( onvacation != NULL ) {
694 ldap_value_free( onvacation );
697 ldap_value_free( uid );
718 * If this group has an rfc822ErrorsTo attribute, we need to
719 * arrange for errors involving this group to go there, not
720 * to the sender. Since sendmail only has the concept of a
721 * single sender, we arrange for errors to go to groupname-errors,
722 * which we then handle specially when (if) it comes back to us
723 * by expanding to all the rfc822ErrorsTo addresses. If it has no
724 * rfc822ErrorsTo attribute, we call do_group_members() to expand
728 if ( group_loop( dn ) ) {
733 * check for moderated groups - if the group has a moderator
734 * attribute, we check to see if the from address is one of
735 * the moderator values. if so, continue on. if not, arrange
736 * to send the mail to the moderator(s). need to do this before
737 * we change the from below.
740 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
741 /* check if it came from any of the group's moderators */
742 for ( i = 0; moderator[i] != NULL; i++ ) {
743 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
747 /* not from the moderator? */
748 if ( moderator[i] == NULL ) {
749 add_to( to, nto, moderator );
750 ldap_value_free( moderator );
754 /* else from the moderator - fall through and deliver it */
757 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
758 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
759 add_group( dn, togroups, ngroups );
764 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
782 int i, rc, anymembers;
784 char **mail, **member, **joinable, **suppress;
786 LDAPMessage *ee, *res;
787 struct timeval timeout;
791 * if all has gone according to plan, we've already arranged for
792 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
793 * so all we have to do here is arrange to send to the
794 * rfc822Mailbox attribute, the member attribute, and anyone who
795 * has joined the group by setting memberOfGroup equal to the
799 /* add members in the group itself - mail attribute */
801 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
803 add_to( to, nto, mail );
805 ldap_value_free( mail );
808 /* add members in the group itself - member attribute */
809 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
810 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
812 for ( i = 0; member[i] != NULL; i++ ) {
813 if ( strcasecmp( dn, member[i] ) == 0 ) {
814 syslog( LOG_ALERT, "group (%s) contains itself",
818 add_member( dn, member[i], to, nto, togroups,
819 ngroups, err, nerr, suppress );
823 ldap_value_free( suppress );
825 ldap_value_free( member );
828 /* add members who have joined by setting memberOfGroup */
829 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
830 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
831 if ( ! anymembers ) {
832 add_error( err, nerr, E_NOMEMBERS, dn,
836 ldap_value_free( joinable );
839 ldap_value_free( joinable );
841 sprintf( filter, "(memberOfGroup=%s)", dn );
843 timeout.tv_sec = MAIL500_TIMEOUT;
846 /* for each subtree to look in... */
847 opt = MAIL500_MAXAMBIGUOUS;
848 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
849 for ( i = 0; base[i].b_dn != NULL; i++ ) {
850 /* find entries that have joined this group... */
851 rc = ldap_search_st( ld, base[i].b_dn,
852 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
855 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
856 rc == LDAP_TIMELIMIT_EXCEEDED ) {
858 "group search limit exceeded %d", rc );
859 unbind_and_exit( EX_TEMPFAIL );
862 if ( rc != LDAP_SUCCESS ) {
863 syslog( LOG_ALERT, "group search return 0x%x",
865 unbind_and_exit( EX_TEMPFAIL );
868 /* for each entry that has joined... */
869 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
870 ee = ldap_next_entry( ld, ee ) ) {
872 if ( isgroup( ee ) ) {
873 ndn = ldap_get_dn( ld, ee );
875 if ( do_group( e, ndn, to, nto,
876 togroups, ngroups, err, nerr )
879 "group loop (%s) (%s)",
888 /* add them to the to list */
889 if ( (mail = ldap_get_values( ld, ee, "mail" ))
891 add_to( to, nto, mail );
893 ldap_value_free( mail );
895 /* else generate a bounce */
897 ndn = ldap_get_dn( ld, ee );
899 add_error( err, nerr,
900 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
908 opt = MAIL500_MAXAMBIGUOUS;
909 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
912 if ( ! anymembers ) {
913 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
933 LDAPMessage *res, *e;
934 struct timeval timeout;
936 timeout.tv_sec = MAIL500_TIMEOUT;
938 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
939 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
940 if ( rc == LDAP_NO_SUCH_OBJECT ) {
941 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
945 syslog( LOG_ALERT, "member search return 0x%x", rc );
947 unbind_and_exit( EX_TEMPFAIL );
951 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
952 syslog( LOG_ALERT, "member search error parsing entry" );
954 unbind_and_exit( EX_TEMPFAIL );
956 ndn = ldap_get_dn( ld, e );
958 /* allow groups within groups */
959 if ( isgroup( e ) ) {
960 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
962 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
970 /* send to the member's mail attribute */
971 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
972 add_to( to, nto, mail );
974 ldap_value_free( mail );
976 /* else generate a bounce */
978 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
980 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
999 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
1000 "requestsTo" )) != NULL ) {
1001 add_to( to, nto, requeststo );
1003 ldap_value_free( requeststo );
1005 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
1021 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1022 "errorsTo" )) != NULL ) {
1023 add_to( to, nto, errorsto );
1025 ldap_value_free( errorsto );
1027 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1043 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1044 add_to( to, nto, owner );
1045 ldap_value_free( owner );
1047 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1052 send_message( char **to )
1055 #ifndef HAVE_WAITPID
1056 WAITSTATUSTYPE status;
1063 strcpy( buf, to[0] );
1064 for ( i = 1; to[i] != NULL; i++ ) {
1066 strcat( buf, to[i] );
1069 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1073 if ( (pid = fork()) != 0 ) {
1075 waitpid( pid, (int *) NULL, 0 );
1077 wait4( pid, &status, WAIT_FLAGS, 0 );
1081 /* to includes sendmailargs */
1082 execv( MAIL500_SENDMAIL, to );
1084 syslog( LOG_ALERT, "execv failed" );
1086 exit( EX_TEMPFAIL );
1091 send_group( Group *group, int ngroup )
1097 #ifndef HAVE_WAITPID
1098 WAITSTATUSTYPE status;
1101 for ( i = 0; i < ngroup; i++ ) {
1102 (void) rewind( stdin );
1104 iargv[0] = MAIL500_SENDMAIL;
1106 iargv[2] = group[i].g_errorsto;
1107 iargv[3] = "-oMrX.500";
1114 add_to( &argv, &argc, iargv );
1115 add_to( &argv, &argc, group[i].g_members );
1121 strcpy( buf, argv[0] );
1122 for ( i = 1; i < argc; i++ ) {
1124 strcat( buf, argv[i] );
1127 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1131 if ( (pid = fork()) != 0 ) {
1133 waitpid( pid, (int *) NULL, 0 );
1135 wait4( pid, &status, WAIT_FLAGS, 0 );
1139 execv( MAIL500_SENDMAIL, argv );
1141 syslog( LOG_ALERT, "execv failed" );
1143 exit( EX_TEMPFAIL );
1149 send_errors( Error *err, int nerr )
1151 int pid, i, namelen;
1156 #ifndef HAVE_WAITPID
1157 WAITSTATUSTYPE status;
1160 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1161 mailfrom = errorsfrom;
1164 argv[0] = MAIL500_SENDMAIL;
1165 argv[1] = "-oMrX.500";
1169 argv[5] = MAIL500_BOUNCEFROM;
1176 strcpy( buf, argv[0] );
1177 for ( i = 1; argv[i] != NULL; i++ ) {
1179 strcat( buf, argv[i] );
1182 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1185 if ( pipe( fd ) == -1 ) {
1186 syslog( LOG_ALERT, "cannot create pipe" );
1187 exit( EX_TEMPFAIL );
1190 if ( (pid = fork()) != 0 ) {
1191 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1192 syslog( LOG_ALERT, "cannot fdopen pipe" );
1193 exit( EX_TEMPFAIL );
1196 fprintf( fp, "To: %s\n", mailfrom );
1197 fprintf( fp, "From: %s\n", errorsfrom );
1198 fprintf( fp, "Subject: undeliverable mail\n" );
1199 fprintf( fp, "\n" );
1200 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1201 for ( i = 0; i < nerr; i++ ) {
1202 namelen = strlen( err[i].e_addr );
1203 fprintf( fp, "\n" );
1205 switch ( err[i].e_code ) {
1207 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1210 case E_GROUPUNKNOWN:
1211 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1215 fprintf( fp, "%s: Group member does not exist\n",
1217 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1218 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1219 fprintf( fp, "then re-adding the person to the group.\n" );
1223 fprintf( fp, "%s: Group exists but has no request address\n",
1228 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1233 fprintf( fp, "%s: Group exists but has no owner\n",
1238 do_ambiguous( fp, &err[i], namelen );
1242 do_noemail( fp, &err[i], namelen );
1245 case E_MEMBERNOEMAIL:
1246 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1250 case E_JOINMEMBERNOEMAIL:
1251 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1256 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1257 err[i].e_addr, err[i].e_loop );
1261 fprintf( fp, "%s: Group has no members\n",
1266 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1267 unbind_and_exit( EX_TEMPFAIL );
1272 fprintf( fp, "\n------- The original message sent:\n\n" );
1274 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1280 waitpid( pid, (int *) NULL, 0 );
1282 wait4( pid, &status, WAIT_FLAGS, 0 );
1287 execv( MAIL500_SENDMAIL, argv );
1289 syslog( LOG_ALERT, "execv failed" );
1291 exit( EX_TEMPFAIL );
1296 do_noemail( FILE *fp, Error *err, int namelen )
1302 fprintf(fp, "%s: User has no email address registered.\n",
1304 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1305 namelen, " ", err->e_addr );
1308 dn = ldap_get_dn( ld, err->e_msg );
1309 ufn = ldap_explode_dn( dn, 1 );
1310 rdn = strdup( ufn[0] );
1311 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1312 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1314 for ( i = 0; vals[i]; i++ ) {
1315 last = strlen( vals[i] ) - 1;
1316 if ( isdigit( vals[i][last] ) ) {
1317 rdn = strdup( vals[i] );
1322 ldap_value_free( vals );
1325 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1328 ldap_value_free( ufn );
1330 /* titles or descriptions */
1331 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1332 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1334 fprintf( fp, "%*s No title or description registered\n",
1337 for ( i = 0; vals[i] != NULL; i++ ) {
1338 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1341 ldap_value_free( vals );
1344 /* postal address */
1345 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1347 fprintf( fp, "%*s No postal address registered\n", namelen,
1350 fprintf( fp, "%*s ", namelen, " " );
1351 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1352 if ( vals[0][i] == '$' ) {
1353 fprintf( fp, "\n%*s ", namelen, " " );
1354 while ( isspace( vals[0][i+1] ) )
1357 fprintf( fp, "%c", vals[0][i] );
1360 fprintf( fp, "\n" );
1362 ldap_value_free( vals );
1365 /* telephone number */
1366 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1368 fprintf( fp, "%*s No phone number registered\n", namelen,
1371 for ( i = 0; vals[i] != NULL; i++ ) {
1372 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1375 ldap_value_free( vals );
1381 do_ambiguous( FILE *fp, Error *err, int namelen )
1388 i = ldap_result2error( ld, err->e_msg, 0 );
1390 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1391 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1392 ldap_count_entries( ld, err->e_msg ) );
1394 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1395 e = ldap_next_entry( ld, e ) ) {
1396 dn = ldap_get_dn( ld, e );
1397 ufn = ldap_explode_dn( dn, 1 );
1398 rdn = strdup( ufn[0] );
1399 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1400 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1401 for ( i = 0; vals[i]; i++ ) {
1402 last = strlen( vals[i] ) - 1;
1403 if ( isdigit( vals[i][last] ) ) {
1404 rdn = strdup( vals[i] );
1409 ldap_value_free( vals );
1413 if ( isgroup( e ) ) {
1414 vals = ldap_get_values( ld, e, "description" );
1416 vals = ldap_get_values( ld, e, "title" );
1419 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1420 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1421 fprintf( fp, " %s\n", vals[i] );
1426 ldap_value_free( ufn );
1428 ldap_value_free( vals );
1433 count_values( char **list )
1437 for ( i = 0; list && list[i] != NULL; i++ )
1444 add_to( char ***list, int *nlist, char **new )
1446 int i, nnew, oldnlist;
1448 nnew = count_values( new );
1451 if ( *list == NULL || *nlist == 0 ) {
1452 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1455 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1456 nnew * sizeof(char *) + sizeof(char *) );
1460 for ( i = 0; i < nnew; i++ )
1461 (*list)[i + oldnlist] = strdup( new[i] );
1462 (*list)[*nlist] = NULL;
1466 isgroup( LDAPMessage *e )
1471 oclist = ldap_get_values( ld, e, "objectClass" );
1473 for ( i = 0; oclist[i] != NULL; i++ ) {
1474 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1475 ldap_value_free( oclist );
1479 ldap_value_free( oclist );
1485 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1488 *err = (Error *) malloc( sizeof(Error) );
1490 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1493 (*err)[*nerr].e_code = code;
1494 (*err)[*nerr].e_addr = strdup( addr );
1495 (*err)[*nerr].e_msg = msg;
1500 add_group( char *dn, Group **list, int *nlist )
1505 for ( i = 0; i < *nlist; i++ ) {
1506 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1507 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1512 ufn = ldap_explode_dn( dn, 1 );
1513 namelen = strlen( ufn[0] );
1515 if ( *nlist == 0 ) {
1516 *list = (Group *) malloc( sizeof(Group) );
1518 *list = (Group *) realloc( *list, (*nlist + 1) *
1522 /* send errors to groupname-errors@host */
1523 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1525 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1526 (void) canonical( (*list)[*nlist].g_errorsto );
1528 /* send to groupname-members@host - make it a list for send_group */
1529 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1530 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1531 sizeof(MEMBERS) + hostlen + 2 );
1532 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1534 (void) canonical( (*list)[*nlist].g_members[0] );
1535 (*list)[*nlist].g_members[1] = NULL;
1537 /* save the group's dn so we can check for loops above */
1538 (*list)[*nlist].g_dn = strdup( dn );
1542 ldap_value_free( ufn );
1546 unbind_and_exit( int rc )
1550 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1551 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1557 canonical( char *s )
1561 for ( ; *s != '\0'; s++ ) {
1570 group_loop( char *dn )
1573 static char **groups;
1576 for ( i = 0; i < ngroups; i++ ) {
1577 if ( strcmp( dn, groups[i] ) == 0 )
1582 groups = (char **) malloc( sizeof(char *) );
1584 groups = (char **) realloc( groups,
1585 (ngroups + 1) * sizeof(char *) );
1587 groups[ngroups++] = strdup( dn );
1593 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1597 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1598 ldap_value_free( attr );
1602 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1603 ldap_value_free( attr );
1611 get_attributes_mail_dn(
1614 char *attr2 /* this one is dn-valued */
1617 LDAPMessage *ee, *res;
1618 char **vals, **dnlist, **mail, **grname, **graddr;
1621 struct timeval timeout;
1623 dn = ldap_get_dn( ld, e );
1625 vals = ldap_get_values( ld, e, attr1 );
1626 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1629 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1630 timeout.tv_sec = MAIL500_TIMEOUT;
1631 timeout.tv_usec = 0;
1633 for ( i = 0; dnlist[i] != NULL; i++ ) {
1634 if ( (rc = ldap_search_st( ld, dnlist[i],
1635 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1636 &timeout, &res )) != LDAP_SUCCESS ) {
1637 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1638 unbind_and_exit( EX_TEMPFAIL );
1641 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1647 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1648 syslog( LOG_ALERT, "error parsing x500 entry" );
1652 if ( isgroup(ee) ) {
1655 grname = ldap_explode_dn( dnlist[i], 1 );
1657 /* groupname + host + @ + null */
1658 graddr[0] = (char *) malloc( strlen( grname[0] )
1659 + strlen( host ) + 2 );
1661 sprintf( graddr[0], "%s@%s", grname[0], host);
1662 (void) canonical( graddr[0] );
1664 add_to( &vals, &nto, graddr );
1667 ldap_value_free( grname );
1668 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1670 add_to( &vals, &nto, mail );
1672 ldap_value_free( mail );
1675 ldap_msgfree( res );