2 * Copyright (c) 1990 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
18 #include <ac/socket.h>
19 #include <ac/string.h>
20 #include <ac/syslog.h>
24 #ifdef HAVE_SYS_PARAM_H
25 #include <sys/param.h>
28 #include <sys/resource.h>
34 #if LDAP_VERSION < LDAP_VERSION3
35 /* quick fix until we have ldap_set_options */
36 #include "../libraries/libldap/ldap-int.h"
39 #include <ldapconfig.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;
63 #define E_USERUNKNOWN 1
69 #define E_JOINMEMBERNOEMAIL 7
70 #define E_MEMBERNOEMAIL 8
72 #define E_JOINMEMBERNOFAXNUM 10
73 #define E_MEMBERNOFAXNUM 11
74 #define E_FAXTOEMAILMBR 12
80 typedef struct groupto {
86 typedef struct baseinfo {
87 char *b_dn; /* dn to start searching at */
88 char b_rdnpref; /* give rdn's preference when searching? */
89 char *b_filter[3]; /* filter to apply - name substituted for %s */
90 /* (up to three of them) */
94 { "ou=People, o=University of Michigan, c=US", 0,
95 "uid=%s", "cn=%s", NULL,
96 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
97 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
98 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
99 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
103 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
105 static char *attrs[] = { "objectClass", "title", "postaladdress",
106 "telephoneNumber", "mail", "description",
107 "errorsTo", "rfc822ErrorsTo", "requestsTo",
108 "rfc822RequestsTo", "joinable", "cn", "member",
109 "facsimileTelephoneNumber", NULL };
113 static do_group_members();
114 static send_message();
115 static send_errors();
116 static do_noemailorfax();
117 static do_ambiguous();
122 static unbind_and_exit();
125 static has_attributes();
126 static char **get_attributes_mail_dn();
127 static char *canonical();
137 int numto, ngroups, numerr, nargs;
140 extern int optind, errno;
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 );
313 if ( (ld = ldap_open( LDAPHOST, LDAP_PORT )) == NULL ) {
314 syslog( LOG_ALERT, "ldap_open failed" );
317 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
318 ld->ld_deref = LDAP_DEREF_ALWAYS;
320 if ( ldap_simple_bind_s( ld, FAX_BINDDN, FAX_BIND_CRED ) != LDAP_SUCCESS ) {
321 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
330 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
341 LDAPMessage *e, *res;
342 struct timeval timeout;
349 * Look up the name in X.500, add the appropriate addresses found
350 * to the to list, or to the err list in case of error. Groups are
351 * handled by the do_group routine, individuals are handled here.
352 * When looking up name, we follow the bases hierarchy, looking
353 * in base[0] first, then base[1], etc. For each base, there is
354 * a set of search filters to try, in order. If something goes
355 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
356 * If the b_rdnpref flag is set, then we give preference to entries
357 * that matched name because it's their rdn, otherwise not.
360 timeout.tv_sec = FAX_TIMEOUT;
362 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
363 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
364 char *format, *p, *argv[3];
367 for ( argc = 0; argc < 3; argc++ ) {
371 format = strdup( base[b].b_filter[f] );
372 for ( argc = 0, p = format; *p; p++ ) {
375 case 's': /* %s is the name */
379 case 'h': /* %h is the host */
386 "unknown format %c", *p );
394 /* three names ought to do... */
395 sprintf( filter, format, argv[0], argv[1], argv[2] );
399 rc = ldap_search_st( ld, base[b].b_dn,
400 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
403 /* some other trouble - try again later */
404 if ( rc != LDAP_SUCCESS &&
405 rc != LDAP_SIZELIMIT_EXCEEDED ) {
406 syslog( LOG_ALERT, "return %d from X.500", rc );
407 unbind_and_exit( EX_TEMPFAIL );
410 if ( (match = ldap_count_entries( ld, res )) != 0 )
420 /* trouble - try again later */
422 syslog( LOG_ALERT, "error parsing result from X.500" );
423 unbind_and_exit( EX_TEMPFAIL );
426 /* no matches - bounce with user unknown */
428 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
432 /* more than one match - bounce with ambiguous user? */
434 LDAPMessage *next, *tmpres = NULL;
438 /* not giving rdn preference - bounce with ambiguous user */
439 if ( base[b].b_rdnpref == 0 ) {
440 add_error( err, nerr, E_AMBIGUOUS, name, res );
445 * giving rdn preference - see if any entries were matched
446 * because of their rdn. If so, collect them to deal with
447 * later (== 1 we deliver, > 1 we bounce).
450 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
451 next = ldap_next_entry( ld, e );
452 dn = ldap_get_dn( ld, e );
453 xdn = ldap_explode_dn( dn, 1 );
455 /* XXX bad, but how else can we do it? XXX */
456 if ( strcasecmp( xdn[0], name ) == 0 ) {
457 ldap_delete_result_entry( &res, e );
458 ldap_add_result_entry( &tmpres, e );
461 ldap_value_free( xdn );
465 /* nothing matched by rdn - go ahead and bounce */
466 if ( tmpres == NULL ) {
467 add_error( err, nerr, E_AMBIGUOUS, name, res );
470 /* more than one matched by rdn - bounce with rdn matches */
471 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
472 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
476 } else if ( match < 0 ) {
477 syslog( LOG_ALERT, "error parsing result from X.500" );
478 unbind_and_exit( EX_TEMPFAIL );
481 /* otherwise one matched by rdn - send to it */
487 * if we get this far, it means that we found a single match for
488 * name. for a user, we deliver to the mail attribute or bounce
489 * with address and phone if no mail attr. for a group, we
490 * deliver to all members or bounce to rfc822ErrorsTo if no members.
494 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
495 syslog( LOG_ALERT, "error parsing entry from X.500" );
496 unbind_and_exit( EX_TEMPFAIL );
499 dn = ldap_get_dn( ld, e );
501 if ( type == GROUP_ERRORS ) {
502 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
503 do_group_errors( e, dn, to, nto, err, nerr );
505 } else if ( type == GROUP_REQUEST ) {
506 /* sent to group-request - resend to [rfc822]RequestsTo attr */
507 do_group_request( e, dn, to, nto, err, nerr );
509 } else if ( type == GROUP_MEMBERS ) {
510 /* sent to group-members - expand */
511 do_group_members( e, dn, to, nto, togroups, ngroups, err,
514 } else if ( isgroup( e ) ) {
516 * sent to group - resend from [rfc822]ErrorsTo if it's there,
517 * otherwise, expand the group
520 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
526 * sent to user - mail attribute => add it to the to list,
531 if ( (mail = ldap_get_values( ld, e,
532 "facsimileTelephoneNumber" )) != NULL ) {
537 fdn = ldap_get_dn( ld, e );
538 ufn = ldap_explode_dn( fdn, 1 );
539 /* Convert spaces to underscores for rp */
540 for (i = 0; ufn[0][i] != '\0'; i++) {
541 if (ufn[0][i] == ' ') {
545 *mail = faxtotpc(*mail, ufn[0]);
547 add_to(to, nto, mail);
552 ldap_value_free( ufn );
553 ldap_value_free( mail );
556 add_error( err, nerr, E_NOFAXNUM,
561 if ( (mail = ldap_get_values( ld, e, "mail" ))
563 add_to( to, nto, mail );
565 ldap_value_free( mail );
568 add_error( err, nerr, E_NOEMAIL,
581 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
592 * If this group has an rfc822ErrorsTo attribute, we need to
593 * arrange for errors involving this group to go there, not
594 * to the sender. Since sendmail only has the concept of a
595 * single sender, we arrange for errors to go to groupname-errors,
596 * which we then handle specially when (if) it comes back to us
597 * by expanding to all the rfc822ErrorsTo addresses. If it has no
598 * rfc822ErrorsTo attribute, we call do_group_members() to expand
602 if ( group_loop( dn ) ) {
606 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
607 add_group( dn, togroups, ngroups );
612 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
619 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
631 char **mail, **member, **joinable;
633 LDAPMessage *ee, *res;
634 struct timeval timeout;
637 * if all has gone according to plan, we've already arranged for
638 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
639 * so all we have to do here is arrange to send to the
640 * rfc822Mailbox attribute, the member attribute, and anyone who
641 * has joined the group by setting memberOfGroup equal to the
645 /* add members in the group itself - mail attribute */
646 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
648 * These are only email addresses - impossible
649 * to have a fax number
653 /* XXX How do we want to inform sender that the */
654 /* group they sent to has email-only members? */
657 add_to( to, nto, mail );
661 ldap_value_free( mail );
664 /* add members in the group itself - member attribute */
665 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
666 for ( i = 0; member[i] != NULL; i++ ) {
667 add_member( dn, member[i], to, nto, togroups,
668 ngroups, err, nerr );
671 ldap_value_free( member );
674 /* add members who have joined by setting memberOfGroup */
675 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
676 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
677 ldap_value_free( joinable );
680 ldap_value_free( joinable );
682 sprintf( filter, "(memberOfGroup=%s)", dn );
684 timeout.tv_sec = FAX_TIMEOUT;
687 /* for each subtree to look in... */
688 ld->ld_sizelimit = FAX_MAXMEMBERS;
689 for ( i = 0; base[i].b_dn != NULL; i++ ) {
690 /* find entries that have joined this group... */
691 rc = ldap_search_st( ld, base[i].b_dn,
692 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
695 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
696 rc == LDAP_TIMELIMIT_EXCEEDED ) {
698 "group search limit exceeded %d", rc );
699 unbind_and_exit( EX_TEMPFAIL );
702 if ( rc != LDAP_SUCCESS ) {
703 syslog( LOG_ALERT, "group search bad return %d",
705 unbind_and_exit( EX_TEMPFAIL );
708 /* for each entry that has joined... */
709 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
710 ee = ldap_next_entry( ld, ee ) ) {
711 if ( isgroup( ee ) ) {
712 ndn = ldap_get_dn( ld, ee );
714 if ( do_group( e, ndn, to, nto,
715 togroups, ngroups, err, nerr )
718 "group loop (%s) (%s)",
727 /* add them to the to list */
730 if ( (mail = ldap_get_values( ld, ee,
731 "facsimileTelephoneNumber" ))
737 fdn = ldap_get_dn( ld, ee );
738 ufn = ldap_explode_dn( fdn, 1 );
743 for (i = 0; ufn[0][i] != '\0';
745 if (ufn[0][i] == ' ') {
749 *mail = faxtotpc(*mail, ufn[0]);
750 add_to(to, nto, mail);
752 ldap_value_free( mail );
755 ndn = ldap_get_dn( ld, ee );
757 add_error( err, nerr,
758 E_JOINMEMBERNOFAXNUM, ndn,
765 if ( (mail = ldap_get_values( ld, ee,
766 "mail" )) != NULL ) {
767 add_to( to, nto, mail );
769 ldap_value_free( mail );
771 /* else generate a bounce */
773 ndn = ldap_get_dn( ld, ee );
775 add_error( err, nerr,
776 E_JOINMEMBERNOEMAIL, ndn,
787 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
793 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr )
806 LDAPMessage *res, *e;
807 struct timeval timeout;
809 timeout.tv_sec = FAX_TIMEOUT;
811 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
812 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
813 if ( rc == LDAP_NO_SUCH_OBJECT ) {
814 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
818 syslog( LOG_ALERT, "member search bad return %d", rc );
820 unbind_and_exit( EX_TEMPFAIL );
824 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
825 syslog( LOG_ALERT, "member search error parsing entry" );
827 unbind_and_exit( EX_TEMPFAIL );
829 ndn = ldap_get_dn( ld, e );
831 /* allow groups within groups */
832 if ( isgroup( e ) ) {
833 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
835 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
845 if ( (mail = ldap_get_values( ld, e,
846 "facsimileTelephoneNumber" )) != NULL ) {
850 fdn = ldap_get_dn( ld, e );
851 ufn = ldap_explode_dn( fdn, 1 );
852 /* Convert spaces to underscores for rp */
853 for (i = 0; ufn[0][i] != '\0'; i++) {
854 if (ufn[0][i] == ' ') {
858 *mail = faxtotpc(*mail, ufn[0]);
859 add_to(to, nto, mail);
861 ldap_value_free( mail );
864 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
868 /* send to the member's mail attribute */
869 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
870 add_to( to, nto, mail );
872 ldap_value_free( mail );
874 /* else generate a bounce */
876 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
884 do_group_request( e, dn, to, nto, err, nerr )
894 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
895 "requestsTo" )) != NULL ) {
896 add_to( to, nto, requeststo );
898 ldap_value_free( requeststo );
900 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
906 do_group_errors( e, dn, to, nto, err, nerr )
916 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
917 "errorsTo" )) != NULL ) {
918 add_to( to, nto, errorsto );
920 ldap_value_free( errorsto );
922 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
934 WAITSTATUSTYPE status;
939 if ( pid = fork() ) {
941 waitpid( pid, (int *) NULL, 0 );
943 wait4( pid, &status, WAIT_FLAGS, 0 );
947 /* to includes sendmailargs */
948 execv( FAX_SENDMAIL, to );
950 syslog( LOG_ALERT, "execv failed" );
957 send_group( group, ngroup )
966 WAITSTATUSTYPE status;
969 for ( i = 0; i < ngroup; i++ ) {
970 (void) rewind( stdin );
972 iargv[0] = FAX_SENDMAIL;
974 iargv[2] = group[i].g_errorsto;
975 iargv[3] = "-oMrX.500";
982 add_to( &argv, &argc, iargv );
983 add_to( &argv, &argc, group[i].g_members );
987 if ( pid = fork() ) {
989 waitpid( pid, (int *) NULL, 0 );
991 wait4( pid, &status, WAIT_FLAGS, 0 );
995 execv( FAX_SENDMAIL, argv );
997 syslog( LOG_ALERT, "execv failed" );
1007 send_errors( err, nerr )
1015 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1017 if ( (fp = popen( buf, "w" )) == NULL ) {
1018 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1022 fprintf( fp, "To: %s\n", mailfrom );
1023 fprintf( fp, "From: %s\n", errorsfrom );
1024 fprintf( fp, "Subject: undeliverable mail\n" );
1025 fprintf( fp, "\n" );
1026 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1027 for ( i = 0; i < nerr; i++ ) {
1028 namelen = strlen( err[i].e_addr );
1029 fprintf( fp, "\n" );
1031 switch ( err[i].e_code ) {
1033 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1037 fprintf( fp, "%s: Group member does not exist\n",
1042 fprintf( fp, "%s: Group exists but has no request address\n",
1047 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1052 do_ambiguous( fp, &err[i], namelen );
1056 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1059 case E_MEMBERNOEMAIL:
1060 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1064 case E_JOINMEMBERNOEMAIL:
1065 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1070 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1073 case E_MEMBERNOFAXNUM:
1074 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1078 case E_JOINMEMBERNOFAXNUM:
1079 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1084 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1085 unbind_and_exit( EX_TEMPFAIL );
1090 fprintf( fp, "\n------- The original message sent:\n\n" );
1092 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1096 if ( pclose( fp ) == -1 ) {
1097 syslog( LOG_ALERT, "pclose failed" );
1098 unbind_and_exit( EX_TEMPFAIL );
1106 do_noemailorfax( fp, err, namelen, errtype )
1116 if (errtype == E_NOFAXNUM) {
1117 fprintf(fp, "%s: User has no facsimile number registered.\n",
1119 } else if (errtype == E_NOEMAIL) {
1120 fprintf(fp, "%s: User has no email address registered.\n",
1123 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1124 namelen, " ", err->e_addr );
1127 dn = ldap_get_dn( ld, err->e_msg );
1128 ufn = ldap_explode_dn( dn, 1 );
1129 rdn = strdup( ufn[0] );
1130 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1131 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1133 for ( i = 0; vals[i]; i++ ) {
1134 last = strlen( vals[i] ) - 1;
1135 if ( isdigit( vals[i][last] ) ) {
1136 rdn = strdup( vals[i] );
1141 ldap_value_free( vals );
1144 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1147 ldap_value_free( ufn );
1149 /* titles or descriptions */
1150 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1151 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1153 fprintf( fp, "%*s No title or description registered\n",
1156 for ( i = 0; vals[i] != NULL; i++ ) {
1157 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1160 ldap_value_free( vals );
1163 /* postal address */
1164 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1166 fprintf( fp, "%*s No postal address registered\n", namelen,
1169 fprintf( fp, "%*s ", namelen, " " );
1170 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1171 if ( vals[0][i] == '$' ) {
1172 fprintf( fp, "\n%*s ", namelen, " " );
1173 while ( isspace( vals[0][i+1] ) )
1176 fprintf( fp, "%c", vals[0][i] );
1179 fprintf( fp, "\n" );
1181 ldap_value_free( vals );
1184 /* telephone number */
1185 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1187 fprintf( fp, "%*s No phone number registered\n", namelen,
1190 for ( i = 0; vals[i] != NULL; i++ ) {
1191 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1194 ldap_value_free( vals );
1200 do_ambiguous( fp, err, namelen )
1210 i = ldap_result2error( ld, err->e_msg, 0 );
1212 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1213 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1214 ldap_count_entries( ld, err->e_msg ) );
1216 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1217 e = ldap_next_entry( ld, e ) ) {
1218 dn = ldap_get_dn( ld, e );
1219 ufn = ldap_explode_dn( dn, 1 );
1220 rdn = strdup( ufn[0] );
1221 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1222 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1223 for ( i = 0; vals[i]; i++ ) {
1224 last = strlen( vals[i] ) - 1;
1225 if ( isdigit( vals[i][last] ) ) {
1226 rdn = strdup( vals[i] );
1231 ldap_value_free( vals );
1235 if ( isgroup( e ) ) {
1236 vals = ldap_get_values( ld, e, "description" );
1238 vals = ldap_get_values( ld, e, "title" );
1241 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1242 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1243 fprintf( fp, " %s\n", vals[i] );
1248 ldap_value_free( ufn );
1250 ldap_value_free( vals );
1255 count_values( list )
1260 for ( i = 0; list && list[i] != NULL; i++ )
1267 add_to( list, nlist, 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;
1301 oclist = ldap_get_values( ld, e, "objectClass" );
1303 for ( i = 0; oclist[i] != NULL; i++ ) {
1304 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1305 ldap_value_free( oclist );
1309 ldap_value_free( oclist );
1315 add_error( err, nerr, code, addr, 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( dn, list, 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( rc )
1393 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1394 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1405 for ( ; *s != '\0'; s++ ) {
1418 static char **groups;
1421 for ( i = 0; i < ngroups; i++ ) {
1422 if ( strcmp( dn, groups[i] ) == 0 )
1427 groups = (char **) malloc( sizeof(char *) );
1429 groups = (char **) realloc( groups,
1430 (ngroups + 1) * sizeof(char *) );
1432 groups[ngroups++] = strdup( dn );
1438 has_attributes( e, attr1, attr2 )
1445 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1446 ldap_value_free( attr );
1450 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1451 ldap_value_free( attr );
1459 get_attributes_mail_dn( e, attr1, attr2 )
1462 char *attr2; /* this one is dn-valued */
1464 LDAPMessage *ee, *res;
1465 char **vals, **dnlist, **mail;
1467 struct timeval timeout;
1469 vals = ldap_get_values( ld, e, attr1 );
1470 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1473 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1474 timeout.tv_sec = FAX_TIMEOUT;
1475 timeout.tv_usec = 0;
1477 for ( i = 0; dnlist[i] != NULL; i++ ) {
1478 if ( (rc = ldap_search_st( ld, dnlist[i],
1479 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1480 &timeout, &res )) != LDAP_SUCCESS ) {
1481 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1482 unbind_and_exit( EX_TEMPFAIL );
1485 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1491 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1492 syslog( LOG_ALERT, "error parsing x500 entry" );
1496 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1498 add_to( &vals, &nto, mail );
1500 ldap_value_free( mail );
1503 ldap_msgfree( res );