Skip to content

Commit 19e5c52

Browse files
committed
feat: supports sshd control
1 parent c4da556 commit 19e5c52

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

solutions/supervisor/main/include/global_cfg.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define PATH_SERVER_KEY "/etc/cert/server.key"
3838

3939
#define PATH_FIRST_LOGIN "/etc/.first_login"
40+
#define PATH_SSHD "/etc/init.d/S50sshd"
4041

4142
#define PATH_WLAN0_MAC "/sys/class/net/wlan0/address"
4243

@@ -100,5 +101,6 @@
100101
#define SCRIPT_DEVICE_GETFILEMD5 SCRIPT_DEVICE(getFileMd5)
101102
#define SCRIPT_DEVICE_RESTARTNODERED SCRIPT_DEVICE(restartNodered)
102103
#define SCRIPT_DEVICE_RESTARTSSCMA SCRIPT_DEVICE(restartSscma)
104+
#define SCRIPT_DEVICE_ENABLE_SSHD SCRIPT_DEVICE(enableSshd)
103105

104106
#endif

solutions/supervisor/main/include/utils_user.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ int addSShkey(HttpRequest* req, HttpResponse* resp);
1616
int deleteSShkey(HttpRequest* req, HttpResponse* resp);
1717
int login(HttpRequest* req, HttpResponse* resp);
1818
int authorization(HttpRequest* req, HttpResponse* resp);
19+
int setSShStatus(HttpRequest* req, HttpResponse* resp);
1920

2021
#endif

solutions/supervisor/main/src/http.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static void registerUserApi(HttpService& router) {
7777
API_POST(userMgr, updatePassword);
7878
API_POST(userMgr, addSShkey);
7979
API_POST(userMgr, deleteSShkey);
80+
API_POST(userMgr, setSShStatus);
8081
}
8182

8283
static void registerWiFiApi(HttpService& router) {

solutions/supervisor/main/src/utils_user.cpp

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <unistd.h>
1111
#include <unordered_set>
1212
#include <vector>
13+
#include <time.h>
1314

1415
#include "global_cfg.h"
1516
#include "hv/HttpServer.h"
@@ -261,6 +262,30 @@ static std::string generateToken(const std::string& username) {
261262
return std::string(token);
262263
}
263264

265+
int setSShStatus(HttpRequest* req, HttpResponse* resp) {
266+
hv::Json response;
267+
268+
int ret = 0;
269+
FILE* fp;
270+
char cmd[128] = SCRIPT_DEVICE_ENABLE_SSHD;
271+
272+
strcat(cmd, req->GetString("enabled").c_str());
273+
fp = popen(cmd, "r");
274+
if (fp == NULL) {
275+
syslog(LOG_ERR, "Failed to run `%s`(%s)\n", cmd, strerror(errno));
276+
ret = -1;
277+
goto exit;
278+
}
279+
pclose(fp);
280+
281+
exit:
282+
response["code"] = ret;
283+
response["msg"] = "";
284+
response["data"] = hv::Json({});
285+
286+
return resp->Json(response);
287+
}
288+
264289
int queryUserInfo(HttpRequest* req, HttpResponse* resp) {
265290
hv::Json User;
266291
User["code"] = 0;
@@ -278,6 +303,13 @@ int queryUserInfo(HttpRequest* req, HttpResponse* resp) {
278303
data["firstLogin"] = false;
279304
}
280305

306+
if (access(PATH_SSHD, F_OK) == 0) {
307+
data["sshEnabled"] = true;
308+
}
309+
else {
310+
data["sshEnabled"] = false;
311+
}
312+
281313
FILE* fp;
282314
char info[128];
283315
std::vector<hv::Json> sshkeyList;
@@ -356,7 +388,6 @@ int updatePassword(HttpRequest* req, HttpResponse* resp) {
356388
return resp->Json(User);
357389
}
358390

359-
360391
// if (req->GetString("oldPassword") == req->GetString("newPassword")) {
361392
// hv::Json User;
362393
// User["code"] = -1;
@@ -495,6 +526,7 @@ int authorization(HttpRequest* req, HttpResponse* resp) {
495526
"/api/userMgr/login",
496527
"/api/userMgr/updatePassword",
497528
"/api/userMgr/queryUserInfo",
529+
"/api/userMgr/setSShStatus",
498530
"/api/deviceMgr/queryDeviceInfo",
499531
"/api/deviceMgr/getDeviceList",
500532
"/api/deviceMgr/getModelList",
@@ -536,6 +568,8 @@ int authorization(HttpRequest* req, HttpResponse* resp) {
536568
}
537569

538570

571+
static int retryCount = 5;
572+
static struct timespec tsFailed;
539573
int login(HttpRequest* req, HttpResponse* resp) {
540574
hv::Json res;
541575
std::string username = req->GetString("userName");
@@ -564,11 +598,27 @@ int login(HttpRequest* req, HttpResponse* resp) {
564598
}
565599

566600
if (verifyPasswd(plaintext) != 0) {
601+
if (retryCount > 0) {
602+
retryCount--;
603+
if (retryCount == 0) {
604+
timespec_get(&tsFailed, TIME_UTC);
605+
}
606+
}
607+
if (retryCount == 0) {
608+
struct timespec ts;
609+
timespec_get(&ts, TIME_UTC);
610+
if (ts.tv_sec - ts.tv_sec > 60) {
611+
retryCount = 4;
612+
}
613+
}
567614
res["code"] = -1;
568615
res["msg"] = "Incorrect password";
569-
res["data"] = hv::Json({});
616+
res["data"] = hv::Json({
617+
{"retryCount", retryCount},
618+
});
570619
return resp->Json(res);
571620
}
621+
retryCount = 5;
572622

573623
// generate token randomly
574624
std::string token = generateToken(username);

solutions/supervisor/rootfs/usr/share/supervisor/scripts/devicetool.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,25 @@ restartSscma)
9797
restartApp $sscma
9898
;;
9999

100+
enableSshd)
101+
dir="/etc/init.d"
102+
dir_disabled="$dir/disabled"
103+
sshd="S*sshd"
104+
105+
if [ "$2" = "false" ]; then
106+
path=$(ls $dir/$sshd 2>/dev/null)
107+
if [ -n "$path" ] && [ -f $path ]; then
108+
mkdir -p $dir_disabled
109+
$path stop >/dev/null 2>&1
110+
mv -v $path $dir_disabled/
111+
fi
112+
else
113+
path=$(ls $dir_disabled/$sshd 2>/dev/null)
114+
if [ -n "$path" ] && [ -f $path ]; then
115+
$path restart >/dev/null 2>&1
116+
mv -v $path $dir/
117+
fi
118+
fi
119+
;;
120+
100121
esac

0 commit comments

Comments
 (0)