New developer version 0.6.8; added select () function; added demonstrating example...
[ZeXOS.git] / apps / telnetd / console.c
blob0c4e4460a95c7a05eafde8ac35cd06b3a66e3ff9
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include "client.h"
27 char *login_msg;
28 unsigned login_msg_len;
30 char *tty2;
31 unsigned tty2_len;
33 char *buf;
34 unsigned buf_len;
36 char *tty;
37 char *tty_len;
39 unsigned zlen;
41 /*int tty_update (client_t *c)
43 int i = 0;
45 for (i = 0; i < 2000; i ++)
46 tty[i] = c->tty[(i*2)+1];
48 return 1;
49 }*/
51 int console_init ()
53 login_msg = (char *) malloc (sizeof (char) * 100);
55 if (!login_msg)
56 return 0;
58 memcpy (login_msg, "�����!", 6);
59 memcpy (login_msg+6, "ZeX/OS telnet deamon\n\n", 23);
61 login_msg[29] = '\0';
63 login_msg_len = 29;
66 buf_len = 0;
67 buf = (char *) malloc (sizeof (char) * 128);
69 if (!buf)
70 return 0;
72 tty_len = 0;
73 tty = (char *) malloc (sizeof (char) * 101);
75 if (!tty)
76 return 0;
78 tty2_len = 0;
79 tty2 = (char *) malloc (sizeof (char) * 101);
81 if (!tty2)
82 return 0;
84 zlen = 0;
86 return 1;
89 int console_motd (client_t *c)
91 client_send (c, login_msg, login_msg_len);
93 c->state = CLIENT_STATE_LOGIN;
95 return 1;
98 int console_login (client_t *c)
100 if (!c)
101 return -1;
105 /* int ret = client_recv (c, login_buf, login_buf_len);
107 if (ret > 0) {
108 memcpy (buf+c->buf_len, login_buf, ret);
109 c->buf_len += ret;
110 buf[c->buf_len] = '\0';
111 puts ("he: ");
112 puts (login_buf);
113 puts ("\n");
115 int log = 0;
116 if (ret > 1) {
117 int cislo = login_buf[0];
119 itoa (cislo, tty, 10);
120 puts (tty);
121 if (login_buf[1] == 0 && login_buf[0] == 13)
122 log = 1;
125 if (ret > 1 || log)
126 if (buf[c->buf_len-1] == '\n' || log) {
127 buf[c->buf_len-1] = '\0';
129 puts ("nekdo zadal login: '");
130 puts (buf+c->buf_len-6);
131 puts ("'\n");
133 if (!strcmp (buf+c->buf_len-6, "root")) {
134 puts ("New user was succefully logged !\n");
136 //c->tty = (char *) tty_init ();
138 //if (!c->tty) {
139 // c->state = CLIENT_STATE_DONE;
140 // return 0;
143 c->state = CLIENT_STATE_READY;
146 return 1;
150 c->state = CLIENT_STATE_READY;
152 return 1;
155 int console_handler (client_t *c)
157 int ret = client_recv (c, tty, 100);
159 if (ret == -1) {
160 puts ("Some client was disconnected\n");
161 c->state = CLIENT_STATE_DONE;
162 return 1;
165 if (ret > 0) {
166 unsigned i = 0;
167 for (i = 0; i < (unsigned) ret; i ++) {
168 /*int cislo = tty[i];
170 itoa (cislo, tty2, 10);
171 puts (tty2); puts ("|");*/
172 if (tty[i] == 13 && tty[i+1] == 10) {
173 tty[i] = '\0';
174 tty[i+1] = '\n';
177 if (tty[i] == 127)
178 tty[i] = '\b';
180 if (tty[i] == 32)
181 tty[i] = ' ';
184 write (1, tty, ret);
187 unsigned len = read (1, tty2, 100);
189 if (len > 0) {
190 /*if (!strncmp (tty2, tty, ret))
191 clients_send (tty2+ret, len-ret);
192 else*/
193 /*if (strlen (tty2)) {
194 puts ("data: '");
195 puts (tty);
196 puts ("' - '");
197 puts (tty2);
198 puts ("\n");
201 clients_send (tty2, len);
204 return 1;