3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
13 * Copyright 1998,1999 The OpenLDAP Foundation
14 * COPYING RESTRICTIONS APPLY. See COPYRIGHT File in top level directory
15 * of this package for details.
22 #include <ac/stdlib.h>
26 #include <ac/signal.h>
27 #include <ac/string.h>
28 #include <ac/sysexits.h>
29 #include <ac/syslog.h>
31 #include <ac/unistd.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
42 #include "ldap_defaults.h"
44 #ifndef MAIL500_BOUNCEFROM
45 #define MAIL500_BOUNCEFROM "<>"
49 #define GROUP_ERRORS 0x02
50 #define GROUP_REQUEST 0x04
51 #define GROUP_MEMBERS 0x08
52 #define GROUP_OWNER 0x10
55 #define ERRORS "errors"
56 #define REQUEST "request"
57 #define REQUESTS "requests"
58 #define MEMBERS "members"
60 #define OWNERS "owners"
63 char *vacationhost = NULL;
64 char *errorsfrom = MAIL500_BOUNCEFROM;
65 char *mailfrom = NULL;
67 char *ldaphost = NULL;
73 #define E_USERUNKNOWN 1
79 #define E_JOINMEMBERNOEMAIL 7
80 #define E_MEMBERNOEMAIL 8
82 #define E_NOMEMBERS 10
84 #define E_GROUPUNKNOWN 12
85 #define E_NOOWNADDRESS 13
91 #define e_msg e_union.e_u_msg
92 #define e_loop e_union.e_u_loop
95 typedef struct groupto {
102 typedef struct baseinfo {
105 char b_rdnpref; /* give rdn's preference when searching? */
106 int b_search; /* ORed with the type of thing the address */
107 /* looks like (USER, GROUP_ERRORS, etc.) */
108 /* to see if this should be searched */
112 * We should limit the search to objectclass=mailRecipient or
113 * objectclass=mailGroup.
118 {"dc=StlInter, dc=Net",
120 {"mail=%s", "mailAlternateAddress=%s", NULL}},
127 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
129 typedef struct attr_semantics {
131 int as_m_valued; /* Is multivalued? */
132 int as_priority; /* Priority level of this attribut type */
133 int as_syntax; /* How to interpret values */
134 int as_m_entries; /* Can resolve to several entries? */
135 int as_kind; /* Recipient, sender, etc. */
136 char *as_param; /* Extra info for filters and things alike */
139 #define AS_SYNTAX_UNKNOWN 0
140 #define AS_SYNTAX_NATIVE_MB 1 /* Unqualified mailbox name */
141 #define AS_SYNTAX_RFC822 2 /* RFC822 mail address */
142 #define AS_SYNTAX_HOST 3
143 #define AS_SYNTAX_DN 4 /* A directory entry */
144 #define AS_SYNTAX_RFC822_EXT 5
145 #define AS_SYNTAX_URL 6 /* mailto: or ldap: URL */
146 #define AS_SYNTAX_BOOL_FILTER 7 /* For joinable, filter in as_param */
147 #define AS_SYNTAX_PRESENT 8 /* Value irrelevant, only presence is
150 #define AS_KIND_UNKNOWN 0
151 #define AS_KIND_RECIPIENT 1
152 #define AS_KIND_ERRORS 2 /* For ErrorsTo and similar */
153 #define AS_KIND_REQUEST 3
154 #define AS_KIND_OWNER 4
155 #define AS_KIND_ROUTE_TO_HOST 5 /* Expand at some other host */
156 #define AS_KIND_ALLOWED_SENDER 6 /* Can send to group */
157 #define AS_KIND_MODERATOR 7
158 #define AS_KIND_ROUTE_TO_ADDR 8 /* Rewrite recipient address as */
159 #define AS_KIND_OWN_ADDR 9 /* RFC822 name of this entry */
161 AttrSemantics **attr_semantics = NULL;
162 int current_priority = 0;
164 typedef struct subst {
169 char **groupclasses = NULL;
170 char **def_attr = NULL;
171 char **mydomains = NULL; /* FQDNs not to route elsewhere */
173 static void load_config( char *filespec );
174 static void split_address( char *address, char **localpart, char **domainpart);
175 static int entry_engine( LDAPMessage *e, char *dn, char *address, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
176 static void do_address( char *name, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
177 static void send_message( char **to );
178 static void send_errors( Error *err, int nerr );
179 static void do_noemail( FILE *fp, Error *err, int namelen );
180 static void do_ambiguous( FILE *fp, Error *err, int namelen );
181 static int count_values( char **list );
182 static void add_to( char ***list, int *nlist, char **new );
183 static void add_single_to( char ***list, char *new );
184 static int isgroup( LDAPMessage *e );
185 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
186 static void unbind_and_exit( int rc ) LDAP_GCCATTR((noreturn));
187 static void send_group( Group **group, int ngroup );
189 static int connect_to_x500( void );
193 main ( int argc, char **argv )
199 int numto, ngroups, numerr, nargs;
201 char *conffile = NULL;
203 if ( (myname = strrchr( argv[0], '/' )) == NULL )
204 myname = strdup( argv[0] );
206 myname = strdup( myname + 1 );
209 (void) SIGNAL( SIGPIPE, SIG_IGN );
213 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
215 openlog( myname, OPENLOG_OPTIONS );
218 while ( (i = getopt( argc, argv, "d:C:f:h:l:m:v:" )) != EOF ) {
220 case 'd': /* turn on debugging */
221 debug = atoi( optarg );
224 case 'C': /* path to configuration file */
225 conffile = strdup( optarg );
228 case 'f': /* who it's from & where errors should go */
229 mailfrom = strdup( optarg );
230 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
231 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
232 sendmailargs[j+1] = mailfrom;
238 case 'h': /* hostname */
239 host = strdup( optarg );
240 hostlen = strlen(host);
243 case 'l': /* ldap host */
244 ldaphost = strdup( optarg );
247 /* mailer-daemon address - who we should */
248 case 'm': /* say errors come from */
249 errorsfrom = strdup( optarg );
252 case 'v': /* vacation host */
253 vacationhost = strdup( optarg );
257 syslog( LOG_ALERT, "unknown option" );
262 if ( mailfrom == NULL ) {
263 syslog( LOG_ALERT, "required argument -f not present" );
266 if ( errorsfrom == NULL ) {
267 syslog( LOG_ALERT, "required argument -m not present" );
270 /* if ( host == NULL ) { */
271 /* syslog( LOG_ALERT, "required argument -h not present" ); */
272 /* exit( EX_TEMPFAIL ); */
274 if ( conffile == NULL ) {
275 syslog( LOG_ALERT, "required argument -C not present" );
279 load_config( conffile );
281 if ( connect_to_x500() != 0 )
290 syslog( LOG_ALERT, "running as %d", geteuid() );
291 strcpy( buf, argv[0] );
292 for ( i = 1; i < argc; i++ ) {
294 strcat( buf, argv[i] );
297 syslog( LOG_ALERT, "args: (%s)", buf );
302 add_to( &tolist, &numto, sendmailargs );
304 ngroups = numerr = 0;
307 for ( i = optind; i < argc; i++ ) {
310 char *localpart = NULL, *domainpart = NULL;
314 split_address( argv[i], &localpart, &domainpart );
315 if ( (s = strrchr( localpart, '-' )) != NULL ) {
318 if ((strcasecmp(s, ERROR) == 0) ||
319 (strcasecmp(s, ERRORS) == 0)) {
322 } else if ((strcasecmp(s, REQUEST) == 0) ||
323 (strcasecmp(s, REQUESTS) == 0)) {
324 type = GROUP_REQUEST;
326 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
327 type = GROUP_MEMBERS;
329 } else if ((strcasecmp(s, OWNER) == 0) ||
330 (strcasecmp(s, OWNERS) == 0)) {
337 sprintf( address, "%s@%s", localpart, domainpart );
341 sprintf( address, "%s@%s", localpart, domainpart );
344 do_address( address, &tolist, &numto, &togroups, &ngroups,
345 &errlist, &numerr, type );
349 * If we have both errors and successful deliveries to make or if
350 * if there are any groups to deliver to, we basically need to read
351 * the message twice. So, we have to put it in a tmp file.
354 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
359 if ( (fp = tmpfile()) == NULL ) {
360 syslog( LOG_ALERT, "could not open tmp file" );
361 unbind_and_exit( EX_TEMPFAIL );
364 /* copy the message to a temp file */
365 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
366 if ( fputs( buf, fp ) == EOF ) {
367 syslog( LOG_ALERT, "error writing tmpfile" );
368 unbind_and_exit( EX_TEMPFAIL );
372 if ( dup2( fileno( fp ), 0 ) == -1 ) {
373 syslog( LOG_ALERT, "could not dup2 tmpfile" );
374 unbind_and_exit( EX_TEMPFAIL );
380 /* deal with errors */
383 syslog( LOG_ALERT, "sending errors" );
385 (void) rewind( stdin );
386 send_errors( errlist, numerr );
389 (void) ldap_unbind( ld );
391 /* send to groups with errorsTo */
394 syslog( LOG_ALERT, "sending to groups with errorsto" );
396 (void) rewind( stdin );
397 send_group( togroups, ngroups );
400 /* send to expanded aliases and groups w/o errorsTo */
401 if ( numto > nargs ) {
403 syslog( LOG_ALERT, "sending to aliases and groups" );
405 (void) rewind( stdin );
406 send_message( tolist );
413 get_config_line( FILE *cf, int *lineno)
415 static char buf[2048];
421 room = sizeof( buf );
422 while ( fgets( &buf[pos], room, cf ) ) {
425 /* Delete whitespace at the beginning of new data */
426 if ( isspace( buf[pos] ) ) {
428 for ( s = buf+pos; isspace(*s); s++ )
430 for ( d = buf+pos; *s; s++, d++ ) {
437 if ( buf[len-1] != '\n' ) {
438 syslog( LOG_ALERT, "Definition too long at line %d",
444 if ( strspn( buf, " \t\n" ) == len )
446 if ( buf[len-2] == '\\' ) {
448 room = sizeof(buf) - pos;
451 /* We have a real line, we will exit the loop */
459 add_url ( char *url, int rdnpref, int typemask )
465 b = calloc(1, sizeof(Base));
467 syslog( LOG_ALERT, "Out of memory" );
470 b->b_url = strdup( url );
471 b->b_rdnpref = rdnpref;
472 b->b_search = typemask;
474 if ( base == NULL ) {
475 base = calloc(2, sizeof(LDAPURLDesc *));
477 syslog( LOG_ALERT, "Out of memory" );
482 for ( size = 0; base[size]; size++ )
485 list_temp = realloc( base, size*sizeof(LDAPURLDesc *) );
487 syslog( LOG_ALERT, "Out of memory" );
497 add_def_attr( char *s )
503 p += strspn( p, "\t," );
504 q = strpbrk( p, " \t," );
507 add_single_to( &def_attr, p );
509 add_single_to( &def_attr, p );
517 add_attr_semantics( char *s )
522 as = calloc( 1, sizeof( AttrSemantics ) );
523 as->as_priority = current_priority;
525 while ( isspace ( *p ) )
528 while ( !isspace ( *q ) && *q != '\0' )
531 as->as_name = strdup( p );
535 while ( isspace ( *p ) )
538 while ( !isspace ( *q ) && *q != '\0' )
541 if ( !strcasecmp( p, "multivalued" ) ) {
543 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
544 as->as_m_entries = 1;
545 } else if ( !strcasecmp( p, "local-native-mailbox" ) ) {
546 as->as_syntax = AS_SYNTAX_NATIVE_MB;
547 } else if ( !strcasecmp( p, "rfc822" ) ) {
548 as->as_syntax = AS_SYNTAX_RFC822;
549 } else if ( !strcasecmp( p, "rfc822-extended" ) ) {
550 as->as_syntax = AS_SYNTAX_RFC822_EXT;
551 } else if ( !strcasecmp( p, "dn" ) ) {
552 as->as_syntax = AS_SYNTAX_DN;
553 } else if ( !strcasecmp( p, "url" ) ) {
554 as->as_syntax = AS_SYNTAX_URL;
555 } else if ( !strncasecmp( p, "search-with-filter=", 19 ) ) {
556 as->as_syntax = AS_SYNTAX_BOOL_FILTER;
557 q = strchr( p, '=' );
560 while ( *q && !isspace( *q ) ) {
565 as->as_param = strdup( p );
568 as->as_param = strdup( p );
573 "Missing filter in %s", s );
576 } else if ( !strcasecmp( p, "host" ) ) {
577 as->as_kind = AS_SYNTAX_HOST;
578 } else if ( !strcasecmp( p, "present" ) ) {
579 as->as_kind = AS_SYNTAX_PRESENT;
580 } else if ( !strcasecmp( p, "route-to-host" ) ) {
581 as->as_kind = AS_KIND_ROUTE_TO_HOST;
582 } else if ( !strcasecmp( p, "route-to-address" ) ) {
583 as->as_kind = AS_KIND_ROUTE_TO_ADDR;
584 } else if ( !strcasecmp( p, "own-address" ) ) {
585 as->as_kind = AS_KIND_OWN_ADDR;
586 } else if ( !strcasecmp( p, "recipient" ) ) {
587 as->as_kind = AS_KIND_RECIPIENT;
588 } else if ( !strcasecmp( p, "errors" ) ) {
589 as->as_kind = AS_KIND_ERRORS;
590 } else if ( !strcasecmp( p, "request" ) ) {
591 as->as_kind = AS_KIND_REQUEST;
592 } else if ( !strcasecmp( p, "owner" ) ) {
593 as->as_kind = AS_KIND_OWNER;
596 "Unknown semantics word %s", p );
601 if ( attr_semantics == NULL ) {
602 attr_semantics = calloc(2, sizeof(AttrSemantics *));
603 if ( !attr_semantics ) {
604 syslog( LOG_ALERT, "Out of memory" );
607 attr_semantics[0] = as;
610 AttrSemantics **list_temp;
611 for ( size = 0; attr_semantics[size]; size++ )
614 list_temp = realloc( attr_semantics,
615 size*sizeof(AttrSemantics *) );
617 syslog( LOG_ALERT, "Out of memory" );
620 attr_semantics = list_temp;
621 attr_semantics[size-2] = as;
622 attr_semantics[size-1] = NULL;
627 load_config( char *filespec )
636 cf = fopen( filespec, "r" );
638 perror( "Opening config file" );
642 while ( ( line = get_config_line( cf,&lineno ) ) ) {
643 p = strpbrk( line, " \t" );
646 "Missing space at line %d", lineno );
649 if ( !strncmp( line, "search", p-line ) ) {
650 p += strspn( p, " \t" );
654 add_url( p, rdnpref, typemask );
655 } else if ( !strncmp(line, "attribute", p-line) ) {
656 p += strspn(p, " \t");
657 add_attr_semantics( p );
658 } else if ( !strncmp(line, "default-attributes", p-line) ) {
659 p += strspn(p, " \t");
661 } else if ( !strncmp(line, "group-classes", p-line) ) {
662 p += strspn(p, " \t");
663 add_single_to( &groupclasses, p );
664 } else if ( !strncmp(line, "priority", p-line) ) {
665 p += strspn(p, " \t");
666 current_priority = atoi(p);
667 } else if ( !strncmp(line, "domain", p-line) ) {
668 p += strspn(p, " \t");
669 add_single_to( &mydomains, p );
672 "Unparseable config definition at line %d",
681 connect_to_x500( void )
685 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
686 syslog( LOG_ALERT, "ldap_init failed" );
690 /* TBC: Set this only when it makes sense
691 opt = MAIL500_MAXAMBIGUOUS;
692 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
694 opt = LDAP_DEREF_ALWAYS;
695 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
697 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
698 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
706 new_group( char *dn, Group ***list, int *nlist )
711 for ( i = 0; i < *nlist; i++ ) {
712 if ( strcmp( dn, (*list)[i]->g_dn ) == 0 ) {
713 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
718 this_group = (Group *) malloc( sizeof(Group) );
721 *list = (Group **) malloc( sizeof(Group *) );
723 *list = (Group **) realloc( *list, (*nlist + 1) *
727 this_group->g_errorsto = NULL;
728 this_group->g_members = NULL;
729 this_group->g_nmembers = 0;
730 /* save the group's dn so we can check for loops above */
731 this_group->g_dn = strdup( dn );
733 (*list)[*nlist] = this_group;
736 return( this_group );
748 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
749 *localpart = strdup( address );
752 *localpart = malloc( p - address + 1 );
753 strncpy( *localpart, address, p - address );
754 (*localpart)[p - address] = '\0';
756 *domainpart = strdup( p );
775 LDAPMessage *res, *e;
776 struct timeval timeout;
778 timeout.tv_sec = MAIL500_TIMEOUT;
780 for ( i = 0; dnlist[i]; i++ ) {
781 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
782 "(objectclass=*)", def_attr, 0,
783 &timeout, &res )) != LDAP_SUCCESS ) {
784 if ( rc == LDAP_NO_SUCH_OBJECT ) {
785 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
788 syslog( LOG_ALERT, "member search return 0x%x", rc );
790 unbind_and_exit( EX_TEMPFAIL );
793 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
794 syslog( LOG_ALERT, "member search error parsing entry" );
795 unbind_and_exit( EX_TEMPFAIL );
797 if ( entry_engine( e, dnlist[i], address, to, nto,
798 togroups, ngroups, err, nerr,
799 USER | GROUP_MEMBERS ) ) {
827 LDAPMessage *e, *res;
830 struct timeval timeout;
835 timeout.tv_sec = MAIL500_TIMEOUT;
838 rc = ldap_url_parse( url, &ludp );
841 case LDAP_URL_ERR_NOTLDAP:
843 "Not an LDAP URL: %s", url );
845 case LDAP_URL_ERR_BADENCLOSURE:
847 "Bad Enclosure in URL: %s", url );
849 case LDAP_URL_ERR_BADURL:
851 "Bad URL: %s", url );
853 case LDAP_URL_ERR_BADHOST:
855 "Host is invalid in URL: %s", url );
857 case LDAP_URL_ERR_BADATTRS:
859 "Attributes are invalid in URL: %s", url );
861 case LDAP_URL_ERR_BADSCOPE:
863 "Scope is invalid in URL: %s", url );
865 case LDAP_URL_ERR_BADFILTER:
867 "Filter is invalid in URL: %s", url );
869 case LDAP_URL_ERR_BADEXTS:
871 "Extensions are invalid in URL: %s", url );
873 case LDAP_URL_ERR_MEM:
875 "Out of memory parsing URL: %s", url );
877 case LDAP_URL_ERR_PARAM:
879 "bad parameter parsing URL: %s", url );
883 "Unknown error %d parsing URL: %s",
887 add_error( err, nerr, E_BADMEMBER,
893 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
900 for ( i = 0; substs[i].sub_char != '\0';
902 if ( *s == substs[i].sub_char ) {
903 for ( p = substs[i].sub_value;
911 if ( substs[i].sub_char == '\0' ) {
913 "unknown format %c", *s );
921 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
922 filter[ sizeof( filter ) - 1 ] = '\0';
925 if ( ludp->lud_attrs ) {
926 attrlist = ludp->lud_attrs;
931 /* TBC: we don't read the host, dammit */
932 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
936 /* some other trouble - try again later */
937 if ( rc != LDAP_SUCCESS &&
938 rc != LDAP_SIZELIMIT_EXCEEDED ) {
939 syslog( LOG_ALERT, "return 0x%x from X.500",
941 unbind_and_exit( EX_TEMPFAIL );
944 match = ldap_count_entries( ld, res );
946 /* trouble - try again later */
948 syslog( LOG_ALERT, "error parsing result from X.500" );
949 unbind_and_exit( EX_TEMPFAIL );
952 if ( match == 1 || multi_entry ) {
953 for ( e = ldap_first_entry( ld, res ); e != NULL;
954 e = ldap_next_entry( ld, e ) ) {
955 dn = ldap_get_dn( ld, e );
956 resolved = entry_engine( e, dn, address, to, nto,
960 add_error( err, nerr, E_NOEMAIL, address, res );
966 /* more than one match - bounce with ambiguous user? */
968 LDAPMessage *next, *tmpres = NULL;
972 /* not giving rdn preference - bounce with ambiguous user */
973 if ( rdnpref == 0 ) {
974 add_error( err, nerr, E_AMBIGUOUS, address, res );
979 * giving rdn preference - see if any entries were matched
980 * because of their rdn. If so, collect them to deal with
981 * later (== 1 we deliver, > 1 we bounce).
984 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
985 next = ldap_next_entry( ld, e );
986 dn = ldap_get_dn( ld, e );
987 xdn = ldap_explode_dn( dn, 1 );
989 /* XXX bad, but how else can we do it? XXX */
990 if ( strcasecmp( xdn[0], address ) == 0 ) {
991 ldap_delete_result_entry( &res, e );
992 ldap_add_result_entry( &tmpres, e );
995 ldap_value_free( xdn );
999 /* nothing matched by rdn - go ahead and bounce */
1000 if ( tmpres == NULL ) {
1001 add_error( err, nerr, E_AMBIGUOUS, address, res );
1004 /* more than one matched by rdn - bounce with rdn matches */
1005 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1006 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1010 } else if ( match < 0 ) {
1011 syslog( LOG_ALERT, "error parsing result from X.500" );
1012 unbind_and_exit( EX_TEMPFAIL );
1015 /* otherwise one matched by rdn - send to it */
1016 ldap_msgfree( res );
1020 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1021 syslog( LOG_ALERT, "error parsing entry from X.500" );
1022 unbind_and_exit( EX_TEMPFAIL );
1025 dn = ldap_get_dn( ld, e );
1027 resolved = entry_engine( e, dn, address, to, nto,
1031 add_error( err, nerr, E_NOEMAIL, address, res );
1032 /* Don't free res if we passed it to add_error */
1034 ldap_msgfree( res );
1057 for ( i = 0; urllist[i]; i++ ) {
1059 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1062 vals[0] = urllist[i] + 5;
1064 add_to( to, nto, vals );
1067 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1069 resolved = search_ldap_url( urllist[i], NULL,
1070 address, 0, multi_entry,
1071 to, nto, togroups, ngroups,
1074 /* Produce some sensible error here */
1082 * We should probably take MX records into account to cover all bases,
1083 * but really, routing belongs in the MTA.
1094 for ( d = mydomains; *d; d++ ) {
1095 if ( !strcasecmp(*d,domain) ) {
1103 * The entry engine processes an entry. Normally, each entry will resolve
1104 * to one or more values that will be added to the 'to' argument. This
1105 * argument needs not be the global 'to' list, it may be the g_to field
1106 * in a group. Groups have no special treatment, unless they require
1127 char ***current_to = to;
1128 int *current_nto = nto;
1129 Group *current_group = NULL;
1131 char *localpart = NULL, *domainpart = NULL;
1133 int cur_priority = 0;
1134 char *route_to_host = NULL;
1135 char *route_to_address = NULL;
1136 int needs_mta_routing = 0;
1137 char **own_addresses = NULL;
1138 int own_addresses_total = 0;
1141 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1142 AttrSemantics *as = attr_semantics[i];
1146 if ( as->as_priority < cur_priority ) {
1148 * We already got higher priority information,
1149 * so no further work to do, ignore the rest.
1153 vals = ldap_get_values( ld, e, as->as_name );
1154 if ( !vals || vals[0] == NULL ) {
1157 nent = count_values( vals );
1158 if ( nent > 1 && !as->as_m_valued ) {
1159 add_error( err, nerr, E_AMBIGUOUS, address, e );
1162 switch ( as->as_kind ) {
1163 case AS_KIND_RECIPIENT:
1164 cur_priority = as->as_priority;
1165 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1167 switch ( as->as_syntax ) {
1168 case AS_SYNTAX_RFC822:
1169 add_to( current_to, current_nto, vals );
1172 case AS_SYNTAX_RFC822_EXT:
1173 add_to( current_to, current_nto, vals );
1176 case AS_SYNTAX_NATIVE_MB:
1177 /* We used to concatenate mailHost if set here */
1179 * We used to send a copy to the vacation host
1180 * if onVacation to uid@vacationhost
1182 add_to( current_to, current_nto, vals );
1187 if ( dn_search( vals, address,
1188 current_to, current_nto,
1196 if ( url_list_search( vals, address,
1198 current_to, current_nto,
1200 err, nerr, type ) ) {
1205 case AS_SYNTAX_BOOL_FILTER:
1206 if ( strcasecmp( vals[0], "true" ) ) {
1209 substs[0].sub_char = 'D';
1210 substs[0].sub_value = dn;
1211 substs[1].sub_char = '\0';
1212 substs[1].sub_value = NULL;
1213 if ( url_list_search( vals, address,
1215 current_to, current_nto,
1217 err, nerr, type ) ) {
1224 "Invalid syntax %d for kind %d",
1225 as->as_syntax, as->as_kind );
1230 case AS_KIND_ERRORS:
1231 cur_priority = as->as_priority;
1232 /* This is a group with special processing */
1233 if ( type & GROUP_ERRORS ) {
1234 switch (as->as_kind) {
1235 case AS_SYNTAX_RFC822:
1236 add_to( current_to, current_nto, vals );
1242 "Invalid syntax %d for kind %d",
1243 as->as_syntax, as->as_kind );
1246 current_group = new_group( dn, togroups,
1248 current_to = ¤t_group->g_members;
1249 current_nto = ¤t_group->g_nmembers;
1250 split_address( address,
1251 &localpart, &domainpart );
1253 sprintf( buf, "%s-%s@%s",
1259 sprintf( buf, "%s-%s@%s",
1264 current_group->g_errorsto = strdup( buf );
1268 case AS_KIND_REQUEST:
1269 cur_priority = as->as_priority;
1270 /* This is a group with special processing */
1271 if ( type & GROUP_REQUEST ) {
1272 add_to( current_to, current_nto, vals );
1278 cur_priority = as->as_priority;
1279 /* This is a group with special processing */
1280 if ( type & GROUP_REQUEST ) {
1281 add_to( current_to, current_nto, vals );
1286 case AS_KIND_ROUTE_TO_HOST:
1287 if ( !is_my_domain( vals[0] ) ) {
1288 cur_priority = as->as_priority;
1289 if ( as->as_syntax == AS_SYNTAX_PRESENT ) {
1290 needs_mta_routing = 1;
1292 route_to_host = strdup( vals[0] );
1297 case AS_KIND_ROUTE_TO_ADDR:
1298 for ( j=0; j<own_addresses_total; j++ ) {
1299 if ( strcasecmp( vals[0], own_addresses[j] ) ) {
1300 cur_priority = as->as_priority;
1301 if ( as->as_syntax == AS_SYNTAX_PRESENT ) {
1302 needs_mta_routing = 1;
1304 route_to_address = strdup( vals[0] );
1310 case AS_KIND_OWN_ADDR:
1311 add_to( &own_addresses, &own_addresses_total, vals );
1312 cur_priority = as->as_priority;
1317 "Invalid kind %d", as->as_kind );
1320 ldap_value_free( vals );
1323 * Now check if we are dealing with mail routing. We support
1326 * The first mode and by far the most robust method is doing
1327 * routing at the MTA. In this case, we just checked if the
1328 * routing attributes were present and did not seem like
1329 * pointing to ourselves. The only thing we have to do here
1330 * is adding to the recipient list any of the RFC822 addresses
1331 * of this entry. That means we needed to retrieve them from
1332 * the entry itself because we might have arrived here through
1333 * some directory search. The address received as argument is
1334 * not the address of the entry we are processing, but rather
1335 * the RFC822 address we are expanding now. Unfortunately,
1336 * this requires an MTA that understands LDAP routing.
1337 * Sendmail 8.10.0 does, if compiled properly.
1339 * The second method, that is most emphatically not recommended
1340 * is routing in mail500. This is going to require using the
1341 * percent hack. Moreover, this may occasionally loop.
1343 if ( needs_mta_routing ) {
1344 if ( !own_addresses ) {
1345 add_error( err, nerr, E_NOOWNADDRESS, address, e );
1348 nvals[0] = own_addresses[0]; /* Anyone will do */
1350 add_to( current_to, current_nto, nvals );
1352 } else if ( route_to_host ) {
1354 if ( !route_to_address ) {
1355 if ( !own_addresses ) {
1356 add_error( err, nerr, E_NOOWNADDRESS, address, e );
1359 route_to_address = strdup( own_addresses[0] );
1361 /* This makes use of the percent hack, but there's no choice */
1362 p = strchr( route_to_address, '@' );
1366 sprintf( buf, "%s@%s", route_to_address, route_to_host );
1369 add_to( current_to, current_nto, nvals );
1371 free( route_to_host );
1372 free( route_to_address );
1373 } else if ( route_to_address ) {
1374 nvals[0] = route_to_address;
1376 add_to( current_to, current_nto, nvals );
1378 free( route_to_address );
1380 if ( own_addresses ) {
1381 ldap_value_free( own_addresses );
1401 int b, resolved = 0;
1403 for ( b = 0; base[b] != NULL; b++ ) {
1405 if ( ! (base[b]->b_search & type) ) {
1409 resolved = search_ldap_url( base[b]->b_url, substs, name,
1411 base[b]->b_m_entries,
1412 to, nto, togroups, ngroups,
1432 struct timeval timeout;
1433 char *localpart = NULL, *domainpart = NULL;
1434 char *synthname = NULL;
1440 * Look up the name in X.500, add the appropriate addresses found
1441 * to the to list, or to the err list in case of error. Groups are
1442 * handled by the do_group routine, individuals are handled here.
1443 * When looking up name, we follow the bases hierarchy, looking
1444 * in base[0] first, then base[1], etc. For each base, there is
1445 * a set of search filters to try, in order. If something goes
1446 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1447 * If the b_rdnpref flag is set, then we give preference to entries
1448 * that matched name because it's their rdn, otherwise not.
1451 split_address( name, &localpart, &domainpart );
1452 synthname = strdup( localpart );
1453 for ( i = 0; synthname[i] != '\0'; i++ ) {
1454 if ( synthname[i] == '.' || synthname[i] == '_' )
1457 timeout.tv_sec = MAIL500_TIMEOUT;
1458 timeout.tv_usec = 0;
1459 substs[0].sub_char = 'm';
1460 substs[0].sub_value = name;
1461 substs[1].sub_char = 'h';
1462 substs[1].sub_value = host;
1463 substs[2].sub_char = 'l';
1464 substs[2].sub_value = localpart;
1465 substs[3].sub_char = 'd';
1466 substs[3].sub_value = domainpart;
1467 substs[4].sub_char = 's';
1468 substs[4].sub_value = synthname;
1469 substs[5].sub_char = '\0';
1470 substs[5].sub_value = NULL;
1472 resolved = search_bases( NULL, substs, name,
1473 to, nto, togroups, ngroups,
1487 /* not resolved - bounce with user unknown */
1488 if ( type == USER ) {
1489 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1491 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1497 send_message( char **to )
1500 #ifndef HAVE_WAITPID
1501 WAITSTATUSTYPE status;
1508 strcpy( buf, to[0] );
1509 for ( i = 1; to[i] != NULL; i++ ) {
1511 strcat( buf, to[i] );
1514 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1518 if ( (pid = fork()) != 0 ) {
1520 waitpid( pid, (int *) NULL, 0 );
1522 wait4( pid, &status, WAIT_FLAGS, 0 );
1526 /* to includes sendmailargs */
1527 execv( MAIL500_SENDMAIL, to );
1529 syslog( LOG_ALERT, "execv failed" );
1531 exit( EX_TEMPFAIL );
1536 send_group( Group **group, int ngroup )
1542 #ifndef HAVE_WAITPID
1543 WAITSTATUSTYPE status;
1546 for ( i = 0; i < ngroup; i++ ) {
1547 (void) rewind( stdin );
1549 iargv[0] = MAIL500_SENDMAIL;
1551 iargv[2] = group[i]->g_errorsto;
1552 iargv[3] = "-oMrX.500";
1559 add_to( &argv, &argc, iargv );
1560 add_to( &argv, &argc, group[i]->g_members );
1566 strcpy( buf, argv[0] );
1567 for ( i = 1; i < argc; i++ ) {
1569 strcat( buf, argv[i] );
1572 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1576 if ( (pid = fork()) != 0 ) {
1578 waitpid( pid, (int *) NULL, 0 );
1580 wait4( pid, &status, WAIT_FLAGS, 0 );
1584 execv( MAIL500_SENDMAIL, argv );
1586 syslog( LOG_ALERT, "execv failed" );
1588 exit( EX_TEMPFAIL );
1594 send_errors( Error *err, int nerr )
1596 int pid, i, namelen;
1601 #ifndef HAVE_WAITPID
1602 WAITSTATUSTYPE status;
1605 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1606 mailfrom = errorsfrom;
1609 argv[0] = MAIL500_SENDMAIL;
1610 argv[1] = "-oMrX.500";
1614 argv[5] = MAIL500_BOUNCEFROM;
1621 strcpy( buf, argv[0] );
1622 for ( i = 1; argv[i] != NULL; i++ ) {
1624 strcat( buf, argv[i] );
1627 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1630 if ( pipe( fd ) == -1 ) {
1631 syslog( LOG_ALERT, "cannot create pipe" );
1632 exit( EX_TEMPFAIL );
1635 if ( (pid = fork()) != 0 ) {
1636 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1637 syslog( LOG_ALERT, "cannot fdopen pipe" );
1638 exit( EX_TEMPFAIL );
1641 fprintf( fp, "To: %s\n", mailfrom );
1642 fprintf( fp, "From: %s\n", errorsfrom );
1643 fprintf( fp, "Subject: undeliverable mail\n" );
1644 fprintf( fp, "\n" );
1645 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1646 for ( i = 0; i < nerr; i++ ) {
1647 namelen = strlen( err[i].e_addr );
1648 fprintf( fp, "\n" );
1650 switch ( err[i].e_code ) {
1652 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1655 case E_GROUPUNKNOWN:
1656 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1660 fprintf( fp, "%s: Group member does not exist\n",
1662 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1663 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1664 fprintf( fp, "then re-adding the person to the group.\n" );
1668 fprintf( fp, "%s: Group exists but has no request address\n",
1673 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1678 fprintf( fp, "%s: Group exists but has no owner\n",
1683 do_ambiguous( fp, &err[i], namelen );
1687 do_noemail( fp, &err[i], namelen );
1690 case E_MEMBERNOEMAIL:
1691 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1695 case E_JOINMEMBERNOEMAIL:
1696 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1701 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1702 err[i].e_addr, err[i].e_loop );
1706 fprintf( fp, "%s: Group has no members\n",
1710 case E_NOOWNADDRESS:
1711 fprintf( fp, "%s: Not enough information to perform required routing\n",
1716 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1717 unbind_and_exit( EX_TEMPFAIL );
1722 fprintf( fp, "\n------- The original message sent:\n\n" );
1724 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1730 waitpid( pid, (int *) NULL, 0 );
1732 wait4( pid, &status, WAIT_FLAGS, 0 );
1737 execv( MAIL500_SENDMAIL, argv );
1739 syslog( LOG_ALERT, "execv failed" );
1741 exit( EX_TEMPFAIL );
1746 do_noemail( FILE *fp, Error *err, int namelen )
1752 fprintf(fp, "%s: User has no email address registered.\n",
1754 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1755 namelen, " ", err->e_addr );
1758 dn = ldap_get_dn( ld, err->e_msg );
1759 ufn = ldap_explode_dn( dn, 1 );
1760 rdn = strdup( ufn[0] );
1761 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1762 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1764 for ( i = 0; vals[i]; i++ ) {
1765 last = strlen( vals[i] ) - 1;
1766 if ( isdigit((unsigned char) vals[i][last]) ) {
1767 rdn = strdup( vals[i] );
1772 ldap_value_free( vals );
1775 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1778 ldap_value_free( ufn );
1780 /* titles or descriptions */
1781 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1782 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1784 fprintf( fp, "%*s No title or description registered\n",
1787 for ( i = 0; vals[i] != NULL; i++ ) {
1788 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1791 ldap_value_free( vals );
1794 /* postal address */
1795 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1797 fprintf( fp, "%*s No postal address registered\n", namelen,
1800 fprintf( fp, "%*s ", namelen, " " );
1801 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1802 if ( vals[0][i] == '$' ) {
1803 fprintf( fp, "\n%*s ", namelen, " " );
1804 while ( isspace((unsigned char) vals[0][i+1]) )
1807 fprintf( fp, "%c", vals[0][i] );
1810 fprintf( fp, "\n" );
1812 ldap_value_free( vals );
1815 /* telephone number */
1816 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1818 fprintf( fp, "%*s No phone number registered\n", namelen,
1821 for ( i = 0; vals[i] != NULL; i++ ) {
1822 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1825 ldap_value_free( vals );
1831 do_ambiguous( FILE *fp, Error *err, int namelen )
1838 i = ldap_result2error( ld, err->e_msg, 0 );
1840 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1841 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1842 ldap_count_entries( ld, err->e_msg ) );
1844 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1845 e = ldap_next_entry( ld, e ) ) {
1846 dn = ldap_get_dn( ld, e );
1847 ufn = ldap_explode_dn( dn, 1 );
1848 rdn = strdup( ufn[0] );
1849 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1850 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1851 for ( i = 0; vals[i]; i++ ) {
1852 last = strlen( vals[i] ) - 1;
1853 if (isdigit((unsigned char) vals[i][last])) {
1854 rdn = strdup( vals[i] );
1859 ldap_value_free( vals );
1864 if ( isgroup( e ) ) {
1865 vals = ldap_get_values( ld, e, "description" );
1867 vals = ldap_get_values( ld, e, "title" );
1870 vals = ldap_get_values( ld, e, "description" );
1872 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1873 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1874 fprintf( fp, " %s\n", vals[i] );
1879 ldap_value_free( ufn );
1881 ldap_value_free( vals );
1886 count_values( char **list )
1890 for ( i = 0; list && list[i] != NULL; i++ )
1897 add_to( char ***list, int *nlist, char **new )
1899 int i, nnew, oldnlist;
1901 nnew = count_values( new );
1904 if ( *list == NULL || *nlist == 0 ) {
1905 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1908 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1909 nnew * sizeof(char *) + sizeof(char *) );
1913 for ( i = 0; i < nnew; i++ )
1914 (*list)[i + oldnlist] = strdup( new[i] );
1915 (*list)[*nlist] = NULL;
1919 add_single_to( char ***list, char *new )
1923 if ( *list == NULL ) {
1925 *list = (char **) malloc( 2 * sizeof(char *) );
1927 nlist = count_values( *list );
1928 *list = (char **) realloc( *list,
1929 ( nlist + 2 ) * sizeof(char *) );
1932 (*list)[nlist] = strdup( new );
1933 (*list)[nlist+1] = NULL;
1937 isgroup( LDAPMessage *e )
1942 if ( !groupclasses ) {
1946 oclist = ldap_get_values( ld, e, "objectClass" );
1948 for ( i = 0; oclist[i] != NULL; i++ ) {
1949 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1950 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1951 ldap_value_free( oclist );
1956 ldap_value_free( oclist );
1962 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1965 *err = (Error *) malloc( sizeof(Error) );
1967 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1970 (*err)[*nerr].e_code = code;
1971 (*err)[*nerr].e_addr = strdup( addr );
1972 (*err)[*nerr].e_msg = msg;
1977 unbind_and_exit( int rc )
1981 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1982 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );