Skip to content

Commit

Permalink
实现命令行不选择网卡登录
Browse files Browse the repository at this point in the history
- 自动识别网卡登录
  • Loading branch information
leetking authored and leetking committed Sep 3, 2016
1 parent cd195a2 commit 372010a
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 66 deletions.
12 changes: 2 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ ifeq ($(TARGET), WIN)
CFLAGS += $(CFLAGS_WIN)
LDFLAGS += $(LDFLAGSS_WIN)
OBJS += eapol_win.o
ifeq "$(IS_GUI)" ""
CONFIG_WIN := config_netif
endif
else
OBJS += eapol.o
endif
Expand All @@ -45,7 +42,7 @@ ifeq ($(IS_GUI), GUI)
LDFLAGS += $(LDFLAGSS_GUI)
OBJS += main_gui.o gui.o
else
OBJS += main_cli.o
OBJS += main_cli.o wrap_eapol.o
endif

all: drcom $(CONFIG_WIN)
Expand All @@ -55,13 +52,8 @@ drcom: $(OBJS)
%.o: %.c
$(CC) -c $^ $(CFLAGS)

config_netif: config_netif.o
$(CC) -o $@ $^ $(LDFLAGS)
%.o: %.c
$(CC) -c $^ $(CFLAGS)

zip:

clean:
$(RM) *.o config_netif.exe config_netif drcom
$(RM) *.o drcom
.PHONY: clean all zip
3 changes: 3 additions & 0 deletions drcom-login.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@echo off
drcom.exe
pause
1 change: 0 additions & 1 deletion drcomrc.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
# 这是注释
# 使用方法:
# 把这个文件里的信息填写成你自己的,然后重命名为drcomrc
ethif = "eth0" #网络接口
uname = "201413640731" #用户名
pwd = "pwd" #密码
105 changes: 50 additions & 55 deletions main_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include <unistd.h>
#include "eapol.h"
#include "config.h"
#include "wrap_eapol.h"
#ifdef LINUX
# define _PATH_MAX PATH_MAX
# define PATH_SEP '/'
#elif defined(WINDOWS)
# include <windows.h>
# define _PATH_MAX MAX_PATH
# define PATH_SEP '\\'
//# include <winbase.h>
#endif

