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
90 #define e_msg e_union.e_u_msg
91 #define e_loop e_union.e_u_loop
94 typedef struct groupto {
101 typedef struct baseinfo {
104 char b_rdnpref; /* give rdn's preference when searching? */
105 int b_search; /* ORed with the type of thing the address */
106 /* looks like (USER, GROUP_ERRORS, etc.) */
107 /* to see if this should be searched */
111 * We should limit the search to objectclass=mailRecipient or
112 * objectclass=mailGroup.
117 {"dc=StlInter, dc=Net",
119 {"mail=%s", "mailAlternateAddress=%s", NULL}},
126 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
128 typedef struct attr_semantics {
130 int as_m_valued; /* Is multivalued? */
131 int as_priority; /* Priority level of this attribut type */
132 int as_syntax; /* How to interpret values */
133 int as_m_entries; /* Can resolve to several entries? */
134 int as_kind; /* Recipient, sender, etc. */
135 char *as_param; /* Extra info for filters and things alike */
138 #define AS_SYNTAX_UNKNOWN 0 /* Unqualified mailbox name */
139 #define AS_SYNTAX_NATIVE_MB 1 /* Unqualified mailbox name */
140 #define AS_SYNTAX_RFC822 2 /* RFC822 mail address */
141 #define AS_SYNTAX_HOST 3
142 #define AS_SYNTAX_DN 4 /* A directory entry */
143 #define AS_SYNTAX_RFC822_EXT 5
144 #define AS_SYNTAX_URL 6 /* mailto: or ldap: URL */
145 #define AS_SYNTAX_BOOL_FILTER 7 /* For joinable, filter in as_param */
147 #define AS_KIND_UNKNOWN 0
148 #define AS_KIND_RECIPIENT 1
149 #define AS_KIND_ERRORS 2 /* For ErrorsTo and similar */
150 #define AS_KIND_REQUEST 3
151 #define AS_KIND_OWNER 4
152 #define AS_KIND_ROUTE_TO_HOST 5 /* Expand at some other host */
153 #define AS_KIND_ALLOWED_SENDER 6 /* Can send to group */
154 #define AS_KIND_MODERATOR 7
155 #define AS_KIND_ROUTE_TO_ADDR 8 /* Rewrite recipient address as */
157 AttrSemantics **attr_semantics = NULL;
158 int current_priority = 0;
160 typedef struct subst {
165 char **groupclasses = NULL;
166 char **def_attr = NULL;
167 char **mydomains = NULL; /* FQDNs not to route elsewhere */
169 static void load_config( char *filespec );
170 static void split_address( char *address, char **localpart, char **domainpart);
171 static int entry_engine( LDAPMessage *e, char *dn, char *address, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
172 static void do_address( char *name, char ***to, int *nto, Group ***togroups, int *ngroups, Error **err, int *nerr, int type );
173 static void send_message( char **to );
174 static void send_errors( Error *err, int nerr );
175 static void do_noemail( FILE *fp, Error *err, int namelen );
176 static void do_ambiguous( FILE *fp, Error *err, int namelen );
177 static int count_values( char **list );
178 static void add_to( char ***list, int *nlist, char **new );
179 static void add_single_to( char ***list, char *new );
180 static int isgroup( LDAPMessage *e );
181 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
182 static void unbind_and_exit( int rc ) LDAP_GCCATTR((noreturn));
183 static void send_group( Group **group, int ngroup );
185 static int connect_to_x500( void );
189 main ( int argc, char **argv )
195 int numto, ngroups, numerr, nargs;
197 char *conffile = NULL;
199 if ( (myname = strrchr( argv[0], '/' )) == NULL )
200 myname = strdup( argv[0] );
202 myname = strdup( myname + 1 );
205 (void) SIGNAL( SIGPIPE, SIG_IGN );
209 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
211 openlog( myname, OPENLOG_OPTIONS );
214 while ( (i = getopt( argc, argv, "d:C:f:h:l:m:v:" )) != EOF ) {
216 case 'd': /* turn on debugging */
217 debug = atoi( optarg );
220 case 'C': /* path to configuration file */
221 conffile = strdup( optarg );
224 case 'f': /* who it's from & where errors should go */
225 mailfrom = strdup( optarg );
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, *domainpart;
309 /* TBC: Make this processing optional */
310 /* for ( j = 0; argv[i][j] != '\0'; j++ ) { */
311 /* if ( argv[i][j] == '.' || argv[i][j] == '_' ) */
312 /* argv[i][j] = ' '; */
316 split_address( argv[i], &localpart, &domainpart );
317 if ( (s = strrchr( localpart, '-' )) != NULL ) {
320 if ((strcasecmp(s, ERROR) == 0) ||
321 (strcasecmp(s, ERRORS) == 0)) {
324 } else if ((strcasecmp(s, REQUEST) == 0) ||
325 (strcasecmp(s, REQUESTS) == 0)) {
326 type = GROUP_REQUEST;
328 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
329 type = GROUP_MEMBERS;
331 } else if ((strcasecmp(s, OWNER) == 0) ||
332 (strcasecmp(s, OWNERS) == 0)) {
339 sprintf( address, "%s@%s", localpart, domainpart );
343 sprintf( address, "%s@%s", localpart, domainpart );
346 do_address( address, &tolist, &numto, &togroups, &ngroups,
347 &errlist, &numerr, type );
351 * If we have both errors and successful deliveries to make or if
352 * if there are any groups to deliver to, we basically need to read
353 * the message twice. So, we have to put it in a tmp file.
356 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
361 if ( (fp = tmpfile()) == NULL ) {
362 syslog( LOG_ALERT, "could not open tmp file" );
363 unbind_and_exit( EX_TEMPFAIL );
366 /* copy the message to a temp file */
367 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
368 if ( fputs( buf, fp ) == EOF ) {
369 syslog( LOG_ALERT, "error writing tmpfile" );
370 unbind_and_exit( EX_TEMPFAIL );
374 if ( dup2( fileno( fp ), 0 ) == -1 ) {
375 syslog( LOG_ALERT, "could not dup2 tmpfile" );
376 unbind_and_exit( EX_TEMPFAIL );
382 /* deal with errors */
385 syslog( LOG_ALERT, "sending errors" );
387 (void) rewind( stdin );
388 send_errors( errlist, numerr );
391 (void) ldap_unbind( ld );
393 /* send to groups with errorsTo */
396 syslog( LOG_ALERT, "sending to groups with errorsto" );
398 (void) rewind( stdin );
399 send_group( togroups, ngroups );
402 /* send to expanded aliases and groups w/o errorsTo */
403 if ( numto > nargs ) {
405 syslog( LOG_ALERT, "sending to aliases and groups" );
407 (void) rewind( stdin );
408 send_message( tolist );
415 get_config_line( FILE *cf, int *lineno)
417 static char buf[2048];
423 room = sizeof( buf );
424 while ( fgets( &buf[pos], room, cf ) ) {
427 /* Delete whitespace at the beginning of new data */
428 if ( isspace( buf[pos] ) ) {
430 for ( s = buf+pos; isspace(*s); s++ )
432 for ( d = buf+pos; *s; s++, d++ ) {
439 if ( buf[len-1] != '\n' ) {
440 syslog( LOG_ALERT, "Definition too long at line %d",
446 if ( strspn( buf, " \t\n" ) == len )
448 if ( buf[len-2] == '\\' ) {
450 room = sizeof(buf) - pos;
453 /* We have a real line, we will exit the loop */
461 add_url ( char *url, int rdnpref, int typemask )
467 b = calloc(1, sizeof(Base));
469 syslog( LOG_ALERT, "Out of memory" );
472 b->b_url = strdup( url );
473 b->b_rdnpref = rdnpref;
474 b->b_search = typemask;
476 if ( base == NULL ) {
477 base = calloc(2, sizeof(LDAPURLDesc *));
479 syslog( LOG_ALERT, "Out of memory" );
484 for ( size = 0; base[size]; size++ )
487 list_temp = realloc( base, size*sizeof(LDAPURLDesc *) );
489 syslog( LOG_ALERT, "Out of memory" );
499 add_def_attr( char *s )
505 p += strspn( p, "\t," );
506 q = strpbrk( p, " \t," );
509 add_single_to( &def_attr, p );
511 add_single_to( &def_attr, p );
519 add_attr_semantics( char *s )
524 as = calloc( 1, sizeof( AttrSemantics ) );
525 as->as_priority = current_priority;
527 while ( isspace ( *p ) )
530 while ( !isspace ( *q ) && *q != '\0' )
533 as->as_name = strdup( p );
537 while ( isspace ( *p ) )
540 while ( !isspace ( *q ) && *q != '\0' )
543 if ( !strcasecmp( p, "multivalued" ) ) {
545 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
546 as->as_m_entries = 1;
547 } else if ( !strcasecmp( p, "local-native-mailbox" ) ) {
548 as->as_syntax = AS_SYNTAX_NATIVE_MB;
549 } else if ( !strcasecmp( p, "rfc822" ) ) {
550 as->as_syntax = AS_SYNTAX_RFC822;
551 } else if ( !strcasecmp( p, "rfc822-extended" ) ) {
552 as->as_syntax = AS_SYNTAX_RFC822_EXT;
553 } else if ( !strcasecmp( p, "dn" ) ) {
554 as->as_syntax = AS_SYNTAX_DN;
555 } else if ( !strcasecmp( p, "url" ) ) {
556 as->as_syntax = AS_SYNTAX_URL;
557 } else if ( !strncasecmp( p, "search-with-filter=", 19 ) ) {
558 as->as_syntax = AS_SYNTAX_BOOL_FILTER;
559 q = strchr( p, '=' );
562 while ( *q && !isspace( *q ) ) {
567 as->as_param = strdup( p );
570 as->as_param = strdup( p );
575 "Missing filter in %s", s );
578 } else if ( !strcasecmp( p, "host" ) ) {
579 as->as_kind = AS_SYNTAX_HOST;
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, "recipient" ) ) {
585 as->as_kind = AS_KIND_RECIPIENT;
586 } else if ( !strcasecmp( p, "errors" ) ) {
587 as->as_kind = AS_KIND_ERRORS;
588 } else if ( !strcasecmp( p, "request" ) ) {
589 as->as_kind = AS_KIND_REQUEST;
590 } else if ( !strcasecmp( p, "owner" ) ) {
591 as->as_kind = AS_KIND_OWNER;
594 "Unknown semantics word %s", p );
599 if ( attr_semantics == NULL ) {
600 attr_semantics = calloc(2, sizeof(AttrSemantics *));
601 if ( !attr_semantics ) {
602 syslog( LOG_ALERT, "Out of memory" );
605 attr_semantics[0] = as;
608 AttrSemantics **list_temp;
609 for ( size = 0; attr_semantics[size]; size++ )
612 list_temp = realloc( attr_semantics,
613 size*sizeof(AttrSemantics *) );
615 syslog( LOG_ALERT, "Out of memory" );
618 attr_semantics = list_temp;
619 attr_semantics[size-2] = as;
620 attr_semantics[size-1] = NULL;
625 load_config( char *filespec )
634 cf = fopen( filespec, "r" );
636 perror( "Opening config file" );
640 while ( ( line = get_config_line( cf,&lineno ) ) ) {
641 p = strpbrk( line, " \t" );
644 "Missing space at line %d", lineno );
647 if ( !strncmp( line, "search", p-line ) ) {
648 p += strspn( p, " \t" );
652 add_url( p, rdnpref, typemask );
653 } else if ( !strncmp(line, "attribute", p-line) ) {
654 p += strspn(p, " \t");
655 add_attr_semantics( p );
656 } else if ( !strncmp(line, "default-attributes", p-line) ) {
657 p += strspn(p, " \t");
659 } else if ( !strncmp(line, "group-classes", p-line) ) {
660 p += strspn(p, " \t");
661 add_single_to( &groupclasses, p );
662 } else if ( !strncmp(line, "priority", p-line) ) {
663 p += strspn(p, " \t");
664 current_priority = atoi(p);
665 } else if ( !strncmp(line, "domain", p-line) ) {
666 p += strspn(p, " \t");
667 add_single_to( &mydomains, 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 ( strcmp( 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,
780 "(objectclass=*)", def_attr, 0,
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_NOTLDAP:
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 */
1088 for ( d = mydomains; *d; d++ ) {
1089 if ( !strcmp(*d,domain) ) {
1097 * The entry engine processes an entry. Normally, each entry will resolve
1098 * to one or more values that will be added to the 'to' argument. This
1099 * argument needs not be the global 'to' list, it may be the g_to field
1100 * in a group. Groups have no special treatment, unless they require
1121 char ***current_to = to;
1122 int *current_nto = nto;
1123 Group *current_group = NULL;
1125 char *localpart, *domainpart;
1127 int cur_priority = 0;
1128 char *route_to_host = NULL;
1129 char *route_to_address = NULL;
1132 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1133 AttrSemantics *as = attr_semantics[i];
1136 if ( as->as_priority < cur_priority ) {
1138 * We already got higher priority information,
1139 * so no further work to do, ignore the rest.
1143 vals = ldap_get_values( ld, e, as->as_name );
1144 if ( !vals || vals[0] == NULL ) {
1147 nent = count_values( vals );
1148 if ( nent > 1 && !as->as_m_valued ) {
1149 add_error( err, nerr, E_AMBIGUOUS, address, e );
1152 switch ( as->as_kind ) {
1153 case AS_KIND_RECIPIENT:
1154 cur_priority = as->as_priority;
1155 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1157 switch ( as->as_syntax ) {
1158 case AS_SYNTAX_RFC822:
1159 add_to( current_to, current_nto, vals );
1162 case AS_SYNTAX_RFC822_EXT:
1163 add_to( current_to, current_nto, vals );
1166 case AS_SYNTAX_NATIVE_MB:
1167 /* We used to concatenate mailHost if set here */
1169 * We used to send a copy to the vacation host
1170 * if onVacation to uid@vacationhost
1172 add_to( current_to, current_nto, vals );
1177 if ( dn_search( vals, address,
1178 current_to, current_nto,
1186 if ( url_list_search( vals, address,
1188 current_to, current_nto,
1190 err, nerr, type ) ) {
1195 case AS_SYNTAX_BOOL_FILTER:
1196 if ( strcasecmp( vals[0], "true" ) ) {
1199 substs[0].sub_char = 'D';
1200 substs[0].sub_value = dn;
1201 substs[1].sub_char = '\0';
1202 substs[1].sub_value = NULL;
1203 if ( url_list_search( vals, address,
1205 current_to, current_nto,
1207 err, nerr, type ) ) {
1214 "Invalid syntax %d for kind %d",
1215 as->as_syntax, as->as_kind );
1220 case AS_KIND_ERRORS:
1221 cur_priority = as->as_priority;
1222 /* This is a group with special processing */
1223 if ( type & GROUP_ERRORS ) {
1224 switch (as->as_kind) {
1225 case AS_SYNTAX_RFC822:
1226 add_to( current_to, current_nto, vals );
1232 "Invalid syntax %d for kind %d",
1233 as->as_syntax, as->as_kind );
1236 current_group = new_group( dn, togroups,
1238 current_to = ¤t_group->g_members;
1239 current_nto = ¤t_group->g_nmembers;
1240 split_address( address,
1241 &localpart, &domainpart );
1243 sprintf( buf, "%s-%s@%s",
1249 sprintf( buf, "%s-%s@%s",
1254 current_group->g_errorsto = strdup( buf );
1258 case AS_KIND_REQUEST:
1259 cur_priority = as->as_priority;
1260 /* This is a group with special processing */
1261 if ( type & GROUP_REQUEST ) {
1262 add_to( current_to, current_nto, vals );
1268 cur_priority = as->as_priority;
1269 /* This is a group with special processing */
1270 if ( type & GROUP_REQUEST ) {
1271 add_to( current_to, current_nto, vals );
1276 case AS_KIND_ROUTE_TO_HOST:
1277 if ( !is_my_domain( vals[0] ) ) {
1278 cur_priority = as->as_priority;
1279 route_to_host = strdup( vals[0] );
1283 case AS_KIND_ROUTE_TO_ADDR:
1284 if ( strcmp( vals[0], address ) ) {
1285 cur_priority = as->as_priority;
1286 route_to_address = strdup( vals[0] );
1292 "Invalid kind %d", as->as_kind );
1295 ldap_value_free( vals );
1297 if ( route_to_host ) {
1299 if ( !route_to_address ) {
1300 route_to_address = strdup( address );
1302 /* This makes use of the percent hack, but there's no choice */
1303 p = strchr( route_to_address, '@' );
1307 sprintf( buf, "%s@%s", route_to_address, route_to_host );
1310 add_to( current_to, current_nto, nvals );
1312 free( route_to_host );
1313 free( route_to_address );
1314 } else if ( route_to_address ) {
1315 nvals[0] = route_to_address;
1317 add_to( current_to, current_nto, nvals );
1319 free( route_to_address );
1339 int b, resolved = 0;
1341 for ( b = 0; base[b] != NULL; b++ ) {
1343 if ( ! (base[b]->b_search & type) ) {
1347 resolved = search_ldap_url( base[b]->b_url, substs, name,
1349 base[b]->b_m_entries,
1350 to, nto, togroups, ngroups,
1370 struct timeval timeout;
1371 char *localpart, *domainpart;
1376 * Look up the name in X.500, add the appropriate addresses found
1377 * to the to list, or to the err list in case of error. Groups are
1378 * handled by the do_group routine, individuals are handled here.
1379 * When looking up name, we follow the bases hierarchy, looking
1380 * in base[0] first, then base[1], etc. For each base, there is
1381 * a set of search filters to try, in order. If something goes
1382 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1383 * If the b_rdnpref flag is set, then we give preference to entries
1384 * that matched name because it's their rdn, otherwise not.
1387 split_address( name, &localpart, &domainpart );
1388 timeout.tv_sec = MAIL500_TIMEOUT;
1389 timeout.tv_usec = 0;
1390 substs[0].sub_char = 'm';
1391 substs[0].sub_value = name;
1392 substs[1].sub_char = 'h';
1393 substs[1].sub_value = host;
1394 substs[2].sub_char = 'l';
1395 substs[2].sub_value = localpart;
1396 substs[3].sub_char = 'd';
1397 substs[3].sub_value = domainpart;
1398 substs[4].sub_char = '\0';
1399 substs[4].sub_value = NULL;
1401 resolved = search_bases( NULL, substs, name,
1402 to, nto, togroups, ngroups,
1406 /* not resolved - bounce with user unknown */
1407 if ( type == USER ) {
1408 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1410 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1416 send_message( char **to )
1419 #ifndef HAVE_WAITPID
1420 WAITSTATUSTYPE status;
1427 strcpy( buf, to[0] );
1428 for ( i = 1; to[i] != NULL; i++ ) {
1430 strcat( buf, to[i] );
1433 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1437 if ( (pid = fork()) != 0 ) {
1439 waitpid( pid, (int *) NULL, 0 );
1441 wait4( pid, &status, WAIT_FLAGS, 0 );
1445 /* to includes sendmailargs */
1446 execv( MAIL500_SENDMAIL, to );
1448 syslog( LOG_ALERT, "execv failed" );
1450 exit( EX_TEMPFAIL );
1455 send_group( Group **group, int ngroup )
1461 #ifndef HAVE_WAITPID
1462 WAITSTATUSTYPE status;
1465 for ( i = 0; i < ngroup; i++ ) {
1466 (void) rewind( stdin );
1468 iargv[0] = MAIL500_SENDMAIL;
1470 iargv[2] = group[i]->g_errorsto;
1471 iargv[3] = "-oMrX.500";
1478 add_to( &argv, &argc, iargv );
1479 add_to( &argv, &argc, group[i]->g_members );
1485 strcpy( buf, argv[0] );
1486 for ( i = 1; i < argc; i++ ) {
1488 strcat( buf, argv[i] );
1491 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1495 if ( (pid = fork()) != 0 ) {
1497 waitpid( pid, (int *) NULL, 0 );
1499 wait4( pid, &status, WAIT_FLAGS, 0 );
1503 execv( MAIL500_SENDMAIL, argv );
1505 syslog( LOG_ALERT, "execv failed" );
1507 exit( EX_TEMPFAIL );
1513 send_errors( Error *err, int nerr )
1515 int pid, i, namelen;
1520 #ifndef HAVE_WAITPID
1521 WAITSTATUSTYPE status;
1524 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1525 mailfrom = errorsfrom;
1528 argv[0] = MAIL500_SENDMAIL;
1529 argv[1] = "-oMrX.500";
1533 argv[5] = MAIL500_BOUNCEFROM;
1540 strcpy( buf, argv[0] );
1541 for ( i = 1; argv[i] != NULL; i++ ) {
1543 strcat( buf, argv[i] );
1546 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1549 if ( pipe( fd ) == -1 ) {
1550 syslog( LOG_ALERT, "cannot create pipe" );
1551 exit( EX_TEMPFAIL );
1554 if ( (pid = fork()) != 0 ) {
1555 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1556 syslog( LOG_ALERT, "cannot fdopen pipe" );
1557 exit( EX_TEMPFAIL );
1560 fprintf( fp, "To: %s\n", mailfrom );
1561 fprintf( fp, "From: %s\n", errorsfrom );
1562 fprintf( fp, "Subject: undeliverable mail\n" );
1563 fprintf( fp, "\n" );
1564 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1565 for ( i = 0; i < nerr; i++ ) {
1566 namelen = strlen( err[i].e_addr );
1567 fprintf( fp, "\n" );
1569 switch ( err[i].e_code ) {
1571 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1574 case E_GROUPUNKNOWN:
1575 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1579 fprintf( fp, "%s: Group member does not exist\n",
1581 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1582 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1583 fprintf( fp, "then re-adding the person to the group.\n" );
1587 fprintf( fp, "%s: Group exists but has no request address\n",
1592 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1597 fprintf( fp, "%s: Group exists but has no owner\n",
1602 do_ambiguous( fp, &err[i], namelen );
1606 do_noemail( fp, &err[i], namelen );
1609 case E_MEMBERNOEMAIL:
1610 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1614 case E_JOINMEMBERNOEMAIL:
1615 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1620 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1621 err[i].e_addr, err[i].e_loop );
1625 fprintf( fp, "%s: Group has no members\n",
1630 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1631 unbind_and_exit( EX_TEMPFAIL );
1636 fprintf( fp, "\n------- The original message sent:\n\n" );
1638 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1644 waitpid( pid, (int *) NULL, 0 );
1646 wait4( pid, &status, WAIT_FLAGS, 0 );
1651 execv( MAIL500_SENDMAIL, argv );
1653 syslog( LOG_ALERT, "execv failed" );
1655 exit( EX_TEMPFAIL );
1660 do_noemail( FILE *fp, Error *err, int namelen )
1666 fprintf(fp, "%s: User has no email address registered.\n",
1668 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1669 namelen, " ", err->e_addr );
1672 dn = ldap_get_dn( ld, err->e_msg );
1673 ufn = ldap_explode_dn( dn, 1 );
1674 rdn = strdup( ufn[0] );
1675 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1676 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1678 for ( i = 0; vals[i]; i++ ) {
1679 last = strlen( vals[i] ) - 1;
1680 if ( isdigit((unsigned char) vals[i][last]) ) {
1681 rdn = strdup( vals[i] );
1686 ldap_value_free( vals );
1689 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1692 ldap_value_free( ufn );
1694 /* titles or descriptions */
1695 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1696 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1698 fprintf( fp, "%*s No title or description registered\n",
1701 for ( i = 0; vals[i] != NULL; i++ ) {
1702 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1705 ldap_value_free( vals );
1708 /* postal address */
1709 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1711 fprintf( fp, "%*s No postal address registered\n", namelen,
1714 fprintf( fp, "%*s ", namelen, " " );
1715 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1716 if ( vals[0][i] == '$' ) {
1717 fprintf( fp, "\n%*s ", namelen, " " );
1718 while ( isspace((unsigned char) vals[0][i+1]) )
1721 fprintf( fp, "%c", vals[0][i] );
1724 fprintf( fp, "\n" );
1726 ldap_value_free( vals );
1729 /* telephone number */
1730 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1732 fprintf( fp, "%*s No phone number registered\n", namelen,
1735 for ( i = 0; vals[i] != NULL; i++ ) {
1736 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1739 ldap_value_free( vals );
1745 do_ambiguous( FILE *fp, Error *err, int namelen )
1752 i = ldap_result2error( ld, err->e_msg, 0 );
1754 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1755 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1756 ldap_count_entries( ld, err->e_msg ) );
1758 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1759 e = ldap_next_entry( ld, e ) ) {
1760 dn = ldap_get_dn( ld, e );
1761 ufn = ldap_explode_dn( dn, 1 );
1762 rdn = strdup( ufn[0] );
1763 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1764 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1765 for ( i = 0; vals[i]; i++ ) {
1766 last = strlen( vals[i] ) - 1;
1767 if (isdigit((unsigned char) vals[i][last])) {
1768 rdn = strdup( vals[i] );
1773 ldap_value_free( vals );
1778 if ( isgroup( e ) ) {
1779 vals = ldap_get_values( ld, e, "description" );
1781 vals = ldap_get_values( ld, e, "title" );
1784 vals = ldap_get_values( ld, e, "description" );
1786 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1787 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1788 fprintf( fp, " %s\n", vals[i] );
1793 ldap_value_free( ufn );
1795 ldap_value_free( vals );
1800 count_values( char **list )
1804 for ( i = 0; list && list[i] != NULL; i++ )
1811 add_to( char ***list, int *nlist, char **new )
1813 int i, nnew, oldnlist;
1815 nnew = count_values( new );
1818 if ( *list == NULL || *nlist == 0 ) {
1819 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1822 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1823 nnew * sizeof(char *) + sizeof(char *) );
1827 for ( i = 0; i < nnew; i++ )
1828 (*list)[i + oldnlist] = strdup( new[i] );
1829 (*list)[*nlist] = NULL;
1833 add_single_to( char ***list, char *new )
1837 if ( *list == NULL ) {
1839 *list = (char **) malloc( 2 * sizeof(char *) );
1841 nlist = count_values( *list );
1842 *list = (char **) realloc( *list,
1843 ( nlist + 2 ) * sizeof(char *) );
1846 (*list)[nlist] = strdup( new );
1847 (*list)[nlist+1] = NULL;
1851 isgroup( LDAPMessage *e )
1856 if ( !groupclasses ) {
1860 oclist = ldap_get_values( ld, e, "objectClass" );
1862 for ( i = 0; oclist[i] != NULL; i++ ) {
1863 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1864 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1865 ldap_value_free( oclist );
1870 ldap_value_free( oclist );
1876 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1879 *err = (Error *) malloc( sizeof(Error) );
1881 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1884 (*err)[*nerr].e_code = code;
1885 (*err)[*nerr].e_addr = strdup( addr );
1886 (*err)[*nerr].e_msg = msg;
1891 unbind_and_exit( int rc )
1895 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1896 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );