main.c 6.1 KB

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