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_final; /* If true, no further expansion is tried. */
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_FORWARD_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
156 AttrSemantics **attr_semantics = NULL;
158 typedef struct subst {
163 char **groupclasses = NULL;
164 char **def_attr = NULL;
166 static void load_config( char *filespec );
167 static void split_address( char *address, char **localpart, char **domainpart);
168 static int entry_engine( LDAPMessage *e, char *dn, char *address, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
169 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
170 static void send_message( char **to );
171 static void send_errors( Error *err, int nerr );
172 static void do_noemail( FILE *fp, Error *err, int namelen );
173 static void do_ambiguous( FILE *fp, Error *err, int namelen );
174 static int count_values( char **list );
175 static void add_to( char ***list, int *nlist, char **new );
176 static void add_single_to( char ***list, char *new );
177 static int isgroup( LDAPMessage *e );
178 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
179 static void unbind_and_exit( int rc ) LDAP_GCCATTR((noreturn));
180 static void send_group( Group *group, int ngroup );
182 static int connect_to_x500( void );
186 main ( int argc, char **argv )
192 int numto, ngroups, numerr, nargs;
194 char *conffile = NULL;
196 if ( (myname = strrchr( argv[0], '/' )) == NULL )
197 myname = strdup( argv[0] );
199 myname = strdup( myname + 1 );
202 (void) SIGNAL( SIGPIPE, SIG_IGN );
206 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
208 openlog( myname, OPENLOG_OPTIONS );
211 while ( (i = getopt( argc, argv, "d:C:f:h:l:m:v:" )) != EOF ) {
213 case 'd': /* turn on debugging */
214 debug = atoi( optarg );
217 case 'C': /* path to configuration file */
218 conffile = strdup( optarg );
221 case 'f': /* who it's from & where errors should go */
222 mailfrom = strdup( optarg );
223 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
224 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
225 sendmailargs[j+1] = mailfrom;
231 case 'h': /* hostname */
232 host = strdup( optarg );
233 hostlen = strlen(host);
236 case 'l': /* ldap host */
237 ldaphost = strdup( optarg );
240 /* mailer-daemon address - who we should */
241 case 'm': /* say errors come from */
242 errorsfrom = strdup( optarg );
245 case 'v': /* vacation host */
246 vacationhost = strdup( optarg );
250 syslog( LOG_ALERT, "unknown option" );
255 if ( mailfrom == NULL ) {
256 syslog( LOG_ALERT, "required argument -f not present" );
259 if ( errorsfrom == NULL ) {
260 syslog( LOG_ALERT, "required argument -m not present" );
263 /* if ( host == NULL ) { */
264 /* syslog( LOG_ALERT, "required argument -h not present" ); */
265 /* exit( EX_TEMPFAIL ); */
267 if ( conffile == NULL ) {
268 syslog( LOG_ALERT, "required argument -C not present" );
272 load_config( conffile );
274 if ( connect_to_x500() != 0 )
283 syslog( LOG_ALERT, "running as %d", geteuid() );
284 strcpy( buf, argv[0] );
285 for ( i = 1; i < argc; i++ ) {
287 strcat( buf, argv[i] );
290 syslog( LOG_ALERT, "args: (%s)", buf );
295 add_to( &tolist, &numto, sendmailargs );
297 ngroups = numerr = 0;
300 for ( i = optind; i < argc; i++ ) {
303 char *localpart, *domainpart;
306 /* TBC: Make this processing optional */
307 /* for ( j = 0; argv[i][j] != '\0'; j++ ) { */
308 /* if ( argv[i][j] == '.' || argv[i][j] == '_' ) */
309 /* argv[i][j] = ' '; */
313 split_address( argv[i], &localpart, &domainpart );
314 if ( (s = strrchr( localpart, '-' )) != NULL ) {
317 if ((strcasecmp(s, ERROR) == 0) ||
318 (strcasecmp(s, ERRORS) == 0)) {
321 } else if ((strcasecmp(s, REQUEST) == 0) ||
322 (strcasecmp(s, REQUESTS) == 0)) {
323 type = GROUP_REQUEST;
325 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
326 type = GROUP_MEMBERS;
328 } else if ((strcasecmp(s, OWNER) == 0) ||
329 (strcasecmp(s, OWNERS) == 0)) {
336 sprintf( address, "%s@%s", localpart, domainpart );
340 sprintf( address, "%s@%s", localpart, domainpart );
343 do_address( address, &tolist, &numto, &togroups, &ngroups,
344 &errlist, &numerr, type );
348 * If we have both errors and successful deliveries to make or if
349 * if there are any groups to deliver to, we basically need to read
350 * the message twice. So, we have to put it in a tmp file.
353 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
358 if ( (fp = tmpfile()) == NULL ) {
359 syslog( LOG_ALERT, "could not open tmp file" );
360 unbind_and_exit( EX_TEMPFAIL );
363 /* copy the message to a temp file */
364 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
365 if ( fputs( buf, fp ) == EOF ) {
366 syslog( LOG_ALERT, "error writing tmpfile" );
367 unbind_and_exit( EX_TEMPFAIL );
371 if ( dup2( fileno( fp ), 0 ) == -1 ) {
372 syslog( LOG_ALERT, "could not dup2 tmpfile" );
373 unbind_and_exit( EX_TEMPFAIL );
379 /* deal with errors */
382 syslog( LOG_ALERT, "sending errors" );
384 (void) rewind( stdin );
385 send_errors( errlist, numerr );
388 (void) ldap_unbind( ld );
390 /* send to groups with errorsTo */
393 syslog( LOG_ALERT, "sending to groups with errorsto" );
395 (void) rewind( stdin );
396 send_group( togroups, ngroups );
399 /* send to expanded aliases and groups w/o errorsTo */
400 if ( numto > nargs ) {
402 syslog( LOG_ALERT, "sending to aliases and groups" );
404 (void) rewind( stdin );
405 send_message( tolist );
412 get_config_line( FILE *cf, int *lineno)
414 static char buf[2048];
420 room = sizeof( buf );
421 while ( fgets( &buf[pos], room, cf ) ) {
424 /* Delete whitespace at the beginning of new data */
425 if ( isspace( buf[pos] ) ) {
427 for ( s = buf+pos; isspace(*s); s++ )
429 for ( d = buf+pos; *s; s++, d++ ) {
436 if ( buf[len-1] != '\n' ) {
437 syslog( LOG_ALERT, "Definition too long at line %d",
443 if ( strspn( buf, " \t\n" ) == len )
445 if ( buf[len-2] == '\\' ) {
447 room = sizeof(buf) - pos;
450 /* We have a real line, we will exit the loop */
458 add_url ( char *url, int rdnpref, int typemask )
464 b = calloc(1, sizeof(Base));
466 syslog( LOG_ALERT, "Out of memory" );
469 b->b_url = strdup( url );
470 b->b_rdnpref = rdnpref;
471 b->b_search = typemask;
473 if ( base == NULL ) {
474 base = calloc(2, sizeof(LDAPURLDesc *));
476 syslog( LOG_ALERT, "Out of memory" );
481 for ( size = 0; base[size]; size++ )
484 list_temp = realloc( base, size*sizeof(LDAPURLDesc *) );
486 syslog( LOG_ALERT, "Out of memory" );
496 add_def_attr( char *s )
502 p += strspn( p, "\t," );
503 q = strpbrk( p, " \t," );
506 add_single_to( &def_attr, p );
508 add_single_to( &def_attr, p );
516 add_attr_semantics( char *s )
521 as = calloc( 1, sizeof( AttrSemantics ) );
523 while ( isspace ( *p ) )
526 while ( !isspace ( *q ) && *q != '\0' )
529 as->as_name = strdup( p );
533 while ( isspace ( *p ) )
536 while ( !isspace ( *q ) && *q != '\0' )
539 if ( !strcasecmp( p, "multivalued" ) ) {
541 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
542 as->as_m_entries = 1;
543 } else if ( !strcasecmp( p, "final" ) ) {
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, "forward-to-host" ) ) {
579 as->as_kind = AS_KIND_FORWARD_TO_HOST;
580 } else if ( !strcasecmp( p, "recipient" ) ) {
581 as->as_kind = AS_KIND_RECIPIENT;
582 } else if ( !strcasecmp( p, "errors" ) ) {
583 as->as_kind = AS_KIND_ERRORS;
584 } else if ( !strcasecmp( p, "request" ) ) {
585 as->as_kind = AS_KIND_REQUEST;
586 } else if ( !strcasecmp( p, "owner" ) ) {
587 as->as_kind = AS_KIND_OWNER;
590 "Unknown semantics word %s", p );
595 if ( attr_semantics == NULL ) {
596 attr_semantics = calloc(2, sizeof(AttrSemantics *));
597 if ( !attr_semantics ) {
598 syslog( LOG_ALERT, "Out of memory" );
601 attr_semantics[0] = as;
604 AttrSemantics **list_temp;
605 for ( size = 0; attr_semantics[size]; size++ )
608 list_temp = realloc( attr_semantics,
609 size*sizeof(AttrSemantics *) );
611 syslog( LOG_ALERT, "Out of memory" );
614 attr_semantics = list_temp;
615 attr_semantics[size-2] = as;
616 attr_semantics[size-1] = NULL;
621 load_config( char *filespec )
630 cf = fopen( filespec, "r" );
632 perror( "Opening config file" );
636 while ( ( line = get_config_line( cf,&lineno ) ) ) {
637 p = strpbrk( line, " \t" );
640 "Missing space at line %d", lineno );
643 if ( !strncmp( line, "search", p-line ) ) {
644 p += strspn( p, " \t" );
648 add_url( p, rdnpref, typemask );
649 } else if ( !strncmp(line, "attribute", p-line) ) {
650 p += strspn(p, " \t");
651 add_attr_semantics( p );
652 } else if ( !strncmp(line, "default-attributes", p-line) ) {
653 p += strspn(p, " \t");
655 } else if ( !strncmp(line, "group-classes", p-line) ) {
656 p += strspn(p, " \t");
657 add_single_to( &groupclasses, p );
660 "Unparseable config definition at line %d",
669 connect_to_x500( void )
673 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
674 syslog( LOG_ALERT, "ldap_init failed" );
678 /* TBC: Set this only when it makes sense
679 opt = MAIL500_MAXAMBIGUOUS;
680 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
682 opt = LDAP_DEREF_ALWAYS;
683 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
685 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
686 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
694 new_group( char *dn, Group **list, int *nlist )
699 for ( i = 0; i < *nlist; i++ ) {
700 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
701 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
707 *list = (Group *) malloc( sizeof(Group) );
709 *list = (Group *) realloc( *list, (*nlist + 1) *
715 (*list)[*nlist].g_errorsto = NULL;
716 (*list)[*nlist].g_members = NULL;
717 (*list)[*nlist].g_nmembers = 0;
718 /* save the group's dn so we can check for loops above */
719 (*list)[*nlist].g_dn = strdup( dn );
723 return( this_group );
735 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
736 *localpart = strdup( address );
739 *localpart = malloc( p - address + 1 );
740 strncpy( *localpart, address, p - address );
741 (*localpart)[p - address] = '\0';
743 *domainpart = strdup( p );
762 LDAPMessage *res, *e;
763 struct timeval timeout;
765 timeout.tv_sec = MAIL500_TIMEOUT;
767 for ( i = 0; dnlist[i]; i++ ) {
768 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
769 "(objectclass=*)", def_attr, 0,
770 &timeout, &res )) != LDAP_SUCCESS ) {
771 if ( rc == LDAP_NO_SUCH_OBJECT ) {
772 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
775 syslog( LOG_ALERT, "member search return 0x%x", rc );
777 unbind_and_exit( EX_TEMPFAIL );
780 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
781 syslog( LOG_ALERT, "member search error parsing entry" );
782 unbind_and_exit( EX_TEMPFAIL );
784 if ( entry_engine( e, dnlist[i], address, to, nto,
785 togroups, ngroups, err, nerr,
786 USER | GROUP_MEMBERS ) ) {
814 char realfilter[1024];
815 LDAPMessage *e, *res;
818 struct timeval timeout;
823 timeout.tv_sec = MAIL500_TIMEOUT;
826 rc = ldap_url_parse( url, &ludp );
829 case LDAP_URL_ERR_NOTLDAP:
831 "Not an LDAP URL: %s", url );
833 case LDAP_URL_ERR_BADENCLOSURE:
835 "Bad Enclosure in URL: %s", url );
837 case LDAP_URL_ERR_BADURL:
839 "Bad URL: %s", url );
841 case LDAP_URL_ERR_BADHOST:
843 "Host is invalid in URL: %s", url );
845 case LDAP_URL_ERR_BADATTRS:
847 "Attributes are invalid in URL: %s", url );
849 case LDAP_URL_ERR_BADSCOPE:
851 "Scope is invalid in URL: %s", url );
853 case LDAP_URL_ERR_BADFILTER:
855 "Filter is invalid in URL: %s", url );
857 case LDAP_URL_ERR_BADEXTS:
859 "Extensions are invalid in URL: %s", url );
861 case LDAP_URL_ERR_MEM:
863 "Out of memory parsing URL: %s", url );
865 case LDAP_URL_ERR_PARAM:
867 "bad parameter parsing URL: %s", url );
871 "Unknown error %d parsing URL: %s",
875 add_error( err, nerr, E_BADMEMBER,
881 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
888 for ( i = 0; substs[i].sub_char != '\0';
890 if ( *s == substs[i].sub_char ) {
891 for ( p = substs[i].sub_value;
899 if ( substs[i].sub_char == '\0' ) {
901 "unknown format %c", *s );
909 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
910 filter[ sizeof( filter ) - 1 ] = '\0';
913 for ( s = filter, d = realfilter; *s; s++, d++ ) {
921 if ( ludp->lud_attrs ) {
922 attrlist = ludp->lud_attrs;
927 /* TBC: we don't read the host, dammit */
928 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
929 realfilter, attrlist, 0,
932 /* some other trouble - try again later */
933 if ( rc != LDAP_SUCCESS &&
934 rc != LDAP_SIZELIMIT_EXCEEDED ) {
935 syslog( LOG_ALERT, "return 0x%x from X.500",
937 unbind_and_exit( EX_TEMPFAIL );
940 match = ldap_count_entries( ld, res );
942 /* trouble - try again later */
944 syslog( LOG_ALERT, "error parsing result from X.500" );
945 unbind_and_exit( EX_TEMPFAIL );
948 if ( match == 1 || multi_entry ) {
949 for ( e = ldap_first_entry( ld, res ); e != NULL;
950 e = ldap_next_entry( ld, e ) ) {
951 dn = ldap_get_dn( ld, e );
952 resolved = entry_engine( e, dn, address, to, nto,
956 add_error( err, nerr, E_NOEMAIL, address, res );
962 /* more than one match - bounce with ambiguous user? */
964 LDAPMessage *next, *tmpres = NULL;
968 /* not giving rdn preference - bounce with ambiguous user */
969 if ( rdnpref == 0 ) {
970 add_error( err, nerr, E_AMBIGUOUS, address, res );
975 * giving rdn preference - see if any entries were matched
976 * because of their rdn. If so, collect them to deal with
977 * later (== 1 we deliver, > 1 we bounce).
980 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
981 next = ldap_next_entry( ld, e );
982 dn = ldap_get_dn( ld, e );
983 xdn = ldap_explode_dn( dn, 1 );
985 /* XXX bad, but how else can we do it? XXX */
986 if ( strcasecmp( xdn[0], address ) == 0 ) {
987 ldap_delete_result_entry( &res, e );
988 ldap_add_result_entry( &tmpres, e );
991 ldap_value_free( xdn );
995 /* nothing matched by rdn - go ahead and bounce */
996 if ( tmpres == NULL ) {
997 add_error( err, nerr, E_AMBIGUOUS, address, res );
1000 /* more than one matched by rdn - bounce with rdn matches */
1001 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1002 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1006 } else if ( match < 0 ) {
1007 syslog( LOG_ALERT, "error parsing result from X.500" );
1008 unbind_and_exit( EX_TEMPFAIL );
1011 /* otherwise one matched by rdn - send to it */
1012 ldap_msgfree( res );
1016 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1017 syslog( LOG_ALERT, "error parsing entry from X.500" );
1018 unbind_and_exit( EX_TEMPFAIL );
1021 dn = ldap_get_dn( ld, e );
1023 resolved = entry_engine( e, dn, address, to, nto,
1027 add_error( err, nerr, E_NOEMAIL, address, res );
1028 /* Don't free res if we passed it to add_error */
1030 ldap_msgfree( res );
1053 for ( i = 0; urllist[i]; i++ ) {
1055 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1058 vals[0] = urllist[i] + 5;
1060 add_to( to, nto, vals );
1063 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1065 resolved = search_ldap_url( urllist[i], NULL,
1066 address, 0, multi_entry,
1067 to, nto, togroups, ngroups,
1070 /* Produce some sensible error here */
1078 * The entry engine processes an entry. Normally, each entry will resolve
1079 * to one or more values that will be added to the 'to' argument. This
1080 * argument needs not be the global 'to' list, it may be the g_to field
1081 * in a group. Groups have no special treatment, unless they require
1102 char ***current_to = to;
1103 int *current_nto = nto;
1104 Group *current_group = NULL;
1106 char *localpart, *domainpart;
1109 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1110 AttrSemantics *as = attr_semantics[i];
1113 vals = ldap_get_values( ld, e, as->as_name );
1114 if ( !vals || vals[0] == NULL ) {
1117 nent = count_values( vals );
1118 if ( nent > 1 && !as->as_m_valued ) {
1119 add_error( err, nerr, E_AMBIGUOUS, address, e );
1122 switch ( as->as_kind ) {
1123 case AS_KIND_RECIPIENT:
1124 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1126 switch ( as->as_syntax ) {
1127 case AS_SYNTAX_RFC822:
1128 add_to( current_to, current_nto, vals );
1131 case AS_SYNTAX_RFC822_EXT:
1132 add_to( current_to, current_nto, vals );
1135 case AS_SYNTAX_NATIVE_MB:
1136 /* We used to concatenate mailHost if set here */
1138 * We used to send a copy to the vacation host
1139 * if onVacation to uid@vacationhost
1141 add_to( current_to, current_nto, vals );
1146 if ( dn_search( vals, address,
1147 current_to, current_nto,
1155 if ( url_list_search( vals, address,
1157 current_to, current_nto,
1159 err, nerr, type ) ) {
1164 case AS_SYNTAX_BOOL_FILTER:
1165 if ( strcasecmp( vals[0], "true" ) ) {
1168 substs[0].sub_char = 'D';
1169 substs[0].sub_value = dn;
1170 substs[1].sub_char = '\0';
1171 substs[1].sub_value = NULL;
1172 if ( url_list_search( vals, address,
1174 current_to, current_nto,
1176 err, nerr, type ) ) {
1183 "Invalid syntax %d for kind %d",
1184 as->as_syntax, as->as_kind );
1189 case AS_KIND_ERRORS:
1190 /* This is a group with special processing */
1191 if ( type & GROUP_ERRORS ) {
1192 switch (as->as_kind) {
1193 case AS_SYNTAX_RFC822:
1194 add_to( current_to, current_nto, vals );
1200 "Invalid syntax %d for kind %d",
1201 as->as_syntax, as->as_kind );
1204 current_group = new_group( dn, togroups,
1206 current_to = ¤t_group->g_members;
1207 current_nto = ¤t_group->g_nmembers;
1208 split_address( address,
1209 &localpart, &domainpart );
1211 sprintf( buf, "%s-%s@%s",
1217 sprintf( buf, "%s-%s@%s",
1222 current_group->g_errorsto = strdup( buf );
1226 case AS_KIND_REQUEST:
1227 /* This is a group with special processing */
1228 if ( type & GROUP_REQUEST ) {
1229 add_to( current_to, current_nto, vals );
1235 /* This is a group with special processing */
1236 if ( type & GROUP_REQUEST ) {
1237 add_to( current_to, current_nto, vals );
1244 "Invalid kind %d", as->as_kind );
1247 ldap_value_free( vals );
1248 if ( as->as_final ) {
1269 int b, resolved = 0;
1271 for ( b = 0; base[b] != NULL; b++ ) {
1273 if ( ! (base[b]->b_search & type) ) {
1277 resolved = search_ldap_url( base[b]->b_url, substs, name,
1279 base[b]->b_m_entries,
1280 to, nto, togroups, ngroups,
1300 struct timeval timeout;
1301 char *localpart, *domainpart;
1306 * Look up the name in X.500, add the appropriate addresses found
1307 * to the to list, or to the err list in case of error. Groups are
1308 * handled by the do_group routine, individuals are handled here.
1309 * When looking up name, we follow the bases hierarchy, looking
1310 * in base[0] first, then base[1], etc. For each base, there is
1311 * a set of search filters to try, in order. If something goes
1312 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1313 * If the b_rdnpref flag is set, then we give preference to entries
1314 * that matched name because it's their rdn, otherwise not.
1317 split_address( name, &localpart, &domainpart );
1318 timeout.tv_sec = MAIL500_TIMEOUT;
1319 timeout.tv_usec = 0;
1320 substs[0].sub_char = 'm';
1321 substs[0].sub_value = name;
1322 substs[1].sub_char = 'h';
1323 substs[1].sub_value = host;
1324 substs[2].sub_char = 'l';
1325 substs[2].sub_value = localpart;
1326 substs[3].sub_char = 'd';
1327 substs[3].sub_value = domainpart;
1328 substs[4].sub_char = '\0';
1329 substs[4].sub_value = NULL;
1331 resolved = search_bases( NULL, substs, name,
1332 to, nto, togroups, ngroups,
1336 /* not resolved - bounce with user unknown */
1337 if ( type == USER ) {
1338 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1340 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1346 send_message( char **to )
1349 #ifndef HAVE_WAITPID
1350 WAITSTATUSTYPE status;
1357 strcpy( buf, to[0] );
1358 for ( i = 1; to[i] != NULL; i++ ) {
1360 strcat( buf, to[i] );
1363 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1367 if ( (pid = fork()) != 0 ) {
1369 waitpid( pid, (int *) NULL, 0 );
1371 wait4( pid, &status, WAIT_FLAGS, 0 );
1375 /* to includes sendmailargs */
1376 execv( MAIL500_SENDMAIL, to );
1378 syslog( LOG_ALERT, "execv failed" );
1380 exit( EX_TEMPFAIL );
1385 send_group( Group *group, int ngroup )
1391 #ifndef HAVE_WAITPID
1392 WAITSTATUSTYPE status;
1395 for ( i = 0; i < ngroup; i++ ) {
1396 (void) rewind( stdin );
1398 iargv[0] = MAIL500_SENDMAIL;
1400 iargv[2] = group[i].g_errorsto;
1401 iargv[3] = "-oMrX.500";
1408 add_to( &argv, &argc, iargv );
1409 add_to( &argv, &argc, group[i].g_members );
1415 strcpy( buf, argv[0] );
1416 for ( i = 1; i < argc; i++ ) {
1418 strcat( buf, argv[i] );
1421 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1425 if ( (pid = fork()) != 0 ) {
1427 waitpid( pid, (int *) NULL, 0 );
1429 wait4( pid, &status, WAIT_FLAGS, 0 );
1433 execv( MAIL500_SENDMAIL, argv );
1435 syslog( LOG_ALERT, "execv failed" );
1437 exit( EX_TEMPFAIL );
1443 send_errors( Error *err, int nerr )
1445 int pid, i, namelen;
1450 #ifndef HAVE_WAITPID
1451 WAITSTATUSTYPE status;
1454 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1455 mailfrom = errorsfrom;
1458 argv[0] = MAIL500_SENDMAIL;
1459 argv[1] = "-oMrX.500";
1463 argv[5] = MAIL500_BOUNCEFROM;
1470 strcpy( buf, argv[0] );
1471 for ( i = 1; argv[i] != NULL; i++ ) {
1473 strcat( buf, argv[i] );
1476 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1479 if ( pipe( fd ) == -1 ) {
1480 syslog( LOG_ALERT, "cannot create pipe" );
1481 exit( EX_TEMPFAIL );
1484 if ( (pid = fork()) != 0 ) {
1485 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1486 syslog( LOG_ALERT, "cannot fdopen pipe" );
1487 exit( EX_TEMPFAIL );
1490 fprintf( fp, "To: %s\n", mailfrom );
1491 fprintf( fp, "From: %s\n", errorsfrom );
1492 fprintf( fp, "Subject: undeliverable mail\n" );
1493 fprintf( fp, "\n" );
1494 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1495 for ( i = 0; i < nerr; i++ ) {
1496 namelen = strlen( err[i].e_addr );
1497 fprintf( fp, "\n" );
1499 switch ( err[i].e_code ) {
1501 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1504 case E_GROUPUNKNOWN:
1505 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1509 fprintf( fp, "%s: Group member does not exist\n",
1511 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1512 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1513 fprintf( fp, "then re-adding the person to the group.\n" );
1517 fprintf( fp, "%s: Group exists but has no request address\n",
1522 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1527 fprintf( fp, "%s: Group exists but has no owner\n",
1532 do_ambiguous( fp, &err[i], namelen );
1536 do_noemail( fp, &err[i], namelen );
1539 case E_MEMBERNOEMAIL:
1540 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1544 case E_JOINMEMBERNOEMAIL:
1545 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1550 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1551 err[i].e_addr, err[i].e_loop );
1555 fprintf( fp, "%s: Group has no members\n",
1560 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1561 unbind_and_exit( EX_TEMPFAIL );
1566 fprintf( fp, "\n------- The original message sent:\n\n" );
1568 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1574 waitpid( pid, (int *) NULL, 0 );
1576 wait4( pid, &status, WAIT_FLAGS, 0 );
1581 execv( MAIL500_SENDMAIL, argv );
1583 syslog( LOG_ALERT, "execv failed" );
1585 exit( EX_TEMPFAIL );
1590 do_noemail( FILE *fp, Error *err, int namelen )
1596 fprintf(fp, "%s: User has no email address registered.\n",
1598 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1599 namelen, " ", err->e_addr );
1602 dn = ldap_get_dn( ld, err->e_msg );
1603 ufn = ldap_explode_dn( dn, 1 );
1604 rdn = strdup( ufn[0] );
1605 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1606 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1608 for ( i = 0; vals[i]; i++ ) {
1609 last = strlen( vals[i] ) - 1;
1610 if ( isdigit((unsigned char) vals[i][last]) ) {
1611 rdn = strdup( vals[i] );
1616 ldap_value_free( vals );
1619 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1622 ldap_value_free( ufn );
1624 /* titles or descriptions */
1625 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1626 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1628 fprintf( fp, "%*s No title or description registered\n",
1631 for ( i = 0; vals[i] != NULL; i++ ) {
1632 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1635 ldap_value_free( vals );
1638 /* postal address */
1639 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1641 fprintf( fp, "%*s No postal address registered\n", namelen,
1644 fprintf( fp, "%*s ", namelen, " " );
1645 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1646 if ( vals[0][i] == '$' ) {
1647 fprintf( fp, "\n%*s ", namelen, " " );
1648 while ( isspace((unsigned char) vals[0][i+1]) )
1651 fprintf( fp, "%c", vals[0][i] );
1654 fprintf( fp, "\n" );
1656 ldap_value_free( vals );
1659 /* telephone number */
1660 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1662 fprintf( fp, "%*s No phone number registered\n", namelen,
1665 for ( i = 0; vals[i] != NULL; i++ ) {
1666 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1669 ldap_value_free( vals );
1675 do_ambiguous( FILE *fp, Error *err, int namelen )
1682 i = ldap_result2error( ld, err->e_msg, 0 );
1684 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1685 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1686 ldap_count_entries( ld, err->e_msg ) );
1688 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1689 e = ldap_next_entry( ld, e ) ) {
1690 dn = ldap_get_dn( ld, e );
1691 ufn = ldap_explode_dn( dn, 1 );
1692 rdn = strdup( ufn[0] );
1693 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1694 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1695 for ( i = 0; vals[i]; i++ ) {
1696 last = strlen( vals[i] ) - 1;
1697 if (isdigit((unsigned char) vals[i][last])) {
1698 rdn = strdup( vals[i] );
1703 ldap_value_free( vals );
1708 if ( isgroup( e ) ) {
1709 vals = ldap_get_values( ld, e, "description" );
1711 vals = ldap_get_values( ld, e, "title" );
1714 vals = ldap_get_values( ld, e, "description" );
1716 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1717 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1718 fprintf( fp, " %s\n", vals[i] );
1723 ldap_value_free( ufn );
1725 ldap_value_free( vals );
1730 count_values( char **list )
1734 for ( i = 0; list && list[i] != NULL; i++ )
1741 add_to( char ***list, int *nlist, char **new )
1743 int i, nnew, oldnlist;
1745 nnew = count_values( new );
1748 if ( *list == NULL || *nlist == 0 ) {
1749 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1752 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1753 nnew * sizeof(char *) + sizeof(char *) );
1757 for ( i = 0; i < nnew; i++ )
1758 (*list)[i + oldnlist] = strdup( new[i] );
1759 (*list)[*nlist] = NULL;
1763 add_single_to( char ***list, char *new )
1767 if ( *list == NULL ) {
1769 *list = (char **) malloc( 2 * sizeof(char *) );
1771 nlist = count_values( *list );
1772 *list = (char **) realloc( *list,
1773 ( nlist + 2 ) * sizeof(char *) );
1776 (*list)[nlist] = strdup( new );
1777 (*list)[nlist+1] = NULL;
1781 isgroup( LDAPMessage *e )
1786 if ( !groupclasses ) {
1790 oclist = ldap_get_values( ld, e, "objectClass" );
1792 for ( i = 0; oclist[i] != NULL; i++ ) {
1793 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1794 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1795 ldap_value_free( oclist );
1800 ldap_value_free( oclist );
1806 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1809 *err = (Error *) malloc( sizeof(Error) );
1811 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1814 (*err)[*nerr].e_code = code;
1815 (*err)[*nerr].e_addr = strdup( addr );
1816 (*err)[*nerr].e_msg = msg;
1821 unbind_and_exit( int rc )
1825 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1826 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );