Skip to content

Commit

Permalink
TerminalReadString and TerminalReadPrompt now return NULL only if esc…
Browse files Browse the repository at this point in the history
…ape is pressed, and an empty string for an empty string. Added OpenSSLAutoDetect. Initial work on selecting multiple items in menus.
  • Loading branch information
Root User committed Apr 28, 2020
1 parent a3906d1 commit 0644f63
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 21 deletions.
175 changes: 175 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
CC = gcc
VERSION = 4.27
CFLAGS = -g -O2
LDFLAGS=
LIBS = -lssl -lcrypto -lc -lc -lc -lc -lc -lc -lc -lc
FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -fPIC -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -D_FILE_OFFSET_BITS=64 -DHAVE_LIBC=1 -DHAVE_PTSNAME_R=1 -DHAVE_LIBC=1 -DHAVE_CLEARENV=1 -DHAVE_LIBC=1 -DHAVE_SETRESUID=1 -DHAVE_LIBC=1 -DHAVE_GET_CURR_DIR=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT2=1 -DHAVE_LIBC=1 -DHAVE_UMOUNT=1 -DHAVE_LIBC=1 -DHAVE_MKOSTEMP=1 -DHAVE_LIBC=1 -DHAVE_POLL=1 -DHAVE_LIBCRYPTO=1 -DHAVE_LIBSSL=1 -DHAVE_EVP_BF_CBC=1 -DHAVE_EVP_RC2_CBC=1 -DHAVE_EVP_RC4=1 -DHAVE_EVP_DES_CBC=1 -DHAVE_EVP_DESX_CBC=1 -DHAVE_EVP_CAST5_CBC=1 -DHAVE_EVP_IDEA_CBC=1 -DHAVE_EVP_AES_128_CBC=1 -DHAVE_EVP_AES_256_CBC=1 -DHAVE_X509_CHECK_HOST=1 -DHAVE_DECL_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_OPENSSL_ADD_ALL_ALGORITHMS=1 -DHAVE_DECL_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_SSL_SET_TLSEXT_HOST_NAME=1 -DHAVE_MADVISE -DHAVE_MADVISE_NOFORK -DHAVE_MADVISE_DONTDUMP -DHAVE_MLOCK -DVERSION=\"$(VERSION)\"
prefix=/usr/local
OBJ=String.o Array.o List.o Socket.o UnixSocket.o Stream.o Errors.o Unicode.o Terminal.o FileSystem.o GeneralFunctions.o DataProcessing.o Pty.o Log.o Http.o Smtp.o inet.o Expect.o base64.o crc32.o md5c.o sha1.o sha2.o whirlpool.o jh_ref.o Hash.o Ssh.o Compression.o OAuth.o LibSettings.o Vars.o Time.o Markup.o SpawnPrograms.o Tokenizer.o PatternMatch.o URL.o DataParser.o ConnectionChain.o OpenSSL.o Process.o Encodings.o RawData.o SecureMem.o CommandLineParser.o SysInfo.o


all: $(OBJ)
$(CC) $(FLAGS) -shared -o libUseful-$(VERSION).so $(OBJ) $(LIBS) $(LDFLAGS)
ar rcs libUseful-$(VERSION).a $(OBJ)
-ln -s libUseful-$(VERSION).so libUseful-4.so &>/dev/null
-ln -s libUseful-$(VERSION).a libUseful-4.a &>/dev/null
-ln -s libUseful-$(VERSION).so libUseful.so &>/dev/null
-ln -s libUseful-$(VERSION).a libUseful.a &>/dev/null


String.o: String.h String.c
$(CC) $(FLAGS) -c String.c

Array.o: Array.h Array.c
$(CC) $(FLAGS) -c Array.c

List.o: List.h List.c
$(CC) $(FLAGS) -c List.c

Socket.o: Socket.h Socket.c
$(CC) $(FLAGS) -c Socket.c

Pty.o: Pty.h Pty.c
$(CC) $(FLAGS) -c Pty.c

Http.o: Http.h Http.c
$(CC) $(FLAGS) -c Http.c

