#!/usr/bin/perl
# edit_user.cgi
# Display details of an existing user and allow editing

require './ldap-cyrus-lib.pl';
&ReadParse();
$ldap = &ldap_connect();
if ($in{'new'}) {
	&header($text{'uedit_title2'}, "");
	$pass = $uconfig{'lock_string'};
	$shell = $uconfig{'default_shell'} if ($uconfig{'default_shell'});
	}
else {
	&header($text{'uedit_title'}, "");
	$rv = $ldap->search(base => $in{'dn'},
			    scope => 'base',
			    filter => '(objectClass=posixAccount)');
	($uinfo) = $rv->all_entries;
	$user = $uinfo->get_value('uid');
	$uid = $uinfo->get_value('uidNumber');
	$real = $uinfo->get_value('cn');
	$home = $uinfo->get_value('homeDirectory');
	$shell = $uinfo->get_value('loginShell');
	$gid = $uinfo->get_value('gidNumber');
	$pass = $uinfo->get_value('userPassword');
	foreach $oc ($uinfo->get_value('objectClass')) {
		$oclass{$oc} = 1;
		}
	@alias = $uinfo->get_value('alias');
	}
print "<hr>\n";

# build a list of used shells and uids
$rv = $ldap->search(base => &get_user_base(),
		    filter => '(objectClass=posixAccount)');
@shlist = ($uconfig{'default_shell'} ? ( $uconfig{'default_shell'} ) : ( ),
	   "/bin/sh", "/bin/csh", "/bin/false");
foreach $u ($rv->all_entries) {
	$used{$u->get_value('uidNumber')}++;
	push(@shlist, $u->get_value('loginShell'))
		if ($u->get_value('loginShell'));
	}
open(SHELLS, "/etc/shells");
while(<SHELLS>) {
	s/\r|\n//g;
	s/#.*$//;
	push(@shlist, $_) if (/\S/);
	}
close(SHELLS);
@shlist = &unique(@shlist);

print "<form action=save_user.cgi method=post>\n";
print "<input type=hidden name=new value='$in{'new'}'>\n";
print "<input type=hidden name=dn value='$in{'dn'}'>\n";

print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'uedit_details'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

if (!$in{'new'}) {
	print "<tr> <td><b>$text{'uedit_dn'}</b></td>\n";
	print "<td colspan=3><tt>$in{'dn'}</tt></td> </tr>\n";
	}

# Show username input
print "<tr> <td><b>$text{'user'}</b></td>\n";
print "<td><input name=user size=10 value=\"$user\"></td>\n";

# Show UID input, filled in with a default for new users
print "<td><b>$text{'uid'}</b></td>\n";
if ($in{'new'}) {
	# Find the first free UID above the base
	$newuid = $uconfig{'base_uid'};
	while($used{$newuid}) {
		$newuid++;
		}
	print "<td><input name=uid size=10 value='$newuid'></td> </tr>\n";
	}
else {
	print "<td><input name=uid size=10 value='$uid'></td> </tr>\n";
	}

# Show real name input
print "<tr> <td><b>$text{'real'}</b></td>\n";
print "<td><input name=real size=20 value=\"$real\"></td>\n";

# Show home directory input, with an 'automatic' option
print "<td><b>$text{'home'}</b></td>\n";
print "<td>\n";
if ($uconfig{'home_base'}) {
	local $hb = $in{'new'} ||
	    &useradmin::auto_home_dir($uconfig{'home_base'}, $user) eq $home;
	printf "<input type=radio name=home_base value=1 %s> %s\n",
		$hb ? "checked" : "", $text{'uedit_auto'};
	printf "<input type=radio name=home_base value=0 %s>\n",
		$hb ? "" : "checked";
	printf "<input name=home size=25 value=\"%s\"> %s\n",
		$hb ? "" : $home,
		&file_chooser_button("home", 1);
	}
else {
	print "<input name=home size=25 value=\"$home\">\n",
	      &file_chooser_button("home", 1);
	}
print "</td> </tr>\n";

# Show shell selection menu
print "<tr> <td valign=top><b>$text{'shell'}</b></td>\n";
print "<td valign=top><select name=shell>\n";
foreach $s (@shlist) {
	printf "<option value='%s' %s>%s\n", $s,
		$s eq $shell ? "selected" : "",
		$s eq "" ? "&lt;None&gt;" : $s;
	}
print "<option value=*>$text{'uedit_other'}\n";
print "</select></td>\n";

# Show password fields
if ($in{'new'} && $uconfig{'random_password'}) {
	&seed_random();
	foreach (1 .. 15) {
		$random_password .= $random_password_chars[
					rand(scalar(@random_password_chars))];
		}
	}
