Handle hostnames with upper-case letters
[webmin.git] / inetd / inetd-generic-lib.pl
1
2 # list_services()
3 # Returns a list of services from the services file, each being an array of
4 #  line name port protocol aliases index
5 sub list_services
6 {
7 local(@rv, $l);
8 $l = 0;
9 system("$config{'get_services_command'}") if ($config{'get_services_command'});
10 open(SERVICES, $config{services_file});
11 while(<SERVICES>) {
12         chop; s/#.*$//g;
13         if (/^(\S+)\s+([0-9]+)\/(\S+)\s*(.*)$/) {
14                 push(@rv, [ $l, $1, $2, $3, $4, scalar(@rv) ]);
15                 if ($config{'ipv6'}) {
16                         push(@rv, [ $l, $1, $2, $3.'6', $4, scalar(@rv) ]);
17                         }
18                 }
19         $l++;
20         }
21 close(SERVICES);
22 return @rv;
23 }
24
25 # create_service(name, port, proto, aliases)
26 # Add a new service to the list
27 sub create_service
28 {
29 local $p = $_[2];
30 if ($config{'ipv6'} && $p =~ /^(\S+)6$/) {
31         # don't add the service if it is already there
32         foreach $s (&list_services()) {
33                 return if ($s->[1] eq $_[0] && $s->[2] == $_[1] &&
34                         $s->[3] eq $1);
35                 }
36         $p =~ s/6$//;
37         }
38 &open_tempfile(SERVICES, ">> $config{services_file}");
39 &print_tempfile(SERVICES, "$_[0]\t$_[1]/$p",$_[3] ? "\t$_[3]\n" : "\n");
40 &close_tempfile(SERVICES);
41 system("$config{'put_services_command'}") if ($config{'put_services_command'});
42 }
43
44
45 # modify_service(line, name, port, proto, aliases)
46 # Change an existing service
47 sub modify_service
48 {
49 local(@serv);
50 open(SERVICES, $config{services_file});
51 @serv = <SERVICES>;
52 close(SERVICES);
53 $serv[$_[0]] = "$_[1]\t$_[2]/$_[3]".($_[4] ? "\t$_[4]\n" : "\n");
54 &open_tempfile(SERVICES, "> $config{services_file}");
55 &print_tempfile(SERVICES, @serv);
56 &close_tempfile(SERVICES);
57 system("$config{'put_services_command'}") if ($config{'put_services_command'});
58 }
59
60 # delete_service(line)
61 sub delete_service
62 {
63 local(@serv);
64 open(SERVICES, $config{services_file});
65 @serv = <SERVICES>;
66 close(SERVICES);
67 splice(@serv, $_[0], 1);
68 &open_tempfile(SERVICES, "> $config{services_file}");
69 &print_tempfile(SERVICES, @serv);
70 &close_tempfile(SERVICES);
71 system("$config{'put_services_command'}") if ($config{'put_services_command'});
72 }
73
74 # list_protocols()
75 # Returns a list of supported protocols on this system
76 sub list_protocols
77 {
78 local(@rv);
79 open(PROT, $config{protocols_file});
80 while(<PROT>) {
81         chop; s/#.*$//g;
82         if (!/\S/) { next; }
83         /^(\S+)\s+/;
84         push(@rv, $1);
85         if ($config{'ipv6'}) {
86                 if ($1 eq 'tcp') { push(@rv, 'tcp6'); }
87         elsif ($1 eq 'udp') { push(@rv, 'udp6'); }
88                 }
89         }
90 close(PROT);
91 return &unique(@rv);
92 }
93
94 # list_inets()
95 # Returns a list of service details handled by inetd. RPC services
96 # will have a name like foo/1 or bar/1-3, where the thing after the / is
97 # the version or versions supported. For each service, the list contains
98 #  line active? rpc? name type protocol wait user path|internal args file
99 sub list_inets
100 {
101 # build list of inetd files
102 local (@files, @rv, $l);
103 @files = ( $config{'inetd_conf_file'} );
104 opendir(DIR, $config{'inetd_dir'});
105 foreach $f (readdir(DIR)) {
106         next if ($f =~ /^\./);
107         push(@files, "$config{'inetd_dir'}/$f");
108         }
109 closedir(DIR);
110
111 # parse each file
112 foreach $f (@files) {
113         $l = 0;
114         open(INET, $f);
115         while(<INET>) {
116                 chop;
117                 if (/^(#+|#<off>#)?\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\??\/\S+|internal)\s*(.*)$/) {
118                         push(@rv, [ $l, !$1, 0, $2, $3, $4,
119                                     $5, $6, $7, $8, $f ]);
120                         $rv[$#rv]->[2] = ($2 =~ /\//);
121                         }
122                 $l++;
123                 }
124         close(INET);
125         }
126 return @rv;
127 }
128
129 # create_inet(enabled, name, type, protocol, wait, user, program, args)
130 # Add a new service to the main inetd config file
131 sub create_inet
132 {
133 &open_tempfile(INET, ">> $config{inetd_conf_file}");
134 &print_tempfile(INET,
135          ($_[0] ? "" : "#")."$_[1]\t$_[2]\t$_[3]\t$_[4]\t$_[5]\t$_[6]".
136          ($_[7] ? "\t$_[7]\n" : "\n"));
137 &close_tempfile(INET);
138 }
139
140
141 # modify_inet(line, enabled, name, type, protocol,
142 #             wait, user, program, args, file)
143 # Modify an existing inetd service
144 sub modify_inet
145 {
146 local(@inet);
147 open(INET, $_[9]);
148 @inet = <INET>;
149 close(INET);
150 $inet[$_[0]] = ($_[1] ? "" : "#")."$_[2]\t$_[3]\t$_[4]\t$_[5]\t$_[6]\t$_[7]".
151                ($_[8] ? "\t$_[8]\n" : "\n");
152 &open_tempfile(INET, ">$_[9]");
153 &print_tempfile(INET, @inet);
154 &close_tempfile(INET);
155 }
156
157
158 # delete_inet(line, file)
159 # Delete an internet service at some line
160 sub delete_inet
161 {
162 local(@inet);
163 open(INET, $_[1]);
164 @inet = <INET>;
165 close(INET);
166 splice(@inet, $_[0], 1);
167 &open_tempfile(INET, ">$_[1]");
168 &print_tempfile(INET, @inet);
169 &close_tempfile(INET);
170 }
171
172 %prot_name = ("ip", "Internet Protocol",
173               "tcp", "Transmission Control Protocol",
174               "udp", "User Datagram Protocol",
175               "tcp6", "Transmission Control Protocol IPv6",
176               "udp6", "User Datagram Protocol IPv6");
177
178 1;
179