Skip to content

Commit 3411343

Browse files
committed
Limit script location when webcontrol is open
1 parent 23027ad commit 3411343

File tree

7 files changed

+139
-84
lines changed

7 files changed

+139
-84
lines changed

doc/motion_config.html

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,16 @@ <h3><a name="static_object_time"></a>static_object_time</h3>
20282028
</ul>
20292029

20302030
<h3><a name="OptDetail_Scripts"></a> Script Execution </h3>
2031+
<p></p>
2032+
The following applies to all scripts being executed by Motion.
2033+
If <a href="#webcontrol_parms">webcontrol_parms</a> is
2034+
set to a value greater than or equal 3(i.e. the webcontrol allows changes
2035+
to scripts), then the program/script must be saved in a subdirectory from
2036+
the motion.conf file called 'scripts'. For example, if the
2037+
<code><small>motion.conf</small></code> file is located
2038+
at <code><small>/var/lib/motion/motion.conf</small></code>, the scripts
2039+
must be saved in the directory
2040+
<code><small>/var/lib/motion/scripts</small></code>.
20312041
<ul>
20322042
<h3><a name="on_event_start"></a> on_event_start </h3>
20332043
<ul>
@@ -2513,6 +2523,15 @@ <h3><a name="webcontrol_lock_script"></a> webcontrol_lock_script</h3>
25132523
<li> Values: String | Default: Not Defined</li>
25142524
Script to execute when a lockout has been triggered. Two additional parameters are passed to this script
25152525
when executed. The number of times the userid failed as well as the IP address attempting to log in.
2526+
<p></p>
2527+
If <a href="#webcontrol_parms">webcontrol_parms</a> is
2528+
set to a value greater than or equal 3(i.e. the webcontrol allows changes
2529+
to scripts), then the program/script must be saved in a subdirectory from
2530+
the motion.conf file called 'scripts'. For example, if the
2531+
<code><small>motion.conf</small></code> file is located
2532+
at <code><small>/var/lib/motion/motion.conf</small></code>, the scripts
2533+
must be saved in the directory
2534+
<code><small>/var/lib/motion/scripts</small></code>.
25162535
</ul>
25172536
<p></p>
25182537

@@ -2791,6 +2810,16 @@ <h3><a name="sql_pic_save"></a> sql_pic_save</h3>
27912810
</ul>
27922811

27932812
<h3><a name="OptDetail_Tracking"></a>Tracking</h3>
2813+
<p></p>
2814+
The following applies to all scripts being executed by Motion.
2815+
If <a href="#webcontrol_parms">webcontrol_parms</a> is
2816+
set to a value greater than or equal 3(i.e. the webcontrol allows changes
2817+
to scripts), then the program/script must be saved in a subdirectory from
2818+
the motion.conf file called 'scripts'. For example, if the
2819+
<code><small>motion.conf</small></code> file is located
2820+
at <code><small>/var/lib/motion/motion.conf</small></code>, the scripts
2821+
must be saved in the directory
2822+
<code><small>/var/lib/motion/scripts</small></code>.
27942823
<ul>
27952824
<h3><a name="ptz_auto_track"></a>ptz_auto_track</h3>
27962825
<ul>
@@ -3062,7 +3091,7 @@ <h3><a name="snd_show"></a>snd_show</h3>
30623091
<h2><a name="command_line_options"></a> Command Line Options </h2>
30633092
<ul>
30643093
<p></p>
3065-
<code>Motion [ -hbnsm ] [ -c config file path ] [ -d level ] [ -k level ] [ -p pid_file ] [ -l log_file ]</code>
3094+
<code>motion [ -hbnm ] [ -c config file path ] [ -d level ] [ -k level ] [ -p pid_file ] [ -l log_file ]</code>
30663095
<p></p>
30673096
<ul>
30683097
<li>-c : Full path and filename of config file.</li>
@@ -3082,17 +3111,17 @@ <h2><a name="command_line_options"></a> Command Line Options </h2>
30823111
<h2><a name="config_files"></a> The Configuration Files </h2>
30833112
<ul>
30843113
<p></p>
3085-
Motion will search for the configuration file called 'Motion.conf' in the following order:
3114+
Motion will search for the configuration file called 'motion.conf' in the following order:
30863115
<p></p>
30873116
<ol>
30883117
<li> Current directory where Motion was invoked</li>
3089-
<li> The directory called '.Motion' in the current users home directory</li>
3090-
<li> The sub directory of lib/Motion from the directory defined
3118+
<li> The directory called '.motion' in the current users home directory</li>
3119+
<li> The sub directory of lib/motion from the directory defined
30913120
by --localstatedir=DIR during the configure step of installation. If this was
3092-
not defined, it will default to /var/lib/Motion or /usr/local/var/lib/Motion
3121+
not defined, it will default to /var/lib/motion or /usr/local/var/lib/motion
30933122
depending upon your system</li>
30943123
<li> The directory defined by --sysconfdir=DIR during the configure step of installation.
3095-
If this was not defined, it will default to /etc/Motion or /usr/local/etc/Motion
3124+
If this was not defined, it will default to /etc/motion or /usr/local/etc/motion
30963125
depending upon your system. Please note that the use of this directory for the configuration
30973126
files is deprecated. Some significant functionality of Motion will not be available
30983127
if the configuration files are in this directory.</li>

