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/signal.h>
20 #include <ac/string.h>
21 #include <ac/sysexits.h>
22 #include <ac/syslog.h>
24 #include <ac/unistd.h>
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 #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, dc=OpenLDAP, dc=org",
111 {"uid=%s", "cn=%s", NULL}},
112 {"ou=System Groups, ou=Groups, dc=OpenLDAP, dc=org",
114 {"(&(cn=%s)(associatedDomain=%h))", NULL, NULL}},
115 {"ou=User Groups, ou=Groups, dc=OpenLDAP, dc=org",
117 {"(&(cn=%s)(associatedDomain=%h))", NULL, NULL}},
121 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-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 (void) SIGNAL( SIGPIPE, SIG_IGN );
174 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
176 openlog( myname, OPENLOG_OPTIONS );
179 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
181 case 'd': /* turn on debugging */
182 debug = atoi( optarg );
185 case 'f': /* who it's from & where errors should go */
186 mailfrom = strdup( optarg );
187 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
188 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
189 sendmailargs[j+1] = mailfrom;
195 case 'h': /* hostname */
196 host = strdup( optarg );
197 hostlen = strlen(host);
200 case 'l': /* ldap host */
201 ldaphost = strdup( optarg );
204 /* mailer-daemon address - who we should */
205 case 'm': /* say errors come from */
206 errorsfrom = strdup( optarg );
209 case 'v': /* vacation host */
210 vacationhost = strdup( optarg );
214 syslog( LOG_ALERT, "unknown option" );
219 if ( mailfrom == NULL ) {
220 syslog( LOG_ALERT, "required argument -f not present" );
223 if ( errorsfrom == NULL ) {
224 syslog( LOG_ALERT, "required argument -m not present" );
227 if ( host == NULL ) {
228 syslog( LOG_ALERT, "required argument -h not present" );
232 if ( connect_to_x500() != 0 )
241 syslog( LOG_ALERT, "running as %d", geteuid() );
242 strcpy( buf, argv[0] );
243 for ( i = 1; i < argc; i++ ) {
245 strcat( buf, argv[i] );
248 syslog( LOG_ALERT, "args: (%s)", buf );
253 add_to( &tolist, &numto, sendmailargs );
255 ngroups = numerr = 0;
258 for ( i = optind; i < argc; i++ ) {
262 for ( j = 0; argv[i][j] != '\0'; j++ ) {
263 if ( argv[i][j] == '.' || argv[i][j] == '_' )
268 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
271 if ((strcasecmp(s, ERROR) == 0) ||
272 (strcasecmp(s, ERRORS) == 0)) {
275 } else if ((strcasecmp(s, REQUEST) == 0) ||
276 (strcasecmp(s, REQUESTS) == 0)) {
277 type = GROUP_REQUEST;
279 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
280 type = GROUP_MEMBERS;
282 } else if ((strcasecmp(s, OWNER) == 0) ||
283 (strcasecmp(s, OWNERS) == 0)) {
289 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
290 &errlist, &numerr, type );
294 * If we have both errors and successful deliveries to make or if
295 * if there are any groups to deliver to, we basically need to read
296 * the message twice. So, we have to put it in a tmp file.
299 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
304 if ( (fp = tmpfile()) == NULL ) {
305 syslog( LOG_ALERT, "could not open tmp file" );
306 unbind_and_exit( EX_TEMPFAIL );
309 /* copy the message to a temp file */
310 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
311 if ( fputs( buf, fp ) == EOF ) {
312 syslog( LOG_ALERT, "error writing tmpfile" );
313 unbind_and_exit( EX_TEMPFAIL );
317 if ( dup2( fileno( fp ), 0 ) == -1 ) {
318 syslog( LOG_ALERT, "could not dup2 tmpfile" );
319 unbind_and_exit( EX_TEMPFAIL );
325 /* deal with errors */
328 syslog( LOG_ALERT, "sending errors" );
330 (void) rewind( stdin );
331 send_errors( errlist, numerr );
334 (void) ldap_unbind( ld );
336 /* send to groups with errorsTo */
339 syslog( LOG_ALERT, "sending to groups with errorsto" );
341 (void) rewind( stdin );
342 send_group( togroups, ngroups );
345 /* send to expanded aliases and groups w/o errorsTo */
346 if ( numto > nargs ) {
348 syslog( LOG_ALERT, "sending to aliases and groups" );
350 (void) rewind( stdin );
351 send_message( tolist );
358 connect_to_x500( void )
362 if ( (ld = ldap_open( ldaphost, 0 )) == NULL ) {
363 syslog( LOG_ALERT, "ldap_open failed" );
367 opt = MAIL500_MAXAMBIGUOUS;
368 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
369 opt = LDAP_DEREF_ALWAYS;
370 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
372 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
373 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
381 mailcmp( char *a, char *b )
385 for ( i = 0; a[i] != '\0'; i++ ) {
386 if ( a[i] != b[i] ) {
391 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
417 LDAPMessage *e, *res;
418 struct timeval timeout;
421 char realfilter[1024];
422 char **mail, **onvacation = NULL, **uid = NULL;
425 * Look up the name in X.500, add the appropriate addresses found
426 * to the to list, or to the err list in case of error. Groups are
427 * handled by the do_group routine, individuals are handled here.
428 * When looking up name, we follow the bases hierarchy, looking
429 * in base[0] first, then base[1], etc. For each base, there is
430 * a set of search filters to try, in order. If something goes
431 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
432 * If the b_rdnpref flag is set, then we give preference to entries
433 * that matched name because it's their rdn, otherwise not.
436 timeout.tv_sec = MAIL500_TIMEOUT;
438 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
439 if ( ! (base[b].b_search & type) ) {
442 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
443 char *format, *p, *s, *d;
447 for ( argc = 0; argc < 3; argc++ ) {
451 format = strdup( base[b].b_filter[f] );
452 for ( argc = 0, p = format; *p; p++ ) {
455 case 's': /* %s is the name */
459 case 'h': /* %h is the host */
466 "unknown format %c", *p );
474 /* three names ought to do... */
475 sprintf( filter, format, argv[0], argv[1], argv[2] );
477 for ( s = filter, d = realfilter; *s; s++, d++ ) {
486 rc = ldap_search_st( ld, base[b].b_dn,
487 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
490 /* some other trouble - try again later */
491 if ( rc != LDAP_SUCCESS &&
492 rc != LDAP_SIZELIMIT_EXCEEDED ) {
493 syslog( LOG_ALERT, "return 0x%x from X.500",
495 unbind_and_exit( EX_TEMPFAIL );
498 if ( (match = ldap_count_entries( ld, res )) != 0 )
508 /* trouble - try again later */
510 syslog( LOG_ALERT, "error parsing result from X.500" );
511 unbind_and_exit( EX_TEMPFAIL );
514 /* no matches - bounce with user unknown */
516 if ( type == USER ) {
517 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
519 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
524 /* more than one match - bounce with ambiguous user? */
526 LDAPMessage *next, *tmpres = NULL;
530 /* not giving rdn preference - bounce with ambiguous user */
531 if ( base[b].b_rdnpref == 0 ) {
532 add_error( err, nerr, E_AMBIGUOUS, name, res );
537 * giving rdn preference - see if any entries were matched
538 * because of their rdn. If so, collect them to deal with
539 * later (== 1 we deliver, > 1 we bounce).
542 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
543 next = ldap_next_entry( ld, e );
544 dn = ldap_get_dn( ld, e );
545 xdn = ldap_explode_dn( dn, 1 );
547 /* XXX bad, but how else can we do it? XXX */
548 if ( strcasecmp( xdn[0], name ) == 0 ) {
549 ldap_delete_result_entry( &res, e );
550 ldap_add_result_entry( &tmpres, e );
553 ldap_value_free( xdn );
557 /* nothing matched by rdn - go ahead and bounce */
558 if ( tmpres == NULL ) {
559 add_error( err, nerr, E_AMBIGUOUS, name, res );
562 /* more than one matched by rdn - bounce with rdn matches */
563 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
564 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
568 } else if ( match < 0 ) {
569 syslog( LOG_ALERT, "error parsing result from X.500" );
570 unbind_and_exit( EX_TEMPFAIL );
573 /* otherwise one matched by rdn - send to it */
579 * if we get this far, it means that we found a single match for
580 * name. for a user, we deliver to the mail attribute or bounce
581 * with address and phone if no mail attr. for a group, we
582 * deliver to all members or bounce to rfc822ErrorsTo if no members.
586 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
587 syslog( LOG_ALERT, "error parsing entry from X.500" );
588 unbind_and_exit( EX_TEMPFAIL );
591 dn = ldap_get_dn( ld, e );
593 if ( type == GROUP_ERRORS ) {
594 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
595 do_group_errors( e, dn, to, nto, err, nerr );
597 } else if ( type == GROUP_REQUEST ) {
598 /* sent to group-request - resend to [rfc822]RequestsTo attr */
599 do_group_request( e, dn, to, nto, err, nerr );
601 } else if ( type == GROUP_MEMBERS ) {
602 /* sent to group-members - expand */
603 do_group_members( e, dn, to, nto, togroups, ngroups, err,
606 } else if ( type == GROUP_OWNER ) {
607 /* sent to group-owner - resend to owner attr */
608 do_group_owner( e, dn, to, nto, err, nerr );
610 } else if ( isgroup( e ) ) {
612 * sent to group - resend from [rfc822]ErrorsTo if it's there,
613 * otherwise, expand the group
616 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
622 * sent to user - mail attribute => add it to the to list,
625 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
630 /* try to detect simple mail loops */
631 sprintf( buf, "%s@%s", name, host );
632 for ( i = 0; mail[i] != NULL; i++ ) {
634 * address is the same as the one we're
635 * sending to - mail loop. syslog the
636 * problem, bounce a message back to the
637 * sender (who else?), and delete the bogus
638 * addr from the list.
641 if ( (h = strchr( mail[i], '@' )) != NULL ) {
643 if ( strcasecmp( h, host ) == 0 ) {
645 "potential loop detected (%s)",
650 if ( mailcmp( buf, mail[i] ) == 0 ) {
652 "loop detected (%s)", mail[i] );
654 /* remove the bogus address */
655 for ( j = i; mail[j] != NULL; j++ ) {
660 if ( mail[0] != NULL ) {
661 add_to( to, nto, mail );
663 add_error( err, nerr, E_NOEMAIL, name, res );
666 ldap_value_free( mail );
668 add_error( err, nerr, E_NOEMAIL, name, res );
672 * If the user is on vacation, send a copy of the mail to
673 * the vacation server. The address is constructed from
674 * the vacationhost (set in a command line argument) and
675 * the uid (XXX this should be more general XXX).
678 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
679 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
684 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
685 sprintf( buf, "%s@%s", uid[0], vacationhost );
690 add_to( to, nto, vaddr );
693 "user without a uid on vacation (%s)",
699 if ( onvacation != NULL ) {
700 ldap_value_free( onvacation );
703 ldap_value_free( uid );
724 * If this group has an rfc822ErrorsTo attribute, we need to
725 * arrange for errors involving this group to go there, not
726 * to the sender. Since sendmail only has the concept of a
727 * single sender, we arrange for errors to go to groupname-errors,
728 * which we then handle specially when (if) it comes back to us
729 * by expanding to all the rfc822ErrorsTo addresses. If it has no
730 * rfc822ErrorsTo attribute, we call do_group_members() to expand
734 if ( group_loop( dn ) ) {
739 * check for moderated groups - if the group has a moderator
740 * attribute, we check to see if the from address is one of
741 * the moderator values. if so, continue on. if not, arrange
742 * to send the mail to the moderator(s). need to do this before
743 * we change the from below.
746 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
747 /* check if it came from any of the group's moderators */
748 for ( i = 0; moderator[i] != NULL; i++ ) {
749 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
753 /* not from the moderator? */
754 if ( moderator[i] == NULL ) {
755 add_to( to, nto, moderator );
756 ldap_value_free( moderator );
760 /* else from the moderator - fall through and deliver it */
763 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
764 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
765 add_group( dn, togroups, ngroups );
770 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
788 int i, rc, anymembers;
790 char **mail, **member, **joinable, **suppress;
792 LDAPMessage *ee, *res;
793 struct timeval timeout;
797 * if all has gone according to plan, we've already arranged for
798 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
799 * so all we have to do here is arrange to send to the
800 * rfc822Mailbox attribute, the member attribute, and anyone who
801 * has joined the group by setting memberOfGroup equal to the
805 /* add members in the group itself - mail attribute */
807 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
809 add_to( to, nto, mail );
811 ldap_value_free( mail );
814 /* add members in the group itself - member attribute */
815 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
816 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
818 for ( i = 0; member[i] != NULL; i++ ) {
819 if ( strcasecmp( dn, member[i] ) == 0 ) {
820 syslog( LOG_ALERT, "group (%s) contains itself",
824 add_member( dn, member[i], to, nto, togroups,
825 ngroups, err, nerr, suppress );
829 ldap_value_free( suppress );
831 ldap_value_free( member );
834 /* add members who have joined by setting memberOfGroup */
835 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
836 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
837 if ( ! anymembers ) {
838 add_error( err, nerr, E_NOMEMBERS, dn,
842 ldap_value_free( joinable );
845 ldap_value_free( joinable );
847 sprintf( filter, "(memberOfGroup=%s)", dn );
849 timeout.tv_sec = MAIL500_TIMEOUT;
852 /* for each subtree to look in... */
853 opt = MAIL500_MAXAMBIGUOUS;
854 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
855 for ( i = 0; base[i].b_dn != NULL; i++ ) {
856 /* find entries that have joined this group... */
857 rc = ldap_search_st( ld, base[i].b_dn,
858 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
861 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
862 rc == LDAP_TIMELIMIT_EXCEEDED ) {
864 "group search limit exceeded %d", rc );
865 unbind_and_exit( EX_TEMPFAIL );
868 if ( rc != LDAP_SUCCESS ) {
869 syslog( LOG_ALERT, "group search return 0x%x",
871 unbind_and_exit( EX_TEMPFAIL );
874 /* for each entry that has joined... */
875 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
876 ee = ldap_next_entry( ld, ee ) ) {
878 if ( isgroup( ee ) ) {
879 ndn = ldap_get_dn( ld, ee );
881 if ( do_group( e, ndn, to, nto,
882 togroups, ngroups, err, nerr )
885 "group loop (%s) (%s)",
894 /* add them to the to list */
895 if ( (mail = ldap_get_values( ld, ee, "mail" ))
897 add_to( to, nto, mail );
899 ldap_value_free( mail );
901 /* else generate a bounce */
903 ndn = ldap_get_dn( ld, ee );
905 add_error( err, nerr,
906 E_JOINMEMBERNOEMAIL, ndn, NULL );
914 opt = MAIL500_MAXAMBIGUOUS;
915 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
918 if ( ! anymembers ) {
919 add_error( err, nerr, E_NOMEMBERS, dn, NULL );
939 LDAPMessage *res, *e;
940 struct timeval timeout;
942 timeout.tv_sec = MAIL500_TIMEOUT;
944 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
945 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
946 if ( rc == LDAP_NO_SUCH_OBJECT ) {
947 add_error( err, nerr, E_BADMEMBER, dn, NULL );
951 syslog( LOG_ALERT, "member search return 0x%x", rc );
953 unbind_and_exit( EX_TEMPFAIL );
957 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
958 syslog( LOG_ALERT, "member search error parsing entry" );
960 unbind_and_exit( EX_TEMPFAIL );
962 ndn = ldap_get_dn( ld, e );
964 /* allow groups within groups */
965 if ( isgroup( e ) ) {
966 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
968 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
976 /* send to the member's mail attribute */
977 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
978 add_to( to, nto, mail );
980 ldap_value_free( mail );
982 /* else generate a bounce */
984 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
986 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULL );
1005 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
1006 "requestsTo" )) != NULL ) {
1007 add_to( to, nto, requeststo );
1009 ldap_value_free( requeststo );
1011 add_error( err, nerr, E_NOREQUEST, dn, NULL );
1027 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1028 "errorsTo" )) != NULL ) {
1029 add_to( to, nto, errorsto );
1031 ldap_value_free( errorsto );
1033 add_error( err, nerr, E_NOERRORS, dn, NULL );
1049 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1050 add_to( to, nto, owner );
1051 ldap_value_free( owner );
1053 add_error( err, nerr, E_NOOWNER, dn, NULL );
1058 send_message( char **to )
1061 #ifndef HAVE_WAITPID
1062 WAITSTATUSTYPE status;
1069 strcpy( buf, to[0] );
1070 for ( i = 1; to[i] != NULL; i++ ) {
1072 strcat( buf, to[i] );
1075 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1079 if ( (pid = fork()) != 0 ) {
1081 waitpid( pid, (int *) NULL, 0 );
1083 wait4( pid, &status, WAIT_FLAGS, 0 );
1087 /* to includes sendmailargs */
1088 execv( MAIL500_SENDMAIL, to );
1090 syslog( LOG_ALERT, "execv failed" );
1092 exit( EX_TEMPFAIL );
1097 send_group( Group *group, int ngroup )
1103 #ifndef HAVE_WAITPID
1104 WAITSTATUSTYPE status;
1107 for ( i = 0; i < ngroup; i++ ) {
1108 (void) rewind( stdin );
1110 iargv[0] = MAIL500_SENDMAIL;
1112 iargv[2] = group[i].g_errorsto;
1113 iargv[3] = "-oMrX.500";
1120 add_to( &argv, &argc, iargv );
1121 add_to( &argv, &argc, group[i].g_members );
1127 strcpy( buf, argv[0] );
1128 for ( i = 1; i < argc; i++ ) {
1130 strcat( buf, argv[i] );
1133 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1137 if ( (pid = fork()) != 0 ) {
1139 waitpid( pid, (int *) NULL, 0 );
1141 wait4( pid, &status, WAIT_FLAGS, 0 );
1145 execv( MAIL500_SENDMAIL, argv );
1147 syslog( LOG_ALERT, "execv failed" );
1149 exit( EX_TEMPFAIL );
1155 send_errors( Error *err, int nerr )
1157 int pid, i, namelen;
1162 #ifndef HAVE_WAITPID
1163 WAITSTATUSTYPE status;
1166 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1167 mailfrom = errorsfrom;
1170 argv[0] = MAIL500_SENDMAIL;
1171 argv[1] = "-oMrX.500";
1175 argv[5] = MAIL500_BOUNCEFROM;
1182 strcpy( buf, argv[0] );
1183 for ( i = 1; argv[i] != NULL; i++ ) {
1185 strcat( buf, argv[i] );
1188 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1191 if ( pipe( fd ) == -1 ) {
1192 syslog( LOG_ALERT, "cannot create pipe" );
1193 exit( EX_TEMPFAIL );
1196 if ( (pid = fork()) != 0 ) {
1197 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1198 syslog( LOG_ALERT, "cannot fdopen pipe" );
1199 exit( EX_TEMPFAIL );
1202 fprintf( fp, "To: %s\n", mailfrom );
1203 fprintf( fp, "From: %s\n", errorsfrom );
1204 fprintf( fp, "Subject: undeliverable mail\n" );
1205 fprintf( fp, "\n" );
1206 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1207 for ( i = 0; i < nerr; i++ ) {
1208 namelen = strlen( err[i].e_addr );
1209 fprintf( fp, "\n" );
1211 switch ( err[i].e_code ) {
1213 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1216 case E_GROUPUNKNOWN:
1217 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1221 fprintf( fp, "%s: Group member does not exist\n",
1223 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1224 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1225 fprintf( fp, "then re-adding the person to the group.\n" );
1229 fprintf( fp, "%s: Group exists but has no request address\n",
1234 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1239 fprintf( fp, "%s: Group exists but has no owner\n",
1244 do_ambiguous( fp, &err[i], namelen );
1248 do_noemail( fp, &err[i], namelen );
1251 case E_MEMBERNOEMAIL:
1252 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1256 case E_JOINMEMBERNOEMAIL:
1257 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1262 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1263 err[i].e_addr, err[i].e_loop );
1267 fprintf( fp, "%s: Group has no members\n",
1272 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1273 unbind_and_exit( EX_TEMPFAIL );
1278 fprintf( fp, "\n------- The original message sent:\n\n" );
1280 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1286 waitpid( pid, (int *) NULL, 0 );
1288 wait4( pid, &status, WAIT_FLAGS, 0 );
1293 execv( MAIL500_SENDMAIL, argv );
1295 syslog( LOG_ALERT, "execv failed" );
1297 exit( EX_TEMPFAIL );
1302 do_noemail( FILE *fp, Error *err, int namelen )
1308 fprintf(fp, "%s: User has no email address registered.\n",
1310 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1311 namelen, " ", err->e_addr );
1314 dn = ldap_get_dn( ld, err->e_msg );
1315 ufn = ldap_explode_dn( dn, 1 );
1316 rdn = strdup( ufn[0] );
1317 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1318 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1320 for ( i = 0; vals[i]; i++ ) {
1321 last = strlen( vals[i] ) - 1;
1322 if ( isdigit((unsigned char) vals[i][last]) ) {
1323 rdn = strdup( vals[i] );
1328 ldap_value_free( vals );
1331 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1334 ldap_value_free( ufn );
1336 /* titles or descriptions */
1337 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1338 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1340 fprintf( fp, "%*s No title or description registered\n",
1343 for ( i = 0; vals[i] != NULL; i++ ) {
1344 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1347 ldap_value_free( vals );
1350 /* postal address */
1351 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1353 fprintf( fp, "%*s No postal address registered\n", namelen,
1356 fprintf( fp, "%*s ", namelen, " " );
1357 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1358 if ( vals[0][i] == '$' ) {
1359 fprintf( fp, "\n%*s ", namelen, " " );
1360 while ( isspace((unsigned char) vals[0][i+1]) )
1363 fprintf( fp, "%c", vals[0][i] );
1366 fprintf( fp, "\n" );
1368 ldap_value_free( vals );
1371 /* telephone number */
1372 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1374 fprintf( fp, "%*s No phone number registered\n", namelen,
1377 for ( i = 0; vals[i] != NULL; i++ ) {
1378 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1381 ldap_value_free( vals );
1387 do_ambiguous( FILE *fp, Error *err, int namelen )
1394 i = ldap_result2error( ld, err->e_msg, 0 );
1396 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1397 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1398 ldap_count_entries( ld, err->e_msg ) );
1400 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1401 e = ldap_next_entry( ld, e ) ) {
1402 dn = ldap_get_dn( ld, e );
1403 ufn = ldap_explode_dn( dn, 1 );
1404 rdn = strdup( ufn[0] );
1405 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1406 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1407 for ( i = 0; vals[i]; i++ ) {
1408 last = strlen( vals[i] ) - 1;
1409 if (isdigit((unsigned char) vals[i][last])) {
1410 rdn = strdup( vals[i] );
1415 ldap_value_free( vals );
1419 if ( isgroup( e ) ) {
1420 vals = ldap_get_values( ld, e, "description" );
1422 vals = ldap_get_values( ld, e, "title" );
1425 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1426 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1427 fprintf( fp, " %s\n", vals[i] );
1432 ldap_value_free( ufn );
1434 ldap_value_free( vals );
1439 count_values( char **list )
1443 for ( i = 0; list && list[i] != NULL; i++ )
1450 add_to( char ***list, int *nlist, char **new )
1452 int i, nnew, oldnlist;
1454 nnew = count_values( new );
1457 if ( *list == NULL || *nlist == 0 ) {
1458 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1461 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1462 nnew * sizeof(char *) + sizeof(char *) );
1466 for ( i = 0; i < nnew; i++ )
1467 (*list)[i + oldnlist] = strdup( new[i] );
1468 (*list)[*nlist] = NULL;
1472 isgroup( LDAPMessage *e )
1477 oclist = ldap_get_values( ld, e, "objectClass" );
1479 for ( i = 0; oclist[i] != NULL; i++ ) {
1480 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1481 ldap_value_free( oclist );
1485 ldap_value_free( oclist );
1491 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1494 *err = (Error *) malloc( sizeof(Error) );
1496 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1499 (*err)[*nerr].e_code = code;
1500 (*err)[*nerr].e_addr = strdup( addr );
1501 (*err)[*nerr].e_msg = msg;
1506 add_group( char *dn, Group **list, int *nlist )
1511 for ( i = 0; i < *nlist; i++ ) {
1512 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1513 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1518 ufn = ldap_explode_dn( dn, 1 );
1519 namelen = strlen( ufn[0] );
1521 if ( *nlist == 0 ) {
1522 *list = (Group *) malloc( sizeof(Group) );
1524 *list = (Group *) realloc( *list, (*nlist + 1) *
1528 /* send errors to groupname-errors@host */
1529 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1531 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1532 (void) canonical( (*list)[*nlist].g_errorsto );
1534 /* send to groupname-members@host - make it a list for send_group */
1535 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1536 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1537 sizeof(MEMBERS) + hostlen + 2 );
1538 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1540 (void) canonical( (*list)[*nlist].g_members[0] );
1541 (*list)[*nlist].g_members[1] = NULL;
1543 /* save the group's dn so we can check for loops above */
1544 (*list)[*nlist].g_dn = strdup( dn );
1548 ldap_value_free( ufn );
1552 unbind_and_exit( int rc )
1556 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1557 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1563 canonical( char *s )
1567 for ( ; *s != '\0'; s++ ) {
1576 group_loop( char *dn )
1579 static char **groups;
1582 for ( i = 0; i < ngroups; i++ ) {
1583 if ( strcmp( dn, groups[i] ) == 0 )
1588 groups = (char **) malloc( sizeof(char *) );
1590 groups = (char **) realloc( groups,
1591 (ngroups + 1) * sizeof(char *) );
1593 groups[ngroups++] = strdup( dn );
1599 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1603 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1604 ldap_value_free( attr );
1608 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1609 ldap_value_free( attr );
1617 get_attributes_mail_dn(
1620 char *attr2 /* this one is dn-valued */
1623 LDAPMessage *ee, *res;
1624 char **vals, **dnlist, **mail, **grname;
1627 struct timeval timeout;
1629 dn = ldap_get_dn( ld, e );
1631 vals = ldap_get_values( ld, e, attr1 );
1632 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1635 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1636 timeout.tv_sec = MAIL500_TIMEOUT;
1637 timeout.tv_usec = 0;
1639 for ( i = 0; dnlist[i] != NULL; i++ ) {
1640 if ( (rc = ldap_search_st( ld, dnlist[i],
1641 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1642 &timeout, &res )) != LDAP_SUCCESS ) {
1643 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1644 unbind_and_exit( EX_TEMPFAIL );
1647 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1653 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1654 syslog( LOG_ALERT, "error parsing x500 entry" );
1658 if ( isgroup(ee) ) {
1661 grname = ldap_explode_dn( dnlist[i], 1 );
1663 /* groupname + host + @ + null */
1664 graddr[0] = (char *) malloc( strlen( grname[0] )
1665 + strlen( host ) + 2 );
1667 sprintf( graddr[0], "%s@%s", grname[0], host);
1668 (void) canonical( graddr[0] );
1670 add_to( &vals, &nto, graddr );
1673 ldap_value_free( grname );
1674 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1676 add_to( &vals, &nto, mail );
1678 ldap_value_free( mail );
1681 ldap_msgfree( res );