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 );
706 this_group = (Group *) malloc( sizeof(Group) );
709 *list = (Group **) malloc( sizeof(Group *) );
711 *list = (Group **) realloc( *list, (*nlist + 1) *
715 this_group->g_errorsto = NULL;
716 this_group->g_members = NULL;
717 this_group->g_nmembers = 0;
718 /* save the group's dn so we can check for loops above */
719 this_group->g_dn = strdup( dn );
721 (*list)[*nlist] = this_group;
724 return( this_group );
736 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
737 *localpart = strdup( address );
740 *localpart = malloc( p - address + 1 );
741 strncpy( *localpart, address, p - address );
742 (*localpart)[p - address] = '\0';
744 *domainpart = strdup( p );
763 LDAPMessage *res, *e;
764 struct timeval timeout;
766 timeout.tv_sec = MAIL500_TIMEOUT;
768 for ( i = 0; dnlist[i]; i++ ) {
769 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
770 "(objectclass=*)", def_attr, 0,
771 &timeout, &res )) != LDAP_SUCCESS ) {
772 if ( rc == LDAP_NO_SUCH_OBJECT ) {
773 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
776 syslog( LOG_ALERT, "member search return 0x%x", rc );
778 unbind_and_exit( EX_TEMPFAIL );
781 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
782 syslog( LOG_ALERT, "member search error parsing entry" );
783 unbind_and_exit( EX_TEMPFAIL );
785 if ( entry_engine( e, dnlist[i], address, to, nto,
786 togroups, ngroups, err, nerr,
787 USER | GROUP_MEMBERS ) ) {
815 char realfilter[1024];
816 LDAPMessage *e, *res;
819 struct timeval timeout;
824 timeout.tv_sec = MAIL500_TIMEOUT;
827 rc = ldap_url_parse( url, &ludp );
830 case LDAP_URL_ERR_NOTLDAP:
832 "Not an LDAP URL: %s", url );
834 case LDAP_URL_ERR_BADENCLOSURE:
836 "Bad Enclosure in URL: %s", url );
838 case LDAP_URL_ERR_BADURL:
840 "Bad URL: %s", url );
842 case LDAP_URL_ERR_BADHOST:
844 "Host is invalid in URL: %s", url );
846 case LDAP_URL_ERR_BADATTRS:
848 "Attributes are invalid in URL: %s", url );
850 case LDAP_URL_ERR_BADSCOPE:
852 "Scope is invalid in URL: %s", url );
854 case LDAP_URL_ERR_BADFILTER:
856 "Filter is invalid in URL: %s", url );
858 case LDAP_URL_ERR_BADEXTS:
860 "Extensions are invalid in URL: %s", url );
862 case LDAP_URL_ERR_MEM:
864 "Out of memory parsing URL: %s", url );
866 case LDAP_URL_ERR_PARAM:
868 "bad parameter parsing URL: %s", url );
872 "Unknown error %d parsing URL: %s",
876 add_error( err, nerr, E_BADMEMBER,
882 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
889 for ( i = 0; substs[i].sub_char != '\0';
891 if ( *s == substs[i].sub_char ) {
892 for ( p = substs[i].sub_value;
900 if ( substs[i].sub_char == '\0' ) {
902 "unknown format %c", *s );
910 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
911 filter[ sizeof( filter ) - 1 ] = '\0';
914 for ( s = filter, d = realfilter; *s; s++, d++ ) {
922 if ( ludp->lud_attrs ) {
923 attrlist = ludp->lud_attrs;
928 /* TBC: we don't read the host, dammit */
929 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
930 realfilter, attrlist, 0,
933 /* some other trouble - try again later */
934 if ( rc != LDAP_SUCCESS &&
935 rc != LDAP_SIZELIMIT_EXCEEDED ) {
936 syslog( LOG_ALERT, "return 0x%x from X.500",
938 unbind_and_exit( EX_TEMPFAIL );
941 match = ldap_count_entries( ld, res );
943 /* trouble - try again later */
945 syslog( LOG_ALERT, "error parsing result from X.500" );
946 unbind_and_exit( EX_TEMPFAIL );
949 if ( match == 1 || multi_entry ) {
950 for ( e = ldap_first_entry( ld, res ); e != NULL;
951 e = ldap_next_entry( ld, e ) ) {
952 dn = ldap_get_dn( ld, e );
953 resolved = entry_engine( e, dn, address, to, nto,
957 add_error( err, nerr, E_NOEMAIL, address, res );
963 /* more than one match - bounce with ambiguous user? */
965 LDAPMessage *next, *tmpres = NULL;
969 /* not giving rdn preference - bounce with ambiguous user */
970 if ( rdnpref == 0 ) {
971 add_error( err, nerr, E_AMBIGUOUS, address, res );
976 * giving rdn preference - see if any entries were matched
977 * because of their rdn. If so, collect them to deal with
978 * later (== 1 we deliver, > 1 we bounce).
981 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
982 next = ldap_next_entry( ld, e );
983 dn = ldap_get_dn( ld, e );
984 xdn = ldap_explode_dn( dn, 1 );
986 /* XXX bad, but how else can we do it? XXX */
987 if ( strcasecmp( xdn[0], address ) == 0 ) {
988 ldap_delete_result_entry( &res, e );
989 ldap_add_result_entry( &tmpres, e );
992 ldap_value_free( xdn );
996 /* nothing matched by rdn - go ahead and bounce */
997 if ( tmpres == NULL ) {
998 add_error( err, nerr, E_AMBIGUOUS, address, res );
1001 /* more than one matched by rdn - bounce with rdn matches */
1002 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1003 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1007 } else if ( match < 0 ) {
1008 syslog( LOG_ALERT, "error parsing result from X.500" );
1009 unbind_and_exit( EX_TEMPFAIL );
1012 /* otherwise one matched by rdn - send to it */
1013 ldap_msgfree( res );
1017 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1018 syslog( LOG_ALERT, "error parsing entry from X.500" );
1019 unbind_and_exit( EX_TEMPFAIL );
1022 dn = ldap_get_dn( ld, e );
1024 resolved = entry_engine( e, dn, address, to, nto,
1028 add_error( err, nerr, E_NOEMAIL, address, res );
1029 /* Don't free res if we passed it to add_error */
1031 ldap_msgfree( res );
1054 for ( i = 0; urllist[i]; i++ ) {
1056 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1059 vals[0] = urllist[i] + 5;
1061 add_to( to, nto, vals );
1064 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1066 resolved = search_ldap_url( urllist[i], NULL,
1067 address, 0, multi_entry,
1068 to, nto, togroups, ngroups,
1071 /* Produce some sensible error here */
1079 * The entry engine processes an entry. Normally, each entry will resolve
1080 * to one or more values that will be added to the 'to' argument. This
1081 * argument needs not be the global 'to' list, it may be the g_to field
1082 * in a group. Groups have no special treatment, unless they require
1103 char ***current_to = to;
1104 int *current_nto = nto;
1105 Group *current_group = NULL;
1107 char *localpart, *domainpart;
1110 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1111 AttrSemantics *as = attr_semantics[i];
1114 vals = ldap_get_values( ld, e, as->as_name );
1115 if ( !vals || vals[0] == NULL ) {
1118 nent = count_values( vals );
1119 if ( nent > 1 && !as->as_m_valued ) {
1120 add_error( err, nerr, E_AMBIGUOUS, address, e );
1123 switch ( as->as_kind ) {
1124 case AS_KIND_RECIPIENT:
1125 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1127 switch ( as->as_syntax ) {
1128 case AS_SYNTAX_RFC822:
1129 add_to( current_to, current_nto, vals );
1132 case AS_SYNTAX_RFC822_EXT:
1133 add_to( current_to, current_nto, vals );
1136 case AS_SYNTAX_NATIVE_MB:
1137 /* We used to concatenate mailHost if set here */
1139 * We used to send a copy to the vacation host
1140 * if onVacation to uid@vacationhost
1142 add_to( current_to, current_nto, vals );
1147 if ( dn_search( vals, address,
1148 current_to, current_nto,
1156 if ( url_list_search( vals, address,
1158 current_to, current_nto,
1160 err, nerr, type ) ) {
1165 case AS_SYNTAX_BOOL_FILTER:
1166 if ( strcasecmp( vals[0], "true" ) ) {
1169 substs[0].sub_char = 'D';
1170 substs[0].sub_value = dn;
1171 substs[1].sub_char = '\0';
1172 substs[1].sub_value = NULL;
1173 if ( url_list_search( vals, address,
1175 current_to, current_nto,
1177 err, nerr, type ) ) {
1184 "Invalid syntax %d for kind %d",
1185 as->as_syntax, as->as_kind );
1190 case AS_KIND_ERRORS:
1191 /* This is a group with special processing */
1192 if ( type & GROUP_ERRORS ) {
1193 switch (as->as_kind) {
1194 case AS_SYNTAX_RFC822:
1195 add_to( current_to, current_nto, vals );
1201 "Invalid syntax %d for kind %d",
1202 as->as_syntax, as->as_kind );
1205 current_group = new_group( dn, togroups,
1207 current_to = ¤t_group->g_members;
1208 current_nto = ¤t_group->g_nmembers;
1209 split_address( address,
1210 &localpart, &domainpart );
1212 sprintf( buf, "%s-%s@%s",
1218 sprintf( buf, "%s-%s@%s",
1223 current_group->g_errorsto = strdup( buf );
1227 case AS_KIND_REQUEST:
1228 /* This is a group with special processing */
1229 if ( type & GROUP_REQUEST ) {
1230 add_to( current_to, current_nto, vals );
1236 /* This is a group with special processing */
1237 if ( type & GROUP_REQUEST ) {
1238 add_to( current_to, current_nto, vals );
1245 "Invalid kind %d", as->as_kind );
1248 ldap_value_free( vals );
1249 if ( as->as_final ) {
1270 int b, resolved = 0;
1272 for ( b = 0; base[b] != NULL; b++ ) {
1274 if ( ! (base[b]->b_search & type) ) {
1278 resolved = search_ldap_url( base[b]->b_url, substs, name,
1280 base[b]->b_m_entries,
1281 to, nto, togroups, ngroups,
1301 struct timeval timeout;
1302 char *localpart, *domainpart;
1307 * Look up the name in X.500, add the appropriate addresses found
1308 * to the to list, or to the err list in case of error. Groups are
1309 * handled by the do_group routine, individuals are handled here.
1310 * When looking up name, we follow the bases hierarchy, looking
1311 * in base[0] first, then base[1], etc. For each base, there is
1312 * a set of search filters to try, in order. If something goes
1313 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1314 * If the b_rdnpref flag is set, then we give preference to entries
1315 * that matched name because it's their rdn, otherwise not.
1318 split_address( name, &localpart, &domainpart );
1319 timeout.tv_sec = MAIL500_TIMEOUT;
1320 timeout.tv_usec = 0;
1321 substs[0].sub_char = 'm';
1322 substs[0].sub_value = name;
1323 substs[1].sub_char = 'h';
1324 substs[1].sub_value = host;
1325 substs[2].sub_char = 'l';
1326 substs[2].sub_value = localpart;
1327 substs[3].sub_char = 'd';
1328 substs[3].sub_value = domainpart;
1329 substs[4].sub_char = '\0';
1330 substs[4].sub_value = NULL;
1332 resolved = search_bases( NULL, substs, name,
1333 to, nto, togroups, ngroups,
1337 /* not resolved - bounce with user unknown */
1338 if ( type == USER ) {
1339 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1341 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1347 send_message( char **to )
1350 #ifndef HAVE_WAITPID
1351 WAITSTATUSTYPE status;
1358 strcpy( buf, to[0] );
1359 for ( i = 1; to[i] != NULL; i++ ) {
1361 strcat( buf, to[i] );
1364 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1368 if ( (pid = fork()) != 0 ) {
1370 waitpid( pid, (int *) NULL, 0 );
1372 wait4( pid, &status, WAIT_FLAGS, 0 );
1376 /* to includes sendmailargs */
1377 execv( MAIL500_SENDMAIL, to );
1379 syslog( LOG_ALERT, "execv failed" );
1381 exit( EX_TEMPFAIL );
1386 send_group( Group *group, int ngroup )
1392 #ifndef HAVE_WAITPID
1393 WAITSTATUSTYPE status;
1396 for ( i = 0; i < ngroup; i++ ) {
1397 (void) rewind( stdin );
1399 iargv[0] = MAIL500_SENDMAIL;
1401 iargv[2] = group[i].g_errorsto;
1402 iargv[3] = "-oMrX.500";
1409 add_to( &argv, &argc, iargv );
1410 add_to( &argv, &argc, group[i].g_members );
1416 strcpy( buf, argv[0] );
1417 for ( i = 1; i < argc; i++ ) {
1419 strcat( buf, argv[i] );
1422 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1426 if ( (pid = fork()) != 0 ) {
1428 waitpid( pid, (int *) NULL, 0 );
1430 wait4( pid, &status, WAIT_FLAGS, 0 );
1434 execv( MAIL500_SENDMAIL, argv );
1436 syslog( LOG_ALERT, "execv failed" );
1438 exit( EX_TEMPFAIL );
1444 send_errors( Error *err, int nerr )
1446 int pid, i, namelen;
1451 #ifndef HAVE_WAITPID
1452 WAITSTATUSTYPE status;
1455 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1456 mailfrom = errorsfrom;
1459 argv[0] = MAIL500_SENDMAIL;
1460 argv[1] = "-oMrX.500";
1464 argv[5] = MAIL500_BOUNCEFROM;
1471 strcpy( buf, argv[0] );
1472 for ( i = 1; argv[i] != NULL; i++ ) {
1474 strcat( buf, argv[i] );
1477 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1480 if ( pipe( fd ) == -1 ) {
1481 syslog( LOG_ALERT, "cannot create pipe" );
1482 exit( EX_TEMPFAIL );
1485 if ( (pid = fork()) != 0 ) {
1486 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1487 syslog( LOG_ALERT, "cannot fdopen pipe" );
1488 exit( EX_TEMPFAIL );
1491 fprintf( fp, "To: %s\n", mailfrom );
1492 fprintf( fp, "From: %s\n", errorsfrom );
1493 fprintf( fp, "Subject: undeliverable mail\n" );
1494 fprintf( fp, "\n" );
1495 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1496 for ( i = 0; i < nerr; i++ ) {
1497 namelen = strlen( err[i].e_addr );
1498 fprintf( fp, "\n" );
1500 switch ( err[i].e_code ) {
1502 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1505 case E_GROUPUNKNOWN:
1506 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1510 fprintf( fp, "%s: Group member does not exist\n",
1512 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1513 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1514 fprintf( fp, "then re-adding the person to the group.\n" );
1518 fprintf( fp, "%s: Group exists but has no request address\n",
1523 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1528 fprintf( fp, "%s: Group exists but has no owner\n",
1533 do_ambiguous( fp, &err[i], namelen );
1537 do_noemail( fp, &err[i], namelen );
1540 case E_MEMBERNOEMAIL:
1541 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1545 case E_JOINMEMBERNOEMAIL:
1546 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1551 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1552 err[i].e_addr, err[i].e_loop );
1556 fprintf( fp, "%s: Group has no members\n",
1561 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1562 unbind_and_exit( EX_TEMPFAIL );
1567 fprintf( fp, "\n------- The original message sent:\n\n" );
1569 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1575 waitpid( pid, (int *) NULL, 0 );
1577 wait4( pid, &status, WAIT_FLAGS, 0 );
1582 execv( MAIL500_SENDMAIL, argv );
1584 syslog( LOG_ALERT, "execv failed" );
1586 exit( EX_TEMPFAIL );
1591 do_noemail( FILE *fp, Error *err, int namelen )
1597 fprintf(fp, "%s: User has no email address registered.\n",
1599 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1600 namelen, " ", err->e_addr );
1603 dn = ldap_get_dn( ld, err->e_msg );
1604 ufn = ldap_explode_dn( dn, 1 );
1605 rdn = strdup( ufn[0] );
1606 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1607 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1609 for ( i = 0; vals[i]; i++ ) {
1610 last = strlen( vals[i] ) - 1;
1611 if ( isdigit((unsigned char) vals[i][last]) ) {
1612 rdn = strdup( vals[i] );
1617 ldap_value_free( vals );
1620 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1623 ldap_value_free( ufn );
1625 /* titles or descriptions */
1626 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1627 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1629 fprintf( fp, "%*s No title or description registered\n",
1632 for ( i = 0; vals[i] != NULL; i++ ) {
1633 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1636 ldap_value_free( vals );
1639 /* postal address */
1640 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1642 fprintf( fp, "%*s No postal address registered\n", namelen,
1645 fprintf( fp, "%*s ", namelen, " " );
1646 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1647 if ( vals[0][i] == '$' ) {
1648 fprintf( fp, "\n%*s ", namelen, " " );
1649 while ( isspace((unsigned char) vals[0][i+1]) )
1652 fprintf( fp, "%c", vals[0][i] );
1655 fprintf( fp, "\n" );
1657 ldap_value_free( vals );
1660 /* telephone number */
1661 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1663 fprintf( fp, "%*s No phone number registered\n", namelen,
1666 for ( i = 0; vals[i] != NULL; i++ ) {
1667 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1670 ldap_value_free( vals );
1676 do_ambiguous( FILE *fp, Error *err, int namelen )
1683 i = ldap_result2error( ld, err->e_msg, 0 );
1685 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1686 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1687 ldap_count_entries( ld, err->e_msg ) );
1689 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1690 e = ldap_next_entry( ld, e ) ) {
1691 dn = ldap_get_dn( ld, e );
1692 ufn = ldap_explode_dn( dn, 1 );
1693 rdn = strdup( ufn[0] );
1694 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1695 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1696 for ( i = 0; vals[i]; i++ ) {
1697 last = strlen( vals[i] ) - 1;
1698 if (isdigit((unsigned char) vals[i][last])) {
1699 rdn = strdup( vals[i] );
1704 ldap_value_free( vals );
1709 if ( isgroup( e ) ) {
1710 vals = ldap_get_values( ld, e, "description" );
1712 vals = ldap_get_values( ld, e, "title" );
1715 vals = ldap_get_values( ld, e, "description" );
1717 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1718 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1719 fprintf( fp, " %s\n", vals[i] );
1724 ldap_value_free( ufn );
1726 ldap_value_free( vals );
1731 count_values( char **list )
1735 for ( i = 0; list && list[i] != NULL; i++ )
1742 add_to( char ***list, int *nlist, char **new )
1744 int i, nnew, oldnlist;
1746 nnew = count_values( new );
1749 if ( *list == NULL || *nlist == 0 ) {
1750 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1753 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1754 nnew * sizeof(char *) + sizeof(char *) );
1758 for ( i = 0; i < nnew; i++ )
1759 (*list)[i + oldnlist] = strdup( new[i] );
1760 (*list)[*nlist] = NULL;
1764 add_single_to( char ***list, char *new )
1768 if ( *list == NULL ) {
1770 *list = (char **) malloc( 2 * sizeof(char *) );
1772 nlist = count_values( *list );
1773 *list = (char **) realloc( *list,
1774 ( nlist + 2 ) * sizeof(char *) );
1777 (*list)[nlist] = strdup( new );
1778 (*list)[nlist+1] = NULL;
1782 isgroup( LDAPMessage *e )
1787 if ( !groupclasses ) {
1791 oclist = ldap_get_values( ld, e, "objectClass" );
1793 for ( i = 0; oclist[i] != NULL; i++ ) {
1794 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1795 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1796 ldap_value_free( oclist );
1801 ldap_value_free( oclist );
1807 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1810 *err = (Error *) malloc( sizeof(Error) );
1812 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1815 (*err)[*nerr].e_code = code;
1816 (*err)[*nerr].e_addr = strdup( addr );
1817 (*err)[*nerr].e_msg = msg;
1822 unbind_and_exit( int rc )
1826 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1827 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );