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.
18 #include <ac/string.h>
19 #include <ac/syslog.h>
23 #ifdef HAVE_SYS_PARAM_H
24 #include <sys/param.h>
27 #include <sys/resource.h>
33 #include "ldapconfig.h"
36 #define GROUP_ERRORS 0x02
37 #define GROUP_REQUEST 0x04
38 #define GROUP_MEMBERS 0x08
39 #define GROUP_OWNER 0x10
42 #define ERRORS "errors"
43 #define REQUEST "request"
44 #define REQUESTS "requests"
45 #define MEMBERS "members"
47 #define OWNERS "owners"
50 char *vacationhost = NULL;
51 char *errorsfrom = NULL;
52 char *mailfrom = NULL;
54 char *ldaphost = LDAPHOST;
60 #define E_USERUNKNOWN 1
66 #define E_JOINMEMBERNOEMAIL 7
67 #define E_MEMBERNOEMAIL 8
69 #define E_NOMEMBERS 10
71 #define E_GROUPUNKNOWN 12
77 #define e_msg e_union.e_u_msg
78 #define e_loop e_union.e_u_loop
81 typedef struct groupto {
87 typedef struct baseinfo {
88 char *b_dn; /* dn to start searching at */
89 char b_rdnpref; /* give rdn's preference when searching? */
90 int b_search; /* ORed with the type of thing the address */
91 /* looks like (USER, GROUP_ERRORS, etc.) */
92 /* to see if this should be searched */
93 char *b_filter[3]; /* filter to apply - name substituted for %s */
94 /* (up to three of them) */
98 { "ou=People, o=University of Michigan, c=US",
100 "uid=%s", "cn=%s", NULL,
101 "ou=System Groups, ou=Groups, o=University of Michigan, c=US",
103 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
104 "ou=User Groups, ou=Groups, o=University of Michigan, c=US",
106 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
110 char *sendmailargs[] = { MAIL500_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
112 static char *attrs[] = { "objectClass", "title", "postaladdress",
113 "telephoneNumber", "mail", "description", "owner",
114 "errorsTo", "rfc822ErrorsTo", "requestsTo",
115 "rfc822RequestsTo", "joinable", "cn", "member",
116 "moderator", "onVacation", "uid",
117 "suppressNoEmailError", NULL };
121 static do_group_members();
122 static send_message();
123 static send_errors();
125 static do_ambiguous();
130 static unbind_and_exit();
133 static has_attributes();
134 static char **get_attributes_mail_dn();
135 static char *canonical();
145 int numto, ngroups, numerr, nargs;
148 extern int optind, errno;
151 if ( (myname = strrchr( argv[0], '/' )) == NULL )
152 myname = strdup( argv[0] );
154 myname = strdup( myname + 1 );
157 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
159 openlog( myname, OPENLOG_OPTIONS );
162 while ( (i = getopt( argc, argv, "d:f:h:l:m:v:" )) != EOF ) {
164 case 'd': /* turn on debugging */
165 debug = atoi( optarg );
168 case 'f': /* who it's from & where errors should go */
169 mailfrom = strdup( optarg );
170 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
171 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
172 sendmailargs[j+1] = mailfrom;
178 case 'h': /* hostname */
179 host = strdup( optarg );
180 hostlen = strlen(host);
183 case 'l': /* ldap host */
184 ldaphost = strdup( optarg );
187 /* mailer-daemon address - who we should */
188 case 'm': /* say errors come from */
189 errorsfrom = strdup( optarg );
192 case 'v': /* vacation host */
193 vacationhost = strdup( optarg );
197 syslog( LOG_ALERT, "unknown option" );
202 if ( mailfrom == NULL ) {
203 syslog( LOG_ALERT, "required argument -f not present" );
206 if ( errorsfrom == NULL ) {
207 syslog( LOG_ALERT, "required argument -m not present" );
210 if ( host == NULL ) {
211 syslog( LOG_ALERT, "required argument -h not present" );
215 if ( connect_to_x500() != 0 )
224 syslog( LOG_ALERT, "running as %d", geteuid() );
225 strcpy( buf, argv[0] );
226 for ( i = 1; i < argc; i++ ) {
228 strcat( buf, argv[i] );
231 syslog( LOG_ALERT, "args: (%s)", buf );
236 add_to( &tolist, &numto, sendmailargs );
238 ngroups = numerr = 0;
241 for ( i = optind; i < argc; i++ ) {
245 for ( j = 0; argv[i][j] != '\0'; j++ ) {
246 if ( argv[i][j] == '.' || argv[i][j] == '_' )
251 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
254 if ((strcasecmp(s, ERROR) == 0) ||
255 (strcasecmp(s, ERRORS) == 0)) {
258 } else if ((strcasecmp(s, REQUEST) == 0) ||
259 (strcasecmp(s, REQUESTS) == 0)) {
260 type = GROUP_REQUEST;
262 } else if ( strcasecmp( s, MEMBERS ) == 0 ) {
263 type = GROUP_MEMBERS;
265 } else if ((strcasecmp(s, OWNER) == 0) ||
266 (strcasecmp(s, OWNERS) == 0)) {
272 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
273 &errlist, &numerr, type );
277 * If we have both errors and successful deliveries to make or if
278 * if there are any groups to deliver to, we basically need to read
279 * the message twice. So, we have to put it in a tmp file.
282 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
287 if ( (fp = tmpfile()) == NULL ) {
288 syslog( LOG_ALERT, "could not open tmp file" );
289 unbind_and_exit( EX_TEMPFAIL );
292 /* copy the message to a temp file */
293 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
294 if ( fputs( buf, fp ) == EOF ) {
295 syslog( LOG_ALERT, "error writing tmpfile" );
296 unbind_and_exit( EX_TEMPFAIL );
300 if ( dup2( fileno( fp ), 0 ) == -1 ) {
301 syslog( LOG_ALERT, "could not dup2 tmpfile" );
302 unbind_and_exit( EX_TEMPFAIL );
308 /* deal with errors */
311 syslog( LOG_ALERT, "sending errors" );
313 (void) rewind( stdin );
314 send_errors( errlist, numerr );
317 (void) ldap_unbind( ld );
319 /* send to groups with errorsTo */
322 syslog( LOG_ALERT, "sending to groups with errorsto" );
324 (void) rewind( stdin );
325 send_group( togroups, ngroups );
328 /* send to expanded aliases and groups w/o errorsTo */
329 if ( numto > nargs ) {
331 syslog( LOG_ALERT, "sending to aliases and groups" );
333 (void) rewind( stdin );
334 send_message( tolist );
342 if ( (ld = ldap_open( ldaphost, LDAP_PORT )) == NULL ) {
343 syslog( LOG_ALERT, "ldap_open failed" );
346 ld->ld_sizelimit = MAIL500_MAXAMBIGUOUS;
347 ld->ld_deref = LDAP_DEREF_ALWAYS;
349 if ( ldap_simple_bind_s( ld, MAIL500_BINDDN, NULL ) != LDAP_SUCCESS ) {
350 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
364 for ( i = 0; a[i] != '\0'; i++ ) {
365 if ( a[i] != b[i] ) {
370 if ( b[i] == ' ' || b[i] == '.' || b[i] == '_' )
384 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
395 LDAPMessage *e, *res;
396 struct timeval timeout;
399 char realfilter[1024];
400 char **mail, **onvacation = NULL, **uid = NULL;
403 * Look up the name in X.500, add the appropriate addresses found
404 * to the to list, or to the err list in case of error. Groups are
405 * handled by the do_group routine, individuals are handled here.
406 * When looking up name, we follow the bases hierarchy, looking
407 * in base[0] first, then base[1], etc. For each base, there is
408 * a set of search filters to try, in order. If something goes
409 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
410 * If the b_rdnpref flag is set, then we give preference to entries
411 * that matched name because it's their rdn, otherwise not.
414 timeout.tv_sec = MAIL500_TIMEOUT;
416 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
417 if ( ! (base[b].b_search & type) ) {
420 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
421 char *format, *p, *s, *d;
425 for ( argc = 0; argc < 3; argc++ ) {
429 format = strdup( base[b].b_filter[f] );
430 for ( argc = 0, p = format; *p; p++ ) {
433 case 's': /* %s is the name */
437 case 'h': /* %h is the host */
444 "unknown format %c", *p );
452 /* three names ought to do... */
453 sprintf( filter, format, argv[0], argv[1], argv[2] );
455 for ( s = filter, d = realfilter; *s; s++, d++ ) {
464 rc = ldap_search_st( ld, base[b].b_dn,
465 LDAP_SCOPE_SUBTREE, realfilter, attrs, 0, &timeout,
468 /* some other trouble - try again later */
469 if ( rc != LDAP_SUCCESS &&
470 rc != LDAP_SIZELIMIT_EXCEEDED ) {
471 syslog( LOG_ALERT, "return 0x%x from X.500",
473 unbind_and_exit( EX_TEMPFAIL );
476 if ( (match = ldap_count_entries( ld, res )) != 0 )
486 /* trouble - try again later */
488 syslog( LOG_ALERT, "error parsing result from X.500" );
489 unbind_and_exit( EX_TEMPFAIL );
492 /* no matches - bounce with user unknown */
494 if ( type == USER ) {
495 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
497 add_error( err, nerr, E_GROUPUNKNOWN, name, NULLMSG );
502 /* more than one match - bounce with ambiguous user? */
504 LDAPMessage *next, *tmpres = NULL;
508 /* not giving rdn preference - bounce with ambiguous user */
509 if ( base[b].b_rdnpref == 0 ) {
510 add_error( err, nerr, E_AMBIGUOUS, name, res );
515 * giving rdn preference - see if any entries were matched
516 * because of their rdn. If so, collect them to deal with
517 * later (== 1 we deliver, > 1 we bounce).
520 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
521 next = ldap_next_entry( ld, e );
522 dn = ldap_get_dn( ld, e );
523 xdn = ldap_explode_dn( dn, 1 );
525 /* XXX bad, but how else can we do it? XXX */
526 if ( strcasecmp( xdn[0], name ) == 0 ) {
527 ldap_delete_result_entry( &res, e );
528 ldap_add_result_entry( &tmpres, e );
531 ldap_value_free( xdn );
535 /* nothing matched by rdn - go ahead and bounce */
536 if ( tmpres == NULL ) {
537 add_error( err, nerr, E_AMBIGUOUS, name, res );
540 /* more than one matched by rdn - bounce with rdn matches */
541 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
542 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
546 } else if ( match < 0 ) {
547 syslog( LOG_ALERT, "error parsing result from X.500" );
548 unbind_and_exit( EX_TEMPFAIL );
551 /* otherwise one matched by rdn - send to it */
557 * if we get this far, it means that we found a single match for
558 * name. for a user, we deliver to the mail attribute or bounce
559 * with address and phone if no mail attr. for a group, we
560 * deliver to all members or bounce to rfc822ErrorsTo if no members.
564 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
565 syslog( LOG_ALERT, "error parsing entry from X.500" );
566 unbind_and_exit( EX_TEMPFAIL );
569 dn = ldap_get_dn( ld, e );
571 if ( type == GROUP_ERRORS ) {
572 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
573 do_group_errors( e, dn, to, nto, err, nerr );
575 } else if ( type == GROUP_REQUEST ) {
576 /* sent to group-request - resend to [rfc822]RequestsTo attr */
577 do_group_request( e, dn, to, nto, err, nerr );
579 } else if ( type == GROUP_MEMBERS ) {
580 /* sent to group-members - expand */
581 do_group_members( e, dn, to, nto, togroups, ngroups, err,
584 } else if ( type == GROUP_OWNER ) {
585 /* sent to group-owner - resend to owner attr */
586 do_group_owner( e, dn, to, nto, err, nerr );
588 } else if ( isgroup( e ) ) {
590 * sent to group - resend from [rfc822]ErrorsTo if it's there,
591 * otherwise, expand the group
594 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
600 * sent to user - mail attribute => add it to the to list,
603 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
608 /* try to detect simple mail loops */
609 sprintf( buf, "%s@%s", name, host );
610 for ( i = 0; mail[i] != NULL; i++ ) {
612 * address is the same as the one we're
613 * sending to - mail loop. syslog the
614 * problem, bounce a message back to the
615 * sender (who else?), and delete the bogus
616 * addr from the list.
619 if ( (h = strchr( mail[i], '@' )) != NULL ) {
621 if ( strcasecmp( h, host ) == 0 ) {
623 "potential loop detected (%s)",
628 if ( mailcmp( buf, mail[i] ) == 0 ) {
630 "loop detected (%s)", mail[i] );
632 /* remove the bogus address */
633 for ( j = i; mail[j] != NULL; j++ ) {
638 if ( mail[0] != NULL ) {
639 add_to( to, nto, mail );
641 add_error( err, nerr, E_NOEMAIL, name, res );
644 ldap_value_free( mail );
646 add_error( err, nerr, E_NOEMAIL, name, res );
650 * If the user is on vacation, send a copy of the mail to
651 * the vacation server. The address is constructed from
652 * the vacationhost (set in a command line argument) and
653 * the uid (XXX this should be more general XXX).
656 if ( vacationhost != NULL && (onvacation = ldap_get_values( ld,
657 e, "onVacation" )) != NULL && strcasecmp( onvacation[0],
662 if ( (uid = ldap_get_values( ld, e, "uid" )) != NULL ) {
663 sprintf( buf, "%s@%s", uid[0], vacationhost );
668 add_to( to, nto, vaddr );
671 "user without a uid on vacation (%s)",
677 if ( onvacation != NULL ) {
678 ldap_value_free( onvacation );
681 ldap_value_free( uid );
689 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
703 * If this group has an rfc822ErrorsTo attribute, we need to
704 * arrange for errors involving this group to go there, not
705 * to the sender. Since sendmail only has the concept of a
706 * single sender, we arrange for errors to go to groupname-errors,
707 * which we then handle specially when (if) it comes back to us
708 * by expanding to all the rfc822ErrorsTo addresses. If it has no
709 * rfc822ErrorsTo attribute, we call do_group_members() to expand
713 if ( group_loop( dn ) ) {
718 * check for moderated groups - if the group has a moderator
719 * attribute, we check to see if the from address is one of
720 * the moderator values. if so, continue on. if not, arrange
721 * to send the mail to the moderator(s). need to do this before
722 * we change the from below.
725 if ( (moderator = ldap_get_values( ld, e, "moderator" )) != NULL ) {
726 /* check if it came from any of the group's moderators */
727 for ( i = 0; moderator[i] != NULL; i++ ) {
728 if ( strcasecmp( moderator[i], mailfrom ) == 0 )
732 /* not from the moderator? */
733 if ( moderator[i] == NULL ) {
734 add_to( to, nto, moderator );
735 ldap_value_free( moderator );
739 /* else from the moderator - fall through and deliver it */
742 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
743 add_group( dn, togroups, ngroups );
748 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
755 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
765 int i, rc, anymembers;
767 char **mail, **member, **joinable, **suppress;
769 LDAPMessage *ee, *res;
770 struct timeval timeout;
773 * if all has gone according to plan, we've already arranged for
774 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
775 * so all we have to do here is arrange to send to the
776 * rfc822Mailbox attribute, the member attribute, and anyone who
777 * has joined the group by setting memberOfGroup equal to the
781 /* add members in the group itself - mail attribute */
783 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
785 add_to( to, nto, mail );
787 ldap_value_free( mail );
790 /* add members in the group itself - member attribute */
791 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
792 suppress = ldap_get_values( ld, e, "suppressNoEmailError" );
794 for ( i = 0; member[i] != NULL; i++ ) {
795 if ( strcasecmp( dn, member[i] ) == 0 ) {
796 syslog( LOG_ALERT, "group (%s) contains itself",
800 add_member( dn, member[i], to, nto, togroups,
801 ngroups, err, nerr, suppress );
805 ldap_value_free( suppress );
807 ldap_value_free( member );
810 /* add members who have joined by setting memberOfGroup */
811 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
812 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
813 if ( ! anymembers ) {
814 add_error( err, nerr, E_NOMEMBERS, dn,
818 ldap_value_free( joinable );
821 ldap_value_free( joinable );
823 sprintf( filter, "(memberOfGroup=%s)", dn );
825 timeout.tv_sec = MAIL500_TIMEOUT;
828 /* for each subtree to look in... */
829 ld->ld_sizelimit = MAIL500_MAXGROUPMEMBERS;
830 for ( i = 0; base[i].b_dn != NULL; i++ ) {
831 /* find entries that have joined this group... */
832 rc = ldap_search_st( ld, base[i].b_dn,
833 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
836 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
837 rc == LDAP_TIMELIMIT_EXCEEDED ) {
839 "group search limit exceeded %d", rc );
840 unbind_and_exit( EX_TEMPFAIL );
843 if ( rc != LDAP_SUCCESS ) {
844 syslog( LOG_ALERT, "group search return 0x%x",
846 unbind_and_exit( EX_TEMPFAIL );
849 /* for each entry that has joined... */
850 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
851 ee = ldap_next_entry( ld, ee ) ) {
853 if ( isgroup( ee ) ) {
854 ndn = ldap_get_dn( ld, ee );
856 if ( do_group( e, ndn, to, nto,
857 togroups, ngroups, err, nerr )
860 "group loop (%s) (%s)",
869 /* add them to the to list */
870 if ( (mail = ldap_get_values( ld, ee, "mail" ))
872 add_to( to, nto, mail );
874 ldap_value_free( mail );
876 /* else generate a bounce */
878 ndn = ldap_get_dn( ld, ee );
880 add_error( err, nerr,
881 E_JOINMEMBERNOEMAIL, ndn, NULLMSG );
889 ld->ld_sizelimit = MAIL500_MAXAMBIGUOUS;
892 if ( ! anymembers ) {
893 add_error( err, nerr, E_NOMEMBERS, dn, NULLMSG );
899 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr, suppress )
913 LDAPMessage *res, *e;
914 struct timeval timeout;
916 timeout.tv_sec = MAIL500_TIMEOUT;
918 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
919 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
920 if ( rc == LDAP_NO_SUCH_OBJECT ) {
921 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
925 syslog( LOG_ALERT, "member search return 0x%x", rc );
927 unbind_and_exit( EX_TEMPFAIL );
931 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
932 syslog( LOG_ALERT, "member search error parsing entry" );
934 unbind_and_exit( EX_TEMPFAIL );
936 ndn = ldap_get_dn( ld, e );
938 /* allow groups within groups */
939 if ( isgroup( e ) ) {
940 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
942 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
950 /* send to the member's mail attribute */
951 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
952 add_to( to, nto, mail );
954 ldap_value_free( mail );
956 /* else generate a bounce */
958 if ( suppress == NULL || strcasecmp( suppress[0], "FALSE" )
960 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
969 do_group_request( e, dn, to, nto, err, nerr )
979 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
980 "requestsTo" )) != NULL ) {
981 add_to( to, nto, requeststo );
983 ldap_value_free( requeststo );
985 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
991 do_group_errors( e, dn, to, nto, err, nerr )
1001 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
1002 "errorsTo" )) != NULL ) {
1003 add_to( to, nto, errorsto );
1005 ldap_value_free( errorsto );
1007 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
1013 do_group_owner( e, dn, to, nto, err, nerr )
1023 if ( (owner = get_attributes_mail_dn( e, "", "owner" )) != NULL ) {
1024 add_to( to, nto, owner );
1025 ldap_value_free( owner );
1027 add_error( err, nerr, E_NOOWNER, dn, NULLMSG );
1037 #ifndef HAVE_WAITPID
1038 WAITSTATUSTYPE status;
1045 strcpy( buf, to[0] );
1046 for ( i = 1; to[i] != NULL; i++ ) {
1048 strcat( buf, to[i] );
1051 syslog( LOG_ALERT, "send_message execing sendmail: (%s)", buf );
1055 if ( pid = fork() ) {
1057 waitpid( pid, (int *) NULL, 0 );
1059 wait4( pid, &status, WAIT_FLAGS, 0 );
1063 /* to includes sendmailargs */
1064 execv( MAIL500_SENDMAIL, to );
1066 syslog( LOG_ALERT, "execv failed" );
1068 exit( EX_TEMPFAIL );
1073 send_group( group, ngroup )
1081 #ifndef HAVE_WAITPID
1082 WAITSTATUSTYPE status;
1085 for ( i = 0; i < ngroup; i++ ) {
1086 (void) rewind( stdin );
1088 iargv[0] = MAIL500_SENDMAIL;
1090 iargv[2] = group[i].g_errorsto;
1091 iargv[3] = "-oMrX.500";
1098 add_to( &argv, &argc, iargv );
1099 add_to( &argv, &argc, group[i].g_members );
1105 strcpy( buf, argv[0] );
1106 for ( i = 1; i < argc; i++ ) {
1108 strcat( buf, argv[i] );
1111 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1115 if ( pid = fork() ) {
1117 waitpid( pid, (int *) NULL, 0 );
1119 wait4( pid, &status, WAIT_FLAGS, 0 );
1123 execv( MAIL500_SENDMAIL, argv );
1125 syslog( LOG_ALERT, "execv failed" );
1127 exit( EX_TEMPFAIL );
1135 send_errors( err, nerr )
1139 int pid, i, namelen;
1144 #ifndef HAVE_WAITPID
1145 WAITSTATUSTYPE status;
1148 argv[0] = MAIL500_SENDMAIL;
1149 argv[1] = "-oMrX.500";
1153 argv[5] = errorsfrom;
1160 strcpy( buf, argv[0] );
1161 for ( i = 1; argv[i] != NULL; i++ ) {
1163 strcat( buf, argv[i] );
1166 syslog( LOG_ALERT, "execing sendmail: (%s)", buf );
1169 if ( pipe( fd ) == -1 ) {
1170 syslog( LOG_ALERT, "cannot create pipe" );
1171 exit( EX_TEMPFAIL );
1174 if ( pid = fork() ) {
1175 if ( (fp = fdopen( fd[1], "w" )) == NULL ) {
1176 syslog( LOG_ALERT, "cannot fdopen pipe" );
1177 exit( EX_TEMPFAIL );
1180 fprintf( fp, "To: %s\n", mailfrom );
1181 fprintf( fp, "From: %s\n", errorsfrom );
1182 fprintf( fp, "Subject: undeliverable mail\n" );
1183 fprintf( fp, "\n" );
1184 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1185 for ( i = 0; i < nerr; i++ ) {
1186 namelen = strlen( err[i].e_addr );
1187 fprintf( fp, "\n" );
1189 switch ( err[i].e_code ) {
1191 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1194 case E_GROUPUNKNOWN:
1195 fprintf( fp, "%s: Group unknown\n", err[i].e_addr );
1199 fprintf( fp, "%s: Group member does not exist\n",
1201 fprintf( fp, "This could be because the distinguished name of the person has changed\n" );
1202 fprintf( fp, "If this is the case, the problem can be solved by removing and\n" );
1203 fprintf( fp, "then re-adding the person to the group.\n" );
1207 fprintf( fp, "%s: Group exists but has no request address\n",
1212 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1217 fprintf( fp, "%s: Group exists but has no owner\n",
1222 do_ambiguous( fp, &err[i], namelen );
1226 do_noemail( fp, &err[i], namelen );
1229 case E_MEMBERNOEMAIL:
1230 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1234 case E_JOINMEMBERNOEMAIL:
1235 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1240 fprintf( fp, "%s: User has created a mail loop by adding address %s to their X.500 entry\n",
1241 err[i].e_addr, err[i].e_loop );
1245 fprintf( fp, "%s: Group has no members\n",
1250 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1251 unbind_and_exit( EX_TEMPFAIL );
1256 fprintf( fp, "\n------- The original message sent:\n\n" );
1258 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1264 waitpid( pid, (int *) NULL, 0 );
1266 wait4( pid, &status, WAIT_FLAGS, 0 );
1271 execv( MAIL500_SENDMAIL, argv );
1273 syslog( LOG_ALERT, "execv failed" );
1275 exit( EX_TEMPFAIL );
1282 do_noemail( fp, err, namelen )
1291 fprintf(fp, "%s: User has no email address registered.\n",
1293 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1294 namelen, " ", err->e_addr );
1297 dn = ldap_get_dn( ld, err->e_msg );
1298 ufn = ldap_explode_dn( dn, 1 );
1299 rdn = strdup( ufn[0] );
1300 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1301 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1303 for ( i = 0; vals[i]; i++ ) {
1304 last = strlen( vals[i] ) - 1;
1305 if ( isdigit( vals[i][last] ) ) {
1306 rdn = strdup( vals[i] );
1311 ldap_value_free( vals );
1314 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1317 ldap_value_free( ufn );
1319 /* titles or descriptions */
1320 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1321 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1323 fprintf( fp, "%*s No title or description registered\n",
1326 for ( i = 0; vals[i] != NULL; i++ ) {
1327 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1330 ldap_value_free( vals );
1333 /* postal address */
1334 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1336 fprintf( fp, "%*s No postal address registered\n", namelen,
1339 fprintf( fp, "%*s ", namelen, " " );
1340 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1341 if ( vals[0][i] == '$' ) {
1342 fprintf( fp, "\n%*s ", namelen, " " );
1343 while ( isspace( vals[0][i+1] ) )
1346 fprintf( fp, "%c", vals[0][i] );
1349 fprintf( fp, "\n" );
1351 ldap_value_free( vals );
1354 /* telephone number */
1355 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1357 fprintf( fp, "%*s No phone number registered\n", namelen,
1360 for ( i = 0; vals[i] != NULL; i++ ) {
1361 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1364 ldap_value_free( vals );
1370 do_ambiguous( fp, err, namelen )
1380 i = ldap_result2error( ld, err->e_msg, 0 );
1382 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1383 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1384 ldap_count_entries( ld, err->e_msg ) );
1386 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1387 e = ldap_next_entry( ld, e ) ) {
1388 dn = ldap_get_dn( ld, e );
1389 ufn = ldap_explode_dn( dn, 1 );
1390 rdn = strdup( ufn[0] );
1391 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1392 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1393 for ( i = 0; vals[i]; i++ ) {
1394 last = strlen( vals[i] ) - 1;
1395 if ( isdigit( vals[i][last] ) ) {
1396 rdn = strdup( vals[i] );
1401 ldap_value_free( vals );
1405 if ( isgroup( e ) ) {
1406 vals = ldap_get_values( ld, e, "description" );
1408 vals = ldap_get_values( ld, e, "title" );
1411 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1412 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1413 fprintf( fp, " %s\n", vals[i] );
1418 ldap_value_free( ufn );
1420 ldap_value_free( vals );
1425 count_values( list )
1430 for ( i = 0; list && list[i] != NULL; i++ )
1437 add_to( list, nlist, new )
1442 int i, nnew, oldnlist;
1444 nnew = count_values( new );
1447 if ( *list == NULL || *nlist == 0 ) {
1448 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1451 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1452 nnew * sizeof(char *) + sizeof(char *) );
1456 for ( i = 0; i < nnew; i++ )
1457 (*list)[i + oldnlist] = strdup( new[i] );
1458 (*list)[*nlist] = NULL;
1470 oclist = ldap_get_values( ld, e, "objectClass" );
1472 for ( i = 0; oclist[i] != NULL; i++ ) {
1473 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1474 ldap_value_free( oclist );
1478 ldap_value_free( oclist );
1484 add_error( err, nerr, code, addr, msg )
1492 *err = (Error *) malloc( sizeof(Error) );
1494 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1497 (*err)[*nerr].e_code = code;
1498 (*err)[*nerr].e_addr = strdup( addr );
1499 (*err)[*nerr].e_msg = msg;
1506 add_group( dn, list, nlist )
1514 for ( i = 0; i < *nlist; i++ ) {
1515 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1516 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1521 ufn = ldap_explode_dn( dn, 1 );
1522 namelen = strlen( ufn[0] );
1524 if ( *nlist == 0 ) {
1525 *list = (Group *) malloc( sizeof(Group) );
1527 *list = (Group *) realloc( *list, (*nlist + 1) *
1531 /* send errors to groupname-errors@host */
1532 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1534 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1535 (void) canonical( (*list)[*nlist].g_errorsto );
1537 /* send to groupname-members@host - make it a list for send_group */
1538 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1539 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1540 sizeof(MEMBERS) + hostlen + 2 );
1541 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1543 (void) canonical( (*list)[*nlist].g_members[0] );
1544 (*list)[*nlist].g_members[1] = NULL;
1546 /* save the group's dn so we can check for loops above */
1547 (*list)[*nlist].g_dn = strdup( dn );
1551 ldap_value_free( ufn );
1557 unbind_and_exit( rc )
1562 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1563 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1574 for ( ; *s != '\0'; s++ ) {
1587 static char **groups;
1590 for ( i = 0; i < ngroups; i++ ) {
1591 if ( strcmp( dn, groups[i] ) == 0 )
1596 groups = (char **) malloc( sizeof(char *) );
1598 groups = (char **) realloc( groups,
1599 (ngroups + 1) * sizeof(char *) );
1601 groups[ngroups++] = strdup( dn );
1607 has_attributes( e, attr1, attr2 )
1614 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1615 ldap_value_free( attr );
1619 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1620 ldap_value_free( attr );
1628 get_attributes_mail_dn( e, attr1, attr2 )
1631 char *attr2; /* this one is dn-valued */
1633 LDAPMessage *ee, *res;
1634 char **vals, **dnlist, **mail, **grname, **graddr;
1637 struct timeval timeout;
1639 dn = ldap_get_dn( ld, e );
1641 vals = ldap_get_values( ld, e, attr1 );
1642 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1645 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1646 timeout.tv_sec = MAIL500_TIMEOUT;
1647 timeout.tv_usec = 0;
1649 for ( i = 0; dnlist[i] != NULL; i++ ) {
1650 if ( (rc = ldap_search_st( ld, dnlist[i],
1651 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1652 &timeout, &res )) != LDAP_SUCCESS ) {
1653 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1654 unbind_and_exit( EX_TEMPFAIL );
1657 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1663 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1664 syslog( LOG_ALERT, "error parsing x500 entry" );
1668 if ( isgroup(ee) ) {
1671 grname = ldap_explode_dn( dnlist[i], 1 );
1673 /* groupname + host + @ + null */
1674 graddr[0] = (char *) malloc( strlen( grname[0] )
1675 + strlen( host ) + 2 );
1677 sprintf( graddr[0], "%s@%s", grname[0], host);
1678 (void) canonical( graddr[0] );
1680 add_to( &vals, &nto, graddr );
1683 ldap_value_free( grname );
1684 } else if ( (mail = ldap_get_values( ld, ee, "mail" ))
1686 add_to( &vals, &nto, mail );
1688 ldap_value_free( mail );
1691 ldap_msgfree( res );