src/conf.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,18 @@ ctx_parm config_parms[] = {
109109
{"pre_capture", PARM_TYP_INT, PARM_CAT_07, PARM_LEVEL_LIMITED },
110110
{"post_capture", PARM_TYP_INT, PARM_CAT_07, PARM_LEVEL_LIMITED },
111111

112-
{"on_event_start", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
113-
{"on_event_end", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
114-
{"on_picture_save", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
115-
{"on_area_detected", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
116-
{"on_motion_detected", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
117-
{"on_movie_start", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
118-
{"on_movie_end", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
119-
{"on_camera_lost", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
120-
{"on_camera_found", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
121-
{"on_secondary_detect", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
122-
{"on_action_user", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
123-
{"on_sound_alert", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_RESTRICTED },
112+
{"on_event_start", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
113+
{"on_event_end", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
114+
{"on_picture_save", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
115+
{"on_area_detected", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
116+
{"on_motion_detected", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
117+
{"on_movie_start", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
118+
{"on_movie_end", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
119+
{"on_camera_lost", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
120+
{"on_camera_found", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
121+
{"on_secondary_detect", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
122+
{"on_action_user", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
123+
{"on_sound_alert", PARM_TYP_STRING, PARM_CAT_08, PARM_LEVEL_SCRIPTS },
124124

125125
{"picture_output", PARM_TYP_LIST, PARM_CAT_09, PARM_LEVEL_LIMITED },
126126
{"picture_output_motion", PARM_TYP_LIST, PARM_CAT_09, PARM_LEVEL_LIMITED },
@@ -167,10 +167,10 @@ ctx_parm config_parms[] = {
167167
{"webcontrol_key", PARM_TYP_STRING, PARM_CAT_13, PARM_LEVEL_RESTRICTED },
168168
{"webcontrol_headers", PARM_TYP_PARAMS, PARM_CAT_13, PARM_LEVEL_ADVANCED },
169169
{"webcontrol_html", PARM_TYP_STRING, PARM_CAT_13, PARM_LEVEL_ADVANCED },
170-
{"webcontrol_actions", PARM_TYP_PARAMS, PARM_CAT_13, PARM_LEVEL_RESTRICTED },
170+
{"webcontrol_actions", PARM_TYP_PARAMS, PARM_CAT_13, PARM_LEVEL_ADVANCED },
171171
{"webcontrol_lock_minutes", PARM_TYP_INT, PARM_CAT_13, PARM_LEVEL_ADVANCED },
172172
{"webcontrol_lock_attempts", PARM_TYP_INT, PARM_CAT_13, PARM_LEVEL_ADVANCED },
173-
{"webcontrol_lock_script", PARM_TYP_STRING, PARM_CAT_13, PARM_LEVEL_RESTRICTED },
173+
{"webcontrol_lock_script", PARM_TYP_STRING, PARM_CAT_13, PARM_LEVEL_SCRIPTS },
174174

175175
{"stream_preview_scale", PARM_TYP_INT, PARM_CAT_14, PARM_LEVEL_LIMITED },
176176
{"stream_preview_newline", PARM_TYP_BOOL, PARM_CAT_14, PARM_LEVEL_LIMITED },
@@ -200,13 +200,13 @@ ctx_parm config_parms[] = {
200200

201201
{"ptz_auto_track", PARM_TYP_BOOL, PARM_CAT_17, PARM_LEVEL_LIMITED },
202202
{"ptz_wait", PARM_TYP_INT, PARM_CAT_17, PARM_LEVEL_LIMITED },
203-
{"ptz_move_track", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
204-
{"ptz_pan_left", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
205-
{"ptz_pan_right", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
206-
{"ptz_tilt_up", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
207-
{"ptz_tilt_down", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
208-
{"ptz_zoom_in", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
209-
{"ptz_zoom_out", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_RESTRICTED },
203+
{"ptz_move_track", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
204+
{"ptz_pan_left", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
205+
{"ptz_pan_right", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
206+
{"ptz_tilt_up", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
207+
{"ptz_tilt_down", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
208+
{"ptz_zoom_in", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
209+
{"ptz_zoom_out", PARM_TYP_STRING, PARM_CAT_17, PARM_LEVEL_SCRIPTS },
210210

211211
{"snd_device", PARM_TYP_STRING, PARM_CAT_18, PARM_LEVEL_ADVANCED },
212212
{"snd_params", PARM_TYP_PARAMS, PARM_CAT_18, PARM_LEVEL_ADVANCED },
@@ -2378,10 +2378,10 @@ void cls_config::edit_webcontrol_parms(std::string &parm, enum PARM_ACT pact)
23782378
{
23792379
int parm_in;
23802380
if (pact == PARM_ACT_DFLT) {
2381-
webcontrol_parms = 2;
2381+
webcontrol_parms = PARM_LEVEL_ADVANCED;
23822382
} else if (pact == PARM_ACT_SET) {
23832383
parm_in = atoi(parm.c_str());
2384-
if ((parm_in < 0) || (parm_in > 3)) {
2384+
if ((parm_in < PARM_LEVEL_ALWAYS) || (parm_in > PARM_LEVEL_RESTRICTED)) {
23852385
MOTION_LOG(NTC, TYPE_ALL, NO_ERRNO, _("Invalid webcontrol_parms %d"),parm_in);
23862386
} else {
23872387
webcontrol_parms = parm_in;
@@ -2390,7 +2390,7 @@ void cls_config::edit_webcontrol_parms(std::string &parm, enum PARM_ACT pact)
23902390
parm = std::to_string(webcontrol_parms);
23912391
} else if (pact == PARM_ACT_LIST) {
23922392
parm = "[";
2393-
parm = parm + "\"0\",\"1\",\"2\",\"3\"";
2393+
parm = parm + "\"0\",\"1\",\"2\",\"3\",\"4\"";
23942394
parm = parm + "]";
23952395
}
23962396
return;

src/conf.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@
5555
PARM_LEVEL_ALWAYS = 0
5656
,PARM_LEVEL_LIMITED = 1
5757
,PARM_LEVEL_ADVANCED = 2
58-
,PARM_LEVEL_RESTRICTED = 3
58+
,PARM_LEVEL_SCRIPTS = 3
59+
,PARM_LEVEL_RESTRICTED = 4
5960
,PARM_LEVEL_NEVER = 99
6061
};
6162

src/sound.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class cls_sound {
114114
~cls_sound();
115115

116116
enum DEVICE_STATUS device_status;
117-
117+
cls_motapp *app;
118118
cls_config *conf_src;
119119
cls_config *cfg;
120120
std::string device_name;
@@ -131,7 +131,6 @@ class cls_sound {
131131
void handler_shutdown();
132132

133133
private:
134-
cls_motapp *app;
135134
int watchdog;
136135

137136
void cleanup();

src/util.cpp

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -666,15 +666,13 @@ AVPacket *mypacket_alloc(AVPacket *pkt)
666666

667667
}
668668

669-
void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname)
669+
void util_exec_base(cls_motapp *p_app, std::string rslt)
670670
{
671-
std::string rslt, cmd_full, cmd_nm, tmp_parm;
671+
std::string cmd_full, cmd_nm, cmd_tmp, parm_tmp, parm_log;
672672
std::vector<std::string> parms;
673673
int pid, indx;
674674
size_t pos;
675675

676-
mystrftime(cam, rslt, usrcmd, fname);
677-
mytrim(rslt);
678676

679677
/* Parse the full command*/
680678
if (rslt[0] == '"') {
@@ -693,11 +691,11 @@ void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname)
693691

694692
/* Parse out the parameters */
695693
if (pos == std::string::npos) {
696-
tmp_parm = "";
694+
parm_tmp = "";
697695
} else {
698-
tmp_parm = rslt.substr(pos+1);
696+
parm_tmp = rslt.substr(pos+1);
699697
}
700-
mytrim(tmp_parm);
698+
mytrim(parm_tmp);
701699

702700
/* Parse out the command name to execute */
703701
pos = cmd_full.find_last_of("/");
@@ -707,28 +705,47 @@ void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname)
707705
cmd_nm = cmd_full.substr(pos+1);
708706
}
709707
mytrim(cmd_nm);
708+
parm_log = parm_tmp;
709+
710+
/* If webcontrol is opened up to allow changing of the
711+
scripts, then limit the location of file being executed
712+
to the scripts subdirectory
713+
*/
714+
if (p_app->cfg->webcontrol_parms >= PARM_LEVEL_SCRIPTS) {
715+
cmd_tmp = p_app->cfg->conf_filename.substr(0
716+
, p_app->cfg->conf_filename.find_last_of("/"));
717+
cmd_tmp += "/scripts/" + cmd_nm;
718+
if (cmd_tmp != cmd_full) {
719+
MOTION_LOG(NTC, TYPE_EVENTS, NO_ERRNO
720+
,_("Adjusting the path provided since webcontrol is open"));
721+
}
722+
MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO,"Original >%s<",cmd_full.c_str());
723+
MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO,"New >%s<",cmd_tmp.c_str());
724+
cmd_full = cmd_tmp;
725+
MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO,"Adjusted >%s<",cmd_full.c_str());
726+
}
710727

711728
/* The vector is being used for ease of programming
712729
and is likely not the most efficient way to get the
713730
values into the const char * array for execv
714731
*/
715732
/* Parse the individual parameters into the vector*/
716733
parms.push_back(cmd_nm);
717-
while (tmp_parm != "") {
718-
if (tmp_parm[0] == '"') {
719-
pos = tmp_parm.find('"', 1)+1;
720-
} else if (tmp_parm[0] == '\'') {
721-
pos = tmp_parm.find('\'', 1)+1;
734+
while (parm_tmp != "") {
735+
if (parm_tmp[0] == '"') {
736+
pos = parm_tmp.find('"', 1)+1;
737+
} else if (parm_tmp[0] == '\'') {
738+
pos = parm_tmp.find('\'', 1)+1;
722739
} else {
723-
pos = tmp_parm.find(" ", 0);
740+
pos = parm_tmp.find(" ", 0);
724741
}
725-
parms.push_back(tmp_parm.substr(0, pos));
742+
parms.push_back(parm_tmp.substr(0, pos));
726743
if (pos == std::string::npos) {
727-
tmp_parm = "";
744+
parm_tmp = "";
728745
} else {
729-
tmp_parm.erase(0, pos+1);
746+
parm_tmp.erase(0, pos+1);
730747
}
731-
mytrim(tmp_parm);
748+
mytrim(parm_tmp);
732749
}
733750

734751
/* Put the vector into an array that can be processed by execv*/
@@ -742,60 +759,59 @@ void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname)
742759
if (!pid) {
743760
/* Detach from parent */
744761
setsid();
745-
execv (cmd_full.c_str(), (char **)parmv);
762+
execv(cmd_full.c_str(), (char **)parmv);
746763
/* if above function succeeds the program never reaches here */
747764
MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO
748-
,_("Unable to start external command >%s<"), rslt.c_str());
765+
,_("Unable to start external command >%s %s<")
766+
, cmd_full.c_str(), parm_log.c_str());
749767

750768
exit(1);
751769
}
752770

753771
if (pid == 0) {
754772
MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO
755-
,_("Unable to start external command >%s<"), rslt.c_str());
773+
,_("Unable to start external command >%s %s<")
774+
, cmd_full.c_str(), parm_log.c_str());
756775
} else {
757776
MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO
758-
,_("Executing external command >%s<"), rslt.c_str());
777+
,_("Executing external command >%s %s<")
778+
, cmd_full.c_str(), parm_log.c_str());
759779
}
760780

761781
}
762782

763-
/* Legacy format with chars */
764-
void util_exec_command(cls_camera *cam, const char *c_usrcmd, const char *c_fname)
783+
void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname)
765784
{
766-
util_exec_command(cam,std::string(c_usrcmd), std::string(c_fname));
785+
std::string rslt;
786+
mystrftime(cam, rslt, usrcmd, fname);
787+
mytrim(rslt);
788+
util_exec_base(cam->app, rslt);
767789
}
768790

769-
void util_exec_command(cls_sound *snd, std::string cmd)
791+
/* Legacy format with chars */
792+
void util_exec_command(cls_camera *cam, const char *c_usrcmd, const char *c_fname)
770793
{
771-
std::string dst;
772-
int pid;
794+
std::string usrcmd, fname;
773795

774-
mystrftime(snd, dst, cmd);
775-
776-
pid = fork();
777-
if (!pid) {
778-
/* Detach from parent */
779-
setsid();
780-
781-
execl("/bin/sh", "sh", "-c", dst.c_str(), " &",(char*)NULL);
782-
783-
/* if above function succeeds the program never reaches here */
784-
MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO
785-
,_("Unable to start external command '%s'"),dst.c_str());
786-
787-
exit(1);
796+
if (c_usrcmd == nullptr) {
797+
usrcmd = "";
798+
} else {
799+
usrcmd = std::string(c_usrcmd);
788800
}
789-
790-
if (pid > 0) {
791-
waitpid(pid, NULL, 0);
801+
if (c_fname == nullptr){
802+
fname = "";
792803
} else {
793-
MOTION_LOG(ALR, TYPE_EVENTS, SHOW_ERRNO
794-
,_("Unable to start external command '%s'"), dst.c_str());
804+
fname = std::string(c_fname);
795805
}
806+
util_exec_command(cam, usrcmd, fname);
807+
}
796808

797-
MOTION_LOG(DBG, TYPE_EVENTS, NO_ERRNO
798-
,_("Executing external command '%s'"), dst.c_str());
809+
void util_exec_command(cls_sound *snd, std::string usrcmd)
810+
{
811+
std::string rslt;
812+
mystrftime(snd, rslt, usrcmd);
813+
mytrim(rslt);
814+
util_exec_base(snd->app, rslt);
799815
}
800816

801817
/*********************************************/

src/util.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ struct ctx_params {
8585
void mystrftime(cls_camera *cam, std::string &rslt
8686
, std::string usrcmd, std::string fname);
8787
void mystrftime(cls_sound *snd, std::string &dst, std::string fmt);
88-
void util_exec_command(cls_camera *cam, const char *command, const char *filename);
89-
void util_exec_command(cls_sound *snd, std::string cmd);
88+
void util_exec_base(cls_motapp *p_app, std::string rslt);
9089
void util_exec_command(cls_camera *cam, std::string usrcmd, std::string fname);
90+
void util_exec_command(cls_camera *cam, const char *c_usrcmd, const char *c_fname);
91+
void util_exec_command(cls_sound *snd, std::string usrcmd);
9192

9293
void mythreadname_set(const char *abbr, int threadnbr, const char *threadname);
9394
void mythreadname_get(char *threadname);

0 commit comments

Comments
 (0)