forked from haakonnessjoen/MAC-Telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mactelnetd.c
1181 lines (981 loc) · 32.9 KB
/
mactelnetd.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
/*
Mac-Telnet - Connect to RouterOS or mactelnetd devices via MAC address
Copyright (C) 2010, Håkon Nessjøen <[email protected]>
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 of the License, 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.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#if defined(__linux__)
#define _XOPEN_SOURCE 600
#endif
#define _BSD_SOURCE
#include <libintl.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#if defined(__FreeBSD__)
#include <paths.h>
#include <sys/endian.h>
#else
#include <endian.h>
#endif
#include <time.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#if !defined(__FreeBSD__)
#include <netinet/ether.h>
#endif
#include <sys/time.h>
#include <time.h>
#include <sys/socket.h>
#include <string.h>
#ifdef __linux__
#include <linux/if_ether.h>
#include <sys/mman.h>
#else
#include <sys/time.h>
#endif
#include <sys/ioctl.h>
#include <sys/stat.h>
#if defined(__linux__)
#include <sys/sysinfo.h>
#endif
#include <pwd.h>
#if defined(__FreeBSD__)
#include <sys/time.h>
/* This is the really Posix interface the Linux code should have used !!*/
#include <utmpx.h>
#else
#include <utmp.h>
#endif
#include <syslog.h>
#include <sys/utsname.h>
#include "md5.h"
#include "protocol.h"
#include "console.h"
#include "interfaces.h"
#include "users.h"
#include "config.h"
#include "utlist.h"
#define PROGRAM_NAME "MAC-Telnet Daemon"
#define MAX_INSOCKETS 100
#define MT_INTERFACE_LEN 128
/* Max ~5 pings per second */
#define MT_MAXPPS MT_MNDP_BROADCAST_INTERVAL * 5
#define _(String) gettext (String)
#define gettext_noop(String) String
static int sockfd;
static int insockfd;
static int mndpsockfd;
static int pings = 0;
struct net_interface *interfaces = NULL;
static int use_raw_socket = 0;
static struct in_addr sourceip;
static struct in_addr destip;
static int sourceport;
static time_t last_mndp_time = 0;
/* Protocol data direction */
unsigned char mt_direction_fromserver = 1;
/* Anti-timeout is every 10 seconds. Give up after 15. */
#define MT_CONNECTION_TIMEOUT 15
/* Connection states */
enum mt_connection_state {
STATE_AUTH,
STATE_CLOSED,
STATE_ACTIVE
};
/** Connection struct */
struct mt_connection {
struct net_interface *interface;
char interface_name[256];
unsigned short seskey;
unsigned int incounter;
unsigned int outcounter;
unsigned int lastack;
time_t lastdata;
int terminal_mode;
enum mt_connection_state state;
int ptsfd;
int slavefd;
int pid;
int wait_for_ack;
int have_enckey;
char username[30];
unsigned char trypassword[17];
unsigned char srcip[IPV4_ALEN];
unsigned char srcmac[ETH_ALEN];
unsigned short srcport;
unsigned char dstmac[ETH_ALEN];
unsigned char enckey[16];
unsigned short terminal_width;
unsigned short terminal_height;
char terminal_type[30];
struct mt_connection *prev;
struct mt_connection *next;
};
static void uwtmp_login(struct mt_connection *);
static void uwtmp_logout(struct mt_connection *);
static struct mt_connection *connections_head = NULL;
static void list_add_connection(struct mt_connection *conn) {
DL_APPEND(connections_head, conn);
}
static void list_remove_connection(struct mt_connection *conn) {
if (connections_head == NULL) {
return;
}
if (conn->state == STATE_ACTIVE && conn->ptsfd > 0) {
close(conn->ptsfd);
}
if (conn->state == STATE_ACTIVE && conn->slavefd > 0) {
close(conn->slavefd);
}
uwtmp_logout(conn);
DL_DELETE(connections_head, conn);
free(conn);
}
static struct mt_connection *list_find_connection(unsigned short seskey, unsigned char *srcmac) {
struct mt_connection *p;
DL_FOREACH(connections_head, p) {
if (p->seskey == seskey && memcmp(srcmac, p->srcmac, ETH_ALEN) == 0) {
return p;
}
}
return NULL;
}
static struct net_interface *find_socket(unsigned char *mac) {
struct net_interface *interface;
DL_FOREACH(interfaces, interface) {
if (memcmp(mac, interface->mac_addr, ETH_ALEN) == 0) {
return interface;
}
}
return NULL;
}
static void setup_sockets() {
struct net_interface *interface;
DL_FOREACH(interfaces, interface) {
int optval = 1;
struct sockaddr_in si_me;
struct ether_addr *mac = (struct ether_addr *)&(interface->mac_addr);
if (!interface->has_mac) {
continue;
}
if (!use_raw_socket) {
interface->socketfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (interface->socketfd < 0) {
continue;
}
if (setsockopt(interface->socketfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof (optval))==-1) {
perror("SO_BROADCAST");
continue;
}
setsockopt(interface->socketfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
/* Initialize receiving socket on the device chosen */
si_me.sin_family = AF_INET;
si_me.sin_port = htons(MT_MACTELNET_PORT);
memcpy(&(si_me.sin_addr.s_addr), interface->ipv4_addr, IPV4_ALEN);
if (bind(interface->socketfd, (struct sockaddr *)&si_me, sizeof(si_me))==-1) {
fprintf(stderr, _("Error binding to %s:%d, %s\n"), inet_ntoa(si_me.sin_addr), sourceport, strerror(errno));
continue;
}
}
syslog(LOG_NOTICE, _("Listening on %s for %s\n"), interface->name, ether_ntoa(mac));
}
}
static int send_udp(const struct mt_connection *conn, const struct mt_packet *packet) {
if (use_raw_socket) {
return net_send_udp(sockfd, conn->interface, conn->dstmac, conn->srcmac, &sourceip, sourceport, &destip, conn->srcport, packet->data, packet->size);
} else {
/* Init SendTo struct */
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(conn->srcport);
socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
return sendto(conn->interface->socketfd, packet->data, packet->size, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
}
}
static int send_special_udp(struct net_interface *interface, unsigned short port, const struct mt_packet *packet) {
unsigned char dstmac[ETH_ALEN];
if (use_raw_socket) {
memset(dstmac, 0xff, ETH_ALEN);
return net_send_udp(sockfd, interface, interface->mac_addr, dstmac, (const struct in_addr *)&interface->ipv4_addr, port, &destip, port, packet->data, packet->size);
} else {
/* Init SendTo struct */
struct sockaddr_in socket_address;
socket_address.sin_family = AF_INET;
socket_address.sin_port = htons(port);
socket_address.sin_addr.s_addr = htonl(INADDR_BROADCAST);
return sendto(interface->socketfd, packet->data, packet->size, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
}
}
static void display_motd() {
FILE *fp;
int c;
if ((fp = fopen("/etc/motd", "r"))) {
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
}
}
static void display_nologin() {
FILE *fp;
int c;
if ((fp = fopen(_PATH_NOLOGIN, "r"))) {
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
}
}
static void uwtmp_login(struct mt_connection *conn) {
#if defined(__FreeBSD__)
struct utmpx utent;
#else
struct utmp utent;
#endif
pid_t pid;
pid = getpid();
char *line = ttyname(conn->slavefd);
if (strncmp(line, "/dev/", 5) == 0) {
line += 5;
}
/* Setup utmp struct */
memset((void *) &utent, 0, sizeof(utent));
utent.ut_type = USER_PROCESS;
utent.ut_pid = pid;
strncpy(utent.ut_user, conn->username, sizeof(utent.ut_user));
strncpy(utent.ut_line, line, sizeof(utent.ut_line));
strncpy(utent.ut_id, utent.ut_line + 3, sizeof(utent.ut_id));
strncpy(utent.ut_host,
ether_ntoa((const struct ether_addr *)conn->srcmac),
sizeof(utent.ut_host));
#if defined(__FreeBSD__)
gettimeofday(&utent.ut_tv, NULL);
#else
time((time_t *)&(utent.ut_time));
#endif
/* Update utmp and/or wtmp */
#if defined(__FreeBSD__)
setutxent();
pututxline(&utent);
endutxent();
#else
setutent();
pututline(&utent);
endutent();
updwtmp(_PATH_WTMP, &utent);
#endif
}
static void uwtmp_logout(struct mt_connection *conn) {
if (conn->pid > 0) {
#if defined(__FreeBSD__)
struct utmpx *utentp;
struct utmpx utent;
setutxent();
#else
struct utmp *utentp;
struct utmp utent;
setutent();
#endif
#if defined(__FreeBSD__)
while ((utentp = getutxent()) != NULL) {
#else
while ((utentp = getutent()) != NULL) {
#endif
if (utentp->ut_pid == conn->pid && utentp->ut_id) {
break;
}
}
if (utentp) {
utent = *utentp;
utent.ut_type = DEAD_PROCESS;
utent.ut_tv.tv_sec = time(NULL);
#if defined(__FreeBSD__)
pututxline(&utent);
endutxent();
#else
pututline(&utent);
endutent();
updwtmp(_PATH_WTMP, &utent);
#endif
}
}
}
static void abort_connection(struct mt_connection *curconn, struct mt_mactelnet_hdr *pkthdr, char *message) {
struct mt_packet pdata;
init_packet(&pdata, MT_PTYPE_DATA, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter);
add_control_packet(&pdata, MT_CPTYPE_PLAINDATA, message, strlen(message));
send_udp(curconn, &pdata);
/* Make connection time out; lets the previous message get acked before disconnecting */
curconn->state = STATE_CLOSED;
init_packet(&pdata, MT_PTYPE_END, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter);
send_udp(curconn, &pdata);
}
static void user_login(struct mt_connection *curconn, struct mt_mactelnet_hdr *pkthdr) {
struct mt_packet pdata;
unsigned char md5sum[17];
char md5data[100];
struct mt_credentials *user;
char *slavename;
/* Reparse user file before each login */
read_userfile();
if ((user = find_user(curconn->username)) != NULL) {
md5_state_t state;
#if defined(__linux__) && defined(_POSIX_MEMLOCK_RANGE)
mlock(md5data, sizeof(md5data));
mlock(md5sum, sizeof(md5sum));
if (user->password != NULL) {
mlock(user->password, strlen(user->password));
}
#endif
/* Concat string of 0 + password + encryptionkey */
md5data[0] = 0;
strncpy(md5data + 1, user->password, 82);
memcpy(md5data + 1 + strlen(user->password), curconn->enckey, 16);
/* Generate md5 sum of md5data with a leading 0 */
md5_init(&state);
md5_append(&state, (const md5_byte_t *)md5data, strlen(user->password) + 17);
md5_finish(&state, (md5_byte_t *)md5sum + 1);
md5sum[0] = 0;
init_packet(&pdata, MT_PTYPE_DATA, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter);
curconn->outcounter += add_control_packet(&pdata, MT_CPTYPE_END_AUTH, NULL, 0);
send_udp(curconn, &pdata);
if (curconn->state == STATE_ACTIVE) {
return;
}
}
if (user == NULL || memcmp(md5sum, curconn->trypassword, 17) != 0) {
syslog(LOG_NOTICE, _("(%d) Invalid login by %s."), curconn->seskey, curconn->username);
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("Login failed, incorrect username or password\r\n"));
/* TODO: should wait some time (not with sleep) before returning, to minimalize brute force attacks */
return;
}
/* User is logged in */
curconn->state = STATE_ACTIVE;
/* Enter terminal mode */
curconn->terminal_mode = 1;
/* Open pts handle */
curconn->ptsfd = posix_openpt(O_RDWR);
if (curconn->ptsfd == -1 || grantpt(curconn->ptsfd) == -1 || unlockpt(curconn->ptsfd) == -1) {
syslog(LOG_ERR, "posix_openpt: %s", strerror(errno));
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("Terminal error\r\n"));
return;
}
/* Get file path for our pts */
slavename = ptsname(curconn->ptsfd);
if (slavename != NULL) {
pid_t pid;
struct stat sb;
struct passwd *user = (struct passwd *)malloc(sizeof(struct passwd));
struct passwd *tmpuser=user;
char *buffer = malloc(1024);
if (user == NULL || buffer == NULL) {
syslog(LOG_CRIT, _("(%d) Error allocating memory."), curconn->seskey);
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("System error, out of memory\r\n"));
return;
}
if (getpwnam_r(curconn->username, user, buffer, 1024, &tmpuser) != 0) {
syslog(LOG_WARNING, _("(%d) Login ok, but local user not accessible (%s)."), curconn->seskey, curconn->username);
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("Local user not accessible\r\n"));
free(user);
free(buffer);
return;
}
/* Change the owner of the slave pts */
chown(slavename, user->pw_uid, user->pw_gid);
curconn->slavefd = open(slavename, O_RDWR);
if (curconn->slavefd == -1) {
syslog(LOG_ERR, _("Error opening %s: %s"), slavename, strerror(errno));
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("Error opening terminal\r\n"));
list_remove_connection(curconn);
return;
}
if ((pid = fork()) == 0) {
struct net_interface *interface;
/* Add login information to utmp/wtmp */
uwtmp_login(curconn);
syslog(LOG_INFO, _("(%d) User %s logged in."), curconn->seskey, curconn->username);
/* Initialize terminal environment */
setenv("USER", user->pw_name, 1);
setenv("HOME", user->pw_dir, 1);
setenv("SHELL", user->pw_shell, 1);
setenv("TERM", curconn->terminal_type, 1);
close(sockfd);
close(insockfd);
DL_FOREACH(interfaces, interface) {
if (interface->socketfd > 0) {
close(interface->socketfd);
}
}
setsid();
/* Don't let shell process inherit slavefd */
fcntl (curconn->slavefd, F_SETFD, FD_CLOEXEC);
close(curconn->ptsfd);
/* Redirect STDIN/STDIO/STDERR */
close(0);
dup(curconn->slavefd);
close(1);
dup(curconn->slavefd);
close(2);
dup(curconn->slavefd);
/* Set controlling terminal */
ioctl(0, TIOCSCTTY, 1);
tcsetpgrp(0, getpid());
/* Set user id/group id */
if ((setgid(user->pw_gid) != 0) || (setuid(user->pw_uid) != 0)) {
syslog(LOG_ERR, _("(%d) Could not log in %s (%d:%d): setuid/setgid: %s"), curconn->seskey, curconn->username, user->pw_uid, user->pw_gid, strerror(errno));
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
abort_connection(curconn, pkthdr, _("Internal error\r\n"));
exit(0);
}
/* Abort login if /etc/nologin exists */
if (stat(_PATH_NOLOGIN, &sb) == 0 && getuid() != 0) {
syslog(LOG_NOTICE, _("(%d) User %s disconnected with " _PATH_NOLOGIN " message."), curconn->seskey, curconn->username);
display_nologin();
curconn->state = STATE_CLOSED;
init_packet(&pdata, MT_PTYPE_END, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter);
send_udp(curconn, &pdata);
exit(0);
}
/* Display MOTD */
display_motd();
chdir(user->pw_dir);
/* Spawn shell */
/* TODO: Maybe use "login -f USER" instead? renders motd and executes shell correctly for system */
execl(user->pw_shell, user->pw_shell, "-", (char *) 0);
exit(0); // just to be sure.
}
free(user);
free(buffer);
close(curconn->slavefd);
curconn->pid = pid;
set_terminal_size(curconn->ptsfd, curconn->terminal_width, curconn->terminal_height);
}
}
static void handle_data_packet(struct mt_connection *curconn, struct mt_mactelnet_hdr *pkthdr, int data_len) {
struct mt_mactelnet_control_hdr cpkt;
struct mt_packet pdata;
unsigned char *data = pkthdr->data;
int got_user_packet = 0;
int got_pass_packet = 0;
int got_width_packet = 0;
int got_height_packet = 0;
int success;
/* Parse first control packet */
success = parse_control_packet(data, data_len - MT_HEADER_LEN, &cpkt);
while (success) {
if (cpkt.cptype == MT_CPTYPE_BEGINAUTH) {
int plen,i;
if (!curconn->have_enckey) {
for (i = 0; i < 16; ++i) {
curconn->enckey[i] = rand() % 256;
}
curconn->have_enckey = 1;
memset(curconn->trypassword, 0, sizeof(curconn->trypassword));
}
init_packet(&pdata, MT_PTYPE_DATA, pkthdr->dstaddr, pkthdr->srcaddr, pkthdr->seskey, curconn->outcounter);
plen = add_control_packet(&pdata, MT_CPTYPE_ENCRYPTIONKEY, (curconn->enckey), 16);
curconn->outcounter += plen;
send_udp(curconn, &pdata);
} else if (cpkt.cptype == MT_CPTYPE_USERNAME) {
memcpy(curconn->username, cpkt.data, cpkt.length > 29 ? 29 : cpkt.length);
curconn->username[cpkt.length > 29 ? 29 : cpkt.length] = 0;
got_user_packet = 1;
} else if (cpkt.cptype == MT_CPTYPE_TERM_WIDTH) {
unsigned short width;
memcpy(&width, cpkt.data, 2);
curconn->terminal_width = le16toh(width);
got_width_packet = 1;
} else if (cpkt.cptype == MT_CPTYPE_TERM_HEIGHT) {
unsigned short height;
memcpy(&height, cpkt.data, 2);
curconn->terminal_height = le16toh(height);
got_height_packet = 1;
} else if (cpkt.cptype == MT_CPTYPE_TERM_TYPE) {
memcpy(curconn->terminal_type, cpkt.data, cpkt.length > 29 ? 29 : cpkt.length);
curconn->terminal_type[cpkt.length > 29 ? 29 : cpkt.length] = 0;
} else if (cpkt.cptype == MT_CPTYPE_PASSWORD) {
#if defined(__linux__) && defined(_POSIX_MEMLOCK_RANGE)
mlock(curconn->trypassword, 17);
#endif
memcpy(curconn->trypassword, cpkt.data, 17);
got_pass_packet = 1;
} else if (cpkt.cptype == MT_CPTYPE_PLAINDATA) {
/* relay data from client to shell */
if (curconn->state == STATE_ACTIVE && curconn->ptsfd != -1) {
write(curconn->ptsfd, cpkt.data, cpkt.length);
}
} else {
syslog(LOG_WARNING, _("(%d) Unhandeled control packet type: %d"), curconn->seskey, cpkt.cptype);
}
/* Parse next control packet */
success = parse_control_packet(NULL, 0, &cpkt);
}
if (got_user_packet && got_pass_packet) {
user_login(curconn, pkthdr);
}
if (curconn->state == STATE_ACTIVE && (got_width_packet || got_height_packet)) {
set_terminal_size(curconn->ptsfd, curconn->terminal_width, curconn->terminal_height);
}
}
static void handle_packet(unsigned char *data, int data_len, const struct sockaddr_in *address) {
struct mt_mactelnet_hdr pkthdr;
struct mt_connection *curconn = NULL;
struct mt_packet pdata;
struct net_interface *interface;
parse_packet(data, &pkthdr);
/* Drop packets not belonging to us */
if ((interface = find_socket(pkthdr.dstaddr)) < 0) {
return;
}
switch (pkthdr.ptype) {
case MT_PTYPE_PING:
if (pings++ > MT_MAXPPS) {
break;
}
init_pongpacket(&pdata, (unsigned char *)&(pkthdr.dstaddr), (unsigned char *)&(pkthdr.srcaddr));
add_packetdata(&pdata, pkthdr.data - 4, data_len - (MT_HEADER_LEN - 4));
{
if (index >= 0) {
send_special_udp(interface, MT_MACTELNET_PORT, &pdata);
}
}
break;
case MT_PTYPE_SESSIONSTART:
curconn = list_find_connection(pkthdr.seskey, (unsigned char *)&(pkthdr.srcaddr));
if (curconn != NULL) {
/* Ignore multiple session starts from the same sender, this can be same mac but different interface */
break;
}
syslog(LOG_DEBUG, _("(%d) New connection from %s."), pkthdr.seskey, ether_ntoa((struct ether_addr*)&(pkthdr.srcaddr)));
curconn = calloc(1, sizeof(struct mt_connection));
curconn->seskey = pkthdr.seskey;
curconn->lastdata = time(NULL);
curconn->state = STATE_AUTH;
curconn->interface = interface;
strncpy(curconn->interface_name, interface->name, 254);
curconn->interface_name[255] = '\0';
memcpy(curconn->srcmac, pkthdr.srcaddr, ETH_ALEN);
memcpy(curconn->srcip, &(address->sin_addr), IPV4_ALEN);
curconn->srcport = htons(address->sin_port);
memcpy(curconn->dstmac, pkthdr.dstaddr, ETH_ALEN);
list_add_connection(curconn);
init_packet(&pdata, MT_PTYPE_ACK, pkthdr.dstaddr, pkthdr.srcaddr, pkthdr.seskey, pkthdr.counter);
send_udp(curconn, &pdata);
break;
case MT_PTYPE_END:
curconn = list_find_connection(pkthdr.seskey, (unsigned char *)&(pkthdr.srcaddr));
if (curconn == NULL) {
break;
}
if (curconn->state != STATE_CLOSED) {
init_packet(&pdata, MT_PTYPE_END, pkthdr.dstaddr, pkthdr.srcaddr, pkthdr.seskey, pkthdr.counter);
send_udp(curconn, &pdata);
}
syslog(LOG_DEBUG, _("(%d) Connection closed."), curconn->seskey);
list_remove_connection(curconn);
return;
case MT_PTYPE_ACK:
curconn = list_find_connection(pkthdr.seskey, (unsigned char *)&(pkthdr.srcaddr));
if (curconn == NULL) {
break;
}
if (pkthdr.counter <= curconn->outcounter) {
curconn->wait_for_ack = 0;
curconn->lastack = pkthdr.counter;
}
if (time(0) - curconn->lastdata > 9 || pkthdr.counter == curconn->lastack) {
// Answer to anti-timeout packet
init_packet(&pdata, MT_PTYPE_ACK, pkthdr.dstaddr, pkthdr.srcaddr, pkthdr.seskey, pkthdr.counter);
send_udp(curconn, &pdata);
}
curconn->lastdata = time(NULL);
return;
case MT_PTYPE_DATA:
curconn = list_find_connection(pkthdr.seskey, (unsigned char *)&(pkthdr.srcaddr));
if (curconn == NULL) {
break;
}
curconn->lastdata = time(NULL);
/* ack the data packet */
init_packet(&pdata, MT_PTYPE_ACK, pkthdr.dstaddr, pkthdr.srcaddr, pkthdr.seskey, pkthdr.counter + (data_len - MT_HEADER_LEN));
send_udp(curconn, &pdata);
/* Accept first packet, and all packets greater than incounter, and if counter has
wrapped around. */
if (curconn->incounter == 0 || pkthdr.counter > curconn->incounter || (curconn->incounter - pkthdr.counter) > 16777216) {
curconn->incounter = pkthdr.counter;
} else {
/* Ignore double or old packets */
return;
}
handle_data_packet(curconn, &pkthdr, data_len);
break;
default:
if (curconn) {
syslog(LOG_WARNING, _("(%d) Unhandeled packet type: %d"), curconn->seskey, pkthdr.ptype);
init_packet(&pdata, MT_PTYPE_ACK, pkthdr.dstaddr, pkthdr.srcaddr, pkthdr.seskey, pkthdr.counter);
send_udp(curconn, &pdata);
}
}
if (0 && curconn != NULL) {
printf("Packet, incounter %d, outcounter %d\n", curconn->incounter, curconn->outcounter);
}
}
static void print_version() {
fprintf(stderr, PROGRAM_NAME " " PROGRAM_VERSION "\n");
}
void mndp_broadcast() {
struct mt_packet pdata;
struct utsname s_uname;
struct net_interface *interface;
unsigned int uptime;
#ifdef __linux__
struct sysinfo s_sysinfo;
if (sysinfo(&s_sysinfo) != 0) {
return;
}
/* Seems like ping uptime is transmitted as little endian? */
uptime = htole32(s_sysinfo.uptime);
#else
struct timespec ts;
if (clock_gettime(CLOCK_UPTIME, &ts) != -1) {
uptime = htole32(((unsigned int)ts.tv_sec));
}
#endif
if (uname(&s_uname) != 0) {
return;
}
DL_FOREACH(interfaces, interface) {
struct mt_mndp_hdr *header = (struct mt_mndp_hdr *)&(pdata.data);
if (interface->has_mac == 0) {
continue;
}
mndp_init_packet(&pdata, 0, 1);
mndp_add_attribute(&pdata, MT_MNDPTYPE_ADDRESS, interface->mac_addr, ETH_ALEN);
mndp_add_attribute(&pdata, MT_MNDPTYPE_IDENTITY, s_uname.nodename, strlen(s_uname.nodename));
mndp_add_attribute(&pdata, MT_MNDPTYPE_VERSION, s_uname.release, strlen(s_uname.release));
mndp_add_attribute(&pdata, MT_MNDPTYPE_PLATFORM, PLATFORM_NAME, strlen(PLATFORM_NAME));
mndp_add_attribute(&pdata, MT_MNDPTYPE_HARDWARE, s_uname.machine, strlen(s_uname.machine));
mndp_add_attribute(&pdata, MT_MNDPTYPE_TIMESTAMP, &uptime, 4);
mndp_add_attribute(&pdata, MT_MNDPTYPE_SOFTID, MT_SOFTID_MACTELNET, strlen(MT_SOFTID_MACTELNET));
mndp_add_attribute(&pdata, MT_MNDPTYPE_IFNAME, interface->name, strlen(interface->name));
header->cksum = in_cksum((unsigned short *)&(pdata.data), pdata.size);
send_special_udp(interface, MT_MNDP_PORT, &pdata);
}
}
void sigterm_handler() {
struct mt_connection *p;
struct mt_packet pdata;
struct net_interface *interface, *tmp;
/*_ Please include both \r and \n in translation, this is needed for the terminal emulator. */
char message[] = gettext_noop("\r\n\r\nDaemon shutting down.\r\n");
syslog(LOG_NOTICE, _("Daemon shutting down"));
DL_FOREACH(connections_head, p) {
if (p->state == STATE_ACTIVE) {
init_packet(&pdata, MT_PTYPE_DATA, p->interface->mac_addr, p->srcmac, p->seskey, p->outcounter);
add_control_packet(&pdata, MT_CPTYPE_PLAINDATA, _(message), strlen(_(message)));
send_udp(p, &pdata);
init_packet(&pdata, MT_PTYPE_END, p->interface->mac_addr, p->srcmac, p->seskey, p->outcounter);
send_udp(p, &pdata);
}
}
/* Doesn't hurt to tidy up */
close(sockfd);
close(insockfd);
if (!use_raw_socket) {
DL_FOREACH(interfaces, interface) {
if (interface->socketfd > 0)
close(interface->socketfd);
}
}
DL_FOREACH_SAFE(interfaces, interface, tmp) {
DL_DELETE(interfaces, interface);
free(interface);
}
closelog();
exit(0);
}
void sighup_handler() {
struct mt_connection *p;
syslog(LOG_NOTICE, _("SIGHUP: Reloading interfaces"));
if (!use_raw_socket) {
struct net_interface *interface, *tmp;
DL_FOREACH_SAFE(interfaces, interface, tmp) {
close(interface->socketfd);
DL_DELETE(interfaces, interface);
free(interface);
}
interfaces = NULL;
}
if (net_get_interfaces(&interfaces) <= 0) {
syslog(LOG_ERR, _("No devices found! Exiting.\n"));
exit(1);
}
setup_sockets();
/* Reassign outgoing interfaces to connections again, since they may have changed */
DL_FOREACH(connections_head, p) {
if (p->interface_name != NULL) {
struct net_interface *interface = net_get_interface_ptr(&interfaces, p->interface_name, 0);
if (interface != NULL) {
p->interface = interface;
} else {
struct mt_connection tmp;
syslog(LOG_NOTICE, _("(%d) Connection closed because interface %s is gone."), p->seskey, p->interface_name);
tmp.next = p->next;
list_remove_connection(p);
p = &tmp;
}
}
}
}
/*
* TODO: Rewrite main() when all sub-functionality is tested
*/
int main (int argc, char **argv) {
int result;
struct sockaddr_in si_me;
struct sockaddr_in si_me_mndp;
struct timeval timeout;
struct mt_packet pdata;
struct net_interface *interface;
fd_set read_fds;
int c,optval = 1;
int print_help = 0;
int foreground = 0;
int interface_count = 0;
setlocale(LC_ALL, "");
bindtextdomain("mactelnet","/usr/share/locale");
textdomain("mactelnet");
while ((c = getopt(argc, argv, "fnvh?")) != -1) {
switch (c) {
case 'f':
foreground = 1;
break;
case 'n':
use_raw_socket = 1;
break;
case 'v':
print_version();
exit(0);
break;
case 'h':
case '?':
print_help = 1;
break;
}
}
if (print_help) {
print_version();
fprintf(stderr, _("Usage: %s [-f|-n|-h]\n"), argv[0]);
if (print_help) {
fprintf(stderr, _("\nParameters:\n"
" -f Run process in foreground.\n"
" -n Do not use broadcast packets. Just a tad less insecure.\n"
" -h This help.\n"
"\n"));
}
return 1;
}
if (geteuid() != 0) {
fprintf(stderr, _("You need to have root privileges to use %s.\n"), argv[0]);
return 1;
}
/* Try to read user file */
read_userfile();
/* Seed randomizer */
srand(time(NULL));
if (use_raw_socket) {
/* Transmit raw packets with this socket */
sockfd = net_init_raw_socket();
}
/* Receive regular udp packets with this socket */
insockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (insockfd < 0) {
perror("insockfd");
return 1;
}
/* Set source port */
sourceport = MT_MACTELNET_PORT;
/* Listen address*/
inet_pton(AF_INET, (char *)"0.0.0.0", &sourceip);
/* Set up global info about the connection */
inet_pton(AF_INET, (char *)"255.255.255.255", &destip);
/* Initialize receiving socket on the device chosen */
memset((char *) &si_me, 0, sizeof(si_me));