1: #!/usr/bin/perl
2: # Little user list thingy, controlled by an rc file in each users dir.
3: # Best used if run daily via crontab.
4: #
5: # Mark Thomas <mark@cidera.com>
6:
7: ## Description
8: ## All this does is look for a file in each persons home directory
9: ## called ".luserrc", this holds a few peices of information the syntax
10: ## of the .luserrc file is somthing along the lines of:
11: ## Name: blah
12: ## Email: blah@blah.com
13: ## Comment: blah blah!
14: ##
15: ## If there is no .luserrc in the home directory then it creates
16: ## a default one, and assigns it the correct permissions, if the
17: ## user does not want to be listed on the html page, then they simply
18: ## create a file called .noshow (touch ~/.noshow) and they will not
19: ## show up.
20: ##
21: ## If the user has a ~/public_html/index.html file then their homepage
22: ## will be linked, if not, then it just shows up as non-existant.
23: ##
24: ## I understand this is lame code, but also realize it was done fairly
25: ## quickly, so you understand right? :)
26: ## This is kinda tailored for my server, but can easily be changed.
27:
28: use strict;
29: require "ctime.pl";
30:
31: ## Standard Variables
32: # Set this to 1 if you want to see various output to stdout
33: my $debug = 0;
34: # This is the directory where all your users exist
35: my $homedir = "/home";
36: # This is the file this program reads from to generate the html
37: my $rcfile = ".luserrc";
38: # The hostname of this box.
39: my $hostname = "ackers.net";
40: # What is the file were generating?
41: my $htmlfile = "/var/www/htdocs/index.html";
42: # This is the file a user must have in their home
43: # Directory if they dont want to be listed on the
44: # $htmlfile.
45: my $dontshowme = ".noshow";
46:
47: #############
48:
49: my ($dir, @dirarray, $array);
50: my $current_time = &ctime(time());
51:
52: opendir (HOME, $homedir) or print "cannot open directory $dir!\n";
53: foreach $dir (readdir(HOME)) {
54: push(@dirarray, $dir) if $dir !~ /\./;
55: }
56:
57: open(HTMLFILE, ">$htmlfile");
58:
59: print HTMLFILE <<END_OF_HEADER;
60: <html>
61: <body bgColor=black link=orange vlink=orange text=white>
62: <title>Ackers...Were all assholes here.</title>
63: <center><img src="wankers.jpg"></center>
64: <br><br><br>
65: <center><font color=white><i><b>Users</b></i></center>
66: <center>
67:
68: END_OF_HEADER
69:
70: foreach $dir (@dirarray) {
71: my($name,$email,$comment,$homepage) = read_rc($dir);
72: print HTMLFILE "Name: $name<br>\nEmail: <a href=\"mailto:$email\">$email<\/a><br>\n"
73: if (-e "$homedir/$dir/$dontshowme" eq '');
74: if ($homepage eq 0) {
75: print HTMLFILE "Homepage: Non-Existant<br>\n"
76: if(-e "$homedir/$dir/$dontshowme" eq '');
77: } elsif ($homepage eq 1) {
78: print HTMLFILE "Homepage: <a href=\"\/\~$dir\">Here<\/a><br>\n"
79: if(-e "$homedir/$dir/$dontshowme" eq '');
80: }
81: print HTMLFILE "Comment: $comment<br><br>\n"
82: if(-e "$homedir/$dir/$dontshowme" eq '');
83: print "Name:\t\t$name\nEmail:\t\t$email\nComment:\t$comment\n\n" if ($debug eq 1);
84: }
85:
86: print HTMLFILE <<END_OF_FOOTER;
87:
88: </center>
89: <br><br><br><br>
90: <center>Last updated $current_time<\/center>
91: <br>
92: <center><a href=\"mailto:root\@ackers.net\">Problems?</a>
93: <br><br><br><br><br><br>
94: <center>Want the code that generated this page? <a href=\"makedex.txt\">Click here<\/a><\/center>
95: <br><br>
96: <center><img src="openbsd_pb.jpg"></center>
97:
98: </html>
99:
100: END_OF_FOOTER
101:
102: close(HTMLFILE);
103:
104: sub no_rc {
105: my($dir) = @_;
106: # If there is no rc file, we might as well give them one right?
107: my($name,$passwd,$uid,$gid,$quota,$comment,$realname,$hdir,$shell) = getpwnam $dir
108: or print "uhh for some reason $dir doesnt exist as a user\n" if ($debug eq 1);
109: open(RC, ">>$homedir/$dir/$rcfile") or print "Cannot open $dir/$rcfile for writing\n" if ($debug eq 1);
110: print RC "# Default $rcfile, please change to your liking!\n";
111: print RC "Name: $realname\n";
112: print RC "Email: $dir\@$hostname\n";
113: print RC "Comment: Hi my name is $realname, I suck!";
114: close(RC);
115: chmod 0755, '$homedir/$dir/$rcfile';
116: chown($uid,$gid,"$homedir/$dir/$rcfile");
117: return($realname,"$dir\@$hostname","Hi my name is $realname, I suck!");
118: }
119:
120:
121: sub read_rc {
122: my($dir) = @_;
123: my($name,$email,$comment,$option,$rest,$homepage);
124: open(RC, "$homedir/$dir/$rcfile") or ($name,$email,$comment) = no_rc($dir);
125: while (<RC>) {
126: s/\s+$//g; # Remove trailing whitespace
127: s/\s/ /g; # Replace whitespace with space
128: next if /^\s*\#/; # Ignore Comment Lines
129: s/\s*\#$//g; # Ignore Trailing Comment
130: next if /^\s*$/; # Ignore Empty Lines
131: my($option,$rest) = split(/:/);
132: $rest =~ s/^\s+//g; # Remove First white space.
133: if ($option eq 'Name') {
134: $name = $rest;
135: $name =~ s/<[^>]*>//gs;
136: }
137: elsif ($option eq 'Email') {
138: $email = $rest;
139: $email =~ s/<[^>]*>//gs;
140: }
141: elsif ($option eq 'Comment') {
142: $comment = $rest;
143: $comment =~ s/<[^>]*>//gs;
144: }
145: }
146: if(-e "$homedir/$dir/public_html/index.html") {
147: $homepage = 1;
148: } else { $homepage = 0; }
149: return($name,$email,$comment,$homepage);
150: close(RC);
151: }
152: