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