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>
20 #include <ac/syslog.h>
23 #include <ac/unistd.h>
26 #ifdef HAVE_SYS_PARAM_H
27 #include <sys/param.h>
29 #ifdef HAVE_SYS_RESOURCE_H
30 #include <sys/resource.h>
40 #include <ldapconfig.h>
43 #define GROUP_ERRORS 1
44 #define GROUP_REQUEST 2
45 #define GROUP_MEMBERS 3
47 #define ERRORS "errors"
48 #define REQUEST "request"
49 #define MEMBERS "members"
52 char *errorsfrom = NULL;
53 char *mailfrom = NULL;
63 #define E_USERUNKNOWN 1
69 #define E_JOINMEMBERNOEMAIL 7
70 #define E_MEMBERNOEMAIL 8
72 #define E_JOINMEMBERNOFAXNUM 10
73 #define E_MEMBERNOFAXNUM 11
74 #define E_FAXTOEMAILMBR 12
80 typedef struct groupto {
86 typedef struct baseinfo {
87 char *b_dn; /* dn to start searching at */
88 char b_rdnpref; /* give rdn's preference when searching? */
89 char *b_filter[3]; /* filter to apply - name substituted for %s */
90 /* (up to three of them) */
94 { "ou=People, o=University of Michigan, c=US", 0,
95 "uid=%s", "cn=%s", NULL,
96 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
97 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
98 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
99 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
103 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
105 static char *attrs[] = { "objectClass", "title", "postaladdress",
106 "telephoneNumber", "mail", "description",
107 "errorsTo", "rfc822ErrorsTo", "requestsTo",
108 "rfc822RequestsTo", "joinable", "cn", "member",
109 "facsimileTelephoneNumber", NULL };
112 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
113 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
114 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
115 static void send_message(char **to);
116 static void send_errors(Error *err, int nerr);
117 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
118 static void do_ambiguous(FILE *fp, Error *err, int namelen);
119 static int count_values(char **list);
120 static void add_to(char ***list, int *nlist, char **new);
121 static int isgroup(LDAPMessage *e);
122 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
123 static void add_group(char *dn, Group **list, int *nlist);
124 static void unbind_and_exit(int rc);
125 static int group_loop(char *dn);
126 static void send_group(Group *group, int ngroup);
127 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
128 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
129 static char *canonical(char *s);
130 static int connect_to_x500 (void);
131 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
132 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
133 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
137 main ( int argc, char **argv )
143 int numto, ngroups, numerr, nargs;
146 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
148 case 'f': /* who it's from & where errors should go */
149 mailfrom = strdup( optarg );
150 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
151 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
152 sendmailargs[j+1] = mailfrom;
158 case 'h': /* hostname */
159 host = strdup( optarg );
160 hostlen = strlen(host);
163 /* mailer-daemon address - who we should */
164 case 'm': /* say errors come from */
165 errorsfrom = strdup( optarg );
169 syslog( LOG_ALERT, "unknown option" );
174 if ( (myname = strrchr( argv[0], '/' )) == NULL )
175 myname = strdup( argv[0] );
177 myname = strdup( myname + 1 );
178 if (!strcmp(myname, "mail500")) {
180 } else if (!strcmp(myname, "fax500")) {
183 /* I give up, I must be mail500 */
188 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
190 openlog( myname, OPENLOG_OPTIONS );
193 if ( mailfrom == NULL ) {
194 syslog( LOG_ALERT, "required argument -f not present" );
197 if ( errorsfrom == NULL ) {
198 syslog( LOG_ALERT, "required argument -m not present" );
201 if ( host == NULL ) {
202 syslog( LOG_ALERT, "required argument -h not present" );
206 if ( connect_to_x500() != 0 )
215 strcpy( buf, argv[0] );
216 for ( i = 1; i < argc; i++ ) {
218 strcat( buf, argv[i] );
221 syslog( LOG_ALERT, "args: (%s)", buf );
225 add_to( &tolist, &numto, sendmailargs );
227 ngroups = numerr = 0;
230 for ( i = optind; i < argc; i++ ) {
234 for ( j = 0; argv[i][j] != '\0'; j++ ) {
235 if ( argv[i][j] == '.' || argv[i][j] == '_' )
240 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
243 if ( strcmp( s, ERRORS ) == 0 ) {
246 } else if ( strcmp( s, REQUEST ) == 0 ) {
247 type = GROUP_REQUEST;
249 } else if ( strcmp( s, MEMBERS ) == 0 ) {
250 type = GROUP_MEMBERS;
255 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
256 &errlist, &numerr, type );
260 * If we have both errors and successful deliveries to make or if
261 * if there are any groups to deliver to, we basically need to read
262 * the message twice. So, we have to put it in a tmp file.
265 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
270 if ( (fp = tmpfile()) == NULL ) {
271 syslog( LOG_ALERT, "could not open tmp file" );
272 unbind_and_exit( EX_TEMPFAIL );
275 /* copy the message to a temp file */
276 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
277 if ( fputs( buf, fp ) == EOF ) {
278 syslog( LOG_ALERT, "error writing tmpfile" );
279 unbind_and_exit( EX_TEMPFAIL );
283 if ( dup2( fileno( fp ), 0 ) == -1 ) {
284 syslog( LOG_ALERT, "could not dup2 tmpfile" );
285 unbind_and_exit( EX_TEMPFAIL );
291 /* deal with errors */
293 (void) rewind( stdin );
294 send_errors( errlist, numerr );
297 (void) ldap_unbind( ld );
299 /* send to groups with errorsTo */
301 (void) rewind( stdin );
302 send_group( togroups, ngroups );
305 /* send to expanded aliases and groups w/o errorsTo */
306 if ( numto > nargs ) {
307 (void) rewind( stdin );
308 send_message( tolist );
315 connect_to_x500( void )
317 int sizelimit = FAX_MAXAMBIGUOUS;
318 int deref = LDAP_DEREF_ALWAYS;
320 if ( (ld = ldap_open( NULL, 0 )) == NULL ) {
321 syslog( LOG_ALERT, "ldap_open failed" );
325 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
326 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
328 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
329 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
350 LDAPMessage *e, *res;
351 struct timeval timeout;
358 * Look up the name in X.500, add the appropriate addresses found
359 * to the to list, or to the err list in case of error. Groups are
360 * handled by the do_group routine, individuals are handled here.
361 * When looking up name, we follow the bases hierarchy, looking
362 * in base[0] first, then base[1], etc. For each base, there is
363 * a set of search filters to try, in order. If something goes
364 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
365 * If the b_rdnpref flag is set, then we give preference to entries
366 * that matched name because it's their rdn, otherwise not.
369 timeout.tv_sec = FAX_TIMEOUT;
371 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
372 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
373 char *format, *p, *argv[3];
376 for ( argc = 0; argc < 3; argc++ ) {
380 format = strdup( base[b].b_filter[f] );
381 for ( argc = 0, p = format; *p; p++ ) {
384 case 's': /* %s is the name */
388 case 'h': /* %h is the host */
395 "unknown format %c", *p );
403 /* three names ought to do... */
404 sprintf( filter, format, argv[0], argv[1], argv[2] );
408 rc = ldap_search_st( ld, base[b].b_dn,
409 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
412 /* some other trouble - try again later */
413 if ( rc != LDAP_SUCCESS &&
414 rc != LDAP_SIZELIMIT_EXCEEDED ) {
415 syslog( LOG_ALERT, "return %d from X.500", rc );
416 unbind_and_exit( EX_TEMPFAIL );
419 if ( (match = ldap_count_entries( ld, res )) != 0 )
429 /* trouble - try again later */
431 syslog( LOG_ALERT, "error parsing result from X.500" );
432 unbind_and_exit( EX_TEMPFAIL );
435 /* no matches - bounce with user unknown */
437 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
441 /* more than one match - bounce with ambiguous user? */
443 LDAPMessage *next, *tmpres = NULL;
447 /* not giving rdn preference - bounce with ambiguous user */
448 if ( base[b].b_rdnpref == 0 ) {
449 add_error( err, nerr, E_AMBIGUOUS, name, res );
454 * giving rdn preference - see if any entries were matched
455 * because of their rdn. If so, collect them to deal with
456 * later (== 1 we deliver, > 1 we bounce).
459 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
460 next = ldap_next_entry( ld, e );
461 dn = ldap_get_dn( ld, e );
462 xdn = ldap_explode_dn( dn, 1 );
464 /* XXX bad, but how else can we do it? XXX */
465 if ( strcasecmp( xdn[0], name ) == 0 ) {
466 ldap_delete_result_entry( &res, e );
467 ldap_add_result_entry( &tmpres, e );
470 ldap_value_free( xdn );
474 /* nothing matched by rdn - go ahead and bounce */
475 if ( tmpres == NULL ) {
476 add_error( err, nerr, E_AMBIGUOUS, name, res );
479 /* more than one matched by rdn - bounce with rdn matches */
480 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
481 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
485 } else if ( match < 0 ) {
486 syslog( LOG_ALERT, "error parsing result from X.500" );
487 unbind_and_exit( EX_TEMPFAIL );
490 /* otherwise one matched by rdn - send to it */
496 * if we get this far, it means that we found a single match for
497 * name. for a user, we deliver to the mail attribute or bounce
498 * with address and phone if no mail attr. for a group, we
499 * deliver to all members or bounce to rfc822ErrorsTo if no members.
503 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
504 syslog( LOG_ALERT, "error parsing entry from X.500" );
505 unbind_and_exit( EX_TEMPFAIL );
508 dn = ldap_get_dn( ld, e );
510 if ( type == GROUP_ERRORS ) {
511 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
512 do_group_errors( e, dn, to, nto, err, nerr );
514 } else if ( type == GROUP_REQUEST ) {
515 /* sent to group-request - resend to [rfc822]RequestsTo attr */
516 do_group_request( e, dn, to, nto, err, nerr );
518 } else if ( type == GROUP_MEMBERS ) {
519 /* sent to group-members - expand */
520 do_group_members( e, dn, to, nto, togroups, ngroups, err,
523 } else if ( isgroup( e ) ) {
525 * sent to group - resend from [rfc822]ErrorsTo if it's there,
526 * otherwise, expand the group
529 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
535 * sent to user - mail attribute => add it to the to list,
540 if ( (mail = ldap_get_values( ld, e,
541 "facsimileTelephoneNumber" )) != NULL ) {
546 fdn = ldap_get_dn( ld, e );
547 ufn = ldap_explode_dn( fdn, 1 );
548 /* Convert spaces to underscores for rp */
549 for (i = 0; ufn[0][i] != '\0'; i++) {
550 if (ufn[0][i] == ' ') {
554 *mail = faxtotpc(*mail, ufn[0]);
556 add_to(to, nto, mail);
561 ldap_value_free( ufn );
562 ldap_value_free( mail );
565 add_error( err, nerr, E_NOFAXNUM,
570 if ( (mail = ldap_get_values( ld, e, "mail" ))
572 add_to( to, nto, mail );
574 ldap_value_free( mail );
577 add_error( err, nerr, E_NOEMAIL,
602 * If this group has an rfc822ErrorsTo attribute, we need to
603 * arrange for errors involving this group to go there, not
604 * to the sender. Since sendmail only has the concept of a
605 * single sender, we arrange for errors to go to groupname-errors,
606 * which we then handle specially when (if) it comes back to us
607 * by expanding to all the rfc822ErrorsTo addresses. If it has no
608 * rfc822ErrorsTo attribute, we call do_group_members() to expand
612 if ( group_loop( dn ) ) {
616 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
617 add_group( dn, togroups, ngroups );
622 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
642 char **mail, **member, **joinable;
644 LDAPMessage *ee, *res;
645 struct timeval timeout;
648 * if all has gone according to plan, we've already arranged for
649 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
650 * so all we have to do here is arrange to send to the
651 * rfc822Mailbox attribute, the member attribute, and anyone who
652 * has joined the group by setting memberOfGroup equal to the
656 /* add members in the group itself - mail attribute */
657 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
659 * These are only email addresses - impossible
660 * to have a fax number
664 /* XXX How do we want to inform sender that the */
665 /* group they sent to has email-only members? */
668 add_to( to, nto, mail );
672 ldap_value_free( mail );
675 /* add members in the group itself - member attribute */
676 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
677 for ( i = 0; member[i] != NULL; i++ ) {
678 add_member( dn, member[i], to, nto, togroups,
679 ngroups, err, nerr );
682 ldap_value_free( member );
685 /* add members who have joined by setting memberOfGroup */
686 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
687 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
688 ldap_value_free( joinable );
691 ldap_value_free( joinable );
693 sprintf( filter, "(memberOfGroup=%s)", dn );
695 timeout.tv_sec = FAX_TIMEOUT;
698 /* for each subtree to look in... */
700 int sizelimit = FAX_MAXMEMBERS;
701 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
703 for ( i = 0; base[i].b_dn != NULL; i++ ) {
704 /* find entries that have joined this group... */
705 rc = ldap_search_st( ld, base[i].b_dn,
706 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
709 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
710 rc == LDAP_TIMELIMIT_EXCEEDED ) {
712 "group search limit exceeded %d", rc );
713 unbind_and_exit( EX_TEMPFAIL );
716 if ( rc != LDAP_SUCCESS ) {
717 syslog( LOG_ALERT, "group search bad return %d",
719 unbind_and_exit( EX_TEMPFAIL );
722 /* for each entry that has joined... */
723 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
724 ee = ldap_next_entry( ld, ee ) ) {
725 if ( isgroup( ee ) ) {
726 ndn = ldap_get_dn( ld, ee );
728 if ( do_group( e, ndn, to, nto,
729 togroups, ngroups, err, nerr )
732 "group loop (%s) (%s)",
741 /* add them to the to list */
744 if ( (mail = ldap_get_values( ld, ee,
745 "facsimileTelephoneNumber" ))
751 fdn = ldap_get_dn( ld, ee );
752 ufn = ldap_explode_dn( fdn, 1 );
757 for (i = 0; ufn[0][i] != '\0';
759 if (ufn[0][i] == ' ') {
763 *mail = faxtotpc(*mail, ufn[0]);
764 add_to(to, nto, mail);
766 ldap_value_free( mail );
769 ndn = ldap_get_dn( ld, ee );
771 add_error( err, nerr,
772 E_JOINMEMBERNOFAXNUM, ndn,
779 if ( (mail = ldap_get_values( ld, ee,
780 "mail" )) != NULL ) {
781 add_to( to, nto, mail );
783 ldap_value_free( mail );
785 /* else generate a bounce */
787 ndn = ldap_get_dn( ld, ee );
789 add_error( err, nerr,
790 E_JOINMEMBERNOEMAIL, ndn,
802 int sizelimit = FAX_MAXAMBIGUOUS;
803 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
825 LDAPMessage *res, *e;
826 struct timeval timeout;
828 timeout.tv_sec = FAX_TIMEOUT;
830 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
831 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
832 if ( rc == LDAP_NO_SUCH_OBJECT ) {
833 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
837 syslog( LOG_ALERT, "member search bad return %d", rc );
839 unbind_and_exit( EX_TEMPFAIL );
843 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
844 syslog( LOG_ALERT, "member search error parsing entry" );
846 unbind_and_exit( EX_TEMPFAIL );
848 ndn = ldap_get_dn( ld, e );
850 /* allow groups within groups */
851 if ( isgroup( e ) ) {
852 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
854 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
864 if ( (mail = ldap_get_values( ld, e,
865 "facsimileTelephoneNumber" )) != NULL ) {
869 fdn = ldap_get_dn( ld, e );
870 ufn = ldap_explode_dn( fdn, 1 );
871 /* Convert spaces to underscores for rp */
872 for (i = 0; ufn[0][i] != '\0'; i++) {
873 if (ufn[0][i] == ' ') {
877 *mail = faxtotpc(*mail, ufn[0]);
878 add_to(to, nto, mail);
880 ldap_value_free( mail );
883 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
887 /* send to the member's mail attribute */
888 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
889 add_to( to, nto, mail );
891 ldap_value_free( mail );
893 /* else generate a bounce */
895 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
915 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
916 "requestsTo" )) != NULL ) {
917 add_to( to, nto, requeststo );
919 ldap_value_free( requeststo );
921 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
937 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
938 "errorsTo" )) != NULL ) {
939 add_to( to, nto, errorsto );
941 ldap_value_free( errorsto );
943 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
948 send_message( char **to )
952 WAITSTATUSTYPE status;
957 if ( (pid = fork()) != 0 ) {
959 waitpid( pid, (int *) NULL, 0 );
961 wait4( pid, &status, WAIT_FLAGS, 0 );
965 /* to includes sendmailargs */
966 execv( FAX_SENDMAIL, to );
968 syslog( LOG_ALERT, "execv failed" );
975 send_group( Group *group, int ngroup )
982 WAITSTATUSTYPE status;
985 for ( i = 0; i < ngroup; i++ ) {
986 (void) rewind( stdin );
988 iargv[0] = FAX_SENDMAIL;
990 iargv[2] = group[i].g_errorsto;
991 iargv[3] = "-oMrX.500";
998 add_to( &argv, &argc, iargv );
999 add_to( &argv, &argc, group[i].g_members );
1003 if ( (pid = fork()) != 0 ) {
1005 waitpid( pid, (int *) NULL, 0 );
1007 wait4( pid, &status, WAIT_FLAGS, 0 );
1011 execv( FAX_SENDMAIL, argv );
1013 syslog( LOG_ALERT, "execv failed" );
1015 exit( EX_TEMPFAIL );
1023 send_errors( Error *err, int nerr )
1029 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1031 if ( (fp = popen( buf, "w" )) == NULL ) {
1032 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1036 fprintf( fp, "To: %s\n", mailfrom );
1037 fprintf( fp, "From: %s\n", errorsfrom );
1038 fprintf( fp, "Subject: undeliverable mail\n" );
1039 fprintf( fp, "\n" );
1040 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1041 for ( i = 0; i < nerr; i++ ) {
1042 namelen = strlen( err[i].e_addr );
1043 fprintf( fp, "\n" );
1045 switch ( err[i].e_code ) {
1047 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1051 fprintf( fp, "%s: Group member does not exist\n",
1056 fprintf( fp, "%s: Group exists but has no request address\n",
1061 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1066 do_ambiguous( fp, &err[i], namelen );
1070 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1073 case E_MEMBERNOEMAIL:
1074 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1078 case E_JOINMEMBERNOEMAIL:
1079 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1084 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1087 case E_MEMBERNOFAXNUM:
1088 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1092 case E_JOINMEMBERNOFAXNUM:
1093 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1098 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1099 unbind_and_exit( EX_TEMPFAIL );
1104 fprintf( fp, "\n------- The original message sent:\n\n" );
1106 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1110 if ( pclose( fp ) == -1 ) {
1111 syslog( LOG_ALERT, "pclose failed" );
1112 unbind_and_exit( EX_TEMPFAIL );
1120 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1126 if (errtype == E_NOFAXNUM) {
1127 fprintf(fp, "%s: User has no facsimile number registered.\n",
1129 } else if (errtype == E_NOEMAIL) {
1130 fprintf(fp, "%s: User has no email address registered.\n",
1133 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1134 namelen, " ", err->e_addr );
1137 dn = ldap_get_dn( ld, err->e_msg );
1138 ufn = ldap_explode_dn( dn, 1 );
1139 rdn = strdup( ufn[0] );
1140 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1141 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1143 for ( i = 0; vals[i]; i++ ) {
1144 last = strlen( vals[i] ) - 1;
1145 if ( isdigit( vals[i][last] ) ) {
1146 rdn = strdup( vals[i] );
1151 ldap_value_free( vals );
1154 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1157 ldap_value_free( ufn );
1159 /* titles or descriptions */
1160 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1161 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1163 fprintf( fp, "%*s No title or description registered\n",
1166 for ( i = 0; vals[i] != NULL; i++ ) {
1167 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1170 ldap_value_free( vals );
1173 /* postal address */
1174 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1176 fprintf( fp, "%*s No postal address registered\n", namelen,
1179 fprintf( fp, "%*s ", namelen, " " );
1180 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1181 if ( vals[0][i] == '$' ) {
1182 fprintf( fp, "\n%*s ", namelen, " " );
1183 while ( isspace( vals[0][i+1] ) )
1186 fprintf( fp, "%c", vals[0][i] );
1189 fprintf( fp, "\n" );
1191 ldap_value_free( vals );
1194 /* telephone number */
1195 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1197 fprintf( fp, "%*s No phone number registered\n", namelen,
1200 for ( i = 0; vals[i] != NULL; i++ ) {
1201 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1204 ldap_value_free( vals );
1210 do_ambiguous( FILE *fp, Error *err, int namelen )
1217 i = ldap_result2error( ld, err->e_msg, 0 );
1219 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1220 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1221 ldap_count_entries( ld, err->e_msg ) );
1223 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1224 e = ldap_next_entry( ld, e ) ) {
1225 dn = ldap_get_dn( ld, e );
1226 ufn = ldap_explode_dn( dn, 1 );
1227 rdn = strdup( ufn[0] );
1228 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1229 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1230 for ( i = 0; vals[i]; i++ ) {
1231 last = strlen( vals[i] ) - 1;
1232 if ( isdigit( vals[i][last] ) ) {
1233 rdn = strdup( vals[i] );
1238 ldap_value_free( vals );
1242 if ( isgroup( e ) ) {
1243 vals = ldap_get_values( ld, e, "description" );
1245 vals = ldap_get_values( ld, e, "title" );
1248 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1249 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1250 fprintf( fp, " %s\n", vals[i] );
1255 ldap_value_free( ufn );
1257 ldap_value_free( vals );
1262 count_values( char **list )
1266 for ( i = 0; list && list[i] != NULL; i++ )
1273 add_to( char ***list, int *nlist, char **new )
1275 int i, nnew, oldnlist;
1277 nnew = count_values( new );
1280 if ( *list == NULL || *nlist == 0 ) {
1281 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1284 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1285 nnew * sizeof(char *) + sizeof(char *) );
1289 for ( i = 0; i < nnew; i++ ) {
1290 (*list)[i + oldnlist] = strdup( new[i] );
1292 (*list)[*nlist] = NULL;
1298 isgroup( LDAPMessage *e )
1303 oclist = ldap_get_values( ld, e, "objectClass" );
1305 for ( i = 0; oclist[i] != NULL; i++ ) {
1306 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1307 ldap_value_free( oclist );
1311 ldap_value_free( oclist );
1317 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1320 *err = (Error *) malloc( sizeof(Error) );
1322 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1325 (*err)[*nerr].e_code = code;
1326 (*err)[*nerr].e_addr = strdup( addr );
1327 (*err)[*nerr].e_msg = msg;
1334 add_group( char *dn, Group **list, int *nlist )
1339 for ( i = 0; i < *nlist; i++ ) {
1340 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1341 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1346 ufn = ldap_explode_dn( dn, 1 );
1347 namelen = strlen( ufn[0] );
1349 if ( *nlist == 0 ) {
1350 *list = (Group *) malloc( sizeof(Group) );
1352 *list = (Group *) realloc( *list, (*nlist + 1) *
1356 /* send errors to groupname-errors@host */
1357 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1359 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1360 (void) canonical( (*list)[*nlist].g_errorsto );
1362 /* send to groupname-members@host - make it a list for send_group */
1363 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1364 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1365 sizeof(MEMBERS) + hostlen + 2 );
1366 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1368 (void) canonical( (*list)[*nlist].g_members[0] );
1369 (*list)[*nlist].g_members[1] = NULL;
1371 /* save the group's dn so we can check for loops above */
1372 (*list)[*nlist].g_dn = strdup( dn );
1376 ldap_value_free( ufn );
1382 unbind_and_exit( int rc )
1386 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1387 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1393 canonical( char *s )
1397 for ( ; *s != '\0'; s++ ) {
1406 group_loop( char *dn )
1409 static char **groups;
1412 for ( i = 0; i < ngroups; i++ ) {
1413 if ( strcmp( dn, groups[i] ) == 0 )
1418 groups = (char **) malloc( sizeof(char *) );
1420 groups = (char **) realloc( groups,
1421 (ngroups + 1) * sizeof(char *) );
1423 groups[ngroups++] = strdup( dn );
1429 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1433 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1434 ldap_value_free( attr );
1438 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1439 ldap_value_free( attr );
1447 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1449 LDAPMessage *ee, *res;
1450 char **vals, **dnlist, **mail;
1452 struct timeval timeout;
1454 vals = ldap_get_values( ld, e, attr1 );
1455 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1458 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1459 timeout.tv_sec = FAX_TIMEOUT;
1460 timeout.tv_usec = 0;
1462 for ( i = 0; dnlist[i] != NULL; i++ ) {
1463 if ( (rc = ldap_search_st( ld, dnlist[i],
1464 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1465 &timeout, &res )) != LDAP_SUCCESS ) {
1466 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1467 unbind_and_exit( EX_TEMPFAIL );
1470 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1476 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1477 syslog( LOG_ALERT, "error parsing x500 entry" );
1481 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1483 add_to( &vals, &nto, mail );
1485 ldap_value_free( mail );
1488 ldap_msgfree( res );