3 * Copyright (c) 1990 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
18 #include <ac/stdlib.h>
23 #include <ac/signal.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
26 #include <ac/sysexits.h>
27 #include <ac/syslog.h>
29 #include <ac/unistd.h>
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
39 #include "ldap_defaults.h"
42 #define GROUP_ERRORS 1
43 #define GROUP_REQUEST 2
44 #define GROUP_MEMBERS 3
46 #define ERRORS "errors"
47 #define REQUEST "request"
48 #define MEMBERS "members"
51 char *errorsfrom = NULL;
52 char *mailfrom = NULL;
62 #define E_USERUNKNOWN 1
68 #define E_JOINMEMBERNOEMAIL 7
69 #define E_MEMBERNOEMAIL 8
71 #define E_JOINMEMBERNOFAXNUM 10
72 #define E_MEMBERNOFAXNUM 11
73 #define E_FAXTOEMAILMBR 12
79 typedef struct groupto {
85 typedef struct baseinfo {
86 char *b_dn; /* dn to start searching at */
87 char b_rdnpref; /* give rdn's preference when searching? */
88 char *b_filter[3]; /* filter to apply - name substituted for %s */
89 /* (up to three of them) */
93 { "ou=People, o=University of Michigan, c=US", 0,
94 "uid=%s", "cn=%s", NULL,
95 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
96 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
97 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
98 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
102 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
104 static char *attrs[] = { "objectClass", "title", "postaladdress",
105 "telephoneNumber", "mail", "description",
106 "errorsTo", "rfc822ErrorsTo", "requestsTo",
107 "rfc822RequestsTo", "joinable", "cn", "member",
108 "facsimileTelephoneNumber", NULL };
111 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
112 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
113 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
114 static void send_message(char **to);
115 static void send_errors(Error *err, int nerr);
116 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
117 static void do_ambiguous(FILE *fp, Error *err, int namelen);
118 static int count_values(char **list);
119 static void add_to(char ***list, int *nlist, char **new);
120 static int isgroup(LDAPMessage *e);
121 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
122 static void add_group(char *dn, Group **list, int *nlist);
123 static void unbind_and_exit(int rc) LDAP_GCCATTR((noreturn));
124 static int group_loop(char *dn);
125 static void send_group(Group *group, int ngroup);
126 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
127 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
128 static char *canonical(char *s);
129 static int connect_to_x500 (void);
130 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
131 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
132 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
136 main ( int argc, char **argv )
142 int numto, ngroups, numerr, nargs;
145 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
147 case 'f': /* who it's from & where errors should go */
148 mailfrom = strdup( optarg );
149 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
150 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
151 sendmailargs[j+1] = mailfrom;
157 case 'h': /* hostname */
158 host = strdup( optarg );
159 hostlen = strlen(host);
162 /* mailer-daemon address - who we should */
163 case 'm': /* say errors come from */
164 errorsfrom = strdup( optarg );
168 syslog( LOG_ALERT, "unknown option" );
173 if ( (myname = strrchr( argv[0], '/' )) == NULL )
174 myname = strdup( argv[0] );
176 myname = strdup( myname + 1 );
177 if (!strcmp(myname, "mail500")) {
179 } else if (!strcmp(myname, "fax500")) {
182 /* I give up, I must be mail500 */
187 (void) SIGNAL( SIGPIPE, SIG_IGN );
191 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
193 openlog( myname, OPENLOG_OPTIONS );
196 if ( mailfrom == NULL ) {
197 syslog( LOG_ALERT, "required argument -f not present" );
200 if ( errorsfrom == NULL ) {
201 syslog( LOG_ALERT, "required argument -m not present" );
204 if ( host == NULL ) {
205 syslog( LOG_ALERT, "required argument -h not present" );
209 if ( connect_to_x500() != 0 )
218 strcpy( buf, argv[0] );
219 for ( i = 1; i < argc; i++ ) {
221 strcat( buf, argv[i] );
224 syslog( LOG_ALERT, "args: (%s)", buf );
228 add_to( &tolist, &numto, sendmailargs );
230 ngroups = numerr = 0;
233 for ( i = optind; i < argc; i++ ) {
237 for ( j = 0; argv[i][j] != '\0'; j++ ) {
238 if ( argv[i][j] == '.' || argv[i][j] == '_' )
243 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
246 if ( strcmp( s, ERRORS ) == 0 ) {
249 } else if ( strcmp( s, REQUEST ) == 0 ) {
250 type = GROUP_REQUEST;
252 } else if ( strcmp( s, MEMBERS ) == 0 ) {
253 type = GROUP_MEMBERS;
258 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
259 &errlist, &numerr, type );
263 * If we have both errors and successful deliveries to make or if
264 * if there are any groups to deliver to, we basically need to read
265 * the message twice. So, we have to put it in a tmp file.
268 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
273 if ( (fp = tmpfile()) == NULL ) {
274 syslog( LOG_ALERT, "could not open tmp file" );
275 unbind_and_exit( EX_TEMPFAIL );
278 /* copy the message to a temp file */
279 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
280 if ( fputs( buf, fp ) == EOF ) {
281 syslog( LOG_ALERT, "error writing tmpfile" );
282 unbind_and_exit( EX_TEMPFAIL );
286 if ( dup2( fileno( fp ), 0 ) == -1 ) {
287 syslog( LOG_ALERT, "could not dup2 tmpfile" );
288 unbind_and_exit( EX_TEMPFAIL );
294 /* deal with errors */
296 (void) rewind( stdin );
297 send_errors( errlist, numerr );
300 (void) ldap_unbind( ld );
302 /* send to groups with errorsTo */
304 (void) rewind( stdin );
305 send_group( togroups, ngroups );
308 /* send to expanded aliases and groups w/o errorsTo */
309 if ( numto > nargs ) {
310 (void) rewind( stdin );
311 send_message( tolist );
318 connect_to_x500( void )
320 int sizelimit = FAX_MAXAMBIGUOUS;
321 int deref = LDAP_DEREF_ALWAYS;
323 if ( (ld = ldap_init( NULL, 0 )) == NULL ) {
324 syslog( LOG_ALERT, "ldap_init failed" );
328 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
329 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
331 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
332 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
353 LDAPMessage *e, *res;
354 struct timeval timeout;
361 * Look up the name in X.500, add the appropriate addresses found
362 * to the to list, or to the err list in case of error. Groups are
363 * handled by the do_group routine, individuals are handled here.
364 * When looking up name, we follow the bases hierarchy, looking
365 * in base[0] first, then base[1], etc. For each base, there is
366 * a set of search filters to try, in order. If something goes
367 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
368 * If the b_rdnpref flag is set, then we give preference to entries
369 * that matched name because it's their rdn, otherwise not.
372 timeout.tv_sec = FAX_TIMEOUT;
374 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
375 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
376 char *format, *p, *argv[3];
379 for ( argc = 0; argc < 3; argc++ ) {
383 format = strdup( base[b].b_filter[f] );
384 for ( argc = 0, p = format; *p; p++ ) {
387 case 's': /* %s is the name */
391 case 'h': /* %h is the host */
398 "unknown format %c", *p );
406 /* three names ought to do... */
407 sprintf( filter, format, argv[0], argv[1], argv[2] );
411 rc = ldap_search_st( ld, base[b].b_dn,
412 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
415 /* some other trouble - try again later */
416 if ( rc != LDAP_SUCCESS &&
417 rc != LDAP_SIZELIMIT_EXCEEDED ) {
418 syslog( LOG_ALERT, "return %d from X.500", rc );
419 unbind_and_exit( EX_TEMPFAIL );
422 if ( (match = ldap_count_entries( ld, res )) != 0 )
432 /* trouble - try again later */
434 syslog( LOG_ALERT, "error parsing result from X.500" );
435 unbind_and_exit( EX_TEMPFAIL );
438 /* no matches - bounce with user unknown */
440 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
444 /* more than one match - bounce with ambiguous user? */
446 LDAPMessage *next, *tmpres = NULL;
450 /* not giving rdn preference - bounce with ambiguous user */
451 if ( base[b].b_rdnpref == 0 ) {
452 add_error( err, nerr, E_AMBIGUOUS, name, res );
457 * giving rdn preference - see if any entries were matched
458 * because of their rdn. If so, collect them to deal with
459 * later (== 1 we deliver, > 1 we bounce).
462 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
463 next = ldap_next_entry( ld, e );
464 dn = ldap_get_dn( ld, e );
465 xdn = ldap_explode_dn( dn, 1 );
467 /* XXX bad, but how else can we do it? XXX */
468 if ( strcasecmp( xdn[0], name ) == 0 ) {
469 ldap_delete_result_entry( &res, e );
470 ldap_add_result_entry( &tmpres, e );
473 ldap_value_free( xdn );
477 /* nothing matched by rdn - go ahead and bounce */
478 if ( tmpres == NULL ) {
479 add_error( err, nerr, E_AMBIGUOUS, name, res );
482 /* more than one matched by rdn - bounce with rdn matches */
483 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
484 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
488 } else if ( match < 0 ) {
489 syslog( LOG_ALERT, "error parsing result from X.500" );
490 unbind_and_exit( EX_TEMPFAIL );
493 /* otherwise one matched by rdn - send to it */
499 * if we get this far, it means that we found a single match for
500 * name. for a user, we deliver to the mail attribute or bounce
501 * with address and phone if no mail attr. for a group, we
502 * deliver to all members or bounce to rfc822ErrorsTo if no members.
506 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
507 syslog( LOG_ALERT, "error parsing entry from X.500" );
508 unbind_and_exit( EX_TEMPFAIL );
511 dn = ldap_get_dn( ld, e );
513 if ( type == GROUP_ERRORS ) {
514 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
515 do_group_errors( e, dn, to, nto, err, nerr );
517 } else if ( type == GROUP_REQUEST ) {
518 /* sent to group-request - resend to [rfc822]RequestsTo attr */
519 do_group_request( e, dn, to, nto, err, nerr );
521 } else if ( type == GROUP_MEMBERS ) {
522 /* sent to group-members - expand */
523 do_group_members( e, dn, to, nto, togroups, ngroups, err,
526 } else if ( isgroup( e ) ) {
528 * sent to group - resend from [rfc822]ErrorsTo if it's there,
529 * otherwise, expand the group
532 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
538 * sent to user - mail attribute => add it to the to list,
543 if ( (mail = ldap_get_values( ld, e,
544 "facsimileTelephoneNumber" )) != NULL ) {
549 fdn = ldap_get_dn( ld, e );
550 ufn = ldap_explode_dn( fdn, 1 );
551 /* Convert spaces to underscores for rp */
552 for (i = 0; ufn[0][i] != '\0'; i++) {
553 if (ufn[0][i] == ' ') {
557 *mail = faxtotpc(*mail, ufn[0]);
559 add_to(to, nto, mail);
564 ldap_value_free( ufn );
565 ldap_value_free( mail );
568 add_error( err, nerr, E_NOFAXNUM,
573 if ( (mail = ldap_get_values( ld, e, "mail" ))
575 add_to( to, nto, mail );
577 ldap_value_free( mail );
580 add_error( err, nerr, E_NOEMAIL,
605 * If this group has an rfc822ErrorsTo attribute, we need to
606 * arrange for errors involving this group to go there, not
607 * to the sender. Since sendmail only has the concept of a
608 * single sender, we arrange for errors to go to groupname-errors,
609 * which we then handle specially when (if) it comes back to us
610 * by expanding to all the rfc822ErrorsTo addresses. If it has no
611 * rfc822ErrorsTo attribute, we call do_group_members() to expand
615 if ( group_loop( dn ) ) {
619 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
620 add_group( dn, togroups, ngroups );
625 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
645 char **mail, **member, **joinable;
647 LDAPMessage *ee, *res;
648 struct timeval timeout;
651 * if all has gone according to plan, we've already arranged for
652 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
653 * so all we have to do here is arrange to send to the
654 * rfc822Mailbox attribute, the member attribute, and anyone who
655 * has joined the group by setting memberOfGroup equal to the
659 /* add members in the group itself - mail attribute */
660 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
662 * These are only email addresses - impossible
663 * to have a fax number
667 /* XXX How do we want to inform sender that the */
668 /* group they sent to has email-only members? */
671 add_to( to, nto, mail );
675 ldap_value_free( mail );
678 /* add members in the group itself - member attribute */
679 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
680 for ( i = 0; member[i] != NULL; i++ ) {
681 add_member( dn, member[i], to, nto, togroups,
682 ngroups, err, nerr );
685 ldap_value_free( member );
688 /* add members who have joined by setting memberOfGroup */
689 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
690 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
691 ldap_value_free( joinable );
694 ldap_value_free( joinable );
696 sprintf( filter, "(memberOfGroup=%s)", dn );
698 timeout.tv_sec = FAX_TIMEOUT;
701 /* for each subtree to look in... */
703 int sizelimit = FAX_MAXMEMBERS;
704 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
706 for ( i = 0; base[i].b_dn != NULL; i++ ) {
707 /* find entries that have joined this group... */
708 rc = ldap_search_st( ld, base[i].b_dn,
709 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
712 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
713 rc == LDAP_TIMELIMIT_EXCEEDED ) {
715 "group search limit exceeded %d", rc );
716 unbind_and_exit( EX_TEMPFAIL );
719 if ( rc != LDAP_SUCCESS ) {
720 syslog( LOG_ALERT, "group search bad return %d",
722 unbind_and_exit( EX_TEMPFAIL );
725 /* for each entry that has joined... */
726 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
727 ee = ldap_next_entry( ld, ee ) ) {
728 if ( isgroup( ee ) ) {
729 ndn = ldap_get_dn( ld, ee );
731 if ( do_group( e, ndn, to, nto,
732 togroups, ngroups, err, nerr )
735 "group loop (%s) (%s)",
744 /* add them to the to list */
747 if ( (mail = ldap_get_values( ld, ee,
748 "facsimileTelephoneNumber" ))
754 fdn = ldap_get_dn( ld, ee );
755 ufn = ldap_explode_dn( fdn, 1 );
760 for (i = 0; ufn[0][i] != '\0';
762 if (ufn[0][i] == ' ') {
766 *mail = faxtotpc(*mail, ufn[0]);
767 add_to(to, nto, mail);
769 ldap_value_free( mail );
772 ndn = ldap_get_dn( ld, ee );
774 add_error( err, nerr,
775 E_JOINMEMBERNOFAXNUM, ndn,
782 if ( (mail = ldap_get_values( ld, ee,
783 "mail" )) != NULL ) {
784 add_to( to, nto, mail );
786 ldap_value_free( mail );
788 /* else generate a bounce */
790 ndn = ldap_get_dn( ld, ee );
792 add_error( err, nerr,
793 E_JOINMEMBERNOEMAIL, ndn,
805 int sizelimit = FAX_MAXAMBIGUOUS;
806 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
828 LDAPMessage *res, *e;
829 struct timeval timeout;
831 timeout.tv_sec = FAX_TIMEOUT;
833 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, NULL,
834 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
835 if ( rc == LDAP_NO_SUCH_OBJECT ) {
836 add_error( err, nerr, E_BADMEMBER, dn, NULL );
840 syslog( LOG_ALERT, "member search bad return %d", rc );
842 unbind_and_exit( EX_TEMPFAIL );
846 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
847 syslog( LOG_ALERT, "member search error parsing entry" );
849 unbind_and_exit( EX_TEMPFAIL );
851 ndn = ldap_get_dn( ld, e );
853 /* allow groups within groups */
854 if ( isgroup( e ) ) {
855 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
857 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
867 if ( (mail = ldap_get_values( ld, e,
868 "facsimileTelephoneNumber" )) != NULL ) {
872 fdn = ldap_get_dn( ld, e );
873 ufn = ldap_explode_dn( fdn, 1 );
874 /* Convert spaces to underscores for rp */
875 for (i = 0; ufn[0][i] != '\0'; i++) {
876 if (ufn[0][i] == ' ') {
880 *mail = faxtotpc(*mail, ufn[0]);
881 add_to(to, nto, mail);
883 ldap_value_free( mail );
886 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULL );
890 /* send to the member's mail attribute */
891 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
892 add_to( to, nto, mail );
894 ldap_value_free( mail );
896 /* else generate a bounce */
898 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULL );
918 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
919 "requestsTo" )) != NULL ) {
920 add_to( to, nto, requeststo );
922 ldap_value_free( requeststo );
924 add_error( err, nerr, E_NOREQUEST, dn, NULL );
940 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
941 "errorsTo" )) != NULL ) {
942 add_to( to, nto, errorsto );
944 ldap_value_free( errorsto );
946 add_error( err, nerr, E_NOERRORS, dn, NULL );
951 send_message( char **to )
955 WAITSTATUSTYPE status;
960 if ( (pid = fork()) != 0 ) {
962 waitpid( pid, (int *) NULL, 0 );
964 wait4( pid, &status, WAIT_FLAGS, 0 );
968 /* to includes sendmailargs */
969 execv( FAX_SENDMAIL, to );
971 syslog( LOG_ALERT, "execv failed" );
978 send_group( Group *group, int ngroup )
985 WAITSTATUSTYPE status;
988 for ( i = 0; i < ngroup; i++ ) {
989 (void) rewind( stdin );
991 iargv[0] = FAX_SENDMAIL;
993 iargv[2] = group[i].g_errorsto;
994 iargv[3] = "-oMrX.500";
1001 add_to( &argv, &argc, iargv );
1002 add_to( &argv, &argc, group[i].g_members );
1006 if ( (pid = fork()) != 0 ) {
1008 waitpid( pid, (int *) NULL, 0 );
1010 wait4( pid, &status, WAIT_FLAGS, 0 );
1014 execv( FAX_SENDMAIL, argv );
1016 syslog( LOG_ALERT, "execv failed" );
1018 exit( EX_TEMPFAIL );
1026 send_errors( Error *err, int nerr )
1032 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1034 if ( (fp = popen( buf, "w" )) == NULL ) {
1035 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1039 fprintf( fp, "To: %s\n", mailfrom );
1040 fprintf( fp, "From: %s\n", errorsfrom );
1041 fprintf( fp, "Subject: undeliverable mail\n" );
1042 fprintf( fp, "\n" );
1043 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1044 for ( i = 0; i < nerr; i++ ) {
1045 namelen = strlen( err[i].e_addr );
1046 fprintf( fp, "\n" );
1048 switch ( err[i].e_code ) {
1050 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1054 fprintf( fp, "%s: Group member does not exist\n",
1059 fprintf( fp, "%s: Group exists but has no request address\n",
1064 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1069 do_ambiguous( fp, &err[i], namelen );
1073 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1076 case E_MEMBERNOEMAIL:
1077 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1081 case E_JOINMEMBERNOEMAIL:
1082 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1087 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1090 case E_MEMBERNOFAXNUM:
1091 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1095 case E_JOINMEMBERNOFAXNUM:
1096 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1101 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1102 unbind_and_exit( EX_TEMPFAIL );
1107 fprintf( fp, "\n------- The original message sent:\n\n" );
1109 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1113 if ( pclose( fp ) == -1 ) {
1114 syslog( LOG_ALERT, "pclose failed" );
1115 unbind_and_exit( EX_TEMPFAIL );
1123 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1129 if (errtype == E_NOFAXNUM) {
1130 fprintf(fp, "%s: User has no facsimile number registered.\n",
1132 } else if (errtype == E_NOEMAIL) {
1133 fprintf(fp, "%s: User has no email address registered.\n",
1136 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1137 namelen, " ", err->e_addr );
1140 dn = ldap_get_dn( ld, err->e_msg );
1141 ufn = ldap_explode_dn( dn, 1 );
1142 rdn = strdup( ufn[0] );
1143 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1144 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1146 for ( i = 0; vals[i]; i++ ) {
1147 last = strlen( vals[i] ) - 1;
1148 if ( isdigit((unsigned char) vals[i][last]) ) {
1149 rdn = strdup( vals[i] );
1154 ldap_value_free( vals );
1157 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1160 ldap_value_free( ufn );
1162 /* titles or descriptions */
1163 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1164 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1166 fprintf( fp, "%*s No title or description registered\n",
1169 for ( i = 0; vals[i] != NULL; i++ ) {
1170 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1173 ldap_value_free( vals );
1176 /* postal address */
1177 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1179 fprintf( fp, "%*s No postal address registered\n", namelen,
1182 fprintf( fp, "%*s ", namelen, " " );
1183 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1184 if ( vals[0][i] == '$' ) {
1185 fprintf( fp, "\n%*s ", namelen, " " );
1186 while ( isspace((unsigned char) vals[0][i+1]) )
1189 fprintf( fp, "%c", vals[0][i] );
1192 fprintf( fp, "\n" );
1194 ldap_value_free( vals );
1197 /* telephone number */
1198 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1200 fprintf( fp, "%*s No phone number registered\n", namelen,
1203 for ( i = 0; vals[i] != NULL; i++ ) {
1204 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1207 ldap_value_free( vals );
1213 do_ambiguous( FILE *fp, Error *err, int namelen )
1220 i = ldap_result2error( ld, err->e_msg, 0 );
1222 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1223 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1224 ldap_count_entries( ld, err->e_msg ) );
1226 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1227 e = ldap_next_entry( ld, e ) ) {
1228 dn = ldap_get_dn( ld, e );
1229 ufn = ldap_explode_dn( dn, 1 );
1230 rdn = strdup( ufn[0] );
1231 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1232 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1233 for ( i = 0; vals[i]; i++ ) {
1234 last = strlen( vals[i] ) - 1;
1235 if ( isdigit((unsigned char) vals[i][last]) ) {
1236 rdn = strdup( vals[i] );
1241 ldap_value_free( vals );
1245 if ( isgroup( e ) ) {
1246 vals = ldap_get_values( ld, e, "description" );
1248 vals = ldap_get_values( ld, e, "title" );
1251 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1252 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1253 fprintf( fp, " %s\n", vals[i] );
1258 ldap_value_free( ufn );
1260 ldap_value_free( vals );
1265 count_values( char **list )
1269 for ( i = 0; list && list[i] != NULL; i++ )
1276 add_to( char ***list, int *nlist, char **new )
1278 int i, nnew, oldnlist;
1280 nnew = count_values( new );
1283 if ( *list == NULL || *nlist == 0 ) {
1284 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1287 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1288 nnew * sizeof(char *) + sizeof(char *) );
1292 for ( i = 0; i < nnew; i++ ) {
1293 (*list)[i + oldnlist] = strdup( new[i] );
1295 (*list)[*nlist] = NULL;
1301 isgroup( LDAPMessage *e )
1306 oclist = ldap_get_values( ld, e, "objectClass" );
1308 for ( i = 0; oclist[i] != NULL; i++ ) {
1309 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1310 ldap_value_free( oclist );
1314 ldap_value_free( oclist );
1320 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1323 *err = (Error *) malloc( sizeof(Error) );
1325 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1328 (*err)[*nerr].e_code = code;
1329 (*err)[*nerr].e_addr = strdup( addr );
1330 (*err)[*nerr].e_msg = msg;
1337 add_group( char *dn, Group **list, int *nlist )
1342 for ( i = 0; i < *nlist; i++ ) {
1343 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1344 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1349 ufn = ldap_explode_dn( dn, 1 );
1350 namelen = strlen( ufn[0] );
1352 if ( *nlist == 0 ) {
1353 *list = (Group *) malloc( sizeof(Group) );
1355 *list = (Group *) realloc( *list, (*nlist + 1) *
1359 /* send errors to groupname-errors@host */
1360 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1362 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1363 (void) canonical( (*list)[*nlist].g_errorsto );
1365 /* send to groupname-members@host - make it a list for send_group */
1366 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1367 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1368 sizeof(MEMBERS) + hostlen + 2 );
1369 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1371 (void) canonical( (*list)[*nlist].g_members[0] );
1372 (*list)[*nlist].g_members[1] = NULL;
1374 /* save the group's dn so we can check for loops above */
1375 (*list)[*nlist].g_dn = strdup( dn );
1379 ldap_value_free( ufn );
1385 unbind_and_exit( int rc )
1389 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1390 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1396 canonical( char *s )
1400 for ( ; *s != '\0'; s++ ) {
1409 group_loop( char *dn )
1412 static char **groups;
1415 for ( i = 0; i < ngroups; i++ ) {
1416 if ( strcmp( dn, groups[i] ) == 0 )
1421 groups = (char **) malloc( sizeof(char *) );
1423 groups = (char **) realloc( groups,
1424 (ngroups + 1) * sizeof(char *) );
1426 groups[ngroups++] = strdup( dn );
1432 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1436 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1437 ldap_value_free( attr );
1441 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1442 ldap_value_free( attr );
1450 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1452 LDAPMessage *ee, *res;
1453 char **vals, **dnlist, **mail;
1455 struct timeval timeout;
1457 vals = ldap_get_values( ld, e, attr1 );
1458 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1461 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1462 timeout.tv_sec = FAX_TIMEOUT;
1463 timeout.tv_usec = 0;
1465 for ( i = 0; dnlist[i] != NULL; i++ ) {
1466 if ( (rc = ldap_search_st( ld, dnlist[i],
1467 LDAP_SCOPE_BASE, NULL, attrs, 0,
1468 &timeout, &res )) != LDAP_SUCCESS ) {
1469 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1470 unbind_and_exit( EX_TEMPFAIL );
1473 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1479 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1480 syslog( LOG_ALERT, "error parsing x500 entry" );
1484 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1486 add_to( &vals, &nto, mail );
1488 ldap_value_free( mail );
1491 ldap_msgfree( res );