main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* vcc/main.c
  2. *
  3. * This file is part of vcc.
  4. *
  5. * vcc is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * vcc is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with vcc. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <locale.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #include <poll.h>
  31. #include <signal.h>
  32. #include <klist.h>
  33. #include <vcc/vcc.h>
  34. #include <vcc/version.h>
  35. #include <vcc/pretty.h>
  36. /* we tried to use libreadline, but after using that, we feel strange
  37. * because... because of what? i don't know. */
  38. #define DEFAULT_RATE 1000
  39. int send_msg(const char *msg, const char *usrname);
  40. int read_msg(struct vcc_request *buf);
  41. int get_msgs(void);
  42. int vcc_login(char *, char *);
  43. int is_user_banned(char *);
  44. int show_algo_info(void);
  45. int crypt_init(void *key, void *iv);
  46. int encrypt(char *);
  47. int decrypt(char *);
  48. int do_cmd(char *s);
  49. int do_bh(struct vcc_request *);
  50. int do_cmd_lself(char *);
  51. int init_socket(const char *server_addr, int server_port);
  52. int fd;
  53. int rate;
  54. char usrname[USRNAME_SIZE];
  55. struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
  56. extern int current_sid;
  57. static char const *help_msg = {
  58. "Usage: vcc [-up] [server]\n"
  59. "Options: \n"
  60. " -u --user Set the username on the server. "
  61. "If unspecified, you must input it interatively. \n"
  62. " -p --port Specified the port on the server. "
  63. "Default port is 46. You must keep this as same "
  64. "as the port on the server. \n"
  65. " [server] Address of the server. "
  66. "As default, server is 124.223.105.230. "
  67. };
  68. static int usage(void) {
  69. fputs(help_msg, stderr);
  70. return 1;
  71. }
  72. static int login(int got_usrname) {
  73. char *passwd;
  74. if (!got_usrname) {
  75. fprintf(stderr, "login as: ");
  76. scanf("%s", usrname);
  77. }
  78. passwd = getpass("password: ");
  79. vcc_login(usrname, passwd);
  80. do_cmd_lself(NULL);
  81. return 0;
  82. }
  83. /* vos, the vcc over ssh, allows user to use vcc without install it.
  84. * you can use 'ssh username@example.com' and you will get login just like
  85. * using ssh but you can use vcc over that.
  86. * */
  87. int vos_login(void) {
  88. char *passwd, *key, *iv, *name;
  89. if (!(name = getenv("VOS_USRNAME")))
  90. return 0;
  91. strcpy(usrname, name);
  92. if (!(passwd = getenv("VOS_PASSWD"))) {
  93. fprintf(stderr, "Password not specified. \n");
  94. exit(EXIT_FAILURE);
  95. }
  96. vcc_login(usrname, passwd);
  97. do_cmd_lself(NULL);
  98. /* crypt_init will copy that into a heap buffer */
  99. if (!(key = getenv("VOS_KEY")) || !(iv = getenv("VOS_IV")))
  100. return 1;
  101. crypt_init(key, iv);
  102. return 1;
  103. }
  104. int parse_args(char **argv, int *port, char **ip) {
  105. int i = 1;
  106. /*
  107. * unknown: 0
  108. * usrname: 1
  109. * port: 2
  110. */
  111. int type = 0;
  112. int got_usrname = 0;
  113. char *arg;
  114. while (argv[i]) {
  115. arg = argv[i];
  116. switch (type) {
  117. case 0:
  118. if (!strcmp(arg, "-u") || !strcmp(arg, "--user")) {
  119. type = 1;
  120. } else if (!strcmp(arg, "-p") || !strcmp(arg, "--port")) {
  121. type = 2;
  122. } else if (arg[0] != '-') {
  123. *ip = arg;
  124. } else {
  125. exit(usage());
  126. }
  127. break;
  128. case 1:
  129. printf("%s %s\n", arg, usrname);
  130. strcpy(usrname, arg);
  131. got_usrname = 1;
  132. type = 0;
  133. break;
  134. case 2:
  135. *port = atoi(arg);
  136. type = 0;
  137. break;
  138. default:
  139. abort();
  140. }
  141. ++i;
  142. }
  143. return got_usrname;
  144. }
  145. int do_handle_command(char *buf) {
  146. if (*buf == '-')
  147. do_cmd(buf);
  148. else if (*buf != '\n') {
  149. send_msg(buf, usrname);
  150. pretty_new_msg(usrname, buf, current_sid);
  151. }
  152. pretty_prompt(usrname);
  153. return 0;
  154. }
  155. int main(int argc, char **argv) {
  156. struct pollfd fds[2];
  157. struct vcc_request *req;
  158. char *buf, *msg, *ip = "124.223.105.230";
  159. char key[64], iv[64];
  160. int n, size, port = VCC_PORT, got_usrname;
  161. (void) argc;
  162. #ifdef _WIN32
  163. setlocale(LC_ALL, ".UTF-8");
  164. #else
  165. setlocale(LC_ALL, "C.UTF-8");
  166. #endif
  167. printf(VCC_VERSION "\n");
  168. init_socket(ip, port);
  169. printf("Connected to server. \n");
  170. if (!vos_login()) {
  171. /* normally startup */
  172. got_usrname = parse_args(argv, &port, &ip);
  173. login(got_usrname);
  174. show_algo_info();
  175. memset(key, 0, 64);
  176. memset(iv, 0, 64);
  177. fprintf(stderr, "Key: ");
  178. read(0, key, 64);
  179. fprintf(stderr, "IV: ");
  180. read(0, iv, 64);
  181. crypt_init(key, iv);
  182. }
  183. fds[0].fd = 0;
  184. fds[0].events = POLLIN;
  185. fds[0].revents = 0;
  186. fds[1].fd = fd;
  187. fds[1].events = POLLIN;
  188. fds[1].revents = 0;
  189. rate = DEFAULT_RATE;
  190. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  191. fprintf(stderr, "*** malloc() failed\n");
  192. return 1;
  193. }
  194. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  195. fprintf(stderr, "*** malloc() failed\n");
  196. return 1;
  197. }
  198. for (;;) {
  199. memset(buf, 0, MSG_SIZE);
  200. n = poll(fds, 2, rate);
  201. if (n == -1) {
  202. if (likely(errno == EINTR))
  203. continue;
  204. perror("poll()");
  205. break;
  206. }
  207. if (!n) {
  208. /* 1 sec timed out */
  209. get_msgs();
  210. continue;
  211. }
  212. if (fds[0].revents & POLLIN) {
  213. if (read(0, buf, MSG_SIZE) == -1)
  214. continue;
  215. do_handle_command(buf);
  216. }
  217. if (fds[1].revents & POLLIN) {
  218. size = read(fd, req, REQ_SIZE);
  219. if (unlikely(!size)) {
  220. fprintf(stderr, "Server closed. \n");
  221. break;
  222. }
  223. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  224. do_bh(req);
  225. continue;
  226. }
  227. msg = req->msg;
  228. if (!strcmp(msg, "CQD")) {
  229. pretty_cqd(req->usrname);
  230. pretty_prompt(usrname);
  231. continue;
  232. }
  233. if (unlikely(is_user_banned(req->usrname)))
  234. continue;
  235. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  236. decrypt(msg);
  237. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  238. pretty_prompt(usrname);
  239. }
  240. }
  241. printf("bye. \n");
  242. return 0;
  243. }