/*
Expand All @@ -21,7 +21,7 @@
* -1: 没有配置文件
* -2: 配置文件格式错误
*/
static int getconf(char *uname, char *pwd, char *ifname);
static int getconf(char *uname, char *pwd);
static void help(int argc, char **argv);
/*
* 获取程序所在的目录
Expand All @@ -35,7 +35,6 @@ int main(int argc, char **argv)
{
char uname[UNAME_LEN];
char pwd[PWD_LEN];
char ifname[IFNAMSIZ];

char islogoff = 0;

Expand All @@ -47,87 +46,83 @@ int main(int argc, char **argv)
return 0;
}
}
if (0 != getconf(uname, pwd, ifname)) {
if (0 != getconf(uname, pwd)) {
fprintf(stderr, "Not configure.\n");
return 1;
}
setifname(ifname);
if (islogoff) {
eaplogoff();
return 0;
}

switch (eaplogin(uname, pwd, NULL, NULL)) {
case 0: printf("Login success!!\n"); break;
case 1: fprintf(stderr, "No this user.\n"); break;
case 2: fprintf(stderr, "Password error.\n"); break;
case 3: fprintf(stderr, "Timeout.\n"); break;
case 4: fprintf(stderr, "Server refuse to login.\n"); break;
case -1: fprintf(stderr, "This ethernet can't be used.\n"); break;
case -2: fprintf(stderr, "Server isn't in range.\n"); break;
default: fprintf(stderr, "Other error.\n");
}
return 0;
switch (try_smart_login(uname, pwd, NULL, NULL)) {
case 0: printf("Login success!!\n"); break;
case 1: fprintf(stderr, "No this user.\n"); break;
case 2: fprintf(stderr, "Password error.\n"); break;
case 3: fprintf(stderr, "Timeout.\n"); break;
case 4: fprintf(stderr, "Server refuse to login.\n"); break;
case -1: fprintf(stderr, "This ethernet can't be used.\n"); break;
case -2: fprintf(stderr, "Server isn't in range.\n"); break;
case -3: fprintf(stderr, "Can't found ethernet.\n"); break;
default: fprintf(stderr, "Other error.\n");
}
return 0;
}

static int getconf(char *_uname, char *_pwd, char *_ifname)
static int getconf(char *_uname, char *_pwd)
{
char uname[MAX(RECORD_LEN, UNAME_LEN)];
char pwd[MAX(RECORD_LEN, PWD_LEN)];
char ifname[MAX(RECORD_LEN, IFNAMSIZ)];
char configpath[_PATH_MAX+1];
if (getexedir(configpath)) return -1;
strcat(configpath, CONF_PATH);
char uname[MAX(RECORD_LEN, UNAME_LEN)];
char pwd[MAX(RECORD_LEN, PWD_LEN)];
char configpath[_PATH_MAX+1];
if (getexedir(configpath)) return -1;
strcat(configpath, CONF_PATH);
#ifdef DEBUG
printf("configfile path: %s\n", configpath);
printf("configfile path: %s\n", configpath);
#endif
FILE *conf = fopen(configpath, "r");
if (NULL == conf) {
perror("drcomrc");
return -1;
}
if (0 != getvalue(conf, "uname", uname)) return -2;
if (0 != getvalue(conf, "pwd", pwd)) return -2;
if (0 != getvalue(conf, "ethif", ifname)) return -2;
strncpy(_uname, uname, UNAME_LEN);
strncpy(_pwd, pwd, PWD_LEN);
strncpy(_ifname, ifname, IFNAMSIZ);
FILE *conf = fopen(configpath, "r");
if (NULL == conf) {
perror("drcomrc");
return -1;
}
if (0 != getvalue(conf, "uname", uname)) return -2;
if (0 != getvalue(conf, "pwd", pwd)) return -2;
strncpy(_uname, uname, UNAME_LEN);
strncpy(_pwd, pwd, PWD_LEN);
#ifdef DEBUG
printf("uname: %s\n", _uname);
printf("pwd: %s\n", _pwd);
printf("ifname: %s\n", _ifname);
printf("uname: %s\n", _uname);
printf("pwd: %s\n", _pwd);
#endif
fclose(conf);
fclose(conf);

return 0;
return 0;
}

static void help(int argc, char **argv)
{
(void)argc;
printf("Usage: %s -r|h|l\n", argv[0]);
printf(" -r: relogin.\n");
printf(" -l: logoff.\n");
printf(" -h: show this help page.\n");
printf("NOTE: you must need root to login.\n");
(void)argc;
printf("Usage: %s -r|h|l\n", argv[0]);
printf(" -r: relogin.\n");
printf(" -l: logoff.\n");
printf(" -h: show this help page.\n");
printf("NOTE: you must need root to login.\n");
}
static int getexedir(char *exedir)
{
#ifdef LINUX
int cnt = readlink("/proc/self/exe", exedir, _PATH_MAX+1);
int cnt = readlink("/proc/self/exe", exedir, _PATH_MAX+1);
#elif defined(WINDOWS)
int cnt = GetModuleFileName(NULL, exedir, _PATH_MAX+1);
#endif
if (cnt < 0 || cnt > _PATH_MAX)
return -1;
if (cnt < 0 || cnt > _PATH_MAX)
return -1;
#ifdef DEBUG
printf("exedir: %s\n", exedir);
printf("exedir: %s\n", exedir);
#endif
char *end = strrchr(exedir, PATH_SEP);
if (!end) return -1;
*(end+1) = '\0';
char *end = strrchr(exedir, PATH_SEP);
if (!end) return -1;
*(end+1) = '\0';
#ifdef DEBUG
printf("exedir: %s\n", exedir);
printf("exedir: %s\n", exedir);
#endif
return 0;
return 0;
}
8 changes: 8 additions & 0 deletions type.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ typedef unsigned int uint32; /* 四个字节 */
#define MAX(x, y) ((x)>(y)?(x):(y))
#define MIN(x, y) ((x)>(y)?(y):(x))

#ifdef DEBUG
# define _D(...) fprintf(stderr, __VA_ARGS__)
#else
# define _D(...) ((void)0)
#endif

#define _M(...) fprintf(stderr, __VA_ARGS__)

#endif
136 changes: 136 additions & 0 deletions wrap_eapol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include <pcap.h>
#include "wrap_eapol.h"
#include "type.h"
#include "eapol.h"


