main.c 8.7 KB

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