Handle hostnames with upper-case letters
[webmin.git] / ldap-server / create.cgi
1 #!/usr/local/bin/perl
2 # Actually create a new base DN
3
4 require './ldap-server-lib.pl';
5 &ReadParse();
6 &error_setup($text{'create_err'});
7 $access{'create'} || &error($text{'create_ecannot'});
8 $ldap = &connect_ldap_db();
9 ref($ldap) || &error($ldap);
10
11 # Validate inputs
12 if ($in{'mode'} == 0) {
13         $in{'domain'} =~ /^[a-z0-9\.\-]+$/ || &error($text{'create_edom'});
14         @p = split(/\./, $in{'domain'});
15         $dn = join(", ", map { "dc=$_" } @p);
16         }
17 else {
18         $in{'dn'} =~ /^\S+=\S+/ || &error($text{'create_edn'});
19         $dn = $in{'dn'};
20         }
21
22 # Work out class for the DN
23 $schema = $ldap->schema();
24 @allocs = map { $_->{'name'} }
25            grep { $_->{'structural'} }
26                 $schema->all_objectclasses();
27 @ocs = ( );
28 foreach my $poc ("top", "domain") {
29         if (&indexof($poc, @allocs) >= 0) {
30                 push(@ocs, $poc);
31                 }
32         }
33 @ocs || &error(&text('create_eoc'));
34
35 # Do it, while showing the user
36 &ui_print_unbuffered_header(undef, $text{'create_title'}, "");
37
38 # Create the DN
39 print &text('create_doingdn', "<tt>".&html_escape($dn)."</tt>"),"<br>\n";
40 @attrs = ( "objectClass", \@ocs );
41 if (&indexof("domain", @ocs) >= 0) {
42         # Domain class needs dc
43         if ($dn =~ /^([^=]+)=([^, ]+)/) {
44                 push(@attrs, $1, $2);
45                 }
46         }
47 $rv = $ldap->add($dn, attr => \@attrs);
48 if (!$rv || $rv->code) {
49         print &text('create_edoingdn', &ldap_error($rv)),"<p>\n";
50         }
51 else {
52         print $text{'create_done'},"<p>\n";
53         $ok = 1;
54         }
55
56 if ($ok && $in{'example'}) {
57         # Add the example user/alias
58         if ($in{'example'} == 1 || $in{'example'} == 2) {
59                 # User
60                 $edn = "uid=example, ".$dn;
61                 @attrs = ( "cn", "Example user",
62                            "sn", "Example user",
63                            "uid", "example",
64                            "uidNumber", 9999,
65                            "gidNumber", 9999,
66                            "loginShell", "/bin/sh",
67                            "homeDirectory", "/home/example",
68                            "objectClass", [ "posixAccount", "person" ],
69                            "userPassword", "*LK*" );
70                 if ($in{'example'} == 2) {
71                         # With mail
72                         push(@attrs, "mail", "example\@example.com");
73                         }
74                 }
75         elsif ($in{'example'} == 3) {
76                 # Virtuser
77                 # XXX not sure about these .. is there any standard?
78                 $edn = "cn=example\@example.com, ".$dn;
79                 @attrs = ( "mail", "example\@example.com",
80                            "mailForwardingAddress", "example\@somewhere.com",
81                            "objectClass", [ "top" ] );
82                 }
83         elsif ($in{'example'} == 4) {
84                 # Unix group
85                 $edn = "cn=example, ".$dn;
86                 @attrs = ( "cn", "example",
87                            "gidNumber", 9999,
88                            "memberUid", "example",
89                            "objectClass", [ "posixGroup" ] );
90                 }
91
92         print &text('create_doingex',
93                     "<tt>".&html_escape($edn)."</tt>"),"<br>\n";
94         $rv = $ldap->add($edn, attr => \@attrs);
95         if (!$rv || $rv->code) {
96                 print &text('create_edoingex', &ldap_error($rv)),"<p>\n";
97                 }
98         else {
99                 print $text{'create_done'},"<p>\n";
100                 }
101         }
102
103 if ($ok) {
104         &webmin_log("create", undef, $dn);
105         }
106 &ui_print_footer("", $text{'index_return'});
107