print "<td valign=top rowspan=4><b>$text{'pass'}</b>",
      "</td> <td rowspan=4 valign=top>\n";
printf"<input type=radio name=passmode value=0 %s> %s<br>\n",
	$pass eq "" && $random_password eq "" ? "checked" : "",
	$uconfig{'empty_mode'} ? $text{'none1'} : $text{'none2'};
printf"<input type=radio name=passmode value=1 %s> $text{'nologin'}<br>\n",
	$pass eq $uconfig{'lock_string'} && $random_password eq "" ? "checked" : "";
printf "<input type=radio name=passmode value=3 %s> $text{'clear'}\n",
	$random_password ne "" ? "checked" : "";
printf "<input %s name=pass size=15 value='%s'><br>\n",
	$uconfig{'passwd_stars'} ? "type=password" : "",
	$uconfig{'random_password'} && $n eq "" ? $random_password : "";
if (!$in{'new'}) {
	printf "<input type=radio name=passmode value=2 %s> $text{'encrypted'}\n",
		$pass && $pass ne $uconfig{'lock_string'} ? "checked" : "";
	printf "<input name=encpass size=13 value=\"%s\">\n",
		$pass && $pass ne $uconfig{'lock_string'} ? $pass : "";
	}
print "</td> </tr>\n";

# Show alternate shell field
print "<tr> <td valign=top>$text{'uedit_other'}</td>\n";
print "<td valign=top><input size=25 name=othersh>\n";
print &file_chooser_button("othersh", 0),"</td> </tr>\n";
print "<tr> <td colspan=2><br></td> </tr>\n";

print "</table></td></tr></table><p>\n";

# Show primary group
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'uedit_gmem'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";
print "<tr> <td valign=top><b>$text{'group'}</b></td> <td valign=top>\n";
printf "<input name=gid size=8 value=\"%s\"> %s</td>\n",
	$in{'new'} ? $uconfig{'default_group'} : getgrgid($gid),
	&group_chooser_button("gid");

# Show secondary groups
print "<td valign=top><b>$text{'uedit_2nd'}</b></td>\n";
print "<td><select name=sgid multiple size=5>\n";
$base = &get_group_base();
$rv = $ldap->search(base => $base,
		    filter => '(objectClass=posixGroup)');
@defsecs = split(/\s+/, $config{'default_secs'});
foreach $g (sort { lc($a->dn()) cmp lc($b->dn()) } $rv->all_entries) {
	$group = $g->get_value("cn");
	@mems = $g->get_value("memberUid");
	local $ismem = &indexof($user, @mems) >= 0;
	$ismem = 1 if (&indexof($group, @defsecs) >= 0 && $in{'new'});
	printf "<option value=\"%s\" %s>%s (%s)\n",
		$g->get_value('gidNumber'), $ismem ? "selected" : "",
		$group, $g->get_value('gidNumber');
	}
print "</select></td> </tr>\n";
print "</table></td></tr></table><p>\n";

# Show capabilties section
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'uedit_cap'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

print "<tr> <td><b>$text{'uedit_samba'}</b></td>\n";
printf "<td><input type=radio name=samba value=1 %s> %s\n",
	$oclass{'sambaAccount'} ? "checked" : "", $text{'yes'};
printf "<input type=radio name=samba value=0 %s> %s</td>\n",
	$oclass{'sambaAccount'} ? "" : "checked", $text{'no'};

if ($config{'imap_host'}) {
	print "<td><b>$text{'uedit_cyrus'}</b></td>\n";
	printf "<td><input type=radio name=cyrus value=1 %s> %s\n",
		$oclass{$cyrus_class} ? "checked" : "", $text{'yes'};
	printf "<input type=radio name=cyrus value=0 %s> %s</td> </tr>\n",
		$oclass{$cyrus_class} ? "" : "checked", $text{'no'};

	print "<tr> <td><b>$text{'uedit_alias'}</b></td> <td colspan=3>\n";
	printf "<input name=alias size=50 value='%s'></td> </tr>\n",
		join(" ", @alias);
	}
else {
	printf "<input type=hidden name=cyrus value='%s'>\n",
		$oclass{$cyrus_class};
	print "<td colspan=2 width=50%></td> </tr>\n";
	}

print "</table></td></tr></table>\n";

print "<table width=100%><tr>\n";
if ($in{'new'}) {
	print "<td><input type=submit value='$text{'create'}'></td>\n";
	}
else {
	print "<td><input type=submit value='$text{'save'}'></td>\n";
	print "<td align=right><input type=submit name=delete ",
	      "value='$text{'delete'}'></td>\n";
	}
print "</tr></table>\n";

print "<hr>\n";
&footer("", $text{'index_return'});

