pptpd.c 20 KB

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