diff options
author | Vladislav Grishenko <themiron@mail.ru> | 2016-06-29 13:29:33 +0500 |
---|---|---|
committer | Vladislav Grishenko <themiron@mail.ru> | 2018-02-20 14:09:10 +0500 |
commit | beeefb2bc617f1a871dd05c4cc00ff20bb5374c2 (patch) | |
tree | fab94be5a409f47b6357e8a683bc05e9161f7e23 /accel-cmd/accel_cmd.c | |
parent | 956cb489628621a2298937afeaed57cccf605bb2 (diff) | |
download | accel-ppp-beeefb2bc617f1a871dd05c4cc00ff20bb5374c2.tar.gz accel-ppp-beeefb2bc617f1a871dd05c4cc00ff20bb5374c2.zip |
accel-cmd: add -P/--password support
Diffstat (limited to 'accel-cmd/accel_cmd.c')
-rw-r--r-- | accel-cmd/accel_cmd.c | 63 |
1 files changed, 45 insertions, 18 deletions
diff --git a/accel-cmd/accel_cmd.c b/accel-cmd/accel_cmd.c index d73c9c24..e0ca7d41 100644 --- a/accel-cmd/accel_cmd.c +++ b/accel-cmd/accel_cmd.c @@ -21,6 +21,7 @@ struct host_params { const char *str_addr; const char *str_port; + char *passwd; sa_family_t family; int fd; }; @@ -183,7 +184,7 @@ static int accel_talk(int cmdfd, int accelfd, const struct msghdr *mhdr, fd_set rfds; fd_set wfds; ssize_t res; - char last_char; + char last_char = '\0'; int cmdflg = -1; int accelflg = -1; size_t bytes_rd = 0; @@ -360,11 +361,16 @@ out: return err; } -static struct msghdr *argv_to_msghdr(int argc, char * const *argv) +static struct msghdr *argv_to_msghdr(int argc, char * const *argv, const char *passwd) { struct msghdr *mh = NULL; struct iovec *iv = NULL; - int indx; + int indx, ividx = 0, ivlen = 0; + + if (passwd && *passwd) + ivlen += 2; + if (argc) + ivlen += argc * 2 - 1; mh = calloc(1, sizeof(struct msghdr)); if (mh == NULL) { @@ -373,7 +379,7 @@ static struct msghdr *argv_to_msghdr(int argc, char * const *argv) __func__, __LINE__, strerror(errno)); return NULL; } - iv = calloc(argc * 2 - 1, sizeof(struct iovec)); + iv = calloc(ivlen, sizeof(struct iovec)); if (iv == NULL) { fprintf(stderr, "%s,%i: Impossible to allocate buffer:" " calloc() failed: %s\n", @@ -382,17 +388,23 @@ static struct msghdr *argv_to_msghdr(int argc, char * const *argv) return NULL; } - for (indx = 0; indx < argc - 1; ++indx) { - iv[indx * 2].iov_base = argv[indx]; - iv[indx * 2].iov_len = strlen(argv[indx]); - iv[indx * 2 + 1].iov_base = " "; - iv[indx * 2 + 1].iov_len = 1; + if (passwd && *passwd) { + iv[ividx].iov_base = (void *) passwd; + iv[ividx++].iov_len = strlen(passwd); + iv[ividx].iov_base = "\n"; + iv[ividx++].iov_len = 1; + } + for (indx = 0; indx < argc; ++indx) { + if (indx) { + iv[ividx].iov_base = " "; + iv[ividx++].iov_len = 1; + } + iv[ividx].iov_base = argv[indx]; + iv[ividx++].iov_len = strlen(argv[indx]); } - iv[indx * 2].iov_base = argv[indx]; - iv[indx * 2].iov_len = strlen(argv[indx]); mh->msg_iov = iv; - mh->msg_iovlen = argc * 2 - 1; + mh->msg_iovlen = ivlen; return mh; } @@ -553,6 +565,7 @@ static void print_help(const char *name) printf("\t-p, --port\t- Set remote port to use for communicating" " with HOST. Defaults to \"%s\".\n", DEFAULT_PORT); printf("\t-t, --timeout\t- Set inactivity timeout.\n"); + printf("\t-P, --password\t- Set password for accessing HOST.\n"); printf("\t-v, --verbose\t- Verbose output.\n"); printf("\t-V, --version\t- Display version number and exit.\n"); printf("\t-h, --help\t- Display this help message and exit.\n"); @@ -591,6 +604,11 @@ int main(int argc, char **argv) .flag = NULL, .val = 't' }, + {.name = "password", + .has_arg = required_argument, + .flag = NULL, + .val = 'P' + }, {.name = "verbose", .has_arg = no_argument, .flag = NULL, @@ -630,7 +648,7 @@ int main(int argc, char **argv) char ochar; int rv; - while ((ochar = getopt_long(argc, argv, "f:46ni:H:p:t:vVh-", + while ((ochar = getopt_long(argc, argv, "f:46ni:H:p:t:P:vVh-", long_opts, &oindx)) != -1) { if (ochar == '-') /* End of options, interpret the following arguments @@ -674,6 +692,12 @@ int main(int argc, char **argv) return XSTATUS_BADPARAM; } break; + case 'P': + if (peer.passwd) + free(peer.passwd); + peer.passwd = strdup(optarg); + memset(optarg, '*', strlen(optarg)); + break; case 'v': verbose = true; break; @@ -689,14 +713,16 @@ int main(int argc, char **argv) }; } - if (optind < argc) { - mh = argv_to_msghdr(argc - optind, argv + optind); + if (optind < argc || peer.passwd) { + mh = argv_to_msghdr(argc - optind, argv + optind, peer.passwd); if (mh == NULL) { rv = XSTATUS_INTERNAL; goto out; } + } + if (optind < argc) inputstream = -1; - } else + else inputstream = STDIN_FILENO; rv = accel_connect(&peer, numeric); @@ -711,9 +737,10 @@ int main(int argc, char **argv) rv = accel_talk(inputstream, peer.fd, mh, timeo); out: - if (peer.fd >= 0) { + if (peer.fd >= 0) close(peer.fd); - } + if (peer.passwd) + free(peer.passwd); if (mh) msghdr_free(mh); |