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 <sys/types.h>
21 #include <sys/param.h>
22 #include <sys/resource.h>
24 #include <sys/socket.h>
26 #include <ldapconfig.h>
33 #define GROUP_ERRORS 1
34 #define GROUP_REQUEST 2
35 #define GROUP_MEMBERS 3
37 #define ERRORS "errors"
38 #define REQUEST "request"
39 #define MEMBERS "members"
42 char *errorsfrom = NULL;
43 char *mailfrom = NULL;
54 #define E_USERUNKNOWN 1
60 #define E_JOINMEMBERNOEMAIL 7
61 #define E_MEMBERNOEMAIL 8
63 #define E_JOINMEMBERNOFAXNUM 10
64 #define E_MEMBERNOFAXNUM 11
65 #define E_FAXTOEMAILMBR 12
71 typedef struct groupto {
77 typedef struct baseinfo {
78 char *b_dn; /* dn to start searching at */
79 char b_rdnpref; /* give rdn's preference when searching? */
80 char *b_filter[3]; /* filter to apply - name substituted for %s */
81 /* (up to three of them) */
85 { "ou=People, o=University of Michigan, c=US", 0,
86 "uid=%s", "cn=%s", NULL,
87 "ou=System Groups, ou=Groups, o=University of Michigan, c=US", 1,
88 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
89 "ou=User Groups, ou=Groups, o=University of Michigan, c=US", 1,
90 "(&(cn=%s)(associatedDomain=%h))", NULL, NULL,
94 char *sendmailargs[] = { FAX_SENDMAIL, "-oMrX.500", "-odi", "-oi", "-f", NULL, NULL };
96 static char *attrs[] = { "objectClass", "title", "postaladdress",
97 "telephoneNumber", "mail", "description",
98 "errorsTo", "rfc822ErrorsTo", "requestsTo",
99 "rfc822RequestsTo", "joinable", "cn", "member",
100 "facsimileTelephoneNumber", NULL };
104 static do_group_members();
105 static send_message();
106 static send_errors();
107 static do_noemailorfax();
108 static do_ambiguous();
113 static unbind_and_exit();
116 static has_attributes();
117 static char **get_attributes_mail_dn();
118 static char *canonical();
128 int numto, ngroups, numerr, nargs;
131 extern int optind, errno;
134 while ( (i = getopt( argc, argv, "f:h:m:" )) != EOF ) {
136 case 'f': /* who it's from & where errors should go */
137 mailfrom = strdup( optarg );
138 for ( j = 0; sendmailargs[j] != NULL; j++ ) {
139 if ( strcmp( sendmailargs[j], "-f" ) == 0 ) {
140 sendmailargs[j+1] = mailfrom;
146 case 'h': /* hostname */
147 host = strdup( optarg );
148 hostlen = strlen(host);
151 /* mailer-daemon address - who we should */
152 case 'm': /* say errors come from */
153 errorsfrom = strdup( optarg );
157 syslog( LOG_ALERT, "unknown option" );
162 if ( (myname = strrchr( argv[0], '/' )) == NULL )
163 myname = strdup( argv[0] );
165 myname = strdup( myname + 1 );
166 if (!strcmp(myname, "mail500")) {
168 } else if (!strcmp(myname, "fax500")) {
171 /* I give up, I must be mail500 */
176 openlog( myname, OPENLOG_OPTIONS, LOG_MAIL );
178 openlog( myname, OPENLOG_OPTIONS );
181 if ( mailfrom == NULL ) {
182 syslog( LOG_ALERT, "required argument -f not present" );
185 if ( errorsfrom == NULL ) {
186 syslog( LOG_ALERT, "required argument -m not present" );
189 if ( host == NULL ) {
190 syslog( LOG_ALERT, "required argument -h not present" );
194 if ( connect_to_x500() != 0 )
203 strcpy( buf, argv[0] );
204 for ( i = 1; i < argc; i++ ) {
206 strcat( buf, argv[i] );
209 syslog( LOG_ALERT, "args: (%s)", buf );
213 add_to( &tolist, &numto, sendmailargs );
215 ngroups = numerr = 0;
218 for ( i = optind; i < argc; i++ ) {
222 for ( j = 0; argv[i][j] != '\0'; j++ ) {
223 if ( argv[i][j] == '.' || argv[i][j] == '_' )
228 if ( (s = strrchr( argv[i], '-' )) != NULL ) {
231 if ( strcmp( s, ERRORS ) == 0 ) {
234 } else if ( strcmp( s, REQUEST ) == 0 ) {
235 type = GROUP_REQUEST;
237 } else if ( strcmp( s, MEMBERS ) == 0 ) {
238 type = GROUP_MEMBERS;
243 do_address( argv[i], &tolist, &numto, &togroups, &ngroups,
244 &errlist, &numerr, type );
248 * If we have both errors and successful deliveries to make or if
249 * if there are any groups to deliver to, we basically need to read
250 * the message twice. So, we have to put it in a tmp file.
253 if ( numerr > 0 && numto > nargs || ngroups > 0 ) {
258 if ( (fp = tmpfile()) == NULL ) {
259 syslog( LOG_ALERT, "could not open tmp file" );
260 unbind_and_exit( EX_TEMPFAIL );
263 /* copy the message to a temp file */
264 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
265 if ( fputs( buf, fp ) == EOF ) {
266 syslog( LOG_ALERT, "error writing tmpfile" );
267 unbind_and_exit( EX_TEMPFAIL );
271 if ( dup2( fileno( fp ), 0 ) == -1 ) {
272 syslog( LOG_ALERT, "could not dup2 tmpfile" );
273 unbind_and_exit( EX_TEMPFAIL );
279 /* deal with errors */
281 (void) rewind( stdin );
282 send_errors( errlist, numerr );
285 (void) ldap_unbind( ld );
287 /* send to groups with errorsTo */
289 (void) rewind( stdin );
290 send_group( togroups, ngroups );
293 /* send to expanded aliases and groups w/o errorsTo */
294 if ( numto > nargs ) {
295 (void) rewind( stdin );
296 send_message( tolist );
304 if ( (ld = ldap_open( LDAPHOST, LDAP_PORT )) == NULL ) {
305 syslog( LOG_ALERT, "ldap_open failed" );
308 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
309 ld->ld_deref = LDAP_DEREF_ALWAYS;
311 if ( ldap_simple_bind_s( ld, FAX_BINDDN, NULL ) != LDAP_SUCCESS ) {
312 syslog( LOG_ALERT, "ldap_simple_bind_s failed" );
321 do_address( name, to, nto, togroups, ngroups, err, nerr, type )
332 LDAPMessage *e, *res;
333 struct timeval timeout;
340 * Look up the name in X.500, add the appropriate addresses found
341 * to the to list, or to the err list in case of error. Groups are
342 * handled by the do_group routine, individuals are handled here.
343 * When looking up name, we follow the bases hierarchy, looking
344 * in base[0] first, then base[1], etc. For each base, there is
345 * a set of search filters to try, in order. If something goes
346 * wrong here trying to contact X.500, we exit with EX_TEMPFAIL.
347 * If the b_rdnpref flag is set, then we give preference to entries
348 * that matched name because it's their rdn, otherwise not.
351 timeout.tv_sec = FAX_TIMEOUT;
353 for ( b = 0, match = 0; !match && base[b].b_dn != NULL; b++ ) {
354 for ( f = 0; base[b].b_filter[f] != NULL; f++ ) {
355 char *format, *p, *argv[3];
358 for ( argc = 0; argc < 3; argc++ ) {
362 format = strdup( base[b].b_filter[f] );
363 for ( argc = 0, p = format; *p; p++ ) {
366 case 's': /* %s is the name */
370 case 'h': /* %h is the host */
377 "unknown format %c", *p );
385 /* three names ought to do... */
386 sprintf( filter, format, argv[0], argv[1], argv[2] );
390 rc = ldap_search_st( ld, base[b].b_dn,
391 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
394 /* some other trouble - try again later */
395 if ( rc != LDAP_SUCCESS &&
396 rc != LDAP_SIZELIMIT_EXCEEDED ) {
397 syslog( LOG_ALERT, "return %d from X.500", rc );
398 unbind_and_exit( EX_TEMPFAIL );
401 if ( (match = ldap_count_entries( ld, res )) != 0 )
411 /* trouble - try again later */
413 syslog( LOG_ALERT, "error parsing result from X.500" );
414 unbind_and_exit( EX_TEMPFAIL );
417 /* no matches - bounce with user unknown */
419 add_error( err, nerr, E_USERUNKNOWN, name, NULLMSG );
423 /* more than one match - bounce with ambiguous user? */
425 LDAPMessage *next, *tmpres = NULL;
429 /* not giving rdn preference - bounce with ambiguous user */
430 if ( base[b].b_rdnpref == 0 ) {
431 add_error( err, nerr, E_AMBIGUOUS, name, res );
436 * giving rdn preference - see if any entries were matched
437 * because of their rdn. If so, collect them to deal with
438 * later (== 1 we deliver, > 1 we bounce).
441 for ( e = ldap_first_entry( ld, res ); e != NULL; e = next ) {
442 next = ldap_next_entry( ld, e );
443 dn = ldap_get_dn( ld, e );
444 xdn = ldap_explode_dn( dn, 1 );
446 /* XXX bad, but how else can we do it? XXX */
447 if ( strcasecmp( xdn[0], name ) == 0 ) {
448 ldap_delete_result_entry( &res, e );
449 ldap_add_result_entry( &tmpres, e );
452 ldap_value_free( xdn );
456 /* nothing matched by rdn - go ahead and bounce */
457 if ( tmpres == NULL ) {
458 add_error( err, nerr, E_AMBIGUOUS, name, res );
461 /* more than one matched by rdn - bounce with rdn matches */
462 } else if ( (match = ldap_count_entries( ld, tmpres )) > 1 ) {
463 add_error( err, nerr, E_AMBIGUOUS, name, tmpres );
467 } else if ( match < 0 ) {
468 syslog( LOG_ALERT, "error parsing result from X.500" );
469 unbind_and_exit( EX_TEMPFAIL );
472 /* otherwise one matched by rdn - send to it */
478 * if we get this far, it means that we found a single match for
479 * name. for a user, we deliver to the mail attribute or bounce
480 * with address and phone if no mail attr. for a group, we
481 * deliver to all members or bounce to rfc822ErrorsTo if no members.
485 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
486 syslog( LOG_ALERT, "error parsing entry from X.500" );
487 unbind_and_exit( EX_TEMPFAIL );
490 dn = ldap_get_dn( ld, e );
492 if ( type == GROUP_ERRORS ) {
493 /* sent to group-errors - resend to [rfc822]ErrorsTo attr */
494 do_group_errors( e, dn, to, nto, err, nerr );
496 } else if ( type == GROUP_REQUEST ) {
497 /* sent to group-request - resend to [rfc822]RequestsTo attr */
498 do_group_request( e, dn, to, nto, err, nerr );
500 } else if ( type == GROUP_MEMBERS ) {
501 /* sent to group-members - expand */
502 do_group_members( e, dn, to, nto, togroups, ngroups, err,
505 } else if ( isgroup( e ) ) {
507 * sent to group - resend from [rfc822]ErrorsTo if it's there,
508 * otherwise, expand the group
511 do_group( e, dn, to, nto, togroups, ngroups, err, nerr );
517 * sent to user - mail attribute => add it to the to list,
522 if ( (mail = ldap_get_values( ld, e,
523 "facsimileTelephoneNumber" )) != NULL ) {
528 fdn = ldap_get_dn( ld, e );
529 ufn = ldap_explode_dn( fdn, 1 );
530 /* Convert spaces to underscores for rp */
531 for (i = 0; ufn[0][i] != '\0'; i++) {
532 if (ufn[0][i] == ' ') {
536 *mail = faxtotpc(*mail, ufn[0]);
538 add_to(to, nto, mail);
543 ldap_value_free( ufn );
544 ldap_value_free( mail );
547 add_error( err, nerr, E_NOFAXNUM,
552 if ( (mail = ldap_get_values( ld, e, "mail" ))
554 add_to( to, nto, mail );
556 ldap_value_free( mail );
559 add_error( err, nerr, E_NOEMAIL,
572 do_group( e, dn, to, nto, togroups, ngroups, err, nerr )
583 * If this group has an rfc822ErrorsTo attribute, we need to
584 * arrange for errors involving this group to go there, not
585 * to the sender. Since sendmail only has the concept of a
586 * single sender, we arrange for errors to go to groupname-errors,
587 * which we then handle specially when (if) it comes back to us
588 * by expanding to all the rfc822ErrorsTo addresses. If it has no
589 * rfc822ErrorsTo attribute, we call do_group_members() to expand
593 if ( group_loop( dn ) ) {
597 if ( has_attributes( e, "rfc822ErrorsTo", "errorsTo" ) ) {
598 add_group( dn, togroups, ngroups );
603 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr );
610 do_group_members( e, dn, to, nto, togroups, ngroups, err, nerr )
622 char **mail, **member, **joinable;
624 LDAPMessage *ee, *res;
625 struct timeval timeout;
628 * if all has gone according to plan, we've already arranged for
629 * errors to go to the [rfc822]ErrorsTo attributes (if they exist),
630 * so all we have to do here is arrange to send to the
631 * rfc822Mailbox attribute, the member attribute, and anyone who
632 * has joined the group by setting memberOfGroup equal to the
636 /* add members in the group itself - mail attribute */
637 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
639 * These are only email addresses - impossible
640 * to have a fax number
644 /* XXX How do we want to inform sender that the */
645 /* group they sent to has email-only members? */
648 add_to( to, nto, mail );
652 ldap_value_free( mail );
655 /* add members in the group itself - member attribute */
656 if ( (member = ldap_get_values( ld, e, "member" )) != NULL ) {
657 for ( i = 0; member[i] != NULL; i++ ) {
658 add_member( dn, member[i], to, nto, togroups,
659 ngroups, err, nerr );
662 ldap_value_free( member );
665 /* add members who have joined by setting memberOfGroup */
666 if ( (joinable = ldap_get_values( ld, e, "joinable" )) != NULL ) {
667 if ( strcasecmp( joinable[0], "FALSE" ) == 0 ) {
668 ldap_value_free( joinable );
671 ldap_value_free( joinable );
673 sprintf( filter, "(memberOfGroup=%s)", dn );
675 timeout.tv_sec = FAX_TIMEOUT;
678 /* for each subtree to look in... */
679 ld->ld_sizelimit = FAX_MAXMEMBERS;
680 for ( i = 0; base[i].b_dn != NULL; i++ ) {
681 /* find entries that have joined this group... */
682 rc = ldap_search_st( ld, base[i].b_dn,
683 LDAP_SCOPE_SUBTREE, filter, attrs, 0, &timeout,
686 if ( rc == LDAP_SIZELIMIT_EXCEEDED ||
687 rc == LDAP_TIMELIMIT_EXCEEDED ) {
689 "group search limit exceeded %d", rc );
690 unbind_and_exit( EX_TEMPFAIL );
693 if ( rc != LDAP_SUCCESS ) {
694 syslog( LOG_ALERT, "group search bad return %d",
696 unbind_and_exit( EX_TEMPFAIL );
699 /* for each entry that has joined... */
700 for ( ee = ldap_first_entry( ld, res ); ee != NULL;
701 ee = ldap_next_entry( ld, ee ) ) {
702 if ( isgroup( ee ) ) {
703 ndn = ldap_get_dn( ld, ee );
705 if ( do_group( e, ndn, to, nto,
706 togroups, ngroups, err, nerr )
709 "group loop (%s) (%s)",
718 /* add them to the to list */
721 if ( (mail = ldap_get_values( ld, ee,
722 "facsimileTelephoneNumber" ))
728 fdn = ldap_get_dn( ld, ee );
729 ufn = ldap_explode_dn( fdn, 1 );
734 for (i = 0; ufn[0][i] != '\0';
736 if (ufn[0][i] == ' ') {
740 *mail = faxtotpc(*mail, ufn[0]);
741 add_to(to, nto, mail);
743 ldap_value_free( mail );
746 ndn = ldap_get_dn( ld, ee );
748 add_error( err, nerr,
749 E_JOINMEMBERNOFAXNUM, ndn,
756 if ( (mail = ldap_get_values( ld, ee,
757 "mail" )) != NULL ) {
758 add_to( to, nto, mail );
760 ldap_value_free( mail );
762 /* else generate a bounce */
764 ndn = ldap_get_dn( ld, ee );
766 add_error( err, nerr,
767 E_JOINMEMBERNOEMAIL, ndn,
778 ld->ld_sizelimit = FAX_MAXAMBIGUOUS;
784 add_member( gdn, dn, to, nto, togroups, ngroups, err, nerr )
797 LDAPMessage *res, *e;
798 struct timeval timeout;
800 timeout.tv_sec = FAX_TIMEOUT;
802 if ( (rc = ldap_search_st( ld, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
803 attrs, 0, &timeout, &res )) != LDAP_SUCCESS ) {
804 if ( rc == LDAP_NO_SUCH_OBJECT ) {
805 add_error( err, nerr, E_BADMEMBER, dn, NULLMSG );
809 syslog( LOG_ALERT, "member search bad return %d", rc );
811 unbind_and_exit( EX_TEMPFAIL );
815 if ( (e = ldap_first_entry( ld, res )) == NULL ) {
816 syslog( LOG_ALERT, "member search error parsing entry" );
818 unbind_and_exit( EX_TEMPFAIL );
820 ndn = ldap_get_dn( ld, e );
822 /* allow groups within groups */
823 if ( isgroup( e ) ) {
824 if ( do_group( e, ndn, to, nto, togroups, ngroups, err, nerr )
826 syslog( LOG_ALERT, "group loop (%s) (%s)", gdn, ndn );
836 if ( (mail = ldap_get_values( ld, e,
837 "facsimileTelephoneNumber" )) != NULL ) {
841 fdn = ldap_get_dn( ld, e );
842 ufn = ldap_explode_dn( fdn, 1 );
843 /* Convert spaces to underscores for rp */
844 for (i = 0; ufn[0][i] != '\0'; i++) {
845 if (ufn[0][i] == ' ') {
849 *mail = faxtotpc(*mail, ufn[0]);
850 add_to(to, nto, mail);
852 ldap_value_free( mail );
855 add_error( err, nerr, E_MEMBERNOFAXNUM, ndn, NULLMSG );
859 /* send to the member's mail attribute */
860 if ( (mail = ldap_get_values( ld, e, "mail" )) != NULL ) {
861 add_to( to, nto, mail );
863 ldap_value_free( mail );
865 /* else generate a bounce */
867 add_error( err, nerr, E_MEMBERNOEMAIL, ndn, NULLMSG );
875 do_group_request( e, dn, to, nto, err, nerr )
885 if ( (requeststo = get_attributes_mail_dn( e, "rfc822RequestsTo",
886 "requestsTo" )) != NULL ) {
887 add_to( to, nto, requeststo );
889 ldap_value_free( requeststo );
891 add_error( err, nerr, E_NOREQUEST, dn, NULLMSG );
897 do_group_errors( e, dn, to, nto, err, nerr )
907 if ( (errorsto = get_attributes_mail_dn( e, "rfc822ErrorsTo",
908 "errorsTo" )) != NULL ) {
909 add_to( to, nto, errorsto );
911 ldap_value_free( errorsto );
913 add_error( err, nerr, E_NOERRORS, dn, NULLMSG );
925 WAITSTATUSTYPE status;
930 if ( pid = fork() ) {
932 waitpid( pid, (int *) NULL, 0 );
934 wait4( pid, &status, WAIT_FLAGS, 0 );
938 /* to includes sendmailargs */
939 execv( FAX_SENDMAIL, to );
941 syslog( LOG_ALERT, "execv failed" );
948 send_group( group, ngroup )
957 WAITSTATUSTYPE status;
960 for ( i = 0; i < ngroup; i++ ) {
961 (void) rewind( stdin );
963 iargv[0] = FAX_SENDMAIL;
965 iargv[2] = group[i].g_errorsto;
966 iargv[3] = "-oMrX.500";
973 add_to( &argv, &argc, iargv );
974 add_to( &argv, &argc, group[i].g_members );
978 if ( pid = fork() ) {
980 waitpid( pid, (int *) NULL, 0 );
982 wait4( pid, &status, WAIT_FLAGS, 0 );
986 execv( FAX_SENDMAIL, argv );
988 syslog( LOG_ALERT, "execv failed" );
998 send_errors( err, nerr )
1006 sprintf( buf, "%s -oMrX.500 -odi -oi -f %s %s", FAX_SENDMAIL, errorsfrom,
1008 if ( (fp = popen( buf, "w" )) == NULL ) {
1009 syslog( LOG_ALERT, "could not popen sendmail for errs" );
1013 fprintf( fp, "To: %s\n", mailfrom );
1014 fprintf( fp, "From: %s\n", errorsfrom );
1015 fprintf( fp, "Subject: undeliverable mail\n" );
1016 fprintf( fp, "\n" );
1017 fprintf( fp, "The following errors occurred when trying to deliver the attached mail:\n" );
1018 for ( i = 0; i < nerr; i++ ) {
1019 namelen = strlen( err[i].e_addr );
1020 fprintf( fp, "\n" );
1022 switch ( err[i].e_code ) {
1024 fprintf( fp, "%s: User unknown\n", err[i].e_addr );
1028 fprintf( fp, "%s: Group member does not exist\n",
1033 fprintf( fp, "%s: Group exists but has no request address\n",
1038 fprintf( fp, "%s: Group exists but has no errors-to address\n",
1043 do_ambiguous( fp, &err[i], namelen );
1047 do_noemailorfax( fp, &err[i], namelen, E_NOEMAIL );
1050 case E_MEMBERNOEMAIL:
1051 fprintf( fp, "%s: Group member exists but does not have an email address\n",
1055 case E_JOINMEMBERNOEMAIL:
1056 fprintf( fp, "%s: User has joined group but does not have an email address\n",
1061 do_noemailorfax( fp, &err[i], namelen, E_NOFAXNUM );
1064 case E_MEMBERNOFAXNUM:
1065 fprintf( fp, "%s: Group member exists but does not have a fax number\n",
1069 case E_JOINMEMBERNOFAXNUM:
1070 fprintf( fp, "%s: User has joined group but does not have a fax number\n",
1075 syslog( LOG_ALERT, "unknown error %d", err[i].e_code );
1076 unbind_and_exit( EX_TEMPFAIL );
1081 fprintf( fp, "\n------- The original message sent:\n\n" );
1083 while ( fgets( buf, sizeof(buf), stdin ) != NULL ) {
1087 if ( pclose( fp ) == -1 ) {
1088 syslog( LOG_ALERT, "pclose failed" );
1089 unbind_and_exit( EX_TEMPFAIL );
1097 do_noemailorfax( fp, err, namelen, errtype )
1107 if (errtype == E_NOFAXNUM) {
1108 fprintf(fp, "%s: User has no facsimile number registered.\n",
1110 } else if (errtype == E_NOEMAIL) {
1111 fprintf(fp, "%s: User has no email address registered.\n",
1114 fprintf( fp, "%*s Name, title, postal address and phone for '%s':\n\n",
1115 namelen, " ", err->e_addr );
1118 dn = ldap_get_dn( ld, err->e_msg );
1119 ufn = ldap_explode_dn( dn, 1 );
1120 rdn = strdup( ufn[0] );
1121 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1122 if ( (vals = ldap_get_values( ld, err->e_msg, "cn" ))
1124 for ( i = 0; vals[i]; i++ ) {
1125 last = strlen( vals[i] ) - 1;
1126 if ( isdigit( vals[i][last] ) ) {
1127 rdn = strdup( vals[i] );
1132 ldap_value_free( vals );
1135 fprintf( fp, "%*s %s\n", namelen, " ", rdn );
1138 ldap_value_free( ufn );
1140 /* titles or descriptions */
1141 if ( (vals = ldap_get_values( ld, err->e_msg, "title" )) == NULL &&
1142 (vals = ldap_get_values( ld, err->e_msg, "description" ))
1144 fprintf( fp, "%*s No title or description registered\n",
1147 for ( i = 0; vals[i] != NULL; i++ ) {
1148 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1151 ldap_value_free( vals );
1154 /* postal address */
1155 if ( (vals = ldap_get_values( ld, err->e_msg, "postalAddress" ))
1157 fprintf( fp, "%*s No postal address registered\n", namelen,
1160 fprintf( fp, "%*s ", namelen, " " );
1161 for ( i = 0; vals[0][i] != '\0'; i++ ) {
1162 if ( vals[0][i] == '$' ) {
1163 fprintf( fp, "\n%*s ", namelen, " " );
1164 while ( isspace( vals[0][i+1] ) )
1167 fprintf( fp, "%c", vals[0][i] );
1170 fprintf( fp, "\n" );
1172 ldap_value_free( vals );
1175 /* telephone number */
1176 if ( (vals = ldap_get_values( ld, err->e_msg, "telephoneNumber" ))
1178 fprintf( fp, "%*s No phone number registered\n", namelen,
1181 for ( i = 0; vals[i] != NULL; i++ ) {
1182 fprintf( fp, "%*s %s\n", namelen, " ", vals[i] );
1185 ldap_value_free( vals );
1191 do_ambiguous( fp, err, namelen )
1201 i = ldap_result2error( ld, err->e_msg, 0 );
1203 fprintf( fp, "%s: Ambiguous user. %s%d matches found:\n\n",
1204 err->e_addr, i == LDAP_SIZELIMIT_EXCEEDED ? "First " : "",
1205 ldap_count_entries( ld, err->e_msg ) );
1207 for ( e = ldap_first_entry( ld, err->e_msg ); e != NULL;
1208 e = ldap_next_entry( ld, e ) ) {
1209 dn = ldap_get_dn( ld, e );
1210 ufn = ldap_explode_dn( dn, 1 );
1211 rdn = strdup( ufn[0] );
1212 if ( strcasecmp( rdn, err->e_addr ) == 0 ) {
1213 if ( (vals = ldap_get_values( ld, e, "cn" )) != NULL ) {
1214 for ( i = 0; vals[i]; i++ ) {
1215 last = strlen( vals[i] ) - 1;
1216 if ( isdigit( vals[i][last] ) ) {
1217 rdn = strdup( vals[i] );
1222 ldap_value_free( vals );
1226 if ( isgroup( e ) ) {
1227 vals = ldap_get_values( ld, e, "description" );
1229 vals = ldap_get_values( ld, e, "title" );
1232 fprintf( fp, " %-20s %s\n", rdn, vals ? vals[0] : "" );
1233 for ( i = 1; vals && vals[i] != NULL; i++ ) {
1234 fprintf( fp, " %s\n", vals[i] );
1239 ldap_value_free( ufn );
1241 ldap_value_free( vals );
1246 count_values( list )
1251 for ( i = 0; list && list[i] != NULL; i++ )
1258 add_to( list, nlist, new )
1263 int i, nnew, oldnlist;
1265 nnew = count_values( new );
1268 if ( *list == NULL || *nlist == 0 ) {
1269 *list = (char **) malloc( (nnew + 1) * sizeof(char *) );
1272 *list = (char **) realloc( *list, *nlist * sizeof(char *) +
1273 nnew * sizeof(char *) + sizeof(char *) );
1277 for ( i = 0; i < nnew; i++ ) {
1278 (*list)[i + oldnlist] = strdup( new[i] );
1280 (*list)[*nlist] = NULL;
1292 oclist = ldap_get_values( ld, e, "objectClass" );
1294 for ( i = 0; oclist[i] != NULL; i++ ) {
1295 if ( strcasecmp( oclist[i], "rfc822MailGroup" ) == 0 ) {
1296 ldap_value_free( oclist );
1300 ldap_value_free( oclist );
1306 add_error( err, nerr, code, addr, msg )
1314 *err = (Error *) malloc( sizeof(Error) );
1316 *err = (Error *) realloc( *err, (*nerr + 1) * sizeof(Error) );
1319 (*err)[*nerr].e_code = code;
1320 (*err)[*nerr].e_addr = strdup( addr );
1321 (*err)[*nerr].e_msg = msg;
1328 add_group( dn, list, 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( rc )
1384 if ( (i = ldap_unbind( ld )) != LDAP_SUCCESS )
1385 syslog( LOG_ALERT, "ldap_unbind failed %d\n", i );
1396 for ( ; *s != '\0'; s++ ) {
1409 static char **groups;
1412 for ( i = 0; i < ngroups; i++ ) {
1413 if ( strcmp( dn, groups[i] ) == 0 )
1418 groups = (char **) malloc( sizeof(char *) );
1420 groups = (char **) realloc( groups,
1421 (ngroups + 1) * sizeof(char *) );
1423 groups[ngroups++] = strdup( dn );
1429 has_attributes( e, attr1, attr2 )
1436 if ( (attr = ldap_get_values( ld, e, attr1 )) != NULL ) {
1437 ldap_value_free( attr );
1441 if ( (attr = ldap_get_values( ld, e, attr2 )) != NULL ) {
1442 ldap_value_free( attr );
1450 get_attributes_mail_dn( e, attr1, attr2 )
1453 char *attr2; /* this one is dn-valued */
1455 LDAPMessage *ee, *res;
1456 char **vals, **dnlist, **mail;
1458 struct timeval timeout;
1460 vals = ldap_get_values( ld, e, attr1 );
1461 for ( nto = 0; vals != NULL && vals[nto] != NULL; nto++ )
1464 if ( (dnlist = ldap_get_values( ld, e, attr2 )) != NULL ) {
1465 timeout.tv_sec = FAX_TIMEOUT;
1466 timeout.tv_usec = 0;
1468 for ( i = 0; dnlist[i] != NULL; i++ ) {
1469 if ( (rc = ldap_search_st( ld, dnlist[i],
1470 LDAP_SCOPE_BASE, "(objectclass=*)", attrs, 0,
1471 &timeout, &res )) != LDAP_SUCCESS ) {
1472 if ( rc != LDAP_NO_SUCH_OBJECT ) {
1473 unbind_and_exit( EX_TEMPFAIL );
1476 syslog( LOG_ALERT, "bad (%s) dn (%s)", attr2,
1482 if ( (ee = ldap_first_entry( ld, res )) == NULL ) {
1483 syslog( LOG_ALERT, "error parsing x500 entry" );
1487 if ( (mail = ldap_get_values( ld, ee, "mail" ))
1489 add_to( &vals, &nto, mail );
1491 ldap_value_free( mail );
1494 ldap_msgfree( res );