main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <getopt.h>
  33. #include <klist.h>
  34. #include <vcc/vcc.h>
  35. #include <vcc/version.h>
  36. #include <vcc/robot.h>
  37. #include <vcc/plugin.h>
  38. #include <vcc/pretty.h>
  39. #include <vcc/defserv.h>
  40. /* we tried to use libreadline, but after using that, we feel strange
  41. * because... because of what? i don't know. */
  42. int send_msg(const char *msg, const char *usrname);
  43. int read_msg(struct vcc_request *buf);
  44. int get_msgs(void);
  45. int vcc_login(char *, char *);
  46. int is_user_banned(char *);
  47. int show_algo_info(void);
  48. int crypt_init(void *key, void *iv);
  49. int encrypt(char *);
  50. int decrypt(char *);
  51. int do_cmd(char *s);
  52. int do_bh(struct vcc_request *);
  53. int do_cmd_lself(int, char **);
  54. int init_socket(const char *server_addr, int server_port);
  55. int fd;
  56. /* normally. it belongs to the foreground pgroup, all messages write to terminal. */
  57. #define MODE_NORMAL 1
  58. /* robot mode. we run in the background pgroup, and give all the messages to the plugin specified. */
  59. #define MODE_ROBOT 2
  60. int mode = MODE_NORMAL;
  61. char usrname[USRNAME_SIZE];
  62. struct klist_node banned_people = KLIST_NODE_INIT(&banned_people);
  63. extern int crypt_disabled;
  64. extern int current_sid;
  65. static char const *help_msg = {
  66. "Usage: vcc [-auprh] [server]\n"
  67. "Options: \n"
  68. " -u --user Set the username on the server. "
  69. "If unspecified, you must input it interatively. \n"
  70. " -p --port Specified the port on the server. "
  71. "Default port is 46. You must keep this as same "
  72. "as the port on the server. \n"
  73. " -r --robot Enter the robot mode. \n"
  74. " -a --address Address of the server. "
  75. "As default, server is " VCC_DEFSERV ". \n"
  76. " -h --help Show this help information. \n"
  77. };
  78. static struct option longopts[] = {
  79. {
  80. .name = "address",
  81. .has_arg = 1,
  82. .flag = NULL,
  83. .val = 'a',
  84. },
  85. {
  86. .name = "user",
  87. .has_arg = 1,
  88. .flag = NULL,
  89. .val = 'u'
  90. },
  91. {
  92. .name = "port",
  93. .has_arg = 1,
  94. .flag = NULL,
  95. .val = 'p'
  96. },
  97. {
  98. .name = "robot",
  99. .has_arg = 0,
  100. .flag = NULL,
  101. .val = 'r'
  102. },
  103. {
  104. .name = "help",
  105. .has_arg = 0,
  106. .flag = NULL,
  107. .val = 'h'
  108. },
  109. {
  110. NULL, 0, NULL, 0
  111. }
  112. };
  113. static int usage(void) {
  114. fputs(help_msg, stderr);
  115. return 1;
  116. }
  117. static int login(int got_usrname) {
  118. char *passwd;
  119. if (!got_usrname) {
  120. fprintf(stderr, "login as: ");
  121. scanf("%s", usrname);
  122. }
  123. passwd = getpass("password: ");
  124. vcc_login(usrname, passwd);
  125. do_cmd_lself(1, NULL);
  126. return 0;
  127. }
  128. /* vos, the vcc over ssh, allows user to use vcc without install it.
  129. * you can use 'ssh username@example.com' and you will get login just like
  130. * using ssh but you can use vcc over that.
  131. * */
  132. int vos_login(void) {
  133. char *passwd, *key, *iv, *name;
  134. if (!(name = getenv("VOS_USRNAME")))
  135. return 0;
  136. strcpy(usrname, name);
  137. if (!(passwd = getenv("VOS_PASSWD"))) {
  138. fprintf(stderr, "Password not specified. \n");
  139. exit(EXIT_FAILURE);
  140. }
  141. vcc_login(usrname, passwd);
  142. do_cmd_lself(1, NULL);
  143. show_algo_info();
  144. /* crypt_init will copy that into a heap buffer */
  145. if (!(key = getenv("VOS_KEY")) || !(iv = getenv("VOS_IV"))) {
  146. fprintf(stderr, "warning: crypting disabled. \n");
  147. crypt_disabled = 1;
  148. return 1;
  149. }
  150. crypt_init(key, iv);
  151. return 1;
  152. }
  153. int parse_args(int argc, char **argv, int *port, char **ip) {
  154. int i, c, got_usrname = 0;
  155. while ((c = getopt_long(argc, argv, "a:u:p:rh", longopts, &i)) != -1) {
  156. switch (c) {
  157. case 'a':
  158. *ip = strdup(optarg);
  159. printf("%s\n", *ip);
  160. break;
  161. case 'u':
  162. strcpy(usrname, optarg);
  163. got_usrname = 1;
  164. printf("%s\n", usrname);
  165. break;
  166. case 'p':
  167. *port = atoi(optarg);
  168. break;
  169. case 'r':
  170. mode = MODE_ROBOT;
  171. break;
  172. case 'h':
  173. default:
  174. exit(usage());
  175. }
  176. }
  177. return got_usrname;
  178. }
  179. int handle_command(char *buf) {
  180. switch (*buf) {
  181. case '\n':
  182. break;
  183. case '-':
  184. return do_cmd(buf);
  185. default:
  186. if (msg_hook)
  187. if (unlikely(!(buf = msg_hook(buf))))
  188. return 1;
  189. send_msg(buf, usrname);
  190. pretty_new_msg(usrname, buf, current_sid);
  191. if (msg_hook)
  192. free(buf);
  193. break;
  194. }
  195. return 0;
  196. }
  197. int do_handle_command(char *buf) {
  198. handle_command(buf);
  199. pretty_prompt(usrname);
  200. return 0;
  201. }
  202. void sigint_handler(int signr) {
  203. (void) signr;
  204. printf("\n");
  205. pretty_prompt(usrname);
  206. }
  207. int init_signal(void) {
  208. struct sigaction act;
  209. memset(&act, 0, sizeof(struct sigaction));
  210. act.sa_handler = sigint_handler;
  211. sigaction(SIGINT, &act, NULL);
  212. return 0;
  213. }
  214. int main(int argc, char **argv) {
  215. struct pollfd fds[2];
  216. struct vcc_request *req;
  217. char *buf, *msg, *ip = VCC_DEFSERV;
  218. char key[64], iv[64];
  219. int n, size, port = VCC_PORT, got_usrname;
  220. (void) argc;
  221. #ifdef _WIN32
  222. setlocale(LC_ALL, ".UTF-8");
  223. #else
  224. setlocale(LC_ALL, "C.UTF-8");
  225. #endif
  226. printf(VCC_VERSION "\n");
  227. got_usrname = parse_args(argc, argv, &port, &ip);
  228. init_socket(ip, port);
  229. printf("Connected to server. \n");
  230. if (mode == MODE_ROBOT)
  231. return robot();
  232. if (!vos_login()) {
  233. /* normally startup */
  234. login(got_usrname);
  235. show_algo_info();
  236. memset(key, 0, 64);
  237. memset(iv, 0, 64);
  238. fprintf(stderr, "Key: ");
  239. read(0, key, 64);
  240. fprintf(stderr, "IV: ");
  241. read(0, iv, 64);
  242. crypt_init(key, iv);
  243. }
  244. fds[0].fd = 0;
  245. fds[0].events = POLLIN;
  246. fds[0].revents = 0;
  247. fds[1].fd = fd;
  248. fds[1].events = POLLIN;
  249. fds[1].revents = 0;
  250. init_signal();
  251. if (unlikely(!(buf = malloc(MSG_SIZE)))) {
  252. fprintf(stderr, "*** malloc() failed\n");
  253. return 1;
  254. }
  255. if (unlikely(!(req = malloc(REQ_SIZE)))) {
  256. fprintf(stderr, "*** malloc() failed\n");
  257. return 1;
  258. }
  259. for (;;) {
  260. memset(buf, 0, MSG_SIZE);
  261. n = poll(fds, 2, -1);
  262. if (n == -1) {
  263. if (likely(errno == EINTR))
  264. continue;
  265. perror("poll()");
  266. break;
  267. }
  268. if (fds[0].revents & POLLIN) {
  269. if (read(0, buf, MSG_SIZE) == -1)
  270. continue;
  271. do_handle_command(buf);
  272. }
  273. if (fds[1].revents & POLLIN) {
  274. size = read(fd, req, REQ_SIZE);
  275. if (unlikely(!size)) {
  276. fprintf(stderr, "Server closed. \n");
  277. break;
  278. }
  279. if (ntohl(req->reqtype) != REQ_MSG_NEW) {
  280. do_bh(req);
  281. continue;
  282. }
  283. msg = req->msg;
  284. if (!strcmp(msg, "CQD")) {
  285. pretty_cqd(req->usrname);
  286. pretty_prompt(usrname);
  287. continue;
  288. }
  289. if (unlikely(is_user_banned(req->usrname)))
  290. continue;
  291. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  292. decrypt(msg);
  293. if (recv_hook)
  294. if (unlikely(!(msg = recv_hook(msg))))
  295. continue;
  296. pretty_new_msg(req->usrname, msg, ntohl(req->session));
  297. pretty_prompt(usrname);
  298. if (recv_hook)
  299. free(msg);
  300. }
  301. }
  302. printf("bye. \n");
  303. return 0;
  304. }