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/syslog.h>
22 #include <ac/unistd.h>
24 extern int optind, errno;
27 #ifdef HAVE_SYS_PARAM_H
28 #include <sys/param.h>
31 #include <sys/resource.h>
37 #include <ldapconfig.h>
40 #define GROUP_ERRORS 1
41 #define GROUP_REQUEST 2
42 #define GROUP_MEMBERS 3
44 #define ERRORS "errors"
45 #define REQUEST "request"
46 #define MEMBERS "members"
49 char *errorsfrom = NULL;
50 char *mailfrom = NULL;
60 #define E_USERUNKNOWN 1
66 #define E_JOINMEMBERNOEMAIL 7
67 #define E_MEMBERNOEMAIL 8
69 #define E_JOINMEMBERNOFAXNUM 10
70 #define E_MEMBERNOFAXNUM 11
71 #define E_FAXTOEMAILMBR 12
77 typedef struct groupto {
83 typedef struct baseinfo {
84 char *b_dn; /* dn to start searching at */
85 char b_rdnpref; /* give rdn's preference when searching? */
86 char *b_filter[3]; /* filter to apply - name substituted for %s */
87 /* (up to three of them) */
91 { "ou=People, o=University of Michigan, c=US", 0,
92 "uid=%s", "cn=%s", NULL,
93 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
94 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
95 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
96 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
100 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
102 static char *attrs[] = { "objectClass", "title", "postaladdress",
103 "telephoneNumber", "mail", "description",
104 "errorsTo", "rfc822ErrorsTo", "requestsTo",
105 "rfc822RequestsTo", "joinable", "cn", "member",
106 "facsimileTelephoneNumber", NULL };
109 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
110 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
111 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
112 static void send_message(char **to);
113 static void send_errors(Error *err, int nerr);
114 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
115 static void do_ambiguous(FILE *fp, Error *err, int namelen);
116 static int count_values(char **list);
117 static void add_to(char ***list, int *nlist, char **new);
118 static int isgroup(LDAPMessage *e);
119 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
120 static void add_group(char *dn, Group **list, int *nlist);
121 static void unbind_and_exit(int rc);
122 static int group_loop(char *dn);
123 static void send_group(Group *group, int ngroup);
124 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
125 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
126 static char *canonical(char *s);
127 static int connect_to_x500 (void);
128 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
129 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
130 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
134 main ( int argc, char **argv )
140 int numto, ngroups, numerr, nargs;
143 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
145 case 'f': /* who it's from & where errors should go */
146 mailfrom = strdup( optarg );
147 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
148 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
149 sendmailargs[j+1] = mailfrom;
155 case 'h': /* hostname */
156 host = strdup( optarg );
157 hostlen = strlen(host);
160 /* mailer-daemon address - who we should */
161 case 'm': /* say errors come from */
162 errorsfrom = strdup( optarg );
166 syslog( LOG_ALERT, "unknown option" );
171 if ( (myname = strrchr( argv[0], '/' )) == NULL )
172 myname = strdup( argv[0] );
174 myname = strdup( myname + 1 );
175 if (!strcmp(myname, "mail500")) {
177 } else if (!strcmp(myname, "fax500")) {
180 /* I give up, I must be mail500 */
185 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
187 openlog( myname, OPENLOG_OPTIONS );
190 if ( mailfrom == NULL ) {
191 syslog( LOG_ALERT, "required argument -f not present" );
194 if ( errorsfrom == NULL ) {
195 syslog( LOG_ALERT, "required argument -m not present" );
198 if ( host == NULL ) {
199 syslog( LOG_ALERT, "required argument -h not present" );
203 if ( connect_to_x500() != 0 )
212 strcpy( buf, argv[0] );
213 for ( i = 1; i < argc; i++ ) {
215 strcat( buf, argv[i] );
218 syslog( LOG_ALERT, "args: (%s)", buf );
222 add_to( &tolist, &numto, sendmailargs );
224 ngroups = numerr = 0;
227 for ( i = optind; i < argc; i++ ) {
231 for ( j = 0; argv[i][j] != '\0'; j++ ) {
232 if ( argv[i][j] == '.' || argv[i][j] == '_' )
237 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
240 if ( strcmp( s, ERRORS ) == 0 ) {
243 } else if ( strcmp( s, REQUEST ) == 0 ) {
244 type = GROUP_REQUEST;
246 } else if ( strcmp( s, MEMBERS ) == 0 ) {
247 type = GROUP_MEMBERS;
252 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
253 &errlist, &numerr, type );
257 * If we have both errors and successful deliveries to make or if
258 * if there are any groups to deliver to, we basically need to read
259 * the message twice. So, we have to put it in a tmp file.
262 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
267 if ( (fp = tmpfile()) == NULL ) {
268 syslog( LOG_ALERT, "could not open tmp file" );
269 unbind_and_exit( EX_TEMPFAIL );
272 /* copy the message to a temp file */
273 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
274 if ( fputs( buf, fp ) == EOF ) {
275 syslog( LOG_ALERT, "error writing tmpfile" );
276 unbind_and_exit( EX_TEMPFAIL );
280 if ( dup2( fileno( fp ), 0 ) == -1 ) {
281 syslog( LOG_ALERT, "could not dup2 tmpfile" );
282 unbind_and_exit( EX_TEMPFAIL );
288 /* deal with errors */
290 (void) rewind( stdin );
291 send_errors( errlist, numerr );
294 (void) ldap_unbind( ld );
296 /* send to groups with errorsTo */
298 (void) rewind( stdin );
299 send_group( togroups, ngroups );
302 /* send to expanded aliases and groups w/o errorsTo */
303 if ( numto > nargs ) {
304 (void) rewind( stdin );
305 send_message( tolist );
312 connect_to_x500( void )
314 int sizelimit = FAX_MAXAMBIGUOUS;
315 int deref = LDAP_DEREF_ALWAYS;
317 if ( (ld = ldap_open( NULL, 0 )) == NULL ) {
318 syslog( LOG_ALERT, "ldap_open failed" );
322 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
323 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
325 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
326 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
347 LDAPMessage *e, *res;
348 struct timeval timeout;
355 * Look up the name in X.500, add the appropriate addresses found
356 * to the to list, or to the err list in case of error. Groups are
357 * handled by the do_group routine, individuals are handled here.
358 * When looking up name, we follow the bases hierarchy, looking
359 * in base[0] first, then base[1], etc. For each base, there is
360 * a set of search filters to try, in order. If something goes
361 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
362 * If the b_rdnpref flag is set, then we give preference to entries
363 * that matched name because it's their rdn, otherwise not.
366 timeout.tv_sec = FAX_TIMEOUT;
368 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
369 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
370 char *format, *p, *argv[3];
373 for ( argc = 0; argc < 3; argc++ ) {
377 format = strdup( base[b].b_filter[f] );
378 for ( argc = 0, p = format; *p; p++ ) {
381 case 's': /* %s is the name */
385 case 'h': /* %h is the host */
392 "unknown format %c", *p );
400 /* three names ought to do... */
401 sprintf( filter, format, argv[0], argv[1], argv[2] );
405 rc = ldap_search_st( ld, base[b].b_dn,
406 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
409 /* some other trouble - try again later */
410 if ( rc != LDAP_SUCCESS &&
411 rc != LDAP_SIZELIMIT_EXCEEDED ) {
412 syslog( LOG_ALERT, "return %d from X.500", rc );
413 unbind_and_exit( EX_TEMPFAIL );
416 if ( (match = ldap_count_entries( ld, res )) != 0 )
426 /* trouble - try again later */
428 syslog( LOG_ALERT, "error parsing result from X.500" );
429 unbind_and_exit( EX_TEMPFAIL );
432 /* no matches - bounce with user unknown */
434 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
438 /* more than one match - bounce with ambiguous user? */
440 LDAPMessage *next, *tmpres = NULL;
444 /* not giving rdn preference - bounce with ambiguous user */
445 if ( base[b].b_rdnpref == 0 ) {
446 add_error( err, nerr, E_AMBIGUOUS, name, res );
451 * giving rdn preference - see if any entries were matched
452 * because of their rdn. If so, collect them to deal with
453 * later (== 1 we deliver, > 1 we bounce).
456 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
457 next = ldap_next_entry( ld, e );
458 dn = ldap_get_dn( ld, e );
459 xdn = ldap_explode_dn( dn, 1 );
461 /* XXX bad, but how else can we do it? XXX */
462 if ( strcasecmp( xdn[0], name ) == 0 ) {
463 ldap_delete_result_entry( &res, e );
464 ldap_add_result_entry( &tmpres, e );
467 ldap_value_free( xdn );
471 /* nothing matched by rdn - go ahead and bounce */
472 if ( tmpres == NULL ) {
473 add_error( err, nerr, E_AMBIGUOUS, name, res );
476 /* more than one matched by rdn - bounce with rdn matches */
477 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
478 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
482 } else if ( match < 0 ) {
483 syslog( LOG_ALERT, "error parsing result from X.500" );
484 unbind_and_exit( EX_TEMPFAIL );
487 /* otherwise one matched by rdn - send to it */
493 * if we get this far, it means that we found a single match for
494 * name. for a user, we deliver to the mail attribute or bounce
495 * with address and phone if no mail attr. for a group, we
496 * deliver to all members or bounce to rfc822ErrorsTo if no members.
500 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
501 syslog( LOG_ALERT, "error parsing entry from X.500" );
502 unbind_and_exit( EX_TEMPFAIL );
505 dn = ldap_get_dn( ld, e );
507 if ( type == GROUP_ERRORS ) {
508 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
509 do_group_errors( e, dn, to, nto, err, nerr );
511 } else if ( type == GROUP_REQUEST ) {
512 /* sent to group-request - resend to [rfc822]RequestsTo attr */
513 do_group_request( e, dn, to, nto, err, nerr );
515 } else if ( type == GROUP_MEMBERS ) {
516 /* sent to group-members - expand */
517 do_group_members( e, dn, to, nto, togroups, ngroups, err,
520 } else if ( isgroup( e ) ) {
522 * sent to group - resend from [rfc822]ErrorsTo if it's there,
523 * otherwise, expand the group
526 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
532 * sent to user - mail attribute => add it to the to list,
537 if ( (mail = ldap_get_values( ld, e,
538 "facsimileTelephoneNumber" )) != NULL ) {
543 fdn = ldap_get_dn( ld, e );
544 ufn = ldap_explode_dn( fdn, 1 );
545 /* Convert spaces to underscores for rp */
546 for (i = 0; ufn[0][i] != '\0'; i++) {
547 if (ufn[0][i] == ' ') {
551 *mail = faxtotpc(*mail, ufn[0]);
553 add_to(to, nto, mail);
558 ldap_value_free( ufn );
559 ldap_value_free( mail );
562 add_error( err, nerr, E_NOFAXNUM,
567 if ( (mail = ldap_get_values( ld, e, "mail" ))
569 add_to( to, nto, mail );
571 ldap_value_free( mail );
574 add_error( err, nerr, E_NOEMAIL,
599 * If this group has an rfc822ErrorsTo attribute, we need to
600 * arrange for errors involving this group to go there, not
601 * to the sender. Since sendmail only has the concept of a
602 * single sender, we arrange for errors to go to groupname-errors,
603 * which we then handle specially when (if) it comes back to us
604 * by expanding to all the rfc822ErrorsTo addresses. If it has no
605 * rfc822ErrorsTo attribute, we call do_group_members() to expand
609 if ( group_loop( dn ) ) {
613 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
614 add_group( dn, togroups, ngroups );
619 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
639 char **mail, **member, **joinable;
641 LDAPMessage *ee, *res;
642 struct timeval timeout;
645 * if all has gone according to plan, we've already arranged for
646 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
647 * so all we have to do here is arrange to send to the
648 * rfc822Mailbox attribute, the member attribute, and anyone who
649 * has joined the group by setting memberOfGroup equal to the
653 /* add members in the group itself - mail attribute */
654 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
656 * These are only email addresses - impossible
657 * to have a fax number
661 /* XXX How do we want to inform sender that the */
662 /* group they sent to has email-only members? */
665 add_to( to, nto, mail );
669 ldap_value_free( mail );
672 /* add members in the group itself - member attribute */
673 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
674 for ( i = 0; member[i] != NULL; i++ ) {
675 add_member( dn, member[i], to, nto, togroups,
676 ngroups, err, nerr );
679 ldap_value_free( member );
682 /* add members who have joined by setting memberOfGroup */
683 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
684 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
685 ldap_value_free( joinable );
688 ldap_value_free( joinable );
690 sprintf( filter, "(memberOfGroup=%s)", dn );
692 timeout.tv_sec = FAX_TIMEOUT;
695 /* for each subtree to look in... */
697 int sizelimit = FAX_MAXMEMBERS;
698 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
700 for ( i = 0; base[i].b_dn != NULL; i++ ) {
701 /* find entries that have joined this group... */
702 rc = ldap_search_st( ld, base[i].b_dn,
703 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
706 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
707 rc == LDAP_TIMELIMIT_EXCEEDED ) {
709 "group search limit exceeded %d", rc );
710 unbind_and_exit( EX_TEMPFAIL );
713 if ( rc != LDAP_SUCCESS ) {
714 syslog( LOG_ALERT, "group search bad return %d",
716 unbind_and_exit( EX_TEMPFAIL );
719 /* for each entry that has joined... */
720 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
721 ee = ldap_next_entry( ld, ee ) ) {
722 if ( isgroup( ee ) ) {
723 ndn = ldap_get_dn( ld, ee );
725 if ( do_group( e, ndn, to, nto,
726 togroups, ngroups, err, nerr )
729 "group loop (%s) (%s)",
738 /* add them to the to list */
741 if ( (mail = ldap_get_values( ld, ee,
742 "facsimileTelephoneNumber" ))
748 fdn = ldap_get_dn( ld, ee );
749 ufn = ldap_explode_dn( fdn, 1 );
754 for (i = 0; ufn[0][i] != '\0';
756 if (ufn[0][i] == ' ') {
760 *mail = faxtotpc(*mail, ufn[0]);
761 add_to(to, nto, mail);
763 ldap_value_free( mail );
766 ndn = ldap_get_dn( ld, ee );
768 add_error( err, nerr,
769 E_JOINMEMBERNOFAXNUM, ndn,
776 if ( (mail = ldap_get_values( ld, ee,
777 "mail" )) != NULL ) {
778 add_to( to, nto, mail );
780 ldap_value_free( mail );
782 /* else generate a bounce */
784 ndn = ldap_get_dn( ld, ee );
786 add_error( err, nerr,
787 E_JOINMEMBERNOEMAIL, ndn,
799 int sizelimit = FAX_MAXAMBIGUOUS;
800 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
822 LDAPMessage *res, *e;
823 struct timeval timeout;
825 timeout.tv_sec = FAX_TIMEOUT;
827 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
828 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
829 if ( rc == LDAP_NO_SUCH_OBJECT ) {
830 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
834 syslog( LOG_ALERT, "member search bad return %d", rc );
836 unbind_and_exit( EX_TEMPFAIL );
840 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
841 syslog( LOG_ALERT, "member search error parsing entry" );
843 unbind_and_exit( EX_TEMPFAIL );
845 ndn = ldap_get_dn( ld, e );
847 /* allow groups within groups */
848 if ( isgroup( e ) ) {
849 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
851 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
861 if ( (mail = ldap_get_values( ld, e,
862 "facsimileTelephoneNumber" )) != NULL ) {
866 fdn = ldap_get_dn( ld, e );
867 ufn = ldap_explode_dn( fdn, 1 );
868 /* Convert spaces to underscores for rp */
869 for (i = 0; ufn[0][i] != '\0'; i++) {
870 if (ufn[0][i] == ' ') {
874 *mail = faxtotpc(*mail, ufn[0]);
875 add_to(to, nto, mail);
877 ldap_value_free( mail );
880 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
884 /* send to the member's mail attribute */
885 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
886 add_to( to, nto, mail );
888 ldap_value_free( mail );
890 /* else generate a bounce */
892 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
912 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
913 "requestsTo" )) != NULL ) {
914 add_to( to, nto, requeststo );
916 ldap_value_free( requeststo );
918 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
934 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
935 "errorsTo" )) != NULL ) {
936 add_to( to, nto, errorsto );
938 ldap_value_free( errorsto );
940 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
945 send_message( char **to )
949 WAITSTATUSTYPE status;
954 if ( (pid = fork()) != 0 ) {
956 waitpid( pid, (int *) NULL, 0 );
958 wait4( pid, &status, WAIT_FLAGS, 0 );
962 /* to includes sendmailargs */
963 execv( FAX_SENDMAIL, to );
965 syslog( LOG_ALERT, "execv failed" );
972 send_group( Group *group, int ngroup )
979 WAITSTATUSTYPE status;
982 for ( i = 0; i < ngroup; i++ ) {
983 (void) rewind( stdin );
985 iargv[0] = FAX_SENDMAIL;
987 iargv[2] = group[i].g_errorsto;
988 iargv[3] = "-oMrX.500";
995 add_to( &argv, &argc, iargv );
996 add_to( &argv, &argc, group[i].g_members );
1000 if ( (pid = fork()) != 0 ) {
1002 waitpid( pid, (int *) NULL, 0 );
1004 wait4( pid, &status, WAIT_FLAGS, 0 );
1008 execv( FAX_SENDMAIL, argv );
1010 syslog( LOG_ALERT, "execv failed" );
1012 exit( EX_TEMPFAIL );
1020 send_errors( Error *err, int nerr )
1026 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1028 if ( (fp = popen( buf, "w" )) == NULL ) {
1029 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1033 fprintf( fp, "To: %s\n", mailfrom );
1034 fprintf( fp, "From: %s\n", errorsfrom );
1035 fprintf( fp, "Subject: undeliverable mail\n" );
1036 fprintf( fp, "\n" );
1037 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1038 for ( i = 0; i < nerr; i++ ) {
1039 namelen = strlen( err[i].e_addr );
1040 fprintf( fp, "\n" );
1042 switch ( err[i].e_code ) {
1044 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1048 fprintf( fp, "%s: Group member does not exist\n",
1053 fprintf( fp, "%s: Group exists but has no request address\n",
1058 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1063 do_ambiguous( fp, &err[i], namelen );
1067 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1070 case E_MEMBERNOEMAIL:
1071 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1075 case E_JOINMEMBERNOEMAIL:
1076 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1081 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1084 case E_MEMBERNOFAXNUM:
1085 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1089 case E_JOINMEMBERNOFAXNUM:
1090 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1095 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1096 unbind_and_exit( EX_TEMPFAIL );
1101 fprintf( fp, "\n------- The original message sent:\n\n" );
1103 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1107 if ( pclose( fp ) == -1 ) {
1108 syslog( LOG_ALERT, "pclose failed" );
1109 unbind_and_exit( EX_TEMPFAIL );
1117 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1123 if (errtype == E_NOFAXNUM) {
1124 fprintf(fp, "%s: User has no facsimile number registered.\n",
1126 } else if (errtype == E_NOEMAIL) {
1127 fprintf(fp, "%s: User has no email address registered.\n",
1130 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1131 namelen, " ", err->e_addr );
1134 dn = ldap_get_dn( ld, err->e_msg );
1135 ufn = ldap_explode_dn( dn, 1 );
1136 rdn = strdup( ufn[0] );
1137 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1138 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1140 for ( i = 0; vals[i]; i++ ) {
1141 last = strlen( vals[i] ) - 1;
1142 if ( isdigit( vals[i][last] ) ) {
1143 rdn = strdup( vals[i] );
1148 ldap_value_free( vals );
1151 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1154 ldap_value_free( ufn );
1156 /* titles or descriptions */
1157 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1158 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1160 fprintf( fp, "%*s No title or description registered\n",
1163 for ( i = 0; vals[i] != NULL; i++ ) {
1164 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1167 ldap_value_free( vals );
1170 /* postal address */
1171 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1173 fprintf( fp, "%*s No postal address registered\n", namelen,
1176 fprintf( fp, "%*s ", namelen, " " );
1177 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1178 if ( vals[0][i] == '$' ) {
1179 fprintf( fp, "\n%*s ", namelen, " " );
1180 while ( isspace( vals[0][i+1] ) )
1183 fprintf( fp, "%c", vals[0][i] );
1186 fprintf( fp, "\n" );
1188 ldap_value_free( vals );
1191 /* telephone number */
1192 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1194 fprintf( fp, "%*s No phone number registered\n", namelen,
1197 for ( i = 0; vals[i] != NULL; i++ ) {
1198 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1201 ldap_value_free( vals );
1207 do_ambiguous( FILE *fp, Error *err, int namelen )
1214 i = ldap_result2error( ld, err->e_msg, 0 );
1216 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1217 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1218 ldap_count_entries( ld, err->e_msg ) );
1220 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1221 e = ldap_next_entry( ld, e ) ) {
1222 dn = ldap_get_dn( ld, e );
1223 ufn = ldap_explode_dn( dn, 1 );
1224 rdn = strdup( ufn[0] );
1225 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1226 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1227 for ( i = 0; vals[i]; i++ ) {
1228 last = strlen( vals[i] ) - 1;
1229 if ( isdigit( vals[i][last] ) ) {
1230 rdn = strdup( vals[i] );
1235 ldap_value_free( vals );
1239 if ( isgroup( e ) ) {
1240 vals = ldap_get_values( ld, e, "description" );
1242 vals = ldap_get_values( ld, e, "title" );
1245 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1246 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1247 fprintf( fp, " %s\n", vals[i] );
1252 ldap_value_free( ufn );
1254 ldap_value_free( vals );
1259 count_values( char **list )
1263 for ( i = 0; list && list[i] != NULL; i++ )
1270 add_to( char ***list, int *nlist, char **new )
1272 int i, nnew, oldnlist;
1274 nnew = count_values( new );
1277 if ( *list == NULL || *nlist == 0 ) {
1278 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1281 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1282 nnew * sizeof(char *) + sizeof(char *) );
1286 for ( i = 0; i < nnew; i++ ) {
1287 (*list)[i + oldnlist] = strdup( new[i] );
1289 (*list)[*nlist] = NULL;
1295 isgroup( LDAPMessage *e )
1300 oclist = ldap_get_values( ld, e, "objectClass" );
1302 for ( i = 0; oclist[i] != NULL; i++ ) {
1303 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1304 ldap_value_free( oclist );
1308 ldap_value_free( oclist );
1314 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1317 *err = (Error *) malloc( sizeof(Error) );
1319 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1322 (*err)[*nerr].e_code = code;
1323 (*err)[*nerr].e_addr = strdup( addr );
1324 (*err)[*nerr].e_msg = msg;
1331 add_group( char *dn, Group **list, int *nlist )
1336 for ( i = 0; i < *nlist; i++ ) {
1337 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1338 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1343 ufn = ldap_explode_dn( dn, 1 );
1344 namelen = strlen( ufn[0] );
1346 if ( *nlist == 0 ) {
1347 *list = (Group *) malloc( sizeof(Group) );
1349 *list = (Group *) realloc( *list, (*nlist + 1) *
1353 /* send errors to groupname-errors@host */
1354 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1356 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1357 (void) canonical( (*list)[*nlist].g_errorsto );
1359 /* send to groupname-members@host - make it a list for send_group */
1360 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1361 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1362 sizeof(MEMBERS) + hostlen + 2 );
1363 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1365 (void) canonical( (*list)[*nlist].g_members[0] );
1366 (*list)[*nlist].g_members[1] = NULL;
1368 /* save the group's dn so we can check for loops above */
1369 (*list)[*nlist].g_dn = strdup( dn );
1373 ldap_value_free( ufn );
1379 unbind_and_exit( int rc )
1383 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1384 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1390 canonical( char *s )
1394 for ( ; *s != '\0'; s++ ) {
1403 group_loop( char *dn )
1406 static char **groups;
1409 for ( i = 0; i < ngroups; i++ ) {
1410 if ( strcmp( dn, groups[i] ) == 0 )
1415 groups = (char **) malloc( sizeof(char *) );
1417 groups = (char **) realloc( groups,
1418 (ngroups + 1) * sizeof(char *) );
1420 groups[ngroups++] = strdup( dn );
1426 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1430 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1431 ldap_value_free( attr );
1435 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1436 ldap_value_free( attr );
1444 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1446 LDAPMessage *ee, *res;
1447 char **vals, **dnlist, **mail;
1449 struct timeval timeout;
1451 vals = ldap_get_values( ld, e, attr1 );
1452 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1455 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1456 timeout.tv_sec = FAX_TIMEOUT;
1457 timeout.tv_usec = 0;
1459 for ( i = 0; dnlist[i] != NULL; i++ ) {
1460 if ( (rc = ldap_search_st( ld, dnlist[i],
1461 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1462 &timeout, &res )) != LDAP_SUCCESS ) {
1463 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1464 unbind_and_exit( EX_TEMPFAIL );
1467 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1473 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1474 syslog( LOG_ALERT, "error parsing x500 entry" );
1478 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1480 add_to( &vals, &nto, mail );
1482 ldap_value_free( mail );
1485 ldap_msgfree( res );