-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnoip2.c
2728 lines (2595 loc) · 75.1 KB
/
noip2.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
/////////////////////////////////////////////////////////////////////////////
/*
no-ip.com dynamic IP update client for Linux
Copyright 2000-2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
written June 2000
copyright transferred from
One Vista Associates
to
Free Software Foundation, Inc.
October 6, 2000 (johna)
+ November 4, 2000
+ updated noip.c and Makefile to build on solaris also
+ collapsed expanded code into strerror()
+ suggested by [email protected]
+ December 2, 2000
+ updated noip.c to build on BSD also
+ changed #include <linux/if.h> to #include <net/if.h>
+ suggested by [email protected]
+ April 27, 2001 (Stephane Neveu [email protected])
+ changed the "SAVEDIPFILE" from /tmp/no-ip_save to
/var/run/no-ip_save
+ added configuration default lookup into /usr/local/etc
if /usr/local/lib doesn't have a configuration file
+ fix output of contextual ourname[hostnum] in the function
handle_dynup_error() instead of the "first" name
+ August 27, 2001 (johna)
+ added GROUP concept
+ removed multiple host/domain name hack (use groups)
+ changed SAVEDIPFILE back to /tmp
(must be root to write in /var/run)
+ November 22, 2002 (johna)
+ changed vsprintf to vsnprintf to avoid buffer overflow
+ Version 2.0 December 2002 (johna -- major rewrite)
+ using shared memory
+ new config file format with autoconfig (-C)
+ multiple instances supported (-M)
+ status available for all instances (-S)
+ can terminate an instance (-K)
+ can toggle debugging for an instance (-D)
+ March 2003 (johna)
+ bumped MAX_NET_DEVS to 24
+ drop root privs after acquiring conf (by Michal Ambroz)
+ added -I interface_name flag (by Clifford Kite)
+ April 2003 (johna)
+ avoid listing IPV6 devices (robc at gmx.de)
+ May 2003 (johna)
+ replaced sleep(x) with select(1,0,0,0,timeout)
+ added getifaddrs() for recent BSD systems (Peter Stromberg)
+ added new SIOCGIFCONF for older BSD systems (Peter Stromberg)
+ November 2003 (johna)
+ added <CR> into all http requests along with <LF>
+ added SIGCHLD handler to reap zombies
+ January 2004 (johna)
+ added location logic and revamped XML parsing
+ added User-Agent field to settings.php request
+ changed to version 2.1
+ January 2004 (johna) version 2.1.1
+ added -u, -p and -x options for LR101 project
+ April 2004 (johna version 2.1.2
+ removed -Y in make install rule
+ August 2005 (johna) version 2.1.3
+ added shm dump code for debugging broken libraries
+ added -z flag to invoke shm dump code
+ February 2006 (johna) version 2.1.4
+ added code to handle new pedantic version of gcc
+ made signed/unsigned char assignments explicit
+ February 21, 2007 - djonas version 2.1.5
+ updated noip2.c: added SkipHeaders() instead of the magic 6 line pass
+ Changed to ip1.dynupdate.no-ip.com for ip retrieval
+ August 2007 (johna) version 2.1.6
+ added fclose() for stdin, stdout & stderr to child
+ made Force_Update work on 30 day intervals
+ August 2007 (johna) version 2.1.7
+ fixed bug introduced in 2.1.6 where errors from multiple
+ instances were not diplayed due to stderr being closed
+ added version number into shared mem and -S display
+ December 2007 (johna) version 2.1.8 (not generally released)
+ reworked forced update code to use 'wget' and the
+ hostactivate.php script at www.no-ip.com
+ I discovered that no-ip.com still sent warning email
+ about unused hosts when their address had not changed even
+ though they had been updated two days ago by this program!
+ November 2008 (johna) still version 2.1.8
+ added check of returned IP address in get_our_visble_IP_addr
+ hardened GetNextLine to prevent possible buffer overflow
+ ... exploit claimed by J. Random Cracker but never demonstrated
+ it relied on DNS subversion and buffer overflow
+ November 2008 (johna) version 2.1.9
+ hardened force_update() to prevent possible buffer overflow
+ hardened autoconf() the same way
+ patch suggested by [email protected]
*/
/////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <termios.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <syslog.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <pwd.h>
#include <time.h>
#ifdef bsd_with_getifaddrs
#include <ifaddrs.h>
#include <net/if_types.h>
#endif
#ifdef bsd
#include <sys/sockio.h>
#include <net/if_types.h>
#endif
#ifdef linux
#ifndef SIOCGIFADDR
#include <linux/sockios.h>
#endif
#endif
#ifdef sun
#include <sys/sockio.h>
#endif
#define DEBUG
#define ENCRYPT 1
#define FORCE_UPDATE 0
#define MAX(x,y) (((x)>(y))?(x):(y))
#define READ_TIMEOUT 90
#define WRITE_TIMEOUT 60
#define CONNECT_TIMEOUT 60
#define FORCE_INTERVAL (1440 * 30) // 30 days in minutes
#define IPLEN 16
#define LINELEN 256
#define BIGBUFLEN 16384
#define NOIP_NAME "dynupdate.no-ip.com"
#define UPD_NAME "www.no-ip.com"
#define UPD_SCRIPT "hostactive.php"
#define NOIP_IP_SCRIPT "ip1.dynupdate.no-ip.com"
#define CLIENT_IP_PORT 8245
#define VERSION "2.1.9"
#define USER_AGENT "User-Agent: Linux-DUC/"VERSION
#define SETTING_SCRIPT "settings.php?"
#define USTRNG "username="
#define PWDSTRNG "&pass="
#if ENCRYPT
#define REQUEST "requestL="
#else
#define REQUEST ""
#endif
#define UPDATE_SCRIPT "ducupdate.php"
#ifdef DEBUG
#define OPTCHARS "CYU:Fc:dD:hp:u:x:SMi:K:I:z"
#else
#define OPTCHARS "CYU:Fc:hp:u:x:SMi:K:I:z"
#endif
#define ARGC 1
#define ARGF (1<<1)
#define ARGY (1<<2)
#define ARGU (1<<3)
#define ARGI (1<<4)
#define ARGu (1<<5)
#define ARGp (1<<6)
#define ARGx (1<<7)
#define ARGD (1<<8)
#define ARGS (1<<9)
#define ARGM (1<<10)
#define ARGK (1<<11)
#define ARGi (1<<12)
#define NODNSGROUP "@@NO_GROUP@@"
#define HOST 1
#define GROUP 2
#define DOMAIN 3
#ifndef PREFIX
#define PREFIX "/usr/local"
#endif
#define CONFIG_FILEPATH PREFIX"/etc"
#define CONFIG_FILENAME PREFIX"/etc/no-ip2.conf"
#define CONFSTRLEN 1024
#define MAX_DEVLEN 16
#define MAX_INSTANCE 4
#define MAX_NET_DEVS 48
#define B64MOD 4
#define CONFIG_MAGIC 0x414a324c
#define NOIP_KEY 0x50494f4e
#define SHMEM_SIZE (MAX_INSTANCE * sizeof(struct INSTANCE))
#define DEFAULT_NAT_INTERVAL 30
#define WGET_PGM "/usr/bin/wget"
#define SPACE ' '
#define EQUALS '='
#define COMMENT '#'
#define COMMA ','
#define ALREADYSET 0
#define SUCCESS 1
#define BADHOST 2
#define BADPASSWD 3
#define BADUSER 4
#define TOSVIOLATE 6
#define PRIVATEIP 7
#define HOSTDISABLED 8
#define HOSTISREDIRECT 9
#define BADGRP 10
#define SUCCESSGRP 11
#define ALREADYSETGRP 12
#define RELEASEDISABLED 99
#define UNKNOWNERR -1
#define FATALERR -1
#define NOHOSTLOOKUP -2
#define SOCKETFAIL -3
#define CONNTIMEOUT -4
#define CONNFAIL -5
#define READTIMEOUT -6
#define READFAIL -7
#define WRITETIMEOUT -8
#define WRITEFAIL -9
#define NOCONFIG -10
#define BADCONFIG1 -11
#define BADCONFIG2 -12
#define BADCONFIG3 -13
#define BADCONFIG4 -14
#define BADCONFIG5 -15
#define BADCONFIG6 -16
#define BADACTIVATE -20
#define CMSG01 "Can't locate configuration file %s. (Try -c). Ending!\n"
#define CMSG02 "'%s' is a badly constructed configuration file. Ending!\n"
#define CMSG03 "'%s' is not a valid configuration file. Ending!\n"
#define CMSG04 "Request elements unreadable in '%s'. Ending!\n"
#define CMSG05 "Checksum error in '%s'. Ending!\n"
#define CMSG06 "No hosts are available for this user."
#define CMSG07 "Go to www.no-ip.com and create some!\n"
#define CMSG08 "Configuration file can NOT be created.\n"
#define CMSG09 "You have entered an incorrect username"
#define CMSG10 "\t-or-"
#define CMSG11 "an incorrect password for this username."
#define CMSG12 "Only one host [%s] is registered to this account."
#define CMSG13 "It will be used."
#define CMSG14 "Only one group [%s] is registered to this account."
#define CMSG16 "Too many hosts and/or groups!"
#define CMSG17 "Please consolidate via www.no-ip.com"
#define CMSG18 "Can't find network device names! Ending\n"
#define CMSG19 "No network devices found! Ending."
#define CMSG20 "Multiple network devices have been detected.\n"
#define CMSG21 "Please select the Internet interface from this list.\n"
#define CMSG22 "By typing the number associated with it."
#define CMSG23 "Too many network devices. Limit is %d"
#define CMSG24 "\nAuto configuration for Linux client of no-ip.com.\n"
#define CMSG25 "Can't create config file (%s)"
#define CMSG25a "Re-run noip, adding '-c configfilename' as a parameter."
#define CMSG26 "Can't rename config file (%s)"
#define CMSG27 "Network must be operational to create configfile. Ending!"
#define CMSG28 "\nNew configuration file '%s' created.\n"
#define CMSG29 "Nothing selected. Configuration file '%s' NOT created.\n"
#define CMSG30 "Please enter the login/email string for no-ip.com "
#define CMSG31 "Please enter the password for user '%s' "
#define CMSG32 " are registered to this account.\n"
#define CMSG33 "Do you wish to have them all updated?[N] (y/N) "
#define CMSG34 "Do you wish to have group [%s] updated?[N] (y/N) "
#define CMSG35 "Do you wish to have host [%s] updated?[N] (y/N) "
#define CMSG36 "Empty request list in '%s'. Ending!"
#define CMSG37 "No hosts or groups are registered to this account.\n"
#define CMSG38 "Please enter an update interval:[%d] "
#define CMSG39 "\nConfiguration file '%s' is in use by process %d.\nEnding!\n"
#define CMSG40 "Do you wish to run something at successful update?[N] (y/N) "
#define CMSG40a "Please enter the script/program name "
#define CMSG41 "Exec path unreadable in '%s'. Ending!\n"
#define CMSG42 "Can't get our visible IP address from %s"
#define CMSG43 "Invalid domain name '%s'"
#define CMSG44 "Invalid host name '%s'"
#define CMSG99 "\nThis client has expired. Please go to http://www.no-ip.com/downloads.php"
#define CMSG99a "to get our most recent version.\n"
#define CMSG100 \
"Error! -C option can only be used with -F -Y -U -I -c -u -p -x options."
#define CMSG101 " Both -u and -p options must be used together."
int debug = 0;
int timed_out = 0;
int background = 1; // new default
int port_to_use = CLIENT_IP_PORT;
int socket_fd = -1;
int config_fd = -1;
int nat = 0;
int interval = 0;
int log2syslog = 0;
int connect_fail = 0;
int offset = 0;
int needs_conf = 0;
int firewallbox = 0;
int forceyes = 0;
int update_cycle = 0;
int show_config = 0;
int shmid = 0;
int multiple_instances = 0;
int debug_toggle = 0;
int kill_proc = 0;
int reqnum = 0;
int prompt_for_executable = 1;
int shm_dump_active = 0;
int Force_Update = FORCE_INTERVAL;
int dummy = 0;
void *shmaddr = NULL;
char *program = NULL;
char *ourname = NULL;
char tmp_filename[LINELEN] = "/tmp/NO-IPXXXXXX";
char *config_filename = NULL;
char *request = NULL;
char *pid_path = NULL;
char *execpath = NULL;
char *supplied_username = NULL;
char *supplied_password = NULL;
char *supplied_executable = NULL;
char IPaddress[IPLEN];
char login[LINELEN];
char password[LINELEN];
char device[LINELEN];
char iface[MAX_DEVLEN];
char dmn[LINELEN];
char answer[LINELEN];
char saved_args[LINELEN];
char buffer[BIGBUFLEN];
struct termios argin, argout;
struct sigaction sig;
struct CONFIG {
char lastIP[IPLEN];
ushort interval; // don't move this (see display_current_config)
ushort chksum;
uint magic;
ushort rlength;
ushort elength;
char count;
char encrypt;
char nat;
char filler;
char device[MAX_DEVLEN];
char requests[0];
char execpath[0];
} *new_config = NULL;
#define CHKSUM_SKIP (sizeof(ushort) + sizeof(ushort) + IPLEN)
int ignore(char *p);
int domains(char *p);
int hosts(char *p);
int xmlerr(char *p);
struct SETTINGS {
int len;
char *keyword;
int (*func)(char *);
} settings[] = {
{ 7, "<domain", domains },
{ 5, "<host", hosts },
{ 1, "<", ignore }, // other objects
{ 0, "", xmlerr } // error msgs
};
struct GROUPS {
char *grp;
int use;
int count;
int ncount;
struct GROUPS *glink;
struct NAMES *nlink;
} *groups = NULL;
struct NAMES {
int use;
char *fqdn;
struct NAMES *link;
} *ns = NULL;
struct INSTANCE {
int pid;
char debug;
char version;
short interval;
char Last_IP_Addr[IPLEN];
char cfilename[LINELEN];
char args[LINELEN - (2 *IPLEN)];
} *my_instance = NULL;
struct SHARED {
struct INSTANCE instance[MAX_INSTANCE];
char banned_version[16];
} *shared = NULL;
unsigned char DecodeTable[] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
-1, 0, 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, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1
};
unsigned char EncodeTable[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
///////////////////////////////////////////////////////////////////////////
void process_options(int argc, char *argv[]);
void update_handler(int signum);
void alarm_handler(int signum);
void sigchld_handler(int signum);
void exit_handler(int signum);
void wake_handler(int signum);
char put_version(char *ver);
char *get_version(unsigned char x);
void save_IP();
void dump_shm(struct shmid_ds *d);
int get_shm_info();
int validate_IP_addr(char *src, char *dest);
void get_our_visible_IPaddr(char *dest);
void getip(char *dest, char *device);
int run_as_background();
int Sleep(int seconds);
int Read(int sock, char *buf, size_t count);
int Write(int fd, char *buf, size_t count);
int Connect(int port);
int converse_with_web_server();
char *despace(char *p);
char url_decode(char *p);
void get_one_config(char *name);
void display_current_config();
void display_one_config(char *req, int interval,
int nat, char *device, char *expath);
int parse_config();
ushort chksum(char *buf, unsigned int size);
void dump_buffer(int x);
int dynamic_update();
int validate_name(int flag, char *name);
int force_update();
int handle_dynup_error(int error_code);
int handle_config_error(int err_code);
void *Malloc(int size);
int get_xml_field(char *name, char *line, char *dest);
void add_to_list(char *gnm, struct NAMES *ns);
int yesno(char *fmt, ...);
int add_to_request(int kind, char *p);
int get_update_selection(int tgrp, int thst);
int GetNextLine(char *dest);
void SkipHeaders();
void url_encode(char *in, char *out);
void get_credentials(char *l, char *p);
void get_device_name(char *d);
void autoconf();
int bencode(const char *s, char *dst);
int bdecode(char *in, char *out);
void Msg(char *fmt, ...);
///////////////////////////////////////////////////////////////////////////
void Usage()
{
fprintf(stderr, "\nUSAGE: %s ", program);
fprintf(stderr, "[ -C [ -F][ -Y][ -U #min]\n\t");
fprintf(stderr, "[ -u username][ -p password][ -x progname]]\n\t");
fprintf(stderr, "[ -c file]");
#ifdef DEBUG
fprintf(stderr, "[ -d][ -D pid]");
#endif
fprintf(stderr, "[ -i addr][ -S][ -M][ -h]");
fprintf(stderr, "\n\nVersion Linux-%s\n", VERSION);
fprintf(stderr, "Options: -C create configuration data\n");
fprintf(stderr, " -F force NAT off\n");
fprintf(stderr, " -Y select all hosts/groups\n");
fprintf(stderr, " -U minutes set update interval\n");
fprintf(stderr, " -u username use supplied username\n");
fprintf(stderr, " -p password use supplied password\n");
fprintf(stderr, " -x executable use supplied executable\n");
fprintf(stderr, " -c config_file use alternate data path\n");
#ifdef DEBUG
fprintf(stderr, " -d increase debug verbosity\n");
fprintf(stderr, " -D processID toggle debug flag for PID\n");
#endif
fprintf(stderr, " -i IPaddress use supplied address\n");
fprintf(stderr, " -I interface use supplied interface\n");
fprintf(stderr, " -S show configuration data\n");
fprintf(stderr, " -M permit multiple instances\n");
fprintf(stderr, " -K processID terminate instance PID\n");
fprintf(stderr, " -z activate shm dump code\n");
fprintf(stderr, " -h help (this text)\n");
}
///////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
char *p;
struct passwd *nobody;
port_to_use = CLIENT_IP_PORT;
timed_out = 0;
sig.sa_flags = 0;
sigemptyset(&sig.sa_mask);
sig.sa_handler = SIG_IGN;
sigaction(SIGHUP,&sig,NULL);
sigaction(SIGPIPE,&sig,NULL);
sigaction(SIGUSR1,&sig,NULL);
sigaction(SIGUSR2,&sig,NULL);
sig.sa_handler = alarm_handler;
sigaction(SIGALRM,&sig,NULL);
p = strrchr(argv[0], '/');
if (p)
program = ++p;
else
program = argv[0];
openlog(program, LOG_PID, LOG_DAEMON);
if ((p = realpath(program, buffer)) == NULL) {
p = program;
}
sprintf(saved_args, "%s", p);
*iface = 0; // no supplied interface yet
process_options(argc, argv);
if (config_filename == NULL)
config_filename = CONFIG_FILENAME;
if (needs_conf) {
autoconf();
exit(0);
}
if (handle_config_error(parse_config()) != SUCCESS)
return -1;
/* drop root privileges after reading config */
if (geteuid() == 0) {
if ((nobody = getpwnam("nobody")) != NULL) { // if "nobody" exists
setgid(nobody->pw_gid);
setegid(nobody->pw_gid);
setuid(nobody->pw_uid);
seteuid(nobody->pw_uid);
}
}
if (*IPaddress != 0) {
if (background) {
Msg("IP address detected on command line.");
Msg("Running in single use mode.");
background = 0;
multiple_instances++; // OK to have another running
}
}
if (background) {
sig.sa_handler = (void *)wake_handler;
sigaction(SIGUSR1,&sig,NULL);
sig.sa_handler = update_handler;
sigaction(SIGUSR2,&sig,NULL);
sig.sa_handler = exit_handler;
sigaction(SIGINT,&sig,NULL);
sigaction(SIGTERM,&sig,NULL);
sigaction(SIGQUIT,&sig,NULL);
sig.sa_handler = (void *)sigchld_handler;
sigaction(SIGCHLD, &sig, NULL);
if (run_as_background() == FATALERR) { // get shmem failure
return FATALERR;
}
} else {
if (get_shm_info() == FATALERR)
return FATALERR;
if (*IPaddress == 0) {
if (nat)
get_our_visible_IPaddr(IPaddress);
else
getip(IPaddress, device);
}
if (*IPaddress == 0) {
Msg("Not connected to Net!");
my_instance->pid = 0; // untag instance
shmdt(shmaddr); // done with our shared memory
return FATALERR;
}
if (dynamic_update() != SUCCESS) {
my_instance->pid = 0; // untag instance
shmdt(shmaddr); // done with our shared memory
return FATALERR;
}
strncpy(my_instance->Last_IP_Addr, IPaddress, IPLEN);
}
save_IP();
free(new_config);
free(request);
return 0;
}
///////////////////////////////////////////////////////////////////////////
void process_options(int argc, char *argv[])
{
extern int optind, opterr;
extern char *optarg;
int c, have_args = 0;
while ((c = getopt(argc, argv, OPTCHARS)) != EOF) {
switch (c) {
case 'C':
needs_conf++;
log2syslog = -1;
have_args |= ARGC;
break;
case 'F':
firewallbox++;
have_args |= ARGF;
break;
case 'Y':
forceyes++;
have_args |= ARGY;
break;
case 'U':
update_cycle = atoi(optarg);
have_args |= ARGU;
break;
case 'c':
config_filename = optarg;
strcat(saved_args, " -c ");
strcat(saved_args, optarg);
break;
#ifdef DEBUG
case 'd':
debug++;
log2syslog = -1;
shm_dump_active++;
strcat(saved_args, " -d");
break;
case 'D':
debug_toggle = atoi(optarg);
have_args |= ARGD;
break;
#endif
case 'u':
supplied_username = optarg;
have_args |= ARGu;
break;
case 'p':
supplied_password = optarg;
have_args |= ARGp;
break;
case 'x':
supplied_executable = optarg;
have_args |= ARGx;
break;
case 'h':
Usage();
exit(0);
case 'S':
show_config++;
log2syslog = -1;
have_args |= ARGS;
break;
case 'M':
multiple_instances++;
have_args |= ARGM;
break;
case 'K':
kill_proc = atoi(optarg);
have_args |= ARGK;
break;
case 'i':
strcpy(IPaddress, optarg);
strcat(saved_args, " -i ");
strcat(saved_args, optarg);
have_args |= ARGi;
break;
case 'I':
strcpy(iface, optarg);
strcat(saved_args, " -I ");
strcat(saved_args, optarg);
have_args |= ARGI;
break;
case 'z':
shm_dump_active++;
break;
default:
Usage();
exit(0);
}
}
if (needs_conf && (have_args > ((ARGx * 2) - 1))){
Msg(CMSG100);
exit(1);
}
if (have_args & ARGu) {
if (!(have_args & ARGp)) {
Msg(CMSG101);
exit(1);
}
}
if (have_args & ARGp) {
if (!(have_args & ARGu)) {
Msg(CMSG101);
exit(1);
}
prompt_for_executable = 0; // don't prompt in auto mode
}
if (debug_toggle && (have_args != ARGD)){
Msg("Error! -D option can't be used with any other options.");
exit(1);
}
if (kill_proc && (have_args != ARGK)){
Msg("Error! -K option can't be used with any other options.");
exit(1);
}
if (show_config && (have_args != ARGS)){
Msg("Error! -S option can't be used with any other options.");
exit(1);
}
if (argc - optind) {
Usage();
exit(1);
}
return;
}
///////////////////////////////////////////////////////////////////////////
void update_handler(int signum) // entered on SIGUSR2
{
Force_Update = -1;
}
///////////////////////////////////////////////////////////////////////////
void alarm_handler(int signum) // entered on SIGALRM
{
timed_out = 1;
}
///////////////////////////////////////////////////////////////////////////
void sigchld_handler(int signum) //entered on death of child (SIGCHLD)
{
int status;
waitpid(-1, &status, WNOHANG | WUNTRACED);
}
///////////////////////////////////////////////////////////////////////////
void exit_handler(int signum) //entered on SIGINT, SIGTERM & SIGQUIT
{
background = 0;
// don't reset handler -- exit on second signal
}
///////////////////////////////////////////////////////////////////////////
void wake_handler(int signum) //entered on SIGUSR1
{ // used to wake sleeping process only
dummy++;
}
///////////////////////////////////////////////////////////////////////////
char put_version(char *ver)
{
char *p, x;
p = ver + 2; // step over 2.
x = (*p -'0') * 10; // get major num
p += 2;
x += (*p -'0'); // get minor num
return x;
}
///////////////////////////////////////////////////////////////////////////
char *get_version(unsigned char z)
{
int x, y;
static char vers[10];
if (z > 15) {
x = z / 10;
y = z % 10;
sprintf(vers, "2.%d.%d", x, y);
} else {
strcpy(vers, "unknown");
}
return vers;
}
///////////////////////////////////////////////////////////////////////////
void save_IP()
{
// config_file is opened as root, read-only permission has no effect
if (access(config_filename, W_OK) ==0) {
lseek(config_fd, 0, SEEK_SET); // re-position at start of file
write(config_fd, my_instance->Last_IP_Addr,
strlen(my_instance->Last_IP_Addr) + 1);
#ifdef DEBUG
if (my_instance->debug)
Msg("Saving Last_IP_Addr %s", my_instance->Last_IP_Addr);
} else {
if (my_instance->debug)
Msg("Read-only file '%s' prevents saving Last_IP_Addr %s",
config_filename, my_instance->Last_IP_Addr);
#endif
}
close(config_fd);
my_instance->pid = 0; // untag instance
shmdt(shmaddr); // done with our shared memory
exit(0);
}
///////////////////////////////////////////////////////////////////////////
void getip(char *p, char *device)
{
int fd;
struct sockaddr_in *sin;
struct ifreq ifr;
struct in_addr z;
*p = 0; // remove old address
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
Msg("Can't talk to kernel! (%d)\n", errno);
return;
}
strcpy(ifr.ifr_name, device);
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
Msg("Can't get status for %s. (%d)\n", device, errno);
close(fd);
return;
}
if ((ifr.ifr_flags & IFF_UP) == 0) {
// No longer print message when interface down (johna 6-28-00)
// Msg("Interface %s not active.\n", device);
close(fd);
return;
}
if (ioctl(fd, SIOCGIFADDR, &ifr) != 0) {
Msg("Can't get IP address for %s. (%d)\n", device, errno);
close(fd);
return;
}
close(fd);
sin = (struct sockaddr_in *)&ifr.ifr_addr;
z = sin->sin_addr;
strcpy(p, inet_ntoa(z));
#ifdef DEBUG
if (my_instance ? my_instance->debug : debug)
fprintf(stderr,"! Our IP address is %s\n", p);
#endif
}
/////////////////////////////////////////////////////////////////////////
int config_file_inuse()
{
int i, retval, xid;
void *xaddr;
struct INSTANCE *is;
struct SHARED *shr;
if ((xid = shmget(NOIP_KEY, SHMEM_SIZE, 0)) == -1)
return 0;
if ((xaddr = shmat(xid, 0, 0)) == (void *)-1)
return 0;
retval = 0;
shr = (struct SHARED *)xaddr;
for (i=0; i<MAX_INSTANCE; i++) {
is = &shr->instance[i];
if (is->pid != 0) {
if (strcmp(is->cfilename, config_filename) == 0) {
if (kill(is->pid, 0) == 0) { // does process exist?
retval = is->pid; // file in use
break;
}
}
}
}
shmdt(xaddr);
return retval;
}
/////////////////////////////////////////////////////////////////////////
void dump_shm(struct shmid_ds *d)
{
if (shm_dump_active) {
fprintf(stderr, "\nShared memory info:\n");
fprintf(stderr, "\tsize of struct shmid_ds is %d\n", (int)sizeof(struct shmid_ds));
fprintf(stderr, "\tmode %o\n", d->shm_perm.mode & 0x1ff);
fprintf(stderr, "\tuid %d, gid %d\n",
d->shm_perm.uid, d->shm_perm.gid);
fprintf(stderr, "\tcreated by uid %d, gid %d\n",
d->shm_perm.cuid, d->shm_perm.cgid);
fprintf(stderr, "\tsegment size = %d\n", (int)d->shm_segsz);
fprintf(stderr, "\tlast attach time = %s", ctime(&d->shm_atime));
fprintf(stderr, "\tlast detach time = %s", ctime(&d->shm_dtime));
fprintf(stderr, "\tcreation time = %s", ctime(&d->shm_ctime));
fprintf(stderr, "\tnumber of attached procs = %d\n", (int)d->shm_nattch);
fprintf(stderr, "\tcreation process id = %d\n", d->shm_cpid);
fprintf(stderr, "\tlast user process id = %d\n", d->shm_lpid);
}
}
/////////////////////////////////////////////////////////////////////////
int get_shm_info()
{
int i, flag;
struct shmid_ds ds;
struct INSTANCE *is;
flag = IPC_CREAT | 0666;
if ((shmid = shmget(NOIP_KEY, SHMEM_SIZE, flag)) == -1) {
Msg("Can't get shared memory. (%s) Ending!", strerror(errno));
return FATALERR;
}
if (shmctl(shmid, IPC_STAT, &ds) != 0) {
Msg("Can't stat shared memory. (%s) Ending!", strerror(errno));
return FATALERR;
}
if ((ds.shm_nattch > 0) && (!multiple_instances)) {
Msg("One %s process is already active,", program);
Msg("and the multiple instance flag (-M) is not set.");
dump_shm(&ds);
return FATALERR;
}
if (ds.shm_nattch >= MAX_INSTANCE) {
Msg("Too many %s processes active. Ending!", program);
dump_shm(&ds);
return FATALERR;
}
if ((shmaddr = shmat(shmid, 0, 0)) == (void *)-1) {
Msg("Can't attach shared memory. (%s) Ending!", strerror(errno));
dump_shm(&ds);
return FATALERR;
}
shared = (struct SHARED *)shmaddr;
if (strcmp(shared->banned_version, VERSION) == 0) {
Msg(CMSG99);
Msg(CMSG99a);
shmdt(shmaddr);
shared = NULL;
return FATALERR;
}
for (i=0; i<MAX_INSTANCE; i++) {
is = &shared->instance[i];
if (is->pid != 0) {
if (strcmp(is->cfilename, config_filename) == 0) {
if (kill(is->pid, 0) == -1) { // does process exist?
if (errno == ESRCH) { // no
Msg("Recovering dead process %d shmem slot", is->pid);
my_instance = is; // take over slot
break;
}
}
if (!background && *IPaddress) { // update running daemon
strncpy(is->Last_IP_Addr, IPaddress, IPLEN);
continue;
}
Msg("Configuration '%s' already in use", is->cfilename);
Msg("by process %d. Ending!", is->pid);