2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
12 * Copyright 1998,1999 The OpenLDAP Foundation
13 * COPYING RESTRICTIONS APPLY. See COPYRIGHT File in top level directory
14 * of this package for details.
21 #include <ac/stdlib.h>
24 #include <ac/signal.h>
25 #include <ac/string.h>
26 #include <ac/sysexits.h>
27 #include <ac/syslog.h>
29 #include <ac/unistd.h>
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
45 #include "ldap_defaults.h"
47 #ifndef MAIL500_BOUNCEFROM
48 #define MAIL500_BOUNCEFROM "<>"
52 #define GROUP_ERRORS 0x02
53 #define GROUP_REQUEST 0x04
54 #define GROUP_MEMBERS 0x08
55 #define GROUP_OWNER 0x10
58 #define ERRORS "errors"
59 #define REQUEST "request"
60 #define REQUESTS "requests"
61 #define MEMBERS "members"
63 #define OWNERS "owners"
66 char *vacationhost = NULL;
67 char *errorsfrom = MAIL500_BOUNCEFROM;
68 char *mailfrom = NULL;
70 char *ldaphost = NULL;
76 #define E_USERUNKNOWN 1
82 #define E_JOINMEMBERNOEMAIL 7
83 #define E_MEMBERNOEMAIL 8
85 #define E_NOMEMBERS 10
87 #define E_GROUPUNKNOWN 12
93 #define e_msg e_union.e_u_msg
94 #define e_loop e_union.e_u_loop
97 typedef struct groupto {
104 typedef struct baseinfo {
107 char b_rdnpref; /* give rdn's preference when searching? */
108 int b_search; /* ORed with the type of thing the address */
109 /* looks like (USER, GROUP_ERRORS, etc.) */
110 /* to see if this should be searched */
114 * We should limit the search to objectclass=mailRecipient or
115 * objectclass=mailGroup.
120 {"dc=StlInter, dc=Net",
122 {"mail=%s", "mailAlternateAddress=%s", NULL}},
129 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
131 typedef struct attr_semantics {
133 int as_m_valued; /* Is multivalued? */
134 int as_final; /* If true, no further expansion is tried. */
135 int as_syntax; /* How to interpret values */
136 int as_m_entries; /* Can resolve to several entries? */
137 int as_kind; /* Recipient, sender, etc. */
138 char *as_param; /* Extra info for filters and things alike */
141 #define AS_SYNTAX_UNKNOWN 0 /* Unqualified mailbox name */
142 #define AS_SYNTAX_NATIVE_MB 1 /* Unqualified mailbox name */
143 #define AS_SYNTAX_RFC822 2 /* RFC822 mail address */
144 #define AS_SYNTAX_HOST 3
145 #define AS_SYNTAX_DN 4 /* A directory entry */
146 #define AS_SYNTAX_RFC822_EXT 5
147 #define AS_SYNTAX_URL 6 /* mailto: or ldap: URL */
148 #define AS_SYNTAX_BOOL_FILTER 7 /* For joinable, filter in as_param */
150 #define AS_KIND_UNKNOWN 0
151 #define AS_KIND_RECIPIENT 1
152 #define AS_KIND_ERRORS 2 /* For ErrorsTo and similar */
153 #define AS_KIND_REQUEST 3
154 #define AS_KIND_OWNER 4
155 #define AS_KIND_FORWARD_TO_HOST 5 /* Expand at some other host */
156 #define AS_KIND_ALLOWED_SENDER 6 /* Can send to group */
157 #define AS_KIND_MODERATOR 7
159 AttrSemantics **attr_semantics = NULL;
161 typedef struct subst {
166 char **groupclasses = NULL;
167 char **def_attr = NULL;
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 );
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 ) );
526 while ( isspace ( *p ) )
529 while ( !isspace ( *q ) && *q != '\0' )
532 as->as_name = strdup( p );
536 while ( isspace ( *p ) )
539 while ( !isspace ( *q ) && *q != '\0' )
542 if ( !strcasecmp( p, "multivalued" ) ) {
544 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
545 as->as_m_entries = 1;
546 } else if ( !strcasecmp( p, "final" ) ) {
548 } else if ( !strcasecmp( p, "local-native-mailbox" ) ) {
549 as->as_syntax = AS_SYNTAX_NATIVE_MB;
550 } else if ( !strcasecmp( p, "rfc822" ) ) {
551 as->as_syntax = AS_SYNTAX_RFC822;
552 } else if ( !strcasecmp( p, "rfc822-extended" ) ) {
553 as->as_syntax = AS_SYNTAX_RFC822_EXT;
554 } else if ( !strcasecmp( p, "dn" ) ) {
555 as->as_syntax = AS_SYNTAX_DN;
556 } else if ( !strcasecmp( p, "url" ) ) {
557 as->as_syntax = AS_SYNTAX_URL;
558 } else if ( !strncasecmp( p, "search-with-filter=", 19 ) ) {
559 as->as_syntax = AS_SYNTAX_BOOL_FILTER;
560 q = strchr( p, '=' );
563 while ( *q && !isspace( *q ) ) {
568 as->as_param = strdup( p );
571 as->as_param = strdup( p );
576 "Missing filter in %s", s );
579 } else if ( !strcasecmp( p, "host" ) ) {
580 as->as_kind = AS_SYNTAX_HOST;
581 } else if ( !strcasecmp( p, "forward-to-host" ) ) {
582 as->as_kind = AS_KIND_FORWARD_TO_HOST;
583 } else if ( !strcasecmp( p, "recipient" ) ) {
584 as->as_kind = AS_KIND_RECIPIENT;
585 } else if ( !strcasecmp( p, "errors" ) ) {
586 as->as_kind = AS_KIND_ERRORS;
587 } else if ( !strcasecmp( p, "request" ) ) {
588 as->as_kind = AS_KIND_REQUEST;
589 } else if ( !strcasecmp( p, "owner" ) ) {
590 as->as_kind = AS_KIND_OWNER;
593 "Unknown semantics word %s", p );
598 if ( attr_semantics == NULL ) {
599 attr_semantics = calloc(2, sizeof(AttrSemantics *));
600 if ( !attr_semantics ) {
601 syslog( LOG_ALERT, "Out of memory" );
604 attr_semantics[0] = as;
607 AttrSemantics **list_temp;
608 for ( size = 0; attr_semantics[size]; size++ )
611 list_temp = realloc( attr_semantics,
612 size*sizeof(AttrSemantics *) );
614 syslog( LOG_ALERT, "Out of memory" );
617 attr_semantics = list_temp;
618 attr_semantics[size-2] = as;
619 attr_semantics[size-1] = NULL;
624 load_config( char *filespec )
633 cf = fopen( filespec, "r" );
635 perror( "Opening config file" );
639 while ( ( line = get_config_line( cf,&lineno ) ) ) {
640 fprintf( stderr, "Read line %d:%s\n", lineno, line );
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 );
664 "Unparseable config definition at line %d",
673 connect_to_x500( void )
677 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
678 syslog( LOG_ALERT, "ldap_init failed" );
682 /* TBC: Set this only when it makes sense
683 opt = MAIL500_MAXAMBIGUOUS;
684 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
686 opt = LDAP_DEREF_ALWAYS;
687 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
689 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
690 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
698 new_group( char *dn, Group **list, int *nlist )
703 for ( i = 0; i < *nlist; i++ ) {
704 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
705 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
711 *list = (Group *) malloc( sizeof(Group) );
713 *list = (Group *) realloc( *list, (*nlist + 1) *
719 (*list)[*nlist].g_errorsto = NULL;
720 (*list)[*nlist].g_members = NULL;
721 (*list)[*nlist].g_nmembers = 0;
722 /* save the group's dn so we can check for loops above */
723 (*list)[*nlist].g_dn = strdup( dn );
727 return( this_group );
739 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
740 *localpart = strdup( address );
743 *localpart = malloc( p - address + 1 );
744 strncpy( *localpart, address, p - address );
745 (*localpart)[p - address] = '\0';
747 *domainpart = strdup( p );
766 LDAPMessage *res, *e;
767 struct timeval timeout;
769 timeout.tv_sec = MAIL500_TIMEOUT;
771 for ( i = 0; dnlist[i]; i++ ) {
772 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
773 "(objectclass=*)", def_attr, 0,
774 &timeout, &res )) != LDAP_SUCCESS ) {
775 if ( rc == LDAP_NO_SUCH_OBJECT ) {
776 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
779 syslog( LOG_ALERT, "member search return 0x%x", rc );
781 unbind_and_exit( EX_TEMPFAIL );
784 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
785 syslog( LOG_ALERT, "member search error parsing entry" );
786 unbind_and_exit( EX_TEMPFAIL );
788 if ( entry_engine( e, dnlist[i], address, to, nto,
789 togroups, ngroups, err, nerr,
790 USER | GROUP_MEMBERS ) ) {
818 char realfilter[1024];
819 LDAPMessage *e, *res;
822 struct timeval timeout;
827 timeout.tv_sec = MAIL500_TIMEOUT;
830 rc = ldap_url_parse( url, &ludp );
833 case LDAP_URL_ERR_NOTLDAP:
835 "Not an LDAP URL: %s", url );
837 case LDAP_URL_ERR_BADENCLOSURE:
839 "Bad Enclosure in URL: %s", url );
841 case LDAP_URL_ERR_BADURL:
843 "Bad URL: %s", url );
845 case LDAP_URL_ERR_BADHOST:
847 "Host is invalid in URL: %s", url );
849 case LDAP_URL_ERR_BADATTRS:
851 "Attributes are invalid in URL: %s", url );
853 case LDAP_URL_ERR_BADSCOPE:
855 "Scope is invalid in URL: %s", url );
857 case LDAP_URL_ERR_BADFILTER:
859 "Filter is invalid in URL: %s", url );
861 case LDAP_URL_ERR_BADEXTS:
863 "Extensions are invalid in URL: %s", url );
865 case LDAP_URL_ERR_MEM:
867 "Out of memory parsing URL: %s", url );
869 case LDAP_URL_ERR_PARAM:
871 "bad parameter parsing URL: %s", url );
875 "Unknown error %d parsing URL: %s",
879 add_error( err, nerr, E_BADMEMBER,
885 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
892 for ( i = 0; substs[i].sub_char != '\0';
894 if ( *s == substs[i].sub_char ) {
895 for ( p = substs[i].sub_value;
903 if ( substs[i].sub_char == '\0' ) {
905 "unknown format %c", *s );
913 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
914 filter[ sizeof( filter ) - 1 ] = '\0';
917 for ( s = filter, d = realfilter; *s; s++, d++ ) {
925 if ( ludp->lud_attrs ) {
926 attrlist = ludp->lud_attrs;
931 /* TBC: we don't read the host, dammit */
932 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
933 realfilter, attrlist, 0,
936 /* some other trouble - try again later */
937 if ( rc != LDAP_SUCCESS &&
938 rc != LDAP_SIZELIMIT_EXCEEDED ) {
939 syslog( LOG_ALERT, "return 0x%x from X.500",
941 unbind_and_exit( EX_TEMPFAIL );
944 match = ldap_count_entries( ld, res );
946 /* trouble - try again later */
948 syslog( LOG_ALERT, "error parsing result from X.500" );
949 unbind_and_exit( EX_TEMPFAIL );
952 if ( match == 1 || multi_entry ) {
953 for ( e = ldap_first_entry( ld, res ); e != NULL;
954 e = ldap_next_entry( ld, e ) ) {
955 dn = ldap_get_dn( ld, e );
956 resolved = entry_engine( e, dn, address, to, nto,
960 add_error( err, nerr, E_NOEMAIL, address, res );
966 /* more than one match - bounce with ambiguous user? */
968 LDAPMessage *next, *tmpres = NULL;
972 /* not giving rdn preference - bounce with ambiguous user */
973 if ( rdnpref == 0 ) {
974 add_error( err, nerr, E_AMBIGUOUS, address, res );
979 * giving rdn preference - see if any entries were matched
980 * because of their rdn. If so, collect them to deal with
981 * later (== 1 we deliver, > 1 we bounce).
984 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
985 next = ldap_next_entry( ld, e );
986 dn = ldap_get_dn( ld, e );
987 xdn = ldap_explode_dn( dn, 1 );
989 /* XXX bad, but how else can we do it? XXX */
990 if ( strcasecmp( xdn[0], address ) == 0 ) {
991 ldap_delete_result_entry( &res, e );
992 ldap_add_result_entry( &tmpres, e );
995 ldap_value_free( xdn );
999 /* nothing matched by rdn - go ahead and bounce */
1000 if ( tmpres == NULL ) {
1001 add_error( err, nerr, E_AMBIGUOUS, address, res );
1004 /* more than one matched by rdn - bounce with rdn matches */
1005 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1006 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1010 } else if ( match < 0 ) {
1011 syslog( LOG_ALERT, "error parsing result from X.500" );
1012 unbind_and_exit( EX_TEMPFAIL );
1015 /* otherwise one matched by rdn - send to it */
1016 ldap_msgfree( res );
1020 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1021 syslog( LOG_ALERT, "error parsing entry from X.500" );
1022 unbind_and_exit( EX_TEMPFAIL );
1025 dn = ldap_get_dn( ld, e );
1027 resolved = entry_engine( e, dn, address, to, nto,
1031 add_error( err, nerr, E_NOEMAIL, address, res );
1032 /* Don't free res if we passed it to add_error */
1034 ldap_msgfree( res );
1057 for ( i = 0; urllist[i]; i++ ) {
1059 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1062 vals[0] = urllist[i] + 5;
1064 add_to( to, nto, vals );
1067 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1069 resolved = search_ldap_url( urllist[i], NULL,
1070 address, 0, multi_entry,
1071 to, nto, togroups, ngroups,
1074 /* Produce some sensible error here */
1082 * The entry engine processes an entry. Normally, each entry will resolve
1083 * to one or more values that will be added to the 'to' argument. This
1084 * argument needs not be the global 'to' list, it may be the g_to field
1085 * in a group. Groups have no special treatment, unless they require
1106 char ***current_to = to;
1107 int *current_nto = nto;
1108 Group *current_group = NULL;
1110 char *localpart, *domainpart;
1113 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1114 AttrSemantics *as = attr_semantics[i];
1117 vals = ldap_get_values( ld, e, as->as_name );
1118 if ( !vals || vals[0] == NULL ) {
1121 nent = count_values( vals );
1122 if ( nent > 1 && !as->as_m_valued ) {
1123 add_error( err, nerr, E_AMBIGUOUS, address, e );
1126 switch ( as->as_kind ) {
1127 case AS_KIND_RECIPIENT:
1128 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1130 switch ( as->as_syntax ) {
1131 case AS_SYNTAX_RFC822:
1132 add_to( current_to, current_nto, vals );
1135 case AS_SYNTAX_RFC822_EXT:
1136 add_to( current_to, current_nto, vals );
1139 case AS_SYNTAX_NATIVE_MB:
1140 /* We used to concatenate mailHost if set here */
1142 * We used to send a copy to the vacation host
1143 * if onVacation to uid@vacationhost
1145 add_to( current_to, current_nto, vals );
1150 if ( dn_search( vals, address,
1151 current_to, current_nto,
1159 if ( url_list_search( vals, address,
1161 current_to, current_nto,
1163 err, nerr, type ) ) {
1168 case AS_SYNTAX_BOOL_FILTER:
1169 if ( strcasecmp( vals[0], "true" ) ) {
1172 substs[0].sub_char = 'D';
1173 substs[0].sub_value = dn;
1174 substs[1].sub_char = '\0';
1175 substs[1].sub_value = NULL;
1176 if ( url_list_search( vals, address,
1178 current_to, current_nto,
1180 err, nerr, type ) ) {
1187 "Invalid syntax %d for kind %d",
1188 as->as_syntax, as->as_kind );
1193 case AS_KIND_ERRORS:
1194 /* This is a group with special processing */
1195 if ( type & GROUP_ERRORS ) {
1196 switch (as->as_kind) {
1197 case AS_SYNTAX_RFC822:
1198 add_to( current_to, current_nto, vals );
1204 "Invalid syntax %d for kind %d",
1205 as->as_syntax, as->as_kind );
1208 current_group = new_group( dn, togroups,
1210 current_to = ¤t_group->g_members;
1211 current_nto = ¤t_group->g_nmembers;
1212 split_address( address,
1213 &localpart, &domainpart );
1215 sprintf( buf, "%s-%s@%s",
1221 sprintf( buf, "%s-%s@%s",
1226 current_group->g_errorsto = strdup( buf );
1230 case AS_KIND_REQUEST:
1231 /* This is a group with special processing */
1232 if ( type & GROUP_REQUEST ) {
1233 add_to( current_to, current_nto, vals );
1239 /* This is a group with special processing */
1240 if ( type & GROUP_REQUEST ) {
1241 add_to( current_to, current_nto, vals );
1248 "Invalid kind %d", as->as_kind );
1251 ldap_value_free( vals );
1252 if ( as->as_final ) {
1273 int b, resolved = 0;
1275 for ( b = 0; base[b] != NULL; b++ ) {
1277 if ( ! (base[b]->b_search & type) ) {
1281 resolved = search_ldap_url( base[b]->b_url, substs, name,
1283 base[b]->b_m_entries,
1284 to, nto, togroups, ngroups,
1304 struct timeval timeout;
1305 char *localpart, *domainpart;
1310 * Look up the name in X.500, add the appropriate addresses found
1311 * to the to list, or to the err list in case of error. Groups are
1312 * handled by the do_group routine, individuals are handled here.
1313 * When looking up name, we follow the bases hierarchy, looking
1314 * in base[0] first, then base[1], etc. For each base, there is
1315 * a set of search filters to try, in order. If something goes
1316 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1317 * If the b_rdnpref flag is set, then we give preference to entries
1318 * that matched name because it's their rdn, otherwise not.
1321 split_address( name, &localpart, &domainpart );
1322 timeout.tv_sec = MAIL500_TIMEOUT;
1323 timeout.tv_usec = 0;
1324 substs[0].sub_char = 'm';
1325 substs[0].sub_value = name;
1326 substs[1].sub_char = 'h';
1327 substs[1].sub_value = host;
1328 substs[2].sub_char = 'l';
1329 substs[2].sub_value = localpart;
1330 substs[3].sub_char = 'd';
1331 substs[3].sub_value = domainpart;
1332 substs[4].sub_char = '\0';
1333 substs[4].sub_value = NULL;
1335 resolved = search_bases( NULL, substs, name,
1336 to, nto, togroups, ngroups,
1340 /* not resolved - bounce with user unknown */
1341 if ( type == USER ) {
1342 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1344 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1350 send_message( char **to )
1353 #ifndef HAVE_WAITPID
1354 WAITSTATUSTYPE status;
1361 strcpy( buf, to[0] );
1362 for ( i = 1; to[i] != NULL; i++ ) {
1364 strcat( buf, to[i] );
1367 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1371 if ( (pid = fork()) != 0 ) {
1373 waitpid( pid, (int *) NULL, 0 );
1375 wait4( pid, &status, WAIT_FLAGS, 0 );
1379 /* to includes sendmailargs */
1380 execv( MAIL500_SENDMAIL, to );
1382 syslog( LOG_ALERT, "execv failed" );
1384 exit( EX_TEMPFAIL );
1389 send_group( Group *group, int ngroup )
1395 #ifndef HAVE_WAITPID
1396 WAITSTATUSTYPE status;
1399 for ( i = 0; i < ngroup; i++ ) {
1400 (void) rewind( stdin );
1402 iargv[0] = MAIL500_SENDMAIL;
1404 iargv[2] = group[i].g_errorsto;
1405 iargv[3] = "-oMrX.500";
1412 add_to( &argv, &argc, iargv );
1413 add_to( &argv, &argc, group[i].g_members );
1419 strcpy( buf, argv[0] );
1420 for ( i = 1; i < argc; i++ ) {
1422 strcat( buf, argv[i] );
1425 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1429 if ( (pid = fork()) != 0 ) {
1431 waitpid( pid, (int *) NULL, 0 );
1433 wait4( pid, &status, WAIT_FLAGS, 0 );
1437 execv( MAIL500_SENDMAIL, argv );
1439 syslog( LOG_ALERT, "execv failed" );
1441 exit( EX_TEMPFAIL );
1447 send_errors( Error *err, int nerr )
1449 int pid, i, namelen;
1454 #ifndef HAVE_WAITPID
1455 WAITSTATUSTYPE status;
1458 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1459 mailfrom = errorsfrom;
1462 argv[0] = MAIL500_SENDMAIL;
1463 argv[1] = "-oMrX.500";
1467 argv[5] = MAIL500_BOUNCEFROM;
1474 strcpy( buf, argv[0] );
1475 for ( i = 1; argv[i] != NULL; i++ ) {
1477 strcat( buf, argv[i] );
1480 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1483 if ( pipe( fd ) == -1 ) {
1484 syslog( LOG_ALERT, "cannot create pipe" );
1485 exit( EX_TEMPFAIL );
1488 if ( (pid = fork()) != 0 ) {
1489 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1490 syslog( LOG_ALERT, "cannot fdopen pipe" );
1491 exit( EX_TEMPFAIL );
1494 fprintf( fp, "To: %s\n", mailfrom );
1495 fprintf( fp, "From: %s\n", errorsfrom );
1496 fprintf( fp, "Subject: undeliverable mail\n" );
1497 fprintf( fp, "\n" );
1498 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1499 for ( i = 0; i < nerr; i++ ) {
1500 namelen = strlen( err[i].e_addr );
1501 fprintf( fp, "\n" );
1503 switch ( err[i].e_code ) {
1505 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1508 case E_GROUPUNKNOWN:
1509 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1513 fprintf( fp, "%s: Group member does not exist\n",
1515 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1516 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1517 fprintf( fp, "then re-adding the person to the group.\n" );
1521 fprintf( fp, "%s: Group exists but has no request address\n",
1526 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1531 fprintf( fp, "%s: Group exists but has no owner\n",
1536 do_ambiguous( fp, &err[i], namelen );
1540 do_noemail( fp, &err[i], namelen );
1543 case E_MEMBERNOEMAIL:
1544 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1548 case E_JOINMEMBERNOEMAIL:
1549 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1554 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1555 err[i].e_addr, err[i].e_loop );
1559 fprintf( fp, "%s: Group has no members\n",
1564 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1565 unbind_and_exit( EX_TEMPFAIL );
1570 fprintf( fp, "\n------- The original message sent:\n\n" );
1572 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1578 waitpid( pid, (int *) NULL, 0 );
1580 wait4( pid, &status, WAIT_FLAGS, 0 );
1585 execv( MAIL500_SENDMAIL, argv );
1587 syslog( LOG_ALERT, "execv failed" );
1589 exit( EX_TEMPFAIL );
1594 do_noemail( FILE *fp, Error *err, int namelen )
1600 fprintf(fp, "%s: User has no email address registered.\n",
1602 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1603 namelen, " ", err->e_addr );
1606 dn = ldap_get_dn( ld, err->e_msg );
1607 ufn = ldap_explode_dn( dn, 1 );
1608 rdn = strdup( ufn[0] );
1609 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1610 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1612 for ( i = 0; vals[i]; i++ ) {
1613 last = strlen( vals[i] ) - 1;
1614 if ( isdigit((unsigned char) vals[i][last]) ) {
1615 rdn = strdup( vals[i] );
1620 ldap_value_free( vals );
1623 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1626 ldap_value_free( ufn );
1628 /* titles or descriptions */
1629 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1630 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1632 fprintf( fp, "%*s No title or description registered\n",
1635 for ( i = 0; vals[i] != NULL; i++ ) {
1636 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1639 ldap_value_free( vals );
1642 /* postal address */
1643 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1645 fprintf( fp, "%*s No postal address registered\n", namelen,
1648 fprintf( fp, "%*s ", namelen, " " );
1649 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1650 if ( vals[0][i] == '$' ) {
1651 fprintf( fp, "\n%*s ", namelen, " " );
1652 while ( isspace((unsigned char) vals[0][i+1]) )
1655 fprintf( fp, "%c", vals[0][i] );
1658 fprintf( fp, "\n" );
1660 ldap_value_free( vals );
1663 /* telephone number */
1664 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1666 fprintf( fp, "%*s No phone number registered\n", namelen,
1669 for ( i = 0; vals[i] != NULL; i++ ) {
1670 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1673 ldap_value_free( vals );
1679 do_ambiguous( FILE *fp, Error *err, int namelen )
1686 i = ldap_result2error( ld, err->e_msg, 0 );
1688 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1689 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1690 ldap_count_entries( ld, err->e_msg ) );
1692 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1693 e = ldap_next_entry( ld, e ) ) {
1694 dn = ldap_get_dn( ld, e );
1695 ufn = ldap_explode_dn( dn, 1 );
1696 rdn = strdup( ufn[0] );
1697 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1698 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1699 for ( i = 0; vals[i]; i++ ) {
1700 last = strlen( vals[i] ) - 1;
1701 if (isdigit((unsigned char) vals[i][last])) {
1702 rdn = strdup( vals[i] );
1707 ldap_value_free( vals );
1712 if ( isgroup( e ) ) {
1713 vals = ldap_get_values( ld, e, "description" );
1715 vals = ldap_get_values( ld, e, "title" );
1718 vals = ldap_get_values( ld, e, "description" );
1720 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1721 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1722 fprintf( fp, " %s\n", vals[i] );
1727 ldap_value_free( ufn );
1729 ldap_value_free( vals );
1734 count_values( char **list )
1738 for ( i = 0; list && list[i] != NULL; i++ )
1745 add_to( char ***list, int *nlist, char **new )
1747 int i, nnew, oldnlist;
1749 nnew = count_values( new );
1752 if ( *list == NULL || *nlist == 0 ) {
1753 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1756 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1757 nnew * sizeof(char *) + sizeof(char *) );
1761 for ( i = 0; i < nnew; i++ )
1762 (*list)[i + oldnlist] = strdup( new[i] );
1763 (*list)[*nlist] = NULL;
1767 add_single_to( char ***list, char *new )
1771 if ( *list == NULL ) {
1773 *list = (char **) malloc( 2 * sizeof(char *) );
1775 nlist = count_values( *list );
1776 *list = (char **) realloc( *list,
1777 ( nlist + 2 ) * sizeof(char *) );
1780 (*list)[nlist] = strdup( new );
1781 (*list)[nlist+1] = NULL;
1785 isgroup( LDAPMessage *e )
1790 if ( !groupclasses ) {
1794 oclist = ldap_get_values( ld, e, "objectClass" );
1796 for ( i = 0; oclist[i] != NULL; i++ ) {
1797 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1798 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1799 ldap_value_free( oclist );
1804 ldap_value_free( oclist );
1810 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1813 *err = (Error *) malloc( sizeof(Error) );
1815 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1818 (*err)[*nerr].e_code = code;
1819 (*err)[*nerr].e_addr = strdup( addr );
1820 (*err)[*nerr].e_msg = msg;
1825 unbind_and_exit( int rc )
1829 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1830 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );