diff options
author | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-11-02 18:09:32 +0100 |
---|---|---|
committer | Håkon Nessjøen <haakon.nessjoen@gmail.com> | 2010-11-02 18:09:32 +0100 |
commit | 40362c46806d033316e859db772f46c5652fbea3 (patch) | |
tree | 9fc5569479dddfbd87ce94046184168ffed1b49b | |
parent | a993f53f4ab8e08bfa2c198d9375f85686e87c9f (diff) | |
download | MAC-Telnet-40362c46806d033316e859db772f46c5652fbea3.tar.gz MAC-Telnet-40362c46806d033316e859db772f46c5652fbea3.zip |
Simplified the way parseControlPacket is looped/called
-rw-r--r-- | mactelnet.c | 21 | ||||
-rw-r--r-- | mactelnetd.c | 25 | ||||
-rw-r--r-- | protocol.c | 40 | ||||
-rw-r--r-- | protocol.h | 2 |
4 files changed, 53 insertions, 35 deletions
diff --git a/mactelnet.c b/mactelnet.c index 652023b..2175d65 100644 --- a/mactelnet.c +++ b/mactelnet.c @@ -153,9 +153,9 @@ void handlePacket(unsigned char *data, int data_len) { /* Handle data packets */ if (pkthdr.ptype == MT_PTYPE_DATA) { struct mt_packet odata; + struct mt_mactelnet_control_hdr cpkt; int plen=0,result=0; - int rest = 0; - unsigned char *p = data; + int success = 0; /* Always transmit ACKNOWLEDGE packets in response to DATA packets */ plen = initPacket(&odata, MT_PTYPE_ACK, srcmac, dstmac, sessionkey, pkthdr.counter + (data_len - MT_HEADER_LEN)); @@ -170,18 +170,10 @@ void handlePacket(unsigned char *data, int data_len) { return; } - /* Calculate how much more there is in the packet */ - rest = data_len - MT_HEADER_LEN; - p += MT_HEADER_LEN; + /* Parse controlpacket data */ + success = parseControlPacket(data + MT_HEADER_LEN, data_len - MT_HEADER_LEN, &cpkt); - while (rest > 0) { - int read = 0; - struct mt_mactelnet_control_hdr cpkt; - - /* Parse controlpacket data */ - read = parseControlPacket(p, rest, &cpkt); - p += read; - rest -= read; + while (success) { /* If we receive encryptionkey, transmit auth data back */ if (cpkt.cptype == MT_CPTYPE_ENCRYPTIONKEY) { @@ -209,6 +201,9 @@ void handlePacket(unsigned char *data, int data_len) { /* Add resize signal handler */ signal(SIGWINCH, sig_winch); } + + /* Parse next controlpacket */ + success = parseControlPacket(NULL, 0, &cpkt); } } else if (pkthdr.ptype == MT_PTYPE_ACK) { diff --git a/mactelnetd.c b/mactelnetd.c index 77ac000..5c7f96a 100644 --- a/mactelnetd.c +++ b/mactelnetd.c @@ -212,8 +212,8 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a case MT_PTYPE_DATA: curconn = findConnection(pkthdr.seskey, (unsigned char *)&(pkthdr.srcaddr)); if (curconn != NULL) { - unsigned char *p = data; - int rest; + int success; + struct mt_mactelnet_control_hdr cpkt; char doLogin = 0; curconn->lastdata = time(NULL); @@ -232,18 +232,10 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a } /* Calculate how much more there is in the packet */ - rest = data_len - MT_HEADER_LEN; - p += MT_HEADER_LEN; - - while (rest > 0) { - int read; - struct mt_mactelnet_control_hdr cpkt; - - /* Parse controlpacket data */ - read = parseControlPacket(p, rest, &cpkt); - p += read; - rest -= read; + success = parseControlPacket(data + MT_HEADER_LEN, data_len - MT_HEADER_LEN, &cpkt); + while (success) { +printf("Type: %d\n", cpkt.cptype); if (cpkt.cptype == MT_CPTYPE_BEGINAUTH) { int plen,i; for (i = 0; i < 16; ++i) { @@ -291,6 +283,9 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a } else { printf("Unhandeled CPTYPE: %d\n", cpkt.cptype); } + + /* Parse next controlpacket */ + success = parseControlPacket(NULL, 0, &cpkt); } if (doLogin) { int plen = 0; @@ -301,6 +296,7 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a readUserfile(); if ((user = findUser(curconn->username)) != NULL) { + printf("User %s is logging in.\n", curconn->username); md5_state_t state; /* Concat string of 0 + password + encryptionkey */ md5data[0] = 0; @@ -321,6 +317,7 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a if (curconn->state == STATE_ACTIVE) return; } else { + printf("User %s not found\n", curconn->username); doLogin = 0; } @@ -398,7 +395,7 @@ void handlePacket(unsigned char *data, int data_len, const struct sockaddr_in *a chdir(user->pw_dir); /* Spawn shell */ execl (user->pw_shell, user->pw_shell, (char *) 0); - //exit(0); + exit(0); // just to be sure. } close(curconn->slavefd); curconn->pid = pid; @@ -147,13 +147,33 @@ void parsePacket(unsigned char *data, struct mt_mactelnet_hdr *pkthdr) { } -int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactelnet_control_hdr *cpkthdr) { +int parseControlPacket(unsigned char *packetdata, int data_len, struct mt_mactelnet_control_hdr *cpkthdr) { + static unsigned char *int_data; + static unsigned int int_data_len; + static unsigned int int_pos; + unsigned char *data; + + /* Store info so we can call this function once with data, + and then several times for each control packets. Letting this function + control the data position. */ + if (packetdata != NULL) { + if (data_len <= 0) + return 0; + + int_data = packetdata; + int_data_len = data_len; + int_pos = 0; + } - if (data_len < 0) + /* No more data to parse? */ + if (int_pos >= int_data_len) return 0; + /* Set current position in data buffer */ + data = int_data + int_pos; + /* Check for valid minimum packet length & magic header */ - if (data_len >= 9 && memcmp(data, &mt_mactelnet_cpmagic, 4) == 0) { + if (int_data_len >= 9 && memcmp(data, &mt_mactelnet_cpmagic, 4) == 0) { /* Control packet type */ cpkthdr->cptype = data[4]; @@ -164,17 +184,23 @@ int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactel /* Set pointer to actual data */ cpkthdr->data = data + 9; - /* Return number of bytes in packet */ - return cpkthdr->length + 9; + /* Remember old position, for next call */ + int_pos += cpkthdr->length + 9; + + /* Read data successfully */ + return 1; } else { /* Mark data as raw terminal data */ cpkthdr->cptype = MT_CPTYPE_PLAINDATA; - cpkthdr->length = data_len; + cpkthdr->length = int_data_len - int_pos; cpkthdr->data = data; /* Consume the whole rest of the packet */ - return data_len; + int_pos = int_data_len; + + /* Read data successfully */ + return 1; } } @@ -82,7 +82,7 @@ struct mt_packet { extern int initPacket(struct mt_packet *packet, unsigned char ptype, unsigned char *srcmac, unsigned char *dstmac, unsigned short sessionkey, unsigned int counter); extern int addControlPacket(struct mt_packet *packet, char cptype, void *cpdata, int data_len); extern void parsePacket(unsigned char *data, struct mt_mactelnet_hdr *pkthdr); -extern int parseControlPacket(unsigned char *data, const int data_len, struct mt_mactelnet_control_hdr *cpkthdr); +extern int parseControlPacket(unsigned char *data, int data_len, struct mt_mactelnet_control_hdr *cpkthdr); /* MNDP packets */ struct mt_mndp_packet *parseMNDP(const char *data, const int packetLen); |