Für Ihren Account wurde eine E-Mail verschickt, mit deren Hilfe Sie Ihre Zugangsdaten zurücksetzen können.
27 |
28 |
Falls Sie auch nach einigen Minuten keine E-Mail erhalten, prüfen Sie, dass Sie die richtige E-Mailadresse angegeben haben
29 | und in den letzten 60 Minuten nicht bereits versucht haben, Ihr Passwort zurückzusetzen.
171 |
--------------------------------------------------------------------------------
/scripts/import-transfers.php:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env php
2 | \n";
31 | die();
32 | }
33 |
34 | $parser = new CTXParser\Parser();
35 | $info = $parser->parse($argv[1]);
36 |
37 | $txes = [];
38 |
39 | foreach ($info->accountInfo as $accountInfo) {
40 | if ($accountInfo->transactionList !== null) {
41 | $txes = array_merge($txes, $accountInfo->transactionList[0]->transaction);
42 | }
43 | if ($accountInfo->notedTransactionList !== null) {
44 | $txes = array_merge($txes, $accountInfo->notedTransactionList[0]->notedTransaction);
45 | }
46 | }
47 |
48 | $transfers = array();
49 |
50 | foreach ($txes as $tx) {
51 | $transaction = array();
52 |
53 | if (is_array($tx->purpose)) {
54 | $purpose = implode(' ', $tx->purpose);
55 | } else {
56 | $purpose = $tx->purpose;
57 | }
58 |
59 | $dateInfo = $tx->valutaDate[0]->date[0];
60 | $date = $dateInfo->year . "/" . $dateInfo->month . "/" . $dateInfo->day;
61 |
62 | $user = get_email_from_transfer_code($purpose);
63 |
64 | if ($user === false)
65 | continue;
66 |
67 | if (!isset($transfers[$user]))
68 | $transfers[$user] = array();
69 |
70 | if (!isset($transfers[$user][$date])) {
71 | $transfers[$user][$date] = array(
72 | 'amount' => 0,
73 | 'transactions' => array()
74 | );
75 | }
76 |
77 | $value = $tx->value[0]->value;
78 |
79 | $transfers[$user][$date]['amount'] = bcadd($transfers[$user][$date]['amount'], $value);
80 | $transfers[$user][$date]['transactions'][] = $tx;
81 | }
82 |
83 | foreach ($transfers as $email => $dates) {
84 | foreach ($dates as $date => $info) {
85 | $uid = get_user_attr($email, 'id');
86 |
87 | $date_quoted = $bank_db->quote($date);
88 | $uid_quoted = $bank_db->quote($uid);
89 |
90 | $bank_db->query("BEGIN");
91 |
92 | $query = <<query($query)->fetch();
99 |
100 | if ($row === false) {
101 | $prev_amount = 0;
102 | $new_row = true;
103 | } else {
104 | $prev_amount = $row['amount'];
105 | $new_row = false;
106 | }
107 |
108 | $amount = 0;
109 | $transactions = array();
110 | foreach ($info['transactions'] as $transaction) {
111 | $value = $transaction->value[0]->value;
112 | $amount = bcadd($amount, $value);
113 |
114 | /* Ignore transactions we've already processed. */
115 | if (bccomp($amount, $prev_amount) <= 0)
116 | continue;
117 |
118 | $prev_amount = bcadd($prev_amount, $value);
119 |
120 | $transactions[] = array(
121 | 'amount' => $value,
122 | 'reference' => 'SEPA-Überweisung von ' . $transaction->remoteName . ' (IBAN: ' . $transaction->remoteAccountNumber . ')'
123 | );
124 | }
125 |
126 | if (bccomp($amount, $prev_amount) != 0) {
127 | echo 'Mismatching transfers for date ' . $date . ' and user ' . $email . ': expected ' . $prev_amount . ' - got: ' . $amount . "\n";
128 | die();
129 | }
130 |
131 | $amount_quoted = $bank_db->quote($amount);
132 |
133 | if ($new_row) {
134 | $query = <<query($query);
150 |
151 | foreach ($transactions as $transaction) {
152 | echo "Executing transfer for ${email}: Amount: ${transaction['amount']}, Reference: ${transaction['reference']}\n";
153 | $res = new_transaction(BANK_EXT_ACCOUNT, BANK_MGMT_ACCOUNT, 'Direct Debit', $transaction['amount'], 'Auszahlung auf externes Konto', false);
154 | if ($res[0] === false) {
155 | $bank_db->query("ROLLBACK");
156 | echo "Transaction failed for account ${email}: ${res[1]}\n";
157 | die();
158 | }
159 | $res = new_transaction(BANK_MGMT_ACCOUNT, $email, 'Transfer', $transaction['amount'], $transaction['reference'], false);
160 | if ($res[0] === false) {
161 | $bank_db->query("ROLLBACK");
162 | echo "Transaction failed for account ${email}: ${res[1]}\n";
163 | die();
164 | }
165 | }
166 |
167 | $bank_db->query("COMMIT");
168 | }
169 | }
170 |
171 | ?>
172 |
--------------------------------------------------------------------------------
/app/helpers/session.php:
--------------------------------------------------------------------------------
1 | quote($token);
56 | $email_quoted = $bank_db->quote($email);
57 |
58 | $query = <<query($query);
65 |
66 | return $token;
67 | }
68 |
69 | function get_user_last_reset($email) {
70 | global $bank_db;
71 |
72 | $email_quoted = $bank_db->quote($email);
73 |
74 | $query = <<query($query)->fetch();
81 |
82 | if ($row === false)
83 | return false;
84 | else
85 | return $row['reset_timestamp'];
86 | }
87 |
88 | function email_from_uid($uid) {
89 | global $bank_db;
90 |
91 | $uid_quoted = $bank_db->quote($uid);
92 |
93 | $query = <<query($query)->fetch();
100 |
101 | if ($row === false)
102 | return false;
103 | else
104 | return $row['email'];
105 | }
106 |
107 | function get_user_attr($email, $attr) {
108 | global $bank_db;
109 |
110 | $email_quoted = $bank_db->quote($email);
111 |
112 | $query = <<query($query)->fetch();
119 |
120 | if ($row === false)
121 | return false;
122 | else
123 | return $row[$attr];
124 | }
125 |
126 | function find_addresses($q) {
127 | global $bank_db;
128 |
129 | $q_quoted = $bank_db->quote($q);
130 |
131 | $query = <<query($query) as $row) {
139 | $addresses[] = [
140 | 'email' => $row['email'],
141 | 'name' => $row['name']
142 | ];
143 | }
144 |
145 | return $addresses;
146 | }
147 |
148 | function set_user_attr($email, $attr, $value) {
149 | global $bank_db;
150 |
151 | $email_quoted = $bank_db->quote($email);
152 | $value_quoted = $bank_db->quote($value);
153 |
154 | $query = <<query($query);
161 | }
162 |
163 | function set_user_session($email) {
164 | global $bank_db;
165 |
166 | $email_quoted = $bank_db->quote($email);
167 |
168 | $query = <<query($query)->fetch();
175 |
176 | if ($row === false)
177 | return;
178 |
179 | set_user_attr($email, 'verified', 1);
180 |
181 | $_SESSION['email'] = $email;
182 | $_SESSION['uid'] = $row['id'];
183 | $_SESSION['name'] = $row['name'];
184 | }
185 |
186 | function create_new_account($email, $name, $phone) {
187 | global $bank_db;
188 |
189 | $email_quoted = $bank_db->quote($email);
190 | $name_quoted = $bank_db->quote($name);
191 |
192 | $query = <<query($query);
199 |
200 | if (!get_user_attr($email, 'verified')) {
201 | set_user_attr($email, 'phone', $phone);
202 | }
203 | }
204 |
205 | function send_password_reminder($email) {
206 | $last_reset = get_user_last_reset($email);
207 |
208 | if ($last_reset !== false && ($last_reset === null || $last_reset < time() - 3600)) {
209 | $token = generate_reset_token($email);
210 | $url = "https://" . BANK_DOMAIN . "/app/reset-password?email=" . urlencode($email) . "&token=" . urlencode($token);
211 |
212 | if (get_user_attr($email, 'password') == '') {
213 | $message = <<quote($uid);
30 | $query = <<query($query)->fetch()['balance'];
37 | }
38 |
39 | function new_transaction($from_mail, $to_mail, $type, $amount, $reference, $agent_mail = null, $use_tx = true, $ignore_limits = false) {
40 | global $bank_db;
41 |
42 | if (bccomp($amount, 0) != 1)
43 | return [ false, 'Der Betrag muss positiv sein.' ];
44 |
45 | if ($type != 'Transfer' && $type != 'Direct Debit')
46 | return [ false, 'Ungültiger Transaktionstyp.' ];
47 |
48 | $from = get_user_attr($from_mail, 'id');
49 | $to = get_user_attr($to_mail, 'id');
50 |
51 | if ($agent_mail != null) {
52 | $agent = get_user_attr($agent_mail, 'id');
53 |
54 | if ($agent == false)
55 | return [ false, 'Ungültiger Agent für Transaktion.' ];
56 | }
57 |
58 | if ($from == false || $to == false || $from == $to)
59 | return [ false, 'Auftraggeber oder Empfänger ungültig.' ];
60 |
61 | $from_quoted = $bank_db->quote($from);
62 | $to_quoted = $bank_db->quote($to);
63 | $type_quoted = $bank_db->quote($type);
64 | $amount_quoted = $bank_db->quote($amount);
65 | $reference_quoted = $bank_db->quote($reference);
66 |
67 | if ($agent_mail !== null)
68 | $agent_quoted = $bank_db->quote($agent);
69 | else
70 | $agent_quoted = 'null';
71 |
72 | if ($use_tx)
73 | $bank_db->query("BEGIN");
74 |
75 | $query = <<query($query)->fetch();
82 |
83 | $balance_from = bcsub($row_from['balance'], $amount);
84 |
85 | if (!$ignore_limits) {
86 | $held_amount = get_held_amount(get_user_attr($from_mail, 'id'));
87 | $balance_from_held = bcsub($balance_from, $held_amount);
88 |
89 | $credit_limit_from = get_user_attr($from_mail, 'credit_limit');
90 |
91 | if (bccomp($balance_from_held, bcmul($credit_limit_from, -1)) == -1) {
92 | if ($use_tx)
93 | $bank_db->query("ROLLBACK");
94 | return [ false, 'Unzureichende Kontodeckung.' ];
95 | }
96 | }
97 |
98 | $query = <<query($query)->fetch();
105 |
106 | $balance_to = bcadd($row_to['balance'], $amount);
107 |
108 | $query = <<query($query);
115 |
116 | $txid = $bank_db->lastInsertId();
117 |
118 | $balance_from_quoted = $bank_db->quote($balance_from);
119 |
120 | $query = <<query($query);
126 |
127 | $balance_to_quoted = $bank_db->quote($balance_to);
128 |
129 | $query = <<query($query);
135 |
136 | if ($use_tx)
137 | $bank_db->query("COMMIT");
138 |
139 | $amount_formatted = format_number($amount, false);
140 |
141 | if (get_user_attr($from_mail, 'verified')) {
142 | $subject = "Abbuchung von Ihrem Konto an ${to_mail}: ${reference}";
143 |
144 | $message = <<quote($uid);
176 | $query = <<query($query)->fetchAll(PDO::FETCH_ASSOC);
186 | }
187 |
188 | function get_last_inbound_txid($uid) {
189 | global $bank_db;
190 |
191 | $uid_quoted = $bank_db->quote($uid);
192 | $query = <<query($query)->fetch();
201 |
202 | if ($row === false)
203 | return -1;
204 | else
205 | return $row['id'];
206 | }
207 |
208 | function get_last_outbound_txid($uid) {
209 | global $bank_db;
210 |
211 | $uid_quoted = $bank_db->quote($uid);
212 | $query = <<query($query)->fetch();
221 |
222 | if ($row === false)
223 | return -1;
224 | else
225 | return $row['id'];
226 | }
227 |
228 | function get_last_txid($uid) {
229 | global $bank_db;
230 |
231 | $uid_quoted = $bank_db->quote($uid);
232 | $query = <<query($query)->fetch();
241 |
242 | if ($row === false)
243 | return -1;
244 | else
245 | return $row['id'];
246 | }
247 |
248 | bcscale(2);
249 |
250 | function format_number($num, $html = true) {
251 | if ((float)$num >= 0)
252 | $color = "green";
253 | else
254 | $color = "red";
255 |
256 | $num = str_replace('.', ',', bcadd($num, 0));
257 |
258 | if ($html)
259 | return sprintf('%s', $color, htmlentities($num));
260 | else
261 | return $num;
262 | }
263 |
264 | function get_account_balances() {
265 | global $bank_db;
266 |
267 | $query = <<query($query)->fetchAll();
273 | }
274 |
275 | function get_transactions_between($begin, $end) {
276 | global $bank_db;
277 |
278 | $begin_quoted = $bank_db->quote($begin);
279 | $end_quoted = $bank_db->quote($end);
280 |
281 | $query = << ${begin_quoted} AND UNIX_TIMESTAMP(t.`timestamp`) < ${end_quoted}
288 | QUERY;
289 | return $bank_db->query($query)->fetchAll();
290 | }
291 |
292 | function _cmp_transaction($a, $b) {
293 | if ($a['timestamp'] > $b['timestamp'])
294 | return -1;
295 | else if ($a['timestamp'] < $b['timestamp'])
296 | return 1;
297 | else
298 | return 0;
299 | }
300 |
301 | function get_user_last_balance_above($email, $minBalance = 0) {
302 | $balance = get_user_attr($email, 'balance');
303 |
304 | if (bccomp($balance, $minBalance) != -1) {
305 | return time();
306 | }
307 |
308 | $uid = get_user_attr($email, 'id');
309 |
310 | $transactions = get_transactions($uid);
311 | usort($transactions, '_cmp_transaction');
312 |
313 | foreach ($transactions as $transaction) {
314 | $amount = $transaction['amount'];
315 |
316 | if ($transaction['to'] == $uid) {
317 | $amount = bcmul('-1', $amount);
318 | }
319 |
320 | $balance = bcadd($balance, $amount);
321 |
322 | if (bccomp($balance, $minBalance) != -1) {
323 | return $transaction['timestamp'];
324 | }
325 | }
326 |
327 | return 0;
328 | }
329 |
330 | function get_user_accounts($uid) {
331 | global $bank_db;
332 |
333 | $uid_quoted = $bank_db->quote($uid);
334 | $query = <<query($query)->fetchAll(PDO::FETCH_ASSOC) as $row) {
343 | $result[$row['email']] = [ 'name' => $row['name'] ];
344 | }
345 |
346 | return $result;
347 | }
348 |
--------------------------------------------------------------------------------
/COPYING:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
--------------------------------------------------------------------------------
/scripts/check_smsfinder.pl:
--------------------------------------------------------------------------------
1 | #!/usr/bin/perl -w
2 | # nagios: -epn
3 | #
4 | # COPYRIGHT:
5 | #
6 | # This software is Copyright (c) 2009 NETWAYS GmbH, Birger Schmidt
7 | #
8 | # (Except where explicitly superseded by other copyright notices)
9 | #
10 | # LICENSE:
11 | #
12 | # This work is made available to you under the terms of Version 2 of
13 | # the GNU General Public License. A copy of that license should have
14 | # been provided with this software, but in any event can be snarfed
15 | # from http://www.fsf.org.
16 | #
17 | # This work is distributed in the hope that it will be useful, but
18 | # WITHOUT ANY WARRANTY; without even the implied warranty of
19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 | # General Public License for more details.
21 | #
22 | # You should have received a copy of the GNU General Public License
23 | # along with this program; if not, write to the Free Software
24 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 | # 02110-1301 or visit their web page on the internet at
26 | # http://www.fsf.org.
27 | #
28 | #
29 | # CONTRIBUTION SUBMISSION POLICY:
30 | #
31 | # (The following paragraph is not intended to limit the rights granted
32 | # to you to modify and distribute this software under the terms of
33 | # the GNU General Public License and is only of importance to you if
34 | # you choose to contribute your changes and enhancements to the
35 | # community by submitting them to NETWAYS GmbH.)
36 | #
37 | # By intentionally submitting any modifications, corrections or
38 | # derivatives to this work, or any other work intended for use with
39 | # this Software, to NETWAYS GmbH, you confirm that
40 | # you are the copyright holder for those contributions and you grant
41 | # NETWAYS GmbH a nonexclusive, worldwide, irrevocable,
42 | # royalty-free, perpetual, license to use, copy, create derivative
43 | # works based on those contributions, and sublicense and distribute
44 | # those contributions and any derivatives thereof.
45 | #
46 | # Nagios and the Nagios logo are registered trademarks of Ethan Galstad.
47 |
48 |
49 |
50 | ######################################################################
51 | ######################################################################
52 | #
53 | # configure here to match your system setup
54 | #
55 | my $object_cache = "/usr/local/icinga/var/objects.cache";
56 | my $nagios_cmd = "/usr/local/icinga/var/rw/icinga.cmd";
57 | my $logfile = "/usr/local/icinga/var/smsfinder.log";
58 |
59 | my $ok = "^OK ";
60 | my $ack = "^ACK ";
61 |
62 | #my $ok = "Antwort ist JA";
63 | #my $ack = "Antwort ist NEIN";
64 |
65 | #
66 | # don't change anything below here
67 | #
68 | ######################################################################
69 | ######################################################################
70 |
71 |
72 | use strict;
73 | use warnings;
74 | use Getopt::Long qw(:config no_ignore_case bundling);
75 | use File::Basename;
76 | use IO::Socket;
77 | use XML::Simple;
78 |
79 | our @state = ('OK', 'WARNING', 'CRITICAL', 'UNKNOWN');
80 |
81 | my $HowIwasCalled = "$0 @ARGV";
82 |
83 | # version string
84 | my $version = '0.3';
85 |
86 | my $basename = basename ($0);
87 |
88 | # init command-line parameters
89 | my $hostaddress = undef;
90 | my $timeout = 60;
91 | my $warning = 40;
92 | my $critical = 20;
93 | my $show_version = undef;
94 | my $verbose = undef;
95 | my $help = undef;
96 | my $user = undef;
97 | my $pass = undef;
98 | my $number = undef;
99 | my $splitmax = 1;
100 | my $noma = 0;
101 | my $message = 'no text message given';
102 | my $contactgroup = undef;
103 |
104 | my @msg = ();
105 | my @perfdata = ();
106 | my $exitVal = undef;
107 | my $loginID = '0';
108 | my $do_not_verify = 0;
109 |
110 |
111 | my %smsErrorCodes = (
112 | #Error Code, Error Description
113 | 601,'Authentication Failed',
114 | 602,'Parse Error',
115 | 603,'Invalid Category',
116 | 604,'SMS message size is greater than 160 chars',
117 | 605,'Recipient Overflow',
118 | 606,'Invalid Recipient',
119 | 607,'No Recipient',
120 | 608,'SMSFinder is busy, can’t accept this request',
121 | 609,'Timeout waiting for a TCP API request',
122 | 610,'Unknown Action Trigger',
123 | 611,'Error in broadcast Trigger',
124 | 612,'System Error. Memory Allocation Failure',
125 | );
126 |
127 | sub mypod2usage{
128 | # Load Pod::Usage only if needed.
129 | require "Pod/Usage.pm";
130 | import Pod::Usage;
131 |
132 | pod2usage(@_);
133 | }
134 |
135 | # get command-line parameters
136 | GetOptions(
137 | "H|hostaddress=s" => \$hostaddress,
138 | "t|timeout=i" => \$timeout,
139 | "v|verbose" => \$verbose,
140 | "V|version" => \$show_version,
141 | "h|help" => \$help,
142 | "u|user=s" => \$user,
143 | "p|password=s" => \$pass,
144 | "n|number=s" => \$number,
145 | "s|splitmax=i" => \$splitmax,
146 | "noma" => \$noma,
147 | "o|objectcache=s" => \$object_cache,
148 | "m|message=s" => \$message,
149 | "w|warning=i" => \$warning,
150 | "c|critical=i" => \$critical,
151 | "g|contactgroup=s" => \$contactgroup,
152 | ) or mypod2usage({
153 | -msg => "\n" . 'Invalid argument!' . "\n",
154 | -verbose => 1,
155 | -exitval => 3
156 | });
157 |
158 | sub printResultAndExit {
159 |
160 | # print check result and exit
161 |
162 | my $exitVal = shift;
163 |
164 | print "@_" if (defined @_);
165 |
166 | print "\n";
167 |
168 | # stop timeout
169 | alarm(0);
170 |
171 | exit($exitVal);
172 | }
173 |
174 | if ($show_version) { printResultAndExit (0, $basename . ' - version: ' . $version); }
175 |
176 | mypod2usage({
177 | -verbose => 1,
178 | -exitval => 3
179 | }) if ($help);
180 |
181 | mypod2usage({
182 | -msg => "\n" . 'Warning level is lower than critical level. Please check.' . "\n",
183 | -verbose => 1,
184 | -exitval => 3
185 | }) if ($warning < $critical);
186 |
187 |
188 |
189 | # set timeout
190 | local $SIG{ALRM} = sub {
191 | if (defined $exitVal) {
192 | print 'TIMEOUT: ' . join(' - ', @msg) . "\n";
193 | exit($exitVal);
194 | } else {
195 | print 'CRITICAL: Timeout - ' . join(' - ', @msg) . "\n";
196 | exit(2);
197 | }
198 | };
199 | alarm($timeout);
200 |
201 |
202 | sub urlencode {
203 | my $str = "@_";
204 | $str =~ s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
205 | return $str;
206 | }
207 |
208 | sub urldecode {
209 | my $str = "@_";
210 | $str =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
211 | return $str;
212 | }
213 |
214 | sub prettydate {
215 | # usage: $string = prettydate( [$time_t] );
216 | # omit parameter for current time/date
217 | @_ = localtime(shift || time);
218 | return(sprintf("%04d/%02d/%02d %02d:%02d:%02d", $_[5]+1900, $_[4]+1, $_[3], @_[2,1,0]));
219 | }
220 |
221 | sub justASCII {
222 | join("",
223 | map { # german umlauts
224 | chr($_) eq 'ö' ? 'oe' :
225 | chr($_) eq 'ä' ? 'ae' :
226 | chr($_) eq 'ü' ? 'ue' :
227 | chr($_) eq 'Ö' ? 'Oe' :
228 | chr($_) eq 'Ä' ? 'Ae' :
229 | chr($_) eq 'Ü' ? 'Ue' :
230 | chr($_) eq 'ß' ? 'ss' :
231 | $_ > 128 ? '' : # cut out anything not 7-bit ASCII
232 | chr($_) =~ /[[:cntrl:]]/ ? '' : # and control characters too
233 | chr($_) # just the ASCII as themselves
234 | } unpack("U*", $_[0])); # unpack Unicode characters
235 | }
236 |
237 | sub httpGet {
238 | my $document = shift;
239 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
240 | if ($remote) {
241 | $remote->autoflush(1);
242 | print $remote "GET $document HTTP/1.1\015\012\015\012";
243 | #my $http_answer = join(' ', (<$remote>));
244 | my $http_answer;
245 | while (<$remote>) {
246 | $http_answer .= $_;
247 | }
248 | close $remote;
249 | $http_answer =~ tr/\n\r/ /;
250 | if ($verbose) { print 'SMSFinder response : ' . $http_answer . "\n"; }
251 | return $http_answer;
252 | } else {
253 | return undef;
254 | }
255 | }
256 |
257 | sub httpPostLogin {
258 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
259 | if ($remote) {
260 | $remote->autoflush(1);
261 | my $poststring = "fileName=index.html&userName=$user&password=$pass";
262 | print $remote
263 | "POST /cgi-bin/postquery.cgi HTTP/1.1\015\012" .
264 | "Content-Length: " . length($poststring) . "\015\012" .
265 | "Content-Type: application/x-www-form-urlencoded\015\012" .
266 | "\015\012" .
267 | $poststring;
268 | close $remote;
269 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
270 | if ($remote) {
271 | print $remote "GET /index.html?0 HTTP/1.1\015\012\015\012";
272 | while ( <$remote> ) {
273 | if (/url="(?:home|smsSend).html\?(\d+)"/) {
274 | $loginID = $1;
275 | if ($verbose) { print 'SMSFinder login ID : ' . $loginID . "\n"; }
276 | push (@perfdata, "loginID=$loginID");
277 | last;
278 | }
279 | }
280 | close $remote;
281 | }
282 | return ($loginID != 0);
283 | } else {
284 | return undef;
285 | }
286 | }
287 |
288 | sub httpGetLogout {
289 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
290 | if ($remote) {
291 | print $remote "GET /logout.html?$loginID HTTP/1.1\015\012\015\012";
292 | while ( <$remote> ) {
293 | if ($verbose) { print 'SMSFinder logout : ' . $_ . "\n"; }
294 | }
295 | }
296 | close $remote;
297 | }
298 |
299 | sub httpOutput {
300 | my ($output) = @_;
301 |
302 | print "Content-Type: text/plain\r\n\r\n$output\r\n";
303 | }
304 |
305 | sub httpDie {
306 | my ($output) = @_;
307 |
308 | die "$output" if (*LOG eq *STDERR);
309 | httpOutput($output);
310 | print LOG prettydate() . ' ' . $output . "\n";
311 | die;
312 | }
313 |
314 | sub httpPostReboot {
315 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
316 | if ($remote) {
317 | $remote->autoflush(1);
318 | # Save to flash before rebooting
319 | my $poststring = "filename=save_restartLoading.html%3F$loginID&commandVal=set+save_conf&userid=$loginID";
320 | print $remote
321 | "POST /cgi-bin/postquery.cgi HTTP/1.1\015\012" .
322 | "Content-Length: " . length($poststring) . "\015\012" .
323 | "Content-Type: application/x-www-form-urlencoded\015\012" .
324 | "\015\012" .
325 | $poststring;
326 | while ( <$remote> ) {
327 | if ($verbose) { print 'SMSFinder save to flash : ' . $_; }
328 | }
329 | close $remote;
330 | sleep(30);
331 | # Reboot
332 | $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "http(80)");
333 | $poststring = "filename=save_restartLoading.html%3F$loginID&commandVal=set+reboot_box&userid=$loginID";
334 | print $remote
335 | "POST /cgi-bin/postquery.cgi HTTP/1.1\015\012" .
336 | "Content-Length: " . length($poststring) . "\015\012" .
337 | "Content-Type: application/x-www-form-urlencoded\015\012" .
338 | "\015\012" .
339 | $poststring;
340 | while ( <$remote> ) {
341 | if ($verbose) { print 'SMSFinder reboot : ' . $_; }
342 | }
343 | close $remote;
344 | # Check to see if reboot worked
345 | sleep(30);
346 | unless (httpPostLogin) {
347 | print "ERROR: SMS login failed!\n";
348 | return undef;
349 | }
350 | my $response = httpGet('/statsSysinfo.html' . "?$loginID");
351 | if (defined $response) {
352 | # if uptime is < 1 minute, assume reboot worked
353 | if ($response =~ /200 OK.*?System Uptime.*>(0 Days, 0 Hours, 0 Minutes, \d+ Seconds)/) {
354 | httpGetLogout;
355 | return 1;
356 | } else {
357 | $response =~ /200 OK.*?System Uptime.*>(\d+ Days, \d+ Hours, \d+ Minutes, \d+ Seconds)/;
358 | print "Uptime: $1\n";
359 | return undef;
360 | }
361 | } else {
362 | print "ERROR: cannot contact modem after reboot!\n";
363 | return undef;
364 | }
365 | return ($loginID != 0);
366 | } else {
367 | return undef;
368 | }
369 | }
370 |
371 | sub telnetRW {
372 | my $command = shift;
373 | my $remote = IO::Socket::INET->new(Proto => "tcp", PeerAddr => $hostaddress, PeerPort => "5000");
374 | if ($remote) {
375 | $remote->autoflush(1);
376 | print "$command\n";
377 | print $remote "$command\015";
378 | my $answer;
379 | my $char;
380 | while ($remote->read($char,1)) {
381 | $answer .= $char;
382 | #print ".$char";
383 | if ($answer =~ /(OK|ERROR)(.*)\015\012/) {
384 | last;
385 | }
386 | }
387 | close $remote;
388 | $answer =~ tr/\n\r/ /;
389 | if ($verbose) { print 'SMSFinder response : ' . $answer . "\n"; }
390 | return $answer;
391 | } else {
392 | return undef;
393 | }
394 | }
395 |
396 |
397 | #
398 | # chose one of the possible functions of this script
399 | #
400 |
401 | if ($basename eq 'email2sms.pl') {
402 | # convert e-mail to SMS
403 | open (LOG, ">>".$logfile) or *LOG = *STDERR;
404 | print LOG prettydate();
405 | print LOG " SMSemail: $HowIwasCalled\n";
406 | close LOG;
407 |
408 | # use Mail::Internet and Mail::Header to parse e-mail message
409 | use Mail::Internet;
410 | use Mail::Header;
411 |
412 | my $email = Mail::Internet->new(\*STDIN);
413 | my $head = $email->head();
414 | my $body = $email->body();
415 | $message = "";
416 |
417 | # Only use From: and Subject: hedaers
418 | my $subject = $head->get('subject');
419 | my $from = $head->get('from');
420 | $message .= "$subject From $from: ";
421 | $message .= join(" ",@$body);
422 | $message =~ s/\n//gm; # Remove newlines
423 | $message =~ s/\s\s+/ /g;# and collapse whitespace
424 | }
425 |
426 | if ($basename eq 'sendsms.pl' or $basename eq 'email2sms.pl') {
427 | # sendsms
428 | open (LOG, ">>".$logfile) or *LOG = *STDERR;
429 | print LOG prettydate();
430 | print LOG " SMSsend: $HowIwasCalled\n";
431 | close LOG;
432 |
433 | unless ($hostaddress) {
434 | mypod2usage({
435 | -msg => "\n" . 'ERROR: hostaddress missing!' . "\n",
436 | -verbose => 1,
437 | -exitval => 3 });
438 | }
439 | unless ($number) {
440 | mypod2usage({
441 | -msg => "\n" . 'ERROR: number missing!' . "\n",
442 | -verbose => 1,
443 | -exitval => 3 });
444 | }
445 | unless ($user) {
446 | mypod2usage({
447 | -msg => "\n" . 'ERROR: username missing!' . "\n",
448 | -verbose => 1,
449 | -exitval => 3 });
450 | }
451 | unless ($pass) {
452 | mypod2usage({
453 | -msg => "\n" . 'ERROR: password missing!' . "\n",
454 | -verbose => 1,
455 | -exitval => 3 });
456 | }
457 |
458 | #my $msg = urlencode("@_");
459 | #$message =~ tr/\0-\xff//UC; # unicode to latin-1
460 |
461 | if ($verbose) { print 'message to send : ' . $message . "\n"; }
462 | # Split message into 160 character pieces up to $splitmax msgs
463 | my $offset = 0;
464 | $splitmax = 999 if ($splitmax == 0);
465 | while ($splitmax-- > 0 and length($message) > $offset) {
466 | my $messagepart = urlencode(substr(justASCII($message),$offset,160));
467 | $offset += 160;
468 | if ($verbose) { print 'short clean message : ' . $messagepart . "\n"; }
469 | my $document;
470 | if ($number =~ /^[0-9+-]+$/) {
471 | $document = "/sendmsg?user=$user&passwd=$pass&cat=1&to=$number&text=$messagepart";
472 | } else { # It isn't a number, assume it's in the address book
473 | $document = "/sendmsg?user=$user&passwd=$pass&cat=1&ton=$number&text=$messagepart";
474 | }
475 | my $url = "http://$hostaddress" . $document;
476 | if ($verbose) { print 'SMSFinder URL : ' . $url . "\n"; }
477 |
478 | #$ua->timeout($timeout);
479 | #my $response = get $url;
480 | my $response = httpGet($document);
481 |
482 | push (@msg, '"' . $messagepart . '" to ' . $number . ' via ' . $hostaddress);
483 | if (defined $response) {
484 | if ($response =~ /ID: (\d+)/) {
485 | my $apimsgid = $1;
486 | if ($noma) {
487 | my $statuscode = -1;
488 | $document = "/querymsg?user=$user&passwd=$pass&apimsgid=$apimsgid";
489 | $url = "http://$hostaddress" . $document;
490 | if ($verbose) { print 'SMSFinder URL : ' . $url . "\n"; }
491 | while (1) { # will be ended on timeout or success
492 | #$response = get $url;
493 | $response = httpGet($document);
494 | if (defined $response) {
495 | if ($response =~ /(Status|Err): (.+)/) {
496 | $statuscode = $2;
497 | if ($statuscode == 0) {
498 | # 0='Done'
499 | push (@msg, 'send successfully. MessageID: ' . $apimsgid);
500 | $exitVal = 0; # set global ok
501 | last;
502 | } elsif ($statuscode == 2 or $statuscode == 3) {
503 | # 2='In progress' 3='Request Received'
504 | sleep 1;
505 | next;
506 | } elsif ($statuscode == 5) {
507 | # 5='Message ID Not Found'
508 | push (@msg, 'failed. With an very strange error: Message ID Not Found');
509 | $exitVal = 2; # set global critical
510 | last;
511 | } elsif ($statuscode == 1 or $statuscode == 4) {
512 | # 1='Done with error - message is not sent to all the recipients'
513 | # 4='Error occurred while sending the SMS from the SMSFinder'
514 | push (@msg, 'failed. Error: ' . $statuscode);
515 | $exitVal = 2; # set global critical
516 | last;
517 | } elsif ($1 eq 'Err') {
518 | push (@msg, join ('', ' failed. Error: ',
519 | (defined $smsErrorCodes{$statuscode}) ? $smsErrorCodes{$statuscode} : 'unknown' ));
520 | $exitVal = 2; # set global critical
521 | last;
522 | } else {
523 | push (@msg, 'failed. With an unknown response: ' . $response);
524 | $exitVal = 2; # set global critical
525 | last;
526 | }
527 | }
528 | } else {
529 | push (@msg, 'unknown. Timeout or SMSFinder unreachable while querying result.');
530 | $exitVal = 2; # set global critical
531 | last;
532 | }
533 | }
534 | } else {
535 | # because Nagios notofication is blocking, we dont wait for message to be send.
536 | # not even until timeout
537 | push (@msg, 'queued successfully. MessageID: ' . $apimsgid);
538 | $exitVal = 0; # set global ok
539 | }
540 | } elsif ($response =~ /Err: (\d+)/) {
541 | push (@msg, join(' ', ' failed. Error:', (defined $smsErrorCodes{$1}) ? $smsErrorCodes{$1} : 'unknown' ));
542 | $exitVal = 2; # set global critical
543 | } else {
544 | push (@msg, 'failed. With an unknown response: ' . $response);
545 | $exitVal = 2; # set global critical
546 | }
547 | } else {
548 | push (@msg, 'failed. Timeout or SMSFinder unreachable while try to send message.');
549 | $exitVal = 2; # set global critical
550 | }
551 | open (LOG, ">>".$logfile) or *LOG = *STDERR;
552 | print LOG prettydate();
553 | if (defined $contactgroup) { push (@msg, 'contactgroup: "' . "$contactgroup" . '"'); }
554 | print LOG ' SMSsend: ' . join(' ', @msg) . "\n";
555 | close LOG;
556 | }
557 | printResultAndExit ($exitVal, join(' ', @msg));
558 | }
559 |
560 | elsif ($basename eq 'smsreboot.pl') {
561 | unless ($hostaddress) {
562 | mypod2usage({
563 | -msg => "\n" . 'ERROR: hostaddress missing!' . "\n",
564 | -verbose => 1,
565 | -exitval => 3 });
566 | }
567 | unless ($user) {
568 | mypod2usage({
569 | -msg => "\n" . 'ERROR: username missing!' . "\n",
570 | -verbose => 1,
571 | -exitval => 3 });
572 | }
573 | unless ($pass) {
574 | mypod2usage({
575 | -msg => "\n" . 'ERROR: password missing!' . "\n",
576 | -verbose => 1,
577 | -exitval => 3 });
578 | }
579 | unless (httpPostLogin) {
580 | printResultAndExit (2, 'CRITICAL: SMS login failed!|');
581 | }
582 | unless (httpPostReboot) {
583 | httpGetLogout;
584 | printResultAndExit (1, 'WARNING: reboot failed!|');
585 | }
586 | }
587 |
588 | elsif ($basename eq 'check_smsfinder.pl') {
589 | #check_smsfinder;
590 | unless ($hostaddress) {
591 | mypod2usage({
592 | -msg => "\n" . 'ERROR: hostaddress missing!' . "\n",
593 | -verbose => 1,
594 | -exitval => 3 });
595 | }
596 |
597 | unless (httpPostLogin) {
598 | printResultAndExit (2, 'CRITICAL: SMS login failed!|');
599 | }
600 | my $response = httpGet('/statsSysinfo.html' . "?$loginID");
601 | my $response2 = httpGet('/statsSmsfinder.html' . "?$loginID" . "&1");
602 | httpGetLogout;
603 |
604 | my $firmware;
605 |
606 | if (defined $response) {
607 | if ($response =~ /200 OK.*?Product Model Number.*?>(\S+?)<.*Firmware Version.*?>(\S+?)<.*?>/) {
608 | push (@msg, "model: $1", "firmware: $2");
609 | $firmware = $2;
610 | } else {
611 | printResultAndExit (2, "CRITICAL: $hostaddress SMSFinder returned bad response. \n" . $response);
612 | }
613 | }
614 |
615 | if ($firmware >= 1.44) {
616 | if (defined $response2) {
617 | if ($response2 =~ /200 OK.*?Signal Strength.*?>\S+?([0-9]+).*?/) {
618 | my $strength = $1;
619 | if ($strength > 0) {
620 | $strength = sprintf("%.1f",($strength * 100) / 31);
621 | push (@perfdata, "strength=$strength\%;$warning;$critical;;");
622 | push (@msg, "GSM signal strength is $strength\%");
623 | if ($strength < $critical){
624 | printResultAndExit (2, 'CRITICAL: ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
625 | } elsif ($strength < $warning){
626 | printResultAndExit (1, 'WARNING: ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
627 | } else {
628 | printResultAndExit (0, 'OK: ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
629 | }
630 | }
631 | else
632 | {
633 | push (@msg, "No GSM signal, maybe not connected to the Network.");
634 | $exitVal = 2;
635 | printResultAndExit ($exitVal, 'CRITICAL: ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
636 | }
637 | } else {
638 | printResultAndExit (2, "CRITICAL: $hostaddress SMSFinder returned bad response. \n" . $response2);
639 | }
640 | } else {
641 | printResultAndExit (2, 'CRITICAL: no response from ' . $hostaddress . ' within ' . $timeout . ' seconds.');
642 | }
643 | } else {
644 | if (defined $response) {
645 | if ($response =~ /200 OK.*?Product Model Number.*?>(\S+?)<.*Firmware Version.*?>(\S+?)<.*MAC Address.*?Signal Strength\s*<.*?>(\d+)\s*<.*?Live Details/) {
646 | my $strength = $3;
647 | if ($strength > 0) {
648 | $strength = sprintf("%.1f",($strength * 100) / 31);
649 | push (@perfdata, "strength=$strength\%;$warning;$critical;;");
650 | push (@msg, "GSM signal strength is $strength\%");
651 | if ($strength < $critical){
652 | $exitVal = 2;
653 | } elsif ($strength < $warning){
654 | $exitVal = 1;
655 | } else {
656 | $exitVal = 0;
657 | }
658 | push (@msg, "model: $1", "firmware: $2");
659 | printResultAndExit ($exitVal, $state[$exitVal].': ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
660 | }else {
661 | push (@msg, "No GSM signal, maybe not connected to the Network.");
662 | push (@msg, "model: $1", "firmware: $2");
663 | printResultAndExit (2, 'CRITICAL: ' . join(' - ', @msg) . '|' . join (' ', @perfdata));
664 | }
665 | } else {
666 | printResultAndExit (2, "CRITICAL: $hostaddress SMSFinder returned bad response. \n" . $response);
667 | }
668 | }
669 | }
670 | }
671 |
672 | elsif ($basename eq 'smsack.cgi') {
673 | my $postdata;
674 | read(STDIN, $postdata, $ENV{'CONTENT_LENGTH'})
675 | if (defined $ENV{'CONTENT_LENGTH'}) or read(STDIN, $postdata, 1000);
676 |
677 | # extract the XML data
678 | $postdata =~ s/^.*\&//;
679 | $postdata =~ s/^XMLDATA=//;
680 |
681 |
682 | $postdata = urldecode($postdata);
683 | $postdata =~ s/\012/ /g;
684 | $postdata =~ s/\015/ /g;
685 |
686 | if (defined $ENV{'HTTP_USER_AGENT'}) {
687 | open (LOG, ">>".$logfile) or *LOG = *STDERR;
688 | } else {
689 | *LOG = *STDERR;
690 | }
691 |
692 | print LOG prettydate() . ' SMSreceived: ' . $postdata . "\n";
693 |
694 |
695 | httpDie ('Invalid XML Format') unless ($postdata =~ m/^\s*<\?xml .*>/i);
696 |
697 | my $xml = XMLin($postdata);
698 |
699 | my $SenderNumber = $xml->{'SenderNumber'};
700 | my $Message = $xml->{'Message'};
701 | my $received = $xml->{'Date'}.' '.$xml->{'Time'};
702 |
703 |
704 | my $status;
705 |
706 | my $host = '';
707 | my $service = '';
708 | my $alerttype = 'HostAlert';
709 |
710 | # check new status
711 | if ($Message =~ m/$ok/) {
712 | $status="OK";
713 | } elsif ($Message =~ m/$ack/) {
714 | $status="ACK";
715 | }
716 |
717 | # get service/host
718 | # if ($Message =~ m{ (HostAlert|ServiceAlert) (\S+)\[[\]*]\]/(.+) is })
719 | if ($Message =~ m{^\s*\S+\s+\S+\s+([^,>]+),*([^>]*)>})
720 | {
721 | $alerttype = 'ServiceAlert' if $2 ne '';
722 | $host = $1;
723 | $service = $2;
724 | }
725 | #'$NOTIFICATIONTYPE$ $HOSTNAME$> is $HOSTSTATE$ /$SHORTDATETIME$/ $OUTPUT$'
726 | #'$NOTIFICATIONTYPE$ $HOSTNAME$,$SERVICEDESC$> is $SERVICESTATE$ /$SHORTDATETIME$/ $SERVICEOUTPUT$'
727 |
728 |
729 | # contact is verified via the nagios object.cache
730 | my $verified_contact=0;
731 | open (NCFG,"<".$object_cache);
732 | while () {
733 | if (/pager\s+(\+?\d+)/) {
734 | if($SenderNumber eq $1) {
735 | # pager is ok
736 | $verified_contact=1;
737 | last;
738 | }
739 | }
740 | }
741 | close(NCFG);
742 |
743 | if($verified_contact or $do_not_verify) {
744 | my $comment = " by $SenderNumber at $received $Message";
745 |
746 | if ($status eq "OK") {
747 | # set service OK
748 | $comment = "Reset" . $comment;
749 | open (CMD, ">>" . $nagios_cmd);
750 | if ($alerttype eq 'ServiceAlert') {
751 | print CMD "[".time()."] PROCESS_SERVICE_CHECK_RESULT;".$host.";".$service.";0;".$comment."\n";
752 | print CMD "[".time()."] ENABLE_SVC_NOTIFICATIONS;".$host.";".$service."\n";
753 | } else {
754 | print CMD "[".time()."] PROCESS_HOST_CHECK_RESULT;".$host.";0;".$comment."\n";
755 | print CMD "[".time()."] ENABLE_HOST_NOTIFICATIONS;".$host."\n";
756 | }
757 | close (CMD);
758 | } elsif ($status eq "ACK") {
759 | # aknowledge service
760 | $comment="Aknowledged".$comment;
761 | open (CMD, ">>" . $nagios_cmd);
762 | if ($alerttype eq 'ServiceAlert') {
763 | print CMD "[".time()."] ACKNOWLEDGE_SVC_PROBLEM;".$host.";".$service.";1;1;1;".$SenderNumber.";".$comment."\n";
764 | } else {
765 | print CMD "[".time()."] ACKNOWLEDGE_HOST_PROBLEM;".$host.";1;1;1;".$SenderNumber.";".$comment."\n";
766 | }
767 | close (CMD);
768 | }
769 | print LOG prettydate() . ' SMS sender verified\n';
770 | httpOutput('ACCEPTED');
771 | } else {
772 | print LOG prettydate() . ' SMS sender not verified\n';
773 | httpOutput('NOT ACCEPTED');
774 | }
775 | print LOG " From=$SenderNumber Received=$received Status=$status Host=$host Service=$service MSG=\"$Message\"\n";
776 | close LOG;
777 | }
778 |
779 | else {
780 | mypod2usage({
781 | -verbose => 1,
782 | -exitval => 3
783 | });
784 | }
785 |
786 |
787 | # DOCUMENTATION
788 |
789 | =head1 NAME
790 |
791 | =over 1
792 |
793 | =item B
794 |
795 | the Nagios
796 | - check plugin,
797 | - notification handler / sendSMS and
798 | - ACKnowledgement addon / CGI handler
799 | for the MultitechSMSFinder
800 |
801 | =back
802 |
803 | =head1 DESCRIPTION
804 |
805 | =over 1
806 |
807 | =item Depending on how it is called,
808 |
809 | - Checks a Multitech SMSFinder and returns if it is connected
810 | to the GSM Network and the level of signal strength.
811 | - send an SMS via a Multitech SMSFinder
812 | - handles a received SMS and sets the ACKnowledgement in Nagios
813 |
814 | *THIS script should be symlinked/copied according to your needs*
815 | If you symlink it, make the CGI handler the original. Your http
816 | server may not accept symlinked CGIs.
817 | So once more - this script is all three in one.
818 |
819 |
820 | =back
821 |
822 | =head1 SYNOPSIS
823 |
824 | =over 1
825 |
826 | =item B
827 |
828 | -H hostaddress
829 | [-t|--timeout=]
830 | [-v|--verbose]
831 | [-h|--help] [-V|--version]
832 | [-u|--user=]
833 | [-p|--password=]
834 |
835 | =item B
836 |
837 | -H hostaddress
838 | [-t|--timeout=]
839 | [-v|--verbose]
840 | [-h|--help] [-V|--version]
841 | [-u|--user=]
842 | [-p|--password=]
843 | [-s|--splitmax=]
844 | -n|--number=
845 | -m|--message=
846 |
847 | =item B
848 |
849 | [-v|--verbose]
850 | [-h|--help] [-V|--version]
851 | CONTENT_LENGTH via ENVIRONMENT
852 | SMS data via STDIN (from http post)
853 |
854 | =item B
855 |
856 | -H hostaddress
857 | [-t|--timeout=]
858 | [-v|--verbose]
859 | [-h|--help] [-V|--version]
860 | [-u|--user=]
861 | [-p|--password=]
862 | [-s|--splitmax=]
863 | -n|--number=
864 | -m|--message=
865 |
866 | =back
867 |
868 | =head1 OPTIONS
869 |
870 | =over 4
871 |
872 | =item -H
873 |
874 | Hostaddress of the SMSFinder
875 |
876 | =item -t|--timeout=
877 |
878 | Time in seconds to wait before script stops.
879 |
880 | =item -v|--verbose
881 |
882 | Enable verbose mode and show whats going on.
883 |
884 | =item -V|--version
885 |
886 | Print version an exit.
887 |
888 | =item -h|--help
889 |
890 | Print help message and exit.
891 |
892 | =item -n|--number
893 |
894 | Telephone number of the SMS recipient
895 |
896 | =item -m|--message
897 |
898 | SMS message text
899 |
900 | =item -w|--warning
901 |
902 | Warning level for signal strength in procent. (Default = 40)
903 |
904 | =item -c|--critical
905 |
906 | Critical level for signal strength in procent. (Default = 20)
907 |
908 | =item --noma
909 |
910 | NoMa switch - try to check if the send SMS is send, not just queued.
911 |
912 | =back
913 |
914 |
915 | =head1 HOWTO integrate with Nagios
916 |
917 | =over 1
918 |
919 | =item *
920 |
921 | Prepare your system as described below, be well informed and (sort of)
922 | remote control your Nagios via your mobile and a Multitech SMSFinder.
923 |
924 | =back
925 |
926 | =head2 How to reset/overrule a host/service state?
927 |
928 | Just prepend the notification SMS with "OK " and send it back to your SMSFinder.
929 |
930 | The host/service state will be set to OK and notifications enabled again.
931 |
932 |
933 | =head2 How to acknowledge a notified outage?
934 |
935 | Just prepend the notification SMS with "ACK " and send it back to your SMSFinder.
936 |
937 | The host/service state will be acknowledged and notifications disabled
938 | until the host/service is fine again.
939 |
940 |
941 | =head2 How to prepare your system for acknowledgements?
942 |
943 | 1. Configure the SMSFinder to use the HTTP API to send and receive SMS.
944 |
945 | Configure the following in the web interface of your SMSFinder:
946 |
947 | 1.a. Access for your Nagios server(s) on the
948 | "Administration > Admin Access > Allowed Networks" page.
949 |
950 | 1.b. define a SMS user on the
951 | "SMS Services > Send SMS Users" page.
952 |
953 | 1.c. switch on the HTTP send API on the
954 | "SMS Services > SMS API > Send API" page.
955 |
956 | 1.d. configure the HTTP receive API on the
957 | "SMS Services > SMS API > Receive API" page.
958 | (do not enable authentication, and be sure to set the POST Interval to 0)
959 |
960 | 2. Configure the notifications (via sendsms.pl) in Nagios as shown in the examples.
961 |
962 | 3. Configure the web server to handle the acknowledgements (via smsack.cgi).
963 |
964 | 4. Alter the paths in the congfig section on top of this script to match your system setup.
965 |
966 | 5. Ensure that the logfile is writable by the Nagios and the web server user:
967 | chown nagios:www-data /usr/local/nagios/var/smsfinder.log && chmod 664 /usr/local/nagios/var/smsfinder.log
968 |
969 | 6. Add the passwords to the /usr/local/nagios/etc/resource.cfg
970 | $USER13$=smsuser
971 | $USER14$=smspass
972 | $USER15$=adminuser
973 | $USER16$=adminpass
974 |
975 |
976 | =head1 EXAMPLE for Nagios check configuration
977 |
978 | # command definition to check SMSFinder via HTTP
979 | define command {
980 | command_name check_smsfinder
981 | command_line $USER1$/check_smsfinder.pl -H $HOSTADDRESS$ -u $USER15$ -p $USER16$ -w $ARG1$ -c $ARG2$
982 | }
983 |
984 | # service definition to check the SMSFinder
985 | define service {
986 | use generic-service
987 | host_name smsfinder
988 | service_description smsfinder
989 | check_command check_smsfinder!40!20 # warning and critical in percent
990 | ## maybe it's whise to alter the service/host template
991 | #contact_groups smsfinders
992 | }
993 |
994 | =head1 EXAMPLE for Nagios notification configuration
995 |
996 | define command {
997 | command_name notify-host-by-sms
998 | command_line /usr/local/nagios/smsack/sendsms.pl -H $HOSTADDRESS:smsfinder$ -u $USER13$ -p $USER14$ -n $CONTACTPAGER$ -m '$NOTIFICATIONTYPE$ $HOSTNAME$ is $HOSTSTATE$ /$SHORTDATETIME$/ $HOSTOUTPUT$'
999 | }
1000 |
1001 | define command {
1002 | command_name notify-service-by-sms
1003 | command_line /usr/local/nagios/smsack/sendsms.pl -H $HOSTADDRESS:smsfinder$ -u $USER13$ -p $USER14$ -n $CONTACTPAGER$ -m '$NOTIFICATIONTYPE$ $HOSTNAME$,$SERVICEDESC$ is $SERVICESTATE$ /$SHORTDATETIME$/ $SERVICEOUTPUT$'
1004 | }
1005 |
1006 | # contact definition - maybe it's wise to alter the contact template
1007 | define contact {
1008 | contact_name smsfinder
1009 | use generic-contact
1010 | alias SMS Nagios Admin
1011 | # send notifications via email and SMS
1012 | service_notification_commands notify-service-by-email,notify-service-by-sms
1013 | host_notification_commands notify-host-by-email,notify-host-by-sms
1014 | email nagios@localhost
1015 | pager +491725555555 # alter this please!
1016 | }
1017 |
1018 | # contact definition - maybe it's wise to alter the contact template
1019 | define contactgroup {
1020 | contactgroup_name smsfinders
1021 | alias SMS Nagios Administrators
1022 | members smsfinder
1023 | }
1024 |
1025 | =head1 EXAMPLE for Apache configuraion
1026 |
1027 | ScriptAlias /nagios/smsack "/usr/local/nagios/smsack"
1028 |
1029 | Options ExecCGI
1030 | AllowOverride None
1031 | Order allow,deny
1032 | Allow from 10.0.10.57 # SMS Finder
1033 |
1034 |
1035 | =cut
1036 |
1037 |
1038 |
1039 | # vim: ts=4 shiftwidth=4 softtabstop=4
1040 | #backspace=indent,eol,start expandtab
1041 |
--------------------------------------------------------------------------------