2 # Common functions for NIS client and server management
4 BEGIN { push(@INC, ".."); };
7 if (-r "$module_root_directory/$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl") {
8 do "$gconfig{'os_type'}-$gconfig{'os_version'}-lib.pl";
11 do "$gconfig{'os_type'}-lib.pl";
13 if ($gconfig{'os_type'} =~ /-linux$/) {
16 &foreign_require("init", "init-lib.pl");
19 # Returns the full path to some init script
22 local %iconfig = &foreign_config("init");
23 return "$iconfig{'init_dir'}/$_[0]";
27 # Parses lines of nsswitch.conf into an array
31 open(SWITCH, $config{'nsswitch_conf'});
35 if (/^\s*(\S+):\s*(.*)/) {
36 local $sw = { 'service' => $1,
45 # save_nsswitch(service, order)
46 # Updates the line for some service in nsswitch.conf
49 local $lref = &read_file_lines($config{'nsswitch_conf'});
51 if ($l =~ /^\s*(\S+):/ && $1 eq $_[0]) {
58 # table_edit_setup(table, line, splitter)
59 # Returns &table, &lnums, line1, line2, ...
62 local @tables = &list_nis_tables();
63 local $t = $tables[$_[0]];
64 return ( $t ) if (!defined($_[1]));
65 local @lnums = ( $_[1] );
66 local $lref = &read_file_lines($t->{'files'}->[0]);
67 local @lines = ( [ split($_[2], $lref->[$_[1]]) ] );
69 for($i=1; $t->{'files'}->[$i]; $i++) {
70 local $lref2 = &read_file_lines($t->{'files'}->[$i]);
72 foreach $l (@$lref2) {
73 local @line2 = split($_[2], $l);
74 if ($line2[0] eq $lines[0]->[0]) {
76 push(@lines, \@line2);
82 return ($t, \@lnums, @lines);
85 # table_add(&table, separator, &record, ...)
86 # Adds a record to an NIS table
90 foreach $f (@{$_[0]->{'files'}}) {
91 local $lref = &read_file_lines($f);
92 push(@$lref, join($_[1], @{$_[$i++]}));
97 # table_delete(&table, &lnums)
98 # Delete a record from an NIS table
102 foreach $f (@{$_[0]->{'files'}}) {
103 local $lref = &read_file_lines($f);
104 splice(@$lref, $_[1]->[$i], 1);
110 # table_update(&table, &lnums, separator, &record, ...)
111 # Modify a record in an NIS table
115 foreach $f (@{$_[0]->{'files'}}) {
116 local $lref = &read_file_lines($f);
117 splice(@$lref, $_[1]->[$i], 1, join($_[2], @{$_[$i+3]}));
123 # date_input(day, month, year, prefix)
126 print "<input name=$_[3]d size=3 value='$_[0]'>";
127 print "/<select name=$_[3]m>\n";
130 printf "<option value=%d %s>%s\n",
131 $m, $_[1] eq $m ? 'selected' : '', $text{"smonth_$m"};
134 print "/<input name=$_[3]y size=5 value='$_[2]'>";
135 print &date_chooser_button("$_[3]d", "$_[3]m", "$_[3]y");
138 # parse_ypserv_conf()
139 # Returns &opts, &maps
140 sub parse_ypserv_conf
142 local (%opts, @hosts);
144 open(CONF, $ypserv_conf);
148 if (/^\s*([^:\s]+):\s*(yes|no)/) {
150 $opts{$1} = { 'name' => $1,
151 'value' => $2 eq 'yes' ? 1 : 0,
154 elsif (/^\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*(none|port|mangle|deny)(\/mangle(:(\d+))?)?/) {
155 # Found a host and domain line (new format)
156 push(@hosts, { 'host' => $1,
160 'mangle' => $5 ? 1 : 0,
164 elsif (/^\s*([^:\s]+)\s*:\s*([^:\s]+)\s*:\s*([^:\s]+)(\s*:\s*([^:\s]+))?(\s*:\s*([^:\s]+))?/) {
165 # Found a host line (old format)
166 push(@hosts, { 'host' => $1,
169 'mangle' => $5 eq 'yes' ? 1 : 0,
170 'field' => $7 eq '' ? 2 : $7,
176 return (\%opts, \@hosts);
179 # parse_yp_makefile()
180 # Returns hashes of makefile variables and rules
181 sub parse_yp_makefile
183 # First parse joined lines
185 local (@lines, $llast);
186 open(MAKE, $yp_makefile);
189 local $slash = (s/\\$//);
192 $llast->{'value'} .= " $_";
193 $llast->{'eline'} = $lnum;
196 push(@lines, { 'value' => $_,
200 $llast = $slash ? $lines[$#lines] : undef;
205 # Then look for variables and rules
206 local ($i, %var, %rule);
207 for($i=0; $i<@lines; $i++) {
208 if ($lines[$i]->{'value'} =~ /^\s*(\S+)\s*=\s*(.*)/) {
210 $var{$1} = { 'name' => $1,
213 'line' => $lines[$i]->{'line'},
214 'eline' => $lines[$i]->{'eline'} };
216 elsif ($lines[$i]->{'value'} =~ /^\s*(\S+)\s*\+=\s*(.*)/) {
217 # Adding to a variable
219 $var{$1}->{'value'} .= ' '.$2;
222 elsif ($lines[$i]->{'value'} =~ /^\s*(\S+):\s*(.*)/) {
223 # Found a makefile rule
224 $rule{$1} = { 'name' => $1,
227 'line' => $lines[$i]->{'line'},
228 'eline' => $lines[$i]->{'eline'} };
229 if ($lines[$i+1]->{'value'} =~ /^\s+/) {
230 $rule{$1}->{'code'} = $lines[$i+1]->{'value'};
231 $rule{$1}->{'eline'} = $lines[$i+1]->{'eline'};
236 return ( \%var, \%rule );
239 # expand_vars(string, &vars)
243 while($rv =~ /^(.*)\$\(([A-Za-z0-9_]+)\)(.*)$/) {
244 # if (substr($_[1]->{$2}->{'value'}, 0, 7) eq '$(shell') {
245 # $rv = $1."\0(".$2.")".$3;
248 $rv = $1.$_[1]->{$2}->{'value'}.$3;
255 # update_makefile(&old, value, [value]);
258 local $lref = &read_file_lines($yp_makefile);
260 if ($_[0]->{'type'} == 0) {
261 @n = ( "$_[0]->{'name'} = $_[1]" );
264 @n = ( "$_[0]->{'name'}: $_[1]", $_[2] );
266 splice(@$lref, $_[0]->{'line'}, $_[0]->{'eline'} - $_[0]->{'line'} + 1, @n);