Skip to content

Commit

Permalink
v3.0, sudo support, qrcode export
Browse files Browse the repository at this point in the history
  • Loading branch information
ColumPaget committed Jan 20, 2024
1 parent 82348fa commit a8c70e3
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 171 deletions.
48 changes: 48 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
v3.0 (2024-01-20)
* command-line qrcode export support
* sudo support

v2.1 (2023-03-04)
=================
* support libUseful-5
* Create LICENSE

v2.0 (2022-10-21)
=================
* display options for known and open nets
* channel numbers can be 3 digits!
* add 'change interface' screen
* added 'list interfaces' action

v1.5 (2020-06-18)
=================
* fix for stuck process left when terminal closed

v1.4 (2020-06-06)
=================
* country-code/regulatory option added
* add country code for wpa_supplicant
* Better command-line parsing. Handle devices that require using other wpa_supplicant drivers than nl80211

v1.3 (2020-04-30)
=================
* Fix for 'last character missing' issue with user-inputed strings, like network passphrases

v1.2 (2020-04-28)
=================
* You can now back out of entering network details and cancel joining a network

v1.1 (2020-03-24)
=================
* don't mess up config file is items like 'username' aren't set
* changes for networks using username and password

v1.0 (2019-12-15)
=================
* use iw if iwconfig not available
* handle open wifi networks
* fix return value to allow compile under clang
* Don't launch DHCPCD until wifi is associated
* better behaved bottom bar
* code tidyup, added README.md, added .travis.yml
* initial commit
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
VERSION=1.6
VERSION=3.0
CC=gcc
LIBS= -lcrypto -lssl -lUseful-5
FLAGS=-g -O2 -DVERSION=\"$(VERSION)\"

OBJ=common.o net.o netdev.o runcommand.o iw.o wireless_tools.o wpa_supplicant.o wifi.o interactive.o settings.o help.o
OBJ=common.o net.o netdev.o runcommand.o iw.o wireless_tools.o wpa_supplicant.o wifi.o interactive.o settings.o qrcode.o command_line.o help.o

all: $(OBJ)
$(CC) $(FLAGS) -oterm_wifi $(OBJ) main.c $(LIBS)
Expand All @@ -23,6 +23,9 @@ net.o: net.h net.c common.h
help.o: help.h help.c common.h
$(CC) $(FLAGS) -c help.c

qrcode.o: qrcode.h qrcode.c common.h
$(CC) $(FLAGS) -c qrcode.c

netdev.o: netdev.h netdev.c common.h
$(CC) $(FLAGS) -c netdev.c

Expand All @@ -44,9 +47,12 @@ runcommand.o: runcommand.h runcommand.c common.h
settings.o: settings.h settings.c common.h
$(CC) $(FLAGS) -c settings.c

command_line.o: command_line.h command_line.c common.h
$(CC) $(FLAGS) -c command_line.c