Smtp.o: Smtp.h Smtp.c
$(CC) $(FLAGS) -c Smtp.c

Stream.o: Stream.h Stream.c
$(CC) $(FLAGS) -c Stream.c

Unicode.o: Unicode.c Unicode.h
$(CC) $(FLAGS) -c Unicode.c

Terminal.o: Terminal.h Terminal.c
$(CC) $(FLAGS) -c Terminal.c

Errors.o: Errors.h Errors.c
$(CC) $(FLAGS) -c Errors.c


Log.o: Log.h Log.c
$(CC) $(FLAGS) -c Log.c

UnixSocket.o: UnixSocket.h UnixSocket.c
$(CC) $(FLAGS) -c UnixSocket.c

PatternMatch.o: PatternMatch.h PatternMatch.c
$(CC) $(FLAGS) -c PatternMatch.c

FileSystem.o: FileSystem.h FileSystem.c
$(CC) $(FLAGS) -c FileSystem.c

Time.o: Time.h Time.c
$(CC) $(FLAGS) -c Time.c

Tokenizer.o: Tokenizer.h Tokenizer.c
$(CC) $(FLAGS) -c Tokenizer.c

Markup.o: Markup.h Markup.c
$(CC) $(FLAGS) -c Markup.c

URL.o: URL.h URL.c
$(CC) $(FLAGS) -c URL.c

DataParser.o: DataParser.h DataParser.c
$(CC) $(FLAGS) -c DataParser.c

inet.o: inet.h inet.c
$(CC) $(FLAGS) -c inet.c

Expect.o: Expect.h Expect.c
$(CC) $(FLAGS) -c Expect.c

SecureMem.o: SecureMem.h SecureMem.c
$(CC) $(FLAGS) -c SecureMem.c

GeneralFunctions.o: GeneralFunctions.h GeneralFunctions.c
$(CC) $(FLAGS) -c GeneralFunctions.c

DataProcessing.o: DataProcessing.h DataProcessing.c
$(CC) $(FLAGS) -c DataProcessing.c

Hash.o: Hash.h Hash.c
$(CC) $(FLAGS) -c Hash.c

Ssh.o: Ssh.h Ssh.c
$(CC) $(FLAGS) -c Ssh.c

Compression.o: Compression.h Compression.c
$(CC) $(FLAGS) -c Compression.c

ConnectionChain.o: ConnectionChain.h ConnectionChain.c
$(CC) $(FLAGS) -c ConnectionChain.c

base64.o: base64.c base64.h
$(CC) $(FLAGS) -c base64.c

crc32.o: crc32.c crc32.h
$(CC) $(FLAGS) -c crc32.c

md5c.o: md5c.c md5-global.h md5.h
$(CC) $(FLAGS) -c md5c.c

sha1.o: sha1.c sha1.h
$(CC) $(FLAGS) -c sha1.c

sha2.o: sha2.c sha2.h
$(CC) $(FLAGS) -DSHA2_UNROLL_TRANSFORM -c sha2.c

whirlpool.o: whirlpool.c whirlpool.h
$(CC) $(FLAGS) -c whirlpool.c

jh_ref.o: jh_ref.c jh_ref.h
$(CC) $(FLAGS) -c jh_ref.c

OAuth.o: OAuth.c OAuth.h
$(CC) $(FLAGS) -c OAuth.c

OpenSSL.o: OpenSSL.c OpenSSL.h
$(CC) $(FLAGS) -c OpenSSL.c

Process.o: Process.c Process.h
$(CC) $(FLAGS) -c Process.c

Vars.o: Vars.c Vars.h
$(CC) $(FLAGS) -c Vars.c

SpawnPrograms.o: SpawnPrograms.c SpawnPrograms.h
$(CC) $(FLAGS) -c SpawnPrograms.c

Encodings.o: Encodings.c Encodings.h
$(CC) $(FLAGS) -c Encodings.c

RawData.o: RawData.c RawData.h
$(CC) $(FLAGS) -c RawData.c

CommandLineParser.o: CommandLineParser.c CommandLineParser.h
$(CC) $(FLAGS) -c CommandLineParser.c

