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.
17 #include <ac/stdlib.h>
21 #include <ac/signal.h>
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/sysexits.h>
25 #include <ac/syslog.h>
27 #include <ac/unistd.h>
30 #ifdef HAVE_SYS_PARAM_H
31 #include <sys/param.h>
33 #ifdef HAVE_SYS_RESOURCE_H
34 #include <sys/resource.h>
42 #include <ldapconfig.h>
45 #define GROUP_ERRORS 1
46 #define GROUP_REQUEST 2
47 #define GROUP_MEMBERS 3
49 #define ERRORS "errors"
50 #define REQUEST "request"
51 #define MEMBERS "members"
54 char *errorsfrom = NULL;
55 char *mailfrom = NULL;
65 #define E_USERUNKNOWN 1
71 #define E_JOINMEMBERNOEMAIL 7
72 #define E_MEMBERNOEMAIL 8
74 #define E_JOINMEMBERNOFAXNUM 10
75 #define E_MEMBERNOFAXNUM 11
76 #define E_FAXTOEMAILMBR 12
82 typedef struct groupto {
88 typedef struct baseinfo {
89 char *b_dn; /* dn to start searching at */
90 char b_rdnpref; /* give rdn's preference when searching? */
91 char *b_filter[3]; /* filter to apply - name substituted for %s */
92 /* (up to three of them) */
96 { "ou=People, o=University of Michigan, c=US", 0,
97 "uid=%s", "cn=%s", NULL,
98 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
99 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
100 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
101 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
105 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
107 static char *attrs[] = { "objectClass", "title", "postaladdress",
108 "telephoneNumber", "mail", "description",
109 "errorsTo", "rfc822ErrorsTo", "requestsTo",
110 "rfc822RequestsTo", "joinable", "cn", "member",
111 "facsimileTelephoneNumber", NULL };
114 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
115 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
116 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
117 static void send_message(char **to);
118 static void send_errors(Error *err, int nerr);
119 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
120 static void do_ambiguous(FILE *fp, Error *err, int namelen);
121 static int count_values(char **list);
122 static void add_to(char ***list, int *nlist, char **new);
123 static int isgroup(LDAPMessage *e);
124 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
125 static void add_group(char *dn, Group **list, int *nlist);
126 static void unbind_and_exit(int rc);
127 static int group_loop(char *dn);
128 static void send_group(Group *group, int ngroup);
129 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
130 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
131 static char *canonical(char *s);
132 static int connect_to_x500 (void);
133 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
134 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
135 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
139 main ( int argc, char **argv )
145 int numto, ngroups, numerr, nargs;
148 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
150 case 'f': /* who it's from & where errors should go */
151 mailfrom = strdup( optarg );
152 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
153 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
154 sendmailargs[j+1] = mailfrom;
160 case 'h': /* hostname */
161 host = strdup( optarg );
162 hostlen = strlen(host);
165 /* mailer-daemon address - who we should */
166 case 'm': /* say errors come from */
167 errorsfrom = strdup( optarg );
171 syslog( LOG_ALERT, "unknown option" );
176 if ( (myname = strrchr( argv[0], '/' )) == NULL )
177 myname = strdup( argv[0] );
179 myname = strdup( myname + 1 );
180 if (!strcmp(myname, "mail500")) {
182 } else if (!strcmp(myname, "fax500")) {
185 /* I give up, I must be mail500 */
190 (void) SIGNAL( SIGPIPE, SIG_IGN );
194 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
196 openlog( myname, OPENLOG_OPTIONS );
199 if ( mailfrom == NULL ) {
200 syslog( LOG_ALERT, "required argument -f not present" );
203 if ( errorsfrom == NULL ) {
204 syslog( LOG_ALERT, "required argument -m not present" );
207 if ( host == NULL ) {
208 syslog( LOG_ALERT, "required argument -h not present" );
212 if ( connect_to_x500() != 0 )
221 strcpy( buf, argv[0] );
222 for ( i = 1; i < argc; i++ ) {
224 strcat( buf, argv[i] );
227 syslog( LOG_ALERT, "args: (%s)", buf );
231 add_to( &tolist, &numto, sendmailargs );
233 ngroups = numerr = 0;
236 for ( i = optind; i < argc; i++ ) {
240 for ( j = 0; argv[i][j] != '\0'; j++ ) {
241 if ( argv[i][j] == '.' || argv[i][j] == '_' )
246 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
249 if ( strcmp( s, ERRORS ) == 0 ) {
252 } else if ( strcmp( s, REQUEST ) == 0 ) {
253 type = GROUP_REQUEST;
255 } else if ( strcmp( s, MEMBERS ) == 0 ) {
256 type = GROUP_MEMBERS;
261 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
262 &errlist, &numerr, type );
266 * If we have both errors and successful deliveries to make or if
267 * if there are any groups to deliver to, we basically need to read
268 * the message twice. So, we have to put it in a tmp file.
271 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
276 if ( (fp = tmpfile()) == NULL ) {
277 syslog( LOG_ALERT, "could not open tmp file" );
278 unbind_and_exit( EX_TEMPFAIL );
281 /* copy the message to a temp file */
282 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
283 if ( fputs( buf, fp ) == EOF ) {
284 syslog( LOG_ALERT, "error writing tmpfile" );
285 unbind_and_exit( EX_TEMPFAIL );
289 if ( dup2( fileno( fp ), 0 ) == -1 ) {
290 syslog( LOG_ALERT, "could not dup2 tmpfile" );
291 unbind_and_exit( EX_TEMPFAIL );
297 /* deal with errors */
299 (void) rewind( stdin );
300 send_errors( errlist, numerr );
303 (void) ldap_unbind( ld );
305 /* send to groups with errorsTo */
307 (void) rewind( stdin );
308 send_group( togroups, ngroups );
311 /* send to expanded aliases and groups w/o errorsTo */
312 if ( numto > nargs ) {
313 (void) rewind( stdin );
314 send_message( tolist );
321 connect_to_x500( void )
323 int sizelimit = FAX_MAXAMBIGUOUS;
324 int deref = LDAP_DEREF_ALWAYS;
326 if ( (ld = ldap_init( NULL, 0 )) == NULL ) {
327 syslog( LOG_ALERT, "ldap_init failed" );
331 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
332 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
334 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
335 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
356 LDAPMessage *e, *res;
357 struct timeval timeout;
364 * Look up the name in X.500, add the appropriate addresses found
365 * to the to list, or to the err list in case of error. Groups are
366 * handled by the do_group routine, individuals are handled here.
367 * When looking up name, we follow the bases hierarchy, looking
368 * in base[0] first, then base[1], etc. For each base, there is
369 * a set of search filters to try, in order. If something goes
370 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
371 * If the b_rdnpref flag is set, then we give preference to entries
372 * that matched name because it's their rdn, otherwise not.
375 timeout.tv_sec = FAX_TIMEOUT;
377 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
378 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
379 char *format, *p, *argv[3];
382 for ( argc = 0; argc < 3; argc++ ) {
386 format = strdup( base[b].b_filter[f] );
387 for ( argc = 0, p = format; *p; p++ ) {
390 case 's': /* %s is the name */
394 case 'h': /* %h is the host */
401 "unknown format %c", *p );
409 /* three names ought to do... */
410 sprintf( filter, format, argv[0], argv[1], argv[2] );
414 rc = ldap_search_st( ld, base[b].b_dn,
415 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
418 /* some other trouble - try again later */
419 if ( rc != LDAP_SUCCESS &&
420 rc != LDAP_SIZELIMIT_EXCEEDED ) {
421 syslog( LOG_ALERT, "return %d from X.500", rc );
422 unbind_and_exit( EX_TEMPFAIL );
425 if ( (match = ldap_count_entries( ld, res )) != 0 )
435 /* trouble - try again later */
437 syslog( LOG_ALERT, "error parsing result from X.500" );
438 unbind_and_exit( EX_TEMPFAIL );
441 /* no matches - bounce with user unknown */
443 add_error( err, nerr, E_USERUNKNOWN, name, NULL );
447 /* more than one match - bounce with ambiguous user? */
449 LDAPMessage *next, *tmpres = NULL;
453 /* not giving rdn preference - bounce with ambiguous user */
454 if ( base[b].b_rdnpref == 0 ) {
455 add_error( err, nerr, E_AMBIGUOUS, name, res );
460 * giving rdn preference - see if any entries were matched
461 * because of their rdn. If so, collect them to deal with
462 * later (== 1 we deliver, > 1 we bounce).
465 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
466 next = ldap_next_entry( ld, e );
467 dn = ldap_get_dn( ld, e );
468 xdn = ldap_explode_dn( dn, 1 );
470 /* XXX bad, but how else can we do it? XXX */
471 if ( strcasecmp( xdn[0], name ) == 0 ) {
472 ldap_delete_result_entry( &res, e );
473 ldap_add_result_entry( &tmpres, e );
476 ldap_value_free( xdn );
480 /* nothing matched by rdn - go ahead and bounce */
481 if ( tmpres == NULL ) {
482 add_error( err, nerr, E_AMBIGUOUS, name, res );
485 /* more than one matched by rdn - bounce with rdn matches */
486 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
487 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
491 } else if ( match < 0 ) {
492 syslog( LOG_ALERT, "error parsing result from X.500" );
493 unbind_and_exit( EX_TEMPFAIL );
496 /* otherwise one matched by rdn - send to it */
502 * if we get this far, it means that we found a single match for
503 * name. for a user, we deliver to the mail attribute or bounce
504 * with address and phone if no mail attr. for a group, we
505 * deliver to all members or bounce to rfc822ErrorsTo if no members.
509 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
510 syslog( LOG_ALERT, "error parsing entry from X.500" );
511 unbind_and_exit( EX_TEMPFAIL );
514 dn = ldap_get_dn( ld, e );
516 if ( type == GROUP_ERRORS ) {
517 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
518 do_group_errors( e, dn, to, nto, err, nerr );
520 } else if ( type == GROUP_REQUEST ) {
521 /* sent to group-request - resend to [rfc822]RequestsTo attr */
522 do_group_request( e, dn, to, nto, err, nerr );
524 } else if ( type == GROUP_MEMBERS ) {
525 /* sent to group-members - expand */
526 do_group_members( e, dn, to, nto, togroups, ngroups, err,
529 } else if ( isgroup( e ) ) {
531 * sent to group - resend from [rfc822]ErrorsTo if it's there,
532 * otherwise, expand the group
535 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
541 * sent to user - mail attribute => add it to the to list,
546 if ( (mail = ldap_get_values( ld, e,
547 "facsimileTelephoneNumber" )) != NULL ) {
552 fdn = ldap_get_dn( ld, e );
553 ufn = ldap_explode_dn( fdn, 1 );
554 /* Convert spaces to underscores for rp */
555 for (i = 0; ufn[0][i] != '\0'; i++) {
556 if (ufn[0][i] == ' ') {
560 *mail = faxtotpc(*mail, ufn[0]);
562 add_to(to, nto, mail);
567 ldap_value_free( ufn );
568 ldap_value_free( mail );
571 add_error( err, nerr, E_NOFAXNUM,
576 if ( (mail = ldap_get_values( ld, e, "mail" ))
578 add_to( to, nto, mail );
580 ldap_value_free( mail );
583 add_error( err, nerr, E_NOEMAIL,
608 * If this group has an rfc822ErrorsTo attribute, we need to
609 * arrange for errors involving this group to go there, not
610 * to the sender. Since sendmail only has the concept of a
611 * single sender, we arrange for errors to go to groupname-errors,
612 * which we then handle specially when (if) it comes back to us
613 * by expanding to all the rfc822ErrorsTo addresses. If it has no
614 * rfc822ErrorsTo attribute, we call do_group_members() to expand
618 if ( group_loop( dn ) ) {
622 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
623 add_group( dn, togroups, ngroups );
628 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
648 char **mail, **member, **joinable;
650 LDAPMessage *ee, *res;
651 struct timeval timeout;
654 * if all has gone according to plan, we've already arranged for
655 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
656 * so all we have to do here is arrange to send to the
657 * rfc822Mailbox attribute, the member attribute, and anyone who
658 * has joined the group by setting memberOfGroup equal to the
662 /* add members in the group itself - mail attribute */
663 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
665 * These are only email addresses - impossible
666 * to have a fax number
670 /* XXX How do we want to inform sender that the */
671 /* group they sent to has email-only members? */
674 add_to( to, nto, mail );
678 ldap_value_free( mail );
681 /* add members in the group itself - member attribute */
682 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
683 for ( i = 0; member[i] != NULL; i++ ) {
684 add_member( dn, member[i], to, nto, togroups,
685 ngroups, err, nerr );
688 ldap_value_free( member );
691 /* add members who have joined by setting memberOfGroup */
692 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
693 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
694 ldap_value_free( joinable );
697 ldap_value_free( joinable );
699 sprintf( filter, "(memberOfGroup=%s)", dn );
701 timeout.tv_sec = FAX_TIMEOUT;
704 /* for each subtree to look in... */
706 int sizelimit = FAX_MAXMEMBERS;
707 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
709 for ( i = 0; base[i].b_dn != NULL; i++ ) {
710 /* find entries that have joined this group... */
711 rc = ldap_search_st( ld, base[i].b_dn,
712 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
715 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
716 rc == LDAP_TIMELIMIT_EXCEEDED ) {
718 "group search limit exceeded %d", rc );
719 unbind_and_exit( EX_TEMPFAIL );
722 if ( rc != LDAP_SUCCESS ) {
723 syslog( LOG_ALERT, "group search bad return %d",
725 unbind_and_exit( EX_TEMPFAIL );
728 /* for each entry that has joined... */
729 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
730 ee = ldap_next_entry( ld, ee ) ) {
731 if ( isgroup( ee ) ) {
732 ndn = ldap_get_dn( ld, ee );
734 if ( do_group( e, ndn, to, nto,
735 togroups, ngroups, err, nerr )
738 "group loop (%s) (%s)",
747 /* add them to the to list */
750 if ( (mail = ldap_get_values( ld, ee,
751 "facsimileTelephoneNumber" ))
757 fdn = ldap_get_dn( ld, ee );
758 ufn = ldap_explode_dn( fdn, 1 );
763 for (i = 0; ufn[0][i] != '\0';
765 if (ufn[0][i] == ' ') {
769 *mail = faxtotpc(*mail, ufn[0]);
770 add_to(to, nto, mail);
772 ldap_value_free( mail );
775 ndn = ldap_get_dn( ld, ee );
777 add_error( err, nerr,
778 E_JOINMEMBERNOFAXNUM, ndn,
785 if ( (mail = ldap_get_values( ld, ee,
786 "mail" )) != NULL ) {
787 add_to( to, nto, mail );
789 ldap_value_free( mail );
791 /* else generate a bounce */
793 ndn = ldap_get_dn( ld, ee );
795 add_error( err, nerr,
796 E_JOINMEMBERNOEMAIL, ndn,
808 int sizelimit = FAX_MAXAMBIGUOUS;
809 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
831 LDAPMessage *res, *e;
832 struct timeval timeout;
834 timeout.tv_sec = FAX_TIMEOUT;
836 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
837 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
838 if ( rc == LDAP_NO_SUCH_OBJECT ) {
839 add_error( err, nerr, E_BADMEMBER, dn, NULL );
843 syslog( LOG_ALERT, "member search bad return %d", rc );
845 unbind_and_exit( EX_TEMPFAIL );
849 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
850 syslog( LOG_ALERT, "member search error parsing entry" );
852 unbind_and_exit( EX_TEMPFAIL );
854 ndn = ldap_get_dn( ld, e );
856 /* allow groups within groups */
857 if ( isgroup( e ) ) {
858 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
860 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
870 if ( (mail = ldap_get_values( ld, e,
871 "facsimileTelephoneNumber" )) != NULL ) {
875 fdn = ldap_get_dn( ld, e );
876 ufn = ldap_explode_dn( fdn, 1 );
877 /* Convert spaces to underscores for rp */
878 for (i = 0; ufn[0][i] != '\0'; i++) {
879 if (ufn[0][i] == ' ') {
883 *mail = faxtotpc(*mail, ufn[0]);
884 add_to(to, nto, mail);
886 ldap_value_free( mail );
889 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULL );
893 /* send to the member's mail attribute */
894 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
895 add_to( to, nto, mail );
897 ldap_value_free( mail );
899 /* else generate a bounce */
901 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULL );
921 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
922 "requestsTo" )) != NULL ) {
923 add_to( to, nto, requeststo );
925 ldap_value_free( requeststo );
927 add_error( err, nerr, E_NOREQUEST, dn, NULL );
943 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
944 "errorsTo" )) != NULL ) {
945 add_to( to, nto, errorsto );
947 ldap_value_free( errorsto );
949 add_error( err, nerr, E_NOERRORS, dn, NULL );
954 send_message( char **to )
958 WAITSTATUSTYPE status;
963 if ( (pid = fork()) != 0 ) {
965 waitpid( pid, (int *) NULL, 0 );
967 wait4( pid, &status, WAIT_FLAGS, 0 );
971 /* to includes sendmailargs */
972 execv( FAX_SENDMAIL, to );
974 syslog( LOG_ALERT, "execv failed" );
981 send_group( Group *group, int ngroup )
988 WAITSTATUSTYPE status;
991 for ( i = 0; i < ngroup; i++ ) {
992 (void) rewind( stdin );
994 iargv[0] = FAX_SENDMAIL;
996 iargv[2] = group[i].g_errorsto;
997 iargv[3] = "-oMrX.500";
1004 add_to( &argv, &argc, iargv );
1005 add_to( &argv, &argc, group[i].g_members );
1009 if ( (pid = fork()) != 0 ) {
1011 waitpid( pid, (int *) NULL, 0 );
1013 wait4( pid, &status, WAIT_FLAGS, 0 );
1017 execv( FAX_SENDMAIL, argv );
1019 syslog( LOG_ALERT, "execv failed" );
1021 exit( EX_TEMPFAIL );
1029 send_errors( Error *err, int nerr )
1035 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1037 if ( (fp = popen( buf, "w" )) == NULL ) {
1038 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1042 fprintf( fp, "To: %s\n", mailfrom );
1043 fprintf( fp, "From: %s\n", errorsfrom );
1044 fprintf( fp, "Subject: undeliverable mail\n" );
1045 fprintf( fp, "\n" );
1046 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1047 for ( i = 0; i < nerr; i++ ) {
1048 namelen = strlen( err[i].e_addr );
1049 fprintf( fp, "\n" );
1051 switch ( err[i].e_code ) {
1053 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1057 fprintf( fp, "%s: Group member does not exist\n",
1062 fprintf( fp, "%s: Group exists but has no request address\n",
1067 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1072 do_ambiguous( fp, &err[i], namelen );
1076 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1079 case E_MEMBERNOEMAIL:
1080 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1084 case E_JOINMEMBERNOEMAIL:
1085 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1090 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1093 case E_MEMBERNOFAXNUM:
1094 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1098 case E_JOINMEMBERNOFAXNUM:
1099 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1104 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1105 unbind_and_exit( EX_TEMPFAIL );
1110 fprintf( fp, "\n------- The original message sent:\n\n" );
1112 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1116 if ( pclose( fp ) == -1 ) {
1117 syslog( LOG_ALERT, "pclose failed" );
1118 unbind_and_exit( EX_TEMPFAIL );
1126 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1132 if (errtype == E_NOFAXNUM) {
1133 fprintf(fp, "%s: User has no facsimile number registered.\n",
1135 } else if (errtype == E_NOEMAIL) {
1136 fprintf(fp, "%s: User has no email address registered.\n",
1139 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1140 namelen, " ", err->e_addr );
1143 dn = ldap_get_dn( ld, err->e_msg );
1144 ufn = ldap_explode_dn( dn, 1 );
1145 rdn = strdup( ufn[0] );
1146 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1147 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1149 for ( i = 0; vals[i]; i++ ) {
1150 last = strlen( vals[i] ) - 1;
1151 if ( isdigit((unsigned char) vals[i][last]) ) {
1152 rdn = strdup( vals[i] );
1157 ldap_value_free( vals );
1160 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1163 ldap_value_free( ufn );
1165 /* titles or descriptions */
1166 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1167 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1169 fprintf( fp, "%*s No title or description registered\n",
1172 for ( i = 0; vals[i] != NULL; i++ ) {
1173 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1176 ldap_value_free( vals );
1179 /* postal address */
1180 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1182 fprintf( fp, "%*s No postal address registered\n", namelen,
1185 fprintf( fp, "%*s ", namelen, " " );
1186 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1187 if ( vals[0][i] == '$' ) {
1188 fprintf( fp, "\n%*s ", namelen, " " );
1189 while ( isspace((unsigned char) vals[0][i+1]) )
1192 fprintf( fp, "%c", vals[0][i] );
1195 fprintf( fp, "\n" );
1197 ldap_value_free( vals );
1200 /* telephone number */
1201 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1203 fprintf( fp, "%*s No phone number registered\n", namelen,
1206 for ( i = 0; vals[i] != NULL; i++ ) {
1207 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1210 ldap_value_free( vals );
1216 do_ambiguous( FILE *fp, Error *err, int namelen )
1223 i = ldap_result2error( ld, err->e_msg, 0 );
1225 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1226 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1227 ldap_count_entries( ld, err->e_msg ) );
1229 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1230 e = ldap_next_entry( ld, e ) ) {
1231 dn = ldap_get_dn( ld, e );
1232 ufn = ldap_explode_dn( dn, 1 );
1233 rdn = strdup( ufn[0] );
1234 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1235 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1236 for ( i = 0; vals[i]; i++ ) {
1237 last = strlen( vals[i] ) - 1;
1238 if ( isdigit((unsigned char) vals[i][last]) ) {
1239 rdn = strdup( vals[i] );
1244 ldap_value_free( vals );
1248 if ( isgroup( e ) ) {
1249 vals = ldap_get_values( ld, e, "description" );
1251 vals = ldap_get_values( ld, e, "title" );
1254 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1255 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1256 fprintf( fp, " %s\n", vals[i] );
1261 ldap_value_free( ufn );
1263 ldap_value_free( vals );
1268 count_values( char **list )
1272 for ( i = 0; list && list[i] != NULL; i++ )
1279 add_to( char ***list, int *nlist, char **new )
1281 int i, nnew, oldnlist;
1283 nnew = count_values( new );
1286 if ( *list == NULL || *nlist == 0 ) {
1287 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1290 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1291 nnew * sizeof(char *) + sizeof(char *) );
1295 for ( i = 0; i < nnew; i++ ) {
1296 (*list)[i + oldnlist] = strdup( new[i] );
1298 (*list)[*nlist] = NULL;
1304 isgroup( LDAPMessage *e )
1309 oclist = ldap_get_values( ld, e, "objectClass" );
1311 for ( i = 0; oclist[i] != NULL; i++ ) {
1312 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1313 ldap_value_free( oclist );
1317 ldap_value_free( oclist );
1323 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1326 *err = (Error *) malloc( sizeof(Error) );
1328 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1331 (*err)[*nerr].e_code = code;
1332 (*err)[*nerr].e_addr = strdup( addr );
1333 (*err)[*nerr].e_msg = msg;
1340 add_group( char *dn, Group **list, int *nlist )
1345 for ( i = 0; i < *nlist; i++ ) {
1346 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1347 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1352 ufn = ldap_explode_dn( dn, 1 );
1353 namelen = strlen( ufn[0] );
1355 if ( *nlist == 0 ) {
1356 *list = (Group *) malloc( sizeof(Group) );
1358 *list = (Group *) realloc( *list, (*nlist + 1) *
1362 /* send errors to groupname-errors@host */
1363 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1365 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1366 (void) canonical( (*list)[*nlist].g_errorsto );
1368 /* send to groupname-members@host - make it a list for send_group */
1369 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1370 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1371 sizeof(MEMBERS) + hostlen + 2 );
1372 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1374 (void) canonical( (*list)[*nlist].g_members[0] );
1375 (*list)[*nlist].g_members[1] = NULL;
1377 /* save the group's dn so we can check for loops above */
1378 (*list)[*nlist].g_dn = strdup( dn );
1382 ldap_value_free( ufn );
1388 unbind_and_exit( int rc )
1392 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1393 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1399 canonical( char *s )
1403 for ( ; *s != '\0'; s++ ) {
1412 group_loop( char *dn )
1415 static char **groups;
1418 for ( i = 0; i < ngroups; i++ ) {
1419 if ( strcmp( dn, groups[i] ) == 0 )
1424 groups = (char **) malloc( sizeof(char *) );
1426 groups = (char **) realloc( groups,
1427 (ngroups + 1) * sizeof(char *) );
1429 groups[ngroups++] = strdup( dn );
1435 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1439 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1440 ldap_value_free( attr );
1444 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1445 ldap_value_free( attr );
1453 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1455 LDAPMessage *ee, *res;
1456 char **vals, **dnlist, **mail;
1458 struct timeval timeout;
1460 vals = ldap_get_values( ld, e, attr1 );
1461 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1464 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1465 timeout.tv_sec = FAX_TIMEOUT;
1466 timeout.tv_usec = 0;
1468 for ( i = 0; dnlist[i] != NULL; i++ ) {
1469 if ( (rc = ldap_search_st( ld, dnlist[i],
1470 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1471 &timeout, &res )) != LDAP_SUCCESS ) {
1472 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1473 unbind_and_exit( EX_TEMPFAIL );
1476 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1482 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1483 syslog( LOG_ALERT, "error parsing x500 entry" );
1487 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1489 add_to( &vals, &nto, mail );
1491 ldap_value_free( mail );
1494 ldap_msgfree( res );