Handle hostnames with upper-case letters
[webmin.git] / init / save_upstart.cgi
1 #!/usr/local/bin/perl
2 # Create, update or delete an upstart service
3
4 require './init-lib.pl';
5 &error_setup($text{'upstart_err'});
6 $access{'bootup'} || &error($text{'edit_ecannot'});
7 &ReadParse();
8 @upstarts = &list_upstart_services();
9 $cfile = "/etc/init/$in{'name'}.conf";
10
11 # Get the service
12 if (!$in{'new'}) {
13         ($u) = grep { $_->{'name'} eq $in{'name'} } @upstarts;
14         $u || &error($text{'upstart_egone'});
15         $u->{'legacy'} && &error($text{'upstart_elegacy'});
16         }
17
18 if ($in{'start'} || $in{'stop'}) {
19         # Just redirect to the start page
20         &redirect("mass_upstarts.cgi?d=".&urlize($in{'name'})."&".
21                   ($in{'start'} ? "start=1" : "stop=1").
22                   "&return=".&urlize($in{'name'}));
23         exit;
24         }
25
26 if ($in{'delete'}) {
27         # Delete the service
28         &disable_at_boot($in{'name'});
29         &stop_upstart_service($in{'name'});
30         &delete_upstart_service($in{'name'});
31         &webmin_log("delete", "upstart", $in{'name'});
32         }
33 elsif ($in{'new'}) {
34         # Validate inputs and check for clash
35         $in{'name'} =~ /^[a-z0-9\.\_\-]+$/i ||
36                 &error($text{'upstart_ename'});
37         ($clash) = grep { $_->{'name'} eq $in{'name'} } @upstarts;
38         $clash && &error($text{'upstart_eclash'});
39         $in{'desc'} || &error($text{'upstart_edesc'});
40         $in{'server'} =~ /\S/ || &error($text{'upstart_eserver'});
41         ($bin, $args) = split(/\s+/, $in{'server'});
42         &has_command($bin) || &error($text{'upstart_eserver2'});
43         $in{'server'} =~ /;|\&\&|\|\|/ && &error($text{'upstart_eserver3'});
44         $in{'prestart'} =~ s/\r//g;
45
46         # Create the config file
47         &create_upstart_service($in{'name'}, $in{'desc'}, $in{'server'},
48                                 $in{'prestart'}, $in{'fork'});
49
50         # Enable at boot if selected
51         if ($in{'boot'} == 0) {
52                 &disable_at_boot($in{'name'});
53                 }
54         else {
55                 &enable_at_boot($in{'name'});
56                 }
57
58         &webmin_log("create", "upstart", $in{'name'});
59         }
60 else {
61         # Just save the config file
62         $in{'conf'} =~ /\S/ || &error($text{'upstart_econf'});
63         $in{'conf'} =~ s/\r//g;
64         &open_lock_tempfile(CONF, ">$cfile");
65         &print_tempfile(CONF, $in{'conf'});
66         &close_tempfile(CONF);
67
68         # Enable or disable
69         if (defined($in{'boot'})) {
70                 if ($in{'boot'} == 0) {
71                         &disable_at_boot($in{'name'});
72                         }
73                 else {
74                         &enable_at_boot($in{'name'});
75                         }
76                 }
77
78         &webmin_log("modify", "upstart", $in{'name'});
79         }
80 &redirect("");
81