forked from Motion-Project/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webu.c
2011 lines (1690 loc) · 71.2 KB
/
webu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* webu.c
*
* Webcontrol and Streams for motion.
*
* This software is distributed under the GNU Public License Version 2
* See also the file 'COPYING'.
*
* Portions of code from Angel Carpintero ([email protected])
* from webhttpd.c Copyright 2004-2005
*
* Majority of module written by MrDave.
*
* Function naming scheme:
* webu* - All functions in this module have this prefix.
* webu_start - Entry point to start the daemon.
* webu_stop - Entry point to stop the daemon
* webu_mhd* - Functions related to libmicrohttd implementation
* webu_process_action - Performs most items under the action menu
* webu_process_config - Saves the parameter values into Motion.
* webu_process_track - Performs the tracking functions.
*
* Some function names are long and are not expected to contain any
* logger message that would display the function name to the user.
*
* Functions are generally kept to under one page in length
*
* Known Issues:
* The quit/restart uses signals and this should be reconsidered.
* The tracking is "best effort" since developer does not have tracking camera.
* The conf_cmdparse assumes that the pointers to the motion context for each
* camera are always sequential and enforcement of the pointers being sequential
* has not been observed in the other modules. (This is a legacy assumption)
*/
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "motion.h"
#include "webu.h"
#include "webu_html.h"
#include "webu_text.h"
#include "webu_stream.h"
#include "translate.h"
/* Context to pass the parms to functions to start mhd */
struct mhdstart_ctx {
struct context **cnt;
char *tls_cert;
char *tls_key;
int ctrl;
int indxthrd;
struct MHD_OptionItem *mhd_ops;
int mhd_opt_nbr;
unsigned int mhd_flags;
int ipv6;
struct sockaddr_in lpbk_ipv4;
struct sockaddr_in6 lpbk_ipv6;
};
static void webu_context_init(struct context **cntlst, struct context *cnt, struct webui_ctx *webui) {
int indx;
webui->url = mymalloc(WEBUI_LEN_URLI);
webui->uri_camid = mymalloc(WEBUI_LEN_PARM);
webui->uri_cmd1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_cmd2 = mymalloc(WEBUI_LEN_PARM);
webui->uri_parm1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_value1 = mymalloc(WEBUI_LEN_PARM);
webui->uri_parm2 = mymalloc(WEBUI_LEN_PARM);
webui->uri_value2 = mymalloc(WEBUI_LEN_PARM);
webui->clientip = mymalloc(WEBUI_LEN_URLI);
webui->hostname = mymalloc(WEBUI_LEN_PARM);
webui->auth_denied = mymalloc(WEBUI_LEN_RESP);
webui->auth_opaque = mymalloc(WEBUI_LEN_PARM);
webui->auth_realm = mymalloc(WEBUI_LEN_PARM);
webui->text_eol = mymalloc(WEBUI_LEN_PARM);
webui->auth_user = NULL; /* Buffer to hold the user name*/
webui->auth_pass = NULL; /* Buffer to hold the password */
webui->authenticated = FALSE; /* boolean for whether we are authenticated*/
webui->lang = mymalloc(3); /* Two digit lang code plus null terminator */
webui->lang_full = mymalloc(6); /* lang code, e.g US_en */
webui->resp_size = WEBUI_LEN_RESP * 10; /* The size of the resp_page buffer. May get adjusted */
webui->resp_used = 0; /* How many bytes used so far in resp_page*/
webui->stream_pos = 0; /* Stream position of image being sent */
webui->resp_page = mymalloc(webui->resp_size); /* The response being constructed */
webui->cntlst = cntlst; /* The list of context's for all cameras */
webui->cnt = cnt; /* The context pointer for a single camera */
webui->cnct_type = WEBUI_CNCT_UNKNOWN;
/* get the number of cameras and threads */
indx = 0;
if (webui->cntlst != NULL){
while (webui->cntlst[++indx]);
}
webui->cam_threads = indx;
webui->cam_count = indx;
if (indx > 1)
webui->cam_count--;
/* 1 thread, 1 camera = just motion.conf.
* 2 thread, 1 camera, then using motion.conf plus a separate camera file */
snprintf(webui->lang_full, 6,"%s", getenv("LANGUAGE"));
snprintf(webui->lang, 3,"%s",webui->lang_full);
memset(webui->hostname,'\0',WEBUI_LEN_PARM);
memset(webui->resp_page,'\0',webui->resp_size);
return;
}
static void webu_context_null(struct webui_ctx *webui) {
/* Null out all the pointers in our webui context */
webui->url = NULL;
webui->hostname = NULL;
webui->uri_camid = NULL;
webui->uri_cmd1 = NULL;
webui->uri_cmd2 = NULL;
webui->uri_parm1 = NULL;
webui->uri_value1 = NULL;
webui->uri_parm2 = NULL;
webui->uri_value2 = NULL;
webui->lang = NULL;
webui->lang_full = NULL;
webui->resp_page = NULL;
webui->connection = NULL;
webui->auth_user = NULL;
webui->auth_pass = NULL;
webui->auth_denied = NULL;
webui->auth_opaque = NULL;
webui->auth_realm = NULL;
webui->clientip = NULL;
webui->text_eol = NULL;
return;
}
static void webu_context_free(struct webui_ctx *webui) {
if (webui->hostname != NULL) free(webui->hostname);
if (webui->url != NULL) free(webui->url);
if (webui->uri_camid != NULL) free(webui->uri_camid);
if (webui->uri_cmd1 != NULL) free(webui->uri_cmd1);
if (webui->uri_cmd2 != NULL) free(webui->uri_cmd2);
if (webui->uri_parm1 != NULL) free(webui->uri_parm1);
if (webui->uri_value1 != NULL) free(webui->uri_value1);
if (webui->uri_parm2 != NULL) free(webui->uri_parm2);
if (webui->uri_value2 != NULL) free(webui->uri_value2);
if (webui->lang != NULL) free(webui->lang);
if (webui->lang_full != NULL) free(webui->lang_full);
if (webui->resp_page != NULL) free(webui->resp_page);
if (webui->auth_user != NULL) free(webui->auth_user);
if (webui->auth_pass != NULL) free(webui->auth_pass);
if (webui->auth_denied != NULL) free(webui->auth_denied);
if (webui->auth_opaque != NULL) free(webui->auth_opaque);
if (webui->auth_realm != NULL) free(webui->auth_realm);
if (webui->clientip != NULL) free(webui->clientip);
if (webui->text_eol != NULL) free(webui->text_eol);
webu_context_null(webui);
free(webui);
return;
}
static void webu_badreq(struct webui_ctx *webui){
/* This function is used in this webu module as a central function when there is a bad
* request. Since sometimes we will be unable to determine what camera context (stream
* or camera) originated the request and we have NULL for cntlist and cnt, we default the
* response to be HTML. Otherwise, we do know the type and we send back to the user the
* bad request response either with or without the HTML tags.
*/
if (webui->cnt != NULL) {
if (webui->cnt->conf.webcontrol_interface == 1){
webu_text_badreq(webui);
} else {
webu_html_badreq(webui);
}
} else if (webui->cntlst != NULL) {
if (webui->cntlst[0]->conf.webcontrol_interface == 1){
webu_text_badreq(webui);
} else {
webu_html_badreq(webui);
}
} else {
webu_html_badreq(webui);
}
}
void webu_write(struct webui_ctx *webui, const char *buf) {
/* Copy the buf data to our response buffer. If the response buffer is not large enough to
* accept our new data coming in, then expand it in chunks of 10
*/
int resp_len;
char *temp_resp;
size_t temp_size;
resp_len = strlen(buf);
temp_size = webui->resp_size;
while ((resp_len + webui->resp_used) > temp_size){
temp_size = temp_size + (WEBUI_LEN_RESP * 10);
}
if (temp_size > webui->resp_size){
temp_resp = mymalloc(webui->resp_size);
memcpy(temp_resp, webui->resp_page, webui->resp_size);
free(webui->resp_page);
webui->resp_page = mymalloc(temp_size);
memset(webui->resp_page,'\0',temp_size);
memcpy(webui->resp_page, temp_resp, webui->resp_size);
webui->resp_size = temp_size;
free(temp_resp);
}
memcpy(webui->resp_page + webui->resp_used, buf, resp_len);
webui->resp_used = webui->resp_used + resp_len;
return;
}
static int webu_url_decode(char *urlencoded, size_t length) {
/* We are sent a URI encoded string and this decodes it to characters
* If the sent URL that isn't valid, then we clear out the URL
* so it is not processed in further functions. The "answer" functions
* look for empty urls and answer with bad request
*/
char *data = urlencoded;
char *urldecoded = urlencoded;
int scan_rslt;
size_t origlen;
origlen = length;
if (urlencoded[0] != '/'){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO, _("Invalid url: %s"),urlencoded);
memset(urlencoded,'\0',origlen);
return -1;
}
while (length > 0) {
if (*data == '%') {
char c[3];
int i;
data++;
length--;
c[0] = *data++;
length--;
c[1] = *data;
c[2] = 0;
scan_rslt = sscanf(c, "%x", &i);
if (scan_rslt < 1){
MOTION_LOG(ERR, TYPE_STREAM, NO_ERRNO,_("Error decoding url"));
memset(urlencoded,'\0',origlen);
return -1;
}
if (i < 128) {
*urldecoded++ = (char)i;
} else {
*urldecoded++ = '%';
*urldecoded++ = c[0];
*urldecoded++ = c[1];
}
} else if (*data == '<' || *data == '+' || *data == '>') {
*urldecoded++ = ' ';
} else {
*urldecoded++ = *data;
}
data++;
length--;
}
*urldecoded = '\0';
return 0;
}
static void webu_parms_edit(struct webui_ctx *webui) {
/* Determine the thread number provided.
* If no thread provided, assign it to -1
* Samples:
* http://localhost:8081/0/stream (cntlist will be populated and this function will set cnt)
* http://localhost:8081/stream (cntlist will be null, cnt will be populated)
* http://localhost:8081/ (cntlist will be null, cnt will be populated)
*/
int indx, is_nbr;
if (strlen(webui->uri_camid) > 0){
is_nbr = TRUE;
for (indx=0; indx < (int)strlen(webui->uri_camid);indx++){
if ((webui->uri_camid[indx] > '9') || (webui->uri_camid[indx] < '0')) is_nbr = FALSE;
}
if (is_nbr){
webui->thread_nbr = atoi(webui->uri_camid);
} else {
webui->thread_nbr = -1;
}
} else {
webui->thread_nbr = -1;
}
/* Set the single context pointer to thread we are answering
* If the connection is for a single stream (legacy method of a port
* per stream), then the cntlist will be null and the camera context
* will already be assigned into webui->cnt. This is part of the
* init function which is called for MHD and it has the different
* variations depending upon how the port and cameras were specified.
* Also set/convert the camid into the thread number.
*/
if (webui->cntlst != NULL){
if (webui->thread_nbr < 0){
webui->cnt = webui->cntlst[0];
webui->thread_nbr = 0;
} else {
indx = 0;
while (webui->cntlst[indx] != NULL){
if (webui->cntlst[indx]->camera_id == webui->thread_nbr){
webui->thread_nbr = indx;
break;
}
indx++;
}
/* This may be null, in which case we will not answer the request */
webui->cnt = webui->cntlst[indx];
}
}
}
static void webu_parseurl_parms(struct webui_ctx *webui, char *st_pos) {
/* Parse the parameters of the URI
* Earlier functions have assigned the st_pos to the slash after the action and it is
* pointing at the set/get when this function is invoked.
* Samples (MHD takes off the IP:port)
* /{camid}/config/set?{parm}={value1}
* /{camid}/config/get?query={parm}
* /{camid}/track/set?x={value1}&y={value2}
* /{camid}/track/set?pan={value1}&tilt={value2}
* /{camid}/{cmd1}/{cmd2}?{parm1}={value1}&{parm2}={value2}
*/
int parm_len, last_parm;
char *en_pos;
/* First parse out the "set","get","pan","tilt","x","y"
* from the uri and put them into the cmd2.
* st_pos is at the beginning of the command
* If there is no ? then we are done parsing
* Note that each section is looking for a different
* delimitter. (?, =, &, =, &)
*/
last_parm = FALSE;
en_pos = strstr(st_pos,"?");
if (en_pos != NULL){
parm_len = en_pos - st_pos + 1;
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_cmd2, parm_len,"%s", st_pos);
/* Get the parameter name */
st_pos = st_pos + parm_len; /* Move past the command */
en_pos = strstr(st_pos,"=");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = TRUE;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_parm1, parm_len,"%s", st_pos);
if (!last_parm){
/* Get the parameter value */
st_pos = st_pos + parm_len; /* Move past the equals sign */
en_pos = strstr(st_pos,"&");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = TRUE;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_value1, parm_len,"%s", st_pos);
}
if (!last_parm){
/* Get the next parameter name */
st_pos = st_pos + parm_len; /* Move past the previous command */
en_pos = strstr(st_pos,"=");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = TRUE;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_parm2, parm_len,"%s", st_pos);
}
if (!last_parm){
/* Get the next parameter value */
st_pos = st_pos + parm_len; /* Move past the equals sign */
en_pos = strstr(st_pos,"&");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_parm = TRUE;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return;
snprintf(webui->uri_value2, parm_len,"%s", st_pos);
}
}
}
static void webu_parseurl_reset(struct webui_ctx *webui) {
/* Reset the variables to empty strings*/
memset(webui->uri_camid,'\0',WEBUI_LEN_PARM);
memset(webui->uri_cmd1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_cmd2,'\0',WEBUI_LEN_PARM);
memset(webui->uri_parm1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_value1,'\0',WEBUI_LEN_PARM);
memset(webui->uri_parm2,'\0',WEBUI_LEN_PARM);
memset(webui->uri_value2,'\0',WEBUI_LEN_PARM);
}
static int webu_parseurl(struct webui_ctx *webui) {
/* Parse the sent URI into the commands and parameters
* so we can check the resulting strings in later functions
* and determine what actions to take.
* Samples
* /
* /{camid}
* /{camid}/config/set?log_level=6
* /{camid}/config/set?{parm}={value1}
* /{camid}/config/get?query={parm}
* /{camid}/track/set?x={value1}&y={value2}
* /{camid}/track/set?pan={value1}&tilt={value2}
* /{camid}/{cmd1}/{cmd2}?{parm1}={value1}&{parm2}={value2}
*/
int retcd, parm_len, last_slash;
char *st_pos, *en_pos;
retcd = 0;
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Sent url: %s"),webui->url);
webu_parseurl_reset(webui);
if (strlen(webui->url) == 0) return -1;
retcd = webu_url_decode(webui->url, strlen(webui->url));
if (retcd != 0) return retcd;
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO, _("Decoded url: %s"),webui->url);
/* Home page */
if (strlen(webui->url) == 1) return 0;
last_slash = 0;
/* Get the camid number and which sometimes this will contain an action if the user
* is setting the port for a particular camera and requests the
* stream by using http://localhost:port/stream
*/
st_pos = webui->url + 1; /* Move past the first "/" */
if (*st_pos == '-') return -1; /* Never allow a negative number */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url);
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */
snprintf(webui->uri_camid, parm_len,"%s", st_pos);
if (!last_slash){
/* Get cmd1 or action */
st_pos = st_pos + parm_len; /* Move past the camid */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len ;
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */
snprintf(webui->uri_cmd1, parm_len,"%s", st_pos);
}
if (!last_slash){
/* Get cmd2 or action */
st_pos = st_pos + parm_len; /* Move past the first command */
en_pos = strstr(st_pos,"/");
if (en_pos == NULL){
parm_len = strlen(webui->url) - parm_len;
last_slash = 1;
} else {
parm_len = en_pos - st_pos + 1;
}
if (parm_len >= WEBUI_LEN_PARM) return -1; /* var was malloc'd to WEBUI_LEN_PARM */
snprintf(webui->uri_cmd2, parm_len,"%s", st_pos);
}
if ((!strcmp(webui->uri_cmd1,"config") ||
!strcmp(webui->uri_cmd1,"track") ) &&
(strlen(webui->uri_cmd2) > 0)) {
webu_parseurl_parms(webui, st_pos);
}
MOTION_LOG(DBG, TYPE_STREAM, NO_ERRNO,
"camid: >%s< cmd1: >%s< cmd2: >%s< parm1:>%s< val1:>%s< parm2:>%s< val2:>%s<"
,webui->uri_camid
,webui->uri_cmd1, webui->uri_cmd2
,webui->uri_parm1, webui->uri_value1
,webui->uri_parm2, webui->uri_value2);
return retcd;
}
void webu_process_action(struct webui_ctx *webui) {
/* Process the actions from the webcontrol that the user requested. This is used
* for both the html and text interface. The text interface just adds a additional
* response whereas the html just performs the action
*/
int indx;
indx = 0;
if ((strcmp(webui->uri_cmd2,"makemovie") == 0) ||
(strcmp(webui->uri_cmd2,"eventend") == 0)) {
if (webui->thread_nbr == 0 && webui->cam_threads > 1) {
while (webui->cntlst[++indx]){
webui->cntlst[indx]->event_stop = TRUE;
}
} else {
webui->cnt->event_stop = TRUE;
}
} else if (strcmp(webui->uri_cmd2,"eventstart") == 0){
if (webui->thread_nbr == 0 && webui->cam_threads > 1) {
while (webui->cntlst[++indx]){
webui->cntlst[indx]->event_user = TRUE;
}
} else {
webui->cnt->event_user = TRUE;
}
} else if (!strcmp(webui->uri_cmd2,"snapshot")){
if (webui->thread_nbr == 0 && webui->cam_threads > 1) {
while (webui->cntlst[++indx])
webui->cntlst[indx]->snapshot = 1;
} else {
webui->cnt->snapshot = 1;
}
} else if (!strcmp(webui->uri_cmd2,"restart")){
/* This is the legacy method...(we can do better than signals..).*/
if (webui->thread_nbr == 0) {
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO, _("httpd is going to restart"));
kill(getpid(),SIGHUP);
webui->cntlst[0]->webcontrol_finish = TRUE;
} else {
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO,
_("httpd is going to restart thread %d"),webui->thread_nbr);
if (webui->cnt->running) {
webui->cnt->event_stop = TRUE;
webui->cnt->finish = 1;
}
webui->cnt->restart = 1;
}
} else if (!strcmp(webui->uri_cmd2,"start")){
if (webui->thread_nbr == 0 && webui->cam_threads > 1) {
do {
webui->cntlst[indx]->pause = 0;
} while (webui->cntlst[++indx]);
} else {
webui->cnt->pause = 0;
}
} else if (!strcmp(webui->uri_cmd2,"pause")){
if (webui->thread_nbr == 0 && webui->cam_threads > 1) {
do {
webui->cntlst[indx]->pause = 1;
} while (webui->cntlst[++indx]);
} else {
webui->cnt->pause = 1;
}
} else if (!strcmp(webui->uri_cmd2,"connection")){
webu_text_connection(webui);
} else if (!strcmp(webui->uri_cmd2,"status")){
webu_text_status(webui);
} else if ((!strcmp(webui->uri_cmd2,"write")) ||
(!strcmp(webui->uri_cmd2,"writeyes"))){
conf_print(webui->cntlst);
} else {
MOTION_LOG(INF, TYPE_STREAM, NO_ERRNO,
_("Invalid action requested: >%s< >%s< >%s<")
, webui->uri_camid, webui->uri_cmd1, webui->uri_cmd2);
return;
}
}
static int webu_process_config_set(struct webui_ctx *webui) {
/* Process the request to change the configuration parameters. Used
* both the html and text interfaces. If the parameter was found, then
* we return 0 otherwise a -1 to tell the calling function whether it
* was a valid parm to change.
*/
int indx, retcd;
char temp_name[WEBUI_LEN_PARM];
/* Search through the depreciated parms and if applicable,
* get the new parameter name so we can check its webcontrol_parms level
*/
snprintf(temp_name, WEBUI_LEN_PARM, "%s", webui->uri_parm1);
indx=0;
while (dep_config_params[indx].name != NULL) {
if (strcmp(dep_config_params[indx].name,webui->uri_parm1) == 0){
snprintf(temp_name, WEBUI_LEN_PARM, "%s", dep_config_params[indx].newname);
break;
}
indx++;
}
/* Ignore any request to change an option that is designated above the
* webcontrol_parms level.
*/
indx=0;
while (config_params[indx].param_name != NULL) {
if (((webui->thread_nbr != 0) && (config_params[indx].main_thread)) ||
(config_params[indx].webui_level > webui->cntlst[0]->conf.webcontrol_parms) ||
(config_params[indx].webui_level == WEBUI_LEVEL_NEVER) ) {
indx++;
continue;
}
if (!strcmp(temp_name, config_params[indx].param_name)) break;
indx++;
}
/* If we found the parm, assign it. If the loop above did not find the parm
* then we ignore the request
*/
if (config_params[indx].param_name != NULL){
if (strlen(webui->uri_parm1) > 0){
/* This is legacy assumption on the pointers being sequential
* We send in the original parm name so it will trigger the depreciated warnings
* and perform any required transformations from old parm to new parm
*/
conf_cmdparse(webui->cntlst + webui->thread_nbr
, webui->uri_parm1, webui->uri_value1);
/*If we are updating vid parms, set the flag to update the device.*/
if (!strcmp(config_params[indx].param_name, "vid_control_params") &&
(webui->cntlst[webui->thread_nbr]->vdev != NULL)){
webui->cntlst[webui->thread_nbr]->vdev->update_parms = TRUE;
}
/* If changing language, do it now */
if (!strcmp(config_params[indx].param_name, "native_language")){
nls_enabled = webui->cntlst[webui->thread_nbr]->conf.native_language;
if (nls_enabled){
MOTION_LOG(INF, TYPE_ALL, NO_ERRNO,_("Native Language : on"));
} else {
MOTION_LOG(INF, TYPE_ALL, NO_ERRNO,_("Native Language : off"));
}
}
} else {
MOTION_LOG(NTC, TYPE_STREAM, NO_ERRNO,_("Set the value to null/zero"));
}
retcd = 0;
} else {
retcd = -1;
}
return retcd;
}
int webu_process_config(struct webui_ctx *webui) {
int retcd;
retcd = 0;
if ((!strcmp(webui->uri_cmd1,"config")) &&
(!strcmp(webui->uri_cmd2,"set"))) {
retcd = webu_process_config_set(webui);
} else if ((!strcmp(webui->uri_cmd1,"config")) &&
(!strcmp(webui->uri_cmd2,"get"))) {
webu_text_get_query(webui);
} else if ((!strcmp(webui->uri_cmd1,"config")) &&
(!strcmp(webui->uri_cmd2,"list"))) {
webu_text_list(webui);
} else {
MOTION_LOG(INF, TYPE_STREAM, NO_ERRNO,
_("Invalid action requested: >%s< >%s< >%s<")
, webui->uri_camid, webui->uri_cmd1, webui->uri_cmd2);
}
return retcd;
}
int webu_process_track(struct webui_ctx *webui) {
/* Call the tracking move functions as requested */
struct coord cent;
int retcd;
if (!strcmp(webui->uri_cmd2, "center")) {
webui->cntlst[webui->thread_nbr]->moved = track_center(webui->cntlst[webui->thread_nbr], 0, 1, 0, 0);
retcd = 0;
} else if (!strcmp(webui->uri_cmd2, "set")) {
if (!strcmp(webui->uri_parm1, "pan")) {
cent.width = webui->cntlst[webui->thread_nbr]->imgs.width;
cent.height = webui->cntlst[webui->thread_nbr]->imgs.height;
cent.x = atoi(webui->uri_value1);
cent.y = 0;
webui->cntlst[webui->thread_nbr]->moved = track_move(webui->cntlst[webui->thread_nbr]
,webui->cntlst[webui->thread_nbr]->video_dev
,¢, &webui->cntlst[webui->thread_nbr]->imgs, 1);
cent.width = webui->cntlst[webui->thread_nbr]->imgs.width;
cent.height = webui->cntlst[webui->thread_nbr]->imgs.height;
cent.x = 0;
cent.y = atoi(webui->uri_value2);
webui->cntlst[webui->thread_nbr]->moved = track_move(webui->cntlst[webui->thread_nbr]
,webui->cntlst[webui->thread_nbr]->video_dev
,¢, &webui->cntlst[webui->thread_nbr]->imgs, 1);
retcd = 0;
} else if (!strcasecmp(webui->uri_parm1, "x")) {
webui->cntlst[webui->thread_nbr]->moved = track_center(webui->cntlst[webui->thread_nbr]
, webui->cntlst[webui->thread_nbr]->video_dev, 1
, atoi(webui->uri_value1), atoi(webui->uri_value2));
retcd = 0;
} else {
retcd = -1;
}
} else {
retcd = -1;
}
return retcd;
}
static void webu_clientip(struct webui_ctx *webui) {
/* Extract the IP of the client that is connecting. When the
* user specifies Motion to use IPV6 and a IPV4 address comes to us
* the IPv4 address is prepended with a ::ffff: We then trim that off
* so we don't confuse our users.
*/
const union MHD_ConnectionInfo *con_info;
char client[WEBUI_LEN_URLI];
const char *ip_dst;
struct sockaddr_in6 *con_socket6;
struct sockaddr_in *con_socket4;
int is_ipv6;
is_ipv6 = FALSE;
if (webui->cnt != NULL ){
if (webui->cnt->conf.webcontrol_ipv6) is_ipv6 = TRUE;
} else {
if (webui->cntlst[0]->conf.webcontrol_ipv6) is_ipv6 = TRUE;
}
con_info = MHD_get_connection_info(webui->connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS);
if (is_ipv6){
con_socket6 = (struct sockaddr_in6 *)con_info->client_addr;
ip_dst = inet_ntop(AF_INET6, &con_socket6->sin6_addr, client, WEBUI_LEN_URLI);
if (ip_dst == NULL){
snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", "Unknown");
} else {
if (strncmp(client,"::ffff:",7) == 0){
snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", client + 7);
} else {
snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", client);
}
}
} else {
con_socket4 = (struct sockaddr_in *)con_info->client_addr;
ip_dst = inet_ntop(AF_INET, &con_socket4->sin_addr, client, WEBUI_LEN_URLI);
if (ip_dst == NULL){
snprintf(webui->clientip, WEBUI_LEN_URLI, "%s", "Unknown");
} else {
snprintf(webui->clientip,WEBUI_LEN_URLI,"%s",client);
}
}
MOTION_LOG(INF,TYPE_ALL, NO_ERRNO, _("Connection from: %s"),webui->clientip);
}
static void webu_hostname(struct webui_ctx *webui, int ctrl) {
/* use the hostname the browser used to connect to us when
* constructing links to the stream ports. If available
* (which it is in all modern browsers) it is more likely to
* work than the result of gethostname(), which is reliant on
* the machine we're running on having it's hostname setup
* correctly and corresponding DNS in place. */
const char *hdr;
char *en_pos;
int host_len;
hdr = MHD_lookup_connection_value (webui->connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_HOST);
if (hdr != NULL){
snprintf(webui->hostname, WEBUI_LEN_PARM, "%s", hdr);
en_pos = strstr(webui->hostname, ":");
if (en_pos != NULL){
host_len = en_pos - webui->hostname + 1;
snprintf(webui->hostname, host_len, "%s", hdr);
}
} else {
gethostname(webui->hostname, WEBUI_LEN_PARM - 1);
}
/* Assign the type of protocol that is associated with the host
* so we can use this protocol as we are building the html page or
* streams.
*/
if (ctrl){
if (webui->cnt->conf.webcontrol_tls){
snprintf(webui->hostproto,6,"%s","https");
} else {
snprintf(webui->hostproto,6,"%s","http");
}
} else {
if (webui->cnt->conf.stream_tls){
snprintf(webui->hostproto,6,"%s","https");
} else {
snprintf(webui->hostproto,6,"%s","http");
}
}
return;
}
static int webu_mhd_digest_fail(struct webui_ctx *webui,int signal_stale) {
/* Create a denied response to user*/
struct MHD_Response *response;
int retcd;
webui->authenticated = FALSE;
response = MHD_create_response_from_buffer(strlen(webui->auth_denied)
,(void *)webui->auth_denied, MHD_RESPMEM_PERSISTENT);
if (response == NULL) return MHD_NO;
retcd = MHD_queue_auth_fail_response(webui->connection, webui->auth_realm
,webui->auth_opaque, response
,(signal_stale == MHD_INVALID_NONCE) ? MHD_YES : MHD_NO);
MHD_destroy_response(response);
return retcd;
}
static int webu_mhd_digest(struct webui_ctx *webui) {
/* Perform the digest authentication. This function gets called a couple of
* times by MHD during the authentication process.
*/
int retcd;
char *user;
/*Get username or prompt for a user/pass */
user = MHD_digest_auth_get_username(webui->connection);
if (user == NULL) {
return webu_mhd_digest_fail(webui, MHD_NO);
}
/* Check for valid user name */
if (strcmp(user, webui->auth_user) != 0){
MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO
,_("Failed authentication from %s"), webui->clientip);
if (user != NULL) free(user);
return webu_mhd_digest_fail(webui, MHD_NO);
}
if (user != NULL) free(user);
/* Check the password as well*/
retcd = MHD_digest_auth_check(webui->connection, webui->auth_realm
, webui->auth_user, webui->auth_pass, 300);
if (retcd == MHD_NO) {
MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO
,_("Failed authentication from %s"), webui->clientip);
}
if ( (retcd == MHD_INVALID_NONCE) || (retcd == MHD_NO) ) {
return webu_mhd_digest_fail(webui, retcd);
}
webui->authenticated = TRUE;
return MHD_YES;
}
static int webu_mhd_basic_fail(struct webui_ctx *webui) {
/* Create a denied response to user*/
struct MHD_Response *response;
int retcd;
webui->authenticated = FALSE;
response = MHD_create_response_from_buffer(strlen(webui->auth_denied)
,(void *)webui->auth_denied, MHD_RESPMEM_PERSISTENT);
if (response == NULL) return MHD_NO;
retcd = MHD_queue_basic_auth_fail_response (webui->connection, webui->auth_realm, response);
MHD_destroy_response(response);
return retcd;
}
static int webu_mhd_basic(struct webui_ctx *webui) {
/* Perform Basic Authentication. */
char *user, *pass;
pass = NULL;
user = NULL;
user = MHD_basic_auth_get_username_password (webui->connection, &pass);
if ((user == NULL) || (pass == NULL)){
if (user != NULL) free(user);
if (pass != NULL) free(pass);
return webu_mhd_basic_fail(webui);
}
if ((strcmp(user, webui->auth_user) != 0) || (strcmp(pass, webui->auth_pass) != 0)) {
MOTION_LOG(ALR, TYPE_STREAM, NO_ERRNO
,_("Failed authentication from %s"),webui->clientip);
if (user != NULL) free(user);
if (pass != NULL) free(pass);
return webu_mhd_basic_fail(webui);
}
if (user != NULL) free(user);
if (pass != NULL) free(pass);
webui->authenticated = TRUE;
return MHD_YES;
}
static void webu_mhd_auth_parse(struct webui_ctx *webui, int ctrl){
int auth_len;
char *col_pos;
/* Parse apart the user:pass provided*/
if (webui->auth_user != NULL) free(webui->auth_user);
if (webui->auth_pass != NULL) free(webui->auth_pass);
webui->auth_user = NULL;
webui->auth_pass = NULL;
if (ctrl){
auth_len = strlen(webui->cnt->conf.webcontrol_authentication);
col_pos = strstr(webui->cnt->conf.webcontrol_authentication,":");
if (col_pos == NULL){
webui->auth_user = mymalloc(auth_len+1);
webui->auth_pass = mymalloc(2);
snprintf(webui->auth_user, auth_len + 1, "%s"
,webui->cnt->conf.webcontrol_authentication);
snprintf(webui->auth_pass, 2, "%s","");
} else {
webui->auth_user = mymalloc(auth_len - strlen(col_pos) + 1);
webui->auth_pass = mymalloc(strlen(col_pos));
snprintf(webui->auth_user, auth_len - strlen(col_pos) + 1, "%s"
,webui->cnt->conf.webcontrol_authentication);
snprintf(webui->auth_pass, strlen(col_pos), "%s", col_pos + 1);