summaryrefslogtreecommitdiffstats
path: root/plugins/check_ssh.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-02-25 10:14:29 +0100
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-03-07 23:38:50 +0100
commitc87bc7eee4b83571199ffd14b70bfca5418ec101 (patch)
treea0d08f571c3549140548a885bf0a969cb9ff5dba /plugins/check_ssh.c
parentfb4f46f93da4ac50654fdcc2f26b2f37c73a9230 (diff)
downloadmonitoring-plugins-c87bc7eee4b83571199ffd14b70bfca5418ec101.tar.gz
check_ssh: centralize configuration in external header
Diffstat (limited to 'plugins/check_ssh.c')
-rw-r--r--plugins/check_ssh.c62
1 files changed, 35 insertions, 27 deletions
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c
index a50ca530..3745f799 100644
--- a/plugins/check_ssh.c
+++ b/plugins/check_ssh.c
@@ -35,6 +35,7 @@ const char *email = "devel@monitoring-plugins.org";
35#include "./common.h" 35#include "./common.h"
36#include "./netutils.h" 36#include "./netutils.h"
37#include "utils.h" 37#include "utils.h"
38#include "./check_ssh.d/config.h"
38 39
39#ifndef MSG_DONTWAIT 40#ifndef MSG_DONTWAIT
40# define MSG_DONTWAIT 0 41# define MSG_DONTWAIT 0
@@ -43,14 +44,14 @@ const char *email = "devel@monitoring-plugins.org";
43#define SSH_DFL_PORT 22 44#define SSH_DFL_PORT 22
44#define BUFF_SZ 256 45#define BUFF_SZ 256
45 46
46static int port = -1;
47static char *server_name = NULL;
48static char *remote_version = NULL;
49static char *remote_protocol = NULL;
50static bool verbose = false; 47static bool verbose = false;
51 48
52static int process_arguments(int /*argc*/, char ** /*argv*/); 49typedef struct process_arguments_wrapper {
53static int validate_arguments(void); 50 int errorcode;
51 check_ssh_config config;
52} process_arguments_wrapper;
53
54static process_arguments_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
54static void print_help(void); 55static void print_help(void);
55void print_usage(void); 56void print_usage(void);
56 57
@@ -64,17 +65,21 @@ int main(int argc, char **argv) {
64 /* Parse extra opts if any */ 65 /* Parse extra opts if any */
65 argv = np_extra_opts(&argc, argv, progname); 66 argv = np_extra_opts(&argc, argv, progname);
66 67
67 if (process_arguments(argc, argv) == ERROR) { 68 process_arguments_wrapper tmp_config = process_arguments(argc, argv);
69
70 if (tmp_config.errorcode == ERROR) {
68 usage4(_("Could not parse arguments")); 71 usage4(_("Could not parse arguments"));
69 } 72 }
70 73
74 check_ssh_config config = tmp_config.config;
75
71 /* initialize alarm signal handling */ 76 /* initialize alarm signal handling */
72 signal(SIGALRM, socket_timeout_alarm_handler); 77 signal(SIGALRM, socket_timeout_alarm_handler);
73 78
74 alarm(socket_timeout); 79 alarm(socket_timeout);
75 80
76 /* ssh_connect exits if error is found */ 81 /* ssh_connect exits if error is found */
77 int result = ssh_connect(server_name, port, remote_version, remote_protocol); 82 int result = ssh_connect(config.server_name, config.port, config.remote_version, config.remote_protocol);
78 83
79 alarm(0); 84 alarm(0);
80 85
@@ -82,7 +87,7 @@ int main(int argc, char **argv) {
82} 87}
83 88
84/* process command-line arguments */ 89/* process command-line arguments */
85int process_arguments(int argc, char **argv) { 90process_arguments_wrapper process_arguments(int argc, char **argv) {
86 static struct option longopts[] = {{"help", no_argument, 0, 'h'}, 91 static struct option longopts[] = {{"help", no_argument, 0, 'h'},
87 {"version", no_argument, 0, 'V'}, 92 {"version", no_argument, 0, 'V'},
88 {"host", required_argument, 0, 'H'}, /* backward compatibility */ 93 {"host", required_argument, 0, 'H'}, /* backward compatibility */
@@ -96,8 +101,14 @@ int process_arguments(int argc, char **argv) {
96 {"remote-protocol", required_argument, 0, 'P'}, 101 {"remote-protocol", required_argument, 0, 'P'},
97 {0, 0, 0, 0}}; 102 {0, 0, 0, 0}};
98 103
104 process_arguments_wrapper result = {
105 .config = check_ssh_config_init(),
106 .errorcode = OK,
107 };
108
99 if (argc < 2) { 109 if (argc < 2) {
100 return ERROR; 110 result.errorcode = ERROR;
111 return result;
101 } 112 }
102 113
103 for (int i = 1; i < argc; i++) { 114 for (int i = 1; i < argc; i++) {
@@ -145,20 +156,20 @@ int process_arguments(int argc, char **argv) {
145#endif 156#endif
146 break; 157 break;
147 case 'r': /* remote version */ 158 case 'r': /* remote version */
148 remote_version = optarg; 159 result.config.remote_version = optarg;
149 break; 160 break;
150 case 'P': /* remote version */ 161 case 'P': /* remote version */
151 remote_protocol = optarg; 162 result.config.remote_protocol = optarg;
152 break; 163 break;
153 case 'H': /* host */ 164 case 'H': /* host */
154 if (!is_host(optarg)) { 165 if (!is_host(optarg)) {
155 usage2(_("Invalid hostname/address"), optarg); 166 usage2(_("Invalid hostname/address"), optarg);
156 } 167 }
157 server_name = optarg; 168 result.config.server_name = optarg;
158 break; 169 break;
159 case 'p': /* port */ 170 case 'p': /* port */
160 if (is_intpos(optarg)) { 171 if (is_intpos(optarg)) {
161 port = atoi(optarg); 172 result.config.port = atoi(optarg);
162 } else { 173 } else {
163 usage2(_("Port number must be a positive integer"), optarg); 174 usage2(_("Port number must be a positive integer"), optarg);
164 } 175 }
@@ -166,32 +177,29 @@ int process_arguments(int argc, char **argv) {
166 } 177 }
167 178
168 option_char = optind; 179 option_char = optind;
169 if (server_name == NULL && option_char < argc) { 180 if (result.config.server_name == NULL && option_char < argc) {
170 if (is_host(argv[option_char])) { 181 if (is_host(argv[option_char])) {
171 server_name = argv[option_char++]; 182 result.config.server_name = argv[option_char++];
172 } 183 }
173 } 184 }
174 185
175 if (port == -1 && option_char < argc) { 186 if (result.config.port == -1 && option_char < argc) {
176 if (is_intpos(argv[option_char])) { 187 if (is_intpos(argv[option_char])) {
177 port = atoi(argv[option_char++]); 188 result.config.port = atoi(argv[option_char++]);
178 } else { 189 } else {
179 print_usage(); 190 print_usage();
180 exit(STATE_UNKNOWN); 191 exit(STATE_UNKNOWN);
181 } 192 }
182 } 193 }
183 194
184 return validate_arguments(); 195 if (result.config.server_name == NULL) {
185} 196 result.errorcode = ERROR;
186 197 return result;
187int validate_arguments(void) {
188 if (server_name == NULL) {
189 return ERROR;
190 } 198 }
191 if (port == -1) { /* funky, but allows -p to override stray integer in args */ 199 if (result.config.port == -1) { /* funky, but allows -p to override stray integer in args */
192 port = SSH_DFL_PORT; 200 result.config.port = SSH_DFL_PORT;
193 } 201 }
194 return OK; 202 return result;
195} 203}
196 204
197/************************************************************************ 205/************************************************************************