main.c 7.4 KB

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