main.c 8.2 KB

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