[1] | 1 | /* SAMHAIN file system integrity testing */
|
---|
| 2 | /* Copyright (C) 2003 Rainer Wichmann */
|
---|
| 3 | /* */
|
---|
| 4 | /* This program is free software; you can redistribute it */
|
---|
| 5 | /* and/or modify */
|
---|
| 6 | /* it under the terms of the GNU General Public License as */
|
---|
| 7 | /* published by */
|
---|
| 8 | /* the Free Software Foundation; either version 2 of the License, or */
|
---|
| 9 | /* (at your option) any later version. */
|
---|
| 10 | /* */
|
---|
| 11 | /* This program is distributed in the hope that it will be useful, */
|
---|
| 12 | /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
---|
| 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
---|
| 14 | /* GNU General Public License for more details. */
|
---|
| 15 | /* */
|
---|
| 16 | /* You should have received a copy of the GNU General Public License */
|
---|
| 17 | /* along with this program; if not, write to the Free Software */
|
---|
| 18 | /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
---|
| 19 |
|
---|
| 20 | #include "config_xor.h"
|
---|
| 21 |
|
---|
| 22 | #include <stddef.h>
|
---|
| 23 | #include <stdio.h>
|
---|
| 24 | #include <stdlib.h>
|
---|
| 25 | #include <string.h>
|
---|
[481] | 26 | #include <ctype.h>
|
---|
[1] | 27 | #include <errno.h>
|
---|
| 28 |
|
---|
| 29 | #include <unistd.h>
|
---|
| 30 | #include <fcntl.h>
|
---|
| 31 |
|
---|
| 32 | #include <sys/socket.h>
|
---|
| 33 | #include <sys/un.h>
|
---|
| 34 |
|
---|
| 35 | #include <signal.h>
|
---|
| 36 | #include <pwd.h>
|
---|
| 37 |
|
---|
| 38 | #if !defined(AF_FILE)
|
---|
| 39 | #define AF_FILE AF_UNIX
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | #define SH_MAXMSG 209
|
---|
| 43 |
|
---|
[481] | 44 | #if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && \
|
---|
| 45 | !defined(HAVE_STRUCT_CMSGCRED) && !defined(HAVE_STRUCT_FCRED) && \
|
---|
| 46 | !(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))
|
---|
| 47 | #define SH_REQ_PASSWORD 1
|
---|
| 48 | #endif
|
---|
| 49 |
|
---|
[562] | 50 | #define SH_PW_SIZE 15
|
---|
| 51 |
|
---|
[1] | 52 | static int sock = -1;
|
---|
[562] | 53 | static char password[SH_PW_SIZE] = "";
|
---|
[1] | 54 | static int verbose = 0;
|
---|
| 55 |
|
---|
| 56 | #ifdef SH_STEALTH
|
---|
| 57 | char * globber(const char * string);
|
---|
| 58 | #define _(string) globber(string)
|
---|
| 59 | #define N_(string) string
|
---|
| 60 | #else
|
---|
| 61 | #define _(string) string
|
---|
| 62 | #define N_(string) string
|
---|
| 63 | #endif
|
---|
| 64 |
|
---|
| 65 | #ifdef SH_STEALTH
|
---|
| 66 | #ifndef SH_MAX_GLOBS
|
---|
| 67 | #define SH_MAX_GLOBS 32
|
---|
| 68 | #endif
|
---|
| 69 | char * globber(const char * str)
|
---|
| 70 | {
|
---|
| 71 | register int i, j;
|
---|
| 72 | static int count = -1;
|
---|
| 73 | static char glob[SH_MAX_GLOBS][128];
|
---|
| 74 |
|
---|
| 75 | ++count; if (count > (SH_MAX_GLOBS-1) ) count = 0;
|
---|
| 76 | j = strlen(str);
|
---|
| 77 | if (j > 127) j = 127;
|
---|
| 78 |
|
---|
| 79 | for (i = 0; i < j; ++i)
|
---|
| 80 | {
|
---|
| 81 | if (str[i] != '\n' && str[i] != '\t')
|
---|
| 82 | glob[count][i] = str[i] ^ XOR_CODE;
|
---|
| 83 | else
|
---|
| 84 | glob[count][i] = str[i];
|
---|
| 85 | }
|
---|
| 86 | glob[count][j] = '\0';
|
---|
| 87 | return glob[count];
|
---|
| 88 | }
|
---|
| 89 | #endif
|
---|
| 90 |
|
---|
| 91 | #define CLIENT _("yulectl")
|
---|
| 92 |
|
---|
| 93 |
|
---|
[481] | 94 | static int
|
---|
| 95 | create_unix_socket ()
|
---|
[1] | 96 | {
|
---|
| 97 | int sock;
|
---|
| 98 |
|
---|
| 99 | /* Create the socket. */
|
---|
| 100 |
|
---|
| 101 | sock = socket (PF_UNIX, SOCK_STREAM, 0);
|
---|
| 102 | if (sock < 0)
|
---|
| 103 | {
|
---|
| 104 | perror (_("ERROR: socket"));
|
---|
| 105 | return -1;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return sock;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[481] | 111 | static void
|
---|
[1] | 112 | termination_handler (int signum)
|
---|
| 113 | {
|
---|
| 114 | /* Clean up. */
|
---|
| 115 | if (signum != 0)
|
---|
| 116 | {
|
---|
| 117 | if (verbose)
|
---|
| 118 | fprintf(stdout, _("# Terminated on signal %d\n"), signum);
|
---|
| 119 | }
|
---|
[481] | 120 | if (sock >= 0 )
|
---|
| 121 | close (sock);
|
---|
[1] | 122 | return;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[481] | 125 | static char * safe_copy(char * to, const char * from, size_t size)
|
---|
| 126 | {
|
---|
[562] | 127 | if (to && from && (size > 0))
|
---|
[481] | 128 | {
|
---|
[562] | 129 | strncpy (to, from, size-1);
|
---|
| 130 | to[size-1] = '\0';
|
---|
[481] | 131 | }
|
---|
| 132 | return to;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[1] | 135 |
|
---|
[481] | 136 | static int send_to_server (char * serversock, char * message)
|
---|
[1] | 137 | {
|
---|
| 138 | struct sockaddr_un name;
|
---|
| 139 | int size;
|
---|
| 140 | int nbytes;
|
---|
| 141 |
|
---|
| 142 | /* Initialize the server socket address.
|
---|
| 143 | */
|
---|
| 144 | name.sun_family = AF_UNIX;
|
---|
[562] | 145 | memcpy(name.sun_path, serversock, sizeof(name.sun_path));
|
---|
| 146 | name.sun_path[sizeof(name.sun_path)-1] = '\0';
|
---|
| 147 | if (strlen(serversock) > strlen(name.sun_path))
|
---|
| 148 | {
|
---|
| 149 | perror (_("ERROR: socket path too long"));
|
---|
| 150 | return -1;
|
---|
| 151 | }
|
---|
[1] | 152 | size = (offsetof (struct sockaddr_un, sun_path)
|
---|
| 153 | + strlen (name.sun_path) + 1);
|
---|
| 154 |
|
---|
| 155 | nbytes = connect(sock, (struct sockaddr *) & name, size);
|
---|
| 156 | if (nbytes < 0)
|
---|
| 157 | {
|
---|
| 158 | perror (_("ERROR: connect"));
|
---|
| 159 | return -1;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[481] | 162 | /* Send the data.
|
---|
[1] | 163 | */
|
---|
| 164 | nbytes = send (sock, message, strlen (message) + 1, 0);
|
---|
| 165 | if (nbytes < 0)
|
---|
| 166 | {
|
---|
| 167 | perror (_("ERROR: send"));
|
---|
| 168 | return -1;
|
---|
| 169 | }
|
---|
| 170 | return 0;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | static int getline_from_server (int sock, char * buf, int size)
|
---|
| 174 | {
|
---|
| 175 | int nbytes = 0;
|
---|
| 176 | int status = 0;
|
---|
| 177 | char * p = buf;
|
---|
| 178 |
|
---|
| 179 | do {
|
---|
| 180 | status = read (sock, p, 1);
|
---|
| 181 | if (status <= 0)
|
---|
| 182 | {
|
---|
| 183 | buf[nbytes] = '\0';
|
---|
| 184 | return ((status == 0) ? nbytes : status);
|
---|
| 185 | }
|
---|
| 186 | else if (*p == '\0')
|
---|
| 187 | {
|
---|
| 188 | return nbytes;
|
---|
| 189 | }
|
---|
| 190 | ++nbytes; ++p;
|
---|
| 191 | } while (nbytes < size);
|
---|
| 192 | buf[size-1] = '\0';
|
---|
| 193 | return 0;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[481] | 196 | static int recv_from_server (char * message)
|
---|
[1] | 197 | {
|
---|
| 198 | int nbytes = 0;
|
---|
| 199 | char recvmsg[SH_MAXMSG];
|
---|
| 200 | int num = 0;
|
---|
| 201 | int good = -1;
|
---|
| 202 | char * p;
|
---|
| 203 |
|
---|
| 204 | if (password[0] == '\0')
|
---|
[481] | 205 | p = message;
|
---|
[1] | 206 | else
|
---|
[481] | 207 | p = &message[strlen(password)+1];
|
---|
[1] | 208 |
|
---|
[481] | 209 | if (0 == strncmp(p, _("PROBE"), 5) ||
|
---|
| 210 | 0 == strncmp(p, _("LIST"), 4))
|
---|
[1] | 211 | {
|
---|
| 212 | do {
|
---|
| 213 | nbytes = getline_from_server (sock, recvmsg, SH_MAXMSG);
|
---|
| 214 | if (nbytes < 0)
|
---|
| 215 | {
|
---|
| 216 | if (errno == EAGAIN)
|
---|
[481] | 217 | return 0;
|
---|
[1] | 218 | else
|
---|
| 219 | {
|
---|
| 220 | perror (_("ERROR: recv"));
|
---|
| 221 | return -1;
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | else if (nbytes == 0)
|
---|
[481] | 225 | return 0;
|
---|
| 226 |
|
---|
[1] | 227 | if (recvmsg[0] == 'E' && recvmsg[1] == 'N' && recvmsg[2] == 'D')
|
---|
| 228 | {
|
---|
| 229 | if (verbose && (num == 0))
|
---|
[396] | 230 | fprintf (stdout, "%s", _("# There are no pending commands.\n"));
|
---|
[1] | 231 | return 0;
|
---|
| 232 | }
|
---|
| 233 | ++num;
|
---|
| 234 | fprintf (stdout, _("%03d: %s\n"), num, recvmsg);
|
---|
| 235 | } while (nbytes >= 0);
|
---|
| 236 | }
|
---|
| 237 | else
|
---|
| 238 | {
|
---|
| 239 | nbytes = recv (sock, recvmsg, SH_MAXMSG, 0);
|
---|
| 240 | if (nbytes < 0)
|
---|
| 241 | {
|
---|
| 242 | perror (_("ERROR: recv"));
|
---|
| 243 | return -1;
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /* Print a diagnostic message. */
|
---|
| 248 | if (password[0] == '\0')
|
---|
[481] | 249 | good = strcmp (message, recvmsg);
|
---|
[1] | 250 | else
|
---|
[481] | 251 | good = strcmp (&message[strlen(password)+1], recvmsg);
|
---|
[1] | 252 |
|
---|
| 253 | if (0 != good)
|
---|
| 254 | {
|
---|
[481] | 255 | if (0 == strncmp(recvmsg, _("!E:"), 3))
|
---|
| 256 | {
|
---|
| 257 | fputs(recvmsg, stderr);
|
---|
| 258 | fputc('\n', stderr);
|
---|
| 259 | }
|
---|
| 260 | else
|
---|
| 261 | {
|
---|
| 262 | fputs (_("ERROR: Bounced message != original message.\n"), stderr);
|
---|
| 263 | }
|
---|
[1] | 264 | return -1;
|
---|
| 265 | }
|
---|
| 266 | else
|
---|
| 267 | {
|
---|
| 268 | if (verbose)
|
---|
[396] | 269 | fprintf (stdout, "%s", _("# Message received by server.\n"));
|
---|
[1] | 270 | }
|
---|
| 271 |
|
---|
| 272 | return 0;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[481] | 275 | static int check_uuid(const char * in)
|
---|
[1] | 276 | {
|
---|
[481] | 277 | int i;
|
---|
| 278 | const char *cp;
|
---|
| 279 |
|
---|
| 280 | if (!in || strlen(in) != 36)
|
---|
| 281 | return -1;
|
---|
| 282 | for (i=0, cp = in; i <= 36; i++,cp++) {
|
---|
| 283 | if ((i == 8) || (i == 13) || (i == 18) ||
|
---|
| 284 | (i == 23)) {
|
---|
| 285 | if (*cp == '-')
|
---|
| 286 | continue;
|
---|
| 287 | else
|
---|
| 288 | return -1;
|
---|
| 289 | }
|
---|
| 290 | if (i== 36)
|
---|
| 291 | if (*cp == 0)
|
---|
| 292 | continue;
|
---|
| 293 | if (!isxdigit(*cp))
|
---|
| 294 | return -1;
|
---|
| 295 | }
|
---|
| 296 | return 0;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | static int check_command(const char * str)
|
---|
| 300 | {
|
---|
| 301 | unsigned int i = 0;
|
---|
| 302 | char * commands[] = { N_("DELTA:"), N_("RELOAD"), N_("STOP"), N_("SCAN"),
|
---|
| 303 | N_("CANCEL"), N_("LISTALL"), N_("LIST"), N_("PROBE"), NULL };
|
---|
| 304 |
|
---|
| 305 | while (commands[i])
|
---|
| 306 | {
|
---|
| 307 | size_t len = strlen(_(commands[i]));
|
---|
| 308 |
|
---|
| 309 | if (0 == strncmp(_(commands[i]), str, len))
|
---|
| 310 | {
|
---|
| 311 | if (i == 0)
|
---|
| 312 | {
|
---|
| 313 | char * p = strchr(str, ':'); ++p;
|
---|
| 314 | if ( 0 == check_uuid(p) )
|
---|
| 315 | return 0;
|
---|
| 316 | }
|
---|
| 317 | else
|
---|
| 318 | {
|
---|
| 319 | if (len == strlen(str))
|
---|
| 320 | return 0;
|
---|
| 321 | }
|
---|
| 322 | }
|
---|
| 323 | ++i;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | fprintf (stderr, _("ERROR: invalid command <%s>\n\n"), str);
|
---|
| 327 | return -1;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | static void print_usage_and_exit(char * name, int exit_status)
|
---|
| 331 | {
|
---|
[1] | 332 | printf(_("\nUsage : %s [-v][-s server_socket] -c command <client_hostname>\n\n"),
|
---|
| 333 | name);
|
---|
| 334 |
|
---|
[396] | 335 | printf("%s", _("Purpose : send commands to the server via a socket,\n"));
|
---|
| 336 | printf("%s", _(" in particular commands that the server would\n"));
|
---|
| 337 | printf("%s", _(" transfer to the client <client_hostname> when\n"));
|
---|
| 338 | printf("%s", _(" this client connects to deliver a message.\n\n"));
|
---|
| 339 | printf("%s", _(" If password is required, it is read from\n"));
|
---|
| 340 | printf("%s", _(" $HOME/.yulectl_cred or taken from the environment\n"));
|
---|
| 341 | printf("%s", _(" variable YULECTL_PASSWORD (not recommended).\n\n"));
|
---|
[1] | 342 |
|
---|
[481] | 343 | printf("%s", _("Commands: RELOAD reload configuration\n"));
|
---|
| 344 | printf("%s", _(" DELTA:<uuid> load delta database with given uuid\n"));
|
---|
| 345 | printf("%s", _(" STOP terminate\n"));
|
---|
| 346 | printf("%s", _(" SCAN initiate file system check\n"));
|
---|
| 347 | printf("%s", _(" CANCEL cancel pending command(s)\n"));
|
---|
| 348 | printf("%s", _(" LIST list queued commands\n"));
|
---|
| 349 | printf("%s", _(" LISTALL list queued and last sent commands\n"));
|
---|
| 350 | printf("%s", _(" PROBE probe all clients for necessity of reload\n"));
|
---|
| 351 | exit(exit_status);
|
---|
[1] | 352 | }
|
---|
| 353 |
|
---|
[117] | 354 | char * rtrim(char * str)
|
---|
| 355 | {
|
---|
| 356 | size_t len;
|
---|
| 357 |
|
---|
[481] | 358 | if (!str) return str;
|
---|
[117] | 359 |
|
---|
| 360 | len = strlen(str);
|
---|
| 361 | while (len > 0)
|
---|
| 362 | {
|
---|
| 363 | --len;
|
---|
| 364 | if (str[len] == '\n' || str[len] == '\r')
|
---|
| 365 | str[len] = '\0';
|
---|
| 366 | else
|
---|
| 367 | break;
|
---|
| 368 | }
|
---|
| 369 | return str;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[481] | 372 | static int get_home(char * home, size_t size)
|
---|
[1] | 373 | {
|
---|
| 374 | struct passwd * pwent;
|
---|
| 375 |
|
---|
| 376 | pwent = getpwuid(geteuid());
|
---|
| 377 | if ((pwent == 0) || (pwent->pw_dir == NULL))
|
---|
| 378 | {
|
---|
| 379 | if (verbose)
|
---|
| 380 | fprintf (stderr, _("WARNING: no home directory for euid %ld\n"),
|
---|
| 381 | (long) geteuid());
|
---|
| 382 | if (NULL != getenv(_("HOME")))
|
---|
| 383 | {
|
---|
[481] | 384 | safe_copy(home, getenv(_("HOME")), size);
|
---|
[1] | 385 | }
|
---|
| 386 | else
|
---|
| 387 | {
|
---|
| 388 | fprintf (stderr, _("ERROR: no home directory for euid %ld (tried $HOME and password database).\n"), (long) geteuid());
|
---|
[481] | 389 | return -1;
|
---|
[1] | 390 | }
|
---|
| 391 | }
|
---|
| 392 | else
|
---|
| 393 | {
|
---|
[481] | 394 | safe_copy(home, pwent->pw_dir, size);
|
---|
[1] | 395 | }
|
---|
[481] | 396 | return 0;
|
---|
| 397 | }
|
---|
[1] | 398 |
|
---|
[481] | 399 | static int get_passwd(char * message2, size_t size)
|
---|
| 400 | {
|
---|
| 401 | char home[4096];
|
---|
| 402 | FILE * fp;
|
---|
| 403 | char * pw;
|
---|
| 404 |
|
---|
| 405 | /* 1) Password from environment
|
---|
| 406 | */
|
---|
| 407 | pw = getenv(_("YULECTL_PASSWORD"));
|
---|
[562] | 408 | if (pw && strlen(pw) < SH_PW_SIZE)
|
---|
[481] | 409 | {
|
---|
| 410 | strcpy(password, pw);
|
---|
| 411 | strcpy(message2, password);
|
---|
| 412 | return 0;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
| 415 | /* 2) Password from $HOME/.yule_cred
|
---|
| 416 | */
|
---|
| 417 | if (get_home(home, sizeof(home)) < 0)
|
---|
| 418 | return -1;
|
---|
| 419 |
|
---|
[562] | 420 | if ( (strlen(home) + strlen(_("/.yulectl_cred")) + 1) > sizeof(home))
|
---|
[1] | 421 | {
|
---|
[396] | 422 | fprintf (stderr, "%s", _("ERROR: path for $HOME is too long.\n"));
|
---|
[481] | 423 | return -1;
|
---|
[1] | 424 | }
|
---|
| 425 | strcat(home, _("/.yulectl_cred"));
|
---|
| 426 | fp = fopen(home, "r");
|
---|
[200] | 427 |
|
---|
[481] | 428 | #if defined(SH_REQ_PASSWORD)
|
---|
[1] | 429 | if (fp == NULL)
|
---|
| 430 | {
|
---|
[200] | 431 | if (errno == ENOENT) {
|
---|
| 432 | fprintf (stderr,
|
---|
| 433 | _("ERROR No password file (%s) exists\n"),
|
---|
[1] | 434 | home);
|
---|
[200] | 435 | }
|
---|
| 436 | else {
|
---|
| 437 | fprintf (stderr,
|
---|
| 438 | _("ERROR: Password file (%s) not accessible for euid %ld uid %ld\n"),
|
---|
[1] | 439 | home, (long)geteuid(), (long)getuid());
|
---|
[200] | 440 | }
|
---|
[481] | 441 | return -1;
|
---|
[1] | 442 | }
|
---|
[200] | 443 | #else
|
---|
| 444 | if (fp == NULL)
|
---|
[481] | 445 | return 0;
|
---|
[200] | 446 | #endif
|
---|
| 447 |
|
---|
[481] | 448 | if (NULL == fgets(message2, size, fp))
|
---|
[1] | 449 | {
|
---|
[396] | 450 | fprintf (stderr,
|
---|
[1] | 451 | _("ERROR: empty or unreadable password file (%s).\n"),
|
---|
| 452 | home);
|
---|
[481] | 453 | return -1;
|
---|
[1] | 454 | }
|
---|
[117] | 455 |
|
---|
| 456 | (void) rtrim(message2);
|
---|
| 457 |
|
---|
[562] | 458 | if (strlen(message2) > (SH_PW_SIZE -1))
|
---|
[1] | 459 | {
|
---|
[396] | 460 | fprintf (stderr, "%s",
|
---|
[1] | 461 | _("ERROR: Password too long (max. 14 characters).\n"));
|
---|
[481] | 462 | return -1;
|
---|
[1] | 463 | }
|
---|
| 464 | strcpy(password, message2);
|
---|
[22] | 465 | fclose(fp);
|
---|
[117] | 466 |
|
---|
[481] | 467 | return 0;
|
---|
| 468 | }
|
---|
[1] | 469 |
|
---|
[481] | 470 | static int fixup_message (char * message)
|
---|
| 471 | {
|
---|
| 472 | char message_fixed[SH_MAXMSG] = { 0 };
|
---|
| 473 |
|
---|
| 474 | if (get_passwd(message_fixed, sizeof(message_fixed)) < 0)
|
---|
| 475 | return -1;
|
---|
| 476 |
|
---|
| 477 | if (strlen(message_fixed) > 0)
|
---|
| 478 | {
|
---|
| 479 | strcat(message_fixed, "@");
|
---|
| 480 |
|
---|
| 481 | strncat(message_fixed, message, SH_MAXMSG - strlen(message_fixed) -1);
|
---|
| 482 | message_fixed[SH_MAXMSG-1] = '\0';
|
---|
| 483 | strcpy(message, message_fixed);
|
---|
| 484 | }
|
---|
| 485 | return 0;
|
---|
[1] | 486 | }
|
---|
| 487 |
|
---|
[481] | 488 | static int fill_serversock(char * serversock, size_t size)
|
---|
[1] | 489 | {
|
---|
[481] | 490 | int status;
|
---|
[1] | 491 |
|
---|
| 492 | #ifdef HAVE_VSNPRINTF
|
---|
[481] | 493 | status = snprintf(serversock, size, _("%s/%s.sock"),
|
---|
[1] | 494 | DEFAULT_PIDDIR, SH_INSTALL_NAME);
|
---|
| 495 | #else
|
---|
[481] | 496 | if ((strlen(DEFAULT_PIDDIR) + strlen(SH_INSTALL_NAME) + 1 + 6) > size)
|
---|
| 497 | status = -1;
|
---|
[1] | 498 | else
|
---|
[481] | 499 | status = sprintf (serversock, _("%s/%s.sock"),
|
---|
| 500 | DEFAULT_PIDDIR, SH_INSTALL_NAME);
|
---|
| 501 | #endif
|
---|
| 502 |
|
---|
| 503 | if ((status < 0) || (status > (int)(size-1)))
|
---|
[1] | 504 | {
|
---|
[481] | 505 | fprintf(stderr, _("ERROR: Path too long (maximum %d): %s/%s.sock\n"),
|
---|
| 506 | (int) (size-1), DEFAULT_PIDDIR, SH_INSTALL_NAME);
|
---|
| 507 | return -1;
|
---|
[1] | 508 | }
|
---|
[481] | 509 | return 0;
|
---|
| 510 | }
|
---|
[1] | 511 |
|
---|
[481] | 512 | static void checklen(char * command, char * str, size_t maxlen)
|
---|
| 513 | {
|
---|
| 514 | if (strlen(str) > maxlen)
|
---|
[1] | 515 | {
|
---|
[481] | 516 | fprintf(stderr, _("ERROR: String too long (max %d): %s\n\n"),
|
---|
| 517 | (int) maxlen, str);
|
---|
| 518 | print_usage_and_exit (command, EXIT_FAILURE);
|
---|
[1] | 519 | }
|
---|
[481] | 520 | return;
|
---|
| 521 | }
|
---|
[1] | 522 |
|
---|
[481] | 523 | static void checknull(char * command, char * str)
|
---|
| 524 | {
|
---|
| 525 | if (str == NULL || str[0] == '\0') {
|
---|
| 526 | fprintf(stderr, "%s", _("ERROR: option with missing argument\n\n"));
|
---|
| 527 | print_usage_and_exit(command, EXIT_FAILURE);
|
---|
| 528 | }
|
---|
| 529 | return;
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | int
|
---|
| 533 | main (int argc, char * argv[])
|
---|
| 534 | {
|
---|
| 535 |
|
---|
| 536 | char message[SH_MAXMSG] = "";
|
---|
| 537 | char serversock[256];
|
---|
| 538 | int status;
|
---|
| 539 | int num = 1;
|
---|
| 540 | int flag = 0;
|
---|
| 541 |
|
---|
| 542 | if (fill_serversock(serversock, sizeof(serversock)) < 0)
|
---|
| 543 | return (EXIT_FAILURE);
|
---|
| 544 |
|
---|
| 545 |
|
---|
[1] | 546 | while (argc > 1 && argv[num][0] == '-')
|
---|
| 547 | {
|
---|
| 548 | switch (argv[num][1])
|
---|
| 549 | {
|
---|
| 550 | case 'h':
|
---|
[481] | 551 | print_usage_and_exit(argv[0], EXIT_SUCCESS);
|
---|
| 552 | break;
|
---|
[1] | 553 | case 'v':
|
---|
| 554 | ++verbose;
|
---|
| 555 | break;
|
---|
| 556 | case 's':
|
---|
| 557 | --argc; ++num;
|
---|
[481] | 558 | checknull(argv[0], argv[num]);
|
---|
| 559 | checklen(argv[0], argv[num], sizeof(serversock)-1);
|
---|
| 560 | safe_copy (serversock, argv[num], sizeof(serversock));
|
---|
[1] | 561 | break;
|
---|
| 562 | case 'c':
|
---|
| 563 | --argc; ++num;
|
---|
[481] | 564 | checknull(argv[0], argv[num]);
|
---|
| 565 | checklen(argv[0], argv[num], SH_MAXMSG-1);
|
---|
| 566 | if (0 != check_command(argv[num]))
|
---|
| 567 | print_usage_and_exit(argv[0], EXIT_FAILURE);
|
---|
| 568 | safe_copy(message, argv[num], SH_MAXMSG);
|
---|
| 569 | strncat(message, ":", SH_MAXMSG-strlen(message)-1);
|
---|
| 570 | message[SH_MAXMSG-1] = '\0';
|
---|
| 571 | flag = 1;
|
---|
[1] | 572 | break;
|
---|
| 573 | default:
|
---|
[481] | 574 | fprintf(stderr, _("ERROR: unknown option -%c\n\n"), argv[num][1]);
|
---|
| 575 | print_usage_and_exit(argv[0], EXIT_FAILURE);
|
---|
| 576 | break;
|
---|
[1] | 577 | }
|
---|
| 578 | --argc; ++num;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | if (flag == 0) /* no command given */
|
---|
[481] | 582 | print_usage_and_exit(argv[0], EXIT_FAILURE);
|
---|
[1] | 583 |
|
---|
| 584 | if (argc > 1)
|
---|
| 585 | {
|
---|
[481] | 586 | checklen(argv[0], argv[num], SH_MAXMSG - strlen(message) - 1);
|
---|
| 587 | strncat (message, argv[num], SH_MAXMSG - strlen(message) - 1);
|
---|
[1] | 588 | message[SH_MAXMSG-1] = '\0';
|
---|
| 589 | }
|
---|
| 590 | else
|
---|
| 591 | {
|
---|
[481] | 592 | if (0 == strncmp(message, _("PROBE"), 5) ||
|
---|
| 593 | 0 == strncmp(message, _("LIST"), 4))
|
---|
[1] | 594 | {
|
---|
| 595 | strncat (message, _("dummy"), SH_MAXMSG -strlen(message) - 1);
|
---|
| 596 | message[SH_MAXMSG-1] = '\0';
|
---|
| 597 | }
|
---|
| 598 | else
|
---|
| 599 | {
|
---|
[396] | 600 | fprintf(stderr, "%s", _("ERROR: this command requires a hostname\n"));
|
---|
[481] | 601 | print_usage_and_exit(argv[0], EXIT_FAILURE);
|
---|
[1] | 602 | }
|
---|
| 603 | }
|
---|
| 604 |
|
---|
[481] | 605 | if (fixup_message(message) < 0)
|
---|
| 606 | return (EXIT_FAILURE);
|
---|
[1] | 607 |
|
---|
| 608 | /* Make the socket.
|
---|
| 609 | */
|
---|
[481] | 610 | sock = create_unix_socket ();
|
---|
[1] | 611 | if (sock < 0)
|
---|
[481] | 612 | return (EXIT_FAILURE);
|
---|
[1] | 613 |
|
---|
| 614 | /* Set up termination handler.
|
---|
| 615 | */
|
---|
| 616 | signal (SIGINT, termination_handler);
|
---|
| 617 | signal (SIGHUP, termination_handler);
|
---|
| 618 | signal (SIGTERM, termination_handler);
|
---|
| 619 | signal (SIGQUIT, termination_handler);
|
---|
| 620 |
|
---|
| 621 | /* Send the datagram.
|
---|
| 622 | */
|
---|
| 623 | status = send_to_server (serversock, message);
|
---|
| 624 | if (status < 0)
|
---|
| 625 | {
|
---|
[396] | 626 | fprintf(stderr, "%s", _("ERROR: sending command to server failed\n"));
|
---|
[1] | 627 | (void) termination_handler(0);
|
---|
| 628 | return (EXIT_FAILURE);
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | /* Wait for a reply.
|
---|
| 632 | */
|
---|
[481] | 633 | if (verbose)
|
---|
[1] | 634 | {
|
---|
[481] | 635 | if (0 == strncmp(message, "LIST", 4))
|
---|
[396] | 636 | fprintf(stdout, "%s", _("# Waiting for listing.\n"));
|
---|
[481] | 637 | else
|
---|
[396] | 638 | fprintf(stdout, "%s", _("# Waiting for confirmation.\n"));
|
---|
[1] | 639 | }
|
---|
[481] | 640 |
|
---|
[1] | 641 | status = recv_from_server (message);
|
---|
| 642 |
|
---|
| 643 | if (status < 0)
|
---|
| 644 | {
|
---|
[481] | 645 | fputs(_("ERROR: unexpected or no reply from server.\n"), stderr);
|
---|
[1] | 646 | (void) termination_handler(0);
|
---|
| 647 | return (EXIT_FAILURE);
|
---|
| 648 | }
|
---|
| 649 |
|
---|
| 650 | /* Clean up. */
|
---|
| 651 | (void) termination_handler(0);
|
---|
| 652 | return (EXIT_SUCCESS);
|
---|
| 653 | }
|
---|
| 654 |
|
---|