main.c 8.1 KB

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