|
|
182b9e |
From 52ed50a1bd4b4bfe632bd560de27195fcea65802 Mon Sep 17 00:00:00 2001
|
|
|
182b9e |
From: Marek 'marx' Grac <mgrac@redhat.com>
|
|
|
182b9e |
Date: Mon, 17 Feb 2014 14:20:24 +0100
|
|
|
182b9e |
Subject: [PATCH] fence_ilo: Replacing nss_wrapper with gnutls-cli
|
|
|
182b9e |
|
|
|
182b9e |
SSLv2 was disabled in nss package (rhbz#1001841), ilo2 now supports also SSLv3
|
|
|
182b9e |
and it is possible to use standard tool for communication.
|
|
|
182b9e |
---
|
|
|
182b9e |
configure.ac | 2 +-
|
|
|
182b9e |
fence/agents/Makefile.am | 1 -
|
|
|
182b9e |
fence/agents/lib/fencing.py.py | 5 +-
|
|
|
182b9e |
fence/agents/nss_wrapper/Makefile.am | 7 -
|
|
|
182b9e |
fence/agents/nss_wrapper/fence_nss_wrapper.c | 484 --------------------------
|
|
|
182b9e |
make/fencebuild.mk | 1 +
|
|
|
182b9e |
6 files changed, 4 insertions(+), 496 deletions(-)
|
|
|
182b9e |
delete mode 100644 fence/agents/nss_wrapper/Makefile.am
|
|
|
182b9e |
delete mode 100644 fence/agents/nss_wrapper/fence_nss_wrapper.c
|
|
|
182b9e |
|
|
|
182b9e |
diff --git a/fence/agents/lib/fencing.py.py b/fence/agents/lib/fencing.py.py
|
|
|
182b9e |
index fd21c69..798f855 100644
|
|
|
182b9e |
--- a/fence/agents/lib/fencing.py.py
|
|
|
182b9e |
+++ b/fence/agents/lib/fencing.py.py
|
|
|
182b9e |
@@ -28,7 +28,7 @@ EC_INVALID_PRIVILEGES = 11
|
|
|
182b9e |
|
|
|
182b9e |
TELNET_PATH = "/usr/bin/telnet"
|
|
|
182b9e |
SSH_PATH = "/usr/bin/ssh"
|
|
|
182b9e |
-SSL_PATH = "@LIBEXECDIR@/fence_nss_wrapper"
|
|
|
182b9e |
+SSL_PATH = "@GNUTLSCLI_PATH@"
|
|
|
182b9e |
SUDO_PATH = "/usr/bin/sudo"
|
|
|
182b9e |
|
|
|
182b9e |
all_opt = {
|
|
|
182b9e |
@@ -960,11 +960,10 @@ def fence_login(options, re_login_string = "(login\s*: )|(Login Name: )|(userna
|
|
|
182b9e |
re_pass = re.compile("(password)|(pass phrase)", re.IGNORECASE)
|
|
|
182b9e |
|
|
|
182b9e |
if options.has_key("--ssl"):
|
|
|
182b9e |
- command = '%s %s %s %s' % (SSL_PATH, force_ipvx, options["--ip"], options["--ipport"])
|
|
|
182b9e |
+ command = '%s --insecure --crlf -p %s %s' % (SSL_PATH, options["--ipport"], options["--ip"])
|
|
|
182b9e |
try:
|
|
|
182b9e |
conn = fspawn(options, command)
|
|
|
182b9e |
except pexpect.ExceptionPexpect, ex:
|
|
|
182b9e |
- ## SSL telnet is part of the fencing package
|
|
|
182b9e |
sys.stderr.write(str(ex) + "\n")
|
|
|
182b9e |
syslog.syslog(syslog.LOG_ERR, str(ex))
|
|
|
182b9e |
sys.exit(EC_GENERIC_ERROR)
|
|
|
182b9e |
diff --git a/fence/agents/nss_wrapper/Makefile.am b/fence/agents/nss_wrapper/Makefile.am
|
|
|
182b9e |
deleted file mode 100644
|
|
|
182b9e |
index 16273ed..0000000
|
|
|
182b9e |
--- a/fence/agents/nss_wrapper/Makefile.am
|
|
|
182b9e |
+++ /dev/null
|
|
|
182b9e |
@@ -1,7 +0,0 @@
|
|
|
182b9e |
-MAINTAINERCLEANFILES = Makefile.in
|
|
|
182b9e |
-
|
|
|
182b9e |
-libexec_PROGRAMS = fence_nss_wrapper
|
|
|
182b9e |
-
|
|
|
182b9e |
-fence_nss_wrapper_CFLAGS = $(nss_CFLAGS) $(nspr_CFLAGS)
|
|
|
182b9e |
-
|
|
|
182b9e |
-fence_nss_wrapper_LDFLAGS = $(nss_LIBS) $(nspr_LIBS)
|
|
|
182b9e |
diff --git a/fence/agents/nss_wrapper/fence_nss_wrapper.c b/fence/agents/nss_wrapper/fence_nss_wrapper.c
|
|
|
182b9e |
deleted file mode 100644
|
|
|
182b9e |
index b960cf0..0000000
|
|
|
182b9e |
--- a/fence/agents/nss_wrapper/fence_nss_wrapper.c
|
|
|
182b9e |
+++ /dev/null
|
|
|
182b9e |
@@ -1,484 +0,0 @@
|
|
|
182b9e |
-/** @file fence_nss_wrapper.c - Main source code of hobbit like tool with
|
|
|
182b9e |
- support for NSS (SSL) connection.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-#include "clusterautoconfig.h"
|
|
|
182b9e |
-
|
|
|
182b9e |
-#include <stdio.h>
|
|
|
182b9e |
-#include <nss.h>
|
|
|
182b9e |
-#include <ssl.h>
|
|
|
182b9e |
-#include <prio.h>
|
|
|
182b9e |
-#include <prnetdb.h>
|
|
|
182b9e |
-#include <prerror.h>
|
|
|
182b9e |
-#include <prinit.h>
|
|
|
182b9e |
-#include <getopt.h>
|
|
|
182b9e |
-#include <libgen.h>
|
|
|
182b9e |
-
|
|
|
182b9e |
-/*---- CONSTANTS -------------*/
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Default operation = connect and telnet*/
|
|
|
182b9e |
-#define OPERATION_DEFAULT 0
|
|
|
182b9e |
-/** Operation display help*/
|
|
|
182b9e |
-#define OPERATION_HELP 1
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Default mode of connection. Try first found working address*/
|
|
|
182b9e |
-#define MODE_DEFAULT 3
|
|
|
182b9e |
-/** Use only IPv4*/
|
|
|
182b9e |
-#define MODE_IP4MODE 1
|
|
|
182b9e |
-/** Use only IPv6*/
|
|
|
182b9e |
-#define MODE_IP6MODE 2
|
|
|
182b9e |
-/** Use RAW mode - no change of \r and \n to \r\n*/
|
|
|
182b9e |
-#define MODE_RAW 4
|
|
|
182b9e |
-/** Use non-secure mode (without SSL, only pure socket)*/
|
|
|
182b9e |
-#define MODE_NO_SSL 8
|
|
|
182b9e |
-
|
|
|
182b9e |
-/*------ Functions ---------------*/
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Return port inserted in string. Fuction tests, if port is integer, and than return
|
|
|
182b9e |
- integer value of string. Otherwise, it will use /etc/services. On fail, it returns
|
|
|
182b9e |
- port -1.
|
|
|
182b9e |
- @param port_s Input port or service name
|
|
|
182b9e |
- @return port number (converted with ntohs) on success, otherwise -1.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static int return_port(char *port_s) {
|
|
|
182b9e |
- char *end_c;
|
|
|
182b9e |
- int res;
|
|
|
182b9e |
- struct servent *serv;
|
|
|
182b9e |
-
|
|
|
182b9e |
- res=strtol(port_s,&end_c,10);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (*end_c=='\0') return res;
|
|
|
182b9e |
-
|
|
|
182b9e |
- /*It's not number, so try service name*/
|
|
|
182b9e |
- serv=getservbyname(port_s,NULL);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (serv==NULL) return -1;
|
|
|
182b9e |
-
|
|
|
182b9e |
- return ntohs(serv->s_port);
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Hook handler for bad certificate (because we have no DB, EVERY certificate is bad).
|
|
|
182b9e |
- Returned value is always SECSuccess = it's ok certificate.
|
|
|
182b9e |
- @param arg NULL value
|
|
|
182b9e |
- @param fd socket cased error
|
|
|
182b9e |
- @return SECSuccess.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static SECStatus nss_bad_cert_hook(void *arg,PRFileDesc *fd) {
|
|
|
182b9e |
- return SECSuccess;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Display last NSPR/NSS error code and user readable message.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static void print_nspr_error(void) {
|
|
|
182b9e |
- fprintf(stderr,"Error (%d): %s\n",PR_GetError(),PR_ErrorToString(PR_GetError(),PR_LANGUAGE_I_DEFAULT));
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Initialize NSS. NSS is initialized without DB and with
|
|
|
182b9e |
- domnestic policy.
|
|
|
182b9e |
- @return 1 on success, otherwise 0.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static int init_nss(void) {
|
|
|
182b9e |
- if ((NSS_NoDB_Init(NULL)!=SECSuccess) ||
|
|
|
182b9e |
- (NSS_SetDomesticPolicy()!=SECSuccess)) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- SSL_ClearSessionCache();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Create socket. If ssl is >0, socket is ssl enabled.
|
|
|
182b9e |
- @param ssl Enable ssl (Client, SSL2+3, no TLS, compatible hello) if PR_TRUE, otherwise no.
|
|
|
182b9e |
- @param ipv6 New socket will be IPv4 if this value is 0, otherwise it will be ipv6
|
|
|
182b9e |
- @return NULL on error, otherwise socket.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static PRFileDesc *create_socket(int ssl,int ipv6) {
|
|
|
182b9e |
- PRFileDesc *res_socket;
|
|
|
182b9e |
-
|
|
|
182b9e |
- res_socket=PR_OpenTCPSocket((ipv6?PR_AF_INET6:PR_AF_INET));
|
|
|
182b9e |
- if (res_socket==NULL) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!ssl) return res_socket;
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!(res_socket=SSL_ImportFD(NULL,res_socket))) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if ((SSL_OptionSet(res_socket,SSL_SECURITY,ssl)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_HANDSHAKE_AS_SERVER,PR_FALSE)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_HANDSHAKE_AS_CLIENT,PR_TRUE)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_ENABLE_SSL2,ssl)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_ENABLE_SSL3,ssl)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_ENABLE_TLS,PR_FALSE)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_OptionSet(res_socket,SSL_V2_COMPATIBLE_HELLO,ssl)!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_SetPKCS11PinArg(res_socket,NULL)==-1) ||
|
|
|
182b9e |
- (SSL_AuthCertificateHook(res_socket,SSL_AuthCertificate,CERT_GetDefaultCertDB())!=SECSuccess) ||
|
|
|
182b9e |
- (SSL_BadCertHook(res_socket,nss_bad_cert_hook,NULL)!=SECSuccess)) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (PR_Close(res_socket)!=PR_SUCCESS) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- return NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- return res_socket;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Create socket and connect to it.
|
|
|
182b9e |
- @param hostname Hostname to connect
|
|
|
182b9e |
- @param port Port name/number to connect
|
|
|
182b9e |
- @param mode Connection mode. Bit-array of MODE_NO_SSL, MODE_IP6MODE, MODE_IP4MODE.
|
|
|
182b9e |
- @return NULL on error, otherwise connected socket.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static PRFileDesc *create_connected_socket(char *hostname,int port,int mode) {
|
|
|
182b9e |
- PRAddrInfo *addr_info;
|
|
|
182b9e |
- void *addr_iter;
|
|
|
182b9e |
- PRNetAddr addr;
|
|
|
182b9e |
- PRFileDesc *localsocket;
|
|
|
182b9e |
- int can_exit,valid_socket;
|
|
|
182b9e |
- PRUint16 af_spec;
|
|
|
182b9e |
-
|
|
|
182b9e |
- localsocket=NULL;
|
|
|
182b9e |
-
|
|
|
182b9e |
- addr_info=NULL;
|
|
|
182b9e |
-
|
|
|
182b9e |
- af_spec=PR_AF_UNSPEC;
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!(mode&MODE_IP6MODE)) af_spec=PR_AF_INET;
|
|
|
182b9e |
-
|
|
|
182b9e |
- addr_info=PR_GetAddrInfoByName(hostname,af_spec,PR_AI_ADDRCONFIG);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (addr_info == NULL) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
- return NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- /*We have socket -> enumerate and try to connect*/
|
|
|
182b9e |
- addr_iter=NULL;
|
|
|
182b9e |
- can_exit=0;
|
|
|
182b9e |
- valid_socket=0;
|
|
|
182b9e |
-
|
|
|
182b9e |
- while (!can_exit) {
|
|
|
182b9e |
- addr_iter=PR_EnumerateAddrInfo(addr_iter,addr_info,port,&addr);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (addr_iter==NULL) {
|
|
|
182b9e |
- can_exit=1;
|
|
|
182b9e |
- } else {
|
|
|
182b9e |
- if ((PR_NetAddrFamily(&addr)==PR_AF_INET && (mode&MODE_IP4MODE)) ||
|
|
|
182b9e |
- (PR_NetAddrFamily(&addr)==PR_AF_INET6 && (mode&MODE_IP6MODE))) {
|
|
|
182b9e |
- /*Type of address is what user want, try to create socket and make connection*/
|
|
|
182b9e |
-
|
|
|
182b9e |
- /*Create socket*/
|
|
|
182b9e |
- localsocket=create_socket(!(mode&MODE_NO_SSL),(PR_NetAddrFamily(&addr)==PR_AF_INET6));
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (localsocket) {
|
|
|
182b9e |
- /*Try to connect*/
|
|
|
182b9e |
- if (PR_Connect(localsocket,&addr,PR_INTERVAL_NO_TIMEOUT)==PR_SUCCESS) {
|
|
|
182b9e |
- /*Force handshake*/
|
|
|
182b9e |
- if ((!(mode&MODE_NO_SSL)) && SSL_ForceHandshake(localsocket)!=SECSuccess) {
|
|
|
182b9e |
- /*Handhake failure -> fail*/
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
- if (PR_Close(localsocket)!=PR_SUCCESS) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
- can_exit=1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- localsocket=NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- /*Socket is connected -> we can return it*/
|
|
|
182b9e |
- can_exit=1;
|
|
|
182b9e |
- } else {
|
|
|
182b9e |
- /*Try another address*/
|
|
|
182b9e |
- if (PR_Close(localsocket)!=PR_SUCCESS) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
- can_exit=1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- localsocket=NULL;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!localsocket) {
|
|
|
182b9e |
- /*Socket is unvalid -> we don't found any usable address*/
|
|
|
182b9e |
- fprintf(stderr,"Can't connect to host %s on port %d!\n",hostname,port);
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- PR_FreeAddrInfo(addr_info);
|
|
|
182b9e |
-
|
|
|
182b9e |
- return localsocket;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Parse arguments from command line.
|
|
|
182b9e |
- @param argc Number of arguments in argv
|
|
|
182b9e |
- @param argv Array of arguments
|
|
|
182b9e |
- @param mode Pointer to int will be filled with OPERATION_DEFAULT or OPERATION_HELP.
|
|
|
182b9e |
- @param mode Pointer to int will be filled with MODE_DEFAULT, MODE_IP4MODE or MODE_IP4MODE.
|
|
|
182b9e |
- @return 1 on success, otherwise 0.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static int parse_cli(int argc,char *argv[],int *operation,int *mode,char **hostname,char **port) {
|
|
|
182b9e |
- int opt;
|
|
|
182b9e |
-
|
|
|
182b9e |
- *operation=OPERATION_DEFAULT;
|
|
|
182b9e |
- *mode=MODE_DEFAULT;
|
|
|
182b9e |
- *port=NULL;
|
|
|
182b9e |
- *hostname=NULL;
|
|
|
182b9e |
-
|
|
|
182b9e |
- while ((opt=getopt(argc,argv,"h46rz"))!=-1) {
|
|
|
182b9e |
- switch (opt) {
|
|
|
182b9e |
- case 'h':
|
|
|
182b9e |
- *operation=OPERATION_HELP;
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- case '4':
|
|
|
182b9e |
- (*mode)&=~MODE_IP6MODE;
|
|
|
182b9e |
- (*mode)|=MODE_IP4MODE;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- case '6':
|
|
|
182b9e |
- (*mode)&=~MODE_IP4MODE;
|
|
|
182b9e |
- (*mode)|=MODE_IP6MODE;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- case 'r':
|
|
|
182b9e |
- (*mode)|=MODE_RAW;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- case 'z':
|
|
|
182b9e |
- (*mode)|=MODE_NO_SSL;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- default:
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (argc-optind<2) {
|
|
|
182b9e |
- fprintf(stderr,"Hostname and port is expected!\n");
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- *hostname=argv[optind];
|
|
|
182b9e |
- *port=argv[optind+1];
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Show usage of application.
|
|
|
182b9e |
- @param pname Name of program (usually basename of argv[0])
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static void show_usage(char *pname) {
|
|
|
182b9e |
- printf("usage: %s [options] hostname port\n", pname);
|
|
|
182b9e |
- printf(" -4 Force to use IPv4\n");
|
|
|
182b9e |
- printf(" -6 Force to use IPv6\n");
|
|
|
182b9e |
- printf(" -r Use RAW connection (don't convert \\r and \\n characters)\n");
|
|
|
182b9e |
- printf(" -z Don't use SSL connection (use pure socket)\n");
|
|
|
182b9e |
- printf(" -h Show this help\n");
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Convert End Of Lines (Unix \n, Macs \r or DOS/Win \r\n) to \r\n.
|
|
|
182b9e |
- @param in_buffer Input buffer
|
|
|
182b9e |
- @param in_size Input buffer size
|
|
|
182b9e |
- @param out_buffer Output buffer (must be prealocated). Should be (2*in_size) (in worst case)
|
|
|
182b9e |
- @param out_size There will be size of out_buffer
|
|
|
182b9e |
- @param in_state Internal state of finite automata. First call should have this 0, other calls
|
|
|
182b9e |
- shouldn't change this value. After end of file, you may add to this value +100 and call this
|
|
|
182b9e |
- again, to make sure of proper end (in_buffer can be in this case everything, including NULL).
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static void convert_eols(char *in_buffer,int in_size,char *out_buffer,int *out_size,int *in_state) {
|
|
|
182b9e |
- int in_pos,out_pos;
|
|
|
182b9e |
- int status;
|
|
|
182b9e |
- char in_char;
|
|
|
182b9e |
-
|
|
|
182b9e |
- out_pos=0;
|
|
|
182b9e |
- status=*in_state;
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (status==100 || status==101) {
|
|
|
182b9e |
- if (status==101) {
|
|
|
182b9e |
- out_buffer[out_pos++]='\r';
|
|
|
182b9e |
- out_buffer[out_pos++]='\n';
|
|
|
182b9e |
- }
|
|
|
182b9e |
- } else {
|
|
|
182b9e |
- for (in_pos=0;in_pos
|
|
|
182b9e |
- in_char=in_buffer[in_pos];
|
|
|
182b9e |
-
|
|
|
182b9e |
- switch (status) {
|
|
|
182b9e |
- case 0:
|
|
|
182b9e |
- if (in_char=='\r') status=1;
|
|
|
182b9e |
- if (in_char=='\n') {
|
|
|
182b9e |
- out_buffer[out_pos++]='\r';
|
|
|
182b9e |
- out_buffer[out_pos++]='\n';
|
|
|
182b9e |
- }
|
|
|
182b9e |
- if ((in_char!='\r') && (in_char!='\n')) out_buffer[out_pos++]=in_char;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
-
|
|
|
182b9e |
- case 1:
|
|
|
182b9e |
- out_buffer[out_pos++]='\r';
|
|
|
182b9e |
- out_buffer[out_pos++]='\n';
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (in_char!='\n') out_buffer[out_pos++]=in_char;
|
|
|
182b9e |
-
|
|
|
182b9e |
- status=0;
|
|
|
182b9e |
- break;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- *out_size=out_pos;
|
|
|
182b9e |
- *in_state=status;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Start polling cycle.
|
|
|
182b9e |
- @param socket Network connected socket.
|
|
|
182b9e |
- @param mode Bit-array of MODE_*. This function take care on MODE_RAW.
|
|
|
182b9e |
- @return 0 on failure, otherwise 1
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-static int poll_cycle(PRFileDesc *localsocket,int mode) {
|
|
|
182b9e |
- PRPollDesc pool[2];
|
|
|
182b9e |
- char buffer[1024],buffer_eol[1024*2];
|
|
|
182b9e |
- int readed_bytes;
|
|
|
182b9e |
- int can_exit;
|
|
|
182b9e |
- int res;
|
|
|
182b9e |
- int bytes_to_write;
|
|
|
182b9e |
- int eol_state;
|
|
|
182b9e |
-
|
|
|
182b9e |
- can_exit=0;
|
|
|
182b9e |
- eol_state=0;
|
|
|
182b9e |
-
|
|
|
182b9e |
- /* Fill pool*/
|
|
|
182b9e |
- pool[1].fd=localsocket;
|
|
|
182b9e |
- pool[0].fd=PR_STDIN;
|
|
|
182b9e |
- pool[0].in_flags=pool[1].in_flags=PR_POLL_READ;
|
|
|
182b9e |
- pool[0].out_flags=pool[1].out_flags=0;
|
|
|
182b9e |
-
|
|
|
182b9e |
- while (!can_exit) {
|
|
|
182b9e |
- res=(PR_Poll(pool,sizeof(pool)/sizeof(PRPollDesc),PR_INTERVAL_NO_TIMEOUT));
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (res==-1) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (pool[1].out_flags&PR_POLL_READ) {
|
|
|
182b9e |
- /*We have something in socket*/
|
|
|
182b9e |
- if ((readed_bytes=PR_Read(pool[1].fd,buffer,sizeof(buffer)))>0) {
|
|
|
182b9e |
- if (PR_Write(PR_STDOUT,buffer,readed_bytes)!=readed_bytes) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- } else {
|
|
|
182b9e |
- /*End of stream -> quit*/
|
|
|
182b9e |
- can_exit=1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (pool[0].out_flags&(PR_POLL_READ|PR_POLL_HUP)) {
|
|
|
182b9e |
- /*We have something in stdin*/
|
|
|
182b9e |
- if ((readed_bytes=PR_Read(pool[0].fd,buffer,sizeof(buffer)))>0) {
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!(mode&MODE_RAW)) {
|
|
|
182b9e |
- convert_eols(buffer,readed_bytes,buffer_eol,&bytes_to_write,&eol_state);
|
|
|
182b9e |
- } else
|
|
|
182b9e |
- bytes_to_write=readed_bytes;
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (PR_Write(pool[1].fd,(mode&MODE_RAW?buffer:buffer_eol),bytes_to_write)!=bytes_to_write) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- } else {
|
|
|
182b9e |
- /*End of stream -> send EOL (if needed)*/
|
|
|
182b9e |
- if (!(mode&MODE_RAW)) {
|
|
|
182b9e |
- eol_state+=100;
|
|
|
182b9e |
- convert_eols(NULL,0,buffer_eol,&bytes_to_write,&eol_state);
|
|
|
182b9e |
- if (PR_Write(pool[1].fd,buffer_eol,bytes_to_write)!=bytes_to_write) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- pool[0].out_flags=pool[1].out_flags=0;
|
|
|
182b9e |
- } /*while (!can_exit)*/
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-static void atexit_handler(void) {
|
|
|
182b9e |
- if (PR_Initialized())
|
|
|
182b9e |
- PR_Cleanup();
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (fclose(stdout)!=0) {
|
|
|
182b9e |
- fprintf(stderr,"Can't close stdout!\n");
|
|
|
182b9e |
-
|
|
|
182b9e |
- exit(1);
|
|
|
182b9e |
- }
|
|
|
182b9e |
-}
|
|
|
182b9e |
-
|
|
|
182b9e |
-/** Entry point of application.
|
|
|
182b9e |
- @param argc Number of arguments on command line
|
|
|
182b9e |
- @param argv Array of strings with arguments from command line
|
|
|
182b9e |
- @return 0 on success, otherwise >0.
|
|
|
182b9e |
-*/
|
|
|
182b9e |
-int main(int argc,char *argv[]) {
|
|
|
182b9e |
- int mode,operation;
|
|
|
182b9e |
- char *hostname, *port;
|
|
|
182b9e |
- char *pname;
|
|
|
182b9e |
- int port_n;
|
|
|
182b9e |
- PRFileDesc *fd_socket;
|
|
|
182b9e |
- int res;
|
|
|
182b9e |
-
|
|
|
182b9e |
- pname=basename(argv[0]);
|
|
|
182b9e |
-
|
|
|
182b9e |
- atexit(atexit_handler);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!parse_cli(argc,argv,&operation,&mode,&hostname,&port) || operation==OPERATION_HELP) {
|
|
|
182b9e |
- show_usage(pname);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (operation!=OPERATION_HELP) return 1;
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 0;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if ((port_n=return_port(port))==-1) {
|
|
|
182b9e |
- fprintf(stderr,"Error. Unknown port number/name %s!\n",port);
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!(mode&MODE_NO_SSL)) {
|
|
|
182b9e |
- if (!init_nss()) return 1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (!(fd_socket=create_connected_socket(hostname,port_n,mode)))
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
-
|
|
|
182b9e |
- res=poll_cycle(fd_socket,mode);
|
|
|
182b9e |
-
|
|
|
182b9e |
- if (PR_Close(fd_socket)!=PR_SUCCESS) {
|
|
|
182b9e |
- print_nspr_error();
|
|
|
182b9e |
-
|
|
|
182b9e |
- return 1;
|
|
|
182b9e |
- }
|
|
|
182b9e |
-
|
|
|
182b9e |
- return (res?0:1);
|
|
|
182b9e |
-}
|
|
|
182b9e |
--
|
|
|
182b9e |
1.7.7.6
|
|
|
182b9e |
|