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-2000 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 */
113 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
115 typedef struct attr_semantics {
117 int as_m_valued; /* Is multivalued? */
118 int as_priority; /* Priority level of this attribut type */
119 int as_syntax; /* How to interpret values */
120 int as_m_entries; /* Can resolve to several entries? */
121 int as_kind; /* Recipient, sender, etc. */
122 char *as_param; /* Extra info for filters and things alike */
125 #define AS_SYNTAX_UNKNOWN 0
126 #define AS_SYNTAX_NATIVE_MB 1 /* Unqualified mailbox name */
127 #define AS_SYNTAX_RFC822 2 /* RFC822 mail address */
128 #define AS_SYNTAX_HOST 3
129 #define AS_SYNTAX_DN 4 /* A directory entry */
130 #define AS_SYNTAX_RFC822_EXT 5
131 #define AS_SYNTAX_URL 6 /* mailto: or ldap: URL */
132 #define AS_SYNTAX_BOOL_FILTER 7 /* For joinable, filter in as_param */
133 #define AS_SYNTAX_PRESENT 8 /* Value irrelevant, only presence is
136 #define AS_KIND_UNKNOWN 0
137 #define AS_KIND_RECIPIENT 1
138 #define AS_KIND_ERRORS 2 /* For ErrorsTo and similar */
139 #define AS_KIND_REQUEST 3
140 #define AS_KIND_OWNER 4
141 #define AS_KIND_ROUTE_TO_HOST 5 /* Expand at some other host */
142 #define AS_KIND_ALLOWED_SENDER 6 /* Can send to group */
143 #define AS_KIND_MODERATOR 7
144 #define AS_KIND_ROUTE_TO_ADDR 8 /* Rewrite recipient address as */
145 #define AS_KIND_OWN_ADDR 9 /* RFC822 name of this entry */
146 #define AS_KIND_DELIVERY_TYPE 10 /* How to deliver mail to this entry */
148 AttrSemantics **attr_semantics = NULL;
149 int current_priority = 0;
151 typedef struct subst {
156 char **groupclasses = NULL;
157 char **def_attr = NULL;
158 char **myhosts = NULL; /* FQDNs not to route elsewhere */
159 char **mydomains = NULL; /* If an RFC822 address points to one
160 of these domains, search it in the
161 directory instead of returning it
164 static void load_config( char *filespec );
165 static void split_address( char *address, char **localpart, char **domainpart);
166 static int entry_engine( LDAPMessage *e, char *dn, char *address, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
167 static void do_address( char *name, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
168 static void send_message( char **to );
169 static void send_errors( Error *err, int nerr );
170 static void do_noemail( FILE *fp, Error *err, int namelen );
171 static void do_ambiguous( FILE *fp, Error *err, int namelen );
172 static int count_values( char **list );
173 static void add_to( char ***list, int *nlist, char **new );
174 static void add_single_to( char ***list, char *new );
175 static int isgroup( LDAPMessage *e );
176 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
177 static void unbind_and_exit( int rc ) LDAP_GCCATTR((noreturn));
178 static void send_group( Group **group, int ngroup );
180 static int connect_to_x500( void );
184 main ( int argc, char **argv )
190 int numto, ngroups, numerr, nargs;
192 char *conffile = NULL;
194 if ( (myname = strrchr( argv[0], '/' )) == NULL )
195 myname = strdup( argv[0] );
197 myname = strdup( myname + 1 );
200 (void) SIGNAL( SIGPIPE, SIG_IGN );
204 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
206 openlog( myname, OPENLOG_OPTIONS );
209 while ( (i = getopt( argc, argv, "d:C:f:h:l:m:v:" )) != EOF ) {
211 case 'd': /* turn on debugging */
212 debug |= atoi( optarg );
215 case 'C': /* path to configuration file */
216 conffile = strdup( optarg );
219 case 'f': /* who it's from & where errors should go */
220 mailfrom = strdup( optarg );
222 if ( mailfrom[0] == '\0' ) {
224 mailfrom = strdup( "<>" );
226 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
227 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
228 sendmailargs[j+1] = mailfrom;
234 case 'h': /* hostname */
235 host = strdup( optarg );
236 hostlen = strlen(host);
239 case 'l': /* ldap host */
240 ldaphost = strdup( optarg );
243 /* mailer-daemon address - who we should */
244 case 'm': /* say errors come from */
245 errorsfrom = strdup( optarg );
248 case 'v': /* vacation host */
249 vacationhost = strdup( optarg );
253 syslog( LOG_ALERT, "unknown option" );
258 if ( mailfrom == NULL ) {
259 syslog( LOG_ALERT, "required argument -f not present" );
262 if ( errorsfrom == NULL ) {
263 syslog( LOG_ALERT, "required argument -m not present" );
266 /* if ( host == NULL ) { */
267 /* syslog( LOG_ALERT, "required argument -h not present" ); */
268 /* exit( EX_TEMPFAIL ); */
270 if ( conffile == NULL ) {
271 syslog( LOG_ALERT, "required argument -C not present" );
275 load_config( conffile );
277 if ( connect_to_x500() != 0 )
286 syslog( LOG_ALERT, "running as %d", geteuid() );
287 strcpy( buf, argv[0] );
288 for ( i = 1; i < argc; i++ ) {
290 strcat( buf, argv[i] );
293 syslog( LOG_ALERT, "args: (%s)", buf );
298 add_to( &tolist, &numto, sendmailargs );
300 ngroups = numerr = 0;
303 for ( i = optind; i < argc; i++ ) {
306 char *localpart = NULL, *domainpart = NULL;
310 split_address( argv[i], &localpart, &domainpart );
311 if ( (s = strrchr( localpart, '-' )) != NULL ) {
314 if ((strcasecmp(s, ERROR) == 0) ||
315 (strcasecmp(s, ERRORS) == 0)) {
318 } else if ((strcasecmp(s, REQUEST) == 0) ||
319 (strcasecmp(s, REQUESTS) == 0)) {
320 type = GROUP_REQUEST;
322 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
323 type = GROUP_MEMBERS;
325 } else if ((strcasecmp(s, OWNER) == 0) ||
326 (strcasecmp(s, OWNERS) == 0)) {
333 sprintf( address, "%s@%s", localpart, domainpart );
337 sprintf( address, "%s", localpart );
340 do_address( address, &tolist, &numto, &togroups, &ngroups,
341 &errlist, &numerr, type );
345 * If we have both errors and successful deliveries to make or if
346 * if there are any groups to deliver to, we basically need to read
347 * the message twice. So, we have to put it in a tmp file.
350 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
355 if ( (fp = tmpfile()) == NULL ) {
356 syslog( LOG_ALERT, "could not open tmp file" );
357 unbind_and_exit( EX_TEMPFAIL );
360 /* copy the message to a temp file */
361 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
362 if ( fputs( buf, fp ) == EOF ) {
363 syslog( LOG_ALERT, "error writing tmpfile" );
364 unbind_and_exit( EX_TEMPFAIL );
368 if ( dup2( fileno( fp ), 0 ) == -1 ) {
369 syslog( LOG_ALERT, "could not dup2 tmpfile" );
370 unbind_and_exit( EX_TEMPFAIL );
376 /* deal with errors */
379 syslog( LOG_ALERT, "sending errors" );
381 (void) rewind( stdin );
382 send_errors( errlist, numerr );
385 (void) ldap_unbind( ld );
387 /* send to groups with errorsTo */
390 syslog( LOG_ALERT, "sending to groups with errorsto" );
392 (void) rewind( stdin );
393 send_group( togroups, ngroups );
396 /* send to expanded aliases and groups w/o errorsTo */
397 if ( numto > nargs ) {
399 syslog( LOG_ALERT, "sending to aliases and groups" );
401 (void) rewind( stdin );
402 send_message( tolist );
409 get_config_line( FILE *cf, int *lineno)
411 static char buf[2048];
417 room = sizeof( buf );
418 while ( fgets( &buf[pos], room, cf ) ) {
421 /* Delete whitespace at the beginning of new data */
422 if ( isspace( buf[pos] ) ) {
424 for ( s = buf+pos; isspace(*s); s++ )
426 for ( d = buf+pos; *s; s++, d++ ) {
433 if ( buf[len-1] != '\n' ) {
434 syslog( LOG_ALERT, "Definition too long at line %d",
440 if ( strspn( buf, " \t\n" ) == len )
442 if ( buf[len-2] == '\\' ) {
444 room = sizeof(buf) - pos;
447 /* We have a real line, we will exit the loop */
455 add_url ( char *url, int rdnpref, int typemask )
461 b = calloc(1, sizeof(Base));
463 syslog( LOG_ALERT, "Out of memory" );
466 b->b_url = strdup( url );
467 b->b_rdnpref = rdnpref;
468 b->b_search = typemask;
470 if ( base == NULL ) {
471 base = calloc(2, sizeof(LDAPURLDesc *));
473 syslog( LOG_ALERT, "Out of memory" );
478 for ( size = 0; base[size]; size++ )
481 list_temp = realloc( base, size*sizeof(LDAPURLDesc *) );
483 syslog( LOG_ALERT, "Out of memory" );
493 add_def_attr( char *s )
499 p += strspn( p, "\t," );
500 q = strpbrk( p, " \t," );
503 add_single_to( &def_attr, p );
505 add_single_to( &def_attr, p );
513 add_attr_semantics( char *s )
518 as = calloc( 1, sizeof( AttrSemantics ) );
519 as->as_priority = current_priority;
521 while ( isspace ( *p ) )
524 while ( !isspace ( *q ) && *q != '\0' )
527 as->as_name = strdup( p );
531 while ( isspace ( *p ) )
534 while ( !isspace ( *q ) && *q != '\0' )
537 if ( !strcasecmp( p, "multivalued" ) ) {
539 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
540 as->as_m_entries = 1;
541 } else if ( !strcasecmp( p, "local-native-mailbox" ) ) {
542 as->as_syntax = AS_SYNTAX_NATIVE_MB;
543 } else if ( !strcasecmp( p, "rfc822" ) ) {
544 as->as_syntax = AS_SYNTAX_RFC822;
545 } else if ( !strcasecmp( p, "rfc822-extended" ) ) {
546 as->as_syntax = AS_SYNTAX_RFC822_EXT;
547 } else if ( !strcasecmp( p, "dn" ) ) {
548 as->as_syntax = AS_SYNTAX_DN;
549 } else if ( !strcasecmp( p, "url" ) ) {
550 as->as_syntax = AS_SYNTAX_URL;
551 } else if ( !strcasecmp( p, "search-with-filter" ) ) {
552 as->as_syntax = AS_SYNTAX_BOOL_FILTER;
553 } else if ( !strncasecmp( p, "param=", 6 ) ) {
554 q = strchr( p, '=' );
557 while ( *q && !isspace( *q ) ) {
562 as->as_param = strdup( p );
565 as->as_param = strdup( p );
569 } else if ( !strcasecmp( p, "host" ) ) {
570 as->as_kind = AS_SYNTAX_HOST;
571 } else if ( !strcasecmp( p, "present" ) ) {
572 as->as_kind = AS_SYNTAX_PRESENT;
573 } else if ( !strcasecmp( p, "route-to-host" ) ) {
574 as->as_kind = AS_KIND_ROUTE_TO_HOST;
575 } else if ( !strcasecmp( p, "route-to-address" ) ) {
576 as->as_kind = AS_KIND_ROUTE_TO_ADDR;
577 } else if ( !strcasecmp( p, "own-address" ) ) {
578 as->as_kind = AS_KIND_OWN_ADDR;
579 } else if ( !strcasecmp( p, "recipient" ) ) {
580 as->as_kind = AS_KIND_RECIPIENT;
581 } else if ( !strcasecmp( p, "errors" ) ) {
582 as->as_kind = AS_KIND_ERRORS;
583 } else if ( !strcasecmp( p, "request" ) ) {
584 as->as_kind = AS_KIND_REQUEST;
585 } else if ( !strcasecmp( p, "owner" ) ) {
586 as->as_kind = AS_KIND_OWNER;
587 } else if ( !strcasecmp( p, "delivery-type" ) ) {
588 as->as_kind = AS_KIND_DELIVERY_TYPE;
591 "Unknown semantics word %s", p );
596 if ( attr_semantics == NULL ) {
597 attr_semantics = calloc(2, sizeof(AttrSemantics *));
598 if ( !attr_semantics ) {
599 syslog( LOG_ALERT, "Out of memory" );
602 attr_semantics[0] = as;
605 AttrSemantics **list_temp;
606 for ( size = 0; attr_semantics[size]; size++ )
609 list_temp = realloc( attr_semantics,
610 size*sizeof(AttrSemantics *) );
612 syslog( LOG_ALERT, "Out of memory" );
615 attr_semantics = list_temp;
616 attr_semantics[size-2] = as;
617 attr_semantics[size-1] = NULL;
622 load_config( char *filespec )
631 cf = fopen( filespec, "r" );
633 perror( "Opening config file" );
637 while ( ( line = get_config_line( cf,&lineno ) ) ) {
638 p = strpbrk( line, " \t" );
641 "Missing space at line %d", lineno );
644 if ( !strncmp( line, "search", p-line ) ) {
645 p += strspn( p, " \t" );
649 add_url( p, rdnpref, typemask );
650 } else if ( !strncmp(line, "attribute", p-line) ) {
651 p += strspn(p, " \t");
652 add_attr_semantics( p );
653 } else if ( !strncmp(line, "default-attributes", p-line) ) {
654 p += strspn(p, " \t");
656 } else if ( !strncmp(line, "group-classes", p-line) ) {
657 p += strspn(p, " \t");
658 add_single_to( &groupclasses, p );
659 } else if ( !strncmp(line, "priority", p-line) ) {
660 p += strspn(p, " \t");
661 current_priority = atoi(p);
662 } else if ( !strncmp(line, "domain", p-line) ) {
663 p += strspn(p, " \t");
664 add_single_to( &mydomains, p );
665 } else if ( !strncmp(line, "host", p-line) ) {
666 p += strspn(p, " \t");
667 add_single_to( &myhosts, p );
670 "Unparseable config definition at line %d",
679 connect_to_x500( void )
683 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
684 syslog( LOG_ALERT, "ldap_init failed" );
688 /* TBC: Set this only when it makes sense
689 opt = MAIL500_MAXAMBIGUOUS;
690 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
692 opt = LDAP_DEREF_ALWAYS;
693 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
695 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
696 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
704 new_group( char *dn, Group ***list, int *nlist )
709 for ( i = 0; i < *nlist; i++ ) {
710 if ( strcasecmp( dn, (*list)[i]->g_dn ) == 0 ) {
711 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
716 this_group = (Group *) malloc( sizeof(Group) );
719 *list = (Group **) malloc( sizeof(Group *) );
721 *list = (Group **) realloc( *list, (*nlist + 1) *
725 this_group->g_errorsto = NULL;
726 this_group->g_members = NULL;
727 this_group->g_nmembers = 0;
728 /* save the group's dn so we can check for loops above */
729 this_group->g_dn = strdup( dn );
731 (*list)[*nlist] = this_group;
734 return( this_group );
746 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
747 *localpart = strdup( address );
750 *localpart = malloc( p - address + 1 );
751 strncpy( *localpart, address, p - address );
752 (*localpart)[p - address] = '\0';
754 *domainpart = strdup( p );
773 LDAPMessage *res, *e;
774 struct timeval timeout;
776 timeout.tv_sec = MAIL500_TIMEOUT;
778 for ( i = 0; dnlist[i]; i++ ) {
779 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
781 &timeout, &res )) != LDAP_SUCCESS ) {
782 if ( rc == LDAP_NO_SUCH_OBJECT ) {
783 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
786 syslog( LOG_ALERT, "member search return 0x%x", rc );
788 unbind_and_exit( EX_TEMPFAIL );
791 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
792 syslog( LOG_ALERT, "member search error parsing entry" );
793 unbind_and_exit( EX_TEMPFAIL );
795 if ( entry_engine( e, dnlist[i], address, to, nto,
796 togroups, ngroups, err, nerr,
797 USER | GROUP_MEMBERS ) ) {
825 LDAPMessage *e, *res;
828 struct timeval timeout;
833 timeout.tv_sec = MAIL500_TIMEOUT;
836 rc = ldap_url_parse( url, &ludp );
839 case LDAP_URL_ERR_BADSCHEME:
841 "Not an LDAP URL: %s", url );
843 case LDAP_URL_ERR_BADENCLOSURE:
845 "Bad Enclosure in URL: %s", url );
847 case LDAP_URL_ERR_BADURL:
849 "Bad URL: %s", url );
851 case LDAP_URL_ERR_BADHOST:
853 "Host is invalid in URL: %s", url );
855 case LDAP_URL_ERR_BADATTRS:
857 "Attributes are invalid in URL: %s", url );
859 case LDAP_URL_ERR_BADSCOPE:
861 "Scope is invalid in URL: %s", url );
863 case LDAP_URL_ERR_BADFILTER:
865 "Filter is invalid in URL: %s", url );
867 case LDAP_URL_ERR_BADEXTS:
869 "Extensions are invalid in URL: %s", url );
871 case LDAP_URL_ERR_MEM:
873 "Out of memory parsing URL: %s", url );
875 case LDAP_URL_ERR_PARAM:
877 "bad parameter parsing URL: %s", url );
881 "Unknown error %d parsing URL: %s",
885 add_error( err, nerr, E_BADMEMBER,
891 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
898 for ( i = 0; substs[i].sub_char != '\0';
900 if ( *s == substs[i].sub_char ) {
901 for ( p = substs[i].sub_value;
909 if ( substs[i].sub_char == '\0' ) {
911 "unknown format %c", *s );
919 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
920 filter[ sizeof( filter ) - 1 ] = '\0';
923 if ( ludp->lud_attrs ) {
924 attrlist = ludp->lud_attrs;
929 /* TBC: we don't read the host, dammit */
930 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
934 /* some other trouble - try again later */
935 if ( rc != LDAP_SUCCESS &&
936 rc != LDAP_SIZELIMIT_EXCEEDED ) {
937 syslog( LOG_ALERT, "return 0x%x from X.500",
939 unbind_and_exit( EX_TEMPFAIL );
942 match = ldap_count_entries( ld, res );
944 /* trouble - try again later */
946 syslog( LOG_ALERT, "error parsing result from X.500" );
947 unbind_and_exit( EX_TEMPFAIL );
950 if ( match == 1 || multi_entry ) {
951 for ( e = ldap_first_entry( ld, res ); e != NULL;
952 e = ldap_next_entry( ld, e ) ) {
953 dn = ldap_get_dn( ld, e );
954 resolved = entry_engine( e, dn, address, to, nto,
958 add_error( err, nerr, E_NOEMAIL, address, res );
964 /* more than one match - bounce with ambiguous user? */
966 LDAPMessage *next, *tmpres = NULL;
970 /* not giving rdn preference - bounce with ambiguous user */
971 if ( rdnpref == 0 ) {
972 add_error( err, nerr, E_AMBIGUOUS, address, res );
977 * giving rdn preference - see if any entries were matched
978 * because of their rdn. If so, collect them to deal with
979 * later (== 1 we deliver, > 1 we bounce).
982 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
983 next = ldap_next_entry( ld, e );
984 dn = ldap_get_dn( ld, e );
985 xdn = ldap_explode_dn( dn, 1 );
987 /* XXX bad, but how else can we do it? XXX */
988 if ( strcasecmp( xdn[0], address ) == 0 ) {
989 ldap_delete_result_entry( &res, e );
990 ldap_add_result_entry( &tmpres, e );
993 ldap_value_free( xdn );
997 /* nothing matched by rdn - go ahead and bounce */
998 if ( tmpres == NULL ) {
999 add_error( err, nerr, E_AMBIGUOUS, address, res );
1002 /* more than one matched by rdn - bounce with rdn matches */
1003 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1004 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1008 } else if ( match < 0 ) {
1009 syslog( LOG_ALERT, "error parsing result from X.500" );
1010 unbind_and_exit( EX_TEMPFAIL );
1013 /* otherwise one matched by rdn - send to it */
1014 ldap_msgfree( res );
1018 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1019 syslog( LOG_ALERT, "error parsing entry from X.500" );
1020 unbind_and_exit( EX_TEMPFAIL );
1023 dn = ldap_get_dn( ld, e );
1025 resolved = entry_engine( e, dn, address, to, nto,
1029 add_error( err, nerr, E_NOEMAIL, address, res );
1030 /* Don't free res if we passed it to add_error */
1032 ldap_msgfree( res );
1055 for ( i = 0; urllist[i]; i++ ) {
1057 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1060 vals[0] = urllist[i] + 5;
1062 add_to( to, nto, vals );
1065 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1067 resolved = search_ldap_url( urllist[i], NULL,
1068 address, 0, multi_entry,
1069 to, nto, togroups, ngroups,
1072 /* Produce some sensible error here */
1080 * We should probably take MX records into account to cover all bases,
1081 * but really, routing belongs in the MTA.
1090 if ( myhosts == NULL )
1092 for ( d = myhosts; *d; d++ ) {
1093 if ( !strcasecmp(*d,host) ) {
1108 if ( mydomains == NULL )
1110 p = strchr( address, '@' );
1113 for ( d = mydomains; *d; d++ ) {
1114 if ( !strcasecmp(*d,p+1) ) {
1137 * Well, this is tricky, every address in my_addresses will be
1138 * removed from the list while we shift the other values down
1139 * and we do it in a single scan of the address list and
1140 * without using additional memory. We are going to be
1141 * modifying the value list in a way that the later
1142 * ldap_value_free works.
1145 for ( i = 0; addresses[i]; i++ ) {
1146 if ( is_my_domain(addresses[i]) ) {
1147 do_address( addresses[i], to, nto, togroups, ngroups,
1149 ldap_memfree( addresses[i] );
1152 addresses[j] = addresses[i];
1157 addresses[j] = NULL;
1158 if ( addresses[0] ) {
1159 add_to( to, nto, addresses );
1164 * The entry engine processes an entry. Normally, each entry will resolve
1165 * to one or more values that will be added to the 'to' argument. This
1166 * argument needs not be the global 'to' list, it may be the g_to field
1167 * in a group. Groups have no special treatment, unless they require
1188 char ***current_to = to;
1189 int *current_nto = nto;
1190 Group *current_group = NULL;
1192 char *localpart = NULL, *domainpart = NULL;
1194 int cur_priority = 0;
1195 char *route_to_host = NULL;
1196 char *route_to_address = NULL;
1197 int needs_mta_routing = 0;
1198 char **own_addresses = NULL;
1199 int own_addresses_total = 0;
1200 char **delivery_types = NULL;
1201 int delivery_types_total = 0;
1204 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1205 AttrSemantics *as = attr_semantics[i];
1209 if ( as->as_priority < cur_priority ) {
1211 * We already got higher priority information,
1212 * so no further work to do, ignore the rest.
1216 vals = ldap_get_values( ld, e, as->as_name );
1217 if ( !vals || vals[0] == NULL ) {
1220 nent = count_values( vals );
1221 if ( nent > 1 && !as->as_m_valued ) {
1222 add_error( err, nerr, E_AMBIGUOUS, address, e );
1225 switch ( as->as_kind ) {
1226 case AS_KIND_RECIPIENT:
1227 cur_priority = as->as_priority;
1228 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1230 switch ( as->as_syntax ) {
1231 case AS_SYNTAX_RFC822:
1232 do_addresses( vals, current_to, current_nto,
1233 togroups, ngroups, err, nerr,
1237 case AS_SYNTAX_RFC822_EXT:
1238 do_addresses( vals, current_to, current_nto,
1239 togroups, ngroups, err, nerr,
1243 case AS_SYNTAX_NATIVE_MB:
1244 /* We used to concatenate mailHost if set here */
1246 * We used to send a copy to the vacation host
1247 * if onVacation to uid@vacationhost
1249 if ( as->as_param ) {
1250 for ( j=0; j<delivery_types_total; j++ ) {
1251 if ( !strcasecmp( as->as_param, delivery_types[j] ) ) {
1252 add_to( current_to, current_nto, vals );
1258 add_to( current_to, current_nto, vals );
1264 if ( dn_search( vals, address,
1265 current_to, current_nto,
1273 if ( url_list_search( vals, address,
1275 current_to, current_nto,
1277 err, nerr, type ) ) {
1282 case AS_SYNTAX_BOOL_FILTER:
1283 if ( strcasecmp( vals[0], "true" ) ) {
1286 substs[0].sub_char = 'D';
1287 substs[0].sub_value = dn;
1288 substs[1].sub_char = '\0';
1289 substs[1].sub_value = NULL;
1290 if ( url_list_search( vals, address,
1292 current_to, current_nto,
1294 err, nerr, type ) ) {
1301 "Invalid syntax %d for kind %d",
1302 as->as_syntax, as->as_kind );
1307 case AS_KIND_ERRORS:
1308 cur_priority = as->as_priority;
1309 /* This is a group with special processing */
1310 if ( type & GROUP_ERRORS ) {
1311 switch (as->as_kind) {
1312 case AS_SYNTAX_RFC822:
1313 add_to( current_to, current_nto, vals );
1319 "Invalid syntax %d for kind %d",
1320 as->as_syntax, as->as_kind );
1323 current_group = new_group( dn, togroups,
1325 if ( ! current_group )
1327 * We have already considered
1328 * this group, so we just
1332 current_to = ¤t_group->g_members;
1333 current_nto = ¤t_group->g_nmembers;
1334 split_address( address,
1335 &localpart, &domainpart );
1337 sprintf( buf, "%s-%s@%s",
1343 sprintf( buf, "%s-%s@%s",
1348 current_group->g_errorsto = strdup( buf );
1352 case AS_KIND_REQUEST:
1353 cur_priority = as->as_priority;
1354 /* This is a group with special processing */
1355 if ( type & GROUP_REQUEST ) {
1356 add_to( current_to, current_nto, vals );
1362 cur_priority = as->as_priority;
1363 /* This is a group with special processing */
1364 if ( type & GROUP_REQUEST ) {
1365 add_to( current_to, current_nto, vals );
1370 case AS_KIND_ROUTE_TO_HOST:
1371 if ( !is_my_host( vals[0] ) ) {
1372 cur_priority = as->as_priority;
1373 if ( as->as_syntax == AS_SYNTAX_PRESENT ) {
1374 needs_mta_routing = 1;
1376 route_to_host = strdup( vals[0] );
1381 case AS_KIND_ROUTE_TO_ADDR:
1382 for ( j=0; j<own_addresses_total; j++ ) {
1383 if ( strcasecmp( vals[0], own_addresses[j] ) ) {
1384 cur_priority = as->as_priority;
1385 if ( as->as_syntax == AS_SYNTAX_PRESENT ) {
1386 needs_mta_routing = 1;
1388 route_to_address = strdup( vals[0] );
1394 case AS_KIND_OWN_ADDR:
1395 add_to( &own_addresses, &own_addresses_total, vals );
1396 cur_priority = as->as_priority;
1399 case AS_KIND_DELIVERY_TYPE:
1400 add_to( &delivery_types, &delivery_types_total, vals );
1401 cur_priority = as->as_priority;
1406 "Invalid kind %d", as->as_kind );
1409 ldap_value_free( vals );
1412 * Now check if we are dealing with mail routing. We support
1415 * The first mode and by far the most robust method is doing
1416 * routing at the MTA. In this case, we just checked if the
1417 * routing attributes were present and did not seem like
1418 * pointing to ourselves. The only thing we have to do here
1419 * is adding to the recipient list any of the RFC822 addresses
1420 * of this entry. That means we needed to retrieve them from
1421 * the entry itself because we might have arrived here through
1422 * some directory search. The address received as argument is
1423 * not the address of the entry we are processing, but rather
1424 * the RFC822 address we are expanding now. Unfortunately,
1425 * this requires an MTA that understands LDAP routing.
1426 * Sendmail 8.10.0 does, if compiled properly.
1428 * The second method, that is most emphatically not recommended
1429 * is routing in maildap. This is going to require using the
1430 * percent hack. Moreover, this may occasionally loop.
1432 if ( needs_mta_routing ) {
1433 if ( !own_addresses ) {
1434 add_error( err, nerr, E_NOOWNADDRESS, address, e );
1437 nvals[0] = own_addresses[0]; /* Anyone will do */
1439 add_to( current_to, current_nto, nvals );
1441 } else if ( route_to_host ) {
1443 if ( !route_to_address ) {
1444 if ( !own_addresses ) {
1445 add_error( err, nerr, E_NOOWNADDRESS, address, e );
1448 route_to_address = strdup( own_addresses[0] );
1450 /* This makes use of the percent hack, but there's no choice */
1451 p = strchr( route_to_address, '@' );
1455 sprintf( buf, "%s@%s", route_to_address, route_to_host );
1458 add_to( current_to, current_nto, nvals );
1460 free( route_to_host );
1461 free( route_to_address );
1462 } else if ( route_to_address ) {
1463 nvals[0] = route_to_address;
1465 add_to( current_to, current_nto, nvals );
1467 free( route_to_address );
1469 if ( own_addresses ) {
1470 ldap_value_free( own_addresses );
1472 if ( delivery_types ) {
1473 ldap_value_free( delivery_types );
1493 int b, resolved = 0;
1495 for ( b = 0; base[b] != NULL; b++ ) {
1497 if ( ! (base[b]->b_search & type) ) {
1501 resolved = search_ldap_url( base[b]->b_url, substs, name,
1503 base[b]->b_m_entries,
1504 to, nto, togroups, ngroups,
1524 char *localpart = NULL, *domainpart = NULL;
1525 char *synthname = NULL;
1531 * Look up the name in X.500, add the appropriate addresses found
1532 * to the to list, or to the err list in case of error. Groups are
1533 * handled by the do_group routine, individuals are handled here.
1534 * When looking up name, we follow the bases hierarchy, looking
1535 * in base[0] first, then base[1], etc. For each base, there is
1536 * a set of search filters to try, in order. If something goes
1537 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1538 * If the b_rdnpref flag is set, then we give preference to entries
1539 * that matched name because it's their rdn, otherwise not.
1542 split_address( name, &localpart, &domainpart );
1543 synthname = strdup( localpart );
1544 for ( i = 0; synthname[i] != '\0'; i++ ) {
1545 if ( synthname[i] == '.' || synthname[i] == '_' )
1548 substs[0].sub_char = 'm';
1549 substs[0].sub_value = name;
1550 substs[1].sub_char = 'h';
1551 substs[1].sub_value = host;
1552 substs[2].sub_char = 'l';
1553 substs[2].sub_value = localpart;
1554 substs[3].sub_char = 'd';
1555 substs[3].sub_value = domainpart;
1556 substs[4].sub_char = 's';
1557 substs[4].sub_value = synthname;
1558 substs[5].sub_char = '\0';
1559 substs[5].sub_value = NULL;
1561 resolved = search_bases( NULL, substs, name,
1562 to, nto, togroups, ngroups,
1576 /* not resolved - bounce with user unknown */
1577 if ( type == USER ) {
1578 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1580 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1586 send_message( char **to )
1589 #ifndef HAVE_WAITPID
1590 WAITSTATUSTYPE status;
1597 strcpy( buf, to[0] );
1598 for ( i = 1; to[i] != NULL; i++ ) {
1600 strcat( buf, to[i] );
1603 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1607 if ( (pid = fork()) != 0 ) {
1609 waitpid( pid, (int *) NULL, 0 );
1611 wait4( pid, &status, WAIT_FLAGS, 0 );
1615 /* to includes sendmailargs */
1616 execv( MAIL500_SENDMAIL, to );
1618 syslog( LOG_ALERT, "execv failed" );
1620 exit( EX_TEMPFAIL );
1625 send_group( Group **group, int ngroup )
1631 #ifndef HAVE_WAITPID
1632 WAITSTATUSTYPE status;
1635 for ( i = 0; i < ngroup; i++ ) {
1636 (void) rewind( stdin );
1638 iargv[0] = MAIL500_SENDMAIL;
1640 iargv[2] = group[i]->g_errorsto;
1641 iargv[3] = "-oMrX.500";
1648 add_to( &argv, &argc, iargv );
1649 add_to( &argv, &argc, group[i]->g_members );
1655 strcpy( buf, argv[0] );
1656 for ( i = 1; i < argc; i++ ) {
1658 strcat( buf, argv[i] );
1661 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1665 if ( (pid = fork()) != 0 ) {
1667 waitpid( pid, (int *) NULL, 0 );
1669 wait4( pid, &status, WAIT_FLAGS, 0 );
1673 execv( MAIL500_SENDMAIL, argv );
1675 syslog( LOG_ALERT, "execv failed" );
1677 exit( EX_TEMPFAIL );
1683 send_errors( Error *err, int nerr )
1685 int pid, i, namelen;
1690 #ifndef HAVE_WAITPID
1691 WAITSTATUSTYPE status;
1694 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1695 mailfrom = errorsfrom;
1698 argv[0] = MAIL500_SENDMAIL;
1699 argv[1] = "-oMrX.500";
1703 argv[5] = MAIL500_BOUNCEFROM;
1710 strcpy( buf, argv[0] );
1711 for ( i = 1; argv[i] != NULL; i++ ) {
1713 strcat( buf, argv[i] );
1716 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1719 if ( pipe( fd ) == -1 ) {
1720 syslog( LOG_ALERT, "cannot create pipe" );
1721 exit( EX_TEMPFAIL );
1724 if ( (pid = fork()) != 0 ) {
1725 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1726 syslog( LOG_ALERT, "cannot fdopen pipe" );
1727 exit( EX_TEMPFAIL );
1730 fprintf( fp, "To: %s\n", mailfrom );
1731 fprintf( fp, "From: %s\n", errorsfrom );
1732 fprintf( fp, "Subject: undeliverable mail\n" );
1733 fprintf( fp, "\n" );
1734 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1735 for ( i = 0; i < nerr; i++ ) {
1736 namelen = strlen( err[i].e_addr );
1737 fprintf( fp, "\n" );
1739 switch ( err[i].e_code ) {
1741 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1744 case E_GROUPUNKNOWN:
1745 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1749 fprintf( fp, "%s: Group member does not exist\n",
1751 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1752 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1753 fprintf( fp, "then re-adding the person to the group.\n" );
1757 fprintf( fp, "%s: Group exists but has no request address\n",
1762 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1767 fprintf( fp, "%s: Group exists but has no owner\n",
1772 do_ambiguous( fp, &err[i], namelen );
1776 do_noemail( fp, &err[i], namelen );
1779 case E_MEMBERNOEMAIL:
1780 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1784 case E_JOINMEMBERNOEMAIL:
1785 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1790 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1791 err[i].e_addr, err[i].e_loop );
1795 fprintf( fp, "%s: Group has no members\n",
1799 case E_NOOWNADDRESS:
1800 fprintf( fp, "%s: Not enough information to perform required routing\n",
1805 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1806 unbind_and_exit( EX_TEMPFAIL );
1811 fprintf( fp, "\n------- The original message sent:\n\n" );
1813 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1819 waitpid( pid, (int *) NULL, 0 );
1821 wait4( pid, &status, WAIT_FLAGS, 0 );
1826 execv( MAIL500_SENDMAIL, argv );
1828 syslog( LOG_ALERT, "execv failed" );
1830 exit( EX_TEMPFAIL );
1835 do_noemail( FILE *fp, Error *err, int namelen )
1841 fprintf(fp, "%s: User has no email address registered.\n",
1843 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1844 namelen, " ", err->e_addr );
1847 dn = ldap_get_dn( ld, err->e_msg );
1848 ufn = ldap_explode_dn( dn, 1 );
1849 rdn = strdup( ufn[0] );
1850 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1851 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1853 for ( i = 0; vals[i]; i++ ) {
1854 last = strlen( vals[i] ) - 1;
1855 if ( isdigit((unsigned char) vals[i][last]) ) {
1856 rdn = strdup( vals[i] );
1861 ldap_value_free( vals );
1864 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1867 ldap_value_free( ufn );
1869 /* titles or descriptions */
1870 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1871 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1873 fprintf( fp, "%*s No title or description registered\n",
1876 for ( i = 0; vals[i] != NULL; i++ ) {
1877 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1880 ldap_value_free( vals );
1883 /* postal address */
1884 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1886 fprintf( fp, "%*s No postal address registered\n", namelen,
1889 fprintf( fp, "%*s ", namelen, " " );
1890 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1891 if ( vals[0][i] == '$' ) {
1892 fprintf( fp, "\n%*s ", namelen, " " );
1893 while ( isspace((unsigned char) vals[0][i+1]) )
1896 fprintf( fp, "%c", vals[0][i] );
1899 fprintf( fp, "\n" );
1901 ldap_value_free( vals );
1904 /* telephone number */
1905 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1907 fprintf( fp, "%*s No phone number registered\n", namelen,
1910 for ( i = 0; vals[i] != NULL; i++ ) {
1911 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1914 ldap_value_free( vals );
1920 do_ambiguous( FILE *fp, Error *err, int namelen )
1927 i = ldap_result2error( ld, err->e_msg, 0 );
1929 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1930 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1931 ldap_count_entries( ld, err->e_msg ) );
1933 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1934 e = ldap_next_entry( ld, e ) ) {
1935 dn = ldap_get_dn( ld, e );
1936 ufn = ldap_explode_dn( dn, 1 );
1937 rdn = strdup( ufn[0] );
1938 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1939 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1940 for ( i = 0; vals[i]; i++ ) {
1941 last = strlen( vals[i] ) - 1;
1942 if (isdigit((unsigned char) vals[i][last])) {
1943 rdn = strdup( vals[i] );
1948 ldap_value_free( vals );
1953 if ( isgroup( e ) ) {
1954 vals = ldap_get_values( ld, e, "description" );
1956 vals = ldap_get_values( ld, e, "title" );
1959 vals = ldap_get_values( ld, e, "description" );
1961 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1962 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1963 fprintf( fp, " %s\n", vals[i] );
1968 ldap_value_free( ufn );
1970 ldap_value_free( vals );
1975 count_values( char **list )
1979 for ( i = 0; list && list[i] != NULL; i++ )
1986 add_to( char ***list, int *nlist, char **new )
1988 int i, nnew, oldnlist;
1990 nnew = count_values( new );
1993 if ( *list == NULL || *nlist == 0 ) {
1994 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1997 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1998 nnew * sizeof(char *) + sizeof(char *) );
2002 for ( i = 0; i < nnew; i++ )
2003 (*list)[i + oldnlist] = strdup( new[i] );
2004 (*list)[*nlist] = NULL;
2008 add_single_to( char ***list, char *new )
2012 if ( *list == NULL ) {
2014 *list = (char **) malloc( 2 * sizeof(char *) );
2016 nlist = count_values( *list );
2017 *list = (char **) realloc( *list,
2018 ( nlist + 2 ) * sizeof(char *) );
2021 (*list)[nlist] = strdup( new );
2022 (*list)[nlist+1] = NULL;
2026 isgroup( LDAPMessage *e )
2031 if ( !groupclasses ) {
2035 oclist = ldap_get_values( ld, e, "objectClass" );
2037 for ( i = 0; oclist[i] != NULL; i++ ) {
2038 for ( j = 0; groupclasses[j] != NULL; j++ ) {
2039 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
2040 ldap_value_free( oclist );
2045 ldap_value_free( oclist );
2051 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
2054 *err = (Error *) malloc( sizeof(Error) );
2056 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
2059 (*err)[*nerr].e_code = code;
2060 (*err)[*nerr].e_addr = strdup( addr );
2061 (*err)[*nerr].e_msg = msg;
2066 unbind_and_exit( int rc )
2070 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
2071 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );