main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #include <vcc/defserv.h>
  37. /* we tried to use libreadline, but after using that, we feel strange
  38. * because... because of what? i don't know. */
  39. #define DEFAULT_RATE 1000
  40. int send_msg(const char *msg, const char *usrname);
  41. int read_msg(struct vcc_request *buf);
  42. int get_msgs(void);
  43. int vcc_login(char *, char *);
  44. int is_user_banned(char *);
  45. int show_algo_info(void);
  46. int crypt_init(void *key, void *iv);
  47. int encrypt(char *);
  48. int decrypt(char *);
  49. int do_cmd(char *s);
  50. int do_bh(struct vcc_request *);
  51. int do_cmd_lself(char *);
  52. int init_socket(const char *server_addr, int server_port);
  53. int fd;
  54. char usrname[USRNAME_SIZE];
  55. struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
  56. extern int crypt_disabled;
  57. extern int current_sid;
  58. static char const *help_msg = {
  59. "Usage: vcc [-up] [server]\n"
  60. "Options: \n"
  61. " -u --user Set the username on the server. "
  62. "If unspecified, you must input it interatively. \n"
  63. " -p --port Specified the port on the server. "
  64. "Default port is 46. You must keep this as same "
  65. "as the port on the server. \n"
  66. " [server] Address of the server. "
  67. "As default, server is " VCC_DEFSERV ". \n"
  68. };
  69. static int usage(void) {
  70. fputs(help_msg, stderr);
  71. return 1;
  72. }
  73. static int login(int got_usrname) {
  74. char *passwd;
  75. if (!got_usrname) {
  76. fprintf(stderr, "login as: ");
  77. scanf("%s", usrname);
  78. }
  79. passwd = getpass("password: ");
  80. vcc_login(usrname, passwd);
  81. do_cmd_lself(NULL);
  82. return 0;
  83. }
  84. /* vos, the vcc over ssh, allows user to use vcc without install it.
  85. * you can use 'ssh username@example.com' and you will get login just like
  86. * using ssh but you can use vcc over that.
  87. * */
  88. int vos_login(void) {
  89. char *passwd, *key, *iv, *name;
  90. if (!(name = getenv("VOS_USRNAME")))
  91. return 0;
  92. strcpy(usrname, name);
  93. if (!(passwd = getenv("VOS_PASSWD"))) {
  94. fprintf(stderr, "Password not specified. \n");
  95. exit(EXIT_FAILURE);
  96. }
  97. vcc_login(usrname, passwd);
  98. do_cmd_lself(NULL);
  99. show_algo_info();
  100. /* crypt_init will copy that into a heap buffer */
  101. if (!(key = getenv("VOS_KEY")) || !(iv = getenv("VOS_IV"))) {
  102. fprintf(stderr, "warning: crypting disabled. \n");
  103. crypt_disabled = 1;
  104. return 1;
  105. }
  106. crypt_init(key, iv);
  107. return 1;
  108. }
  109. int parse_args(char **argv, int *port, char **ip) {
  110. int i = 1;
  111. /*
  112. * unknown: 0
  113. * usrname: 1
  114. * port: 2
  115. */
  116. int type = 0;
  117. int got_usrname = 0;
  118. char *arg;
  119. while (argv[i]) {
  120. arg = argv[i];
  121. switch (type) {
  122. case 0:
  123. if (!strcmp(arg, "-u") || !strcmp(arg, "--user")) {
  124. type = 1;
  125. } else if (!strcmp(arg, "-p") || !strcmp(arg, "--port")) {
  126. type = 2;
  127. } else if (arg[0] != '-') {
  128. *ip = arg;
  129. } else {
  130. exit(usage());
  131. }
  132. break;
  133. case 1:
  134. strcpy(usrname, arg);
  135. got_usrname = 1;
  136. type = 0;
  137. break;
  138. case 2:
  139. *port = atoi(arg);
  140. type = 0;
  141. break;
  142. default:
  143. abort();
  144. }
  145. ++i;
  146. }
  147. return got_usrname;
  148. }
  149. int do_handle_command(char *buf) {
  150. if (*buf == '-')
  151. do_cmd(buf);
  152. else if (*buf != '\n') {
  153. send_msg(buf, usrname);
  154. pretty_new_msg(usrname, buf, current_sid);
  155. }
  156. pretty_prompt(usrname);
  157. return 0;
  158. }
  159. int main(int argc, char **argv) {
  160. struct pollfd fds[2];
  161. struct vcc_request *req;
  162. char *buf, *msg, *ip = VCC_DEFSERV;
  163. char key[64], iv[64];
  164. int n, size, port = VCC_PORT, got_usrname;
  165. (void) argc;
  166. #ifdef _WIN32
  167. setlocale(LC_ALL, ".UTF-8");
  168. #else
  169. setlocale(LC_ALL, "C.UTF-8");
  170. #endif
  171. printf(VCC_VERSION "\n");
  172. got_usrname = parse_args(argv, &port, &ip);
  173. init_socket(ip, port);
  174. printf("Connected to server. \n");
  175. if (!vos_login()) {
  176. /* normally startup */
  177. login(got_usrname);
  178. show_algo_info();
  179. memset(key, 0, 64);
  180. memset(iv, 0, 64);
  181. fprintf(stderr, "Key: ");
  182. read(0, key, 64);
  183. fprintf(stderr, "IV: ");
  184. read(0, iv, 64);
  185. crypt_init(key, iv);
  186. }
  187. fds[0].fd = 0;
  188. fds[0].events = POLLIN;
  189. fds[0].revents = 0;
  190. fds[1].fd = fd;
  191. fds[1].events = POLLIN;
  192. fds[1].revents = 0;
  193. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  194. fprintf(stderr, "*** malloc() failed\n");
  195. return 1;
  196. }
  197. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  198. fprintf(stderr, "*** malloc() failed\n");
  199. return 1;
  200. }
  201. for (;;) {
  202. memset(buf, 0, MSG_SIZE);
  203. n = poll(fds, 2, -1);
  204. if (n == -1) {
  205. if (likely(errno == EINTR))
  206. continue;
  207. perror("poll()");
  208. break;
  209. }
  210. if (fds[0].revents & POLLIN) {
  211. if (read(0, buf, MSG_SIZE) == -1)
  212. continue;
  213. do_handle_command(buf);
  214. }
  215. if (fds[1].revents & POLLIN) {
  216. size = read(fd, req, REQ_SIZE);
  217. if (unlikely(!size)) {
  218. fprintf(stderr, "Server closed. \n");
  219. break;
  220. }
  221. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  222. do_bh(req);
  223. continue;
  224. }
  225. msg = req->msg;
  226. if (!strcmp(msg, "CQD")) {
  227. pretty_cqd(req->usrname);
  228. pretty_prompt(usrname);
  229. continue;
  230. }
  231. if (unlikely(is_user_banned(req->usrname)))
  232. continue;
  233. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  234. decrypt(msg);
  235. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  236. pretty_prompt(usrname);
  237. }
  238. }
  239. printf("bye. \n");
  240. return 0;
  241. }