main.c 6.7 KB

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