pptpmanager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * pptpmanager.c
  3. *
  4. * Manages the PoPToP sessions.
  5. *
  6. * $Id: pptpmanager.c,v 1.15 2011/05/19 00:02:50 quozl Exp $
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #ifdef __linux__
  12. #define _GNU_SOURCE 1 /* broken arpa/inet.h */
  13. #endif
  14. #include "our_syslog.h"
  15. #include <errno.h>
  16. #include <netdb.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <sys/un.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #include <time.h>
  29. #include <sys/time.h>
  30. #include <fcntl.h>
  31. #ifdef VRF
  32. #include <vrf.h>
  33. #endif
  34. #if HAVE_LIBWRAP
  35. /* re-include, just in case HAVE_SYSLOG_H wasn't defined */
  36. #include <syslog.h>
  37. #include <tcpd.h>
  38. int allow_severity = LOG_WARNING;
  39. int deny_severity = LOG_WARNING;
  40. #endif
  41. #ifdef __UCLIBC__
  42. #define socklen_t int
  43. #endif
  44. #include "configfile.h"
  45. #include "defaults.h"
  46. #include "pptpctrl.h"
  47. #include "pptpdefs.h"
  48. #include "pptpmanager.h"
  49. #include "compat.h"
  50. /* command line arg variables */
  51. extern char *ppp_binary;
  52. extern char *pppdoptstr;
  53. extern char *speedstr;
  54. extern char *bindaddr;
  55. extern int pptp_debug;
  56. extern int pptp_noipparam;
  57. extern int pptp_logwtmp;
  58. extern int pptp_delegate;
  59. /* option for timeout on starting ctrl connection */
  60. extern int pptp_stimeout;
  61. extern int pptp_connections;
  62. /* local function prototypes */
  63. static void connectCall(int clientSocket, int clientNumber);
  64. static int createHostSocket(int *hostSocket);
  65. /* this end's call identifier */
  66. uint16_t unique_call_id = 0;
  67. /* slots - begin */
  68. /* data about connection slots */
  69. struct slot {
  70. pid_t pid;
  71. char *local;
  72. char *remote;
  73. } *slots;
  74. /* number of connection slots allocated */
  75. int slot_count;
  76. static void slot_iterate(struct slot *slots, int count, void (*callback) (struct slot *slot))
  77. {
  78. int i;
  79. for(i=0; i<count; i++)
  80. (*callback)(&slots[i]);
  81. }
  82. static void slot_slot_init(struct slot *slot)
  83. {
  84. slot->pid = 0;
  85. slot->local = NULL;
  86. slot->remote = NULL;
  87. }
  88. void slot_init(int count)
  89. {
  90. slot_count = count;
  91. slots = (struct slot *) calloc(slot_count, sizeof(struct slot));
  92. slot_iterate(slots, slot_count, slot_slot_init);
  93. }
  94. static void slot_slot_free(struct slot *slot)
  95. {
  96. slot->pid = 0;
  97. if (slot->local) free(slot->local);
  98. slot->local = NULL;
  99. if (slot->remote) free(slot->remote);
  100. slot->remote = NULL;
  101. }
  102. void slot_free()
  103. {
  104. slot_iterate(slots, slot_count, slot_slot_free);
  105. free(slots);
  106. slots = NULL;
  107. slot_count = 0;
  108. }
  109. void slot_set_local(int i, char *ip)
  110. {
  111. struct slot *slot = &slots[i];
  112. if (slot->local) free(slot->local);
  113. slot->local = strdup(ip);
  114. }
  115. void slot_set_remote(int i, char *ip)
  116. {
  117. struct slot *slot = &slots[i];
  118. if (slot->remote) free(slot->remote);
  119. slot->remote = strdup(ip);
  120. }
  121. void slot_set_pid(int i, pid_t pid)
  122. {
  123. struct slot *slot = &slots[i];
  124. slot->pid = pid;
  125. }
  126. int slot_find_by_pid(pid_t pid)
  127. {
  128. int i;
  129. for(i=0; i<slot_count; i++) {
  130. struct slot *slot = &slots[i];
  131. if (slot->pid == pid) return i;
  132. }
  133. return -1;
  134. }
  135. int slot_find_empty()
  136. {
  137. return slot_find_by_pid(0);
  138. }
  139. char *slot_get_local(int i)
  140. {
  141. struct slot *slot = &slots[i];
  142. return slot->local;
  143. }
  144. char *slot_get_remote(int i)
  145. {
  146. struct slot *slot = &slots[i];
  147. return slot->remote;
  148. }
  149. /* slots - end */
  150. static void sigchld_responder(int sig)
  151. {
  152. int child, status;
  153. while ((child = waitpid(-1, &status, WNOHANG)) > 0) {
  154. if (pptp_delegate) {
  155. if (pptp_debug) syslog(LOG_DEBUG, "MGR: Reaped child %d", child);
  156. } else {
  157. int i;
  158. i = slot_find_by_pid(child);
  159. if (i != -1) {
  160. slot_set_pid(i, 0);
  161. if (pptp_debug) syslog(LOG_DEBUG, "MGR: Reaped child %d", child);
  162. } else {
  163. syslog(LOG_INFO, "MGR: Reaped unknown child %d", child);
  164. }
  165. }
  166. }
  167. }
  168. int pptp_manager(int argc, char **argv)
  169. {
  170. int firstOpen = -1;
  171. int ctrl_pid;
  172. socklen_t addrsize;
  173. int hostSocket;
  174. fd_set connSet;
  175. int rc, sig_fd;
  176. rc = sigpipe_create();
  177. if (rc < 0) {
  178. syslog(LOG_ERR, "MGR: unable to setup sigchld pipe!");
  179. syslog_perror("sigpipe_create");
  180. exit(-1);
  181. }
  182. sigpipe_assign(SIGCHLD);
  183. sigpipe_assign(SIGTERM);
  184. sig_fd = sigpipe_fd();
  185. /* openlog() not required, done in pptpd.c */
  186. syslog(LOG_INFO, "MGR: Manager process started");
  187. if (!pptp_delegate) {
  188. syslog(LOG_INFO, "MGR: Maximum of %d connections available",
  189. pptp_connections);
  190. }
  191. /* Connect the host socket and activate it for listening */
  192. if (createHostSocket(&hostSocket) < 0) {
  193. syslog(LOG_ERR, "MGR: Couldn't create host socket");
  194. syslog_perror("createHostSocket");
  195. exit(-1);
  196. }
  197. while (1) {
  198. int max_fd;
  199. FD_ZERO(&connSet);
  200. if (pptp_delegate) {
  201. FD_SET(hostSocket, &connSet);
  202. } else {
  203. firstOpen = slot_find_empty();
  204. if (firstOpen == -1) {
  205. syslog(LOG_ERR, "MGR: No free connection slots or IPs - no more clients can connect!");
  206. } else {
  207. FD_SET(hostSocket, &connSet);
  208. }
  209. }
  210. max_fd = hostSocket;
  211. FD_SET(sig_fd, &connSet);
  212. if (max_fd < sig_fd) max_fd = sig_fd;
  213. while (1) {
  214. if (select(max_fd + 1, &connSet, NULL, NULL, NULL) != -1) break;
  215. if (errno == EINTR) continue;
  216. syslog(LOG_ERR, "MGR: Error with manager select()!");
  217. syslog_perror("select");
  218. exit(-1);
  219. }
  220. if (FD_ISSET(sig_fd, &connSet)) { /* SIGCHLD */
  221. int signum = sigpipe_read();
  222. if (signum == SIGCHLD)
  223. sigchld_responder(signum);
  224. else if (signum == SIGTERM)
  225. return signum;
  226. }
  227. if (FD_ISSET(hostSocket, &connSet)) { /* A call came! */
  228. int clientSocket;
  229. struct sockaddr_in client_addr;
  230. /* Accept call and launch PPTPCTRL */
  231. addrsize = sizeof(client_addr);
  232. clientSocket = accept(hostSocket, (struct sockaddr *) &client_addr, &addrsize);
  233. #if HAVE_LIBWRAP
  234. if (clientSocket != -1) {
  235. struct request_info r;
  236. request_init(&r, RQ_DAEMON, "pptpd", RQ_FILE, clientSocket, NULL);
  237. fromhost(&r);
  238. if (!hosts_access(&r)) {
  239. /* send a permission denied message? this is a tcp wrapper
  240. * type deny so probably best to just drop it immediately like
  241. * this, as tcp wrappers usually do.
  242. */
  243. close(clientSocket);
  244. /* this would never be file descriptor 0, so use it as a error
  245. * value
  246. */
  247. clientSocket = 0;
  248. }
  249. }
  250. #endif
  251. if (clientSocket == -1) {
  252. /* accept failed, but life goes on... */
  253. syslog(LOG_ERR, "MGR: accept() failed");
  254. syslog_perror("accept");
  255. } else if (clientSocket != 0) {
  256. fd_set rfds;
  257. struct timeval tv;
  258. struct pptp_header ph;
  259. /* TODO: this select below prevents
  260. other connections from being
  261. processed during the wait for the
  262. first data packet from the
  263. client. */
  264. /*
  265. * DOS protection: get a peek at the first packet
  266. * and do some checks on it before we continue.
  267. * A 10 second timeout on the first packet seems reasonable
  268. * to me, if anything looks sus, throw it away.
  269. */
  270. FD_ZERO(&rfds);
  271. FD_SET(clientSocket, &rfds);
  272. tv.tv_sec = pptp_stimeout;
  273. tv.tv_usec = 0;
  274. if (select(clientSocket + 1, &rfds, NULL, NULL, &tv) <= 0) {
  275. syslog(LOG_ERR, "MGR: dropped slow initial connection");
  276. close(clientSocket);
  277. continue;
  278. }
  279. if (recv(clientSocket, &ph, sizeof(ph), MSG_PEEK) !=
  280. sizeof(ph)) {
  281. syslog(LOG_ERR, "MGR: dropped small initial connection");
  282. close(clientSocket);
  283. continue;
  284. }
  285. ph.length = ntohs(ph.length);
  286. ph.pptp_type = ntohs(ph.pptp_type);
  287. ph.magic = ntohl(ph.magic);
  288. ph.ctrl_type = ntohs(ph.ctrl_type);
  289. if (ph.length <= 0 || ph.length > PPTP_MAX_CTRL_PCKT_SIZE) {
  290. syslog(LOG_WARNING, "MGR: initial packet length %d outside "
  291. "(0 - %d)", ph.length, PPTP_MAX_CTRL_PCKT_SIZE);
  292. goto dos_exit;
  293. }
  294. if (ph.magic != PPTP_MAGIC_COOKIE) {
  295. syslog(LOG_WARNING, "MGR: initial packet bad magic");
  296. goto dos_exit;
  297. }
  298. if (ph.pptp_type != PPTP_CTRL_MESSAGE) {
  299. syslog(LOG_WARNING, "MGR: initial packet has bad type");
  300. goto dos_exit;
  301. }
  302. if (ph.ctrl_type != START_CTRL_CONN_RQST) {
  303. syslog(LOG_WARNING, "MGR: initial packet has bad ctrl type "
  304. "0x%x", ph.ctrl_type);
  305. dos_exit:
  306. close(clientSocket);
  307. continue;
  308. }
  309. #ifndef HAVE_FORK
  310. switch (ctrl_pid = vfork()) {
  311. #else
  312. switch (ctrl_pid = fork()) {
  313. #endif
  314. case -1: /* error */
  315. syslog(LOG_ERR, "MGR: fork() failed launching " PPTP_CTRL_BIN);
  316. close(clientSocket);
  317. break;
  318. case 0: /* child */
  319. close(hostSocket);
  320. if (pptp_debug)
  321. syslog(LOG_DEBUG, "MGR: Launching " PPTP_CTRL_BIN " to handle client");
  322. connectCall(clientSocket, !pptp_delegate ? firstOpen : 0);
  323. _exit(1);
  324. /* NORETURN */
  325. default: /* parent */
  326. close(clientSocket);
  327. unique_call_id += MAX_CALLS_PER_TCP_LINK;
  328. if (!pptp_delegate)
  329. slot_set_pid(firstOpen, ctrl_pid);
  330. break;
  331. }
  332. }
  333. } /* FD_ISSET(hostSocket, &connSet) */
  334. } /* while (1) */
  335. } /* pptp_manager() */
  336. /*
  337. * Author: Kevin Thayer
  338. *
  339. * This creates a socket to listen on, sets the max # of pending connections and
  340. * various other options.
  341. *
  342. * Returns the fd of the host socket.
  343. *
  344. * The function return values are:
  345. * 0 for sucessful
  346. * -1 for bad socket creation
  347. * -2 for bad socket options
  348. * -3 for bad bind
  349. * -4 for bad listen
  350. */
  351. static int createHostSocket(int *hostSocket)
  352. {
  353. int opt = 1;
  354. struct sockaddr_in address;
  355. #ifdef HAVE_GETSERVBYNAME
  356. struct servent *serv;
  357. #endif
  358. /* create the master socket and check it worked */
  359. if ((*hostSocket = vrf_socket(vrf, AF_INET, SOCK_STREAM, 0)) == 0)
  360. return -1;
  361. /* set master socket to allow daemon to be restarted with connections active */
  362. if (setsockopt(*hostSocket, SOL_SOCKET, SO_REUSEADDR,
  363. (char *) &opt, sizeof(opt)) < 0)
  364. return -2;
  365. /* set up socket */
  366. memset(&address, 0, sizeof(address));
  367. address.sin_family = AF_INET;
  368. if(bindaddr)
  369. address.sin_addr.s_addr = inet_addr(bindaddr);
  370. else
  371. address.sin_addr.s_addr = INADDR_ANY;
  372. #ifdef HAVE_GETSERVBYNAME
  373. if ((serv = getservbyname("pptp", "tcp")) != NULL) {
  374. address.sin_port = serv->s_port;
  375. } else
  376. #endif
  377. address.sin_port = htons(PPTP_PORT);
  378. /* bind the socket to the pptp port */
  379. if (bind(*hostSocket, (struct sockaddr *) &address, sizeof(address)) < 0)
  380. return -3;
  381. /* minimal backlog to avoid DoS */
  382. if (listen(*hostSocket, 3) < 0)
  383. return -4;
  384. return 0;
  385. }
  386. /*
  387. * Author: Kevin Thayer
  388. *
  389. * this routine sets up the arguments for the call handler and calls it.
  390. */
  391. static void connectCall(int clientSocket, int clientNumber)
  392. {
  393. #define NUM2ARRAY(array, num) snprintf(array, sizeof(array), "%d", num)
  394. char *ctrl_argv[16]; /* arguments for launching 'pptpctrl' binary */
  395. int pptpctrl_argc = 0; /* count the number of arguments sent to pptpctrl */
  396. /* lame strings to hold passed args. */
  397. char ctrl_debug[2];
  398. char ctrl_noipparam[2];
  399. char pppdoptfile_argv[2];
  400. char speedgiven_argv[2];
  401. extern char **environ;
  402. char callid_argv[16];
  403. /*
  404. * Launch the CTRL manager binary; we send it some information such as
  405. * speed and option file on the command line.
  406. */
  407. ctrl_argv[pptpctrl_argc++] = PPTP_CTRL_BIN " ";
  408. /* Pass socket as stdin */
  409. if (clientSocket != 0) {
  410. dup2(clientSocket, 0);
  411. close(clientSocket);
  412. }
  413. /* get argv set up */
  414. NUM2ARRAY(ctrl_debug, pptp_debug ? 1 : 0);
  415. ctrl_debug[1] = '\0';
  416. ctrl_argv[pptpctrl_argc++] = ctrl_debug;
  417. NUM2ARRAY(ctrl_noipparam, pptp_noipparam ? 1 : 0);
  418. ctrl_noipparam[1] = '\0';
  419. ctrl_argv[pptpctrl_argc++] = ctrl_noipparam;
  420. #ifdef VRF
  421. ctrl_argv[pptpctrl_argc++] = vrf ? vrf : "";
  422. #endif
  423. /* optionfile = TRUE or FALSE; so the CTRL manager knows whether to load a non-standard options file */
  424. NUM2ARRAY(pppdoptfile_argv, pppdoptstr ? 1 : 0);
  425. pppdoptfile_argv[1] = '\0';
  426. ctrl_argv[pptpctrl_argc++] = pppdoptfile_argv;
  427. if (pppdoptstr) {
  428. /* send the option filename so the CTRL manager can launch pppd with this alternate file */
  429. ctrl_argv[pptpctrl_argc++] = pppdoptstr;
  430. }
  431. /* tell the ctrl manager whether we were given a speed */
  432. NUM2ARRAY(speedgiven_argv, speedstr ? 1 : 0);
  433. speedgiven_argv[1] = '\0';
  434. ctrl_argv[pptpctrl_argc++] = speedgiven_argv;
  435. if (speedstr) {
  436. /* send the CTRL manager the speed of the connection so it can fire pppd at that speed */
  437. ctrl_argv[pptpctrl_argc++] = speedstr;
  438. }
  439. if (pptp_delegate) {
  440. /* no local or remote address to specify */
  441. ctrl_argv[pptpctrl_argc++] = "0";
  442. ctrl_argv[pptpctrl_argc++] = "0";
  443. } else {
  444. /* specify local & remote addresses for this call */
  445. ctrl_argv[pptpctrl_argc++] = "1";
  446. ctrl_argv[pptpctrl_argc++] = slot_get_local(clientNumber);
  447. ctrl_argv[pptpctrl_argc++] = "1";
  448. ctrl_argv[pptpctrl_argc++] = slot_get_remote(clientNumber);
  449. }
  450. /* our call id to be included in GRE packets the client
  451. * will send to us */
  452. NUM2ARRAY(callid_argv, unique_call_id);
  453. ctrl_argv[pptpctrl_argc++] = callid_argv;
  454. /* pass path to ppp binary */
  455. ctrl_argv[pptpctrl_argc++] = ppp_binary;
  456. /* pass logwtmp flag */
  457. ctrl_argv[pptpctrl_argc++] = pptp_logwtmp ? "1" : "0";
  458. /* note: update pptpctrl.8 if the argument list format is changed */
  459. /* terminate argv array with a NULL */
  460. ctrl_argv[pptpctrl_argc] = NULL;
  461. pptpctrl_argc++;
  462. /* ok, args are setup: invoke the call handler */
  463. execve(PPTP_CTRL_BIN, ctrl_argv, environ);
  464. syslog(LOG_ERR, "MGR: Failed to exec " PPTP_CTRL_BIN "!");
  465. _exit(1);
  466. }