Handle hostnames with upper-case letters
[webmin.git] / group_chooser.cgi
1 #!/usr/local/bin/perl
2 # group_chooser.cgi
3 # This CGI generated the HTML for choosing a group or list of groups.
4
5 BEGIN { push(@INC, ".."); };
6 use WebminCore;
7
8 $trust_unknown_referers = 1;
9 &init_config();
10 if (&get_product_name() eq 'usermin') {
11         &switch_to_remote_user();
12         }
13 &ReadParse(undef, undef, 2);
14 %access = &get_module_acl();
15
16 # Build list of primary groups
17 setpwent();
18 while(@uinfo = getpwent()) {
19         push(@{$members{$uinfo[3]}}, $uinfo[0]);
20         }
21 endpwent() if ($gconfig{'os_type'} ne 'hpux');
22
23 if ($in{'multi'}) {
24         # selecting multiple groups.
25         if ($in{'frame'} == 0) {
26                 # base frame
27                 &PrintHeader();
28                 print "<script>\n";
29                 @ul = &split_quoted($in{'group'});
30                 $len = @ul;
31                 print "sel = new Array($len);\n";
32                 print "selr = new Array($len);\n";
33                 for($i=0; $i<$len; $i++) {
34                         print "sel[$i] = \"".
35                               &quote_escape($ul[$i], '"')."\";\n";
36                         @ginfo = getgrnam($ul[$i]);
37                         if (@ginfo) {
38                                 @mems = &unique( split(/ /, $ginfo[3]),
39                                                  @{$members{$ginfo[2]}} );
40                                 if (@mems > 3) { @mems = (@mems[0..1], "..."); }
41                                 print "selr[$i] = \"",
42                                   &quote_escape(join(' ', @mems), "'"),"\";\n";
43                                 }
44                         else { print "selr[$i] = \"???\";\n"; }
45                         }
46                 print "</script>\n";
47                 print "<title>$text{'groups_title1'}</title>\n";
48                 print "<frameset cols='50%,50%'>\n";
49                 print "<frame src=\"group_chooser.cgi?frame=1&multi=1\">\n";
50                 print "<frameset rows='*,50' frameborder=no>\n";
51                 print " <frame src=\"group_chooser.cgi?frame=2&multi=1\">\n";
52                 print " <frame src=\"group_chooser.cgi?frame=3&multi=1\" scrolling=no>\n";
53                 print "</frameset>\n";
54                 print "</frameset>\n";
55                 }
56         elsif ($in{'frame'} == 1) {
57                 # list of all groups to choose from
58                 &popup_header();
59                 print "<script>\n";
60                 print "function addgroup(u, r)\n";
61                 print "{\n";
62                 print "top.sel[top.sel.length] = u\n";
63                 print "top.selr[top.selr.length] = r\n";
64                 print "top.frames[1].location = top.frames[1].location\n";
65                 print "return false;\n";
66                 print "}\n";
67                 print "</script>\n";
68                 print "<font size=+1>$text{'groups_all'}</font>\n";
69                 print "<table width=100%>\n";
70                 foreach $u (&get_groups_list()) {
71                         if ($in{'group'} eq $u->[0]) { print "<tr $cb>\n"; }
72                         else { print "<tr>\n"; }
73                         print "<td width=20%><a href=\"\" onClick='return addgroup(\"$u->[0]\", \"$u->[3]\")'>$u->[0]</a></td>\n";
74                         print "<td>$u->[3]</td> </tr>\n";
75                         }
76                 print "</table>\n";
77                 &popup_footer();
78                 }
79         elsif ($in{'frame'} == 2) {
80                 # show chosen groups
81                 &popup_header();
82                 print "<font size=+1>$text{'groups_sel'}</font>\n";
83                 print <<'EOF';
84 <table width=100%>
85 <script>
86 function sub(j)
87 {
88 sel2 = new Array(); selr2 = new Array();
89 for(k=0,l=0; k<top.sel.length; k++) {
90         if (k != j) {
91                 sel2[l] = top.sel[k];
92                 selr2[l] = top.selr[k];
93                 l++;
94                 }
95         }
96 top.sel = sel2; top.selr = selr2;
97 location = location;
98 return false;
99 }
100 for(i=0; i<top.sel.length; i++) {
101         document.write("<tr>\n");
102         document.write("<td><a href=\"\" onClick='return sub("+i+")'>"+top.sel[i]+"</a></td>\n");
103         document.write("<td>"+top.selr[i]+"</td>\n");
104         }
105 </script>
106 </table>
107 EOF
108                 &popup_footer();
109                 }
110         elsif ($in{'frame'} == 3) {
111                 # output OK and Cancel buttons
112                 &popup_header();
113                 print "<script>\n";
114                 print "function qjoin(l)\n";
115                 print "{\n";
116                 print "rv = \"\";\n";
117                 print "for(i=0; i<l.length; i++) {\n";
118                 print "    if (rv != '') rv += ' ';\n";
119                 print "    if (l[i].indexOf(' ') < 0) rv += l[i];\n";
120                 print "    else rv += '\"'+l[i]+'\"'\n";
121                 print "    }\n";
122                 print "return rv;\n";
123                 print "}\n";
124                 print "</script>\n";
125                 print "<form>\n";
126                 print "<input type=button value=\"$text{'groups_ok'}\" ",
127                       " onClick='top.opener.ifield.value = qjoin(top.sel); ",
128                       "top.close()'>\n";
129                 print "<input type=button value=\"$text{'groups_cancel'}\" ",
130                       "onClick='top.close()'>\n";
131                 print "&nbsp;&nbsp;<input type=button value=\"$text{'groups_clear'}\" onClick='top.sel = new Array(); top.selr = new Array(); top.frames[1].location = top.frames[1].location'>\n";
132                 print "</form>\n";
133                 &popup_footer();
134                 }
135         }
136 else {
137         # selecting just one group .. display a list of all groups to
138         # choose from
139         &popup_header($text{'groups_title2'});
140         print "<script>\n";
141         print "function select(f)\n";
142         print "{\n";
143         print "top.opener.ifield.value = f;\n";
144         print "top.close();\n";
145         print "return false;\n";
146         print "}\n";
147         print "</script>\n";
148         print "<table width=100%>\n";
149         foreach $u (&get_groups_list()) {
150                 if ($in{'group'} eq $u->[0]) { print "<tr $cb>\n"; }
151                 else { print "<tr>\n"; }
152                 print "<td width=20%><a href=\"\" onClick='return select(\"$u->[0]\")'>$u->[0]</a></td>\n";
153                 print "<td>$u->[3]</td> </tr>\n";
154                 }
155         print "</table>\n";
156         &popup_footer();
157         }
158
159 sub get_groups_list
160 {
161 local(@ginfo, @groups, %gcan, %found);
162 if ($access{'gedit_mode'} == 2 || $access{'gedit_mode'} == 3) {
163         map { $gcan{$_}++ } split(/\s+/, $access{'gedit'});
164         }
165 setgrent();
166 while(@ginfo = getgrent()) {
167         @mems = &unique( split(/ /, $ginfo[3]), @{$members{$ginfo[2]}} );
168         if (@mems > 3) { @mems = (@mems[0..1], "..."); }
169         $ginfo[3] = join(' ', @mems);
170         if ($access{'gedit_mode'} == 0 ||
171             $access{'gedit_mode'} == 2 && $gcan{$ginfo[0]} ||
172             $access{'gedit_mode'} == 3 && !$gcan{$ginfo[0]} ||
173             $access{'gedit_mode'} == 4 &&
174                 (!$access{'gedit'} || $ginfo[2] >= $access{'gedit'}) &&
175                 (!$access{'gedit2'} || $ginfo[2] <= $access{'gedit2'})) {
176                 push(@groups, [ @ginfo ]) if (!$found{$ginfo[0]}++);
177                 }
178         }
179 endgrent() if ($gconfig{'os_type'} ne 'hpux');
180 return sort { $a->[0] cmp $b->[0] } @groups;
181 }
182
183 # split_quoted(string)
184 sub split_quoted
185 {
186 local @rv;
187 local $str = $_[0];
188 while($str =~ /^\s*(\S*"[^"]+"\S*)(.*)$/ || $str =~ /^\s*(\S+)(.*)$/) {
189         $str = $2;
190         local $g = $1;
191         $g =~ s/"//g;
192         push(@rv, $g);
193         }
194 return @rv;
195 }
196