pptpctrl.c 30 KB

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