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>
44 #include "ldap_defaults.h"
46 #ifndef MAIL500_BOUNCEFROM
47 #define MAIL500_BOUNCEFROM "<>"
51 #define GROUP_ERRORS 0x02
52 #define GROUP_REQUEST 0x04
53 #define GROUP_MEMBERS 0x08
54 #define GROUP_OWNER 0x10
57 #define ERRORS "errors"
58 #define REQUEST "request"
59 #define REQUESTS "requests"
60 #define MEMBERS "members"
62 #define OWNERS "owners"
65 char *vacationhost = NULL;
66 char *errorsfrom = MAIL500_BOUNCEFROM;
67 char *mailfrom = NULL;
69 char *ldaphost = NULL;
75 #define E_USERUNKNOWN 1
81 #define E_JOINMEMBERNOEMAIL 7
82 #define E_MEMBERNOEMAIL 8
84 #define E_NOMEMBERS 10
86 #define E_GROUPUNKNOWN 12
92 #define e_msg e_union.e_u_msg
93 #define e_loop e_union.e_u_loop
96 typedef struct groupto {
103 typedef struct baseinfo {
106 char b_rdnpref; /* give rdn's preference when searching? */
107 int b_search; /* ORed with the type of thing the address */
108 /* looks like (USER, GROUP_ERRORS, etc.) */
109 /* to see if this should be searched */
113 * We should limit the search to objectclass=mailRecipient or
114 * objectclass=mailGroup.
119 {"dc=StlInter, dc=Net",
121 {"mail=%s", "mailAlternateAddress=%s", NULL}},
128 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrLDAP", "-odi", "-oi", "-f", NULL, NULL };
130 typedef struct attr_semantics {
132 int as_m_valued; /* Is multivalued? */
133 int as_final; /* If true, no further expansion is tried. */
134 int as_syntax; /* How to interpret values */
135 int as_m_entries; /* Can resolve to several entries? */
136 int as_kind; /* Recipient, sender, etc. */
137 char *as_param; /* Extra info for filters and things alike */
140 #define AS_SYNTAX_UNKNOWN 0 /* Unqualified mailbox name */
141 #define AS_SYNTAX_NATIVE_MB 1 /* Unqualified mailbox name */
142 #define AS_SYNTAX_RFC822 2 /* RFC822 mail address */
143 #define AS_SYNTAX_HOST 3
144 #define AS_SYNTAX_DN 4 /* A directory entry */
145 #define AS_SYNTAX_RFC822_EXT 5
146 #define AS_SYNTAX_URL 6 /* mailto: or ldap: URL */
147 #define AS_SYNTAX_BOOL_FILTER 7 /* For joinable, filter in as_param */
149 #define AS_KIND_UNKNOWN 0
150 #define AS_KIND_RECIPIENT 1
151 #define AS_KIND_ERRORS 2 /* For ErrorsTo and similar */
152 #define AS_KIND_REQUEST 3
153 #define AS_KIND_OWNER 4
154 #define AS_KIND_FORWARD_TO_HOST 5 /* Expand at some other host */
155 #define AS_KIND_ALLOWED_SENDER 6 /* Can send to group */
156 #define AS_KIND_MODERATOR 7
158 AttrSemantics **attr_semantics = NULL;
160 typedef struct subst {
165 char **groupclasses = NULL;
166 char **def_attr = NULL;
168 static void load_config( char *filespec );
169 static void split_address( char *address, char **localpart, char **domainpart);
170 static int entry_engine( LDAPMessage *e, char *dn, char *address, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
171 static void do_address( char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type );
172 static void send_message( char **to );
173 static void send_errors( Error *err, int nerr );
174 static void do_noemail( FILE *fp, Error *err, int namelen );
175 static void do_ambiguous( FILE *fp, Error *err, int namelen );
176 static int count_values( char **list );
177 static void add_to( char ***list, int *nlist, char **new );
178 static void add_single_to( char ***list, char *new );
179 static int isgroup( LDAPMessage *e );
180 static void add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg );
181 static void unbind_and_exit( int rc );
182 static void send_group( Group *group, int ngroup );
184 static int connect_to_x500( void );
188 main ( int argc, char **argv )
194 int numto, ngroups, numerr, nargs;
196 char *conffile = NULL;
198 if ( (myname = strrchr( argv[0], '/' )) == NULL )
199 myname = strdup( argv[0] );
201 myname = strdup( myname + 1 );
204 (void) SIGNAL( SIGPIPE, SIG_IGN );
208 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
210 openlog( myname, OPENLOG_OPTIONS );
213 while ( (i = getopt( argc, argv, "d:C:f:h:l:m:v:" )) != EOF ) {
215 case 'd': /* turn on debugging */
216 debug = atoi( optarg );
219 case 'C': /* path to configuration file */
220 conffile = strdup( optarg );
223 case 'f': /* who it's from & where errors should go */
224 mailfrom = strdup( optarg );
225 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
226 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
227 sendmailargs[j+1] = mailfrom;
233 case 'h': /* hostname */
234 host = strdup( optarg );
235 hostlen = strlen(host);
238 case 'l': /* ldap host */
239 ldaphost = strdup( optarg );
242 /* mailer-daemon address - who we should */
243 case 'm': /* say errors come from */
244 errorsfrom = strdup( optarg );
247 case 'v': /* vacation host */
248 vacationhost = strdup( optarg );
252 syslog( LOG_ALERT, "unknown option" );
257 if ( mailfrom == NULL ) {
258 syslog( LOG_ALERT, "required argument -f not present" );
261 if ( errorsfrom == NULL ) {
262 syslog( LOG_ALERT, "required argument -m not present" );
265 /* if ( host == NULL ) { */
266 /* syslog( LOG_ALERT, "required argument -h not present" ); */
267 /* exit( EX_TEMPFAIL ); */
269 if ( conffile == NULL ) {
270 syslog( LOG_ALERT, "required argument -C not present" );
274 load_config( conffile );
276 if ( connect_to_x500() != 0 )
285 syslog( LOG_ALERT, "running as %d", geteuid() );
286 strcpy( buf, argv[0] );
287 for ( i = 1; i < argc; i++ ) {
289 strcat( buf, argv[i] );
292 syslog( LOG_ALERT, "args: (%s)", buf );
297 add_to( &tolist, &numto, sendmailargs );
299 ngroups = numerr = 0;
302 for ( i = optind; i < argc; i++ ) {
305 char *localpart, *domainpart;
308 /* TBC: Make this processing optional */
309 /* for ( j = 0; argv[i][j] != '\0'; j++ ) { */
310 /* if ( argv[i][j] == '.' || argv[i][j] == '_' ) */
311 /* argv[i][j] = ' '; */
315 split_address( argv[i], &localpart, &domainpart );
316 if ( (s = strrchr( localpart, '-' )) != NULL ) {
319 if ((strcasecmp(s, ERROR) == 0) ||
320 (strcasecmp(s, ERRORS) == 0)) {
323 } else if ((strcasecmp(s, REQUEST) == 0) ||
324 (strcasecmp(s, REQUESTS) == 0)) {
325 type = GROUP_REQUEST;
327 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
328 type = GROUP_MEMBERS;
330 } else if ((strcasecmp(s, OWNER) == 0) ||
331 (strcasecmp(s, OWNERS) == 0)) {
338 sprintf( address, "%s@%s", localpart, domainpart );
342 sprintf( address, "%s@%s", localpart, domainpart );
345 do_address( address, &tolist, &numto, &togroups, &ngroups,
346 &errlist, &numerr, type );
350 * If we have both errors and successful deliveries to make or if
351 * if there are any groups to deliver to, we basically need to read
352 * the message twice. So, we have to put it in a tmp file.
355 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
360 if ( (fp = tmpfile()) == NULL ) {
361 syslog( LOG_ALERT, "could not open tmp file" );
362 unbind_and_exit( EX_TEMPFAIL );
365 /* copy the message to a temp file */
366 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
367 if ( fputs( buf, fp ) == EOF ) {
368 syslog( LOG_ALERT, "error writing tmpfile" );
369 unbind_and_exit( EX_TEMPFAIL );
373 if ( dup2( fileno( fp ), 0 ) == -1 ) {
374 syslog( LOG_ALERT, "could not dup2 tmpfile" );
375 unbind_and_exit( EX_TEMPFAIL );
381 /* deal with errors */
384 syslog( LOG_ALERT, "sending errors" );
386 (void) rewind( stdin );
387 send_errors( errlist, numerr );
390 (void) ldap_unbind( ld );
392 /* send to groups with errorsTo */
395 syslog( LOG_ALERT, "sending to groups with errorsto" );
397 (void) rewind( stdin );
398 send_group( togroups, ngroups );
401 /* send to expanded aliases and groups w/o errorsTo */
402 if ( numto > nargs ) {
404 syslog( LOG_ALERT, "sending to aliases and groups" );
406 (void) rewind( stdin );
407 send_message( tolist );
414 get_config_line( FILE *cf, int *lineno)
416 static char buf[2048];
422 room = sizeof( buf );
423 while ( fgets( &buf[pos], room, cf ) ) {
426 /* Delete whitespace at the beginning of new data */
427 if ( isspace( buf[pos] ) ) {
429 for ( s = buf+pos; isspace(*s); s++ )
431 for ( d = buf+pos; *s; s++, d++ ) {
438 if ( buf[len-1] != '\n' ) {
439 syslog( LOG_ALERT, "Definition too long at line %d",
445 if ( strspn( buf, " \t\n" ) == len )
447 if ( buf[len-2] == '\\' ) {
449 room = sizeof(buf) - pos;
452 /* We have a real line, we will exit the loop */
460 add_url ( char *url, int rdnpref, int typemask )
466 b = calloc(1, sizeof(Base));
468 syslog( LOG_ALERT, "Out of memory" );
471 b->b_url = strdup( url );
472 b->b_rdnpref = rdnpref;
473 b->b_search = typemask;
475 if ( base == NULL ) {
476 base = calloc(2, sizeof(LDAPURLDesc *));
478 syslog( LOG_ALERT, "Out of memory" );
483 for ( size = 0; base[size]; size++ )
486 list_temp = realloc( base, size*sizeof(LDAPURLDesc *) );
488 syslog( LOG_ALERT, "Out of memory" );
498 add_def_attr( char *s )
504 p += strspn( p, "\t," );
505 q = strpbrk( p, " \t," );
508 add_single_to( &def_attr, p );
510 add_single_to( &def_attr, p );
518 add_attr_semantics( char *s )
523 as = calloc( 1, sizeof( AttrSemantics ) );
525 while ( isspace ( *p ) )
528 while ( !isspace ( *q ) && *q != '\0' )
531 as->as_name = strdup( p );
535 while ( isspace ( *p ) )
538 while ( !isspace ( *q ) && *q != '\0' )
541 if ( !strcasecmp( p, "multivalued" ) ) {
543 } else if ( !strcasecmp( p, "multiple-entries" ) ) {
544 as->as_m_entries = 1;
545 } else if ( !strcasecmp( p, "final" ) ) {
547 } else if ( !strcasecmp( p, "local-native-mailbox" ) ) {
548 as->as_syntax = AS_SYNTAX_NATIVE_MB;
549 } else if ( !strcasecmp( p, "rfc822" ) ) {
550 as->as_syntax = AS_SYNTAX_RFC822;
551 } else if ( !strcasecmp( p, "rfc822-extended" ) ) {
552 as->as_syntax = AS_SYNTAX_RFC822_EXT;
553 } else if ( !strcasecmp( p, "dn" ) ) {
554 as->as_syntax = AS_SYNTAX_DN;
555 } else if ( !strcasecmp( p, "url" ) ) {
556 as->as_syntax = AS_SYNTAX_URL;
557 } else if ( !strncasecmp( p, "search-with-filter=", 19 ) ) {
558 as->as_syntax = AS_SYNTAX_BOOL_FILTER;
559 q = strchr( p, '=' );
562 while ( *q && !isspace( *q ) ) {
567 as->as_param = strdup( p );
570 as->as_param = strdup( p );
575 "Missing filter in %s", s );
578 } else if ( !strcasecmp( p, "host" ) ) {
579 as->as_kind = AS_SYNTAX_HOST;
580 } else if ( !strcasecmp( p, "forward-to-host" ) ) {
581 as->as_kind = AS_KIND_FORWARD_TO_HOST;
582 } else if ( !strcasecmp( p, "recipient" ) ) {
583 as->as_kind = AS_KIND_RECIPIENT;
584 } else if ( !strcasecmp( p, "errors" ) ) {
585 as->as_kind = AS_KIND_ERRORS;
586 } else if ( !strcasecmp( p, "request" ) ) {
587 as->as_kind = AS_KIND_REQUEST;
588 } else if ( !strcasecmp( p, "owner" ) ) {
589 as->as_kind = AS_KIND_OWNER;
592 "Unknown semantics word %s", p );
597 if ( attr_semantics == NULL ) {
598 attr_semantics = calloc(2, sizeof(AttrSemantics *));
599 if ( !attr_semantics ) {
600 syslog( LOG_ALERT, "Out of memory" );
603 attr_semantics[0] = as;
606 AttrSemantics **list_temp;
607 for ( size = 0; attr_semantics[size]; size++ )
610 list_temp = realloc( attr_semantics,
611 size*sizeof(AttrSemantics *) );
613 syslog( LOG_ALERT, "Out of memory" );
616 attr_semantics = list_temp;
617 attr_semantics[size-2] = as;
618 attr_semantics[size-1] = NULL;
623 load_config( char *filespec )
632 cf = fopen( filespec, "r" );
634 perror( "Opening config file" );
638 while ( ( line = get_config_line( cf,&lineno ) ) ) {
639 p = strpbrk( line, " \t" );
642 "Missing space at line %d", lineno );
645 if ( !strncmp( line, "search", p-line ) ) {
646 p += strspn( p, " \t" );
650 add_url( p, rdnpref, typemask );
651 } else if ( !strncmp(line, "attribute", p-line) ) {
652 p += strspn(p, " \t");
653 add_attr_semantics( p );
654 } else if ( !strncmp(line, "default-attributes", p-line) ) {
655 p += strspn(p, " \t");
657 } else if ( !strncmp(line, "group-classes", p-line) ) {
658 p += strspn(p, " \t");
659 add_single_to( &groupclasses, p );
662 "Unparseable config definition at line %d",
671 connect_to_x500( void )
675 if ( (ld = ldap_init( ldaphost, 0 )) == NULL ) {
676 syslog( LOG_ALERT, "ldap_init failed" );
680 /* TBC: Set this only when it makes sense
681 opt = MAIL500_MAXAMBIGUOUS;
682 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &opt);
684 opt = LDAP_DEREF_ALWAYS;
685 ldap_set_option(ld, LDAP_OPT_DEREF, &opt);
687 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
688 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
696 new_group( char *dn, Group **list, int *nlist )
701 for ( i = 0; i < *nlist; i++ ) {
702 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
703 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
709 *list = (Group *) malloc( sizeof(Group) );
711 *list = (Group *) realloc( *list, (*nlist + 1) *
717 (*list)[*nlist].g_errorsto = NULL;
718 (*list)[*nlist].g_members = NULL;
719 (*list)[*nlist].g_nmembers = 0;
720 /* save the group's dn so we can check for loops above */
721 (*list)[*nlist].g_dn = strdup( dn );
725 return( this_group );
737 if ( ( p = strrchr( address, '@' ) ) == NULL ) {
738 *localpart = strdup( address );
741 *localpart = malloc( p - address + 1 );
742 strncpy( *localpart, address, p - address );
743 (*localpart)[p - address] = '\0';
745 *domainpart = strdup( p );
764 LDAPMessage *res, *e;
765 struct timeval timeout;
767 timeout.tv_sec = MAIL500_TIMEOUT;
769 for ( i = 0; dnlist[i]; i++ ) {
770 if ( (rc = ldap_search_st( ld, dnlist[i], LDAP_SCOPE_BASE,
771 "(objectclass=*)", def_attr, 0,
772 &timeout, &res )) != LDAP_SUCCESS ) {
773 if ( rc == LDAP_NO_SUCH_OBJECT ) {
774 add_error( err, nerr, E_BADMEMBER, dnlist[i], NULL );
777 syslog( LOG_ALERT, "member search return 0x%x", rc );
779 unbind_and_exit( EX_TEMPFAIL );
782 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
783 syslog( LOG_ALERT, "member search error parsing entry" );
784 unbind_and_exit( EX_TEMPFAIL );
786 if ( entry_engine( e, dnlist[i], address, to, nto,
787 togroups, ngroups, err, nerr,
788 USER | GROUP_MEMBERS ) ) {
816 char realfilter[1024];
817 LDAPMessage *e, *res;
820 struct timeval timeout;
825 timeout.tv_sec = MAIL500_TIMEOUT;
828 rc = ldap_url_parse( url, &ludp );
831 case LDAP_URL_ERR_NOTLDAP:
833 "Not an LDAP URL: %s", url );
835 case LDAP_URL_ERR_BADENCLOSURE:
837 "Bad Enclosure in URL: %s", url );
839 case LDAP_URL_ERR_BADURL:
841 "Bad URL: %s", url );
843 case LDAP_URL_ERR_BADHOST:
845 "Host is invalid in URL: %s", url );
847 case LDAP_URL_ERR_BADATTRS:
849 "Attributes are invalid in URL: %s", url );
851 case LDAP_URL_ERR_BADSCOPE:
853 "Scope is invalid in URL: %s", url );
855 case LDAP_URL_ERR_BADFILTER:
857 "Filter is invalid in URL: %s", url );
859 case LDAP_URL_ERR_BADEXTS:
861 "Extensions are invalid in URL: %s", url );
863 case LDAP_URL_ERR_MEM:
865 "Out of memory parsing URL: %s", url );
867 case LDAP_URL_ERR_PARAM:
869 "bad parameter parsing URL: %s", url );
873 "Unknown error %d parsing URL: %s",
877 add_error( err, nerr, E_BADMEMBER,
883 for ( s = ludp->lud_filter, d = filter; *s; s++,d++ ) {
890 for ( i = 0; substs[i].sub_char != '\0';
892 if ( *s == substs[i].sub_char ) {
893 for ( p = substs[i].sub_value;
901 if ( substs[i].sub_char == '\0' ) {
903 "unknown format %c", *s );
911 strncpy( filter, ludp->lud_filter, sizeof( filter ) - 1 );
912 filter[ sizeof( filter ) - 1 ] = '\0';
915 for ( s = filter, d = realfilter; *s; s++, d++ ) {
923 if ( ludp->lud_attrs ) {
924 attrlist = ludp->lud_attrs;
929 /* TBC: we don't read the host, dammit */
930 rc = ldap_search_st( ld, ludp->lud_dn, ludp->lud_scope,
931 realfilter, attrlist, 0,
934 /* some other trouble - try again later */
935 if ( rc != LDAP_SUCCESS &&
936 rc != LDAP_SIZELIMIT_EXCEEDED ) {
937 syslog( LOG_ALERT, "return 0x%x from X.500",
939 unbind_and_exit( EX_TEMPFAIL );
942 match = ldap_count_entries( ld, res );
944 /* trouble - try again later */
946 syslog( LOG_ALERT, "error parsing result from X.500" );
947 unbind_and_exit( EX_TEMPFAIL );
950 if ( match == 1 || multi_entry ) {
951 for ( e = ldap_first_entry( ld, res ); e != NULL;
952 e = ldap_next_entry( ld, e ) ) {
953 dn = ldap_get_dn( ld, e );
954 resolved = entry_engine( e, dn, address, to, nto,
958 add_error( err, nerr, E_NOEMAIL, address, res );
964 /* more than one match - bounce with ambiguous user? */
966 LDAPMessage *next, *tmpres = NULL;
970 /* not giving rdn preference - bounce with ambiguous user */
971 if ( rdnpref == 0 ) {
972 add_error( err, nerr, E_AMBIGUOUS, address, res );
977 * giving rdn preference - see if any entries were matched
978 * because of their rdn. If so, collect them to deal with
979 * later (== 1 we deliver, > 1 we bounce).
982 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
983 next = ldap_next_entry( ld, e );
984 dn = ldap_get_dn( ld, e );
985 xdn = ldap_explode_dn( dn, 1 );
987 /* XXX bad, but how else can we do it? XXX */
988 if ( strcasecmp( xdn[0], address ) == 0 ) {
989 ldap_delete_result_entry( &res, e );
990 ldap_add_result_entry( &tmpres, e );
993 ldap_value_free( xdn );
997 /* nothing matched by rdn - go ahead and bounce */
998 if ( tmpres == NULL ) {
999 add_error( err, nerr, E_AMBIGUOUS, address, res );
1002 /* more than one matched by rdn - bounce with rdn matches */
1003 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
1004 add_error( err, nerr, E_AMBIGUOUS, address, tmpres );
1008 } else if ( match < 0 ) {
1009 syslog( LOG_ALERT, "error parsing result from X.500" );
1010 unbind_and_exit( EX_TEMPFAIL );
1013 /* otherwise one matched by rdn - send to it */
1014 ldap_msgfree( res );
1018 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
1019 syslog( LOG_ALERT, "error parsing entry from X.500" );
1020 unbind_and_exit( EX_TEMPFAIL );
1023 dn = ldap_get_dn( ld, e );
1025 resolved = entry_engine( e, dn, address, to, nto,
1029 add_error( err, nerr, E_NOEMAIL, address, res );
1030 /* Don't free res if we passed it to add_error */
1032 ldap_msgfree( res );
1055 for ( i = 0; urllist[i]; i++ ) {
1057 if ( !strncasecmp( urllist[i], "mail:", 5 ) ) {
1060 vals[0] = urllist[i] + 5;
1062 add_to( to, nto, vals );
1065 } else if ( ldap_is_ldap_url( urllist[i] ) ) {
1067 resolved = search_ldap_url( urllist[i], NULL,
1068 address, 0, multi_entry,
1069 to, nto, togroups, ngroups,
1072 /* Produce some sensible error here */
1080 * The entry engine processes an entry. Normally, each entry will resolve
1081 * to one or more values that will be added to the 'to' argument. This
1082 * argument needs not be the global 'to' list, it may be the g_to field
1083 * in a group. Groups have no special treatment, unless they require
1104 char ***current_to = to;
1105 int *current_nto = nto;
1106 Group *current_group = NULL;
1108 char *localpart, *domainpart;
1111 for ( i=0; attr_semantics[i] != NULL; i++ ) {
1112 AttrSemantics *as = attr_semantics[i];
1115 vals = ldap_get_values( ld, e, as->as_name );
1116 if ( !vals || vals[0] == NULL ) {
1119 nent = count_values( vals );
1120 if ( nent > 1 && !as->as_m_valued ) {
1121 add_error( err, nerr, E_AMBIGUOUS, address, e );
1124 switch ( as->as_kind ) {
1125 case AS_KIND_RECIPIENT:
1126 if ( ! ( type & ( USER | GROUP_MEMBERS ) ) )
1128 switch ( as->as_syntax ) {
1129 case AS_SYNTAX_RFC822:
1130 add_to( current_to, current_nto, vals );
1133 case AS_SYNTAX_RFC822_EXT:
1134 add_to( current_to, current_nto, vals );
1137 case AS_SYNTAX_NATIVE_MB:
1138 /* We used to concatenate mailHost if set here */
1140 * We used to send a copy to the vacation host
1141 * if onVacation to uid@vacationhost
1143 add_to( current_to, current_nto, vals );
1148 if ( dn_search( vals, address,
1149 current_to, current_nto,
1157 if ( url_list_search( vals, address,
1159 current_to, current_nto,
1161 err, nerr, type ) ) {
1166 case AS_SYNTAX_BOOL_FILTER:
1167 if ( strcasecmp( vals[0], "true" ) ) {
1170 substs[0].sub_char = 'D';
1171 substs[0].sub_value = dn;
1172 substs[1].sub_char = '\0';
1173 substs[1].sub_value = NULL;
1174 if ( url_list_search( vals, address,
1176 current_to, current_nto,
1178 err, nerr, type ) ) {
1185 "Invalid syntax %d for kind %d",
1186 as->as_syntax, as->as_kind );
1191 case AS_KIND_ERRORS:
1192 /* This is a group with special processing */
1193 if ( type & GROUP_ERRORS ) {
1194 switch (as->as_kind) {
1195 case AS_SYNTAX_RFC822:
1196 add_to( current_to, current_nto, vals );
1202 "Invalid syntax %d for kind %d",
1203 as->as_syntax, as->as_kind );
1206 current_group = new_group( dn, togroups,
1208 current_to = ¤t_group->g_members;
1209 current_nto = ¤t_group->g_nmembers;
1210 split_address( address,
1211 &localpart, &domainpart );
1213 sprintf( buf, "%s-%s@%s",
1219 sprintf( buf, "%s-%s@%s",
1224 current_group->g_errorsto = strdup( buf );
1228 case AS_KIND_REQUEST:
1229 /* This is a group with special processing */
1230 if ( type & GROUP_REQUEST ) {
1231 add_to( current_to, current_nto, vals );
1237 /* This is a group with special processing */
1238 if ( type & GROUP_REQUEST ) {
1239 add_to( current_to, current_nto, vals );
1246 "Invalid kind %d", as->as_kind );
1249 ldap_value_free( vals );
1250 if ( as->as_final ) {
1271 int b, resolved = 0;
1273 for ( b = 0; base[b] != NULL; b++ ) {
1275 if ( ! (base[b]->b_search & type) ) {
1279 resolved = search_ldap_url( base[b]->b_url, substs, name,
1281 base[b]->b_m_entries,
1282 to, nto, togroups, ngroups,
1302 struct timeval timeout;
1303 char *localpart, *domainpart;
1308 * Look up the name in X.500, add the appropriate addresses found
1309 * to the to list, or to the err list in case of error. Groups are
1310 * handled by the do_group routine, individuals are handled here.
1311 * When looking up name, we follow the bases hierarchy, looking
1312 * in base[0] first, then base[1], etc. For each base, there is
1313 * a set of search filters to try, in order. If something goes
1314 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
1315 * If the b_rdnpref flag is set, then we give preference to entries
1316 * that matched name because it's their rdn, otherwise not.
1319 split_address( name, &localpart, &domainpart );
1320 timeout.tv_sec = MAIL500_TIMEOUT;
1321 timeout.tv_usec = 0;
1322 substs[0].sub_char = 'm';
1323 substs[0].sub_value = name;
1324 substs[1].sub_char = 'h';
1325 substs[1].sub_value = host;
1326 substs[2].sub_char = 'l';
1327 substs[2].sub_value = localpart;
1328 substs[3].sub_char = 'd';
1329 substs[3].sub_value = domainpart;
1330 substs[4].sub_char = '\0';
1331 substs[4].sub_value = NULL;
1333 resolved = search_bases( NULL, substs, name,
1334 to, nto, togroups, ngroups,
1338 /* not resolved - bounce with user unknown */
1339 if ( type == USER ) {
1340 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
1342 add_error( err, nerr, E_GROUPUNKNOWN, name, NULL );
1348 send_message( char **to )
1351 #ifndef HAVE_WAITPID
1352 WAITSTATUSTYPE status;
1359 strcpy( buf, to[0] );
1360 for ( i = 1; to[i] != NULL; i++ ) {
1362 strcat( buf, to[i] );
1365 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1369 if ( (pid = fork()) != 0 ) {
1371 waitpid( pid, (int *) NULL, 0 );
1373 wait4( pid, &status, WAIT_FLAGS, 0 );
1377 /* to includes sendmailargs */
1378 execv( MAIL500_SENDMAIL, to );
1380 syslog( LOG_ALERT, "execv failed" );
1382 exit( EX_TEMPFAIL );
1387 send_group( Group *group, int ngroup )
1393 #ifndef HAVE_WAITPID
1394 WAITSTATUSTYPE status;
1397 for ( i = 0; i < ngroup; i++ ) {
1398 (void) rewind( stdin );
1400 iargv[0] = MAIL500_SENDMAIL;
1402 iargv[2] = group[i].g_errorsto;
1403 iargv[3] = "-oMrX.500";
1410 add_to( &argv, &argc, iargv );
1411 add_to( &argv, &argc, group[i].g_members );
1417 strcpy( buf, argv[0] );
1418 for ( i = 1; i < argc; i++ ) {
1420 strcat( buf, argv[i] );
1423 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1427 if ( (pid = fork()) != 0 ) {
1429 waitpid( pid, (int *) NULL, 0 );
1431 wait4( pid, &status, WAIT_FLAGS, 0 );
1435 execv( MAIL500_SENDMAIL, argv );
1437 syslog( LOG_ALERT, "execv failed" );
1439 exit( EX_TEMPFAIL );
1445 send_errors( Error *err, int nerr )
1447 int pid, i, namelen;
1452 #ifndef HAVE_WAITPID
1453 WAITSTATUSTYPE status;
1456 if ( strcmp( MAIL500_BOUNCEFROM, mailfrom ) == 0 ) {
1457 mailfrom = errorsfrom;
1460 argv[0] = MAIL500_SENDMAIL;
1461 argv[1] = "-oMrX.500";
1465 argv[5] = MAIL500_BOUNCEFROM;
1472 strcpy( buf, argv[0] );
1473 for ( i = 1; argv[i] != NULL; i++ ) {
1475 strcat( buf, argv[i] );
1478 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1481 if ( pipe( fd ) == -1 ) {
1482 syslog( LOG_ALERT, "cannot create pipe" );
1483 exit( EX_TEMPFAIL );
1486 if ( (pid = fork()) != 0 ) {
1487 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1488 syslog( LOG_ALERT, "cannot fdopen pipe" );
1489 exit( EX_TEMPFAIL );
1492 fprintf( fp, "To: %s\n", mailfrom );
1493 fprintf( fp, "From: %s\n", errorsfrom );
1494 fprintf( fp, "Subject: undeliverable mail\n" );
1495 fprintf( fp, "\n" );
1496 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1497 for ( i = 0; i < nerr; i++ ) {
1498 namelen = strlen( err[i].e_addr );
1499 fprintf( fp, "\n" );
1501 switch ( err[i].e_code ) {
1503 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1506 case E_GROUPUNKNOWN:
1507 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1511 fprintf( fp, "%s: Group member does not exist\n",
1513 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1514 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1515 fprintf( fp, "then re-adding the person to the group.\n" );
1519 fprintf( fp, "%s: Group exists but has no request address\n",
1524 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1529 fprintf( fp, "%s: Group exists but has no owner\n",
1534 do_ambiguous( fp, &err[i], namelen );
1538 do_noemail( fp, &err[i], namelen );
1541 case E_MEMBERNOEMAIL:
1542 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1546 case E_JOINMEMBERNOEMAIL:
1547 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1552 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1553 err[i].e_addr, err[i].e_loop );
1557 fprintf( fp, "%s: Group has no members\n",
1562 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1563 unbind_and_exit( EX_TEMPFAIL );
1568 fprintf( fp, "\n------- The original message sent:\n\n" );
1570 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1576 waitpid( pid, (int *) NULL, 0 );
1578 wait4( pid, &status, WAIT_FLAGS, 0 );
1583 execv( MAIL500_SENDMAIL, argv );
1585 syslog( LOG_ALERT, "execv failed" );
1587 exit( EX_TEMPFAIL );
1592 do_noemail( FILE *fp, Error *err, int namelen )
1598 fprintf(fp, "%s: User has no email address registered.\n",
1600 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1601 namelen, " ", err->e_addr );
1604 dn = ldap_get_dn( ld, err->e_msg );
1605 ufn = ldap_explode_dn( dn, 1 );
1606 rdn = strdup( ufn[0] );
1607 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1608 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1610 for ( i = 0; vals[i]; i++ ) {
1611 last = strlen( vals[i] ) - 1;
1612 if ( isdigit((unsigned char) vals[i][last]) ) {
1613 rdn = strdup( vals[i] );
1618 ldap_value_free( vals );
1621 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1624 ldap_value_free( ufn );
1626 /* titles or descriptions */
1627 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1628 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1630 fprintf( fp, "%*s No title or description registered\n",
1633 for ( i = 0; vals[i] != NULL; i++ ) {
1634 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1637 ldap_value_free( vals );
1640 /* postal address */
1641 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1643 fprintf( fp, "%*s No postal address registered\n", namelen,
1646 fprintf( fp, "%*s ", namelen, " " );
1647 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1648 if ( vals[0][i] == '$' ) {
1649 fprintf( fp, "\n%*s ", namelen, " " );
1650 while ( isspace((unsigned char) vals[0][i+1]) )
1653 fprintf( fp, "%c", vals[0][i] );
1656 fprintf( fp, "\n" );
1658 ldap_value_free( vals );
1661 /* telephone number */
1662 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1664 fprintf( fp, "%*s No phone number registered\n", namelen,
1667 for ( i = 0; vals[i] != NULL; i++ ) {
1668 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1671 ldap_value_free( vals );
1677 do_ambiguous( FILE *fp, Error *err, int namelen )
1684 i = ldap_result2error( ld, err->e_msg, 0 );
1686 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1687 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1688 ldap_count_entries( ld, err->e_msg ) );
1690 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1691 e = ldap_next_entry( ld, e ) ) {
1692 dn = ldap_get_dn( ld, e );
1693 ufn = ldap_explode_dn( dn, 1 );
1694 rdn = strdup( ufn[0] );
1695 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1696 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1697 for ( i = 0; vals[i]; i++ ) {
1698 last = strlen( vals[i] ) - 1;
1699 if (isdigit((unsigned char) vals[i][last])) {
1700 rdn = strdup( vals[i] );
1705 ldap_value_free( vals );
1710 if ( isgroup( e ) ) {
1711 vals = ldap_get_values( ld, e, "description" );
1713 vals = ldap_get_values( ld, e, "title" );
1716 vals = ldap_get_values( ld, e, "description" );
1718 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1719 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1720 fprintf( fp, " %s\n", vals[i] );
1725 ldap_value_free( ufn );
1727 ldap_value_free( vals );
1732 count_values( char **list )
1736 for ( i = 0; list && list[i] != NULL; i++ )
1743 add_to( char ***list, int *nlist, char **new )
1745 int i, nnew, oldnlist;
1747 nnew = count_values( new );
1750 if ( *list == NULL || *nlist == 0 ) {
1751 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1754 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1755 nnew * sizeof(char *) + sizeof(char *) );
1759 for ( i = 0; i < nnew; i++ )
1760 (*list)[i + oldnlist] = strdup( new[i] );
1761 (*list)[*nlist] = NULL;
1765 add_single_to( char ***list, char *new )
1769 if ( *list == NULL ) {
1771 *list = (char **) malloc( 2 * sizeof(char *) );
1773 nlist = count_values( *list );
1774 *list = (char **) realloc( *list,
1775 ( nlist + 2 ) * sizeof(char *) );
1778 (*list)[nlist] = strdup( new );
1779 (*list)[nlist+1] = NULL;
1783 isgroup( LDAPMessage *e )
1788 if ( !groupclasses ) {
1792 oclist = ldap_get_values( ld, e, "objectClass" );
1794 for ( i = 0; oclist[i] != NULL; i++ ) {
1795 for ( j = 0; groupclasses[j] != NULL; j++ ) {
1796 if ( strcasecmp( oclist[i], groupclasses[j] ) == 0 ) {
1797 ldap_value_free( oclist );
1802 ldap_value_free( oclist );
1808 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1811 *err = (Error *) malloc( sizeof(Error) );
1813 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1816 (*err)[*nerr].e_code = code;
1817 (*err)[*nerr].e_addr = strdup( addr );
1818 (*err)[*nerr].e_msg = msg;
1823 unbind_and_exit( int rc )
1827 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1828 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );