diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2020-06-18 12:41:32 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2020-06-18 12:41:32 +0300 |
commit | fb0cd0f84d0ecfba0c5a5b0045eee0ede0477a0e (patch) | |
tree | 30229f5483a036ffc3c21e429d7caa94587e1478 | |
parent | e30ddffa198dedeed50c1d9e2bbb98cabbec2eae (diff) | |
download | netsnmp-sendmail-fb0cd0f84d0ecfba0c5a5b0045eee0ede0477a0e.tar.gz netsnmp-sendmail-fb0cd0f84d0ecfba0c5a5b0045eee0ede0477a0e.tar.bz2 |
Update netsnmp-sendmail-setup
* netsnmp-sendmail-setup: Add --deconfigure option: remove previosly
added configuration statements.
-rwxr-xr-x | netsnmp-sendmail-setup | 213 |
1 files changed, 153 insertions, 60 deletions
diff --git a/netsnmp-sendmail-setup b/netsnmp-sendmail-setup index 86e8002..5e49ad6 100755 --- a/netsnmp-sendmail-setup +++ b/netsnmp-sendmail-setup | |||
@@ -1,7 +1,7 @@ | |||
1 | #!/bin/sh | 1 | #!/bin/sh |
2 | #! -*-perl-*- | 2 | #! -*-perl-*- |
3 | # This file is part of NetSNMP::Sendmail | 3 | # This file is part of NetSNMP::Sendmail |
4 | # Copyright (C) 2019 Sergey Poznyakoff <gray@gnu.org> | 4 | # Copyright (C) 2019-2020 Sergey Poznyakoff <gray@gnu.org> |
5 | # | 5 | # |
6 | # This program is free software; you can redistribute it and/or modify | 6 | # This program is free software; you can redistribute it and/or modify |
7 | # it under the terms of the GNU General Public License as published by | 7 | # it under the terms of the GNU General Public License as published by |
@@ -35,6 +35,8 @@ sub addts { | |||
35 | . "\n"; | 35 | . "\n"; |
36 | } | 36 | } |
37 | 37 | ||
38 | my $my_ts_rx = qr{^# Line added by $0 at }; | ||
39 | |||
38 | use constant { | 40 | use constant { |
39 | EX_OK => 0, # Success | 41 | EX_OK => 0, # Success |
40 | EX_UNCHANGED => 1, # Files not changed | 42 | EX_UNCHANGED => 1, # Files not changed |
@@ -42,9 +44,13 @@ use constant { | |||
42 | EX_USAGE => 64 # Usage error | 44 | EX_USAGE => 64 # Usage error |
43 | }; | 45 | }; |
44 | 46 | ||
47 | use constant { | ||
48 | CMD_SETUP => 0, | ||
49 | CMD_REMOVE => 1 | ||
50 | }; | ||
51 | |||
45 | my $suppress_level = 0; | 52 | my $suppress_level = 0; |
46 | my $dry_run; | 53 | my $dry_run; |
47 | my $changed = 0; | ||
48 | my @updated_services; | 54 | my @updated_services; |
49 | 55 | ||
50 | use constant { | 56 | use constant { |
@@ -54,7 +60,7 @@ use constant { | |||
54 | L_ERR => 3 | 60 | L_ERR => 3 |
55 | }; | 61 | }; |
56 | 62 | ||
57 | use constant MAX_SUPPRESS_LEVEL => L_NOTICE; | 63 | use constant MAX_SUPPRESS_LEVEL => L_WARN; |
58 | 64 | ||
59 | sub printlog { | 65 | sub printlog { |
60 | my $level = shift; | 66 | my $level = shift; |
@@ -96,6 +102,41 @@ sub restart_service { | |||
96 | } | 102 | } |
97 | } | 103 | } |
98 | 104 | ||
105 | sub file_replace { | ||
106 | my ($file, $newfile) = @_; | ||
107 | my $bk = "$file~"; | ||
108 | unlink $bk if -e $bk; | ||
109 | rename $file, $bk or die "can't rename $file to $bk: $!"; | ||
110 | unless (rename $newfile, $file) { | ||
111 | printlog(L_WARN, | ||
112 | "can't rename $newfile to $file: $!; restoring from backup"); | ||
113 | unless (rename $bk, $file) { | ||
114 | printlog(L_ERR, "failed to rename $bk to $file: $!"); | ||
115 | exit(EX_FATAL); | ||
116 | } | ||
117 | } | ||
118 | } | ||
119 | |||
120 | sub file_remline { | ||
121 | my ($file, $fd, $line, $endline) = @_; | ||
122 | $endline //= $line; | ||
123 | printlog(L_NOTICE, | ||
124 | ($endline == $line) ? "editing $file: removing line $line" | ||
125 | : "editing $file: removing lines $line-$endline"); | ||
126 | return if $dry_run; | ||
127 | my $ofd = File::Temp->new(DIR => dirname($file), UNLINK => $dry_run); | ||
128 | seek($fd, 0, SEEK_SET) or die "seek $file: $!"; | ||
129 | my $ln = 0; | ||
130 | while (<$fd>) { | ||
131 | $ln++; | ||
132 | next if ($line <= $ln && $ln <= $endline); | ||
133 | print $ofd $_; | ||
134 | } | ||
135 | close $ofd; | ||
136 | close $fd; | ||
137 | file_replace($file, $ofd->filename); | ||
138 | } | ||
139 | |||
99 | sub scan_snmpd_conf { | 140 | sub scan_snmpd_conf { |
100 | my ($file, $fd) = @_; | 141 | my ($file, $fd) = @_; |
101 | 142 | ||
@@ -107,15 +148,25 @@ sub scan_snmpd_conf { | |||
107 | ++$line; | 148 | ++$line; |
108 | chomp; | 149 | chomp; |
109 | s/^\s+//; | 150 | s/^\s+//; |
151 | |||
152 | if (/$my_ts_rx/) { | ||
153 | $comline = $line; | ||
154 | next; | ||
155 | } | ||
156 | |||
110 | if (/^perl\s+use\s+NetSNMP::Sendmail/) { | 157 | if (/^perl\s+use\s+NetSNMP::Sendmail/) { |
111 | printlog(L_INFO, "$file:$line: NetSNMP::Sendmail already enabled"); | 158 | if ($comline && $comline + 1 == $line) { |
112 | return; | 159 | return (1, $comline, $line); |
160 | } else { | ||
161 | return (1, $line); | ||
162 | } | ||
113 | } | 163 | } |
164 | |||
114 | if (/^perl\s+use/) { | 165 | if (/^perl\s+use/) { |
115 | $insert_line = $line; | 166 | $insert_line = $line; |
116 | } | 167 | } |
117 | } | 168 | } |
118 | return $insert_line || $line + 1; | 169 | return (0, $insert_line || $line + 1); |
119 | } | 170 | } |
120 | 171 | ||
121 | sub update_snmpd_conf { | 172 | sub update_snmpd_conf { |
@@ -140,30 +191,25 @@ sub update_snmpd_conf { | |||
140 | addts $ofd; | 191 | addts $ofd; |
141 | print $ofd "$stmt\n"; | 192 | print $ofd "$stmt\n"; |
142 | } | 193 | } |
143 | $changed++; | 194 | close $ofd; |
144 | return $ofd; | 195 | file_replace($ifile, $ofd->filename) unless ($dry_run); |
145 | } | 196 | } |
146 | 197 | ||
147 | sub edit_snmpd_conf { | 198 | sub edit_snmpd_conf { |
148 | my ($name, $stmt) = @_; | 199 | my ($command, $name, $stmt) = @_; |
149 | my $u = umask(077); | 200 | my $u = umask(077); |
150 | if (open(my $fd, '<', $name)) { | 201 | if (open(my $fd, '<', $name)) { |
151 | if (defined(my $insert_line = scan_snmpd_conf($name, $fd))) { | 202 | my ($found, $line, $endline) = scan_snmpd_conf($name, $fd); |
152 | my $fh = update_snmpd_conf($name, $fd, $insert_line, $stmt); | 203 | if ($command == CMD_SETUP) { |
153 | unless ($dry_run) { | 204 | if ($found) { |
154 | my $bk = "$name~"; | 205 | printlog(L_INFO, "$name:".($endline ? $endline : $line).": NetSNMP::Sendmail already enabled"); |
155 | unlink $bk if -e $bk; | 206 | } else { |
156 | rename $name, $bk or die "can't rename $name to $bk: $!"; | 207 | update_snmpd_conf($name, $fd, $line, $stmt); |
157 | unless (rename $fh->filename, $name) { | 208 | push @updated_services, 'snmpd'; |
158 | printlog(L_WARN, | ||
159 | "can't rename ".$fh->filename." to $name: $!; restoring from backup"); | ||
160 | unless (rename $bk, $name) { | ||
161 | printlog(L_ERR, "failed to rename $bk to $name: $!"); | ||
162 | exit(EX_FATAL); | ||
163 | } | ||
164 | } | ||
165 | } | 209 | } |
166 | push @updated_services, 'snmpd'; | 210 | } elsif ($found) { |
211 | file_remline($name, $fd, $line, $endline); | ||
212 | push @updated_services, 'snmpd'; | ||
167 | } | 213 | } |
168 | close $fd; | 214 | close $fd; |
169 | } else { | 215 | } else { |
@@ -210,50 +256,73 @@ sub scan_sendmail_mc { | |||
210 | my $fd = shift; | 256 | my $fd = shift; |
211 | my $name; | 257 | my $name; |
212 | my $last_nl; | 258 | my $last_nl; |
259 | my $comline; | ||
260 | my $line = 0; | ||
213 | while (<$fd>) { | 261 | while (<$fd>) { |
262 | $line++; | ||
214 | $last_nl = chomp; | 263 | $last_nl = chomp; |
215 | s/^\s+//; | 264 | s/^\s+//; |
265 | |||
266 | if (/$my_ts_rx/) { | ||
267 | $comline = $line; | ||
268 | next; | ||
269 | } | ||
216 | if (/^define\(\s*`?STATUS_FILE'?\s*,\s*`?(.+?)'?\s*\)/) { | 270 | if (/^define\(\s*`?STATUS_FILE'?\s*,\s*`?(.+?)'?\s*\)/) { |
217 | $name = $1; | 271 | $name = $1; |
272 | last | ||
218 | } | 273 | } |
219 | } | 274 | } |
220 | return ($name, $last_nl); | 275 | |
276 | return ($name, $last_nl, | ||
277 | ($comline && $comline + 1 == $line) ? ($comline, $line) : ($line)); | ||
221 | } | 278 | } |
222 | 279 | ||
223 | sub edit_sendmail_mc { | 280 | sub edit_sendmail_mc { |
224 | my $file = shift; | 281 | my ($command, $file, $default_statfile) = @_; |
225 | if (open(my $fd, '+<', $file)) { | 282 | if (open(my $fd, '+<', $file)) { |
226 | my $need_make; | 283 | my $need_make; |
227 | my ($statfile, $last_nl) = scan_sendmail_mc($fd); | 284 | my ($statfile, $last_nl, $line, $endline) = scan_sendmail_mc($fd); |
228 | if ($statfile) { | 285 | if ($command == CMD_SETUP) { |
229 | printlog(L_INFO, "status file $statfile"); | 286 | if ($statfile) { |
230 | } else { | 287 | printlog(L_INFO, |
231 | $statfile = shift; | 288 | "$file:".($endline ? $endline : $line). |
232 | printlog(L_NOTICE, "editing $file"); | 289 | ": status fi |