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.
17 #include <ac/stdlib.h>
20 #include <ac/signal.h>
21 #include <ac/string.h>
22 #include <ac/sysexits.h>
23 #include <ac/syslog.h>
25 #include <ac/unistd.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 #include <sys/resource.h>
41 #include "ldap_defaults.h"
43 #ifndef MAIL500_BOUNCEFROM
44 #define MAIL500_BOUNCEFROM "<>"
48 #define GROUP_ERRORS 0x02
49 #define GROUP_REQUEST 0x04
50 #define GROUP_MEMBERS 0x08
51 #define GROUP_OWNER 0x10
54 #define ERRORS "errors"
55 #define REQUEST "request"
56 #define REQUESTS "requests"
57 #define MEMBERS "members"
59 #define OWNERS "owners"
62 char *vacationhost = NULL;
63 char *errorsfrom = NULL;
64 char *mailfrom = NULL;
66 char *ldaphost = NULL;
72 #define E_USERUNKNOWN 1
78 #define E_JOINMEMBERNOEMAIL 7
79 #define E_MEMBERNOEMAIL 8
81 #define E_NOMEMBERS 10
83 #define E_GROUPUNKNOWN 12
89 #define e_msg e_union.e_u_msg
90 #define e_loop e_union.e_u_loop
93 typedef struct groupto {
99 typedef struct baseinfo {
100 char *b_dn; /* dn to start searching at */
101 char b_rdnpref; /* give rdn's preference when searching? */
102 int b_search; /* ORed with the type of thing the address */
103 /* looks like (USER, GROUP_ERRORS, etc.) */
104 /* to see if this should be searched */
105 char *b_filter[3]; /* filter to apply - name substituted for %s */
106 /* (up to three of them) */
110 {"ou=People, dc=OpenLDAP, dc=org",
112 {"uid=%s", "cn=%s", NULL}},
113 {"ou=System Groups, ou=Groups, dc=OpenLDAP, dc=org",
115 {"(&(cn=%s)(associatedDomain=%h))", NULL, NULL}},
116 {"ou=User Groups, ou=Groups, dc=OpenLDAP, dc=org",
118 {"(&(cn=%s)(associatedDomain=%h))", NULL, NULL}},
122 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
124 static char *attrs[] = { "objectClass", "title", "postaladdress",
125 "telephoneNumber", "mail", "description", "owner",
126 "errorsTo", "rfc822ErrorsTo", "requestsTo",
127 "rfc822RequestsTo", "joinable", "cn", "member",
128 "moderator", "onVacation", "uid",
129 "suppressNoEmailError", NULL };
131 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
132 static int do_group( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
133 static void do_group_members( LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr );
134 static void send_message( char **to );
135 static void send_errors( Error *err, int nerr );
136 static void do_noemail( FILE *fp, Error *err, int namelen );
137 static void do_ambiguous( FILE *fp, Error *err, int namelen );
138 static void add_to( char ***list, int *nlist, char **new );
139 static int isgroup( LDAPMessage *e );
140 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
141 static void add_group( char *dn, Group **list, int *nlist );
142 static void unbind_and_exit( int rc );
143 static int group_loop( char *dn );
144 static void send_group( Group *group, int ngroup );
145 static int has_attributes( LDAPMessage *e, char *attr1, char *attr2 );
146 static char **get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 );
147 static char *canonical( char *s );
148 static int connect_to_x500( void );
150 static void do_group_errors( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
151 static void do_group_request( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
152 static void do_group_owner( LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr );
153 static void add_member( char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, char **suppress );
156 main ( int argc, char **argv )
162 int numto, ngroups, numerr, nargs;
165 if ( (myname = strrchr( argv[0], '/' )) == NULL )
166 myname = strdup( argv[0] );
168 myname = strdup( myname + 1 );
171 (void) SIGNAL( SIGPIPE, SIG_IGN );
175 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
177 openlog( myname, OPENLOG_OPTIONS );
180 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
182 case 'd': /* turn on debugging */
183 debug = atoi( optarg );
186 case 'f': /* who it's from & where errors should go */
187 mailfrom = strdup( optarg );
188 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
189 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
190 sendmailargs[j+1] = mailfrom;
196 case 'h': /* hostname */
197 host = strdup( optarg );
198 hostlen = strlen(host);
201 case 'l': /* ldap host */
202 ldaphost = strdup( optarg );
205 /* mailer-daemon address - who we should */
206 case 'm': /* say errors come from */
207 errorsfrom = strdup( optarg );
210 case 'v': /* vacation host */
211 vacationhost = strdup( optarg );
215 syslog( LOG_ALERT, "unknown option" );
220 if ( mailfrom == NULL ) {
221 syslog( LOG_ALERT, "required argument -f not present" );
224 if ( errorsfrom == NULL ) {
225 syslog( LOG_ALERT, "required argument -m not present" );
228 if ( host == NULL ) {
229 syslog( LOG_ALERT, "required argument -h not present" );
233 if ( connect_to_x500() != 0 )
242 syslog( LOG_ALERT, "running as %d", geteuid() );
243 strcpy( buf, argv[0] );
244 for ( i = 1; i < argc; i++ ) {
246 strcat( buf, argv[i] );
249 syslog( LOG_ALERT, "args: (%s)", buf );
254 add_to( &tolist, &numto, sendmailargs );
256 ngroups = numerr = 0;
259 for ( i = optind; i < argc; i++ ) {
263 for ( j = 0; argv[i][j] != '\0'; j++ ) {
264 if ( argv[i][j] == '.' || argv[i][j] == '_' )
269 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
272 if ((strcasecmp(s, ERROR) == 0) ||
273 (strcasecmp(s, ERRORS) == 0)) {
276 } else if ((strcasecmp(s, REQUEST) == 0) ||
277 (strcasecmp(s, REQUESTS) == 0)) {
278 type = GROUP_REQUEST;
280 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
281 type = GROUP_MEMBERS;
283 } else if ((strcasecmp(s, OWNER) == 0) ||
284 (strcasecmp(s, OWNERS) == 0)) {
290 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
291 &errlist, &numerr, type );
295 * If we have both errors and successful deliveries to make or if
296 * if there are any groups to deliver to, we basically need to read
297 * the message twice. So, we have to put it in a tmp file.
300 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
305 if ( (fp = tmpfile()) == NULL ) {
306 syslog( LOG_ALERT, "could not open tmp file" );
307 unbind_and_exit( EX_TEMPFAIL );
310 /* copy the message to a temp file */
311 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
312 if ( fputs( buf, fp ) == EOF ) {
313 syslog( LOG_ALERT, "error writing tmpfile" );
314 unbind_and_exit( EX_TEMPFAIL );
318 if ( dup2( fileno( fp ), 0 ) == -1 ) {
319 syslog( LOG_ALERT, "could not dup2 tmpfile" );
320 unbind_and_exit( EX_TEMPFAIL );
326 /* deal with errors */
329 syslog( LOG_ALERT, "sending errors" );
331 (void) rewind( stdin );
332 send_errors( errlist, numerr );
335 (void) ldap_unbind( ld );
337 /* send to groups with errorsTo */
340 syslog( LOG_ALERT, "sending to groups with errorsto" );
342 (void) rewind( stdin );
343 send_group( togroups, ngroups );
346 /* send to expanded aliases and groups w/o errorsTo */
347 if ( numto > nargs ) {
349 syslog( LOG_ALERT, "sending to aliases and groups" );
351 (void) rewind( stdin );
352 send_message( tolist );
359 connect_to_x500( void )
363 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
364 syslog( LOG_ALERT, "ldap_init failed" );
368 opt = MAIL500_MAXAMBIGUOUS;
369 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
370 opt = LDAP_DEREF_ALWAYS;
371 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
373 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
374 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
382 mailcmp( char *a, char *b )
386 for ( i = 0; a[i] != '\0'; i++ ) {
387 if ( a[i] != b[i] ) {
392 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
418 LDAPMessage *e, *res;
419 struct timeval timeout;
422 char realfilter[1024];
423 char **mail, **onvacation = NULL, **uid = NULL;
426 * Look up the name in X.500, add the appropriate addresses found
427 * to the to list, or to the err list in case of error. Groups are
428 * handled by the do_group routine, individuals are handled here.
429 * When looking up name, we follow the bases hierarchy, looking
430 * in base[0] first, then base[1], etc. For each base, there is
431 * a set of search filters to try, in order. If something goes
432 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
433 * If the b_rdnpref flag is set, then we give preference to entries
434 * that matched name because it's their rdn, otherwise not.
437 timeout.tv_sec = MAIL500_TIMEOUT;
439 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
440 if ( ! (base[b].b_search & type) ) {
443 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
444 char *format, *p, *s, *d;
448 for ( argc = 0; argc < 3; argc++ ) {
452 format = strdup( base[b].b_filter[f] );
453 for ( argc = 0, p = format; *p; p++ ) {
456 case 's': /* %s is the name */
460 case 'h': /* %h is the host */
467 "unknown format %c", *p );
475 /* three names ought to do... */
476 sprintf( filter, format, argv[0], argv[1], argv[2] );
478 for ( s = filter, d = realfilter; *s; s++, d++ ) {
487 rc = ldap_search_st( ld, base[b].b_dn,
488 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
491 /* some other trouble - try again later */
492 if ( rc != LDAP_SUCCESS &&
493 rc != LDAP_SIZELIMIT_EXCEEDED ) {
494 syslog( LOG_ALERT, "return 0x%x from X.500",
496 unbind_and_exit( EX_TEMPFAIL );
499 if ( (match = ldap_count_entries( ld, res )) != 0 )
509 /* trouble - try again later */
511 syslog( LOG_ALERT, "error parsing result from X.500" );
512 unbind_and_exit( EX_TEMPFAIL );
515 /* no matches - bounce with user unknown */
517 if ( type == USER ) {
518 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
520 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
525 /* more than one match - bounce with ambiguous user? */
527 LDAPMessage *next, *tmpres = NULL;
531 /* not giving rdn preference - bounce with ambiguous user */
532 if ( base[b].b_rdnpref == 0 ) {
533 add_error( err, nerr, E_AMBIGUOUS, name, res );
538 * giving rdn preference - see if any entries were matched
539 * because of their rdn. If so, collect them to deal with
540 * later (== 1 we deliver, > 1 we bounce).
543 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
544 next = ldap_next_entry( ld, e );
545 dn = ldap_get_dn( ld, e );
546 xdn = ldap_explode_dn( dn, 1 );
548 /* XXX bad, but how else can we do it? XXX */
549 if ( strcasecmp( xdn[0], name ) == 0 ) {
550 ldap_delete_result_entry( &res, e );
551 ldap_add_result_entry( &tmpres, e );
554 ldap_value_free( xdn );
558 /* nothing matched by rdn - go ahead and bounce */
559 if ( tmpres == NULL ) {
560 add_error( err, nerr, E_AMBIGUOUS, name, res );
563 /* more than one matched by rdn - bounce with rdn matches */
564 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
565 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
569 } else if ( match < 0 ) {
570 syslog( LOG_ALERT, "error parsing result from X.500" );
571 unbind_and_exit( EX_TEMPFAIL );
574 /* otherwise one matched by rdn - send to it */
580 * if we get this far, it means that we found a single match for
581 * name. for a user, we deliver to the mail attribute or bounce
582 * with address and phone if no mail attr. for a group, we
583 * deliver to all members or bounce to rfc822ErrorsTo if no members.
587 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
588 syslog( LOG_ALERT, "error parsing entry from X.500" );
589 unbind_and_exit( EX_TEMPFAIL );
592 dn = ldap_get_dn( ld, e );
594 if ( type == GROUP_ERRORS ) {
595 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
596 do_group_errors( e, dn, to, nto, err, nerr );
598 } else if ( type == GROUP_REQUEST ) {
599 /* sent to group-request - resend to [rfc822]RequestsTo attr */
600 do_group_request( e, dn, to, nto, err, nerr );
602 } else if ( type == GROUP_MEMBERS ) {
603 /* sent to group-members - expand */
604 do_group_members( e, dn, to, nto, togroups, ngroups, err,
607 } else if ( type == GROUP_OWNER ) {
608 /* sent to group-owner - resend to owner attr */
609 do_group_owner( e, dn, to, nto, err, nerr );
611 } else if ( isgroup( e ) ) {
613 * sent to group - resend from [rfc822]ErrorsTo if it's there,
614 * otherwise, expand the group
617 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
623 * sent to user - mail attribute => add it to the to list,
626 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
631 /* try to detect simple mail loops */
632 sprintf( buf, "%s@%s", name, host );
633 for ( i = 0; mail[i] != NULL; i++ ) {
635 * address is the same as the one we're
636 * sending to - mail loop. syslog the
637 * problem, bounce a message back to the
638 * sender (who else?), and delete the bogus
639 * addr from the list.
642 if ( (h = strchr( mail[i], '@' )) != NULL ) {
644 if ( strcasecmp( h, host ) == 0 ) {
646 "potential loop detected (%s)",
651 if ( mailcmp( buf, mail[i] ) == 0 ) {
653 "loop detected (%s)", mail[i] );
655 /* remove the bogus address */
656 for ( j = i; mail[j] != NULL; j++ ) {
661 if ( mail[0] != NULL ) {
662 add_to( to, nto, mail );
664 add_error( err, nerr, E_NOEMAIL, name, res );
667 ldap_value_free( mail );
669 add_error( err, nerr, E_NOEMAIL, name, res );
673 * If the user is on vacation, send a copy of the mail to
674 * the vacation server. The address is constructed from
675 * the vacationhost (set in a command line argument) and
676 * the uid (XXX this should be more general XXX).
679 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
680 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
685 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
686 sprintf( buf, "%s@%s", uid[0], vacationhost );
691 add_to( to, nto, vaddr );
694 "user without a uid on vacation (%s)",
700 if ( onvacation != NULL ) {
701 ldap_value_free( onvacation );
704 ldap_value_free( uid );
725 * If this group has an rfc822ErrorsTo attribute, we need to
726 * arrange for errors involving this group to go there, not
727 * to the sender. Since sendmail only has the concept of a
728 * single sender, we arrange for errors to go to groupname-errors,
729 * which we then handle specially when (if) it comes back to us
730 * by expanding to all the rfc822ErrorsTo addresses. If it has no
731 * rfc822ErrorsTo attribute, we call do_group_members() to expand
735 if ( group_loop( dn ) ) {
740 * check for moderated groups - if the group has a moderator
741 * attribute, we check to see if the from address is one of
742 * the moderator values. if so, continue on. if not, arrange
743 * to send the mail to the moderator(s). need to do this before
744 * we change the from below.
747 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
748 /* check if it came from any of the group's moderators */
749 for ( i = 0; moderator[i] != NULL; i++ ) {
750 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
754 /* not from the moderator? */
755 if ( moderator[i] == NULL ) {
756 add_to( to, nto, moderator );
757 ldap_value_free( moderator );
761 /* else from the moderator - fall through and deliver it */
764 if (strcmp(MAIL500_BOUNCEFROM, mailfrom) != 0 &&
765 has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
766 add_group( dn, togroups, ngroups );
771 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
789 int i, rc, anymembers;
791 char **mail, **member, **joinable, **suppress;
793 LDAPMessage *ee, *res;
794 struct timeval timeout;
798 * if all has gone according to plan, we've already arranged for
799 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
800 * so all we have to do here is arrange to send to the
801 * rfc822Mailbox attribute, the member attribute, and anyone who
802 * has joined the group by setting memberOfGroup equal to the
806 /* add members in the group itself - mail attribute */
808 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
810 add_to( to, nto, mail );
812 ldap_value_free( mail );
815 /* add members in the group itself - member attribute */
816 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
817 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
819 for ( i = 0; member[i] != NULL; i++ ) {
820 if ( strcasecmp( dn, member[i] ) == 0 ) {
821 syslog( LOG_ALERT, "group (%s) contains itself",
825 add_member( dn, member[i], to, nto, togroups,
826 ngroups, err, nerr, suppress );
830 ldap_value_free( suppress );
832 ldap_value_free( member );
835 /* add members who have joined by setting memberOfGroup */
836 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
837 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
838 if ( ! anymembers ) {
839 add_error( err, nerr, E_NOMEMBERS, dn,
843 ldap_value_free( joinable );
846 ldap_value_free( joinable );
848 sprintf( filter, "(memberOfGroup=%s)", dn );
850 timeout.tv_sec = MAIL500_TIMEOUT;
853 /* for each subtree to look in... */
854 opt = MAIL500_MAXAMBIGUOUS;
855 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
856 for ( i = 0; base[i].b_dn != NULL; i++ ) {
857 /* find entries that have joined this group... */
858 rc = ldap_search_st( ld, base[i].b_dn,
859 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
862 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
863 rc == LDAP_TIMELIMIT_EXCEEDED ) {
865 "group search limit exceeded %d", rc );
866 unbind_and_exit( EX_TEMPFAIL );
869 if ( rc != LDAP_SUCCESS ) {
870 syslog( LOG_ALERT, "group search return 0x%x",
872 unbind_and_exit( EX_TEMPFAIL );
875 /* for each entry that has joined... */
876 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
877 ee = ldap_next_entry( ld, ee ) ) {
879 if ( isgroup( ee ) ) {
880 ndn = ldap_get_dn( ld, ee );
882 if ( do_group( e, ndn, to, nto,
883 togroups, ngroups, err, nerr )
886 "group loop (%s) (%s)",
895 /* add them to the to list */
896 if ( (mail = ldap_get_values( ld, ee, "mail" ))
898 add_to( to, nto, mail );
900 ldap_value_free( mail );
902 /* else generate a bounce */
904 ndn = ldap_get_dn( ld, ee );
906 add_error( err, nerr,
907 E_JOINMEMBERNOEMAIL, ndn, NULL );
915 opt = MAIL500_MAXAMBIGUOUS;
916 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
919 if ( ! anymembers ) {
920 add_error( err, nerr, E_NOMEMBERS, dn, NULL );
940 LDAPMessage *res, *e;
941 struct timeval timeout;
943 timeout.tv_sec = MAIL500_TIMEOUT;
945 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
946 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
947 if ( rc == LDAP_NO_SUCH_OBJECT ) {
948 add_error( err, nerr, E_BADMEMBER, dn, NULL );
952 syslog( LOG_ALERT, "member search return 0x%x", rc );
954 unbind_and_exit( EX_TEMPFAIL );
958 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
959 syslog( LOG_ALERT, "member search error parsing entry" );
961 unbind_and_exit( EX_TEMPFAIL );
963 ndn = ldap_get_dn( ld, e );
965 /* allow groups within groups */
966 if ( isgroup( e ) ) {
967 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
969 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
977 /* send to the member's mail attribute */
978 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
979 add_to( to, nto, mail );
981 ldap_value_free( mail );
983 /* else generate a bounce */
985 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
987 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULL );
1006 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
1007 "requestsTo" )) != NULL ) {
1008 add_to( to, nto, requeststo );
1010 ldap_value_free( requeststo );
1012 add_error( err, nerr, E_NOREQUEST, dn, NULL );
1028 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1029 "errorsTo" )) != NULL ) {
1030 add_to( to, nto, errorsto );
1032 ldap_value_free( errorsto );
1034 add_error( err, nerr, E_NOERRORS, dn, NULL );
1050 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1051 add_to( to, nto, owner );
1052 ldap_value_free( owner );
1054 add_error( err, nerr, E_NOOWNER, dn, NULL );
1059 send_message( char **to )
1062 #ifndef HAVE_WAITPID
1063 WAITSTATUSTYPE status;
1070 strcpy( buf, to[0] );
1071 for ( i = 1; to[i] != NULL; i++ ) {
1073 strcat( buf, to[i] );
1076 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1080 if ( (pid = fork()) != 0 ) {
1082 waitpid( pid, (int *) NULL, 0 );
1084 wait4( pid, &status, WAIT_FLAGS, 0 );
1088 /* to includes sendmailargs */
1089 execv( MAIL500_SENDMAIL, to );
1091 syslog( LOG_ALERT, "execv failed" );
1093 exit( EX_TEMPFAIL );
1098 send_group( Group *group, int ngroup )
1104 #ifndef HAVE_WAITPID
1105 WAITSTATUSTYPE status;
1108 for ( i = 0; i < ngroup; i++ ) {
1109 (void) rewind( stdin );
1111 iargv[0] = MAIL500_SENDMAIL;
1113 iargv[2] = group[i].g_errorsto;
1114 iargv[3] = "-oMrX.500";
1121 add_to( &argv, &argc, iargv );
1122 add_to( &argv, &argc, group[i].g_members );
1128 strcpy( buf, argv[0] );
1129 for ( i = 1; i < argc; i++ ) {
1131 strcat( buf, argv[i] );
1134 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1138 if ( (pid = fork()) != 0 ) {
1140 waitpid( pid, (int *) NULL, 0 );
1142 wait4( pid, &status, WAIT_FLAGS, 0 );
1146 execv( MAIL500_SENDMAIL, argv );
1148 syslog( LOG_ALERT, "execv failed" );
1150 exit( EX_TEMPFAIL );
1156 send_errors( Error *err, int nerr )
1158 int pid, i, namelen;
1163 #ifndef HAVE_WAITPID
1164 WAITSTATUSTYPE status;
1167 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1168 mailfrom = errorsfrom;
1171 argv[0] = MAIL500_SENDMAIL;
1172 argv[1] = "-oMrX.500";
1176 argv[5] = MAIL500_BOUNCEFROM;
1183 strcpy( buf, argv[0] );
1184 for ( i = 1; argv[i] != NULL; i++ ) {
1186 strcat( buf, argv[i] );
1189 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1192 if ( pipe( fd ) == -1 ) {
1193 syslog( LOG_ALERT, "cannot create pipe" );
1194 exit( EX_TEMPFAIL );
1197 if ( (pid = fork()) != 0 ) {
1198 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1199 syslog( LOG_ALERT, "cannot fdopen pipe" );
1200 exit( EX_TEMPFAIL );
1203 fprintf( fp, "To: %s\n", mailfrom );
1204 fprintf( fp, "From: %s\n", errorsfrom );
1205 fprintf( fp, "Subject: undeliverable mail\n" );
1206 fprintf( fp, "\n" );
1207 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1208 for ( i = 0; i < nerr; i++ ) {
1209 namelen = strlen( err[i].e_addr );
1210 fprintf( fp, "\n" );
1212 switch ( err[i].e_code ) {
1214 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1217 case E_GROUPUNKNOWN:
1218 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1222 fprintf( fp, "%s: Group member does not exist\n",
1224 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1225 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1226 fprintf( fp, "then re-adding the person to the group.\n" );
1230 fprintf( fp, "%s: Group exists but has no request address\n",
1235 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1240 fprintf( fp, "%s: Group exists but has no owner\n",
1245 do_ambiguous( fp, &err[i], namelen );
1249 do_noemail( fp, &err[i], namelen );
1252 case E_MEMBERNOEMAIL:
1253 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1257 case E_JOINMEMBERNOEMAIL:
1258 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1263 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1264 err[i].e_addr, err[i].e_loop );
1268 fprintf( fp, "%s: Group has no members\n",
1273 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1274 unbind_and_exit( EX_TEMPFAIL );
1279 fprintf( fp, "\n------- The original message sent:\n\n" );
1281 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1287 waitpid( pid, (int *) NULL, 0 );
1289 wait4( pid, &status, WAIT_FLAGS, 0 );
1294 execv( MAIL500_SENDMAIL, argv );
1296 syslog( LOG_ALERT, "execv failed" );
1298 exit( EX_TEMPFAIL );
1303 do_noemail( FILE *fp, Error *err, int namelen )
1309 fprintf(fp, "%s: User has no email address registered.\n",
1311 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1312 namelen, " ", err->e_addr );
1315 dn = ldap_get_dn( ld, err->e_msg );
1316 ufn = ldap_explode_dn( dn, 1 );
1317 rdn = strdup( ufn[0] );
1318 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1319 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1321 for ( i = 0; vals[i]; i++ ) {
1322 last = strlen( vals[i] ) - 1;
1323 if ( isdigit((unsigned char) vals[i][last]) ) {
1324 rdn = strdup( vals[i] );
1329 ldap_value_free( vals );
1332 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1335 ldap_value_free( ufn );
1337 /* titles or descriptions */
1338 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1339 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1341 fprintf( fp, "%*s No title or description registered\n",
1344 for ( i = 0; vals[i] != NULL; i++ ) {
1345 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1348 ldap_value_free( vals );
1351 /* postal address */
1352 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1354 fprintf( fp, "%*s No postal address registered\n", namelen,
1357 fprintf( fp, "%*s ", namelen, " " );
1358 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1359 if ( vals[0][i] == '$' ) {
1360 fprintf( fp, "\n%*s ", namelen, " " );
1361 while ( isspace((unsigned char) vals[0][i+1]) )
1364 fprintf( fp, "%c", vals[0][i] );
1367 fprintf( fp, "\n" );
1369 ldap_value_free( vals );
1372 /* telephone number */
1373 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1375 fprintf( fp, "%*s No phone number registered\n", namelen,
1378 for ( i = 0; vals[i] != NULL; i++ ) {
1379 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1382 ldap_value_free( vals );
1388 do_ambiguous( FILE *fp, Error *err, int namelen )
1395 i = ldap_result2error( ld, err->e_msg, 0 );
1397 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1398 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1399 ldap_count_entries( ld, err->e_msg ) );
1401 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1402 e = ldap_next_entry( ld, e ) ) {
1403 dn = ldap_get_dn( ld, e );
1404 ufn = ldap_explode_dn( dn, 1 );
1405 rdn = strdup( ufn[0] );
1406 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1407 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1408 for ( i = 0; vals[i]; i++ ) {
1409 last = strlen( vals[i] ) - 1;
1410 if (isdigit((unsigned char) vals[i][last])) {
1411 rdn = strdup( vals[i] );
1416 ldap_value_free( vals );
1420 if ( isgroup( e ) ) {
1421 vals = ldap_get_values( ld, e, "description" );
1423 vals = ldap_get_values( ld, e, "title" );
1426 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1427 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1428 fprintf( fp, " %s\n", vals[i] );
1433 ldap_value_free( ufn );
1435 ldap_value_free( vals );
1440 count_values( char **list )
1444 for ( i = 0; list && list[i] != NULL; i++ )
1451 add_to( char ***list, int *nlist, char **new )
1453 int i, nnew, oldnlist;
1455 nnew = count_values( new );
1458 if ( *list == NULL || *nlist == 0 ) {
1459 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1462 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1463 nnew * sizeof(char *) + sizeof(char *) );
1467 for ( i = 0; i < nnew; i++ )
1468 (*list)[i + oldnlist] = strdup( new[i] );
1469 (*list)[*nlist] = NULL;
1473 isgroup( LDAPMessage *e )
1478 oclist = ldap_get_values( ld, e, "objectClass" );
1480 for ( i = 0; oclist[i] != NULL; i++ ) {
1481 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1482 ldap_value_free( oclist );
1486 ldap_value_free( oclist );
1492 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1495 *err = (Error *) malloc( sizeof(Error) );
1497 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1500 (*err)[*nerr].e_code = code;
1501 (*err)[*nerr].e_addr = strdup( addr );
1502 (*err)[*nerr].e_msg = msg;
1507 add_group( char *dn, Group **list, int *nlist )
1512 for ( i = 0; i < *nlist; i++ ) {
1513 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1514 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1519 ufn = ldap_explode_dn( dn, 1 );
1520 namelen = strlen( ufn[0] );
1522 if ( *nlist == 0 ) {
1523 *list = (Group *) malloc( sizeof(Group) );
1525 *list = (Group *) realloc( *list, (*nlist + 1) *
1529 /* send errors to groupname-errors@host */
1530 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1532 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1533 (void) canonical( (*list)[*nlist].g_errorsto );
1535 /* send to groupname-members@host - make it a list for send_group */
1536 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1537 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1538 sizeof(MEMBERS) + hostlen + 2 );
1539 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1541 (void) canonical( (*list)[*nlist].g_members[0] );
1542 (*list)[*nlist].g_members[1] = NULL;
1544 /* save the group's dn so we can check for loops above */
1545 (*list)[*nlist].g_dn = strdup( dn );
1549 ldap_value_free( ufn );
1553 unbind_and_exit( int rc )
1557 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1558 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1564 canonical( char *s )
1568 for ( ; *s != '\0'; s++ ) {
1577 group_loop( char *dn )
1580 static char **groups;
1583 for ( i = 0; i < ngroups; i++ ) {
1584 if ( strcmp( dn, groups[i] ) == 0 )
1589 groups = (char **) malloc( sizeof(char *) );
1591 groups = (char **) realloc( groups,
1592 (ngroups + 1) * sizeof(char *) );
1594 groups[ngroups++] = strdup( dn );
1600 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1604 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1605 ldap_value_free( attr );
1609 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1610 ldap_value_free( attr );
1618 get_attributes_mail_dn(
1621 char *attr2 /* this one is dn-valued */
1624 LDAPMessage *ee, *res;
1625 char **vals, **dnlist, **mail, **grname;
1628 struct timeval timeout;
1630 dn = ldap_get_dn( ld, e );
1632 vals = ldap_get_values( ld, e, attr1 );
1633 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1636 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1637 timeout.tv_sec = MAIL500_TIMEOUT;
1638 timeout.tv_usec = 0;
1640 for ( i = 0; dnlist[i] != NULL; i++ ) {
1641 if ( (rc = ldap_search_st( ld, dnlist[i],
1642 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1643 &timeout, &res )) != LDAP_SUCCESS ) {
1644 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1645 unbind_and_exit( EX_TEMPFAIL );
1648 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1654 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1655 syslog( LOG_ALERT, "error parsing x500 entry" );
1659 if ( isgroup(ee) ) {
1662 grname = ldap_explode_dn( dnlist[i], 1 );
1664 /* groupname + host + @ + null */
1665 graddr[0] = (char *) malloc( strlen( grname[0] )
1666 + strlen( host ) + 2 );
1668 sprintf( graddr[0], "%s@%s", grname[0], host);
1669 (void) canonical( graddr[0] );
1671 add_to( &vals, &nto, graddr );
1674 ldap_value_free( grname );
1675 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1677 add_to( &vals, &nto, mail );
1679 ldap_value_free( mail );
1682 ldap_msgfree( res );