vcc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /* vcc/vcc.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 <sys/stat.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <arpa/inet.h>
  29. #include <poll.h>
  30. #include <signal.h>
  31. #include <klist.h>
  32. #include <vcc/vcc.h>
  33. #include <vcc/plugin.h>
  34. #include <vcc/version.h>
  35. #include <vcc/interfaces.h>
  36. #define clear_req(req) memset(req, 0, sizeof(*req))
  37. int current_sid = 0;
  38. struct klist_node sname_cache = KLIST_NODE_INIT(&sname_cache);
  39. int init_socket(const char *server_addr, int server_port) {
  40. struct sockaddr_in addr, server;
  41. if (unlikely((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) {
  42. perror("socket()");
  43. exit(1);
  44. }
  45. memset(&addr, 0, sizeof(struct sockaddr_in));
  46. addr.sin_family = AF_INET;
  47. addr.sin_addr.s_addr = htonl(INADDR_ANY);
  48. addr.sin_port = htons(0);
  49. if (unlikely(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)) {
  50. perror("bind()");
  51. exit(1);
  52. }
  53. memset(&server, 0, sizeof(struct sockaddr_in));
  54. server.sin_family = AF_INET;
  55. server.sin_port = htons(server_port);
  56. if (unlikely(!inet_aton(server_addr, &server.sin_addr))) {
  57. fprintf(stderr, _("invalid ip address. "
  58. "leave blank if you want to connect the default server. \n"));
  59. exit(1);
  60. }
  61. if (unlikely(connect(fd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) < 0)) {
  62. perror("connect()");
  63. exit(1);
  64. }
  65. return fd;
  66. }
  67. int __send_msg(const char *msg, const char *usrname, int flags) {
  68. struct vcc_request req;
  69. clear_req(&req);
  70. req.magic = htonl(VCC_MAGIC);
  71. req.reqtype = htonl(REQ_MSG_SEND);
  72. req.uid = 0;
  73. req.session = htonl(current_sid);
  74. req.flags = htonl(flags);
  75. strncpy(req.usrname, usrname, USRNAME_SIZE);
  76. strncpy(req.msg, msg, MSG_SIZE);
  77. write(fd, &req, REQ_SIZE);
  78. return 0;
  79. }
  80. extern inline int send_msg(const char *msg, const char *usrname) {
  81. return __send_msg(msg, usrname, 0);
  82. }
  83. extern inline int send_msg_encrypted(const char *msg, const char *usrname) {
  84. return __send_msg(msg, usrname, FLAG_ENCRYPTED);
  85. }
  86. int send_relay_msg(const char *msg, const char *usrname, const char *visible) {
  87. struct vcc_relay_header req;
  88. int len;
  89. len = strlen(msg);
  90. clear_req(&req);
  91. req.magic = htonl(VCC_MAGIC_RL);
  92. req.reqtype = htonl(REQ_REL_MSG);
  93. req.uid = 0;
  94. req.session = htonl(current_sid);
  95. req.size = htonl(sizeof(struct vcc_relay_header) + len);
  96. strncpy(req.usrname, usrname, USRNAME_SIZE);
  97. if (visible)
  98. strncpy(req.visible, visible, USRNAME_SIZE);
  99. else
  100. memset(req.visible, 0, USRNAME_SIZE);
  101. write(fd, &req, sizeof(struct vcc_relay_header));
  102. write(fd, msg, len);
  103. return 0;
  104. }
  105. int read_until(int fd, void *p, int until) {
  106. int did_read = 0;
  107. int n;
  108. while ((did_read += n = read(fd, p, until)) < until) {
  109. if (!n)
  110. return 0;
  111. p += n;
  112. }
  113. return until;
  114. }
  115. char *relay_get_msg(int fd, struct vcc_relay_header *hdr) {
  116. char *p;
  117. int size;
  118. size = ntohl(hdr->size);
  119. if (unlikely(!(p = malloc(size)))) {
  120. fprintf(stderr, "*** malloc() failed\n");
  121. return NULL;
  122. }
  123. memset(p, 0, size);
  124. read_until(fd, p, size - sizeof(struct vcc_relay_header));
  125. return p;
  126. }
  127. int get_user_info(char *usrname) {
  128. struct vcc_request req;
  129. clear_req(&req);
  130. req.magic = htonl(VCC_MAGIC);
  131. req.reqtype = htonl(REQ_CTL_UINFO);
  132. req.uid = 0;
  133. strncpy(req.msg, usrname, USRNAME_SIZE);
  134. write(fd, &req, REQ_SIZE);
  135. return 0;
  136. }
  137. int get_users(void) {
  138. struct vcc_request req;
  139. clear_req(&req);
  140. req.magic = htonl(VCC_MAGIC);
  141. req.reqtype = htonl(REQ_CTL_USRS);
  142. req.uid = 0;
  143. write(fd, &req, REQ_SIZE);
  144. return 0;
  145. }
  146. int get_sessions(void) {
  147. struct vcc_request req;
  148. clear_req(&req);
  149. req.magic = htonl(VCC_MAGIC);
  150. req.reqtype = htonl(REQ_CTL_SESS);
  151. req.uid = 0;
  152. write(fd, &req, REQ_SIZE);
  153. return 0;
  154. }
  155. int do_bh(struct vcc_request *req) {
  156. switch (ntohl(req->reqtype)) {
  157. case REQ_CTL_LOGIN:
  158. vcc_login_bh(req);
  159. break;
  160. #if 0
  161. /* obsolete, see do_relay_bh() */
  162. case REQ_CTL_USRS:
  163. do_cmd_ls_bh(req);
  164. break;
  165. #endif
  166. case REQ_CTL_SESS:
  167. do_cmd_lsse_bh(req);
  168. break;
  169. case REQ_CTL_UINFO:
  170. do_cmd_uinfo_bh(req);
  171. break;
  172. case REQ_SYS_SCRINC:
  173. do_cmd_incr_bh(req);
  174. break;
  175. default:
  176. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  177. break;
  178. }
  179. return 0;
  180. }
  181. int do_relay_bh(struct vcc_relay_header *req) {
  182. switch (ntohl(req->reqtype)) {
  183. case REQ_CTL_USRS:
  184. do_cmd_ls_bh(req);
  185. break;
  186. default:
  187. fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
  188. break;
  189. }
  190. return 0;
  191. }
  192. int __login(char *usrname, char *passwd, int type) {
  193. struct vcc_request req;
  194. clear_req(&req);
  195. req.magic = htonl(VCC_MAGIC);
  196. /* REQ_CTL_LOGIN or REQ_CTL_IALOG */
  197. req.reqtype = htonl(type);
  198. strncpy(req.usrname, usrname, USRNAME_SIZE);
  199. strncpy(req.msg, passwd, MSG_SIZE);
  200. write(fd, &req, REQ_SIZE);
  201. return 0;
  202. }
  203. int vcc_login(char *usrname, char *passwd) {
  204. return __login(usrname, passwd, REQ_CTL_LOGIN);
  205. }
  206. int ia_login(char *session) {
  207. return __login(usrname, session, REQ_CTL_IALOG);
  208. }
  209. int vcc_login_bh(struct vcc_request *req) {
  210. if (ntohl(req->reqtype) != REQ_CTL_LOGIN) {
  211. fprintf(stderr, "*** bad package. \n");
  212. return 1;
  213. }
  214. if (ntohl(req->uid))
  215. printf(_("ready. \n"));
  216. else {
  217. fprintf(stderr, _("login failed: wrong password or user doesn't exists. \n"));
  218. fprintf(stderr, _("if you haven't got an account on this server, ask the administrator "
  219. "and you can get one. \n"));
  220. exit(1);
  221. }
  222. return 0;
  223. }
  224. int vcc_create_session(char *name) {
  225. struct vcc_request req;
  226. clear_req(&req);
  227. req.magic = htonl(VCC_MAGIC);
  228. req.reqtype = htonl(REQ_CTL_NEWSE);
  229. strncpy(req.usrname, name, USRNAME_SIZE);
  230. write(fd, &req, REQ_SIZE);
  231. return 0;
  232. }
  233. int join_session(int sid) {
  234. struct vcc_request req;
  235. clear_req(&req);
  236. req.magic = htonl(VCC_MAGIC);
  237. req.reqtype = htonl(REQ_CTL_JOINS);
  238. req.session = htonl(sid);
  239. write(fd, &req, REQ_SIZE);
  240. return 0;
  241. }
  242. int session_name(int sid, char *buf) {
  243. struct vcc_request req;
  244. clear_req(&req);
  245. req.magic = htonl(VCC_MAGIC);
  246. req.reqtype = htonl(REQ_CTL_SENAME);
  247. req.session = htonl(sid);
  248. write(fd, &req, REQ_SIZE);
  249. for (;;) {
  250. read(fd, &req, REQ_SIZE);
  251. if (ntohl(req.reqtype) != REQ_CTL_SENAME)
  252. continue;
  253. if (ntohl(req.session) == -1u)
  254. strcpy(buf, "*** no such session");
  255. else
  256. strcpy(buf, req.msg);
  257. break;
  258. }
  259. return 0;
  260. }
  261. char *getsname(int sid) {
  262. struct klist_node *n;
  263. struct sname_cache *p;
  264. for (n = sname_cache.next; n != &sname_cache; n = n->next) {
  265. p = sname_cache_of(n);
  266. if (p->sid == sid)
  267. return p->name;
  268. }
  269. if (unlikely(!(p = malloc(sizeof(struct sname_cache))))) {
  270. fprintf(stderr, "*** malloc() failed\n");
  271. return 0;
  272. }
  273. klist_init(&p->node);
  274. klist_add(&sname_cache, &p->node);
  275. session_name(sid, p->name);
  276. p->sid = sid;
  277. return p->name;
  278. }
  279. int get_name_sid(char *sname) {
  280. char *p;
  281. int i;
  282. for (i = 0; ; i++) {
  283. p = getsname(i);
  284. if (!strcmp(p, "*** no such session"))
  285. return -1;
  286. if (!strcmp(sname, p))
  287. return i;
  288. }
  289. return -1;
  290. }
  291. int incr_score(char *name, int incr) {
  292. struct vcc_request req;
  293. clear_req(&req);
  294. req.magic = htonl(VCC_MAGIC);
  295. req.reqtype = htonl(REQ_SYS_SCRINC);
  296. req.session = htonl(incr);
  297. strcpy(req.usrname, name);
  298. write(fd, &req, REQ_SIZE);
  299. return 0;
  300. }
  301. int is_user_banned(char *usrname) {
  302. struct klist_node *item;
  303. struct banlist *ban;
  304. for (item = banned_people.next; item != &banned_people; item = item->next) {
  305. ban = banlist_of(item);
  306. if (ban->usrname && !strcmp(usrname, ban->usrname)) {
  307. return 1;
  308. }
  309. }
  310. return 0;
  311. }
  312. int get_sys_info(void) {
  313. struct vcc_request req;
  314. clear_req(&req);
  315. req.magic = htonl(VCC_MAGIC);
  316. req.reqtype = htonl(REQ_SYS_INFO);
  317. strcpy(req.usrname, usrname);
  318. write(fd, &req, REQ_SIZE);
  319. return 0;
  320. }