#define IFDESCSIZ (126)
typedef struct {
char name[IFNAMSIZ]; /* linux下是eth0, windows采用的是注册表类似的(\Device\NPF_{xxxx-xxx-xx-xx-xxx}) */
#ifdef WINDOWS
char desc[IFDESCSIZ]; /* windows下描述(AMD PCNET Family PCI Ethernet Adapter) */
#endif
}iflist_t;

static int is_filter(char const *ifname)
{
/* 过滤掉无线,虚拟机接口等 */
char const *filter[] = {
/* windows */
"Wireless", "Microsoft",
"Virtual",
/* linux */
"lo", "wlan", "vboxnet",
};
for (int i = 0; i < ARRAY_SIZE(filter); ++i) {
if (strstr(ifname, filter[i]))
return 1;
}
return 0;
}
/*
* 获取所有网络接口
* ifnames 实际获取的接口
* cnt 两个作用,1:传入表示ifnames最多可以存储的接口个数
* 2:返回表示实际获取了的接口个数
* 返回接口个数在cnt里
* @return: >=0 成功,实际获取的接口个数
* -1 获取失败
* -2 cnt过小
*/
static int getall_ifs(iflist_t *ifs, int *cnt)
{
int i = 0;
if (!ifs || *cnt <= 0) return -2;
#ifdef LINUX /* linux (unix osx?) */
#define _PATH_PROCNET_DEV "/proc/net/dev"
#define BUFF_LINE_MAX (1024)
char buff[BUFF_LINE_MAX];
FILE *fd = fopen(_PATH_PROCNET_DEV, "r");
char const *filter[] = {
"lo", "docker",
"wlan", "vboxnet",
};
char *name;
if (NULL == fd) {
perror("fopen");
return -1;
}
/* _PATH_PROCNET_DEV文件格式如下,...表示后面我们不关心
* Inter-| Receive ...
* face |bytes packets ...
* eth0: 147125283 119599 ...
* wlan0: 229230 2635 ...
* lo: 10285509 38254 ...
*/
/* 略过开始两行 */
fgets(buff, BUFF_LINE_MAX, fd);
fgets(buff, BUFF_LINE_MAX, fd);
while (0 < fgets(buff, BUFF_LINE_MAX, fd)) {
name = get_ifname_from_buff(buff);
_D("%s\n", name);
/* 过滤无关网络接口 */
if (is_filter(name)) {
_D("filtered %s.\n", name);
continue;
}
strncpy(ifs[i].name, name, IFNAMSIZ);
_D("ifs[%d].name: %s\n", i, ifs[i].name);
++i;
if (i >= *cnt) {
fclose(fd);
return -2;
}
}
fclose(fd);
#elif defined(WINDOWS)
pcap_if_t *alldevs;
char errbuf[PCAP_ERRBUF_SIZE];
if (-1 == pcap_findalldevs(&alldevs, errbuf)) {
_M("Get interfaces handler error: %s\n", errbuf);
return -1;
}
for (pcap_if_t *d = alldevs; d; d = d->next) {
if (is_filter(d->description)) {
_D("filtered %s.\n", d->description);
continue;
}
if (i >= *cnt) return -2;
strncpy(ifs[i].name, d->name, IFNAMSIZ);
strncpy(ifs[i].desc, d->description, IFDESCSIZ);
++i;
}
pcap_freealldevs(alldevs);
#endif
*cnt = i;
return i;
}


extern int try_smart_login(char const *uname, char const *pwd,
int (*sucess_handle)(void const*), void const *args)
{
iflist_t ifs[IFS_MAX];
int ifs_max = IFS_MAX;
if (0 >= getall_ifs(ifs, &ifs_max)) return -3;
for (int i = 0; i < ifs_max; ++i) {
_M("%d. try interface (%s) to login\n", i,
#ifdef LINUX
ifs[i].name
#elif defined(WINDOWS)
ifs[i].desc
#endif
);
setifname(ifs[i].name);
int statu;
switch (statu = eaplogin(uname, pwd, NULL, NULL)) {
default:
break;
case 0:
case 1:
case 2:
return statu;
}
}
return -3;
}
14 changes: 14 additions & 0 deletions wrap_eapol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef _WRAP_EAPOL_H
#define _WRAP_EAPOL_H
/*
* 对eapol登录的一个包裹,实现自己选择网卡登录
*/

/*
* 自动选择网卡登录
*/
extern int try_smart_login(char const *uname, char const *pwd,
int (*sucess_handle)(void const*), void const *args);


#endif

0 comments on commit 372010a

Please sign in to comment.