| 1 |
/* |
| 2 |
* pkd_util.c -- pkd utilities |
| 3 |
* |
| 4 |
* (c) 2014, 2018 Jon Simons <[email protected]> |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <errno.h> |
| 8 |
#include <limits.h> |
| 9 |
#include <signal.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/wait.h> |
| 14 |
|
| 15 |
#include "pkd_client.h" |
| 16 |
#include "pkd_util.h" |
| 17 |
|
| 18 |
/** |
| 19 |
* @brief runs system(3); exits if that is interrupted with SIGINT/QUIT |
| 20 |
* @returns 0 upon success, non-zero otherwise |
| 21 |
*/ |
| 22 |
int system_checked(const char *cmd) { |
| 23 |
int rc = system(cmd); |
| 24 |
|
| 25 |
if (WIFSIGNALED(rc) && |
| 26 |
((WTERMSIG(rc) == SIGINT) || (WTERMSIG(rc) == SIGQUIT))) { |
| 27 |
exit(1); |
| 28 |
} |
| 29 |
|
| 30 |
if (rc == -1) { |
| 31 |
return -1; |
| 32 |
} |
| 33 |
|
| 34 |
return WEXITSTATUS(rc); |
| 35 |
} |
| 36 |
|
| 37 |
static int bin_exists(const char *binary) { |
| 38 |
char bin[1024] = { 0 }; |
| 39 |
snprintf(&bin[0], sizeof(bin), "type %s 1>/dev/null 2>/dev/null", binary); |
| 40 |
return (system_checked(bin) == 0); |
| 41 |
} |
| 42 |
|
| 43 |
static int is_openssh_client_new_enough(void) { |
| 44 |
int rc = -1; |
| 45 |
FILE *fp = NULL; |
| 46 |
char version_buff[1024] = { 0 }; |
| 47 |
char *version; |
| 48 |
|
| 49 |
static int version_ok = 0; |
| 50 |
unsigned long int major = 0; |
| 51 |
char *tmp = NULL; |
| 52 |
|
| 53 |
if (version_ok) { |
| 54 |
return version_ok; |
| 55 |
} |
| 56 |
|
| 57 |
fp = popen("ssh -V 2>&1", "r"); |
| 58 |
if (fp == NULL) { |
| 59 |
fprintf(stderr, "failed to get OpenSSH client version\n"); |
| 60 |
goto done; |
| 61 |
} |
| 62 |
|
| 63 |
do { |
| 64 |
if (fgets(&version_buff[0], sizeof(version_buff), fp) == NULL) { |
| 65 |
fprintf(stderr, "failed to get OpenSSH client version string\n"); |
| 66 |
goto errfgets; |
| 67 |
} |
| 68 |
version = strstr(version_buff, "OpenSSH"); |
| 69 |
} while(version == NULL); |
| 70 |
|
| 71 |
/* "OpenSSH_<major>.<minor><SP>..." */ |
| 72 |
if (strlen(version) < 11) { |
| 73 |
goto errversion; |
| 74 |
} |
| 75 |
|
| 76 |
/* Extract major. */ |
| 77 |
major = strtoul(version + 8, &tmp, 10); |
| 78 |
if ((tmp == (version + 8)) || |
| 79 |
((errno == ERANGE) && (major == ULONG_MAX)) || |
| 80 |
((errno != 0) && (major == 0)) || |
| 81 |
((major < 1) || (major > 100))) { |
| 82 |
fprintf(stderr, "failed to parse OpenSSH client version, " |
| 83 |
"errno %d\n", errno); |
| 84 |
errno = 0; |
| 85 |
goto errversion; |
| 86 |
} |
| 87 |
|
| 88 |
if (major < 7) { |
| 89 |
fprintf(stderr, "error: minimum OpenSSH client version " |
| 90 |
"required is 7, found: %ld\n", major); |
| 91 |
goto errversion; |
| 92 |
} |
| 93 |
|
| 94 |
version_ok = 1; |
| 95 |
|
| 96 |
errversion: |
| 97 |
errfgets: |
| 98 |
rc = pclose(fp); |
| 99 |
if (rc != 0) { |
| 100 |
fprintf(stderr, "failed to get OpenSSH client version: %d\n", rc); |
| 101 |
} |
| 102 |
done: |
| 103 |
return version_ok; |
| 104 |
} |
| 105 |
|
| 106 |
int is_openssh_client_enabled(void) { |
| 107 |
return (bin_exists(OPENSSH_BINARY) && |
| 108 |
bin_exists(OPENSSH_KEYGEN) && |
| 109 |
is_openssh_client_new_enough()); |
| 110 |
} |
| 111 |
|
| 112 |
int is_dropbear_client_enabled(void) { |
| 113 |
return (bin_exists(DROPBEAR_BINARY) && bin_exists(DROPBEAR_KEYGEN)); |
| 114 |
} |
| 115 |
|
| 116 |
int is_putty_client_enabled(void) |
| 117 |
{ |
| 118 |
return (bin_exists(PUTTY_BINARY) && |
| 119 |
bin_exists(PUTTY_KEYGEN) && |
| 120 |
bin_exists(OPENSSH_KEYGEN)); |
| 121 |
} |