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.
20 #include <ac/signal.h>
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/sysexits.h>
24 #include <ac/syslog.h>
26 #include <ac/unistd.h>
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
32 #ifdef HAVE_SYS_RESOURCE_H
33 #include <sys/resource.h>
41 #include <ldapconfig.h>
44 #define GROUP_ERRORS 1
45 #define GROUP_REQUEST 2
46 #define GROUP_MEMBERS 3
48 #define ERRORS "errors"
49 #define REQUEST "request"
50 #define MEMBERS "members"
53 char *errorsfrom = NULL;
54 char *mailfrom = NULL;
64 #define E_USERUNKNOWN 1
70 #define E_JOINMEMBERNOEMAIL 7
71 #define E_MEMBERNOEMAIL 8
73 #define E_JOINMEMBERNOFAXNUM 10
74 #define E_MEMBERNOFAXNUM 11
75 #define E_FAXTOEMAILMBR 12
81 typedef struct groupto {
87 typedef struct baseinfo {
88 char *b_dn; /* dn to start searching at */
89 char b_rdnpref; /* give rdn's preference when searching? */
90 char *b_filter[3]; /* filter to apply - name substituted for %s */
91 /* (up to three of them) */
95 { "ou=People, o=University of Michigan, c=US", 0,
96 "uid=%s", "cn=%s", NULL,
97 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
98 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
99 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
100 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
104 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
106 static char *attrs[] = { "objectClass", "title", "postaladdress",
107 "telephoneNumber", "mail", "description",
108 "errorsTo", "rfc822ErrorsTo", "requestsTo",
109 "rfc822RequestsTo", "joinable", "cn", "member",
110 "facsimileTelephoneNumber", NULL };
113 static void do_address(char *name, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr, int type);
114 static int do_group(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
115 static void do_group_members(LDAPMessage *e, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
116 static void send_message(char **to);
117 static void send_errors(Error *err, int nerr);
118 static void do_noemailorfax(FILE *fp, Error *err, int namelen, int errtype);
119 static void do_ambiguous(FILE *fp, Error *err, int namelen);
120 static int count_values(char **list);
121 static void add_to(char ***list, int *nlist, char **new);
122 static int isgroup(LDAPMessage *e);
123 static void add_error(Error **err, int *nerr, int code, char *addr, LDAPMessage *msg);
124 static void add_group(char *dn, Group **list, int *nlist);
125 static void unbind_and_exit(int rc);
126 static int group_loop(char *dn);
127 static void send_group(Group *group, int ngroup);
128 static int has_attributes(LDAPMessage *e, char *attr1, char *attr2);
129 static char **get_attributes_mail_dn(LDAPMessage *e, char *attr1, char *attr2);
130 static char *canonical(char *s);
131 static int connect_to_x500 (void);
132 static void do_group_errors (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
133 static void do_group_request (LDAPMessage *e, char *dn, char ***to, int *nto, Error **err, int *nerr);
134 static void add_member (char *gdn, char *dn, char ***to, int *nto, Group **togroups, int *ngroups, Error **err, int *nerr);
138 main ( int argc, char **argv )
144 int numto, ngroups, numerr, nargs;
147 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
149 case 'f': /* who it's from & where errors should go */
150 mailfrom = strdup( optarg );
151 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
152 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
153 sendmailargs[j+1] = mailfrom;
159 case 'h': /* hostname */
160 host = strdup( optarg );
161 hostlen = strlen(host);
164 /* mailer-daemon address - who we should */
165 case 'm': /* say errors come from */
166 errorsfrom = strdup( optarg );
170 syslog( LOG_ALERT, "unknown option" );
175 if ( (myname = strrchr( argv[0], '/' )) == NULL )
176 myname = strdup( argv[0] );
178 myname = strdup( myname + 1 );
179 if (!strcmp(myname, "mail500")) {
181 } else if (!strcmp(myname, "fax500")) {
184 /* I give up, I must be mail500 */
189 (void) SIGNAL( SIGPIPE, SIG_IGN );
193 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
195 openlog( myname, OPENLOG_OPTIONS );
198 if ( mailfrom == NULL ) {
199 syslog( LOG_ALERT, "required argument -f not present" );
202 if ( errorsfrom == NULL ) {
203 syslog( LOG_ALERT, "required argument -m not present" );
206 if ( host == NULL ) {
207 syslog( LOG_ALERT, "required argument -h not present" );
211 if ( connect_to_x500() != 0 )
220 strcpy( buf, argv[0] );
221 for ( i = 1; i < argc; i++ ) {
223 strcat( buf, argv[i] );
226 syslog( LOG_ALERT, "args: (%s)", buf );
230 add_to( &tolist, &numto, sendmailargs );
232 ngroups = numerr = 0;
235 for ( i = optind; i < argc; i++ ) {
239 for ( j = 0; argv[i][j] != '\0'; j++ ) {
240 if ( argv[i][j] == '.' || argv[i][j] == '_' )
245 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
248 if ( strcmp( s, ERRORS ) == 0 ) {
251 } else if ( strcmp( s, REQUEST ) == 0 ) {
252 type = GROUP_REQUEST;
254 } else if ( strcmp( s, MEMBERS ) == 0 ) {
255 type = GROUP_MEMBERS;
260 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
261 &errlist, &numerr, type );
265 * If we have both errors and successful deliveries to make or if
266 * if there are any groups to deliver to, we basically need to read
267 * the message twice. So, we have to put it in a tmp file.
270 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
275 if ( (fp = tmpfile()) == NULL ) {
276 syslog( LOG_ALERT, "could not open tmp file" );
277 unbind_and_exit( EX_TEMPFAIL );
280 /* copy the message to a temp file */
281 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
282 if ( fputs( buf, fp ) == EOF ) {
283 syslog( LOG_ALERT, "error writing tmpfile" );
284 unbind_and_exit( EX_TEMPFAIL );
288 if ( dup2( fileno( fp ), 0 ) == -1 ) {
289 syslog( LOG_ALERT, "could not dup2 tmpfile" );
290 unbind_and_exit( EX_TEMPFAIL );
296 /* deal with errors */
298 (void) rewind( stdin );
299 send_errors( errlist, numerr );
302 (void) ldap_unbind( ld );
304 /* send to groups with errorsTo */
306 (void) rewind( stdin );
307 send_group( togroups, ngroups );
310 /* send to expanded aliases and groups w/o errorsTo */
311 if ( numto > nargs ) {
312 (void) rewind( stdin );
313 send_message( tolist );
320 connect_to_x500( void )
322 int sizelimit = FAX_MAXAMBIGUOUS;
323 int deref = LDAP_DEREF_ALWAYS;
325 if ( (ld = ldap_open( NULL, 0 )) == NULL ) {
326 syslog( LOG_ALERT, "ldap_open failed" );
330 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
331 ldap_set_option(ld, LDAP_OPT_DEREF, &deref);
333 if ( ldap_simple_bind_s( ld, NULL, NULL ) != LDAP_SUCCESS ) {
334 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
355 LDAPMessage *e, *res;
356 struct timeval timeout;
363 * Look up the name in X.500, add the appropriate addresses found
364 * to the to list, or to the err list in case of error. Groups are
365 * handled by the do_group routine, individuals are handled here.
366 * When looking up name, we follow the bases hierarchy, looking
367 * in base[0] first, then base[1], etc. For each base, there is
368 * a set of search filters to try, in order. If something goes
369 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
370 * If the b_rdnpref flag is set, then we give preference to entries
371 * that matched name because it's their rdn, otherwise not.
374 timeout.tv_sec = FAX_TIMEOUT;
376 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
377 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
378 char *format, *p, *argv[3];
381 for ( argc = 0; argc < 3; argc++ ) {
385 format = strdup( base[b].b_filter[f] );
386 for ( argc = 0, p = format; *p; p++ ) {
389 case 's': /* %s is the name */
393 case 'h': /* %h is the host */
400 "unknown format %c", *p );
408 /* three names ought to do... */
409 sprintf( filter, format, argv[0], argv[1], argv[2] );
413 rc = ldap_search_st( ld, base[b].b_dn,
414 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
417 /* some other trouble - try again later */
418 if ( rc != LDAP_SUCCESS &&
419 rc != LDAP_SIZELIMIT_EXCEEDED ) {
420 syslog( LOG_ALERT, "return %d from X.500", rc );
421 unbind_and_exit( EX_TEMPFAIL );
424 if ( (match = ldap_count_entries( ld, res )) != 0 )
434 /* trouble - try again later */
436 syslog( LOG_ALERT, "error parsing result from X.500" );
437 unbind_and_exit( EX_TEMPFAIL );
440 /* no matches - bounce with user unknown */
442 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
446 /* more than one match - bounce with ambiguous user? */
448 LDAPMessage *next, *tmpres = NULL;
452 /* not giving rdn preference - bounce with ambiguous user */
453 if ( base[b].b_rdnpref == 0 ) {
454 add_error( err, nerr, E_AMBIGUOUS, name, res );
459 * giving rdn preference - see if any entries were matched
460 * because of their rdn. If so, collect them to deal with
461 * later (== 1 we deliver, > 1 we bounce).
464 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
465 next = ldap_next_entry( ld, e );
466 dn = ldap_get_dn( ld, e );
467 xdn = ldap_explode_dn( dn, 1 );
469 /* XXX bad, but how else can we do it? XXX */
470 if ( strcasecmp( xdn[0], name ) == 0 ) {
471 ldap_delete_result_entry( &res, e );
472 ldap_add_result_entry( &tmpres, e );
475 ldap_value_free( xdn );
479 /* nothing matched by rdn - go ahead and bounce */
480 if ( tmpres == NULL ) {
481 add_error( err, nerr, E_AMBIGUOUS, name, res );
484 /* more than one matched by rdn - bounce with rdn matches */
485 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
486 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
490 } else if ( match < 0 ) {
491 syslog( LOG_ALERT, "error parsing result from X.500" );
492 unbind_and_exit( EX_TEMPFAIL );
495 /* otherwise one matched by rdn - send to it */
501 * if we get this far, it means that we found a single match for
502 * name. for a user, we deliver to the mail attribute or bounce
503 * with address and phone if no mail attr. for a group, we
504 * deliver to all members or bounce to rfc822ErrorsTo if no members.
508 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
509 syslog( LOG_ALERT, "error parsing entry from X.500" );
510 unbind_and_exit( EX_TEMPFAIL );
513 dn = ldap_get_dn( ld, e );
515 if ( type == GROUP_ERRORS ) {
516 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
517 do_group_errors( e, dn, to, nto, err, nerr );
519 } else if ( type == GROUP_REQUEST ) {
520 /* sent to group-request - resend to [rfc822]RequestsTo attr */
521 do_group_request( e, dn, to, nto, err, nerr );
523 } else if ( type == GROUP_MEMBERS ) {
524 /* sent to group-members - expand */
525 do_group_members( e, dn, to, nto, togroups, ngroups, err,
528 } else if ( isgroup( e ) ) {
530 * sent to group - resend from [rfc822]ErrorsTo if it's there,
531 * otherwise, expand the group
534 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
540 * sent to user - mail attribute => add it to the to list,
545 if ( (mail = ldap_get_values( ld, e,
546 "facsimileTelephoneNumber" )) != NULL ) {
551 fdn = ldap_get_dn( ld, e );
552 ufn = ldap_explode_dn( fdn, 1 );
553 /* Convert spaces to underscores for rp */
554 for (i = 0; ufn[0][i] != '\0'; i++) {
555 if (ufn[0][i] == ' ') {
559 *mail = faxtotpc(*mail, ufn[0]);
561 add_to(to, nto, mail);
566 ldap_value_free( ufn );
567 ldap_value_free( mail );
570 add_error( err, nerr, E_NOFAXNUM,
575 if ( (mail = ldap_get_values( ld, e, "mail" ))
577 add_to( to, nto, mail );
579 ldap_value_free( mail );
582 add_error( err, nerr, E_NOEMAIL,
607 * If this group has an rfc822ErrorsTo attribute, we need to
608 * arrange for errors involving this group to go there, not
609 * to the sender. Since sendmail only has the concept of a
610 * single sender, we arrange for errors to go to groupname-errors,
611 * which we then handle specially when (if) it comes back to us
612 * by expanding to all the rfc822ErrorsTo addresses. If it has no
613 * rfc822ErrorsTo attribute, we call do_group_members() to expand
617 if ( group_loop( dn ) ) {
621 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
622 add_group( dn, togroups, ngroups );
627 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
647 char **mail, **member, **joinable;
649 LDAPMessage *ee, *res;
650 struct timeval timeout;
653 * if all has gone according to plan, we've already arranged for
654 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
655 * so all we have to do here is arrange to send to the
656 * rfc822Mailbox attribute, the member attribute, and anyone who
657 * has joined the group by setting memberOfGroup equal to the
661 /* add members in the group itself - mail attribute */
662 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
664 * These are only email addresses - impossible
665 * to have a fax number
669 /* XXX How do we want to inform sender that the */
670 /* group they sent to has email-only members? */
673 add_to( to, nto, mail );
677 ldap_value_free( mail );
680 /* add members in the group itself - member attribute */
681 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
682 for ( i = 0; member[i] != NULL; i++ ) {
683 add_member( dn, member[i], to, nto, togroups,
684 ngroups, err, nerr );
687 ldap_value_free( member );
690 /* add members who have joined by setting memberOfGroup */
691 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
692 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
693 ldap_value_free( joinable );
696 ldap_value_free( joinable );
698 sprintf( filter, "(memberOfGroup=%s)", dn );
700 timeout.tv_sec = FAX_TIMEOUT;
703 /* for each subtree to look in... */
705 int sizelimit = FAX_MAXMEMBERS;
706 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
708 for ( i = 0; base[i].b_dn != NULL; i++ ) {
709 /* find entries that have joined this group... */
710 rc = ldap_search_st( ld, base[i].b_dn,
711 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
714 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
715 rc == LDAP_TIMELIMIT_EXCEEDED ) {
717 "group search limit exceeded %d", rc );
718 unbind_and_exit( EX_TEMPFAIL );
721 if ( rc != LDAP_SUCCESS ) {
722 syslog( LOG_ALERT, "group search bad return %d",
724 unbind_and_exit( EX_TEMPFAIL );
727 /* for each entry that has joined... */
728 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
729 ee = ldap_next_entry( ld, ee ) ) {
730 if ( isgroup( ee ) ) {
731 ndn = ldap_get_dn( ld, ee );
733 if ( do_group( e, ndn, to, nto,
734 togroups, ngroups, err, nerr )
737 "group loop (%s) (%s)",
746 /* add them to the to list */
749 if ( (mail = ldap_get_values( ld, ee,
750 "facsimileTelephoneNumber" ))
756 fdn = ldap_get_dn( ld, ee );
757 ufn = ldap_explode_dn( fdn, 1 );
762 for (i = 0; ufn[0][i] != '\0';
764 if (ufn[0][i] == ' ') {
768 *mail = faxtotpc(*mail, ufn[0]);
769 add_to(to, nto, mail);
771 ldap_value_free( mail );
774 ndn = ldap_get_dn( ld, ee );
776 add_error( err, nerr,
777 E_JOINMEMBERNOFAXNUM, ndn,
784 if ( (mail = ldap_get_values( ld, ee,
785 "mail" )) != NULL ) {
786 add_to( to, nto, mail );
788 ldap_value_free( mail );
790 /* else generate a bounce */
792 ndn = ldap_get_dn( ld, ee );
794 add_error( err, nerr,
795 E_JOINMEMBERNOEMAIL, ndn,
807 int sizelimit = FAX_MAXAMBIGUOUS;
808 ldap_set_option(ld, LDAP_OPT_SIZELIMIT, &sizelimit);
830 LDAPMessage *res, *e;
831 struct timeval timeout;
833 timeout.tv_sec = FAX_TIMEOUT;
835 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
836 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
837 if ( rc == LDAP_NO_SUCH_OBJECT ) {
838 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
842 syslog( LOG_ALERT, "member search bad return %d", rc );
844 unbind_and_exit( EX_TEMPFAIL );
848 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
849 syslog( LOG_ALERT, "member search error parsing entry" );
851 unbind_and_exit( EX_TEMPFAIL );
853 ndn = ldap_get_dn( ld, e );
855 /* allow groups within groups */
856 if ( isgroup( e ) ) {
857 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
859 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
869 if ( (mail = ldap_get_values( ld, e,
870 "facsimileTelephoneNumber" )) != NULL ) {
874 fdn = ldap_get_dn( ld, e );
875 ufn = ldap_explode_dn( fdn, 1 );
876 /* Convert spaces to underscores for rp */
877 for (i = 0; ufn[0][i] != '\0'; i++) {
878 if (ufn[0][i] == ' ') {
882 *mail = faxtotpc(*mail, ufn[0]);
883 add_to(to, nto, mail);
885 ldap_value_free( mail );
888 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
892 /* send to the member's mail attribute */
893 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
894 add_to( to, nto, mail );
896 ldap_value_free( mail );
898 /* else generate a bounce */
900 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
920 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
921 "requestsTo" )) != NULL ) {
922 add_to( to, nto, requeststo );
924 ldap_value_free( requeststo );
926 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
942 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
943 "errorsTo" )) != NULL ) {
944 add_to( to, nto, errorsto );
946 ldap_value_free( errorsto );
948 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
953 send_message( char **to )
957 WAITSTATUSTYPE status;
962 if ( (pid = fork()) != 0 ) {
964 waitpid( pid, (int *) NULL, 0 );
966 wait4( pid, &status, WAIT_FLAGS, 0 );
970 /* to includes sendmailargs */
971 execv( FAX_SENDMAIL, to );
973 syslog( LOG_ALERT, "execv failed" );
980 send_group( Group *group, int ngroup )
987 WAITSTATUSTYPE status;
990 for ( i = 0; i < ngroup; i++ ) {
991 (void) rewind( stdin );
993 iargv[0] = FAX_SENDMAIL;
995 iargv[2] = group[i].g_errorsto;
996 iargv[3] = "-oMrX.500";
1003 add_to( &argv, &argc, iargv );
1004 add_to( &argv, &argc, group[i].g_members );
1008 if ( (pid = fork()) != 0 ) {
1010 waitpid( pid, (int *) NULL, 0 );
1012 wait4( pid, &status, WAIT_FLAGS, 0 );
1016 execv( FAX_SENDMAIL, argv );
1018 syslog( LOG_ALERT, "execv failed" );
1020 exit( EX_TEMPFAIL );
1028 send_errors( Error *err, int nerr )
1034 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1036 if ( (fp = popen( buf, "w" )) == NULL ) {
1037 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1041 fprintf( fp, "To: %s\n", mailfrom );
1042 fprintf( fp, "From: %s\n", errorsfrom );
1043 fprintf( fp, "Subject: undeliverable mail\n" );
1044 fprintf( fp, "\n" );
1045 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1046 for ( i = 0; i < nerr; i++ ) {
1047 namelen = strlen( err[i].e_addr );
1048 fprintf( fp, "\n" );
1050 switch ( err[i].e_code ) {
1052 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1056 fprintf( fp, "%s: Group member does not exist\n",
1061 fprintf( fp, "%s: Group exists but has no request address\n",
1066 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1071 do_ambiguous( fp, &err[i], namelen );
1075 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1078 case E_MEMBERNOEMAIL:
1079 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1083 case E_JOINMEMBERNOEMAIL:
1084 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1089 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1092 case E_MEMBERNOFAXNUM:
1093 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1097 case E_JOINMEMBERNOFAXNUM:
1098 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1103 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1104 unbind_and_exit( EX_TEMPFAIL );
1109 fprintf( fp, "\n------- The original message sent:\n\n" );
1111 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1115 if ( pclose( fp ) == -1 ) {
1116 syslog( LOG_ALERT, "pclose failed" );
1117 unbind_and_exit( EX_TEMPFAIL );
1125 do_noemailorfax( FILE *fp, Error *err, int namelen, int errtype )
1131 if (errtype == E_NOFAXNUM) {
1132 fprintf(fp, "%s: User has no facsimile number registered.\n",
1134 } else if (errtype == E_NOEMAIL) {
1135 fprintf(fp, "%s: User has no email address registered.\n",
1138 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1139 namelen, " ", err->e_addr );
1142 dn = ldap_get_dn( ld, err->e_msg );
1143 ufn = ldap_explode_dn( dn, 1 );
1144 rdn = strdup( ufn[0] );
1145 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1146 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1148 for ( i = 0; vals[i]; i++ ) {
1149 last = strlen( vals[i] ) - 1;
1150 if ( isdigit((unsigned char) vals[i][last]) ) {
1151 rdn = strdup( vals[i] );
1156 ldap_value_free( vals );
1159 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1162 ldap_value_free( ufn );
1164 /* titles or descriptions */
1165 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1166 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1168 fprintf( fp, "%*s No title or description registered\n",
1171 for ( i = 0; vals[i] != NULL; i++ ) {
1172 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1175 ldap_value_free( vals );
1178 /* postal address */
1179 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1181 fprintf( fp, "%*s No postal address registered\n", namelen,
1184 fprintf( fp, "%*s ", namelen, " " );
1185 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1186 if ( vals[0][i] == '$' ) {
1187 fprintf( fp, "\n%*s ", namelen, " " );
1188 while ( isspace((unsigned char) vals[0][i+1]) )
1191 fprintf( fp, "%c", vals[0][i] );
1194 fprintf( fp, "\n" );
1196 ldap_value_free( vals );
1199 /* telephone number */
1200 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1202 fprintf( fp, "%*s No phone number registered\n", namelen,
1205 for ( i = 0; vals[i] != NULL; i++ ) {
1206 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1209 ldap_value_free( vals );
1215 do_ambiguous( FILE *fp, Error *err, int namelen )
1222 i = ldap_result2error( ld, err->e_msg, 0 );
1224 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1225 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1226 ldap_count_entries( ld, err->e_msg ) );
1228 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1229 e = ldap_next_entry( ld, e ) ) {
1230 dn = ldap_get_dn( ld, e );
1231 ufn = ldap_explode_dn( dn, 1 );
1232 rdn = strdup( ufn[0] );
1233 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1234 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1235 for ( i = 0; vals[i]; i++ ) {
1236 last = strlen( vals[i] ) - 1;
1237 if ( isdigit((unsigned char) vals[i][last]) ) {
1238 rdn = strdup( vals[i] );
1243 ldap_value_free( vals );
1247 if ( isgroup( e ) ) {
1248 vals = ldap_get_values( ld, e, "description" );
1250 vals = ldap_get_values( ld, e, "title" );
1253 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1254 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1255 fprintf( fp, " %s\n", vals[i] );
1260 ldap_value_free( ufn );
1262 ldap_value_free( vals );
1267 count_values( char **list )
1271 for ( i = 0; list && list[i] != NULL; i++ )
1278 add_to( char ***list, int *nlist, char **new )
1280 int i, nnew, oldnlist;
1282 nnew = count_values( new );
1285 if ( *list == NULL || *nlist == 0 ) {
1286 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1289 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1290 nnew * sizeof(char *) + sizeof(char *) );
1294 for ( i = 0; i < nnew; i++ ) {
1295 (*list)[i + oldnlist] = strdup( new[i] );
1297 (*list)[*nlist] = NULL;
1303 isgroup( LDAPMessage *e )
1308 oclist = ldap_get_values( ld, e, "objectClass" );
1310 for ( i = 0; oclist[i] != NULL; i++ ) {
1311 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1312 ldap_value_free( oclist );
1316 ldap_value_free( oclist );
1322 add_error( Error **err, int *nerr, int code, char *addr, LDAPMessage *msg )
1325 *err = (Error *) malloc( sizeof(Error) );
1327 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1330 (*err)[*nerr].e_code = code;
1331 (*err)[*nerr].e_addr = strdup( addr );
1332 (*err)[*nerr].e_msg = msg;
1339 add_group( char *dn, Group **list, int *nlist )
1344 for ( i = 0; i < *nlist; i++ ) {
1345 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1346 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1351 ufn = ldap_explode_dn( dn, 1 );
1352 namelen = strlen( ufn[0] );
1354 if ( *nlist == 0 ) {
1355 *list = (Group *) malloc( sizeof(Group) );
1357 *list = (Group *) realloc( *list, (*nlist + 1) *
1361 /* send errors to groupname-errors@host */
1362 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1364 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1365 (void) canonical( (*list)[*nlist].g_errorsto );
1367 /* send to groupname-members@host - make it a list for send_group */
1368 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1369 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1370 sizeof(MEMBERS) + hostlen + 2 );
1371 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1373 (void) canonical( (*list)[*nlist].g_members[0] );
1374 (*list)[*nlist].g_members[1] = NULL;
1376 /* save the group's dn so we can check for loops above */
1377 (*list)[*nlist].g_dn = strdup( dn );
1381 ldap_value_free( ufn );
1387 unbind_and_exit( int rc )
1391 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1392 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1398 canonical( char *s )
1402 for ( ; *s != '\0'; s++ ) {
1411 group_loop( char *dn )
1414 static char **groups;
1417 for ( i = 0; i < ngroups; i++ ) {
1418 if ( strcmp( dn, groups[i] ) == 0 )
1423 groups = (char **) malloc( sizeof(char *) );
1425 groups = (char **) realloc( groups,
1426 (ngroups + 1) * sizeof(char *) );
1428 groups[ngroups++] = strdup( dn );
1434 has_attributes( LDAPMessage *e, char *attr1, char *attr2 )
1438 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1439 ldap_value_free( attr );
1443 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1444 ldap_value_free( attr );
1452 get_attributes_mail_dn( LDAPMessage *e, char *attr1, char *attr2 )
1454 LDAPMessage *ee, *res;
1455 char **vals, **dnlist, **mail;
1457 struct timeval timeout;
1459 vals = ldap_get_values( ld, e, attr1 );
1460 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1463 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1464 timeout.tv_sec = FAX_TIMEOUT;
1465 timeout.tv_usec = 0;
1467 for ( i = 0; dnlist[i] != NULL; i++ ) {
1468 if ( (rc = ldap_search_st( ld, dnlist[i],
1469 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1470 &timeout, &res )) != LDAP_SUCCESS ) {
1471 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1472 unbind_and_exit( EX_TEMPFAIL );
1475 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1481 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1482 syslog( LOG_ALERT, "error parsing x500 entry" );
1486 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1488 add_to( &vals, &nto, mail );
1490 ldap_value_free( mail );
1493 ldap_msgfree( res );