
Fikk følgende oppgave:
We will simulate a site with many users on a universtity. You will be supplied with a text-file containing
the names, addresses, telephonenumber etc and the courses (code) that the new students will have a certain term.
The format of the textfile is:
Lastname,Firstname:studentnumber:Comma separated list of courses:Address (comma separated):Telephonenumber
There will be one student per line, and you can assume that there is no spelling or other errors in the file.
Write a Perlscript that take the file described above as input, and that does the following:
· Generates usernames based on the real name of the students. The usernames should not be longer than six characters. Be sure that the usernames are unique.
· Generate real passwords for each user. This output should be written to STDOUT only.
· Generates encrypted passwords for each user.
· Generates a unique UID for each user, and assigns a default group (GID).
· Generates a home directory for each user containing configuration files for a login shell and a public html subdirectory. All with correct ownership and permissions.
· Generates a homepage for each user containing the student’s name, address, thelephonenumber and a list of the courses the student take. (The design of the student is not important.)
· The script should then modify the “user database” files in /etc, that is passwd, group and shadow, such that the user will be able to login to the system.
· The users personal webpage should be accessed using the “tilde” notation, and each user should be able to write CGI-script. Hence you must generate a file containing the needed directives for the configuration files for the webserver.
Eksempel på input:(fiktiv data)
Karlsrud, Mariell:s7102913:IN 102, MA 002, MA 003, MA 103, FY 002:Balders Plass 29,2101 Bondestad:94463785
Fjellbo, Herman:s4902671:IN 003, KJ 002, GF 002:Smuget 102,1006 Andeby:91665797
Bonsak, Marius:s7917498:IN 002, FY 100:Thors gate 48,1002 Lilleby:93984159
Dette ble løst på følgende måte:(Legg merke til at denne scriptet ikke gjør noe i praksis, siden skriving til passwd, group og shadow er erstattet med dummylinjer. For å aktivere scriptet må du ta bort kommentering på open, print og close)
Kan lastes ned her:
Password-script
1 #!/usr/bin/perl
2
3 use strict;
4
5 my $GID = get_startGID();
6 my $UID = get_startUID();
7 my $shell = "/bin/bash";
8
9 open(FIL,"input.txt");
10
11 foreach(<FIL>)
12 {
13
14 chomp($_);
15
16 my $username = get_username($_[0]);
17 my $password = get_password();
18 my $hash = get_hash($password);
19 my $userUID = ++$GID;
20 my $userGID = ++$UID;
21
22 print "############################################\n";
23 print "Username: $username \n";
24 print "password: $password \n";
25 print "Hash: $hash \n";
26 print "UID: $userUID \n";
27 print "GID: $userGID \n";
28
29 while(is_taken($username) eq "true")
30 {
31 print "Username taken - Generating new...\n";
32 $username = get_nextUsername();
33 print "... New username: $username \n";
34 }
35
36 my $homedir = create_directory($username);
37 create_group($username);
38
39 create_user($username, $hash, $userUID, $userGID, $homedir, $shell);
40
41 create_html($username);
42
43 create_profile($username);
44
45 create_htaccess($username);
46
47 set_permission($username,$userGID);
48
49 print "############################################\n";
50
51 }
52
53
54 ##############################
55 sub get_username()
56 {
57
58 my @name = split(", ", $_);
59 my $f = substr($name[0], 0, 3);
60 my $l = substr($name[1], 0, 3);
61
62 my $result = "$f$l";
63
64 # For å strippe unordiske tegn;
65 #$result =~ s/ø/oe/;
66 #$result =~ s/æ/ae/g;
67 #$result =~ s/å/aa/g;
68 #$result =~ s//Oe/g;
69 #$result =~ s//Ae/g;
70 #$result =~ s//Aa/g;
71
72 return $result;
73
74 }
75
76 sub get_nextUsername()
77 {
78 my @name = split(", ", $_);
79 my $f = substr($name[0], 0, 2);
80 my $l = substr($name[1], 0, 2);
81 my $r = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 2 | head -n 1`;
82 chomp($r);
83 return "$f$l$r";
84 }
85
86
87 sub get_password()
88 {
89
90 my $password = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 6 | head -n 1`;
91
92 chomp($password);
93 return $password;
94 }
95
96 sub get_hash()
97 {
98 my $salt = `cat /dev/urandom|tr -dc "a-zA-Z0-9"|fold -w 2 | head -n 1`;
99 my $hash = crypt($_[0], $salt);
100
101 return $hash;
102 }
103
104
105 sub get_startGID()
106 {
107 my $result = `cat /etc/passwd | tail -n 1 | cut -d":" -f 3`;
108 return $result;
109
110 }
111
112 sub get_startUID()
113 {
114 my $result = `cat /etc/group | tail -n 1 | cut -d":" -f 3`;
115 return $result;
116 }
117
118 sub create_directory()
119 {
120 `mkdir home/$_[0]`;
121 print "... Creating home-dir: home/$_[0] \n";
122 `mkdir home/$_[0]/public_html`;
123 print "... Creating HTML-dir: home/$_[0]/public_html \n";
124
125 `mkdir home/$_[0]/public_html/cgi-bin`;
126 print "... Creating CGI-dir: home/$_[0]/public_html/cgi-bin \n";
127
128 return "/home/$_[0]";
129
130 }
131
132 sub create_html()
133 {
134 my @input = split(":",$_);
135 my $name = $input[0];
136 my $subjects = $input[2];
137 my $address = $input[3];
138 my $phone = $input[4];
139
140 print "... Creating HTML\n";
141 print "Name: $name \n";
142 print "Adr: $address \n";
143 print "Phone: $phone \n";
144 print "Subjects: $subjects \n";
145
146
147 open(HTML, ">>home/$_[0]/public_html/index.html");
148 print HTML "<html>\n";
149 print HTML "<head><title>Welcome to $name s homepage</title></head>\n";
150 print HTML "<body>\n";
151
152 print HTML "<H1>Name: $name<br> \n</H1>";
153 print HTML "Adr: $address<br> \n";
154 print HTML "Phone: $phone<br> \n";
155 print HTML "Subjects: $subjects <br>\n";
156
157 print HTML "</body>\n";
158 print HTML "</html>\n";
159
160 close(HTML);
161 }
162
163 sub is_taken()
164 {
165 my $user = $_[0];
166 my %names = ();
167
168 open(PASS,"names.txt");
169 foreach(<PASS>)
170 {
171 chomp($_);
172 split(":");
173 $names{$_[0]} = "true";
174 }
175 close(PASS);
176
177 foreach my $key (sort keys %names) {
178 if($key eq $user)
179 {
180 return "true";
181 }
182 else
183 {
184 #print "$key does not match $user\n";
185 }
186 }
187 return "false";
188 }
189
190 sub create_profile
191 {
192 open(A, ">>home/$_[0]/.profile");
193 print A "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n";
194 print A "export PATH\n";
195 print A "\n";
196 print A "\n";
197 close(A);
198
199 }
200
201 sub create_htaccess
202 {
203 open(A, ">>home/$_[0]/public_html/cgi-bin/.htaccess");
204 print A "Options +ExecCGI\n";
205 close(A);
206
207 }
208
209 sub create_group
210 {
211 my($username, $gid) = @_;
212 #open(GROUP, ">>/etc/group");
213 #print GROUP "$username:x:$gid:\n";
214 #close(GROUP);
215
216 print "------ GROUP LINE ------\n";
217 print "$username:x:$gid:\n";
218
219 }
220
221 sub create_user
222 {
223 my($username, $hash, $uid, $gid, $homedir, $shell) = @_;
224 print "------ PASSWD LINE ------\n";
225
226 #open(PASS, ">>/etc/NAME_OF_PASSFILE");
227 #print PASS "$username:x:$uid:$gid:,,,:$homedir:$shell\n";
228 #close(PASS);
229
230 print "$username:x:$uid:$gid:,,,:$homedir:$shell\n";
231
232 print "------ SHADOW LINE ------\n";
233
234 #open(SHADOW, ">>/etc/shadow");
235 #print SHADOW "$username:$hash";
236 #print SHADOW ":::::::\n";
237 #close(SHADOW);
238
239 print "$username:$hash";
240 print ":::::::\n";
241 }
242
243 sub set_permission
244 {
245 my ($username, $gid) = @_;
246
247 print "... Setting permissions ";
248 #`chown -hR $username:$gid home/$username`;
249 print "... Done\n";
250 }
Israr Khan IT, IT: Utvikling adduser, automation, flatfile, perl, script, users