1
0

pptpd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*
  2. * pptpd.c
  3. *
  4. * Grabs any command line argument and processes any further options in
  5. * the pptpd config file, before throwing over to pptpmanager.c.
  6. *
  7. * $Id: pptpd.c,v 1.20 2013/02/07 00:12:09 quozl Exp $
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #ifdef __linux__
  13. #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
  14. #endif
  15. #ifdef __svr4__
  16. #define __EXTENSIONS__ 1 /* strdup() prototype */
  17. #endif
  18. #ifdef __sgi__
  19. #define _XOPEN_SOURCE 500 /* strdup() prototype */
  20. #endif
  21. #include "our_syslog.h"
  22. #include "our_getopt.h"
  23. #include <fcntl.h>
  24. #include <netdb.h>
  25. #include <signal.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <sys/wait.h>
  34. #include <sys/stat.h>
  35. #include <unistd.h>
  36. #include "configfile.h"
  37. #include "defaults.h"
  38. #include "compat.h"
  39. #include "pptpmanager.h"
  40. #ifdef CONFIG_NETtel
  41. #include <linux/ledman.h>
  42. #endif
  43. /* command line arg variables */
  44. char *ppp_binary = NULL;
  45. char *pppdoptstr = NULL;
  46. char *speedstr = NULL;
  47. char *bindaddr = NULL;
  48. #ifdef VRF
  49. char *vrf = NULL;
  50. #endif
  51. #ifdef BCRELAY
  52. char *bcrelay = NULL;
  53. #endif
  54. int pptp_debug = 0;
  55. int pptp_noipparam = 0;
  56. int pptp_logwtmp = 0;
  57. int pptp_delegate = 0;
  58. int pptp_stimeout = STIMEOUT_DEFAULT;
  59. int pptp_connections = CONNECTIONS_DEFAULT;
  60. /* Local prototypes */
  61. static void processIPStr(int type, char *ipstr);
  62. #ifndef HAVE_DAEMON
  63. static void my_daemon(int argc, char **argv);
  64. #endif
  65. static void log_pid(char *pid_file);
  66. static char *lookup(char *);
  67. #ifdef BCRELAY
  68. static void launch_bcrelay();
  69. static pid_t bcrelayfork;
  70. #endif
  71. static void showusage(char *prog)
  72. {
  73. printf("\npptpd v%s\n", VERSION);
  74. printf("Usage: pptpd [options], where options are:\n\n");
  75. #ifdef BCRELAY
  76. #define OPT_BCRELAY "b"
  77. printf(" [-b] [--bcrelay if] Use broadcast relay for broadcasts comming from.\n");
  78. printf(" the specified interface (default is eth1).\n");
  79. #else
  80. #define OPT_BCRELAY ""
  81. #endif
  82. printf(" [-c] [--conf file] Specifies the config file to read default\n");
  83. printf(" settings from (default is %s).\n", PPTPD_CONFIG_FILE_DEFAULT);
  84. printf(" [-d] [--debug] Turns on debugging (to syslog).\n");
  85. printf(" [-e] [--ppp file] Use alternate pppd binary, default %s.\n", PPP_BINARY);
  86. printf(" [-f] [--fg] Run in foreground.\n");
  87. printf(" [-h] [--help] Displays this help message.\n");
  88. printf(" [-i] [--noipparam] Suppress the passing of the client's IP address\n");
  89. printf(" to PPP, which is done by default otherwise.\n");
  90. printf(" [-l] [--listen x.x.x.x] Specifies IP of local interface to listen to.\n");
  91. #ifdef VRF
  92. #define OPT_VRFA "V:"
  93. #define OPT_VRF "V"
  94. printf(" [-V] [--vrf name] Use given VRF for GRE/TCP sockets.\n");
  95. #else
  96. #define OPT_VRFA ""
  97. #define OPT_VRF ""
  98. #endif
  99. #if !defined(BSDUSER_PPP)
  100. printf(" [-o] [--option file] Specifies the PPP options file to use\n");
  101. printf(" (default is /etc/ppp/options).\n");
  102. #endif
  103. printf(" [-p] [--pidfile file] Specifies the file to write the process ID to\n");
  104. printf(" (default is /var/run/pptpd.pid).\n");
  105. #if !defined(BSDUSER_PPP)
  106. printf(" [-s] [--speed baud] Specifies the baud speed for the PPP daemon\n");
  107. printf(" (default is 115200).\n");
  108. #endif
  109. printf(" [-t] [--stimeout seconds] Specifies the timeout for the first packet. This is a DOS protection\n");
  110. printf(" (default is 10).\n");
  111. printf(" [-v] [--version] Displays the pptpd version number.\n");
  112. printf(" [-w] [--logwtmp] Update wtmp as users login.\n");
  113. printf(" [-C] [--connections n] Limit on number of connections.\n");
  114. printf(" [-D] [--delegate] Delegate IP allocation to pppd.\n");
  115. printf("\n\nLogs and debugging go to syslog as DAEMON.");
  116. printf("\n\nCommand line options will override any default settings and any settings\n");
  117. printf("specified in the config file (default config file: %s).\n\n", PPTPD_CONFIG_FILE_DEFAULT);
  118. }
  119. static void showversion()
  120. {
  121. printf("pptpd v%s\n", VERSION);
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. /* command line options */
  126. int c;
  127. /* function-local options */
  128. int foreground = FALSE;
  129. char *pid_file = NULL;
  130. /* config file */
  131. char *configFile = NULL;
  132. /* config file parsing temp strings */
  133. char tmp[MAX_CONFIG_STRING_SIZE], *tmpstr;
  134. /* open a connection to the syslog daemon */
  135. openlog("pptpd", LOG_PID, PPTP_FACILITY);
  136. /* process command line options */
  137. while (1) {
  138. int option_index = 0;
  139. char *optstring = OPT_BCRELAY ":c:de:fhil:o:p:s:t:vwC:D" OPT_VRFA;
  140. static struct option long_options[] =
  141. {
  142. #ifdef BCRELAY
  143. {"bcrelay", 1, 0, 0},
  144. #endif
  145. {"conf", 1, 0, 0},
  146. {"debug", 0, 0, 0},
  147. {"ppp", 1, 0, 0},
  148. {"fg", 0, 0, 0},
  149. {"help", 0, 0, 0},
  150. {"noipparam", 0, 0, 0},
  151. {"listen", 1, 0, 0},
  152. {"option", 1, 0, 0},
  153. {"pidfile", 1, 0, 0},
  154. {"speed", 1, 0, 0},
  155. {"stimeout", 1, 0, 0},
  156. {"version", 0, 0, 0},
  157. {"logwtmp", 0, 0, 0},
  158. {"connections", 1, 0, 0},
  159. {"delegate", 0, 0, 0},
  160. #ifdef VRF
  161. {"vrf", 1, 0, 0},
  162. #endif
  163. {0, 0, 0, 0}
  164. };
  165. c = getopt_long(argc, argv, optstring, long_options, &option_index);
  166. if (c == -1)
  167. break;
  168. /* convert long options to short form */
  169. if (c == 0)
  170. c = OPT_BCRELAY "cdefhilopstvwCD" OPT_VRF [option_index];
  171. switch (c) {
  172. #ifdef BCRELAY
  173. case 'b': /* --bcrelay */
  174. if (bcrelay) free(bcrelay);
  175. bcrelay = strdup(optarg);
  176. break;
  177. #endif
  178. case 'l': /* --listen */
  179. tmpstr = lookup(optarg);
  180. if (!tmpstr) {
  181. syslog(LOG_ERR, "MGR: Invalid listening address: %s!", optarg);
  182. return 1;
  183. }
  184. if (bindaddr) free(bindaddr);
  185. bindaddr = strdup(tmpstr);
  186. break;
  187. #ifdef VRF
  188. case 'V': /* --vrf */
  189. if (vrf) free(vrf);
  190. vrf = strdup(optarg);
  191. break;
  192. #endif
  193. case 'h': /* --help */
  194. showusage(argv[0]);
  195. return 0;
  196. case 'i': /* --noipparam */
  197. pptp_noipparam = TRUE;
  198. break;
  199. case 'e': /* --ppp */
  200. if (ppp_binary) free(ppp_binary);
  201. ppp_binary = strdup(optarg);
  202. break;
  203. case 'd': /* --debug */
  204. pptp_debug = TRUE;
  205. break;
  206. case 'f': /* --fg */
  207. foreground = TRUE;
  208. break;
  209. case 'v': /* --version */
  210. showversion();
  211. return 0;
  212. case 'w': /* --logwtmp */
  213. pptp_logwtmp = TRUE;
  214. break;
  215. case 'C': /* --connections */
  216. pptp_connections = atoi(optarg);
  217. break;
  218. case 'D': /* --delegate */
  219. pptp_delegate = TRUE;
  220. break;
  221. case 'o': /* --option */
  222. if (pppdoptstr) free(pppdoptstr);
  223. pppdoptstr = strdup(optarg);
  224. break;
  225. case 'p': /* --pidfile */
  226. if (pid_file) free(pid_file);
  227. pid_file = strdup(optarg);
  228. break;
  229. case 's': /* --speed */
  230. if (speedstr) free(speedstr);
  231. speedstr = strdup(optarg);
  232. break;
  233. case 't': /* --stimeout */
  234. pptp_stimeout = atoi(optarg);
  235. break;
  236. case 'c': /* --conf */
  237. {
  238. FILE *f;
  239. if (!(f = fopen(optarg, "r"))) {
  240. syslog(LOG_ERR, "MGR: Config file not found!");
  241. return 1;
  242. }
  243. fclose(f);
  244. if(configFile) free(configFile);
  245. configFile = strdup(optarg);
  246. break;
  247. }
  248. default:
  249. showusage(argv[0]);
  250. return 1;
  251. }
  252. }
  253. /* Now that we have all the command line args.. lets open the
  254. * conf file and add anything else (remembering not to override
  255. * anything since the command line has more privilages :-)
  256. */
  257. if (!configFile)
  258. configFile = strdup(PPTPD_CONFIG_FILE_DEFAULT);
  259. if (read_config_file(configFile, CONNECTIONS_KEYWORD, tmp) > 0) {
  260. pptp_connections = atoi(tmp);
  261. if (pptp_connections <= 0)
  262. pptp_connections = CONNECTIONS_DEFAULT;
  263. }
  264. slot_init(pptp_connections);
  265. if (!pptp_debug && read_config_file(configFile, DEBUG_KEYWORD, tmp) > 0)
  266. pptp_debug = TRUE;
  267. #ifdef BCRELAY
  268. if (!bcrelay && read_config_file(configFile, BCRELAY_KEYWORD, tmp) > 0)
  269. bcrelay = strdup(tmp);
  270. #endif
  271. if (!pptp_stimeout && read_config_file(configFile, STIMEOUT_KEYWORD, tmp) > 0) {
  272. pptp_stimeout = atoi(tmp);
  273. if (pptp_stimeout <= 0)
  274. pptp_stimeout = STIMEOUT_DEFAULT;
  275. }
  276. if (!pptp_noipparam && read_config_file(configFile, NOIPPARAM_KEYWORD, tmp) > 0) {
  277. pptp_noipparam = TRUE;
  278. }
  279. if (!bindaddr && read_config_file(configFile, LISTEN_KEYWORD, tmp) > 0) {
  280. tmpstr = lookup(tmp);
  281. if(!tmpstr) {
  282. syslog(LOG_ERR, "MGR: Invalid listening address: %s!", tmp);
  283. return 1;
  284. }
  285. bindaddr = strdup(tmpstr);
  286. }
  287. #ifdef VRF
  288. if (!vrf && read_config_file(configFile, VRF_KEYWORD, tmp) > 0) {
  289. vrf = strdup(tmp);
  290. }
  291. #endif
  292. if (!speedstr && read_config_file(configFile, SPEED_KEYWORD, tmp) > 0)
  293. speedstr = strdup(tmp);
  294. if (!pppdoptstr && read_config_file(configFile, PPPD_OPTION_KEYWORD, tmp) > 0) {
  295. pppdoptstr = strdup(tmp);
  296. }
  297. if (!ppp_binary && read_config_file(configFile, PPP_BINARY_KEYWORD, tmp) > 0) {
  298. ppp_binary = strdup(tmp);
  299. }
  300. if (!pptp_logwtmp && read_config_file(configFile, LOGWTMP_KEYWORD, tmp) > 0) {
  301. pptp_logwtmp = TRUE;
  302. }
  303. if (!pptp_delegate && read_config_file(configFile, DELEGATE_KEYWORD, tmp) > 0) {
  304. pptp_delegate = TRUE;
  305. }
  306. if (!pid_file)
  307. pid_file = strdup((read_config_file(configFile, PIDFILE_KEYWORD,
  308. tmp) > 0) ? tmp : PIDFILE_DEFAULT);
  309. if (!pptp_delegate) {
  310. /* NOTE: remote then local, reason can be seen at the end of processIPStr */
  311. /* grab the remoteip string from the config file */
  312. if (read_config_file(configFile, REMOTEIP_KEYWORD, tmp) <= 0) {
  313. /* use "smart" defaults */
  314. strlcpy(tmp, DEFAULT_REMOTE_IP_LIST, sizeof(tmp));
  315. }
  316. processIPStr(REMOTE, tmp);
  317. /* grab the localip string from the config file */
  318. if (read_config_file(configFile, LOCALIP_KEYWORD, tmp) <= 0) {
  319. /* use "smart" defaults */
  320. strlcpy(tmp, DEFAULT_LOCAL_IP_LIST, sizeof(tmp));
  321. }
  322. processIPStr(LOCAL, tmp);
  323. }
  324. free(configFile);
  325. /* if not yet set, adopt default PPP binary path */
  326. if (!ppp_binary) ppp_binary = strdup(PPP_BINARY);
  327. /* check that the PPP binary is executable */
  328. if (access(ppp_binary, X_OK) < 0) {
  329. syslog(LOG_ERR, "MGR: PPP binary %s not executable",
  330. ppp_binary);
  331. return 1;
  332. }
  333. /* check that the PPP options file is readable */
  334. if (pppdoptstr && access(pppdoptstr, R_OK) < 0) {
  335. syslog(LOG_ERR, "MGR: PPP options file %s not readable",
  336. pppdoptstr);
  337. return 1;
  338. }
  339. #ifdef BCRELAY
  340. /* check that the bcrelay binary is executable */
  341. if (bcrelay && access(BCRELAY_BIN, X_OK) < 0) {
  342. syslog(LOG_ERR, "MGR: bcrelay binary %s not executable",
  343. BCRELAY_BIN);
  344. return 1;
  345. }
  346. #endif
  347. if (!foreground) {
  348. #if HAVE_DAEMON
  349. closelog();
  350. freopen("/dev/null", "r", stdin);
  351. daemon(0, 0);
  352. /* returns to child only */
  353. /* pid will have changed */
  354. openlog("pptpd", LOG_PID, PPTP_FACILITY);
  355. #else /* !HAVE_DAEMON */
  356. my_daemon(argc, argv);
  357. /* returns to child if !HAVE_FORK
  358. * never returns if HAVE_FORK (re-execs with -f)
  359. */
  360. #endif
  361. }
  362. #ifdef BCRELAY
  363. if (bcrelay) {
  364. syslog(LOG_DEBUG, "CTRL: BCrelay incoming interface is %s", bcrelay);
  365. /* Launch BCrelay */
  366. #ifndef HAVE_FORK
  367. switch(bcrelayfork = vfork()){
  368. #else
  369. switch(bcrelayfork = fork()){
  370. #endif
  371. case -1: /* fork() error */
  372. syslog(LOG_ERR, "CTRL: Error forking to exec bcrelay");
  373. _exit(1);
  374. case 0: /* child */
  375. syslog(LOG_DEBUG, "CTRL (BCrelay Launcher): Launching BCrelay with pid %i", bcrelayfork);
  376. launch_bcrelay();
  377. syslog(LOG_ERR, "CTRL (BCrelay Launcher): Failed to launch BCrelay.");
  378. _exit(1);
  379. }
  380. } /* End bcrelay */
  381. #endif
  382. #ifdef CONFIG_NETtel
  383. /* turn the NETtel VPN LED on */
  384. ledman_cmd(LEDMAN_CMD_ON, LEDMAN_VPN);
  385. #endif
  386. /* after we have our final pid... */
  387. log_pid(pid_file);
  388. /* manage connections until SIGTERM */
  389. pptp_manager(argc, argv);
  390. #ifdef BCRELAY
  391. if (bcrelayfork > 0) {
  392. syslog(LOG_DEBUG, "CTRL: Closing child BCrelay with pid %i", bcrelayfork);
  393. kill(bcrelayfork, SIGTERM);
  394. }
  395. #endif
  396. slot_free();
  397. return 0;
  398. }
  399. static void log_pid(char *pid_file) {
  400. FILE *f;
  401. pid_t pid;
  402. pid = getpid();
  403. if ((f = fopen(pid_file, "w")) == NULL) {
  404. syslog(LOG_ERR, "PPTPD: failed to open(%s), errno=%d\n",
  405. pid_file, errno);
  406. return;
  407. }
  408. fprintf(f, "%d\n", pid);
  409. fclose(f);
  410. }
  411. #ifndef HAVE_DAEMON
  412. static void my_daemon(int argc, char **argv)
  413. {
  414. #ifndef HAVE_FORK
  415. /* need to use vfork - eg, uClinux */
  416. char **new_argv;
  417. int pid;
  418. extern char **environ;
  419. int fdr;
  420. new_argv = malloc((argc + 2) * sizeof(char **));
  421. fdr = open("/dev/null", O_RDONLY);
  422. syslog(LOG_INFO, "MGR: Option parse OK, re-execing as daemon");
  423. fflush(stderr);
  424. if ((pid = vfork()) == 0) {
  425. if (fdr != 0) { dup2(fdr, 0); close(fdr); }
  426. SETSIDPGRP();
  427. chdir("/");
  428. umask(0);
  429. memcpy(new_argv + 1, argv, (argc + 1) * sizeof(char **));
  430. new_argv[0] = PPTPD_BIN;
  431. new_argv[1] = "-f";
  432. execve(PPTPD_BIN, new_argv, environ);
  433. _exit(1);
  434. } else if (pid > 0) {
  435. exit(0);
  436. } else {
  437. syslog_perror("vfork");
  438. exit(1);
  439. }
  440. #else
  441. int pid;
  442. closelog();
  443. if ((pid = fork()) < 0) {
  444. syslog_perror("fork");
  445. exit(1);
  446. } else if (pid)
  447. exit(0);
  448. freopen("/dev/null", "r", stdin);
  449. SETSIDPGRP();
  450. chdir("/");
  451. umask(0);
  452. /* pid will have changed */
  453. openlog("pptpd", LOG_PID, PPTP_FACILITY);
  454. #endif
  455. }
  456. #endif
  457. /* added for hostname/address lookup -tmk
  458. * returns NULL if not a valid hostname
  459. */
  460. static char *lookup(char *hostname)
  461. {
  462. struct hostent *ent;
  463. struct in_addr hst_addr;
  464. /* Try to parse IP directly */
  465. if (inet_addr(hostname) != -1)
  466. return hostname;
  467. /* Else lookup hostname, return NULL if it fails */
  468. if ((ent = gethostbyname(hostname)) == NULL)
  469. return NULL;
  470. /* That worked, print it back as a dotted quad. */
  471. memcpy(&hst_addr.s_addr, ent->h_addr, ent->h_length);
  472. return inet_ntoa(hst_addr);
  473. }
  474. #define DEBUG_IP_PARSER 0
  475. /* Return the address or NULL if not valid */
  476. static char *validip(char *hostname)
  477. {
  478. /* Try to parse IP directly */
  479. if (inet_addr(hostname) != -1)
  480. return hostname;
  481. else
  482. return NULL;
  483. }
  484. /* Check if it's a valid IP range */
  485. static int isIpRange(char *str)
  486. {
  487. int dashes = 0;
  488. int dots = 0;
  489. #if DEBUG_IP_PARSER
  490. syslog(LOG_DEBUG, "MGR: Checking if %s is a valid IP range", str);
  491. #endif
  492. do {
  493. if (*str == '-')
  494. dashes++;
  495. else if (*str == '.')
  496. dots++;
  497. else if (!strchr("0123456789", *str)) {
  498. #if DEBUG_IP_PARSER
  499. syslog(LOG_DEBUG, "MGR: Not an IP range: character %c is not valid", *str);
  500. #endif
  501. return 0;
  502. }
  503. } while (*++str);
  504. #if DEBUG_IP_PARSER
  505. syslog(LOG_DEBUG, "MGR: Dashes = %d (wanted: 1), Dots = %d (wanted: 4)", dashes, dots);
  506. #endif
  507. return (dashes == 1 && dots == 3);
  508. }
  509. /* process a type 0 (LOCAL) or type 1 (REMOTE) IP string */
  510. static void processIPStr(int type, char *ipstr)
  511. {
  512. int pos;
  513. char *tmpstr;
  514. /* char tmpstr2[20]; xxx.xxx.xxx.xxx-xxx (largest we can get) */
  515. char tmpstr2[128]; /* allow hostnames */
  516. char *tmpstr3;
  517. char tmpstr5[16];
  518. char *tmpstr6;
  519. char *tmpstr7;
  520. int num;
  521. char ipa[8]; /* xxx-xxx (largest we can get) */
  522. char ipb[8];
  523. char ipc[8];
  524. char ipd[8];
  525. char ip_pre[13]; /* xxx.xxx.xxx. (largest we can get) */
  526. char ip_post[13];
  527. char ipl[4];
  528. char ipu[4];
  529. int bail = FALSE; /* so we know when to stop formatting the ip line */
  530. int lower, upper, n;
  531. num = 0;
  532. while (!bail) {
  533. if ((tmpstr = strchr(ipstr, ',')) == NULL) {
  534. /* last (or only) entry reached */
  535. strlcpy(tmpstr2, ipstr, sizeof(tmpstr2));
  536. bail = TRUE;
  537. } else {
  538. pos = tmpstr - ipstr;
  539. ipstr[pos] = '\0';
  540. strlcpy(tmpstr2, ipstr, sizeof(tmpstr2));
  541. ipstr = tmpstr + 1;
  542. }
  543. #if DEBUG_IP_PARSER
  544. syslog(LOG_DEBUG, "MGR: Parsing segment: %s", tmpstr2);
  545. #endif
  546. if (!isIpRange(tmpstr2)) {
  547. /* We got a normal IP
  548. * Check if the IP address is valid, use it if so
  549. */
  550. if ((tmpstr7 = lookup(tmpstr2)) == NULL) {
  551. syslog(LOG_ERR, "MGR: Bad IP address (%s) in config file!", tmpstr2);
  552. exit(1);
  553. }
  554. if (num == pptp_connections) {
  555. syslog(LOG_WARNING, "MGR: connections limit (%d) reached, extra IP addresses ignored", pptp_connections);
  556. return;
  557. }
  558. #if DEBUG_IP_PARSER
  559. syslog(LOG_DEBUG, "MGR: Setting IP %d = %s", num, tmpstr7);
  560. #endif
  561. if (type == LOCAL)
  562. slot_set_local(num, tmpstr7);
  563. else
  564. slot_set_remote(num, tmpstr7);
  565. num++;
  566. } else {
  567. /* Got a range;
  568. * eg. 192.168.0.234-238
  569. * or (thanx Kev! :-).. i thought i was finished :-)
  570. * 192.168-178.1.231
  571. */
  572. /* lose the "."'s */
  573. while ((tmpstr3 = strchr(tmpstr2, '.')) != NULL) {
  574. pos = tmpstr3 - tmpstr2;
  575. tmpstr2[pos] = ' ';
  576. }
  577. if ((tmpstr3 = strchr(tmpstr2, '-')) == NULL ||
  578. strchr(tmpstr3 + 1, '-') != NULL) {
  579. syslog(LOG_ERR, "MGR: Confused in IP parse routines (multiple hyphens)");
  580. continue;
  581. }
  582. /* should be left with "192 168 0 234-238"
  583. * or 192 168-178 1 231
  584. */
  585. sscanf(tmpstr2, "%7s %7s %7s %7s", ipa, ipb, ipc, ipd);
  586. if ((tmpstr6 = strchr(ipd, '-')) != NULL) {
  587. pos = tmpstr6 - ipd;
  588. ipd[pos] = ' ';
  589. sscanf(ipd, "%3s %3s", ipl, ipu);
  590. #if DEBUG_IP_PARSER
  591. syslog(LOG_DEBUG, "MGR: (lower upper) = (%s %s)", ipl, ipu);
  592. #endif
  593. lower = atoi(ipl);
  594. upper = atoi(ipu);
  595. #if DEBUG_IP_PARSER
  596. syslog(LOG_DEBUG, "MGR: Range = %d to %d on 4th segment", lower, upper);
  597. #endif
  598. sprintf(ip_pre, "%.3s.%.3s.%.3s.", ipa, ipb, ipc);
  599. ip_post[0] = '\0';
  600. #if DEBUG_IP_PARSER
  601. syslog(LOG_DEBUG, "MGR: Pre = %s Post = %s", ip_pre, ip_post);
  602. #endif
  603. } else if ((tmpstr6 = strchr(ipc, '-')) != NULL) {
  604. pos = tmpstr6 - ipc;
  605. ipc[pos] = ' ';
  606. sscanf(ipc, "%3s %3s", ipl, ipu);
  607. #if DEBUG_IP_PARSER
  608. syslog(LOG_DEBUG, "MGR: (lower upper) = (%s %s)", ipl, ipu);
  609. #endif
  610. lower = atoi(ipl);
  611. upper = atoi(ipu);
  612. #if DEBUG_IP_PARSER
  613. syslog(LOG_DEBUG, "MGR: Range = %d to %d on 3rd segment", lower, upper);
  614. #endif
  615. sprintf(ip_pre, "%.3s.%.3s.", ipa, ipb);
  616. sprintf(ip_post, ".%.3s", ipd);
  617. #if DEBUG_IP_PARSER
  618. syslog(LOG_DEBUG, "MGR: Pre = %s Post = %s", ip_pre, ip_post);
  619. #endif
  620. } else if ((tmpstr6 = strchr(ipb, '-')) != NULL) {
  621. pos = tmpstr6 - ipb;
  622. ipb[pos] = ' ';
  623. sscanf(ipb, "%3s %3s", ipl, ipu);
  624. #if DEBUG_IP_PARSER
  625. syslog(LOG_DEBUG, "MGR: (lower upper) = (%s %s)", ipl, ipu);
  626. #endif
  627. lower = atoi(ipl);
  628. upper = atoi(ipu);
  629. #if DEBUG_IP_PARSER
  630. syslog(LOG_DEBUG, "MGR: Range = %d to %d on 2nd segment", lower, upper);
  631. #endif
  632. sprintf(ip_pre, "%.3s.", ipa);
  633. sprintf(ip_post, ".%.3s.%.3s", ipc, ipd);
  634. #if DEBUG_IP_PARSER
  635. syslog(LOG_DEBUG, "MGR: Pre = %s Post = %s", ip_pre, ip_post);
  636. #endif
  637. } else if ((tmpstr6 = strchr(ipa, '-')) != NULL) {
  638. pos = tmpstr6 - ipa;
  639. ipa[pos] = ' ';
  640. sscanf(ipa, "%3s %3s", ipl, ipu);
  641. #if DEBUG_IP_PARSER
  642. syslog(LOG_DEBUG, "MGR: (lower upper) = (%s %s)", ipl, ipu);
  643. #endif
  644. lower = atoi(ipl);
  645. upper = atoi(ipu);
  646. #if DEBUG_IP_PARSER
  647. syslog(LOG_DEBUG, "MGR: Range = %d to %d on 1st segment", lower, upper);
  648. #endif
  649. ip_pre[0] = '\0';
  650. sprintf(ip_post, ".%.3s.%.3s.%.3s", ipb, ipc, ipd);
  651. #if DEBUG_IP_PARSER
  652. syslog(LOG_DEBUG, "MGR: Pre = %s Post = %s", ip_pre, ip_post);
  653. #endif
  654. } else {
  655. syslog(LOG_ERR, "MGR: Confused in IP parse routines (lost hyphen)");
  656. continue;
  657. }
  658. if (upper < lower) {
  659. syslog(LOG_ERR, "MGR: Bad %s IP range: %s",
  660. (type == LOCAL) ? "local" : "remote",
  661. ipstr);
  662. exit(1);
  663. }
  664. for (n = lower; n <= upper; n++) {
  665. sprintf(tmpstr5, "%s%d%s", ip_pre, n, ip_post);
  666. /* Check if the ip address is valid */
  667. if ((tmpstr7 = validip(tmpstr5)) == NULL) {
  668. syslog(LOG_ERR, "MGR: Bad IP address (%s) in config file!", tmpstr5);
  669. exit(1);
  670. }
  671. if (num == pptp_connections) {
  672. syslog(LOG_WARNING, "MGR: connections limit (%d) reached, extra IP addresses ignored", pptp_connections);
  673. return;
  674. }
  675. #if DEBUG_IP_PARSER
  676. syslog(LOG_DEBUG, "MGR: Setting IP %d = %s", num, tmpstr7);
  677. #endif
  678. if (type == LOCAL)
  679. slot_set_local(num, tmpstr7);
  680. else
  681. slot_set_remote(num, tmpstr7);
  682. num++;
  683. }
  684. }
  685. }
  686. if (num == 1 && type == LOCAL && pptp_connections > 1) {
  687. #if DEBUG_IP_PARSER
  688. syslog(LOG_DEBUG, "MGR: Setting all %d local IPs to %s", pptp_connections, slot_get_local(0));
  689. #endif
  690. for (n = 1; n < pptp_connections; n++)
  691. slot_set_local(n, slot_get_local(0));
  692. } else if (pptp_connections > num) {
  693. syslog(LOG_INFO, "MGR: Maximum of %d connections reduced to %d, not enough IP addresses given",
  694. pptp_connections, num);
  695. pptp_connections = num;
  696. }
  697. }
  698. #ifdef BCRELAY
  699. /* launch_bcrelay
  700. * Launches broadcast relay. Broadcast relay is responsible for relaying broadcasts to the clients
  701. * retn: 0 on success, -1 on failure.
  702. */
  703. static void launch_bcrelay() {
  704. char *bcrelay_argv[8];
  705. int an = 0;
  706. if (bcrelay) {
  707. syslog(LOG_DEBUG, "MGR: BCrelay incoming interface is %s", bcrelay);
  708. syslog(LOG_DEBUG, "MGR: BCrelay outgoing interface is regexp ppp[0-9].*");
  709. bcrelay_argv[an++] = BCRELAY_BIN;
  710. bcrelay_argv[an++] = "-i";
  711. bcrelay_argv[an++] = bcrelay;
  712. bcrelay_argv[an++] = "-o";
  713. bcrelay_argv[an++] = "ppp[0-9].*";
  714. if (!pptp_debug) {
  715. bcrelay_argv[an++] = "-n";
  716. }
  717. bcrelay_argv[an++] = NULL;
  718. execvp(bcrelay_argv[0], bcrelay_argv);
  719. }
  720. }
  721. #endif