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>
33 #include <ldapconfig.h>
36 #define GROUP_ERRORS 1
37 #define GROUP_REQUEST 2
38 #define GROUP_MEMBERS 3
40 #define ERRORS "errors"
41 #define REQUEST "request"
42 #define MEMBERS "members"
45 char *errorsfrom = NULL;
46 char *mailfrom = NULL;
57 #define E_USERUNKNOWN 1
63 #define E_JOINMEMBERNOEMAIL 7
64 #define E_MEMBERNOEMAIL 8
66 #define E_JOINMEMBERNOFAXNUM 10
67 #define E_MEMBERNOFAXNUM 11
68 #define E_FAXTOEMAILMBR 12
74 typedef struct groupto {
80 typedef struct baseinfo {
81 char *b_dn; /* dn to start searching at */
82 char b_rdnpref; /* give rdn's preference when searching? */
83 char *b_filter[3]; /* filter to apply - name substituted for %s */
84 /* (up to three of them) */
88 { "ou=People, o=University of Michigan, c=US", 0,
89 "uid=%s", "cn=%s", NULL,
90 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
91 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
92 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
93 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
97 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
99 static char *attrs[] = { "objectClass", "title", "postaladdress",
100 "telephoneNumber", "mail", "description",
101 "errorsTo", "rfc822ErrorsTo", "requestsTo",
102 "rfc822RequestsTo", "joinable", "cn", "member",
103 "facsimileTelephoneNumber", NULL };
107 static do_group_members();
108 static send_message();
109 static send_errors();
110 static do_noemailorfax();
111 static do_ambiguous();
116 static unbind_and_exit();
119 static has_attributes();
120 static char **get_attributes_mail_dn();
121 static char *canonical();
131 int numto, ngroups, numerr, nargs;
134 extern int optind, errno;
137 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
139 case 'f': /* who it's from & where errors should go */
140 mailfrom = strdup( optarg );
141 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
142 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
143 sendmailargs[j+1] = mailfrom;
149 case 'h': /* hostname */
150 host = strdup( optarg );
151 hostlen = strlen(host);
154 /* mailer-daemon address - who we should */
155 case 'm': /* say errors come from */
156 errorsfrom = strdup( optarg );
160 syslog( LOG_ALERT, "unknown option" );
165 if ( (myname = strrchr( argv[0], '/' )) == NULL )
166 myname = strdup( argv[0] );
168 myname = strdup( myname + 1 );
169 if (!strcmp(myname, "mail500")) {
171 } else if (!strcmp(myname, "fax500")) {
174 /* I give up, I must be mail500 */
179 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
181 openlog( myname, OPENLOG_OPTIONS );
184 if ( mailfrom == NULL ) {
185 syslog( LOG_ALERT, "required argument -f not present" );
188 if ( errorsfrom == NULL ) {
189 syslog( LOG_ALERT, "required argument -m not present" );
192 if ( host == NULL ) {
193 syslog( LOG_ALERT, "required argument -h not present" );
197 if ( connect_to_x500() != 0 )
206 strcpy( buf, argv[0] );
207 for ( i = 1; i < argc; i++ ) {
209 strcat( buf, argv[i] );
212 syslog( LOG_ALERT, "args: (%s)", buf );
216 add_to( &tolist, &numto, sendmailargs );
218 ngroups = numerr = 0;
221 for ( i = optind; i < argc; i++ ) {
225 for ( j = 0; argv[i][j] != '\0'; j++ ) {
226 if ( argv[i][j] == '.' || argv[i][j] == '_' )
231 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
234 if ( strcmp( s, ERRORS ) == 0 ) {
237 } else if ( strcmp( s, REQUEST ) == 0 ) {
238 type = GROUP_REQUEST;
240 } else if ( strcmp( s, MEMBERS ) == 0 ) {
241 type = GROUP_MEMBERS;
246 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
247 &errlist, &numerr, type );
251 * If we have both errors and successful deliveries to make or if
252 * if there are any groups to deliver to, we basically need to read
253 * the message twice. So, we have to put it in a tmp file.
256 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
261 if ( (fp = tmpfile()) == NULL ) {
262 syslog( LOG_ALERT, "could not open tmp file" );
263 unbind_and_exit( EX_TEMPFAIL );
266 /* copy the message to a temp file */
267 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
268 if ( fputs( buf, fp ) == EOF ) {
269 syslog( LOG_ALERT, "error writing tmpfile" );
270 unbind_and_exit( EX_TEMPFAIL );
274 if ( dup2( fileno( fp ), 0 ) == -1 ) {
275 syslog( LOG_ALERT, "could not dup2 tmpfile" );
276 unbind_and_exit( EX_TEMPFAIL );
282 /* deal with errors */
284 (void) rewind( stdin );
285 send_errors( errlist, numerr );
288 (void) ldap_unbind( ld );
290 /* send to groups with errorsTo */
292 (void) rewind( stdin );
293 send_group( togroups, ngroups );
296 /* send to expanded aliases and groups w/o errorsTo */
297 if ( numto > nargs ) {
298 (void) rewind( stdin );
299 send_message( tolist );
307 if ( (ld = ldap_open( LDAPHOST, LDAP_PORT )) == NULL ) {
308 syslog( LOG_ALERT, "ldap_open failed" );
311 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
312 ld->ld_deref = LDAP_DEREF_ALWAYS;
314 if ( ldap_simple_bind_s( ld, FAX_BINDDN, FAX_BIND_CRED ) != LDAP_SUCCESS ) {
315 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
324 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
335 LDAPMessage *e, *res;
336 struct timeval timeout;
343 * Look up the name in X.500, add the appropriate addresses found
344 * to the to list, or to the err list in case of error. Groups are
345 * handled by the do_group routine, individuals are handled here.
346 * When looking up name, we follow the bases hierarchy, looking
347 * in base[0] first, then base[1], etc. For each base, there is
348 * a set of search filters to try, in order. If something goes
349 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
350 * If the b_rdnpref flag is set, then we give preference to entries
351 * that matched name because it's their rdn, otherwise not.
354 timeout.tv_sec = FAX_TIMEOUT;
356 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
357 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
358 char *format, *p, *argv[3];
361 for ( argc = 0; argc < 3; argc++ ) {
365 format = strdup( base[b].b_filter[f] );
366 for ( argc = 0, p = format; *p; p++ ) {
369 case 's': /* %s is the name */
373 case 'h': /* %h is the host */
380 "unknown format %c", *p );
388 /* three names ought to do... */
389 sprintf( filter, format, argv[0], argv[1], argv[2] );
393 rc = ldap_search_st( ld, base[b].b_dn,
394 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
397 /* some other trouble - try again later */
398 if ( rc != LDAP_SUCCESS &&
399 rc != LDAP_SIZELIMIT_EXCEEDED ) {
400 syslog( LOG_ALERT, "return %d from X.500", rc );
401 unbind_and_exit( EX_TEMPFAIL );
404 if ( (match = ldap_count_entries( ld, res )) != 0 )
414 /* trouble - try again later */
416 syslog( LOG_ALERT, "error parsing result from X.500" );
417 unbind_and_exit( EX_TEMPFAIL );
420 /* no matches - bounce with user unknown */
422 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
426 /* more than one match - bounce with ambiguous user? */
428 LDAPMessage *next, *tmpres = NULL;
432 /* not giving rdn preference - bounce with ambiguous user */
433 if ( base[b].b_rdnpref == 0 ) {
434 add_error( err, nerr, E_AMBIGUOUS, name, res );
439 * giving rdn preference - see if any entries were matched
440 * because of their rdn. If so, collect them to deal with
441 * later (== 1 we deliver, > 1 we bounce).
444 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
445 next = ldap_next_entry( ld, e );
446 dn = ldap_get_dn( ld, e );
447 xdn = ldap_explode_dn( dn, 1 );
449 /* XXX bad, but how else can we do it? XXX */
450 if ( strcasecmp( xdn[0], name ) == 0 ) {
451 ldap_delete_result_entry( &res, e );
452 ldap_add_result_entry( &tmpres, e );
455 ldap_value_free( xdn );
459 /* nothing matched by rdn - go ahead and bounce */
460 if ( tmpres == NULL ) {
461 add_error( err, nerr, E_AMBIGUOUS, name, res );
464 /* more than one matched by rdn - bounce with rdn matches */
465 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
466 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
470 } else if ( match < 0 ) {
471 syslog( LOG_ALERT, "error parsing result from X.500" );
472 unbind_and_exit( EX_TEMPFAIL );
475 /* otherwise one matched by rdn - send to it */
481 * if we get this far, it means that we found a single match for
482 * name. for a user, we deliver to the mail attribute or bounce
483 * with address and phone if no mail attr. for a group, we
484 * deliver to all members or bounce to rfc822ErrorsTo if no members.
488 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
489 syslog( LOG_ALERT, "error parsing entry from X.500" );
490 unbind_and_exit( EX_TEMPFAIL );
493 dn = ldap_get_dn( ld, e );
495 if ( type == GROUP_ERRORS ) {
496 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
497 do_group_errors( e, dn, to, nto, err, nerr );
499 } else if ( type == GROUP_REQUEST ) {
500 /* sent to group-request - resend to [rfc822]RequestsTo attr */
501 do_group_request( e, dn, to, nto, err, nerr );
503 } else if ( type == GROUP_MEMBERS ) {
504 /* sent to group-members - expand */
505 do_group_members( e, dn, to, nto, togroups, ngroups, err,
508 } else if ( isgroup( e ) ) {
510 * sent to group - resend from [rfc822]ErrorsTo if it's there,
511 * otherwise, expand the group
514 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
520 * sent to user - mail attribute => add it to the to list,
525 if ( (mail = ldap_get_values( ld, e,
526 "facsimileTelephoneNumber" )) != NULL ) {
531 fdn = ldap_get_dn( ld, e );
532 ufn = ldap_explode_dn( fdn, 1 );
533 /* Convert spaces to underscores for rp */
534 for (i = 0; ufn[0][i] != '\0'; i++) {
535 if (ufn[0][i] == ' ') {
539 *mail = faxtotpc(*mail, ufn[0]);
541 add_to(to, nto, mail);
546 ldap_value_free( ufn );
547 ldap_value_free( mail );
550 add_error( err, nerr, E_NOFAXNUM,
555 if ( (mail = ldap_get_values( ld, e, "mail" ))
557 add_to( to, nto, mail );
559 ldap_value_free( mail );
562 add_error( err, nerr, E_NOEMAIL,
575 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
586 * If this group has an rfc822ErrorsTo attribute, we need to
587 * arrange for errors involving this group to go there, not
588 * to the sender. Since sendmail only has the concept of a
589 * single sender, we arrange for errors to go to groupname-errors,
590 * which we then handle specially when (if) it comes back to us
591 * by expanding to all the rfc822ErrorsTo addresses. If it has no
592 * rfc822ErrorsTo attribute, we call do_group_members() to expand
596 if ( group_loop( dn ) ) {
600 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
601 add_group( dn, togroups, ngroups );
606 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
613 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
625 char **mail, **member, **joinable;
627 LDAPMessage *ee, *res;
628 struct timeval timeout;
631 * if all has gone according to plan, we've already arranged for
632 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
633 * so all we have to do here is arrange to send to the
634 * rfc822Mailbox attribute, the member attribute, and anyone who
635 * has joined the group by setting memberOfGroup equal to the
639 /* add members in the group itself - mail attribute */
640 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
642 * These are only email addresses - impossible
643 * to have a fax number
647 /* XXX How do we want to inform sender that the */
648 /* group they sent to has email-only members? */
651 add_to( to, nto, mail );
655 ldap_value_free( mail );
658 /* add members in the group itself - member attribute */
659 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
660 for ( i = 0; member[i] != NULL; i++ ) {
661 add_member( dn, member[i], to, nto, togroups,
662 ngroups, err, nerr );
665 ldap_value_free( member );
668 /* add members who have joined by setting memberOfGroup */
669 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
670 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
671 ldap_value_free( joinable );
674 ldap_value_free( joinable );
676 sprintf( filter, "(memberOfGroup=%s)", dn );
678 timeout.tv_sec = FAX_TIMEOUT;
681 /* for each subtree to look in... */
682 ld->ld_sizelimit = FAX_MAXMEMBERS;
683 for ( i = 0; base[i].b_dn != NULL; i++ ) {
684 /* find entries that have joined this group... */
685 rc = ldap_search_st( ld, base[i].b_dn,
686 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
689 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
690 rc == LDAP_TIMELIMIT_EXCEEDED ) {
692 "group search limit exceeded %d", rc );
693 unbind_and_exit( EX_TEMPFAIL );
696 if ( rc != LDAP_SUCCESS ) {
697 syslog( LOG_ALERT, "group search bad return %d",
699 unbind_and_exit( EX_TEMPFAIL );
702 /* for each entry that has joined... */
703 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
704 ee = ldap_next_entry( ld, ee ) ) {
705 if ( isgroup( ee ) ) {
706 ndn = ldap_get_dn( ld, ee );
708 if ( do_group( e, ndn, to, nto,
709 togroups, ngroups, err, nerr )
712 "group loop (%s) (%s)",
721 /* add them to the to list */
724 if ( (mail = ldap_get_values( ld, ee,
725 "facsimileTelephoneNumber" ))
731 fdn = ldap_get_dn( ld, ee );
732 ufn = ldap_explode_dn( fdn, 1 );
737 for (i = 0; ufn[0][i] != '\0';
739 if (ufn[0][i] == ' ') {
743 *mail = faxtotpc(*mail, ufn[0]);
744 add_to(to, nto, mail);
746 ldap_value_free( mail );
749 ndn = ldap_get_dn( ld, ee );
751 add_error( err, nerr,
752 E_JOINMEMBERNOFAXNUM, ndn,
759 if ( (mail = ldap_get_values( ld, ee,
760 "mail" )) != NULL ) {
761 add_to( to, nto, mail );
763 ldap_value_free( mail );
765 /* else generate a bounce */
767 ndn = ldap_get_dn( ld, ee );
769 add_error( err, nerr,
770 E_JOINMEMBERNOEMAIL, ndn,
781 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
787 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr )
800 LDAPMessage *res, *e;
801 struct timeval timeout;
803 timeout.tv_sec = FAX_TIMEOUT;
805 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
806 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
807 if ( rc == LDAP_NO_SUCH_OBJECT ) {
808 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
812 syslog( LOG_ALERT, "member search bad return %d", rc );
814 unbind_and_exit( EX_TEMPFAIL );
818 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
819 syslog( LOG_ALERT, "member search error parsing entry" );
821 unbind_and_exit( EX_TEMPFAIL );
823 ndn = ldap_get_dn( ld, e );
825 /* allow groups within groups */
826 if ( isgroup( e ) ) {
827 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
829 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
839 if ( (mail = ldap_get_values( ld, e,
840 "facsimileTelephoneNumber" )) != NULL ) {
844 fdn = ldap_get_dn( ld, e );
845 ufn = ldap_explode_dn( fdn, 1 );
846 /* Convert spaces to underscores for rp */
847 for (i = 0; ufn[0][i] != '\0'; i++) {
848 if (ufn[0][i] == ' ') {
852 *mail = faxtotpc(*mail, ufn[0]);
853 add_to(to, nto, mail);
855 ldap_value_free( mail );
858 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
862 /* send to the member's mail attribute */
863 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
864 add_to( to, nto, mail );
866 ldap_value_free( mail );
868 /* else generate a bounce */
870 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
878 do_group_request( e, dn, to, nto, err, nerr )
888 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
889 "requestsTo" )) != NULL ) {
890 add_to( to, nto, requeststo );
892 ldap_value_free( requeststo );
894 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
900 do_group_errors( e, dn, to, nto, err, nerr )
910 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
911 "errorsTo" )) != NULL ) {
912 add_to( to, nto, errorsto );
914 ldap_value_free( errorsto );
916 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
928 WAITSTATUSTYPE status;
933 if ( pid = fork() ) {
935 waitpid( pid, (int *) NULL, 0 );
937 wait4( pid, &status, WAIT_FLAGS, 0 );
941 /* to includes sendmailargs */
942 execv( FAX_SENDMAIL, to );
944 syslog( LOG_ALERT, "execv failed" );
951 send_group( group, ngroup )
960 WAITSTATUSTYPE status;
963 for ( i = 0; i < ngroup; i++ ) {
964 (void) rewind( stdin );
966 iargv[0] = FAX_SENDMAIL;
968 iargv[2] = group[i].g_errorsto;
969 iargv[3] = "-oMrX.500";
976 add_to( &argv, &argc, iargv );
977 add_to( &argv, &argc, group[i].g_members );
981 if ( pid = fork() ) {
983 waitpid( pid, (int *) NULL, 0 );
985 wait4( pid, &status, WAIT_FLAGS, 0 );
989 execv( FAX_SENDMAIL, argv );
991 syslog( LOG_ALERT, "execv failed" );
1001 send_errors( err, nerr )
1009 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1011 if ( (fp = popen( buf, "w" )) == NULL ) {
1012 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1016 fprintf( fp, "To: %s\n", mailfrom );
1017 fprintf( fp, "From: %s\n", errorsfrom );
1018 fprintf( fp, "Subject: undeliverable mail\n" );
1019 fprintf( fp, "\n" );
1020 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1021 for ( i = 0; i < nerr; i++ ) {
1022 namelen = strlen( err[i].e_addr );
1023 fprintf( fp, "\n" );
1025 switch ( err[i].e_code ) {
1027 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1031 fprintf( fp, "%s: Group member does not exist\n",
1036 fprintf( fp, "%s: Group exists but has no request address\n",
1041 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1046 do_ambiguous( fp, &err[i], namelen );
1050 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1053 case E_MEMBERNOEMAIL:
1054 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1058 case E_JOINMEMBERNOEMAIL:
1059 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1064 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1067 case E_MEMBERNOFAXNUM:
1068 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1072 case E_JOINMEMBERNOFAXNUM:
1073 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1078 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1079 unbind_and_exit( EX_TEMPFAIL );
1084 fprintf( fp, "\n------- The original message sent:\n\n" );
1086 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1090 if ( pclose( fp ) == -1 ) {
1091 syslog( LOG_ALERT, "pclose failed" );
1092 unbind_and_exit( EX_TEMPFAIL );
1100 do_noemailorfax( fp, err, namelen, errtype )
1110 if (errtype == E_NOFAXNUM) {
1111 fprintf(fp, "%s: User has no facsimile number registered.\n",
1113 } else if (errtype == E_NOEMAIL) {
1114 fprintf(fp, "%s: User has no email address registered.\n",
1117 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1118 namelen, " ", err->e_addr );
1121 dn = ldap_get_dn( ld, err->e_msg );
1122 ufn = ldap_explode_dn( dn, 1 );
1123 rdn = strdup( ufn[0] );
1124 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1125 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1127 for ( i = 0; vals[i]; i++ ) {
1128 last = strlen( vals[i] ) - 1;
1129 if ( isdigit( vals[i][last] ) ) {
1130 rdn = strdup( vals[i] );
1135 ldap_value_free( vals );
1138 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1141 ldap_value_free( ufn );
1143 /* titles or descriptions */
1144 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1145 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1147 fprintf( fp, "%*s No title or description registered\n",
1150 for ( i = 0; vals[i] != NULL; i++ ) {
1151 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1154 ldap_value_free( vals );
1157 /* postal address */
1158 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1160 fprintf( fp, "%*s No postal address registered\n", namelen,
1163 fprintf( fp, "%*s ", namelen, " " );
1164 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1165 if ( vals[0][i] == '$' ) {
1166 fprintf( fp, "\n%*s ", namelen, " " );
1167 while ( isspace( vals[0][i+1] ) )
1170 fprintf( fp, "%c", vals[0][i] );
1173 fprintf( fp, "\n" );
1175 ldap_value_free( vals );
1178 /* telephone number */
1179 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1181 fprintf( fp, "%*s No phone number registered\n", namelen,
1184 for ( i = 0; vals[i] != NULL; i++ ) {
1185 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1188 ldap_value_free( vals );
1194 do_ambiguous( fp, err, namelen )
1204 i = ldap_result2error( ld, err->e_msg, 0 );
1206 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1207 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1208 ldap_count_entries( ld, err->e_msg ) );
1210 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1211 e = ldap_next_entry( ld, e ) ) {
1212 dn = ldap_get_dn( ld, e );
1213 ufn = ldap_explode_dn( dn, 1 );
1214 rdn = strdup( ufn[0] );
1215 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1216 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1217 for ( i = 0; vals[i]; i++ ) {
1218 last = strlen( vals[i] ) - 1;
1219 if ( isdigit( vals[i][last] ) ) {
1220 rdn = strdup( vals[i] );
1225 ldap_value_free( vals );
1229 if ( isgroup( e ) ) {
1230 vals = ldap_get_values( ld, e, "description" );
1232 vals = ldap_get_values( ld, e, "title" );
1235 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1236 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1237 fprintf( fp, " %s\n", vals[i] );
1242 ldap_value_free( ufn );
1244 ldap_value_free( vals );
1249 count_values( list )
1254 for ( i = 0; list && list[i] != NULL; i++ )
1261 add_to( list, nlist, new )
1266 int i, nnew, oldnlist;
1268 nnew = count_values( new );
1271 if ( *list == NULL || *nlist == 0 ) {
1272 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1275 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1276 nnew * sizeof(char *) + sizeof(char *) );
1280 for ( i = 0; i < nnew; i++ ) {
1281 (*list)[i + oldnlist] = strdup( new[i] );
1283 (*list)[*nlist] = NULL;
1295 oclist = ldap_get_values( ld, e, "objectClass" );
1297 for ( i = 0; oclist[i] != NULL; i++ ) {
1298 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1299 ldap_value_free( oclist );
1303 ldap_value_free( oclist );
1309 add_error( err, nerr, code, addr, 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( dn, list, nlist )
1339 for ( i = 0; i < *nlist; i++ ) {
1340 if ( strcmp( dn, (*list)[i].g_dn ) == 0 ) {
1341 syslog( LOG_ALERT, "group loop 2 detected (%s)", dn );
1346 ufn = ldap_explode_dn( dn, 1 );
1347 namelen = strlen( ufn[0] );
1349 if ( *nlist == 0 ) {
1350 *list = (Group *) malloc( sizeof(Group) );
1352 *list = (Group *) realloc( *list, (*nlist + 1) *
1356 /* send errors to groupname-errors@host */
1357 (*list)[*nlist].g_errorsto = (char *) malloc( namelen + sizeof(ERRORS)
1359 sprintf( (*list)[*nlist].g_errorsto, "%s-%s@%s", ufn[0], ERRORS, host );
1360 (void) canonical( (*list)[*nlist].g_errorsto );
1362 /* send to groupname-members@host - make it a list for send_group */
1363 (*list)[*nlist].g_members = (char **) malloc( 2 * sizeof(char *) );
1364 (*list)[*nlist].g_members[0] = (char *) malloc( namelen +
1365 sizeof(MEMBERS) + hostlen + 2 );
1366 sprintf( (*list)[*nlist].g_members[0], "%s-%s@%s", ufn[0], MEMBERS,
1368 (void) canonical( (*list)[*nlist].g_members[0] );
1369 (*list)[*nlist].g_members[1] = NULL;
1371 /* save the group's dn so we can check for loops above */
1372 (*list)[*nlist].g_dn = strdup( dn );
1376 ldap_value_free( ufn );
1382 unbind_and_exit( rc )
1387 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1388 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1399 for ( ; *s != '\0'; s++ ) {
1412 static char **groups;
1415 for ( i = 0; i < ngroups; i++ ) {
1416 if ( strcmp( dn, groups[i] ) == 0 )
1421 groups = (char **) malloc( sizeof(char *) );
1423 groups = (char **) realloc( groups,
1424 (ngroups + 1) * sizeof(char *) );
1426 groups[ngroups++] = strdup( dn );
1432 has_attributes( e, attr1, 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( e, attr1, attr2 )
1456 char *attr2; /* this one is dn-valued */
1458 LDAPMessage *ee, *res;
1459 char **vals, **dnlist, **mail;
1461 struct timeval timeout;
1463 vals = ldap_get_values( ld, e, attr1 );
1464 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1467 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1468 timeout.tv_sec = FAX_TIMEOUT;
1469 timeout.tv_usec = 0;
1471 for ( i = 0; dnlist[i] != NULL; i++ ) {
1472 if ( (rc = ldap_search_st( ld, dnlist[i],
1473 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1474 &timeout, &res )) != LDAP_SUCCESS ) {
1475 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1476 unbind_and_exit( EX_TEMPFAIL );
1479 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1485 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1486 syslog( LOG_ALERT, "error parsing x500 entry" );
1490 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1492 add_to( &vals, &nto, mail );
1494 ldap_value_free( mail );
1497 ldap_msgfree( res );