clean:
rm term_wifi *.o libUseful-4/*.o libUseful-4/*.a libUseful-4/*.so
rm term_wifi *.o libUseful-4/*.o libUseful-4/*.a libUseful-4/*.so *.orig

test:
echo "no tests"
12 changes: 9 additions & 3 deletions Makefile.in
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
VERSION=1.6
VERSION=3.0
CC=@CC@
LIBS=@LDFLAGS@ @LIBS@ @LIBUSEFUL_BUNDLED@
FLAGS=@CFLAGS@ -DVERSION=\"$(VERSION)\"

OBJ=common.o net.o netdev.o runcommand.o iw.o wireless_tools.o wpa_supplicant.o wifi.o interactive.o settings.o help.o
OBJ=common.o net.o netdev.o runcommand.o iw.o wireless_tools.o wpa_supplicant.o wifi.o interactive.o settings.o qrcode.o command_line.o help.o

all: $(OBJ) @LIBUSEFUL_BUNDLED@
$(CC) $(FLAGS) -oterm_wifi $(OBJ) main.c $(LIBS)
Expand All @@ -23,6 +23,9 @@ net.o: net.h net.c common.h
help.o: help.h help.c common.h
$(CC) $(FLAGS) -c help.c

qrcode.o: qrcode.h qrcode.c common.h
$(CC) $(FLAGS) -c qrcode.c

netdev.o: netdev.h netdev.c common.h
$(CC) $(FLAGS) -c netdev.c

Expand All @@ -44,9 +47,12 @@ runcommand.o: runcommand.h runcommand.c common.h
settings.o: settings.h settings.c common.h
$(CC) $(FLAGS) -c settings.c

command_line.o: command_line.h command_line.c common.h
$(CC) $(FLAGS) -c command_line.c


clean:
rm term_wifi *.o libUseful-4/*.o libUseful-4/*.a libUseful-4/*.so
rm term_wifi *.o libUseful-4/*.o libUseful-4/*.a libUseful-4/*.so *.orig

test:
echo "no tests"
53 changes: 48 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,26 @@ make

This should produce an executable called 'term_wifi' that you can copy to a bin directory.


# REQUIRED 'HELPER' PROGRAMS

term_wifi requires iwconfig or iw to be installed for wifi scanning/setup, wpa_supplicant for WPA networks, dhcpcd for dhcp support and ifconfig and route for network setup.

if term_wifi is not run as root, then it will try to use sudo, or if that's not installed, su, to run network commands with root permissions.

qr-code export requires qrencode utility to be installed, and also an image viewer. term_wifi searches for a default list of image viewers, but this can be overridden with the `-viewer` option.



# INTERACTIVE (USER INTERFACE) MODE

if term_wifi is run without any arguments the user will be presented with a simple terminal user interface. You can use the '-i' option to show the tui for a given wifi device like so:

```
term_wifi -i wlan1
```


# USAGE: Command line mode

To view available wifi networks:
Expand Down Expand Up @@ -71,16 +91,39 @@ To 'forget' (delete) a configured network
```


# INTERACTIVE (USER INTERFACE) MODE

if term_wifi is run without any arguments the user will be presented with a simple terminal user interface. You can use the '-i' option to show the tui for a given wifi device like so:
# COMMAND LINE REFERENCE

```
term_wifi -i wlan1
term_wifi interfaces list interfaces
term_wifi scan <interface> scan networks and output details
term_wifi add <essid> <address> <netmask> <gateway> <dns server> add a config for a network
term_wifi add <essid> dhcp add a config for a network using dhcp
term_wifi forget <essid> delete (forget) network
term_wifi list list configured networks
term_wifi join <interface> <essid> join a configured network
term_wifi leave <interface> leave current network
term_wifi connect <essid> join configured network with default interface
term_wifi qrcode <essid> display qr code for saved network with essid '<essid>'
term_wifi qrcode <essid> -viewer <list> display qr code for saved network with essid '<essid>' using first viewer program found in comma-separated list '<list>'
term_wifi qrcode <essid> -o <path> write qr code for network '<essid>' to PNG file at <path>
term_wifi -? this help
term_wifi -h this help
term_wifi -help this help
term_wifi --help this help
options that apply to connect/interactive mode
-i <interface> interface to use
-ap <access point mac address> access point to join (if many for same essid)
-k <key> authentication key for given essid/network)
```

you can use `-i <interface>` to specify and interface to use for the 'connect', 'scan' and tui commands.


# OPTIONS
QR CODES
========

the command-line 'qrcode' action will produce a qrcode PNG and attempt to find an image-viewer to display it. You can specify an image-viewer command using the '-viewer' option. Failing that, term_wifi has an internal list of image-viewer programs to try. After all the image viewers, there are two options that output sixel images to the terminal, provided your terminal supports sixel image display. Finally, if nothing matching is found, the ultimate fallback is to use qrencodes 'ANSI256' display message to create a giant QR code with ANSI graphics.

you can use `-i <interface>` to specify and interface to use for the 'connect', 'scan' and tui commands.

104 changes: 104 additions & 0 deletions command_line.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "command_line.h"
#include "settings.h"

int ParseCommandLine(int argc, char *argv[], TNet *Conf)
{
CMDLINE *CL;
int Act=ACT_INTERACTIVE;
const char *ptr;

CL=CommandLineParserCreate(argc, argv);

ptr=CommandLineFirst(CL);
if (StrValid(ptr))
{
if (strcmp(ptr, "list")==0) Act=ACT_LIST;
else if (strcmp(ptr, "interfaces")==0) Act=ACT_IFACE_LIST;
else if (strcmp(ptr, "scan")==0)
{
ptr=CommandLineNext(CL);
if (ListFindNamedItem(Interfaces, ptr))
{
Conf->Interface=CopyStr(Conf->Interface, ptr);
}
Act=ACT_SCAN;
}
else if (strcmp(ptr, "add")==0)
{
Act=ACT_ADD;
Conf->Flags |= NET_STORE;
Conf->ESSID=CopyStr(Conf->ESSID, CommandLineNext(CL));
Conf->Address=CopyStr(Conf->Address, CommandLineNext(CL));
if (strcasecmp(Conf->Address, "dhcp") !=0)
{
Conf->Netmask=CopyStr(Conf->Netmask, CommandLineNext(CL));
Conf->Gateway=CopyStr(Conf->Gateway, CommandLineNext(CL));
Conf->DNSServer=CopyStr(Conf->DNSServer, CommandLineNext(CL));
}
}
else if (strcmp(ptr, "join")==0)
{
Act=ACT_JOIN;
Conf->Interface=CopyStr(Conf->Interface, CommandLineNext(CL));
if (! ListFindNamedItem(Interfaces, Conf->Interface))
{
printf("ERROR: '%s' is not an interface\n", Conf->Interface);
printf("usage: %s join <interface> <essid>\n", argv[0]);
exit(1);
}

Conf->ESSID=CopyStr(Conf->ESSID, CommandLineNext(CL));
}
else if (strcmp(ptr, "connect")==0)
{
Act=ACT_JOIN;
Conf->ESSID=CopyStr(Conf->ESSID, CommandLineNext(CL));
Conf->Interface=CopyStr(Conf->Interface, CommandLineNext(CL));
}
else if (strcmp(ptr, "leave")==0)
{
Act=ACT_LEAVE;
Conf->Interface=CopyStr(Conf->Interface, CommandLineNext(CL));
if (! ListFindNamedItem(Interfaces, Conf->Interface))
{
printf("ERROR: '%s' is not an interface\n", Conf->Interface);
printf("usage: %s join <interface> <essid>\n", argv[0]);
exit(1);
}
}
else if (strcmp(ptr, "forget")==0)
{
Act=ACT_FORGET;
Conf->ESSID=CopyStr(Conf->ESSID, CommandLineNext(CL));
}
else if (strcmp(ptr, "qrcode")==0)
{
Act=ACT_QRCODE;
Conf->ESSID=CopyStr(Conf->ESSID, CommandLineNext(CL));
}
else if (strcmp(ptr, "help")==0) Act=ACT_HELP;
}



ptr=CommandLineCurr(CL);
while (ptr)
{
if (strcmp(ptr, "-?")==0) Act=ACT_HELP;
else if (strcmp(ptr, "-h")==0) Act=ACT_HELP;
else if (strcmp(ptr, "-help")==0) Act=ACT_HELP;
else if (strcmp(ptr, "--help")==0) Act=ACT_HELP;
else if (strcmp(ptr, "-i")==0) Conf->Interface=CopyStr(Conf->Interface, CommandLineNext(CL));
else if (strcmp(ptr, "-ap")==0) Conf->AccessPoint=CopyStr(Conf->AccessPoint, CommandLineNext(CL));
else if (strcmp(ptr, "-k")==0) Conf->Key=CopyStr(Conf->Key, CommandLineNext(CL));
else if (strcmp(ptr, "-w")==0) Settings.WPASupplicantSock=CopyStr(Settings.WPASupplicantSock, CommandLineNext(CL));
else if (strcmp(ptr, "-o")==0) Settings.OutputPath=CopyStr(Settings.OutputPath, CommandLineNext(CL));
else if (strcmp(ptr, "-viewer")==0) Settings.ImageViewer=CopyStr(Settings.ImageViewer, CommandLineNext(CL));
else if (strcmp(ptr, "-view")==0) Settings.ImageViewer=CopyStr(Settings.ImageViewer, CommandLineNext(CL));

ptr=CommandLineNext(CL);
}

return(Act);
}

22 changes: 22 additions & 0 deletions command_line.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef TERM_WIFI_CMD_LINE_H
#define TERM_WIFI_CMD_LINE_H

#include "common.h"


#define ACT_INTERACTIVE 0
#define ACT_ADD 1
#define ACT_JOIN 2
#define ACT_LEAVE 3
#define ACT_LIST 4
#define ACT_SCAN 5
#define ACT_FORGET 6
#define ACT_IFACE_LIST 7
#define ACT_QRCODE 8
#define ACT_HELP 99



int ParseCommandLine(int argc, char *argv[], TNet *Conf);

#endif
33 changes: 33 additions & 0 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void QueryRootPassword(const char *Prompt)
S=STREAMFromDualFD(0,1);
Settings.RootPassword=TerminalReadPrompt(Settings.RootPassword, Prompt, TERM_SHOWSTARS, S);
StripCRLF(Settings.RootPassword);
TerminalPutStr("\r~>~0", S);
STREAMDestroy(S);
}

Expand Down Expand Up @@ -105,6 +106,12 @@ void NetDestroy(void *p_Net)
}


void NetSetESSID(TNet *Net, const char *ESSID)
{
Net->ESSID=ReplaceStr(Net->ESSID, ESSID, "\\x00", "");
StripQuotes(Net->ESSID);
}


const char *OutputNetQualityColor(TNet *Net)
{
Expand Down Expand Up @@ -163,3 +170,29 @@ char *OutputFormatNet(char *Output, TNet *Net)
return(Output);
}


char *FindCommandFromList(char *RetStr, const char *CmdList)
{
char *Token=NULL, *Cmd=NULL;
const char *ptr, *args;

RetStr=CopyStr(RetStr, "");
ptr=GetToken(CmdList, ",", &Token, 0);
while (ptr)
{
//Command might have arguments, so extract first word
args=GetToken(Token, "\\S", &Cmd, 0);
RetStr=FindFileInPath(RetStr, Cmd, getenv("PATH"));
if (StrValid(RetStr))
{
if (StrValid(args)) RetStr=MCatStr(RetStr, " ", args, NULL);
break;
}
ptr=GetToken(ptr, ",", &Token, 0);
}

Destroy(Token);
Destroy(Cmd);

return(RetStr);
}
3 changes: 3 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,7 @@ char *OutputFormatNet(char *Output, TNet *Net);
int FrequencyToChannel(int freq);
void QueryRootPassword(const char *Prompt);

void NetSetESSID(TNet *Net, const char *ESSID);

char *FindCommandFromList(char *RetStr, const char *List);
#endif
Loading

0 comments on commit a8c70e3

Please sign in to comment.