pptpctrl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * pptpctrl.c
  3. *
  4. * PPTP control connection between PAC-PNS pair
  5. *
  6. * $Id: pptpctrl.c,v 1.25 2013/02/07 00:32:29 quozl Exp $
  7. */
  8. #ifdef HAVE_CONFIG_H
  9. #include "config.h"
  10. #endif
  11. #ifdef __linux__
  12. #define _GNU_SOURCE 1 /* kill() prototype, broken arpa/inet.h */
  13. #endif
  14. #include "our_syslog.h"
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <sys/time.h>
  24. #include <dirent.h>
  25. #include <sys/types.h>
  26. #include <sys/wait.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <arpa/inet.h>
  30. #ifdef HAVE_OPENPTY
  31. #ifdef HAVE_PTY_H
  32. #include <pty.h>
  33. #include <termios.h>
  34. #endif
  35. #ifdef HAVE_LIBUTIL_H
  36. #include <libutil.h>
  37. #endif
  38. #endif
  39. #ifdef __UCLIBC__
  40. #define socklen_t int
  41. #endif
  42. #include "compat.h"
  43. #include "pptpctrl.h"
  44. #include "pptpgre.h"
  45. #include "pptpdefs.h"
  46. #include "ctrlpacket.h"
  47. #include "defaults.h"
  48. // placing net/if.h here fixes build on Solaris
  49. #include <net/if.h>
  50. #ifdef VRF
  51. char *vrf = NULL;
  52. #endif
  53. static char *ppp_binary = PPP_BINARY;
  54. static int pptp_logwtmp;
  55. static int noipparam; /* if true, don't send ipparam to ppp */
  56. static char speed[32];
  57. static char pppdxfig[256];
  58. static pid_t pppfork; /* so we can kill it after disconnect */
  59. /*
  60. * Global to handle dying
  61. *
  62. * I'd be nice if someone could figure out a way to do it
  63. * without the global, but i don't think you can.. -tmk
  64. */
  65. #define clientSocket 0 /* in case it changes back to a variable */
  66. static u_int32_t call_id_pair; /* call id (to terminate call) */
  67. /* Needed by this and ctrlpacket.c */
  68. int pptpctrl_debug = 0; /* specifies if debugging is on or off */
  69. uint16_t unique_call_id = 0xFFFF; /* Start value for our call IDs on this TCP link */
  70. int gargc; /* Command line argument count */
  71. char **gargv; /* Command line argument vector */
  72. /* Local function prototypes */
  73. static void bail(int sigraised);
  74. static void pptp_handle_ctrl_connection(char **pppaddrs, struct in_addr *inetaddrs);
  75. static int startCall(char **pppaddrs, struct in_addr *inetaddrs);
  76. static void launch_pppd(char **pppaddrs, struct in_addr *inetaddrs);
  77. /* Oh the horror.. lets hope this covers all the ones we have to handle */
  78. #if defined(O_NONBLOCK) && !defined(__sun__) && !defined(__sun)
  79. #define OUR_NB_MODE O_NONBLOCK
  80. #else
  81. #define OUR_NB_MODE O_NDELAY
  82. #endif
  83. /* read a command line argument, a flag alone */
  84. #define GETARG_INT(X) \
  85. X = atoi(argv[arg++])
  86. /* read a command line argument, a string alone */
  87. #define GETARG_STRING(X) \
  88. X = strdup(argv[arg++])
  89. /* read a command line argument, a presence flag followed by string */
  90. #define GETARG_VALUE(X) \
  91. if(atoi(argv[arg++]) != 0) \
  92. strlcpy(X, argv[arg++], sizeof(X)); \
  93. else \
  94. *X = '\0'
  95. int main(int argc, char **argv)
  96. {
  97. char pppLocal[16]; /* local IP to pass to pppd */
  98. char pppRemote[16]; /* remote IP address to pass to pppd */
  99. struct sockaddr_in addr; /* client address */
  100. socklen_t addrlen;
  101. int arg = 1;
  102. int flags;
  103. struct in_addr inetaddrs[2];
  104. char *pppaddrs[2] = { pppLocal, pppRemote };
  105. gargc = argc;
  106. gargv = argv;
  107. /* fail if argument count invalid */
  108. if (argc < 7) {
  109. fprintf(stderr, "pptpctrl: insufficient arguments, see man pptpctrl\n");
  110. exit(2);
  111. }
  112. /* open a connection to the syslog daemon */
  113. openlog("pptpd", LOG_PID, PPTP_FACILITY);
  114. /* autoreap if supported */
  115. signal(SIGCHLD, SIG_IGN);
  116. /* note: update pptpctrl.8 if the argument list format is changed */
  117. GETARG_INT(pptpctrl_debug);
  118. GETARG_INT(noipparam);
  119. #ifdef VRF
  120. GETARG_STRING(vrf);
  121. #endif
  122. GETARG_VALUE(pppdxfig);
  123. GETARG_VALUE(speed);
  124. GETARG_VALUE(pppLocal);
  125. GETARG_VALUE(pppRemote);
  126. if (arg < argc) GETARG_INT(unique_call_id);
  127. if (arg < argc) GETARG_STRING(ppp_binary);
  128. if (arg < argc) GETARG_INT(pptp_logwtmp);
  129. #ifdef VRF
  130. if (!*vrf) {
  131. free(vrf);
  132. vrf = NULL;
  133. }
  134. #endif
  135. if (pptpctrl_debug) {
  136. #ifdef VRF
  137. syslog(LOG_DEBUG, "CTRL: VRF used = %s", vrf ? vrf : "NONE");
  138. #endif
  139. if (*pppLocal)
  140. syslog(LOG_DEBUG, "CTRL: local address = %s", pppLocal);
  141. if (*pppRemote)
  142. syslog(LOG_DEBUG, "CTRL: remote address = %s", pppRemote);
  143. if (*speed)
  144. syslog(LOG_DEBUG, "CTRL: pppd speed = %s", speed);
  145. if (*pppdxfig)
  146. syslog(LOG_DEBUG, "CTRL: pppd options file = %s", pppdxfig);
  147. }
  148. addrlen = sizeof(addr);
  149. if (getsockname(clientSocket, (struct sockaddr *) &addr, &addrlen) != 0) {
  150. syslog(LOG_ERR, "CTRL: getsockname() failed");
  151. syslog_perror("getsockname");
  152. close(clientSocket);
  153. bail(0); /* NORETURN */
  154. }
  155. inetaddrs[0] = addr.sin_addr;
  156. addrlen = sizeof(addr);
  157. if (getpeername(clientSocket, (struct sockaddr *) &addr, &addrlen) != 0) {
  158. syslog(LOG_ERR, "CTRL: getpeername() failed");
  159. syslog_perror("getpeername");
  160. close(clientSocket);
  161. bail(0); /* NORETURN */
  162. }
  163. inetaddrs[1] = addr.sin_addr;
  164. /* Set non-blocking */
  165. if ((flags = fcntl(clientSocket, F_GETFL, arg /* ignored */)) == -1 ||
  166. fcntl(clientSocket, F_SETFL, flags|OUR_NB_MODE) == -1) {
  167. syslog(LOG_ERR, "CTRL: Failed to set client socket non-blocking");
  168. syslog_perror("fcntl");
  169. close(clientSocket);
  170. bail(0); /* NORETURN */
  171. }
  172. /* Fiddle with argv */
  173. my_setproctitle(gargc, gargv, "pptpd [%s]%20c",
  174. inet_ntoa(addr.sin_addr), ' ');
  175. /* be ready for a grisly death */
  176. sigpipe_create();
  177. sigpipe_assign(SIGTERM);
  178. NOTE_VALUE(PAC, call_id_pair, htons(-1));
  179. NOTE_VALUE(PNS, call_id_pair, htons(-1));
  180. syslog(LOG_INFO, "CTRL: Client %s control connection started", inet_ntoa(addr.sin_addr));
  181. pptp_handle_ctrl_connection(pppaddrs, inetaddrs);
  182. syslog(LOG_DEBUG, "CTRL: Reaping child PPP[%i]", pppfork);
  183. if (pppfork > 0)
  184. waitpid(pppfork, NULL, 0);
  185. syslog(LOG_INFO, "CTRL: Client %s control connection finished", inet_ntoa(addr.sin_addr));
  186. bail(0); /* NORETURN */
  187. return 1; /* make gcc happy */
  188. }
  189. /*
  190. * Local functions only below
  191. */
  192. /*
  193. * pptp_handle_ctrl_connection
  194. *
  195. * 1. read a packet (should be start_ctrl_conn_rqst)
  196. * 2. reply to packet (send a start_ctrl_conn_rply)
  197. * 3. proceed with GRE and CTRL connections
  198. *
  199. * args: pppaddrs - ppp local and remote addresses (strings)
  200. * inetaddrs - local and client socket address
  201. * retn: 0 success, -1 failure
  202. */
  203. static void pptp_handle_ctrl_connection(char **pppaddrs, struct in_addr *inetaddrs)
  204. {
  205. /* For echo requests used to check link is alive */
  206. int echo_wait = FALSE; /* Waiting for echo? */
  207. u_int32_t echo_count = 0; /* Sequence # of echo */
  208. time_t echo_time = 0; /* Time last echo req sent */
  209. time_t last_time = time(NULL); /* Time last received data */
  210. struct timeval idleTime; /* How long to select() */
  211. /* General local variables */
  212. ssize_t rply_size; /* Reply packet size */
  213. fd_set fds; /* For select() */
  214. int maxfd = clientSocket; /* For select() */
  215. int send_packet; /* Send a packet this time? */
  216. #if BSDUSER_PPP || SLIRP
  217. /* not needed by stuff which uses socketpair() in startCall() */
  218. #define init 1
  219. #else
  220. int init = 0; /* Has pppd initialized the pty? */
  221. #endif
  222. int pty_fd = -1; /* File descriptor of pty */
  223. int gre_fd = -1; /* Network file descriptor */
  224. int sig_fd = sigpipe_fd(); /* Signal pipe descriptor */
  225. unsigned char packet[PPTP_MAX_CTRL_PCKT_SIZE];
  226. unsigned char rply_packet[PPTP_MAX_CTRL_PCKT_SIZE];
  227. for (;;) {
  228. FD_ZERO(&fds);
  229. FD_SET(sig_fd, &fds);
  230. FD_SET(clientSocket, &fds);
  231. if (pty_fd != -1)
  232. FD_SET(pty_fd, &fds);
  233. if (gre_fd != -1 && init)
  234. FD_SET(gre_fd, &fds);
  235. /* set timeout */
  236. if (encaps_gre(-1, NULL, 0) || decaps_hdlc(-1, NULL, 0)) {
  237. idleTime.tv_sec = 0;
  238. idleTime.tv_usec = 50000; /* don't ack immediately */
  239. } else {
  240. idleTime.tv_sec = IDLE_WAIT;
  241. idleTime.tv_usec = 0;
  242. }
  243. /* default: do nothing */
  244. send_packet = FALSE;
  245. switch (select(maxfd + 1, &fds, NULL, NULL, &idleTime)) {
  246. case -1: /* Error with select() */
  247. if (errno != EINTR)
  248. syslog(LOG_ERR, "CTRL: Error with select(), quitting");
  249. goto leave_clear_call;
  250. case 0:
  251. if (decaps_hdlc(-1, NULL, 0)) {
  252. if(decaps_hdlc(-1, encaps_gre, gre_fd))
  253. syslog(LOG_ERR, "CTRL: GRE re-xmit failed");
  254. } else if (encaps_gre(-1, NULL, 0))
  255. /* Pending ack and nothing else to do */
  256. encaps_gre(gre_fd, NULL, 0); /* send ack with no payload */
  257. break;
  258. default:
  259. break;
  260. }
  261. /* check for pending SIGTERM delivery */
  262. if (FD_ISSET(sig_fd, &fds)) {
  263. if (sigpipe_read() == SIGTERM)
  264. bail(SIGTERM);
  265. }
  266. /* detect startup of pppd */
  267. #ifndef init
  268. if (!init && pty_fd != -1 && FD_ISSET(pty_fd, &fds))
  269. init = 1;
  270. #endif
  271. /* handle actual packets */
  272. /* send from pty off via GRE */
  273. if (pty_fd != -1 && FD_ISSET(pty_fd, &fds) && decaps_hdlc(pty_fd, encaps_gre, gre_fd) < 0) {
  274. syslog(LOG_ERR, "CTRL: PTY read or GRE write failed (pty,gre)=(%d,%d)", pty_fd, gre_fd);
  275. break;
  276. }
  277. /* send from GRE off to pty */
  278. if (gre_fd != -1 && FD_ISSET(gre_fd, &fds) && decaps_gre(gre_fd, encaps_hdlc, pty_fd) < 0) {
  279. if (gre_fd == 6 && pty_fd == 5) {
  280. syslog(LOG_ERR, "CTRL: GRE-tunnel has collapsed (GRE read or PTY write failed (gre,pty)=(%d,%d))", gre_fd, pty_fd);
  281. } else {
  282. syslog(LOG_ERR, "CTRL: GRE read or PTY write failed (gre,pty)=(%d,%d)", gre_fd, pty_fd);
  283. }
  284. break;
  285. }
  286. /* handle control messages */
  287. if (FD_ISSET(clientSocket, &fds)) {
  288. time(&last_time);
  289. send_packet = TRUE;
  290. switch (read_pptp_packet(clientSocket, packet, rply_packet, &rply_size)) {
  291. case 0:
  292. syslog(LOG_ERR, "CTRL: CTRL read failed");
  293. goto leave_drop_call;
  294. case -1:
  295. send_packet = FALSE;
  296. break;
  297. case STOP_CTRL_CONN_RQST:
  298. if (pptpctrl_debug)
  299. syslog(LOG_DEBUG, "CTRL: Received STOP CTRL CONN request (disconnecting)");
  300. if (gre_fd != -1 || pty_fd != -1)
  301. syslog(LOG_WARNING, "CTRL: Request to close control connection when call is open, closing");
  302. send_pptp_packet(clientSocket, rply_packet, rply_size);
  303. goto leave_drop_call;
  304. case CALL_CLR_RQST:
  305. if(pptpctrl_debug)
  306. syslog(LOG_DEBUG, "CTRL: Received CALL CLR request (closing call)");
  307. if (gre_fd == -1 || pty_fd == -1)
  308. syslog(LOG_WARNING, "CTRL: Request to close call but call not open");
  309. if (gre_fd != -1) {
  310. FD_CLR(gre_fd, &fds);
  311. close(gre_fd);
  312. gre_fd = -1;
  313. }
  314. if (pty_fd != -1) {
  315. FD_CLR(pty_fd, &fds);
  316. close(pty_fd);
  317. pty_fd = -1;
  318. }
  319. /* violating RFC */
  320. goto leave_drop_call;
  321. case OUT_CALL_RQST:
  322. /* for killing off the link later (ugly) */
  323. NOTE_VALUE(PAC, call_id_pair, ((struct pptp_out_call_rply *) (rply_packet))->call_id);
  324. NOTE_VALUE(PNS, call_id_pair, ((struct pptp_out_call_rply *) (rply_packet))->call_id_peer);
  325. if (gre_fd != -1 || pty_fd != -1) {
  326. syslog(LOG_WARNING, "CTRL: Request to open call when call is already open, closing");
  327. if (gre_fd != -1) {
  328. FD_CLR(gre_fd, &fds);
  329. close(gre_fd);
  330. gre_fd = -1;
  331. }
  332. if (pty_fd != -1) {
  333. FD_CLR(pty_fd, &fds);
  334. close(pty_fd);
  335. pty_fd = -1;
  336. }
  337. }
  338. /* change process title for accounting and status scripts */
  339. my_setproctitle(gargc, gargv,
  340. "pptpd [%s:%04X - %04X]",
  341. inet_ntoa(inetaddrs[1]),
  342. ntohs(((struct pptp_out_call_rply *) (rply_packet))->call_id_peer),
  343. ntohs(((struct pptp_out_call_rply *) (rply_packet))->call_id));
  344. /* start the call, by launching pppd */
  345. syslog(LOG_INFO, "CTRL: Starting call (launching pppd, opening GRE)");
  346. pty_fd = startCall(pppaddrs, inetaddrs);
  347. if (pty_fd > maxfd) maxfd = pty_fd;
  348. if ((gre_fd = pptp_gre_init(call_id_pair, pty_fd, inetaddrs)) > maxfd)
  349. maxfd = gre_fd;
  350. break;
  351. case ECHO_RPLY:
  352. if (echo_wait == TRUE && ((struct pptp_echo_rply *) (packet))->identifier == echo_count)
  353. echo_wait = FALSE;
  354. else
  355. syslog(LOG_WARNING, "CTRL: Unexpected ECHO REPLY packet");
  356. /* FALLTHRU */
  357. case SET_LINK_INFO:
  358. send_packet = FALSE;
  359. break;
  360. #ifdef PNS_MODE
  361. case IN_CALL_RQST:
  362. case IN_CALL_RPLY:
  363. case IN_CALL_CONN:
  364. #endif
  365. case CALL_DISCONN_NTFY:
  366. case STOP_CTRL_CONN_RPLY:
  367. /* These don't generate replies. Also they come from things we don't send in this section. */
  368. syslog(LOG_WARNING, "CTRL: Got a reply to a packet we didn't send");
  369. send_packet = FALSE;
  370. break;
  371. /* Otherwise, the already-formed reply will do fine, so send it */
  372. }
  373. /* send echo request packet if we have not heard from the TCP socket in IDLE_TIME */
  374. } else if (echo_wait != TRUE && (time(NULL) - last_time) > IDLE_WAIT) {
  375. echo_count++;
  376. if (pptpctrl_debug)
  377. syslog(LOG_DEBUG, "CTRL: Sending ECHO REQ id %d", echo_count);
  378. time(&echo_time);
  379. make_echo_req_packet(rply_packet, &rply_size, echo_count);
  380. echo_wait = TRUE;
  381. send_packet = TRUE;
  382. }
  383. /* send reply packet - this may block, but it should be very rare */
  384. if (send_packet == TRUE && send_pptp_packet(clientSocket, rply_packet, rply_size) < 0) {
  385. syslog(LOG_ERR, "CTRL: Error sending GRE, aborting call");
  386. goto leave_clear_call;
  387. }
  388. /* waiting for echo reply and curtime - echo_time > max wait */
  389. if (echo_wait == TRUE && (time(NULL) - echo_time) > MAX_ECHO_WAIT) {
  390. syslog(LOG_INFO, "CTRL: Session timed out, ending call");
  391. goto leave_clear_call;
  392. }
  393. }
  394. /* Finished! :-) */
  395. leave_drop_call:
  396. NOTE_VALUE(PAC, call_id_pair, htons(-1));
  397. NOTE_VALUE(PNS, call_id_pair, htons(-1));
  398. close(clientSocket);
  399. leave_clear_call:
  400. /* leave clientSocket and call_id_pair alone for bail() */
  401. if (gre_fd != -1)
  402. close(gre_fd);
  403. gre_fd = -1;
  404. if (pty_fd != -1)
  405. close(pty_fd);
  406. pty_fd = -1;
  407. return;
  408. #ifdef init
  409. #undef init
  410. #endif
  411. }
  412. /*
  413. * This is the custom exit() for this program.
  414. *
  415. * Updated to also be the default SIGTERM handler, and if
  416. * the link is going down for unnatural reasons, we will close it
  417. * right now, it's only been tested for win98, other tests would be nice
  418. * -tmk
  419. */
  420. static void bail(int sigraised)
  421. {
  422. if (sigraised)
  423. syslog(LOG_INFO, "CTRL: Exiting on signal %d", sigraised);
  424. /* send a disconnect to the other end */
  425. /* ignore any errors */
  426. if (GET_VALUE(PAC, call_id_pair) != htons(-1)) {
  427. fd_set connSet; /* fd_set for select() */
  428. struct timeval tv; /* time to wait for reply */
  429. unsigned char packet[PPTP_MAX_CTRL_PCKT_SIZE];
  430. unsigned char rply_packet[PPTP_MAX_CTRL_PCKT_SIZE];
  431. ssize_t rply_size; /* reply packet size */
  432. int pkt;
  433. int retry = 0;
  434. if (pptpctrl_debug)
  435. syslog(LOG_DEBUG, "CTRL: Exiting with active call");
  436. make_call_admin_shutdown(rply_packet, &rply_size);
  437. if(send_pptp_packet(clientSocket, rply_packet, rply_size) < 0)
  438. goto skip;
  439. make_stop_ctrl_req(rply_packet, &rply_size);
  440. if(send_pptp_packet(clientSocket, rply_packet, rply_size) < 0)
  441. goto skip;
  442. FD_ZERO(&connSet);
  443. FD_SET(clientSocket, &connSet);
  444. tv.tv_sec = 5; /* wait 5 secs for a reply then quit */
  445. tv.tv_usec = 0;
  446. /* Wait for STOP CTRL CONN RQST or RPLY */
  447. while (select(clientSocket + 1, &connSet, NULL, NULL, &tv) == 1) {
  448. switch((pkt = read_pptp_packet(clientSocket, packet, rply_packet, &rply_size))) {
  449. case STOP_CTRL_CONN_RQST:
  450. send_pptp_packet(clientSocket, rply_packet, rply_size);
  451. goto skip;
  452. case CALL_CLR_RQST:
  453. syslog(LOG_WARNING, "CTRL: Got call clear request after call manually shutdown - buggy client");
  454. break;
  455. case STOP_CTRL_CONN_RPLY:
  456. goto skip;
  457. case -1:
  458. syslog(LOG_WARNING, "CTRL: Retryable error in disconnect sequence");
  459. retry++;
  460. break;
  461. case 0:
  462. syslog(LOG_WARNING, "CTRL: Fatal error reading control message in disconnect sequence");
  463. goto skip;
  464. default:
  465. syslog(LOG_WARNING, "CTRL: Unexpected control message %d in disconnect sequence", pkt);
  466. retry++;
  467. break;
  468. }
  469. tv.tv_sec = 5; /* wait 5 secs for another reply then quit */
  470. tv.tv_usec = 0;
  471. if (retry > 100) {
  472. syslog(LOG_WARNING, "CTRL: Too many retries (%d) - giving up", retry);
  473. break;
  474. }
  475. }
  476. skip:
  477. close(clientSocket);
  478. }
  479. if (pptpctrl_debug)
  480. syslog(LOG_DEBUG, "CTRL: Exiting now");
  481. }
  482. /*
  483. * startCall
  484. *
  485. * Launches PPPD for the call.
  486. *
  487. * args: pppaddrs - local/remote IPs or "" for either/both if none
  488. * retn: pty file descriptor
  489. *
  490. */
  491. static int startCall(char **pppaddrs, struct in_addr *inetaddrs)
  492. {
  493. /* PTY/TTY pair for talking to PPPd */
  494. int pty_fd, tty_fd;
  495. /* register pids of children */
  496. #if BSDUSER_PPP || SLIRP
  497. int sockfd[2];
  498. #ifndef AF_LOCAL
  499. #ifdef AF_UNIX
  500. #define AF_LOCAL AF_UNIX /* Old BSD */
  501. #else
  502. #define AF_LOCAL AF_FILE /* POSIX */
  503. #endif
  504. #endif
  505. /* userspace ppp doesn't need to waste a real pty/tty pair */
  506. if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd)) {
  507. syslog(LOG_ERR, "CTRL: socketpair() error");
  508. syslog_perror("socketpair");
  509. exit(1);
  510. }
  511. tty_fd = sockfd[0];
  512. pty_fd = sockfd[1];
  513. #else
  514. /* Finds an open pty/tty pair */
  515. if (openpty(&pty_fd, &tty_fd, NULL, NULL, NULL) != 0) {
  516. syslog(LOG_ERR, "CTRL: openpty() error");
  517. syslog_perror("openpty");
  518. exit(1);
  519. } else {
  520. struct termios tios;
  521. /* Turn off echo in the slave - to prevent loopback.
  522. pppd will do this, but might not do it before we
  523. try to send data. */
  524. if (tcgetattr(tty_fd, &tios) < 0) {
  525. syslog(LOG_ERR, "CTRL: tcgetattr() error");
  526. syslog_perror("tcgetattr");
  527. exit(1);
  528. }
  529. tios.c_lflag &= ~(ECHO | ECHONL);
  530. if (tcsetattr(tty_fd, TCSAFLUSH, &tios) < 0) {
  531. syslog(LOG_ERR, "CTRL: tcsetattr() error");
  532. syslog_perror("tcsetattr");
  533. exit(1);
  534. }
  535. }
  536. #endif
  537. if (pptpctrl_debug) {
  538. syslog(LOG_DEBUG, "CTRL: pty_fd = %d", pty_fd);
  539. syslog(LOG_DEBUG, "CTRL: tty_fd = %d", tty_fd);
  540. }
  541. /* Launch the PPPD */
  542. #ifndef HAVE_FORK
  543. switch(pppfork=vfork()){
  544. #else
  545. switch(pppfork=fork()){
  546. #endif
  547. case -1: /* fork() error */
  548. syslog(LOG_ERR, "CTRL: Error forking to exec pppd");
  549. _exit(1);
  550. case 0: /* child */
  551. if (dup2(tty_fd, 0) == -1) {
  552. syslog(LOG_ERR, "CTRL: child tty_fd dup2 to stdin, %s",
  553. strerror(errno));
  554. exit(1);
  555. }
  556. if (dup2(tty_fd, 1) == -1) {
  557. syslog(LOG_ERR, "CTRL: child tty_fd dup2 to stdout, %s",
  558. strerror(errno));
  559. exit(1);
  560. }
  561. #if 0
  562. /* This must never be used if !HAVE_SYSLOG since that logs to stderr.
  563. * Trying just never using it to see if it causes anyone else problems.
  564. * It may let people see the pppd errors, which would be good.
  565. */
  566. dup2(tty_fd, 2);
  567. #endif
  568. if (tty_fd > 1)
  569. close(tty_fd);
  570. if (pty_fd > 1)
  571. close(pty_fd);
  572. /* In case we move clientSocket back off stdin */
  573. #ifndef clientSocket
  574. if (clientSocket > 1)
  575. close(clientSocket);
  576. #elif clientSocket > 1
  577. close(clientSocket);
  578. #endif
  579. launch_pppd(pppaddrs, inetaddrs);
  580. syslog(LOG_ERR, "CTRL: PPPD launch failed! (launch_pppd did not fork)");
  581. _exit(1);
  582. }
  583. close(tty_fd);
  584. return pty_fd;
  585. }
  586. /*
  587. * launch_pppd
  588. *
  589. * Launches the PPP daemon. The PPP daemon is responsible for assigning the
  590. * PPTP client its IP address.. These values are assigned via the command
  591. * line.
  592. *
  593. * Add return of connected ppp interface
  594. *
  595. * retn: 0 on success, -1 on failure.
  596. *
  597. */
  598. static void launch_pppd(char **pppaddrs, struct in_addr *inetaddrs)
  599. {
  600. char *pppd_argv[16];
  601. int an = 0;
  602. sigset_t sigs;
  603. char pppInterfaceIPs[33];
  604. pppd_argv[an++] = ppp_binary;
  605. if (pptpctrl_debug) {
  606. syslog(LOG_DEBUG,
  607. "CTRL (PPPD Launcher): program binary = %s",
  608. pppd_argv[an - 1]);
  609. }
  610. #if BSDUSER_PPP
  611. /* The way that Brian Somers' user-land ppp works is to use the
  612. * system name as a reference for most of the useful options. Hence
  613. * most things can't be defined on the command line. On OpenBSD at
  614. * least the file used for the systems is /etc/ppp/ppp.conf, where
  615. * the pptp stanza should look something like:
  616. pptp:
  617. set speed sync
  618. enable pap
  619. enable chap
  620. set dns a.a.a.a b.b.b.b
  621. set ndbs x.x.x.x y.y.y.y
  622. accept dns
  623. add 10.0.0/24
  624. * To be honest, at the time of writing, I haven't had the thing
  625. * working enough to understand :) I will update this comment and
  626. * make a sample config available when I get there.
  627. */
  628. /* options for BSDUSER_PPP
  629. *
  630. * ignores IP addresses, config file option, speed
  631. * fix usage info in pptpd.c and configure script if this changes
  632. *
  633. * IP addresses can be specified in /etc/ppp/ppp.secret per user
  634. */
  635. pppd_argv[an++] = "-direct";
  636. pppd_argv[an++] = "pptp"; /* XXX this is the system name */
  637. /* should be dynamic - PMG */
  638. #elif SLIRP
  639. /* options for SLIRP
  640. *
  641. * ignores IP addresses from config - SLIRP handles this
  642. */
  643. pppd_argv[an++] = "-P";
  644. pppd_argv[an++] = "+chap";
  645. pppd_argv[an++] = "-b";
  646. /* If a speed has been specified, use it
  647. * if not, use "smart" default (defaults.h)
  648. */
  649. if (*speed) {
  650. pppd_argv[an++] = speed;
  651. } else {
  652. pppd_argv[an++] = PPP_SPEED_DEFAULT;
  653. }
  654. if (*pppdxfig) {
  655. pppd_argv[an++] = "-f";
  656. pppd_argv[an++] = pppdxfig;
  657. }
  658. if (pptpctrl_debug) {
  659. syslog(LOG_DEBUG, "CTRL (PPPD Launcher): Connection speed = %s", pppd_argv[an - 1]);
  660. }
  661. #else
  662. /* options for 'normal' pppd */
  663. pppd_argv[an++] = "local";
  664. /* If a pppd option file is specified, use it
  665. * if not, pppd will default to /etc/ppp/options
  666. */
  667. if (*pppdxfig) {
  668. pppd_argv[an++] = "file";
  669. pppd_argv[an++] = pppdxfig;
  670. }
  671. /* If a speed has been specified, use it
  672. * if not, use "smart" default (defaults.h)
  673. */
  674. if (*speed) {
  675. pppd_argv[an++] = speed;
  676. } else {
  677. pppd_argv[an++] = PPP_SPEED_DEFAULT;
  678. }
  679. if (pptpctrl_debug) {
  680. if (*pppaddrs[0])
  681. syslog(LOG_DEBUG, "CTRL (PPPD Launcher): local address = %s", pppaddrs[0]);
  682. if (*pppaddrs[1])
  683. syslog(LOG_DEBUG, "CTRL (PPPD Launcher): remote address = %s", pppaddrs[1]);
  684. }
  685. if (*pppaddrs[0] || *pppaddrs[1]) {
  686. sprintf(pppInterfaceIPs, "%s:%s", pppaddrs[0], pppaddrs[1]);
  687. pppd_argv[an++] = pppInterfaceIPs;
  688. }
  689. if (!noipparam) {
  690. pppd_argv[an++] = "ipparam";
  691. pppd_argv[an++] = inet_ntoa(inetaddrs[1]);
  692. }
  693. if (pptp_logwtmp) {
  694. pppd_argv[an++] = "plugin";
  695. pppd_argv[an++] = "/usr/lib/pptpd/pptpd-logwtmp.so";
  696. pppd_argv[an++] = "pptpd-original-ip";
  697. pppd_argv[an++] = inet_ntoa(inetaddrs[1]);
  698. }
  699. #endif
  700. pppd_argv[an++] = "remotenumber";
  701. pppd_argv[an++] = inet_ntoa(inetaddrs[1]);
  702. /* argv arrays must always be NULL terminated */
  703. pppd_argv[an++] = NULL;
  704. /* make sure SIGCHLD is unblocked, pppd does not expect it */
  705. sigfillset(&sigs);
  706. sigprocmask(SIG_UNBLOCK, &sigs, NULL);
  707. /* run pppd now */
  708. execvp(pppd_argv[0], pppd_argv);
  709. /* execvp() failed */
  710. syslog(LOG_ERR,
  711. "CTRL (PPPD Launcher): Failed to launch PPP daemon. %s",
  712. strerror(errno));
  713. }