XSS protection
[webmin.git] / user_chooser.cgi
1 #!/usr/local/bin/perl
2 # user_chooser.cgi
3 # This CGI generated the HTML for choosing a user or list of users.
4
5 $trust_unknown_referers = 1;
6 require './web-lib.pl';
7 &init_config();
8 &ReadParse(undef, undef, 2);
9 %access = &get_module_acl();
10
11 if ($in{'multi'}) {
12         # selecting multiple users.
13         if ($in{'frame'} == 0) {
14                 # base frame
15                 &PrintHeader();
16                 print "<script>\n";
17                 @ul = split(/\s+/, $in{'user'});
18                 $len = @ul;
19                 print "sel = new Array($len);\n";
20                 print "selr = new Array($len);\n";
21                 for($i=0; $i<$len; $i++) {
22                         print "sel[$i] = \"".
23                               &quote_escape($ul[$i], '"')."\";\n";
24                         @uinfo = getpwnam($ul[$i]);
25                         if (@uinfo) { print "selr[$i] = \"$uinfo[6]\";\n"; }
26                         else { print "selr[$i] = \"???\";\n"; }
27                         }
28                 print "</script>\n";
29                 print "<title>$text{'users_title1'}</title>\n";
30                 print "<frameset cols='50%,50%'>\n";
31                 print "<frame src=\"/user_chooser.cgi?frame=1&multi=1\">\n";
32                 print "<frameset rows='*,50' frameborder=no>\n";
33                 print " <frame src=\"/user_chooser.cgi?frame=2&multi=1\">\n";
34                 print " <frame src=\"/user_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
35                 print "</frameset>\n";
36                 print "</frameset>\n";
37                 }
38         elsif ($in{'frame'} == 1) {
39                 # list of all users to choose from
40                 &popup_header();
41                 print "<script>\n";
42                 print "function adduser(u, r)\n";
43                 print "{\n";
44                 print "top.sel[top.sel.length] = u\n";
45                 print "top.selr[top.selr.length] = r\n";
46                 print "top.frames[1].location = top.frames[1].location\n";
47                 print "return false;\n";
48                 print "}\n";
49                 print "</script>\n";
50                 print "<font size=+1>$text{'users_all'}</font>\n";
51                 print "<table width=100%>\n";
52                 foreach $u (&get_users_list()) {
53                         if ($in{'user'} eq $u->[0]) { print "<tr $cb>\n"; }
54                         else { print "<tr>\n"; }
55                         $u->[6] =~ s/'/&#39;/g;
56                         print "<td width=20%><a href=\"\" onClick='return adduser(\"$u->[0]\", \"$u->[6]\")'>$u->[0]</a></td>\n";
57                         print "<td>$u->[6]</td> </tr>\n";
58                         }
59                 print "</table>\n";
60                 &popup_footer();
61                 }
62         elsif ($in{'frame'} == 2) {
63                 # show chosen users
64                 &popup_header();
65                 print "<font size=+1>$text{'users_sel'}</font>\n";
66                 print <<'EOF';
67 <table width=100%>
68 <script>
69 function sub(j)
70 {
71 sel2 = new Array(); selr2 = new Array();
72 for(k=0,l=0; k<top.sel.length; k++) {
73         if (k != j) {
74                 sel2[l] = top.sel[k];
75                 selr2[l] = top.selr[k];
76                 l++;
77                 }
78         }
79 top.sel = sel2; top.selr = selr2;
80 top.frames[1].location = top.frames[1].location;
81 return false;
82 }
83 for(i=0; i<top.sel.length; i++) {
84         document.write("<tr>\n");
85         document.write("<td><a href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
86         document.write("<td>"+top.selr[i]+"</td>\n");
87         }
88 </script>
89 </table>
90 EOF
91                 &popup_footer();
92                 }
93         elsif ($in{'frame'} == 3) {
94                 # output OK and Cancel buttons
95                 &popup_header();
96                 print "<script>\n";
97                 print "function qjoin(l)\n";
98                 print "{\n";
99                 print "rv = \"\";\n";
100                 print "for(i=0; i<l.length; i++) {\n";
101                 print "    if (rv != '') rv += ' ';\n";
102                 print "    if (l[i].indexOf(' ') < 0) rv += l[i];\n";
103                 print "    else rv += '\"'+l[i]+'\"'\n";
104                 print "    }\n";
105                 print "return rv;\n";
106                 print "}\n";
107                 print "</script>\n";
108                 print "<form>\n";
109                 print "<input type=button value=\"$text{'users_ok'}\" ",
110                       "onClick='top.opener.ifield.value = qjoin(top.sel); ",
111                       "top.close()'>\n";
112                 print "<input type=button value=\"$text{'users_cancel'}\" ",
113                       "onClick='top.close()'>\n";
114                 print "&nbsp;&nbsp;<input type=button value=\"$text{'users_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
115                 print "</form>\n";
116                 &popup_footer();
117                 }
118         }
119 else {
120         # selecting just one user .. display a list of all users to choose from
121         &popup_header($text{'users_title2'});
122         print "<script>\n";
123         print "function select(f)\n";
124         print "{\n";
125         print "top.opener.ifield.value = f;\n";
126         print "top.close();\n";
127         print "return false;\n";
128         print "}\n";
129         print "</script>\n";
130         print "<table width=100%>\n";
131         foreach $u (&get_users_list()) {
132                 if ($in{'user'} eq $u->[0]) { print "<tr $cb>\n"; }
133                 else { print "<tr>\n"; }
134                 print "<td width=20%><a href=\"\" onClick='return select(\"$u->[0]\")'>$u->[0]</a></td>\n";
135                 print "<td>$u->[6]</td> </tr>\n";
136                 }
137         print "</table>\n";
138         &popup_footer();
139         }
140
141 sub get_users_list
142 {
143 local(@uinfo, @users, %ucan, %found);
144 if ($access{'uedit_mode'} == 2 || $access{'uedit_mode'} == 3) {
145         map { $ucan{$_}++ } split(/\s+/, $access{'uedit'});
146         }
147 setpwent();
148 while(@uinfo = getpwent()) {
149         if ($access{'uedit_mode'} == 0 ||
150             $access{'uedit_mode'} == 2 && $ucan{$uinfo[0]} ||
151             $access{'uedit_mode'} == 3 && !$ucan{$uinfo[0]} ||
152             $access{'uedit_mode'} == 4 &&
153                 (!$access{'uedit'} || $uinfo[2] >= $access{'uedit'}) &&
154                 (!$access{'uedit2'} || $uinfo[2] <= $access{'uedit2'}) ||
155             $access{'uedit_mode'} == 5 && $uinfo[3] == $access{'uedit'}) {
156                 push(@users, [ @uinfo ]) if (!$found{$uinfo[0]}++);
157                 }
158         }
159 endpwent() if ($gconfig{'os_type'} ne 'hpux');
160 return sort { $a->[0] cmp $b->[0] } @users;
161 }
162