main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 (unlikely(is_user_banned(name)))
  195. return 1;
  196. do_hook_recv(msg, name, session);
  197. if (*msg)
  198. pretty_new_msg(name, msg, session, flags);
  199. pretty_prompt(usrname);
  200. return 0;
  201. }
  202. int init_signal(void) {
  203. struct sigaction act;
  204. memset(&act, 0, sizeof(struct sigaction));
  205. act.sa_handler = sigint_handler;
  206. sigaction(SIGINT, &act, NULL);
  207. return 0;
  208. }
  209. int main(int argc, char **argv) {
  210. struct pollfd fds[2];
  211. struct vcc_request *req;
  212. struct vcc_relay_header *hdr;
  213. char *buf, *msg, *ip = VCC_DEFSERV;
  214. char key[64], iv[64];
  215. int n, size, port = VCC_PORT, flags;
  216. (void) argc;
  217. setlocale(LC_ALL, "");
  218. bindtextdomain("vcc", "lang");
  219. textdomain("vcc");
  220. printf(VCC_VERSION "\n");
  221. n = parse_args(argc, argv, &port, &ip);
  222. init_socket(ip, port);
  223. printf(_("Connected to server. \n"));
  224. if (mode == MODE_ROBOT)
  225. return robot();
  226. if (!vos_login() && !do_intiauth()) {
  227. /* normally startup */
  228. login(n);
  229. show_algo_info();
  230. memset(key, 0, 64);
  231. memset(iv, 0, 64);
  232. fprintf(stderr, "Key: ");
  233. read(0, key, 64);
  234. fprintf(stderr, "IV: ");
  235. read(0, iv, 64);
  236. crypt_init(key, iv);
  237. }
  238. fds[0].fd = 0;
  239. fds[0].events = POLLIN;
  240. fds[0].revents = 0;
  241. fds[1].fd = fd;
  242. fds[1].events = POLLIN;
  243. fds[1].revents = 0;
  244. pretty_init();
  245. init_signal();
  246. if (unlikely(!(buf = malloc(PATH_MAX)))) {
  247. fprintf(stderr, "*** malloc() failed\n");
  248. return 1;
  249. }
  250. if (unlikely(!(hdr = malloc(sizeof(struct vcc_relay_header))))) {
  251. fprintf(stderr, "*** malloc() failed\n");
  252. return 1;
  253. }
  254. if (unlikely(!(req = malloc(sizeof(struct vcc_request ))))) {
  255. fprintf(stderr, "*** malloc() failed\n");
  256. return 1;
  257. }
  258. for (;;) {
  259. memset(buf, 0, MSG_SIZE);
  260. n = poll(fds, 2, -1);
  261. if (n == -1) {
  262. if (likely(errno == EINTR))
  263. continue;
  264. perror("poll()");
  265. break;
  266. }
  267. if (fds[0].revents & POLLIN) {
  268. if (read(0, buf, PATH_MAX) == -1)
  269. continue;
  270. do_handle_command(buf);
  271. }
  272. if (fds[1].revents & POLLIN) {
  273. size = read(fd, hdr, sizeof(struct vcc_relay_header));
  274. if (unlikely(!size)) {
  275. fprintf(stderr, _("Server closed. \n"));
  276. break;
  277. }
  278. if (ntohl(hdr->magic) == VCC_MAGIC) {
  279. memcpy(req, hdr, sizeof(struct vcc_relay_header));
  280. read(fd, (void *) req + sizeof(struct vcc_relay_header),
  281. REQ_SIZE - sizeof(struct vcc_relay_header));
  282. /* classical process */
  283. if (ntohl(req->reqtype) == REQ_MSG_NEW) {
  284. if (ntohl(req->flags) & FLAG_ENCRYPTED)
  285. decrypt(req->msg);
  286. new_msg(req->msg, req->usrname, req->session, 0);
  287. }
  288. else
  289. do_bh(req);
  290. continue;
  291. }
  292. else {
  293. if (ntohl(hdr->reqtype) == REQ_REL_NEW) {
  294. flags = MSG_NEW_RELAY;
  295. if (hdr->uid)
  296. flags |= MSG_NEW_ONLY_VISIBLE;
  297. msg = relay_get_msg(fd, hdr);
  298. new_msg(msg, hdr->usrname, hdr->session, flags);
  299. free(msg);
  300. }
  301. else
  302. do_relay_bh(hdr);
  303. continue;
  304. }
  305. }
  306. }
  307. printf(_("bye. \n"));
  308. return 0;
  309. }