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/socket.h>
19 #include <ac/string.h>
21 #include <ac/syslog.h>
24 #include <ac/unistd.h>
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
30 #ifdef HAVE_SYS_RESOURCE_H
31 #include <sys/resource.h>
41 #include <ldapconfig.h>
44 #define GROUP_ERRORS 1
45 #define GROUP_REQUEST 2
46 #define GROUP_MEMBERS 3
48 #define ERRORS "errors"
49 #define REQUEST "request"
50 #define MEMBERS "members"
53 char *errorsfrom = NULL;
54 char *mailfrom = NULL;
64 #define E_USERUNKNOWN 1
70 #define E_JOINMEMBERNOEMAIL 7
71 #define E_MEMBERNOEMAIL 8
73 #define E_JOINMEMBERNOFAXNUM 10
74 #define E_MEMBERNOFAXNUM 11
75 #define E_FAXTOEMAILMBR 12
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 char *b_filter[3]; /* filter to apply - name substituted for %s */
91 /* (up to three of them) */
95 { "ou=People, o=University of Michigan, c=US", 0,
96 "uid=%s", "cn=%s", NULL,
97 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
98 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
99 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
100 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
104 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
106 static char *attrs[] = { "objectClass", "title", "postaladdress",
107 "telephoneNumber", "mail", "description",
108 "errorsTo", "rfc822ErrorsTo", "requestsTo",
109 "rfc822RequestsTo", "joinable", "cn", "member",
110 "facsimileTelephoneNumber", NULL };
113 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
114 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
115 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
116 static void send_message(char **to);
117 static void send_errors(Error *err, int nerr);
118 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
119 static void do_ambiguous(FILE *fp, Error *err, int namelen);
120 static int count_values(char **list);
121 static void add_to(char ***list, int *nlist, char **new);
122 static int isgroup(LDAPMessage *e);
123 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
124 static void add_group(char *dn, Group **list, int *nlist);
125 static void unbind_and_exit(int rc);
126 static int group_loop(char *dn);
127 static void send_group(Group *group, int ngroup);
128 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
129 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
130 static char *canonical(char *s);
131 static int connect_to_x500 (void);
132 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
133 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
134 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
138 main ( int argc, char **argv )
144 int numto, ngroups, numerr, nargs;
147 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
149 case 'f': /* who it's from & where errors should go */
150 mailfrom = strdup( optarg );
151 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
152 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
153 sendmailargs[j+1] = mailfrom;
159 case 'h': /* hostname */
160 host = strdup( optarg );
161 hostlen = strlen(host);
164 /* mailer-daemon address - who we should */
165 case 'm': /* say errors come from */
166 errorsfrom = strdup( optarg );
170 syslog( LOG_ALERT, "unknown option" );
175 if ( (myname = strrchr( argv[0], '/' )) == NULL )
176 myname = strdup( argv[0] );
178 myname = strdup( myname + 1 );
179 if (!strcmp(myname, "mail500")) {
181 } else if (!strcmp(myname, "fax500")) {
184 /* I give up, I must be mail500 */
189 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
191 openlog( myname, OPENLOG_OPTIONS );
194 if ( mailfrom == NULL ) {
195 syslog( LOG_ALERT, "required argument -f not present" );
198 if ( errorsfrom == NULL ) {
199 syslog( LOG_ALERT, "required argument -m not present" );
202 if ( host == NULL ) {
203 syslog( LOG_ALERT, "required argument -h not present" );
207 if ( connect_to_x500() != 0 )
216 strcpy( buf, argv[0] );
217 for ( i = 1; i < argc; i++ ) {
219 strcat( buf, argv[i] );
222 syslog( LOG_ALERT, "args: (%s)", buf );
226 add_to( &tolist, &numto, sendmailargs );
228 ngroups = numerr = 0;
231 for ( i = optind; i < argc; i++ ) {
235 for ( j = 0; argv[i][j] != '\0'; j++ ) {
236 if ( argv[i][j] == '.' || argv[i][j] == '_' )
241 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
244 if ( strcmp( s, ERRORS ) == 0 ) {
247 } else if ( strcmp( s, REQUEST ) == 0 ) {
248 type = GROUP_REQUEST;
250 } else if ( strcmp( s, MEMBERS ) == 0 ) {
251 type = GROUP_MEMBERS;
256 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
257 &errlist, &numerr, type );
261 * If we have both errors and successful deliveries to make or if
262 * if there are any groups to deliver to, we basically need to read
263 * the message twice. So, we have to put it in a tmp file.
266 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
271 if ( (fp = tmpfile()) == NULL ) {
272 syslog( LOG_ALERT, "could not open tmp file" );
273 unbind_and_exit( EX_TEMPFAIL );
276 /* copy the message to a temp file */
277 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
278 if ( fputs( buf, fp ) == EOF ) {
279 syslog( LOG_ALERT, "error writing tmpfile" );
280 unbind_and_exit( EX_TEMPFAIL );
284 if ( dup2( fileno( fp ), 0 ) == -1 ) {
285 syslog( LOG_ALERT, "could not dup2 tmpfile" );
286 unbind_and_exit( EX_TEMPFAIL );
292 /* deal with errors */
294 (void) rewind( stdin );
295 send_errors( errlist, numerr );
298 (void) ldap_unbind( ld );
300 /* send to groups with errorsTo */
302 (void) rewind( stdin );
303 send_group( togroups, ngroups );
306 /* send to expanded aliases and groups w/o errorsTo */
307 if ( numto > nargs ) {
308 (void) rewind( stdin );
309 send_message( tolist );
316 connect_to_x500( void )
318 int sizelimit = FAX_MAXAMBIGUOUS;
319 int deref = LDAP_DEREF_ALWAYS;
321 if ( (ld = ldap_open( NULL, 0 )) == NULL ) {
322 syslog( LOG_ALERT, "ldap_open failed" );
326 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
327 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
329 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
330 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
351 LDAPMessage *e, *res;
352 struct timeval timeout;
359 * Look up the name in X.500, add the appropriate addresses found
360 * to the to list, or to the err list in case of error. Groups are
361 * handled by the do_group routine, individuals are handled here.
362 * When looking up name, we follow the bases hierarchy, looking
363 * in base[0] first, then base[1], etc. For each base, there is
364 * a set of search filters to try, in order. If something goes
365 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
366 * If the b_rdnpref flag is set, then we give preference to entries
367 * that matched name because it's their rdn, otherwise not.
370 timeout.tv_sec = FAX_TIMEOUT;
372 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
373 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
374 char *format, *p, *argv[3];
377 for ( argc = 0; argc < 3; argc++ ) {
381 format = strdup( base[b].b_filter[f] );
382 for ( argc = 0, p = format; *p; p++ ) {
385 case 's': /* %s is the name */
389 case 'h': /* %h is the host */
396 "unknown format %c", *p );
404 /* three names ought to do... */
405 sprintf( filter, format, argv[0], argv[1], argv[2] );
409 rc = ldap_search_st( ld, base[b].b_dn,
410 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
413 /* some other trouble - try again later */
414 if ( rc != LDAP_SUCCESS &&
415 rc != LDAP_SIZELIMIT_EXCEEDED ) {
416 syslog( LOG_ALERT, "return %d from X.500", rc );
417 unbind_and_exit( EX_TEMPFAIL );
420 if ( (match = ldap_count_entries( ld, res )) != 0 )
430 /* trouble - try again later */
432 syslog( LOG_ALERT, "error parsing result from X.500" );
433 unbind_and_exit( EX_TEMPFAIL );
436 /* no matches - bounce with user unknown */
438 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
442 /* more than one match - bounce with ambiguous user? */
444 LDAPMessage *next, *tmpres = NULL;
448 /* not giving rdn preference - bounce with ambiguous user */
449 if ( base[b].b_rdnpref == 0 ) {
450 add_error( err, nerr, E_AMBIGUOUS, name, res );
455 * giving rdn preference - see if any entries were matched
456 * because of their rdn. If so, collect them to deal with
457 * later (== 1 we deliver, > 1 we bounce).
460 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
461 next = ldap_next_entry( ld, e );
462 dn = ldap_get_dn( ld, e );
463 xdn = ldap_explode_dn( dn, 1 );
465 /* XXX bad, but how else can we do it? XXX */
466 if ( strcasecmp( xdn[0], name ) == 0 ) {
467 ldap_delete_result_entry( &res, e );
468 ldap_add_result_entry( &tmpres, e );
471 ldap_value_free( xdn );
475 /* nothing matched by rdn - go ahead and bounce */
476 if ( tmpres == NULL ) {
477 add_error( err, nerr, E_AMBIGUOUS, name, res );
480 /* more than one matched by rdn - bounce with rdn matches */
481 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
482 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
486 } else if ( match < 0 ) {
487 syslog( LOG_ALERT, "error parsing result from X.500" );
488 unbind_and_exit( EX_TEMPFAIL );
491 /* otherwise one matched by rdn - send to it */
497 * if we get this far, it means that we found a single match for
498 * name. for a user, we deliver to the mail attribute or bounce
499 * with address and phone if no mail attr. for a group, we
500 * deliver to all members or bounce to rfc822ErrorsTo if no members.
504 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
505 syslog( LOG_ALERT, "error parsing entry from X.500" );
506 unbind_and_exit( EX_TEMPFAIL );
509 dn = ldap_get_dn( ld, e );
511 if ( type == GROUP_ERRORS ) {
512 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
513 do_group_errors( e, dn, to, nto, err, nerr );
515 } else if ( type == GROUP_REQUEST ) {
516 /* sent to group-request - resend to [rfc822]RequestsTo attr */
517 do_group_request( e, dn, to, nto, err, nerr );
519 } else if ( type == GROUP_MEMBERS ) {
520 /* sent to group-members - expand */
521 do_group_members( e, dn, to, nto, togroups, ngroups, err,
524 } else if ( isgroup( e ) ) {
526 * sent to group - resend from [rfc822]ErrorsTo if it's there,
527 * otherwise, expand the group
530 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
536 * sent to user - mail attribute => add it to the to list,
541 if ( (mail = ldap_get_values( ld, e,
542 "facsimileTelephoneNumber" )) != NULL ) {
547 fdn = ldap_get_dn( ld, e );
548 ufn = ldap_explode_dn( fdn, 1 );
549 /* Convert spaces to underscores for rp */
550 for (i = 0; ufn[0][i] != '\0'; i++) {
551 if (ufn[0][i] == ' ') {
555 *mail = faxtotpc(*mail, ufn[0]);
557 add_to(to, nto, mail);
562 ldap_value_free( ufn );
563 ldap_value_free( mail );
566 add_error( err, nerr, E_NOFAXNUM,
571 if ( (mail = ldap_get_values( ld, e, "mail" ))
573 add_to( to, nto, mail );
575 ldap_value_free( mail );
578 add_error( err, nerr, E_NOEMAIL,
603 * If this group has an rfc822ErrorsTo attribute, we need to
604 * arrange for errors involving this group to go there, not
605 * to the sender. Since sendmail only has the concept of a
606 * single sender, we arrange for errors to go to groupname-errors,
607 * which we then handle specially when (if) it comes back to us
608 * by expanding to all the rfc822ErrorsTo addresses. If it has no
609 * rfc822ErrorsTo attribute, we call do_group_members() to expand
613 if ( group_loop( dn ) ) {
617 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
618 add_group( dn, togroups, ngroups );
623 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
643 char **mail, **member, **joinable;
645 LDAPMessage *ee, *res;
646 struct timeval timeout;
649 * if all has gone according to plan, we've already arranged for
650 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
651 * so all we have to do here is arrange to send to the
652 * rfc822Mailbox attribute, the member attribute, and anyone who
653 * has joined the group by setting memberOfGroup equal to the
657 /* add members in the group itself - mail attribute */
658 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
660 * These are only email addresses - impossible
661 * to have a fax number
665 /* XXX How do we want to inform sender that the */
666 /* group they sent to has email-only members? */
669 add_to( to, nto, mail );
673 ldap_value_free( mail );
676 /* add members in the group itself - member attribute */
677 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
678 for ( i = 0; member[i] != NULL; i++ ) {
679 add_member( dn, member[i], to, nto, togroups,
680 ngroups, err, nerr );
683 ldap_value_free( member );
686 /* add members who have joined by setting memberOfGroup */
687 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
688 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
689 ldap_value_free( joinable );
692 ldap_value_free( joinable );
694 sprintf( filter, "(memberOfGroup=%s)", dn );
696 timeout.tv_sec = FAX_TIMEOUT;
699 /* for each subtree to look in... */
701 int sizelimit = FAX_MAXMEMBERS;
702 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
704 for ( i = 0; base[i].b_dn != NULL; i++ ) {
705 /* find entries that have joined this group... */
706 rc = ldap_search_st( ld, base[i].b_dn,
707 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
710 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
711 rc == LDAP_TIMELIMIT_EXCEEDED ) {
713 "group search limit exceeded %d", rc );
714 unbind_and_exit( EX_TEMPFAIL );
717 if ( rc != LDAP_SUCCESS ) {
718 syslog( LOG_ALERT, "group search bad return %d",
720 unbind_and_exit( EX_TEMPFAIL );
723 /* for each entry that has joined... */
724 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
725 ee = ldap_next_entry( ld, ee ) ) {
726 if ( isgroup( ee ) ) {
727 ndn = ldap_get_dn( ld, ee );
729 if ( do_group( e, ndn, to, nto,
730 togroups, ngroups, err, nerr )
733 "group loop (%s) (%s)",
742 /* add them to the to list */
745 if ( (mail = ldap_get_values( ld, ee,
746 "facsimileTelephoneNumber" ))
752 fdn = ldap_get_dn( ld, ee );
753 ufn = ldap_explode_dn( fdn, 1 );
758 for (i = 0; ufn[0][i] != '\0';
760 if (ufn[0][i] == ' ') {
764 *mail = faxtotpc(*mail, ufn[0]);
765 add_to(to, nto, mail);
767 ldap_value_free( mail );
770 ndn = ldap_get_dn( ld, ee );
772 add_error( err, nerr,
773 E_JOINMEMBERNOFAXNUM, ndn,
780 if ( (mail = ldap_get_values( ld, ee,
781 "mail" )) != NULL ) {
782 add_to( to, nto, mail );
784 ldap_value_free( mail );
786 /* else generate a bounce */
788 ndn = ldap_get_dn( ld, ee );
790 add_error( err, nerr,
791 E_JOINMEMBERNOEMAIL, ndn,
803 int sizelimit = FAX_MAXAMBIGUOUS;
804 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
826 LDAPMessage *res, *e;
827 struct timeval timeout;
829 timeout.tv_sec = FAX_TIMEOUT;
831 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
832 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
833 if ( rc == LDAP_NO_SUCH_OBJECT ) {
834 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
838 syslog( LOG_ALERT, "member search bad return %d", rc );
840 unbind_and_exit( EX_TEMPFAIL );
844 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
845 syslog( LOG_ALERT, "member search error parsing entry" );
847 unbind_and_exit( EX_TEMPFAIL );
849 ndn = ldap_get_dn( ld, e );
851 /* allow groups within groups */
852 if ( isgroup( e ) ) {
853 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
855 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
865 if ( (mail = ldap_get_values( ld, e,
866 "facsimileTelephoneNumber" )) != NULL ) {
870 fdn = ldap_get_dn( ld, e );
871 ufn = ldap_explode_dn( fdn, 1 );
872 /* Convert spaces to underscores for rp */
873 for (i = 0; ufn[0][i] != '\0'; i++) {
874 if (ufn[0][i] == ' ') {
878 *mail = faxtotpc(*mail, ufn[0]);
879 add_to(to, nto, mail);
881 ldap_value_free( mail );
884 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
888 /* send to the member's mail attribute */
889 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
890 add_to( to, nto, mail );
892 ldap_value_free( mail );
894 /* else generate a bounce */
896 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
916 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
917 "requestsTo" )) != NULL ) {
918 add_to( to, nto, requeststo );
920 ldap_value_free( requeststo );
922 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
938 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
939 "errorsTo" )) != NULL ) {
940 add_to( to, nto, errorsto );
942 ldap_value_free( errorsto );
944 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
949 send_message( char **to )
953 WAITSTATUSTYPE status;
958 if ( (pid = fork()) != 0 ) {
960 waitpid( pid, (int *) NULL, 0 );
962 wait4( pid, &status, WAIT_FLAGS, 0 );
966 /* to includes sendmailargs */
967 execv( FAX_SENDMAIL, to );
969 syslog( LOG_ALERT, "execv failed" );
976 send_group( Group *group, int ngroup )
983 WAITSTATUSTYPE status;
986 for ( i = 0; i < ngroup; i++ ) {
987 (void) rewind( stdin );
989 iargv[0] = FAX_SENDMAIL;
991 iargv[2] = group[i].g_errorsto;
992 iargv[3] = "-oMrX.500";
999 add_to( &argv, &argc, iargv );
1000 add_to( &argv, &argc, group[i].g_members );
1004 if ( (pid = fork()) != 0 ) {
1006 waitpid( pid, (int *) NULL, 0 );
1008 wait4( pid, &status, WAIT_FLAGS, 0 );
1012 execv( FAX_SENDMAIL, argv );
1014 syslog( LOG_ALERT, "execv failed" );
1016 exit( EX_TEMPFAIL );
1024 send_errors( Error *err, int nerr )
1030 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1032 if ( (fp = popen( buf, "w" )) == NULL ) {
1033 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1037 fprintf( fp, "To: %s\n", mailfrom );
1038 fprintf( fp, "From: %s\n", errorsfrom );
1039 fprintf( fp, "Subject: undeliverable mail\n" );
1040 fprintf( fp, "\n" );
1041 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1042 for ( i = 0; i < nerr; i++ ) {
1043 namelen = strlen( err[i].e_addr );
1044 fprintf( fp, "\n" );
1046 switch ( err[i].e_code ) {
1048 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1052 fprintf( fp, "%s: Group member does not exist\n",
1057 fprintf( fp, "%s: Group exists but has no request address\n",
1062 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1067 do_ambiguous( fp, &err[i], namelen );
1071 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1074 case E_MEMBERNOEMAIL:
1075 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1079 case E_JOINMEMBERNOEMAIL:
1080 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1085 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1088 case E_MEMBERNOFAXNUM:
1089 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1093 case E_JOINMEMBERNOFAXNUM:
1094 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1099 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1100 unbind_and_exit( EX_TEMPFAIL );
1105 fprintf( fp, "\n------- The original message sent:\n\n" );
1107 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1111 if ( pclose( fp ) == -1 ) {
1112 syslog( LOG_ALERT, "pclose failed" );
1113 unbind_and_exit( EX_TEMPFAIL );
1121 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1127 if (errtype == E_NOFAXNUM) {
1128 fprintf(fp, "%s: User has no facsimile number registered.\n",
1130 } else if (errtype == E_NOEMAIL) {
1131 fprintf(fp, "%s: User has no email address registered.\n",
1134 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1135 namelen, " ", err->e_addr );
1138 dn = ldap_get_dn( ld, err->e_msg );
1139 ufn = ldap_explode_dn( dn, 1 );
1140 rdn = strdup( ufn[0] );
1141 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1142 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1144 for ( i = 0; vals[i]; i++ ) {
1145 last = strlen( vals[i] ) - 1;
1146 if ( isdigit( vals[i][last] ) ) {
1147 rdn = strdup( vals[i] );
1152 ldap_value_free( vals );
1155 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1158 ldap_value_free( ufn );
1160 /* titles or descriptions */
1161 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1162 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1164 fprintf( fp, "%*s No title or description registered\n",
1167 for ( i = 0; vals[i] != NULL; i++ ) {
1168 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1171 ldap_value_free( vals );
1174 /* postal address */
1175 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1177 fprintf( fp, "%*s No postal address registered\n", namelen,
1180 fprintf( fp, "%*s ", namelen, " " );
1181 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1182 if ( vals[0][i] == '$' ) {
1183 fprintf( fp, "\n%*s ", namelen, " " );
1184 while ( isspace( vals[0][i+1] ) )
1187 fprintf( fp, "%c", vals[0][i] );
1190 fprintf( fp, "\n" );
1192 ldap_value_free( vals );
1195 /* telephone number */
1196 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1198 fprintf( fp, "%*s No phone number registered\n", namelen,
1201 for ( i = 0; vals[i] != NULL; i++ ) {
1202 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1205 ldap_value_free( vals );
1211 do_ambiguous( FILE *fp, Error *err, int namelen )
1218 i = ldap_result2error( ld, err->e_msg, 0 );
1220 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1221 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1222 ldap_count_entries( ld, err->e_msg ) );
1224 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1225 e = ldap_next_entry( ld, e ) ) {
1226 dn = ldap_get_dn( ld, e );
1227 ufn = ldap_explode_dn( dn, 1 );
1228 rdn = strdup( ufn[0] );
1229 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1230 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1231 for ( i = 0; vals[i]; i++ ) {
1232 last = strlen( vals[i] ) - 1;
1233 if ( isdigit( vals[i][last] ) ) {
1234 rdn = strdup( vals[i] );
1239 ldap_value_free( vals );
1243 if ( isgroup( e ) ) {
1244 vals = ldap_get_values( ld, e, "description" );
1246 vals = ldap_get_values( ld, e, "title" );
1249 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1250 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1251 fprintf( fp, " %s\n", vals[i] );
1256 ldap_value_free( ufn );
1258 ldap_value_free( vals );
1263 count_values( char **list )
1267 for ( i = 0; list && list[i] != NULL; i++ )
1274 add_to( char ***list, int *nlist, char **new )
1276 int i, nnew, oldnlist;
1278 nnew = count_values( new );
1281 if ( *list == NULL || *nlist == 0 ) {
1282 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1285 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1286 nnew * sizeof(char *) + sizeof(char *) );
1290 for ( i = 0; i < nnew; i++ ) {
1291 (*list)[i + oldnlist] = strdup( new[i] );
1293 (*list)[*nlist] = NULL;
1299 isgroup( LDAPMessage *e )
1304 oclist = ldap_get_values( ld, e, "objectClass" );
1306 for ( i = 0; oclist[i] != NULL; i++ ) {
1307 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1308 ldap_value_free( oclist );
1312 ldap_value_free( oclist );
1318 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1321 *err = (Error *) malloc( sizeof(Error) );
1323 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1326 (*err)[*nerr].e_code = code;
1327 (*err)[*nerr].e_addr = strdup( addr );
1328 (*err)[*nerr].e_msg = msg;
1335 add_group( char *dn, Group **list, int *nlist )
1340 for ( i = 0; i < *nlist; i++ ) {
1341 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1342 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1347 ufn = ldap_explode_dn( dn, 1 );
1348 namelen = strlen( ufn[0] );
1350 if ( *nlist == 0 ) {
1351 *list = (Group *) malloc( sizeof(Group) );
1353 *list = (Group *) realloc( *list, (*nlist + 1) *
1357 /* send errors to groupname-errors@host */
1358 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1360 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1361 (void) canonical( (*list)[*nlist].g_errorsto );
1363 /* send to groupname-members@host - make it a list for send_group */
1364 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1365 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1366 sizeof(MEMBERS) + hostlen + 2 );
1367 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1369 (void) canonical( (*list)[*nlist].g_members[0] );
1370 (*list)[*nlist].g_members[1] = NULL;
1372 /* save the group's dn so we can check for loops above */
1373 (*list)[*nlist].g_dn = strdup( dn );
1377 ldap_value_free( ufn );
1383 unbind_and_exit( int rc )
1387 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1388 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1394 canonical( char *s )
1398 for ( ; *s != '\0'; s++ ) {
1407 group_loop( char *dn )
1410 static char **groups;
1413 for ( i = 0; i < ngroups; i++ ) {
1414 if ( strcmp( dn, groups[i] ) == 0 )
1419 groups = (char **) malloc( sizeof(char *) );
1421 groups = (char **) realloc( groups,
1422 (ngroups + 1) * sizeof(char *) );
1424 groups[ngroups++] = strdup( dn );
1430 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1434 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1435 ldap_value_free( attr );
1439 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1440 ldap_value_free( attr );
1448 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1450 LDAPMessage *ee, *res;
1451 char **vals, **dnlist, **mail;
1453 struct timeval timeout;
1455 vals = ldap_get_values( ld, e, attr1 );
1456 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1459 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1460 timeout.tv_sec = FAX_TIMEOUT;
1461 timeout.tv_usec = 0;
1463 for ( i = 0; dnlist[i] != NULL; i++ ) {
1464 if ( (rc = ldap_search_st( ld, dnlist[i],
1465 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1466 &timeout, &res )) != LDAP_SUCCESS ) {
1467 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1468 unbind_and_exit( EX_TEMPFAIL );
1471 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1477 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1478 syslog( LOG_ALERT, "error parsing x500 entry" );
1482 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1484 add_to( &vals, &nto, mail );
1486 ldap_value_free( mail );
1489 ldap_msgfree( res );