SysInfo.o: SysInfo.c SysInfo.h
$(CC) $(FLAGS) -c SysInfo.c


#No dependancies, must always be compiled
LibSettings.o: LibSettings.h LibSettings.c
$(CC) $(FLAGS) -c LibSettings.c

clean:
-rm -f *.o *.so *.a
-rm config.log config.status
-rm -r autom4te.cache config.cache

install: libUseful.so
-mkdir -p $(DESTDIR)$(prefix)/lib
cp -P *.so *.a $(DESTDIR)$(prefix)/lib
-mkdir -p $(DESTDIR)$(prefix)/include/libUseful-$(VERSION)
cp *.h $(DESTDIR)$(prefix)/include/libUseful-$(VERSION)
-mkdir -p $(DESTDIR)$(prefix)/include/libUseful-4
cp *.h $(DESTDIR)$(prefix)/include/libUseful-4

test: libUseful.so
-echo "No tests written yet"
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CC = @CC@
VERSION = 4.26
VERSION = 4.27
CFLAGS = @CFLAGS@ @SIMD_CFLAGS@
LDFLAGS=@LDFLAGS@
LIBS = @LIBS@
Expand Down
34 changes: 33 additions & 1 deletion OpenSSL.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ int DoSSLServerNegotiation(STREAM *S, int Flags)



int STREAMIsPeerAuth(STREAM *S)
int OpenSSLIsPeerAuth(STREAM *S)
{
#ifdef HAVE_LIBSSL
void *ptr;
Expand Down Expand Up @@ -690,3 +690,35 @@ ptr=STREAMGetItem(S,"LIBUSEFUL-SSL:CTX");
if (ptr) SSL_CTX_free((SSL_CTX *) ptr);
#endif
}



int OpenSSLAutoDetect(STREAM *S)
{
int result, val, RetVal=FALSE;
char *Tempstr=NULL;

val=S->Timeout;
STREAMSetTimeout(S, 1);

result=STREAMCountWaitingBytes(S);
if (result > 1)
{
Tempstr=SetStrLen(Tempstr,255);
result=recv(S->in_fd, Tempstr, 2, MSG_PEEK);
if (result >1)
{
if (memcmp(Tempstr, "\x16\x03",2)==0)
{
//it's SSL/TLS
DoSSLServerNegotiation(S, 0);
RetVal=TRUE;
}
}
}
STREAMSetTimeout(S, val);

Destroy(Tempstr);

return(RetVal);
}
7 changes: 6 additions & 1 deletion OpenSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void OpenSSLReseedRandom();

//is peer authenticated. Clients can use certificate authentication and this function checks if they
//did and if the certificate passed checks
int STREAMIsPeerAuth(STREAM *S);
int OpenSSLIsPeerAuth(STREAM *S);

void OpenSSLGenerateDHParams();

Expand All @@ -94,8 +94,13 @@ int DoSSLClientNegotiation(STREAM *S, int Flags);
//'Flags' can be any of the LU_SSL_ flags listed above
int DoSSLServerNegotiation(STREAM *S, int Flags);

//This is called automatically by STREAMClose. You won't generally explicitly call this.
void OpenSSLClose(STREAM *S);

//call this before doing anything else with a STREAM that's been 'accept'-ed from a server socket. If the stream is encrypted
//with SSL/TLS this will return TRUE, FALSE otherwise
int OpenSSLAutoDetect(STREAM *S);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 2 additions & 13 deletions Socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,19 +793,8 @@ STREAM *STREAMServerAccept(STREAM *Serv)
S=STREAMFromSock(fd, type, Tempstr, DestIP, DestPort);
if (type==STREAM_TYPE_TCP_ACCEPT)
{
//if TLS autodetection enabled, perform it now
if (Serv->Flags & SF_TLS_AUTO)
{
Tempstr=SetStrLen(Tempstr,255);
result=recv(S->in_fd, Tempstr, 4, MSG_PEEK);
if (result >3)
{
if (memcmp(Tempstr, "\x16\x03\x01",3)==0)
{
//it's SSL/TLS
}
}
}
//if TLS autodetection enabled, perform it now
if (Serv->Flags & SF_TLS_AUTO) OpenSSLAutoDetect(S);
}

