main.c 7.7 KB

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