home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2112 / getalias next >
Encoding:
Text File  |  1990-12-28  |  4.7 KB  |  203 lines

  1. #!/usr/local/bin/perl
  2. 'di';
  3. 'ig00';
  4.  
  5. dbmopen(ALIASES,'/usr/lib/aliases',undef) || die "can't dbmopen aliases";
  6.  
  7. require 'getopts.pl';;
  8.  
  9. (&Getopts('vVs') && $#ARGV >= 0) || die "usage: $0 [-vVs] alias ...\n";
  10.  
  11. chop($host = `hostname`);
  12.  
  13. ($verbose, $showall, $short) = ($opt_v, $opt_V, $opt_s);
  14. $verbose |= $showall;
  15.  
  16. while ($user = shift) {
  17.     local(%seen);
  18.     if ($short) {
  19.     print join(' ', &resolve($user)), "\n";
  20.     } else {
  21.     print "$user -> ", join(', ', sort &resolve($user)), "\n";
  22.     } 
  23. }
  24.  
  25. sub resolve {
  26.     local($addr,$alias,@list,@ilist);
  27.  
  28.     while ($addr = shift) {
  29.     if ($seen{$addr}++) {
  30.         #push(@list, $addr);
  31.         next;
  32.     } 
  33.     unless (defined $ALIASES{"$addr\0"}) {
  34.         push(@list, &forward($addr));
  35.         next;
  36.     } 
  37.     chop($alias = $ALIASES{"$addr\0"});
  38.     $alias =~ s/^\s*(.*)\s*$/$1/;
  39.     if ($alias eq $addr) {
  40.         push(@list, &forward($addr));
  41.         next;
  42.     } 
  43.     if ($alias =~ /^"/) {
  44.         push(@list, $alias);
  45.         next;
  46.     } 
  47.     print "[ $addr -> $alias ]\n" 
  48.         if $showall || ($verbose 
  49.         && ($alias !~ /^$addr@\w+$/ && 
  50.             $alias !~ /^[^!]+![^!]+$/));
  51.     if ($alias eq "$addr@$host") {
  52.         push(@list, &forward($addr));
  53.         next;
  54.     } 
  55.     if ($alias =~ /^:include:(.*)/) {
  56.         unless (open(INC, $file = $1)) {
  57.         print STDERR "$0: can't open $file: $!\n";
  58.         next;
  59.         }
  60.         @ilist = grep(!/^#/, <INC>);
  61.         for (@ilist) { s/\s//g; } 
  62.         close(INC);
  63.         printf "[ %s -> %s ]\n", $file, join(' ', @ilist) if $verbose;
  64.         push(@list,&resolve(@ilist));
  65.     } else {
  66.         push(@list,&resolve(split(/\s*,\s*/,$alias)));
  67.     }
  68.     } 
  69.     return @list;
  70.  
  71.  
  72. ##############################################################
  73.  
  74. sub forward {
  75.      local($user) = @_;
  76.      local($forward); 
  77.  
  78.      return $user if $user =~ /^\s*"?[|\/]/;
  79.      return $user if $user =~ /^\s*.+@.+$/;
  80.      return $user if $user =~ /^\s*.+\\?!.+$/;
  81.      return $user if $user =~ /^\s*\\/;
  82.  
  83.      if (($forward = &logdir($user)) && -r $forward .= '/.forward') {
  84.     if (!open forward) {
  85.         print STDERR "$0: cannot open $forward: $!\n";
  86.     } else {
  87.         print "[ $user -> $forward ]\n" if $verbose;
  88.         chop($user = <forward>);
  89.         close forward;
  90.         print "[ $forward -> $user ]\n" if $verbose;
  91.         return &resolve(split(/\s*,\s*/,$user));
  92.     }
  93.      } else {
  94.     #print "no forward for $user\n";
  95.      } 
  96.      $user = "$user <MAILER-DAEMON>" unless $forward;
  97.      return $user;
  98.  
  99.  
  100. ##############################################################
  101.  
  102. sub logdir {
  103.     if (! $been_here_before++) { # might make it much faster
  104.     setpwent unless $dbm_passwd = dbmopen(PASSWD,'/etc/passwd', undef);
  105.     }
  106.  
  107.     if ($dbm_passwd) {
  108.     return '' unless defined $PASSWD{$_[0]};
  109.     local(@a);
  110.     @a = split(/[\000]+/,$PASSWD{$_[0]});
  111.     return $a[$#a-1];
  112.     } else {
  113.     return (getpwnam($_[0]))[7];
  114.     }
  115. ##############################################################################
  116.  
  117.     # These next few lines are legal in both Perl and nroff.
  118.  
  119. .00;            # finish .ig
  120.  
  121. 'di            \" finish diversion--previous line must be blank
  122. .nr nl 0-1        \" fake up transition to first page again
  123. .nr % 0            \" start at page 1
  124. ';<<'.ex'; #__END__ ############# From here on it's a standard manual page ############
  125. .TH GETALIAS 1L
  126. .de M        \" man page reference
  127. \\fI\\$1\\fR\\|(\\$2\)\\$3
  128. ..
  129. .SH NAME
  130. getalias \- recursively resolve mail aliases
  131. .SH SYNOPSIS
  132. .B getalias
  133. [
  134. .B \-v
  135. .B \-V
  136. .B \-s
  137. ]
  138. .I alias
  139. \&...
  140. .SH DESCRIPTION
  141. The 
  142. .I getalias
  143. program consults the 
  144. .M dbm 3X
  145. version of the
  146. .M aliases 5
  147. database to rescursively resolve each of its arguments, printing
  148. the alias's (alphabetically sorted) resolution to the standard output,
  149. separated by commas.
  150. Include
  151. files referenced by the \fI:include:\fP syntax will be consulted, 
  152. as will local users'
  153. .I ~/.forward
  154. files. 
  155. .PP
  156. Arguemnts not appearing to be deliverable addresses
  157. are resolved to
  158. the form ``\fIalias-name\fP <MAILER-DAEMON>'' to indicate
  159. that such mail will probably be delivered to the mailer daemon
  160. for subsequent complaint.
  161. .PP
  162. The
  163. .B \-v
  164. option traces intermediary passes of the alias resolution for
  165. mailing lists.
  166. Watching the recursion can occasionally be entertaining and informative.
  167. .PP
  168. The
  169. .B \-V
  170. option is like 
  171. .B \-v\c
  172. , except that it also includes simple ``name -> name@host'' aliases as well.
  173. These are mailboxes that simply go to another machine, rather than 
  174. mailing lists requiring recursive expansion.
  175. .PP
  176. The 
  177. .B \-s
  178. option suppresses printing of the original alias before its resolution.
  179. It also makes space the separator character.
  180. .SH NOTES
  181. .I Getalias
  182. is a 
  183. .I perl 
  184. program, so you must have 
  185. .I perl 
  186. on your system to run it.  Since it
  187. is not compiled, you may read the program to learn more about 
  188. how it works internally.
  189. .SH "SEE ALSO"
  190. .M mail 1 ,
  191. .M nfinger 1 ,
  192. .M perl 1 ,
  193. .M dbm 3X ,
  194. .M aliases 5 ,
  195. .M sendmail 8
  196. .SH AUTHOR
  197. Tom Christiansen 
  198. .I "<tchrist@convex.com>"
  199. .ex
  200.