DestroyString(Tempstr);
Expand Down
26 changes: 23 additions & 3 deletions Terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1817,8 +1817,13 @@ char *TerminalReadText(char *RetStr, int Flags, STREAM *S)

if ( (inchar == '\n') || (inchar == '\r') )
{
//backspace over previous character and replace with star
//backspace over previous character and replace with star, so the
//last character is not left chnaging when we press 'enter'
if (Flags & TERM_SHOWTEXTSTARS) STREAMWriteString("\x08*",S);

//ensure we don't return NULL, but that we instead return an empty string
RetStr=CatStr(RetStr, "");

break;
}

Expand Down Expand Up @@ -2123,9 +2128,9 @@ void TerminalBarMenuUpdate(TERMBAR *TB, ListNode *Items)
{
if (Items->Side==Curr)
{
Tempstr=MCatStr(Tempstr, TB->MenuCursorLeft, Curr->Tag, TB->MenuCursorRight,NULL);
Tempstr=MCatStr(Tempstr, TB->MenuCursorLeft, Curr->Tag, TB->MenuCursorRight, NULL);
}
else Tempstr=MCatStr(Tempstr, TB->MenuPadLeft,Curr->Tag,TB->MenuPadRight,NULL);
else Tempstr=MCatStr(Tempstr, TB->MenuPadLeft,Curr->Tag, TB->MenuPadRight, NULL);

Curr=ListGetNext(Curr);
}
Expand Down Expand Up @@ -2263,6 +2268,11 @@ void TerminalMenuDraw(TERMMENU *Menu)
p_Attribs=Menu->MenuCursorLeft;
p_Cursor="> ";
}
else if (Curr->Flags & TERMMENU_SELECTED)
{
p_Attribs=Menu->MenuAttribs;
p_Cursor=" X";
}
else
{
p_Attribs=Menu->MenuAttribs;
Expand Down Expand Up @@ -2353,6 +2363,15 @@ int i;
}
break;

case ' ':
if (Menu->Options->Side)
{
//if Flags for the list head item has 'TERMMENU_SELECTED' set, then toggle that value for the
//list item
if (Menu->Options->Flags & TERMMENU_SELECTED) Menu->Options->Side->Flags ^= TERMMENU_SELECTED;
}
break;

case '\r':
case '\n':
return(Menu->Options->Side);
Expand Down Expand Up @@ -2397,6 +2416,7 @@ ListNode *Node;
int key;

Menu=TerminalMenuCreate(Term, x, y, wid, high);
Menu->Options->Flags = Options->Flags;
Node=ListGetNext(Options);
while (Node)
{
Expand Down
9 changes: 7 additions & 2 deletions Terminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ char *MenuCursorLeft;
char *MenuCursorRight;
} TERMMENU;

#define TERMMENU_SELECTED LIST_FLAG_USER1

TERMMENU *TerminalMenuCreate(STREAM *Term, int x, int y, int wid, int high);
void TerminalMenuDestroy(TERMMENU *Item);
Expand All @@ -286,10 +287,14 @@ ListNode *TerminalMenuOnKey(TERMMENU *Menu, int key);
ListNode *TerminalMenuProcess(TERMMENU *Menu);

//create a menu from a list of options, and run it.
// This keeps reading keypresses until an option is
//selected. Returns the selected option, or returns NULL if escape is pressed
//This keeps reading keypresses until an option is selected.
//Returns the selected option, or returns NULL if escape is pressed.
//if TERMMENU_SELECTED is set on the head of Options, then the menu will allow setting
//'selected' against multiple values
ListNode *TerminalMenu(STREAM *Term, ListNode *Options, int x, int y, int wid, int high);



#ifdef __cplusplus
}
#endif
Expand Down
1 change: 1 addition & 0 deletions Time.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ time_t DateStrToSecs(const char *DateFormat, const char *Str, const char *TimeZo
return(Secs);
}


long TimezoneOffset(const char *TimeZone)
{
long Secs=0;
Expand Down

0 comments on commit 0644f63

Please sign in to comment.