auth.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  1. /*
  2. * auth.c - PPP authentication and phase control.
  3. *
  4. * Copyright (c) 1993-2002 Paul Mackerras. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. The name(s) of the authors of this software must not be used to
  14. * endorse or promote products derived from this software without
  15. * prior written permission.
  16. *
  17. * 3. Redistributions of any form whatsoever must retain the following
  18. * acknowledgment:
  19. * "This product includes software developed by Paul Mackerras
  20. * <paulus@samba.org>".
  21. *
  22. * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
  23. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  24. * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  25. * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  26. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  27. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  28. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  29. *
  30. * Derived from main.c, which is:
  31. *
  32. * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
  33. *
  34. * Redistribution and use in source and binary forms, with or without
  35. * modification, are permitted provided that the following conditions
  36. * are met:
  37. *
  38. * 1. Redistributions of source code must retain the above copyright
  39. * notice, this list of conditions and the following disclaimer.
  40. *
  41. * 2. Redistributions in binary form must reproduce the above copyright
  42. * notice, this list of conditions and the following disclaimer in
  43. * the documentation and/or other materials provided with the
  44. * distribution.
  45. *
  46. * 3. The name "Carnegie Mellon University" must not be used to
  47. * endorse or promote products derived from this software without
  48. * prior written permission. For permission or any legal
  49. * details, please contact
  50. * Office of Technology Transfer
  51. * Carnegie Mellon University
  52. * 5000 Forbes Avenue
  53. * Pittsburgh, PA 15213-3890
  54. * (412) 268-4387, fax: (412) 268-7395
  55. * tech-transfer@andrew.cmu.edu
  56. *
  57. * 4. Redistributions of any form whatsoever must retain the following
  58. * acknowledgment:
  59. * "This product includes software developed by Computing Services
  60. * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
  61. *
  62. * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
  63. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  64. * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
  65. * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  66. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
  67. * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  68. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  69. */
  70. #define RCSID "$Id: auth.c,v 1.117 2008/07/01 12:27:56 paulus Exp $"
  71. #include <stdio.h>
  72. #include <stddef.h>
  73. #include <stdlib.h>
  74. #include <unistd.h>
  75. #include <errno.h>
  76. #include <pwd.h>
  77. #include <grp.h>
  78. #include <string.h>
  79. #include <sys/types.h>
  80. #include <sys/stat.h>
  81. #include <sys/socket.h>
  82. #include <utmp.h>
  83. #include <fcntl.h>
  84. #if defined(_PATH_LASTLOG) && defined(__linux__)
  85. #include <lastlog.h>
  86. #endif
  87. #include <netdb.h>
  88. #include <netinet/in.h>
  89. #include <arpa/inet.h>
  90. #ifdef HAS_SHADOW
  91. #include <shadow.h>
  92. #ifndef PW_PPP
  93. #define PW_PPP PW_LOGIN
  94. #endif
  95. #endif
  96. #include <time.h>
  97. #include "pppd.h"
  98. #include "fsm.h"
  99. #include "lcp.h"
  100. #include "ccp.h"
  101. #include "ecp.h"
  102. #include "ipcp.h"
  103. #include "upap.h"
  104. #include "chap-new.h"
  105. #include "eap.h"
  106. #ifdef CBCP_SUPPORT
  107. #include "cbcp.h"
  108. #endif
  109. #include "pathnames.h"
  110. #include "session.h"
  111. static const char rcsid[] = RCSID;
  112. /* Bits in scan_authfile return value */
  113. #define NONWILD_SERVER 1
  114. #define NONWILD_CLIENT 2
  115. #define ISWILD(word) (word[0] == '*' && word[1] == 0)
  116. /* The name by which the peer authenticated itself to us. */
  117. char peer_authname[MAXNAMELEN];
  118. /* Records which authentication operations haven't completed yet. */
  119. static int auth_pending[NUM_PPP];
  120. /* Records which authentication operations have been completed. */
  121. int auth_done[NUM_PPP];
  122. /* List of addresses which the peer may use. */
  123. static struct permitted_ip *addresses[NUM_PPP];
  124. /* Wordlist giving addresses which the peer may use
  125. without authenticating itself. */
  126. static struct wordlist *noauth_addrs;
  127. /* Remote telephone number, if available */
  128. char remote_number[MAXNAMELEN];
  129. /* Wordlist giving remote telephone numbers which may connect. */
  130. static struct wordlist *permitted_numbers;
  131. /* Extra options to apply, from the secrets file entry for the peer. */
  132. static struct wordlist *extra_options;
  133. /* Number of network protocols which we have opened. */
  134. static int num_np_open;
  135. /* Number of network protocols which have come up. */
  136. static int num_np_up;
  137. /* Set if we got the contents of passwd[] from the pap-secrets file. */
  138. static int passwd_from_file;
  139. /* Set if we require authentication only because we have a default route. */
  140. static bool default_auth;
  141. /* Hook to enable a plugin to control the idle time limit */
  142. int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;
  143. /* Hook for a plugin to say whether we can possibly authenticate any peer */
  144. int (*pap_check_hook) __P((void)) = NULL;
  145. /* Hook for a plugin to check the PAP user and password */
  146. int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp,
  147. struct wordlist **paddrs,
  148. struct wordlist **popts)) = NULL;
  149. /* Hook for a plugin to know about the PAP user logout */
  150. void (*pap_logout_hook) __P((void)) = NULL;
  151. /* Hook for a plugin to get the PAP password for authenticating us */
  152. int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;
  153. /* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */
  154. int (*chap_check_hook) __P((void)) = NULL;
  155. /* Hook for a plugin to get the CHAP password for authenticating us */
  156. int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;
  157. /* Hook for a plugin to say whether it is OK if the peer
  158. refuses to authenticate. */
  159. int (*null_auth_hook) __P((struct wordlist **paddrs,
  160. struct wordlist **popts)) = NULL;
  161. int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;
  162. #ifdef HAVE_MULTILINK
  163. /* Hook for plugin to hear when an interface joins a multilink bundle */
  164. void (*multilink_join_hook) __P((void)) = NULL;
  165. #endif
  166. /* A notifier for when the peer has authenticated itself,
  167. and we are proceeding to the network phase. */
  168. struct notifier *auth_up_notifier = NULL;
  169. /* A notifier for when the link goes down. */
  170. struct notifier *link_down_notifier = NULL;
  171. /*
  172. * This is used to ensure that we don't start an auth-up/down
  173. * script while one is already running.
  174. */
  175. enum script_state {
  176. s_down,
  177. s_up
  178. };
  179. static enum script_state auth_state = s_down;
  180. static enum script_state auth_script_state = s_down;
  181. static pid_t auth_script_pid = 0;
  182. /*
  183. * Option variables.
  184. */
  185. bool uselogin = 0; /* Use /etc/passwd for checking PAP */
  186. bool session_mgmt = 0; /* Do session management (login records) */
  187. bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */
  188. bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */
  189. bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */
  190. bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */
  191. #ifdef CHAPMS
  192. bool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */
  193. bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */
  194. #else
  195. bool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */
  196. bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */
  197. #endif
  198. bool usehostname = 0; /* Use hostname for our_name */
  199. bool auth_required = 0; /* Always require authentication from peer */
  200. bool allow_any_ip = 0; /* Allow peer to use any IP address */
  201. bool explicit_remote = 0; /* User specified explicit remote name */
  202. bool explicit_user = 0; /* Set if "user" option supplied */
  203. bool explicit_passwd = 0; /* Set if "password" option supplied */
  204. char remote_name[MAXNAMELEN]; /* Peer's name for authentication */
  205. static char *uafname; /* name of most recent +ua file */
  206. extern char *crypt __P((const char *, const char *));
  207. /* Prototypes for procedures local to this file. */
  208. static void network_phase __P((int));
  209. static void check_idle __P((void *));
  210. static void connect_time_expired __P((void *));
  211. static int null_login __P((int));
  212. static int get_pap_passwd __P((char *));
  213. static int have_pap_secret __P((int *));
  214. static int have_chap_secret __P((char *, char *, int, int *));
  215. static int have_srp_secret __P((char *client, char *server, int need_ip,
  216. int *lacks_ipp));
  217. static int ip_addr_check __P((u_int32_t, struct permitted_ip *));
  218. static int scan_authfile __P((FILE *, char *, char *, char *,
  219. struct wordlist **, struct wordlist **,
  220. char *, int));
  221. static void free_wordlist __P((struct wordlist *));
  222. static void auth_script __P((char *));
  223. static void auth_script_done __P((void *));
  224. static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));
  225. static int some_ip_ok __P((struct wordlist *));
  226. static int setupapfile __P((char **));
  227. static int privgroup __P((char **));
  228. static int set_noauth_addr __P((char **));
  229. static int set_permitted_number __P((char **));
  230. static void check_access __P((FILE *, char *));
  231. static int wordlist_count __P((struct wordlist *));
  232. #ifdef MAXOCTETS
  233. static void check_maxoctets __P((void *));
  234. #endif
  235. /*
  236. * Authentication-related options.
  237. */
  238. option_t auth_options[] = {
  239. { "auth", o_bool, &auth_required,
  240. "Require authentication from peer", OPT_PRIO | 1 },
  241. { "noauth", o_bool, &auth_required,
  242. "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV,
  243. &allow_any_ip },
  244. { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap,
  245. "Require PAP authentication from peer",
  246. OPT_PRIOSUB | 1, &auth_required },
  247. { "+pap", o_bool, &lcp_wantoptions[0].neg_upap,
  248. "Require PAP authentication from peer",
  249. OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required },
  250. { "require-chap", o_bool, &auth_required,
  251. "Require CHAP authentication from peer",
  252. OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
  253. &lcp_wantoptions[0].chap_mdtype },
  254. { "+chap", o_bool, &auth_required,
  255. "Require CHAP authentication from peer",
  256. OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5,
  257. &lcp_wantoptions[0].chap_mdtype },
  258. #ifdef CHAPMS
  259. { "require-mschap", o_bool, &auth_required,
  260. "Require MS-CHAP authentication from peer",
  261. OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
  262. &lcp_wantoptions[0].chap_mdtype },
  263. { "+mschap", o_bool, &auth_required,
  264. "Require MS-CHAP authentication from peer",
  265. OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT,
  266. &lcp_wantoptions[0].chap_mdtype },
  267. { "require-mschap-v2", o_bool, &auth_required,
  268. "Require MS-CHAPv2 authentication from peer",
  269. OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
  270. &lcp_wantoptions[0].chap_mdtype },
  271. { "+mschap-v2", o_bool, &auth_required,
  272. "Require MS-CHAPv2 authentication from peer",
  273. OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2,
  274. &lcp_wantoptions[0].chap_mdtype },
  275. #endif
  276. { "refuse-pap", o_bool, &refuse_pap,
  277. "Don't agree to auth to peer with PAP", 1 },
  278. { "-pap", o_bool, &refuse_pap,
  279. "Don't allow PAP authentication with peer", OPT_ALIAS | 1 },
  280. { "refuse-chap", o_bool, &refuse_chap,
  281. "Don't agree to auth to peer with CHAP",
  282. OPT_A2CLRB | MDTYPE_MD5,
  283. &lcp_allowoptions[0].chap_mdtype },
  284. { "-chap", o_bool, &refuse_chap,
  285. "Don't allow CHAP authentication with peer",
  286. OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5,
  287. &lcp_allowoptions[0].chap_mdtype },
  288. #ifdef CHAPMS
  289. { "refuse-mschap", o_bool, &refuse_mschap,
  290. "Don't agree to auth to peer with MS-CHAP",
  291. OPT_A2CLRB | MDTYPE_MICROSOFT,
  292. &lcp_allowoptions[0].chap_mdtype },
  293. { "-mschap", o_bool, &refuse_mschap,
  294. "Don't allow MS-CHAP authentication with peer",
  295. OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT,
  296. &lcp_allowoptions[0].chap_mdtype },
  297. { "refuse-mschap-v2", o_bool, &refuse_mschap_v2,
  298. "Don't agree to auth to peer with MS-CHAPv2",
  299. OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
  300. &lcp_allowoptions[0].chap_mdtype },
  301. { "-mschap-v2", o_bool, &refuse_mschap_v2,
  302. "Don't allow MS-CHAPv2 authentication with peer",
  303. OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2,
  304. &lcp_allowoptions[0].chap_mdtype },
  305. #endif
  306. { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap,
  307. "Require EAP authentication from peer", OPT_PRIOSUB | 1,
  308. &auth_required },
  309. { "refuse-eap", o_bool, &refuse_eap,
  310. "Don't agree to authenticate to peer with EAP", 1 },
  311. { "name", o_string, our_name,
  312. "Set local name for authentication",
  313. OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN },
  314. { "+ua", o_special, (void *)setupapfile,
  315. "Get PAP user and password from file",
  316. OPT_PRIO | OPT_A2STRVAL, &uafname },
  317. { "user", o_string, user,
  318. "Set name for auth with peer", OPT_PRIO | OPT_STATIC,
  319. &explicit_user, MAXNAMELEN },
  320. { "password", o_string, passwd,
  321. "Password for authenticating us to the peer",
  322. OPT_PRIO | OPT_STATIC | OPT_HIDE,
  323. &explicit_passwd, MAXSECRETLEN },
  324. { "usehostname", o_bool, &usehostname,
  325. "Must use hostname for authentication", 1 },
  326. { "remotename", o_string, remote_name,
  327. "Set remote name for authentication", OPT_PRIO | OPT_STATIC,
  328. &explicit_remote, MAXNAMELEN },
  329. { "login", o_bool, &uselogin,
  330. "Use system password database for PAP", OPT_A2COPY | 1 ,
  331. &session_mgmt },
  332. { "enable-session", o_bool, &session_mgmt,
  333. "Enable session accounting for remote peers", OPT_PRIV | 1 },
  334. { "papcrypt", o_bool, &cryptpap,
  335. "PAP passwords are encrypted", 1 },
  336. { "privgroup", o_special, (void *)privgroup,
  337. "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST },
  338. { "allow-ip", o_special, (void *)set_noauth_addr,
  339. "Set IP address(es) which can be used without authentication",
  340. OPT_PRIV | OPT_A2LIST },
  341. { "remotenumber", o_string, remote_number,
  342. "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC,
  343. NULL, MAXNAMELEN },
  344. { "allow-number", o_special, (void *)set_permitted_number,
  345. "Set telephone number(s) which are allowed to connect",
  346. OPT_PRIV | OPT_A2LIST },
  347. { NULL }
  348. };
  349. /*
  350. * setupapfile - specifies UPAP info for authenticating with peer.
  351. */
  352. static int
  353. setupapfile(argv)
  354. char **argv;
  355. {
  356. FILE *ufile;
  357. int l;
  358. uid_t euid;
  359. char u[MAXNAMELEN], p[MAXSECRETLEN];
  360. char *fname;
  361. lcp_allowoptions[0].neg_upap = 1;
  362. /* open user info file */
  363. fname = strdup(*argv);
  364. if (fname == NULL)
  365. novm("+ua file name");
  366. euid = geteuid();
  367. if (seteuid(getuid()) == -1) {
  368. option_error("unable to reset uid before opening %s: %m", fname);
  369. return 0;
  370. }
  371. ufile = fopen(fname, "r");
  372. if (seteuid(euid) == -1)
  373. fatal("unable to regain privileges: %m");
  374. if (ufile == NULL) {
  375. option_error("unable to open user login data file %s", fname);
  376. return 0;
  377. }
  378. check_access(ufile, fname);
  379. uafname = fname;
  380. /* get username */
  381. if (fgets(u, MAXNAMELEN - 1, ufile) == NULL
  382. || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) {
  383. fclose(ufile);
  384. option_error("unable to read user login data file %s", fname);
  385. return 0;
  386. }
  387. fclose(ufile);
  388. /* get rid of newlines */
  389. l = strlen(u);
  390. if (l > 0 && u[l-1] == '\n')
  391. u[l-1] = 0;
  392. l = strlen(p);
  393. if (l > 0 && p[l-1] == '\n')
  394. p[l-1] = 0;
  395. if (override_value("user", option_priority, fname)) {
  396. strlcpy(user, u, sizeof(user));
  397. explicit_user = 1;
  398. }
  399. if (override_value("passwd", option_priority, fname)) {
  400. strlcpy(passwd, p, sizeof(passwd));
  401. explicit_passwd = 1;
  402. }
  403. return (1);
  404. }
  405. /*
  406. * privgroup - allow members of the group to have privileged access.
  407. */
  408. static int
  409. privgroup(argv)
  410. char **argv;
  411. {
  412. struct group *g;
  413. int i;
  414. g = getgrnam(*argv);
  415. if (g == 0) {
  416. option_error("group %s is unknown", *argv);
  417. return 0;
  418. }
  419. for (i = 0; i < ngroups; ++i) {
  420. if (groups[i] == g->gr_gid) {
  421. privileged = 1;
  422. break;
  423. }
  424. }
  425. return 1;
  426. }
  427. /*
  428. * set_noauth_addr - set address(es) that can be used without authentication.
  429. * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets.
  430. */
  431. static int
  432. set_noauth_addr(argv)
  433. char **argv;
  434. {
  435. char *addr = *argv;
  436. int l = strlen(addr) + 1;
  437. struct wordlist *wp;
  438. wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
  439. if (wp == NULL)
  440. novm("allow-ip argument");
  441. wp->word = (char *) (wp + 1);
  442. wp->next = noauth_addrs;
  443. BCOPY(addr, wp->word, l);
  444. noauth_addrs = wp;
  445. return 1;
  446. }
  447. /*
  448. * set_permitted_number - set remote telephone number(s) that may connect.
  449. */
  450. static int
  451. set_permitted_number(argv)
  452. char **argv;
  453. {
  454. char *number = *argv;
  455. int l = strlen(number) + 1;
  456. struct wordlist *wp;
  457. wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l);
  458. if (wp == NULL)
  459. novm("allow-number argument");
  460. wp->word = (char *) (wp + 1);
  461. wp->next = permitted_numbers;
  462. BCOPY(number, wp->word, l);
  463. permitted_numbers = wp;
  464. return 1;
  465. }
  466. /*
  467. * An Open on LCP has requested a change from Dead to Establish phase.
  468. */
  469. void
  470. link_required(unit)
  471. int unit;
  472. {
  473. }
  474. /*
  475. * Bring the link up to the point of being able to do ppp.
  476. */
  477. void start_link(unit)
  478. int unit;
  479. {
  480. status = EXIT_CONNECT_FAILED;
  481. new_phase(PHASE_SERIALCONN);
  482. hungup = 0;
  483. devfd = the_channel->connect();
  484. if (devfd < 0)
  485. goto fail;
  486. /* set up the serial device as a ppp interface */
  487. /*
  488. * N.B. we used to do tdb_writelock/tdb_writeunlock around this
  489. * (from establish_ppp to set_ifunit). However, we won't be
  490. * doing the set_ifunit in multilink mode, which is the only time
  491. * we need the atomicity that the tdb_writelock/tdb_writeunlock
  492. * gives us. Thus we don't need the tdb_writelock/tdb_writeunlock.
  493. */
  494. fd_ppp = the_channel->establish_ppp(devfd);
  495. if (fd_ppp < 0) {
  496. status = EXIT_FATAL_ERROR;
  497. goto disconnect;
  498. }
  499. if (!demand && ifunit >= 0)
  500. set_ifunit(1);
  501. /*
  502. * Start opening the connection and wait for
  503. * incoming events (reply, timeout, etc.).
  504. */
  505. if (ifunit >= 0)
  506. notice("Connect: %s <--> %s", ifname, ppp_devnam);
  507. else
  508. notice("Starting negotiation on %s", ppp_devnam);
  509. add_fd(fd_ppp);
  510. status = EXIT_NEGOTIATION_FAILED;
  511. new_phase(PHASE_ESTABLISH);
  512. lcp_lowerup(0);
  513. return;
  514. disconnect:
  515. new_phase(PHASE_DISCONNECT);
  516. if (the_channel->disconnect)
  517. the_channel->disconnect();
  518. fail:
  519. new_phase(PHASE_DEAD);
  520. if (the_channel->cleanup)
  521. (*the_channel->cleanup)();
  522. }
  523. /*
  524. * LCP has terminated the link; go to the Dead phase and take the
  525. * physical layer down.
  526. */
  527. void
  528. link_terminated(unit)
  529. int unit;
  530. {
  531. if (phase == PHASE_DEAD || phase == PHASE_MASTER)
  532. return;
  533. new_phase(PHASE_DISCONNECT);
  534. if (pap_logout_hook) {
  535. pap_logout_hook();
  536. }
  537. session_end(devnam);
  538. if (!doing_multilink) {
  539. notice("Connection terminated.");
  540. print_link_stats();
  541. } else
  542. notice("Link terminated.");
  543. /*
  544. * Delete pid files before disestablishing ppp. Otherwise it
  545. * can happen that another pppd gets the same unit and then
  546. * we delete its pid file.
  547. */
  548. if (!doing_multilink && !demand)
  549. remove_pidfiles();
  550. /*
  551. * If we may want to bring the link up again, transfer
  552. * the ppp unit back to the loopback. Set the
  553. * real serial device back to its normal mode of operation.
  554. */
  555. if (fd_ppp >= 0) {
  556. remove_fd(fd_ppp);
  557. clean_check();
  558. the_channel->disestablish_ppp(devfd);
  559. if (doing_multilink)
  560. mp_exit_bundle();
  561. fd_ppp = -1;
  562. }
  563. if (!hungup)
  564. lcp_lowerdown(0);
  565. if (!doing_multilink && !demand)
  566. script_unsetenv("IFNAME");
  567. /*
  568. * Run disconnector script, if requested.
  569. * XXX we may not be able to do this if the line has hung up!
  570. */
  571. if (devfd >= 0 && the_channel->disconnect) {
  572. the_channel->disconnect();
  573. devfd = -1;
  574. }
  575. if (the_channel->cleanup)
  576. (*the_channel->cleanup)();
  577. if (doing_multilink && multilink_master) {
  578. if (!bundle_terminating) {
  579. new_phase(PHASE_MASTER);
  580. if (master_detach && !detached)
  581. detach();
  582. } else
  583. mp_bundle_terminated();
  584. } else
  585. new_phase(PHASE_DEAD);
  586. }
  587. /*
  588. * LCP has gone down; it will either die or try to re-establish.
  589. */
  590. void
  591. link_down(unit)
  592. int unit;
  593. {
  594. if (auth_state != s_down) {
  595. notify(link_down_notifier, 0);
  596. auth_state = s_down;
  597. if (auth_script_state == s_up && auth_script_pid == 0) {
  598. update_link_stats(unit);
  599. auth_script_state = s_down;
  600. auth_script(_PATH_AUTHDOWN);
  601. }
  602. }
  603. if (!doing_multilink) {
  604. upper_layers_down(unit);
  605. if (phase != PHASE_DEAD && phase != PHASE_MASTER)
  606. new_phase(PHASE_ESTABLISH);
  607. }
  608. /* XXX if doing_multilink, should do something to stop
  609. network-layer traffic on the link */
  610. }
  611. void upper_layers_down(int unit)
  612. {
  613. int i;
  614. struct protent *protp;
  615. for (i = 0; (protp = protocols[i]) != NULL; ++i) {
  616. if (!protp->enabled_flag)
  617. continue;
  618. if (protp->protocol != PPP_LCP && protp->lowerdown != NULL)
  619. (*protp->lowerdown)(unit);
  620. if (protp->protocol < 0xC000 && protp->close != NULL)
  621. (*protp->close)(unit, "LCP down");
  622. }
  623. num_np_open = 0;
  624. num_np_up = 0;
  625. }
  626. /*
  627. * The link is established.
  628. * Proceed to the Dead, Authenticate or Network phase as appropriate.
  629. */
  630. void
  631. link_established(unit)
  632. int unit;
  633. {
  634. int auth;
  635. lcp_options *wo = &lcp_wantoptions[unit];
  636. lcp_options *go = &lcp_gotoptions[unit];
  637. lcp_options *ho = &lcp_hisoptions[unit];
  638. int i;
  639. struct protent *protp;
  640. /*
  641. * Tell higher-level protocols that LCP is up.
  642. */
  643. if (!doing_multilink) {
  644. for (i = 0; (protp = protocols[i]) != NULL; ++i)
  645. if (protp->protocol != PPP_LCP && protp->enabled_flag
  646. && protp->lowerup != NULL)
  647. (*protp->lowerup)(unit);
  648. }
  649. if (!auth_required && noauth_addrs != NULL)
  650. set_allowed_addrs(unit, NULL, NULL);
  651. if (auth_required && !(go->neg_upap || go->neg_chap || go->neg_eap)) {
  652. /*
  653. * We wanted the peer to authenticate itself, and it refused:
  654. * if we have some address(es) it can use without auth, fine,
  655. * otherwise treat it as though it authenticated with PAP using
  656. * a username of "" and a password of "". If that's not OK,
  657. * boot it out.
  658. */
  659. if (noauth_addrs != NULL) {
  660. set_allowed_addrs(unit, NULL, NULL);
  661. } else if (!wo->neg_upap || uselogin || !null_login(unit)) {
  662. warn("peer refused to authenticate: terminating link");
  663. status = EXIT_PEER_AUTH_FAILED;
  664. lcp_close(unit, "peer refused to authenticate");
  665. return;
  666. }
  667. }
  668. new_phase(PHASE_AUTHENTICATE);
  669. auth = 0;
  670. if (go->neg_eap) {
  671. eap_authpeer(unit, our_name);
  672. auth |= EAP_PEER;
  673. } else if (go->neg_chap) {
  674. chap_auth_peer(unit, our_name, CHAP_DIGEST(go->chap_mdtype));
  675. auth |= CHAP_PEER;
  676. } else if (go->neg_upap) {
  677. upap_authpeer(unit);
  678. auth |= PAP_PEER;
  679. }
  680. if (ho->neg_eap) {
  681. eap_authwithpeer(unit, user);
  682. auth |= EAP_WITHPEER;
  683. } else if (ho->neg_chap) {
  684. chap_auth_with_peer(unit, user, CHAP_DIGEST(ho->chap_mdtype));
  685. auth |= CHAP_WITHPEER;
  686. } else if (ho->neg_upap) {
  687. /* If a blank password was explicitly given as an option, trust
  688. the user and don't try to look up one. */
  689. if (passwd[0] == 0 && !explicit_passwd) {
  690. passwd_from_file = 1;
  691. if (!get_pap_passwd(passwd))
  692. error("No secret found for PAP login");
  693. }
  694. upap_authwithpeer(unit, user, passwd);
  695. auth |= PAP_WITHPEER;
  696. }
  697. auth_pending[unit] = auth;
  698. auth_done[unit] = 0;
  699. if (!auth)
  700. network_phase(unit);
  701. }
  702. /*
  703. * Proceed to the network phase.
  704. */
  705. static void
  706. network_phase(unit)
  707. int unit;
  708. {
  709. lcp_options *go = &lcp_gotoptions[unit];
  710. /* Log calling number. */
  711. if (*remote_number)
  712. notice("peer from calling number %q authorized", remote_number);
  713. /*
  714. * If the peer had to authenticate, run the auth-up script now.
  715. */
  716. if (go->neg_chap || go->neg_upap || go->neg_eap) {
  717. notify(auth_up_notifier, 0);
  718. auth_state = s_up;
  719. if (auth_script_state == s_down && auth_script_pid == 0) {
  720. auth_script_state = s_up;
  721. auth_script(_PATH_AUTHUP);
  722. }
  723. }
  724. #ifdef CBCP_SUPPORT
  725. /*
  726. * If we negotiated callback, do it now.
  727. */
  728. if (go->neg_cbcp) {
  729. new_phase(PHASE_CALLBACK);
  730. (*cbcp_protent.open)(unit);
  731. return;
  732. }
  733. #endif
  734. /*
  735. * Process extra options from the secrets file
  736. */
  737. if (extra_options) {
  738. options_from_list(extra_options, 1);
  739. free_wordlist(extra_options);
  740. extra_options = 0;
  741. }
  742. start_networks(unit);
  743. }
  744. void
  745. start_networks(unit)
  746. int unit;
  747. {
  748. int i;
  749. struct protent *protp;
  750. int ecp_required, mppe_required;
  751. new_phase(PHASE_NETWORK);
  752. #ifdef HAVE_MULTILINK
  753. if (multilink) {
  754. if (mp_join_bundle()) {
  755. if (multilink_join_hook)
  756. (*multilink_join_hook)();
  757. if (updetach && !nodetach)
  758. detach();
  759. return;
  760. }
  761. }
  762. #endif /* HAVE_MULTILINK */
  763. #ifdef PPP_FILTER
  764. if (!demand)
  765. set_filters(&pass_filter, &active_filter);
  766. #endif
  767. /* Start CCP and ECP */
  768. for (i = 0; (protp = protocols[i]) != NULL; ++i)
  769. if ((protp->protocol == PPP_ECP || protp->protocol == PPP_CCP)
  770. && protp->enabled_flag && protp->open != NULL)
  771. (*protp->open)(0);
  772. /*
  773. * Bring up other network protocols iff encryption is not required.
  774. */
  775. ecp_required = ecp_gotoptions[unit].required;
  776. mppe_required = ccp_gotoptions[unit].mppe;
  777. if (!ecp_required && !mppe_required)
  778. continue_networks(unit);
  779. }
  780. void
  781. continue_networks(unit)
  782. int unit;
  783. {
  784. int i;
  785. struct protent *protp;
  786. /*
  787. * Start the "real" network protocols.
  788. */
  789. for (i = 0; (protp = protocols[i]) != NULL; ++i)
  790. if (protp->protocol < 0xC000
  791. && protp->protocol != PPP_CCP && protp->protocol != PPP_ECP
  792. && protp->enabled_flag && protp->open != NULL) {
  793. (*protp->open)(0);
  794. ++num_np_open;
  795. }
  796. if (num_np_open == 0)
  797. /* nothing to do */
  798. lcp_close(0, "No network protocols running");
  799. }
  800. /*
  801. * The peer has failed to authenticate himself using `protocol'.
  802. */
  803. void
  804. auth_peer_fail(unit, protocol)
  805. int unit, protocol;
  806. {
  807. /*
  808. * Authentication failure: take the link down
  809. */
  810. status = EXIT_PEER_AUTH_FAILED;
  811. lcp_close(unit, "Authentication failed");
  812. }
  813. /*
  814. * The peer has been successfully authenticated using `protocol'.
  815. */
  816. void
  817. auth_peer_success(unit, protocol, prot_flavor, name, namelen)
  818. int unit, protocol, prot_flavor;
  819. char *name;
  820. int namelen;
  821. {
  822. int bit;
  823. switch (protocol) {
  824. case PPP_CHAP:
  825. bit = CHAP_PEER;
  826. switch (prot_flavor) {
  827. case CHAP_MD5:
  828. bit |= CHAP_MD5_PEER;
  829. break;
  830. #ifdef CHAPMS
  831. case CHAP_MICROSOFT:
  832. bit |= CHAP_MS_PEER;
  833. break;
  834. case CHAP_MICROSOFT_V2:
  835. bit |= CHAP_MS2_PEER;
  836. break;
  837. #endif
  838. }
  839. break;
  840. case PPP_PAP:
  841. bit = PAP_PEER;
  842. break;
  843. case PPP_EAP:
  844. bit = EAP_PEER;
  845. break;
  846. default:
  847. warn("auth_peer_success: unknown protocol %x", protocol);
  848. return;
  849. }
  850. /*
  851. * Save the authenticated name of the peer for later.
  852. */
  853. if (namelen > sizeof(peer_authname) - 1)
  854. namelen = sizeof(peer_authname) - 1;
  855. BCOPY(name, peer_authname, namelen);
  856. peer_authname[namelen] = 0;
  857. script_setenv("PEERNAME", peer_authname, 0);
  858. /* Save the authentication method for later. */
  859. auth_done[unit] |= bit;
  860. /*
  861. * If there is no more authentication still to be done,
  862. * proceed to the network (or callback) phase.
  863. */
  864. if ((auth_pending[unit] &= ~bit) == 0)
  865. network_phase(unit);
  866. }
  867. /*
  868. * We have failed to authenticate ourselves to the peer using `protocol'.
  869. */
  870. void
  871. auth_withpeer_fail(unit, protocol)
  872. int unit, protocol;
  873. {
  874. if (passwd_from_file)
  875. BZERO(passwd, MAXSECRETLEN);
  876. /*
  877. * We've failed to authenticate ourselves to our peer.
  878. * Some servers keep sending CHAP challenges, but there
  879. * is no point in persisting without any way to get updated
  880. * authentication secrets.
  881. */
  882. status = EXIT_AUTH_TOPEER_FAILED;
  883. lcp_close(unit, "Failed to authenticate ourselves to peer");
  884. }
  885. /*
  886. * We have successfully authenticated ourselves with the peer using `protocol'.
  887. */
  888. void
  889. auth_withpeer_success(unit, protocol, prot_flavor)
  890. int unit, protocol, prot_flavor;
  891. {
  892. int bit;
  893. const char *prot = "";
  894. switch (protocol) {
  895. case PPP_CHAP:
  896. bit = CHAP_WITHPEER;
  897. prot = "CHAP";
  898. switch (prot_flavor) {
  899. case CHAP_MD5:
  900. bit |= CHAP_MD5_WITHPEER;
  901. break;
  902. #ifdef CHAPMS
  903. case CHAP_MICROSOFT:
  904. bit |= CHAP_MS_WITHPEER;
  905. break;
  906. case CHAP_MICROSOFT_V2:
  907. bit |= CHAP_MS2_WITHPEER;
  908. break;
  909. #endif
  910. }
  911. break;
  912. case PPP_PAP:
  913. if (passwd_from_file)
  914. BZERO(passwd, MAXSECRETLEN);
  915. bit = PAP_WITHPEER;
  916. prot = "PAP";
  917. break;
  918. case PPP_EAP:
  919. bit = EAP_WITHPEER;
  920. prot = "EAP";
  921. break;
  922. default:
  923. warn("auth_withpeer_success: unknown protocol %x", protocol);
  924. bit = 0;
  925. }
  926. notice("%s authentication succeeded", prot);
  927. /* Save the authentication method for later. */
  928. auth_done[unit] |= bit;
  929. /*
  930. * If there is no more authentication still being done,
  931. * proceed to the network (or callback) phase.
  932. */
  933. if ((auth_pending[unit] &= ~bit) == 0)
  934. network_phase(unit);
  935. }
  936. /*
  937. * np_up - a network protocol has come up.
  938. */
  939. void
  940. np_up(unit, proto)
  941. int unit, proto;
  942. {
  943. int tlim;
  944. if (num_np_up == 0) {
  945. /*
  946. * At this point we consider that the link has come up successfully.
  947. */
  948. status = EXIT_OK;
  949. unsuccess = 0;
  950. new_phase(PHASE_RUNNING);
  951. if (idle_time_hook != 0)
  952. tlim = (*idle_time_hook)(NULL);
  953. else
  954. tlim = idle_time_limit;
  955. if (tlim > 0)
  956. TIMEOUT(check_idle, NULL, tlim);
  957. /*
  958. * Set a timeout to close the connection once the maximum
  959. * connect time has expired.
  960. */
  961. if (maxconnect > 0)
  962. TIMEOUT(connect_time_expired, 0, maxconnect);
  963. #ifdef MAXOCTETS
  964. if (maxoctets > 0)
  965. TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
  966. #endif
  967. /*
  968. * Detach now, if the updetach option was given.
  969. */
  970. if (updetach && !nodetach)
  971. detach();
  972. }
  973. ++num_np_up;
  974. }
  975. /*
  976. * np_down - a network protocol has gone down.
  977. */
  978. void
  979. np_down(unit, proto)
  980. int unit, proto;
  981. {
  982. if (--num_np_up == 0) {
  983. UNTIMEOUT(check_idle, NULL);
  984. UNTIMEOUT(connect_time_expired, NULL);
  985. #ifdef MAXOCTETS
  986. UNTIMEOUT(check_maxoctets, NULL);
  987. #endif
  988. new_phase(PHASE_NETWORK);
  989. }
  990. }
  991. /*
  992. * np_finished - a network protocol has finished using the link.
  993. */
  994. void
  995. np_finished(unit, proto)
  996. int unit, proto;
  997. {
  998. if (--num_np_open <= 0) {
  999. /* no further use for the link: shut up shop. */
  1000. lcp_close(0, "No network protocols running");
  1001. }
  1002. }
  1003. #ifdef MAXOCTETS
  1004. static void
  1005. check_maxoctets(arg)
  1006. void *arg;
  1007. {
  1008. unsigned int used;
  1009. update_link_stats(ifunit);
  1010. link_stats_valid=0;
  1011. switch(maxoctets_dir) {
  1012. case PPP_OCTETS_DIRECTION_IN:
  1013. used = link_stats.bytes_in;
  1014. break;
  1015. case PPP_OCTETS_DIRECTION_OUT:
  1016. used = link_stats.bytes_out;
  1017. break;
  1018. case PPP_OCTETS_DIRECTION_MAXOVERAL:
  1019. case PPP_OCTETS_DIRECTION_MAXSESSION:
  1020. used = (link_stats.bytes_in > link_stats.bytes_out) ? link_stats.bytes_in : link_stats.bytes_out;
  1021. break;
  1022. default:
  1023. used = link_stats.bytes_in+link_stats.bytes_out;
  1024. break;
  1025. }
  1026. if (used > maxoctets) {
  1027. notice("Traffic limit reached. Limit: %u Used: %u", maxoctets, used);
  1028. status = EXIT_TRAFFIC_LIMIT;
  1029. lcp_close(0, "Traffic limit");
  1030. need_holdoff = 0;
  1031. } else {
  1032. TIMEOUT(check_maxoctets, NULL, maxoctets_timeout);
  1033. }
  1034. }
  1035. #endif
  1036. /*
  1037. * check_idle - check whether the link has been idle for long
  1038. * enough that we can shut it down.
  1039. */
  1040. static void
  1041. check_idle(arg)
  1042. void *arg;
  1043. {
  1044. struct ppp_idle idle;
  1045. time_t itime;
  1046. int tlim;
  1047. if (!get_idle_time(0, &idle))
  1048. return;
  1049. if (idle_time_hook != 0) {
  1050. tlim = idle_time_hook(&idle);
  1051. } else {
  1052. itime = MIN(idle.xmit_idle, idle.recv_idle);
  1053. tlim = idle_time_limit - itime;
  1054. }
  1055. if (tlim <= 0) {
  1056. /* link is idle: shut it down. */
  1057. notice("Terminating connection due to lack of activity.");
  1058. status = EXIT_IDLE_TIMEOUT;
  1059. lcp_close(0, "Link inactive");
  1060. need_holdoff = 0;
  1061. } else {
  1062. TIMEOUT(check_idle, NULL, tlim);
  1063. }
  1064. }
  1065. /*
  1066. * connect_time_expired - log a message and close the connection.
  1067. */
  1068. static void
  1069. connect_time_expired(arg)
  1070. void *arg;
  1071. {
  1072. info("Connect time expired");
  1073. status = EXIT_CONNECT_TIME;
  1074. lcp_close(0, "Connect time expired"); /* Close connection */
  1075. }
  1076. /*
  1077. * auth_check_options - called to check authentication options.
  1078. */
  1079. void
  1080. auth_check_options()
  1081. {
  1082. lcp_options *wo = &lcp_wantoptions[0];
  1083. int can_auth;
  1084. int lacks_ip;
  1085. /* Default our_name to hostname, and user to our_name */
  1086. if (our_name[0] == 0 || usehostname)
  1087. strlcpy(our_name, hostname, sizeof(our_name));
  1088. /* If a blank username was explicitly given as an option, trust
  1089. the user and don't use our_name */
  1090. if (user[0] == 0 && !explicit_user)
  1091. strlcpy(user, our_name, sizeof(user));
  1092. /*
  1093. * If we have a default route, require the peer to authenticate
  1094. * unless the noauth option was given or the real user is root.
  1095. */
  1096. if (!auth_required && !allow_any_ip && have_route_to(0) && !privileged) {
  1097. auth_required = 1;
  1098. default_auth = 1;
  1099. }
  1100. /* If we selected any CHAP flavors, we should probably negotiate it. :-) */
  1101. if (wo->chap_mdtype)
  1102. wo->neg_chap = 1;
  1103. /* If authentication is required, ask peer for CHAP, PAP, or EAP. */
  1104. if (auth_required) {
  1105. allow_any_ip = 0;
  1106. if (!wo->neg_chap && !wo->neg_upap && !wo->neg_eap) {
  1107. wo->neg_chap = chap_mdtype_all != MDTYPE_NONE;
  1108. wo->chap_mdtype = chap_mdtype_all;
  1109. wo->neg_upap = 1;
  1110. wo->neg_eap = 1;
  1111. }
  1112. } else {
  1113. wo->neg_chap = 0;
  1114. wo->chap_mdtype = MDTYPE_NONE;
  1115. wo->neg_upap = 0;
  1116. wo->neg_eap = 0;
  1117. }
  1118. /*
  1119. * Check whether we have appropriate secrets to use
  1120. * to authenticate the peer. Note that EAP can authenticate by way
  1121. * of a CHAP-like exchanges as well as SRP.
  1122. */
  1123. lacks_ip = 0;
  1124. can_auth = wo->neg_upap && (uselogin || have_pap_secret(&lacks_ip));
  1125. if (!can_auth && (wo->neg_chap || wo->neg_eap)) {
  1126. can_auth = have_chap_secret((explicit_remote? remote_name: NULL),
  1127. our_name, 1, &lacks_ip);
  1128. }
  1129. if (!can_auth && wo->neg_eap) {
  1130. can_auth = have_srp_secret((explicit_remote? remote_name: NULL),
  1131. our_name, 1, &lacks_ip);
  1132. }
  1133. if (auth_required && !can_auth && noauth_addrs == NULL) {
  1134. if (default_auth) {
  1135. option_error(
  1136. "By default the remote system is required to authenticate itself");
  1137. option_error(
  1138. "(because this system has a default route to the internet)");
  1139. } else if (explicit_remote)
  1140. option_error(
  1141. "The remote system (%s) is required to authenticate itself",
  1142. remote_name);
  1143. else
  1144. option_error(
  1145. "The remote system is required to authenticate itself");
  1146. option_error(
  1147. "but I couldn't find any suitable secret (password) for it to use to do so.");
  1148. if (lacks_ip)
  1149. option_error(
  1150. "(None of the available passwords would let it use an IP address.)");
  1151. exit(1);
  1152. }
  1153. /*
  1154. * Early check for remote number authorization.
  1155. */
  1156. if (!auth_number()) {
  1157. warn("calling number %q is not authorized", remote_number);
  1158. exit(EXIT_CNID_AUTH_FAILED);
  1159. }
  1160. }
  1161. /*
  1162. * auth_reset - called when LCP is starting negotiations to recheck
  1163. * authentication options, i.e. whether we have appropriate secrets
  1164. * to use for authenticating ourselves and/or the peer.
  1165. */
  1166. void
  1167. auth_reset(unit)
  1168. int unit;
  1169. {
  1170. lcp_options *go = &lcp_gotoptions[unit];
  1171. lcp_options *ao = &lcp_allowoptions[unit];
  1172. int hadchap;
  1173. hadchap = -1;
  1174. ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL));
  1175. ao->neg_chap = (!refuse_chap || !refuse_mschap || !refuse_mschap_v2)
  1176. && (passwd[0] != 0 ||
  1177. (hadchap = have_chap_secret(user, (explicit_remote? remote_name:
  1178. NULL), 0, NULL)));
  1179. ao->neg_eap = !refuse_eap && (
  1180. passwd[0] != 0 ||
  1181. (hadchap == 1 || (hadchap == -1 && have_chap_secret(user,
  1182. (explicit_remote? remote_name: NULL), 0, NULL))) ||
  1183. have_srp_secret(user, (explicit_remote? remote_name: NULL), 0, NULL));
  1184. hadchap = -1;
  1185. if (go->neg_upap && !uselogin && !have_pap_secret(NULL))
  1186. go->neg_upap = 0;
  1187. if (go->neg_chap) {
  1188. if (!(hadchap = have_chap_secret((explicit_remote? remote_name: NULL),
  1189. our_name, 1, NULL)))
  1190. go->neg_chap = 0;
  1191. }
  1192. if (go->neg_eap &&
  1193. (hadchap == 0 || (hadchap == -1 &&
  1194. !have_chap_secret((explicit_remote? remote_name: NULL), our_name,
  1195. 1, NULL))) &&
  1196. !have_srp_secret((explicit_remote? remote_name: NULL), our_name, 1,
  1197. NULL))
  1198. go->neg_eap = 0;
  1199. }
  1200. /*
  1201. * check_passwd - Check the user name and passwd against the PAP secrets
  1202. * file. If requested, also check against the system password database,
  1203. * and login the user if OK.
  1204. *
  1205. * returns:
  1206. * UPAP_AUTHNAK: Authentication failed.
  1207. * UPAP_AUTHACK: Authentication succeeded.
  1208. * In either case, msg points to an appropriate message.
  1209. */
  1210. int
  1211. check_passwd(unit, auser, userlen, apasswd, passwdlen, msg)
  1212. int unit;
  1213. char *auser;
  1214. int userlen;
  1215. char *apasswd;
  1216. int passwdlen;
  1217. char **msg;
  1218. {
  1219. int ret;
  1220. char *filename;
  1221. FILE *f;
  1222. struct wordlist *addrs = NULL, *opts = NULL;
  1223. char passwd[256], user[256];
  1224. char secret[MAXWORDLEN];
  1225. static int attempts = 0;
  1226. /*
  1227. * Make copies of apasswd and auser, then null-terminate them.
  1228. * If there are unprintable characters in the password, make
  1229. * them visible.
  1230. */
  1231. slprintf(passwd, sizeof(passwd), "%.*v", passwdlen, apasswd);
  1232. slprintf(user, sizeof(user), "%.*v", userlen, auser);
  1233. *msg = "";
  1234. /*
  1235. * Check if a plugin wants to handle this.
  1236. */
  1237. if (pap_auth_hook) {
  1238. ret = (*pap_auth_hook)(user, passwd, msg, &addrs, &opts);
  1239. if (ret >= 0) {
  1240. /* note: set_allowed_addrs() saves opts (but not addrs):
  1241. don't free it! */
  1242. if (ret)
  1243. set_allowed_addrs(unit, addrs, opts);
  1244. else if (opts != 0)
  1245. free_wordlist(opts);
  1246. if (addrs != 0)
  1247. free_wordlist(addrs);
  1248. BZERO(passwd, sizeof(passwd));
  1249. return ret? UPAP_AUTHACK: UPAP_AUTHNAK;
  1250. }
  1251. }
  1252. /*
  1253. * Open the file of pap secrets and scan for a suitable secret
  1254. * for authenticating this user.
  1255. */
  1256. filename = _PATH_UPAPFILE;
  1257. addrs = opts = NULL;
  1258. ret = UPAP_AUTHNAK;
  1259. f = fopen(filename, "r");
  1260. if (f == NULL) {
  1261. error("Can't open PAP password file %s: %m", filename);
  1262. } else {
  1263. check_access(f, filename);
  1264. if (scan_authfile(f, user, our_name, secret, &addrs, &opts, filename, 0) < 0) {
  1265. warn("no PAP secret found for %s", user);
  1266. } else {
  1267. /*
  1268. * If the secret is "@login", it means to check
  1269. * the password against the login database.
  1270. */
  1271. int login_secret = strcmp(secret, "@login") == 0;
  1272. ret = UPAP_AUTHACK;
  1273. if (uselogin || login_secret) {
  1274. /* login option or secret is @login */
  1275. if (session_full(user, passwd, devnam, msg) == 0) {
  1276. ret = UPAP_AUTHNAK;
  1277. }
  1278. } else if (session_mgmt) {
  1279. if (session_check(user, NULL, devnam, NULL) == 0) {
  1280. warn("Peer %q failed PAP Session verification", user);
  1281. ret = UPAP_AUTHNAK;
  1282. }
  1283. }
  1284. if (secret[0] != 0 && !login_secret) {
  1285. /* password given in pap-secrets - must match */
  1286. if (cryptpap || strcmp(passwd, secret) != 0) {
  1287. char *cbuf = crypt(passwd, secret);
  1288. if (!cbuf || strcmp(cbuf, secret) != 0)
  1289. ret = UPAP_AUTHNAK;
  1290. }
  1291. }
  1292. }
  1293. fclose(f);
  1294. }
  1295. if (ret == UPAP_AUTHNAK) {
  1296. if (**msg == 0)
  1297. *msg = "Login incorrect";
  1298. /*
  1299. * XXX can we ever get here more than once??
  1300. * Frustrate passwd stealer programs.
  1301. * Allow 10 tries, but start backing off after 3 (stolen from login).
  1302. * On 10'th, drop the connection.
  1303. */
  1304. if (attempts++ >= 10) {
  1305. warn("%d LOGIN FAILURES ON %s, %s", attempts, devnam, user);
  1306. lcp_close(unit, "login failed");
  1307. }
  1308. if (attempts > 3)
  1309. sleep((u_int) (attempts - 3) * 5);
  1310. if (opts != NULL)
  1311. free_wordlist(opts);
  1312. } else {
  1313. attempts = 0; /* Reset count */
  1314. if (**msg == 0)
  1315. *msg = "Login ok";
  1316. set_allowed_addrs(unit, addrs, opts);
  1317. }
  1318. if (addrs != NULL)
  1319. free_wordlist(addrs);
  1320. BZERO(passwd, sizeof(passwd));
  1321. BZERO(secret, sizeof(secret));
  1322. return ret;
  1323. }
  1324. /*
  1325. * null_login - Check if a username of "" and a password of "" are
  1326. * acceptable, and iff so, set the list of acceptable IP addresses
  1327. * and return 1.
  1328. */
  1329. static int
  1330. null_login(unit)
  1331. int unit;
  1332. {
  1333. char *filename;
  1334. FILE *f;
  1335. int i, ret;
  1336. struct wordlist *addrs, *opts;
  1337. char secret[MAXWORDLEN];
  1338. /*
  1339. * Check if a plugin wants to handle this.
  1340. */
  1341. ret = -1;
  1342. if (null_auth_hook)
  1343. ret = (*null_auth_hook)(&addrs, &opts);
  1344. /*
  1345. * Open the file of pap secrets and scan for a suitable secret.
  1346. */
  1347. if (ret <= 0) {
  1348. filename = _PATH_UPAPFILE;
  1349. addrs = NULL;
  1350. f = fopen(filename, "r");
  1351. if (f == NULL)
  1352. return 0;
  1353. check_access(f, filename);
  1354. i = scan_authfile(f, "", our_name, secret, &addrs, &opts, filename, 0);
  1355. ret = i >= 0 && secret[0] == 0;
  1356. BZERO(secret, sizeof(secret));
  1357. fclose(f);
  1358. }
  1359. if (ret)
  1360. set_allowed_addrs(unit, addrs, opts);
  1361. else if (opts != 0)
  1362. free_wordlist(opts);
  1363. if (addrs != 0)
  1364. free_wordlist(addrs);
  1365. return ret;
  1366. }
  1367. /*
  1368. * get_pap_passwd - get a password for authenticating ourselves with
  1369. * our peer using PAP. Returns 1 on success, 0 if no suitable password
  1370. * could be found.
  1371. * Assumes passwd points to MAXSECRETLEN bytes of space (if non-null).
  1372. */
  1373. static int
  1374. get_pap_passwd(passwd)
  1375. char *passwd;
  1376. {
  1377. char *filename;
  1378. FILE *f;
  1379. int ret;
  1380. char secret[MAXWORDLEN];
  1381. /*
  1382. * Check whether a plugin wants to supply this.
  1383. */
  1384. if (pap_passwd_hook) {
  1385. ret = (*pap_passwd_hook)(user, passwd);
  1386. if (ret >= 0)
  1387. return ret;
  1388. }
  1389. filename = _PATH_UPAPFILE;
  1390. f = fopen(filename, "r");
  1391. if (f == NULL)
  1392. return 0;
  1393. check_access(f, filename);
  1394. ret = scan_authfile(f, user,
  1395. (remote_name[0]? remote_name: NULL),
  1396. secret, NULL, NULL, filename, 0);
  1397. fclose(f);
  1398. if (ret < 0)
  1399. return 0;
  1400. if (passwd != NULL)
  1401. strlcpy(passwd, secret, MAXSECRETLEN);
  1402. BZERO(secret, sizeof(secret));
  1403. return 1;
  1404. }
  1405. /*
  1406. * have_pap_secret - check whether we have a PAP file with any
  1407. * secrets that we could possibly use for authenticating the peer.
  1408. */
  1409. static int
  1410. have_pap_secret(lacks_ipp)
  1411. int *lacks_ipp;
  1412. {
  1413. FILE *f;
  1414. int ret;
  1415. char *filename;
  1416. struct wordlist *addrs;
  1417. /* let the plugin decide, if there is one */
  1418. if (pap_check_hook) {
  1419. ret = (*pap_check_hook)();
  1420. if (ret >= 0)
  1421. return ret;
  1422. }
  1423. filename = _PATH_UPAPFILE;
  1424. f = fopen(filename, "r");
  1425. if (f == NULL)
  1426. return 0;
  1427. ret = scan_authfile(f, (explicit_remote? remote_name: NULL), our_name,
  1428. NULL, &addrs, NULL, filename, 0);
  1429. fclose(f);
  1430. if (ret >= 0 && !some_ip_ok(addrs)) {
  1431. if (lacks_ipp != 0)
  1432. *lacks_ipp = 1;
  1433. ret = -1;
  1434. }
  1435. if (addrs != 0)
  1436. free_wordlist(addrs);
  1437. return ret >= 0;
  1438. }
  1439. /*
  1440. * have_chap_secret - check whether we have a CHAP file with a
  1441. * secret that we could possibly use for authenticating `client'
  1442. * on `server'. Either can be the null string, meaning we don't
  1443. * know the identity yet.
  1444. */
  1445. static int
  1446. have_chap_secret(client, server, need_ip, lacks_ipp)
  1447. char *client;
  1448. char *server;
  1449. int need_ip;
  1450. int *lacks_ipp;
  1451. {
  1452. FILE *f;
  1453. int ret;
  1454. char *filename;
  1455. struct wordlist *addrs;
  1456. if (chap_check_hook) {
  1457. ret = (*chap_check_hook)();
  1458. if (ret >= 0) {
  1459. return ret;
  1460. }
  1461. }
  1462. filename = _PATH_CHAPFILE;
  1463. f = fopen(filename, "r");
  1464. if (f == NULL)
  1465. return 0;
  1466. if (client != NULL && client[0] == 0)
  1467. client = NULL;
  1468. else if (server != NULL && server[0] == 0)
  1469. server = NULL;
  1470. ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
  1471. fclose(f);
  1472. if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
  1473. if (lacks_ipp != 0)
  1474. *lacks_ipp = 1;
  1475. ret = -1;
  1476. }
  1477. if (addrs != 0)
  1478. free_wordlist(addrs);
  1479. return ret >= 0;
  1480. }
  1481. /*
  1482. * have_srp_secret - check whether we have a SRP file with a
  1483. * secret that we could possibly use for authenticating `client'
  1484. * on `server'. Either can be the null string, meaning we don't
  1485. * know the identity yet.
  1486. */
  1487. static int
  1488. have_srp_secret(client, server, need_ip, lacks_ipp)
  1489. char *client;
  1490. char *server;
  1491. int need_ip;
  1492. int *lacks_ipp;
  1493. {
  1494. FILE *f;
  1495. int ret;
  1496. char *filename;
  1497. struct wordlist *addrs;
  1498. filename = _PATH_SRPFILE;
  1499. f = fopen(filename, "r");
  1500. if (f == NULL)
  1501. return 0;
  1502. if (client != NULL && client[0] == 0)
  1503. client = NULL;
  1504. else if (server != NULL && server[0] == 0)
  1505. server = NULL;
  1506. ret = scan_authfile(f, client, server, NULL, &addrs, NULL, filename, 0);
  1507. fclose(f);
  1508. if (ret >= 0 && need_ip && !some_ip_ok(addrs)) {
  1509. if (lacks_ipp != 0)
  1510. *lacks_ipp = 1;
  1511. ret = -1;
  1512. }
  1513. if (addrs != 0)
  1514. free_wordlist(addrs);
  1515. return ret >= 0;
  1516. }
  1517. /*
  1518. * get_secret - open the CHAP secret file and return the secret
  1519. * for authenticating the given client on the given server.
  1520. * (We could be either client or server).
  1521. */
  1522. int
  1523. get_secret(unit, client, server, secret, secret_len, am_server)
  1524. int unit;
  1525. char *client;
  1526. char *server;
  1527. char *secret;
  1528. int *secret_len;
  1529. int am_server;
  1530. {
  1531. FILE *f;
  1532. int ret, len;
  1533. char *filename;
  1534. struct wordlist *addrs, *opts;
  1535. char secbuf[MAXWORDLEN];
  1536. if (!am_server && passwd[0] != 0) {
  1537. strlcpy(secbuf, passwd, sizeof(secbuf));
  1538. } else if (!am_server && chap_passwd_hook) {
  1539. if ( (*chap_passwd_hook)(client, secbuf) < 0) {
  1540. error("Unable to obtain CHAP password for %s on %s from plugin",
  1541. client, server);
  1542. return 0;
  1543. }
  1544. } else {
  1545. filename = _PATH_CHAPFILE;
  1546. addrs = NULL;
  1547. secbuf[0] = 0;
  1548. f = fopen(filename, "r");
  1549. if (f == NULL) {
  1550. error("Can't open chap secret file %s: %m", filename);
  1551. return 0;
  1552. }
  1553. check_access(f, filename);
  1554. ret = scan_authfile(f, client, server, secbuf, &addrs, &opts, filename, 0);
  1555. fclose(f);
  1556. if (ret < 0)
  1557. return 0;
  1558. if (am_server)
  1559. set_allowed_addrs(unit, addrs, opts);
  1560. else if (opts != 0)
  1561. free_wordlist(opts);
  1562. if (addrs != 0)
  1563. free_wordlist(addrs);
  1564. }
  1565. len = strlen(secbuf);
  1566. if (len > MAXSECRETLEN) {
  1567. error("Secret for %s on %s is too long", client, server);
  1568. len = MAXSECRETLEN;
  1569. }
  1570. BCOPY(secbuf, secret, len);
  1571. BZERO(secbuf, sizeof(secbuf));
  1572. *secret_len = len;
  1573. return 1;
  1574. }
  1575. /*
  1576. * get_srp_secret - open the SRP secret file and return the secret
  1577. * for authenticating the given client on the given server.
  1578. * (We could be either client or server).
  1579. */
  1580. int
  1581. get_srp_secret(unit, client, server, secret, am_server)
  1582. int unit;
  1583. char *client;
  1584. char *server;
  1585. char *secret;
  1586. int am_server;
  1587. {
  1588. FILE *fp;
  1589. int ret;
  1590. char *filename;
  1591. struct wordlist *addrs, *opts;
  1592. if (!am_server && passwd[0] != '\0') {
  1593. strlcpy(secret, passwd, MAXWORDLEN);
  1594. } else {
  1595. filename = _PATH_SRPFILE;
  1596. addrs = NULL;
  1597. fp = fopen(filename, "r");
  1598. if (fp == NULL) {
  1599. error("Can't open srp secret file %s: %m", filename);
  1600. return 0;
  1601. }
  1602. check_access(fp, filename);
  1603. secret[0] = '\0';
  1604. ret = scan_authfile(fp, client, server, secret, &addrs, &opts,
  1605. filename, am_server);
  1606. fclose(fp);
  1607. if (ret < 0)
  1608. return 0;
  1609. if (am_server)
  1610. set_allowed_addrs(unit, addrs, opts);
  1611. else if (opts != NULL)
  1612. free_wordlist(opts);
  1613. if (addrs != NULL)
  1614. free_wordlist(addrs);
  1615. }
  1616. return 1;
  1617. }
  1618. /*
  1619. * set_allowed_addrs() - set the list of allowed addresses.
  1620. * Also looks for `--' indicating options to apply for this peer
  1621. * and leaves the following words in extra_options.
  1622. */
  1623. static void
  1624. set_allowed_addrs(unit, addrs, opts)
  1625. int unit;
  1626. struct wordlist *addrs;
  1627. struct wordlist *opts;
  1628. {
  1629. int n;
  1630. struct wordlist *ap, **plink;
  1631. struct permitted_ip *ip;
  1632. char *ptr_word, *ptr_mask;
  1633. struct hostent *hp;
  1634. struct netent *np;
  1635. u_int32_t a, mask, ah, offset;
  1636. struct ipcp_options *wo = &ipcp_wantoptions[unit];
  1637. u_int32_t suggested_ip = 0;
  1638. if (addresses[unit] != NULL)
  1639. free(addresses[unit]);
  1640. addresses[unit] = NULL;
  1641. if (extra_options != NULL)
  1642. free_wordlist(extra_options);
  1643. extra_options = opts;
  1644. /*
  1645. * Count the number of IP addresses given.
  1646. */
  1647. n = wordlist_count(addrs) + wordlist_count(noauth_addrs);
  1648. if (n == 0)
  1649. return;
  1650. ip = (struct permitted_ip *) malloc((n + 1) * sizeof(struct permitted_ip));
  1651. if (ip == 0)
  1652. return;
  1653. /* temporarily append the noauth_addrs list to addrs */
  1654. for (plink = &addrs; *plink != NULL; plink = &(*plink)->next)
  1655. ;
  1656. *plink = noauth_addrs;
  1657. n = 0;
  1658. for (ap = addrs; ap != NULL; ap = ap->next) {
  1659. /* "-" means no addresses authorized, "*" means any address allowed */
  1660. ptr_word = ap->word;
  1661. if (strcmp(ptr_word, "-") == 0)
  1662. break;
  1663. if (strcmp(ptr_word, "*") == 0) {
  1664. ip[n].permit = 1;
  1665. ip[n].base = ip[n].mask = 0;
  1666. ++n;
  1667. break;
  1668. }
  1669. ip[n].permit = 1;
  1670. if (*ptr_word == '!') {
  1671. ip[n].permit = 0;
  1672. ++ptr_word;
  1673. }
  1674. mask = ~ (u_int32_t) 0;
  1675. offset = 0;
  1676. ptr_mask = strchr (ptr_word, '/');
  1677. if (ptr_mask != NULL) {
  1678. int bit_count;
  1679. char *endp;
  1680. bit_count = (int) strtol (ptr_mask+1, &endp, 10);
  1681. if (bit_count <= 0 || bit_count > 32) {
  1682. warn("invalid address length %v in auth. address list",
  1683. ptr_mask+1);
  1684. continue;
  1685. }
  1686. bit_count = 32 - bit_count; /* # bits in host part */
  1687. if (*endp == '+') {
  1688. offset = ifunit + 1;
  1689. ++endp;
  1690. }
  1691. if (*endp != 0) {
  1692. warn("invalid address length syntax: %v", ptr_mask+1);
  1693. continue;
  1694. }
  1695. *ptr_mask = '\0';
  1696. mask <<= bit_count;
  1697. }
  1698. hp = gethostbyname(ptr_word);
  1699. if (hp != NULL && hp->h_addrtype == AF_INET) {
  1700. a = *(u_int32_t *)hp->h_addr;
  1701. } else {
  1702. np = getnetbyname (ptr_word);
  1703. if (np != NULL && np->n_addrtype == AF_INET) {
  1704. a = htonl ((u_int32_t)np->n_net);
  1705. if (ptr_mask == NULL) {
  1706. /* calculate appropriate mask for net */
  1707. ah = ntohl(a);
  1708. if (IN_CLASSA(ah))
  1709. mask = IN_CLASSA_NET;
  1710. else if (IN_CLASSB(ah))
  1711. mask = IN_CLASSB_NET;
  1712. else if (IN_CLASSC(ah))
  1713. mask = IN_CLASSC_NET;
  1714. }
  1715. } else {
  1716. a = inet_addr (ptr_word);
  1717. }
  1718. }
  1719. if (ptr_mask != NULL)
  1720. *ptr_mask = '/';
  1721. if (a == (u_int32_t)-1L) {
  1722. warn("unknown host %s in auth. address list", ap->word);
  1723. continue;
  1724. }
  1725. if (offset != 0) {
  1726. if (offset >= ~mask) {
  1727. warn("interface unit %d too large for subnet %v",
  1728. ifunit, ptr_word);
  1729. continue;
  1730. }
  1731. a = htonl((ntohl(a) & mask) + offset);
  1732. mask = ~(u_int32_t)0;
  1733. }
  1734. ip[n].mask = htonl(mask);
  1735. ip[n].base = a & ip[n].mask;
  1736. ++n;
  1737. if (~mask == 0 && suggested_ip == 0)
  1738. suggested_ip = a;
  1739. }
  1740. *plink = NULL;
  1741. ip[n].permit = 0; /* make the last entry forbid all addresses */
  1742. ip[n].base = 0; /* to terminate the list */
  1743. ip[n].mask = 0;
  1744. addresses[unit] = ip;
  1745. /*
  1746. * If the address given for the peer isn't authorized, or if
  1747. * the user hasn't given one, AND there is an authorized address
  1748. * which is a single host, then use that if we find one.
  1749. */
  1750. if (suggested_ip != 0
  1751. && (wo->hisaddr == 0 || !auth_ip_addr(unit, wo->hisaddr))) {
  1752. wo->hisaddr = suggested_ip;
  1753. /*
  1754. * Do we insist on this address? No, if there are other
  1755. * addresses authorized than the suggested one.
  1756. */
  1757. if (n > 1)
  1758. wo->accept_remote = 1;
  1759. }
  1760. }
  1761. /*
  1762. * auth_ip_addr - check whether the peer is authorized to use
  1763. * a given IP address. Returns 1 if authorized, 0 otherwise.
  1764. */
  1765. int
  1766. auth_ip_addr(unit, addr)
  1767. int unit;
  1768. u_int32_t addr;
  1769. {
  1770. int ok;
  1771. /* don't allow loopback or multicast address */
  1772. if (bad_ip_adrs(addr))
  1773. return 0;
  1774. if (allowed_address_hook) {
  1775. ok = allowed_address_hook(addr);
  1776. if (ok >= 0) return ok;
  1777. }
  1778. if (addresses[unit] != NULL) {
  1779. ok = ip_addr_check(addr, addresses[unit]);
  1780. if (ok >= 0)
  1781. return ok;
  1782. }
  1783. if (auth_required)
  1784. return 0; /* no addresses authorized */
  1785. return allow_any_ip || privileged || !have_route_to(addr);
  1786. }
  1787. static int
  1788. ip_addr_check(addr, addrs)
  1789. u_int32_t addr;
  1790. struct permitted_ip *addrs;
  1791. {
  1792. for (; ; ++addrs)
  1793. if ((addr & addrs->mask) == addrs->base)
  1794. return addrs->permit;
  1795. }
  1796. /*
  1797. * bad_ip_adrs - return 1 if the IP address is one we don't want
  1798. * to use, such as an address in the loopback net or a multicast address.
  1799. * addr is in network byte order.
  1800. */
  1801. int
  1802. bad_ip_adrs(addr)
  1803. u_int32_t addr;
  1804. {
  1805. addr = ntohl(addr);
  1806. return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET
  1807. || IN_MULTICAST(addr) || IN_BADCLASS(addr);
  1808. }
  1809. /*
  1810. * some_ip_ok - check a wordlist to see if it authorizes any
  1811. * IP address(es).
  1812. */
  1813. static int
  1814. some_ip_ok(addrs)
  1815. struct wordlist *addrs;
  1816. {
  1817. for (; addrs != 0; addrs = addrs->next) {
  1818. if (addrs->word[0] == '-')
  1819. break;
  1820. if (addrs->word[0] != '!')
  1821. return 1; /* some IP address is allowed */
  1822. }
  1823. return 0;
  1824. }
  1825. /*
  1826. * auth_number - check whether the remote number is allowed to connect.
  1827. * Returns 1 if authorized, 0 otherwise.
  1828. */
  1829. int
  1830. auth_number()
  1831. {
  1832. struct wordlist *wp = permitted_numbers;
  1833. int l;
  1834. /* Allow all if no authorization list. */
  1835. if (!wp)
  1836. return 1;
  1837. /* Allow if we have a match in the authorization list. */
  1838. while (wp) {
  1839. /* trailing '*' wildcard */
  1840. l = strlen(wp->word);
  1841. if ((wp->word)[l - 1] == '*')
  1842. l--;
  1843. if (!strncasecmp(wp->word, remote_number, l))
  1844. return 1;
  1845. wp = wp->next;
  1846. }
  1847. return 0;
  1848. }
  1849. /*
  1850. * check_access - complain if a secret file has too-liberal permissions.
  1851. */
  1852. static void
  1853. check_access(f, filename)
  1854. FILE *f;
  1855. char *filename;
  1856. {
  1857. struct stat sbuf;
  1858. if (fstat(fileno(f), &sbuf) < 0) {
  1859. warn("cannot stat secret file %s: %m", filename);
  1860. } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) {
  1861. warn("Warning - secret file %s has world and/or group access",
  1862. filename);
  1863. }
  1864. }
  1865. /*
  1866. * scan_authfile - Scan an authorization file for a secret suitable
  1867. * for authenticating `client' on `server'. The return value is -1
  1868. * if no secret is found, otherwise >= 0. The return value has
  1869. * NONWILD_CLIENT set if the secret didn't have "*" for the client, and
  1870. * NONWILD_SERVER set if the secret didn't have "*" for the server.
  1871. * Any following words on the line up to a "--" (i.e. address authorization
  1872. * info) are placed in a wordlist and returned in *addrs. Any
  1873. * following words (extra options) are placed in a wordlist and
  1874. * returned in *opts.
  1875. * We assume secret is NULL or points to MAXWORDLEN bytes of space.
  1876. * Flags are non-zero if we need two colons in the secret in order to
  1877. * match.
  1878. */
  1879. static int
  1880. scan_authfile(f, client, server, secret, addrs, opts, filename, flags)
  1881. FILE *f;
  1882. char *client;
  1883. char *server;
  1884. char *secret;
  1885. struct wordlist **addrs;
  1886. struct wordlist **opts;
  1887. char *filename;
  1888. int flags;
  1889. {
  1890. int newline, xxx;
  1891. int got_flag, best_flag;
  1892. FILE *sf;
  1893. struct wordlist *ap, *addr_list, *alist, **app;
  1894. char word[MAXWORDLEN];
  1895. char atfile[MAXWORDLEN];
  1896. char lsecret[MAXWORDLEN];
  1897. char *cp;
  1898. if (addrs != NULL)
  1899. *addrs = NULL;
  1900. if (opts != NULL)
  1901. *opts = NULL;
  1902. addr_list = NULL;
  1903. if (!getword(f, word, &newline, filename))
  1904. return -1; /* file is empty??? */
  1905. newline = 1;
  1906. best_flag = -1;
  1907. for (;;) {
  1908. /*
  1909. * Skip until we find a word at the start of a line.
  1910. */
  1911. while (!newline && getword(f, word, &newline, filename))
  1912. ;
  1913. if (!newline)
  1914. break; /* got to end of file */
  1915. /*
  1916. * Got a client - check if it's a match or a wildcard.
  1917. */
  1918. got_flag = 0;
  1919. if (client != NULL && strcmp(word, client) != 0 && !ISWILD(word)) {
  1920. newline = 0;
  1921. continue;
  1922. }
  1923. if (!ISWILD(word))
  1924. got_flag = NONWILD_CLIENT;
  1925. /*
  1926. * Now get a server and check if it matches.
  1927. */
  1928. if (!getword(f, word, &newline, filename))
  1929. break;
  1930. if (newline)
  1931. continue;
  1932. if (!ISWILD(word)) {
  1933. if (server != NULL && strcmp(word, server) != 0)
  1934. continue;
  1935. got_flag |= NONWILD_SERVER;
  1936. }
  1937. /*
  1938. * Got some sort of a match - see if it's better than what
  1939. * we have already.
  1940. */
  1941. if (got_flag <= best_flag)
  1942. continue;
  1943. /*
  1944. * Get the secret.
  1945. */
  1946. if (!getword(f, word, &newline, filename))
  1947. break;
  1948. if (newline)
  1949. continue;
  1950. /*
  1951. * SRP-SHA1 authenticator should never be reading secrets from
  1952. * a file. (Authenticatee may, though.)
  1953. */
  1954. if (flags && ((cp = strchr(word, ':')) == NULL ||
  1955. strchr(cp + 1, ':') == NULL))
  1956. continue;
  1957. if (secret != NULL) {
  1958. /*
  1959. * Special syntax: @/pathname means read secret from file.
  1960. */
  1961. if (word[0] == '@' && word[1] == '/') {
  1962. strlcpy(atfile, word+1, sizeof(atfile));
  1963. if ((sf = fopen(atfile, "r")) == NULL) {
  1964. warn("can't open indirect secret file %s", atfile);
  1965. continue;
  1966. }
  1967. check_access(sf, atfile);
  1968. if (!getword(sf, word, &xxx, atfile)) {
  1969. warn("no secret in indirect secret file %s", atfile);
  1970. fclose(sf);
  1971. continue;
  1972. }
  1973. fclose(sf);
  1974. }
  1975. strlcpy(lsecret, word, sizeof(lsecret));
  1976. }
  1977. /*
  1978. * Now read address authorization info and make a wordlist.
  1979. */
  1980. app = &alist;
  1981. for (;;) {
  1982. if (!getword(f, word, &newline, filename) || newline)
  1983. break;
  1984. ap = (struct wordlist *)
  1985. malloc(sizeof(struct wordlist) + strlen(word) + 1);
  1986. if (ap == NULL)
  1987. novm("authorized addresses");
  1988. ap->word = (char *) (ap + 1);
  1989. strcpy(ap->word, word);
  1990. *app = ap;
  1991. app = &ap->next;
  1992. }
  1993. *app = NULL;
  1994. /*
  1995. * This is the best so far; remember it.
  1996. */
  1997. best_flag = got_flag;
  1998. if (addr_list)
  1999. free_wordlist(addr_list);
  2000. addr_list = alist;
  2001. if (secret != NULL)
  2002. strlcpy(secret, lsecret, MAXWORDLEN);
  2003. if (!newline)
  2004. break;
  2005. }
  2006. /* scan for a -- word indicating the start of options */
  2007. for (app = &addr_list; (ap = *app) != NULL; app = &ap->next)
  2008. if (strcmp(ap->word, "--") == 0)
  2009. break;
  2010. /* ap = start of options */
  2011. if (ap != NULL) {
  2012. ap = ap->next; /* first option */
  2013. free(*app); /* free the "--" word */
  2014. *app = NULL; /* terminate addr list */
  2015. }
  2016. if (opts != NULL)
  2017. *opts = ap;
  2018. else if (ap != NULL)
  2019. free_wordlist(ap);
  2020. if (addrs != NULL)
  2021. *addrs = addr_list;
  2022. else if (addr_list != NULL)
  2023. free_wordlist(addr_list);
  2024. return best_flag;
  2025. }
  2026. /*
  2027. * wordlist_count - return the number of items in a wordlist
  2028. */
  2029. static int
  2030. wordlist_count(wp)
  2031. struct wordlist *wp;
  2032. {
  2033. int n;
  2034. for (n = 0; wp != NULL; wp = wp->next)
  2035. ++n;
  2036. return n;
  2037. }
  2038. /*
  2039. * free_wordlist - release memory allocated for a wordlist.
  2040. */
  2041. static void
  2042. free_wordlist(wp)
  2043. struct wordlist *wp;
  2044. {
  2045. struct wordlist *next;
  2046. while (wp != NULL) {
  2047. next = wp->next;
  2048. free(wp);
  2049. wp = next;
  2050. }
  2051. }
  2052. /*
  2053. * auth_script_done - called when the auth-up or auth-down script
  2054. * has finished.
  2055. */
  2056. static void
  2057. auth_script_done(arg)
  2058. void *arg;
  2059. {
  2060. auth_script_pid = 0;
  2061. switch (auth_script_state) {
  2062. case s_up:
  2063. if (auth_state == s_down) {
  2064. auth_script_state = s_down;
  2065. auth_script(_PATH_AUTHDOWN);
  2066. }
  2067. break;
  2068. case s_down:
  2069. if (auth_state == s_up) {
  2070. auth_script_state = s_up;
  2071. auth_script(_PATH_AUTHUP);
  2072. }
  2073. break;
  2074. }
  2075. }
  2076. /*
  2077. * auth_script - execute a script with arguments
  2078. * interface-name peer-name real-user tty speed
  2079. */
  2080. static void
  2081. auth_script(script)
  2082. char *script;
  2083. {
  2084. char strspeed[32];
  2085. struct passwd *pw;
  2086. char struid[32];
  2087. char *user_name;
  2088. char *argv[8];
  2089. if ((pw = getpwuid(getuid())) != NULL && pw->pw_name != NULL)
  2090. user_name = pw->pw_name;
  2091. else {
  2092. slprintf(struid, sizeof(struid), "%d", getuid());
  2093. user_name = struid;
  2094. }
  2095. slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
  2096. argv[0] = script;
  2097. argv[1] = ifname;
  2098. argv[2] = peer_authname;
  2099. argv[3] = user_name;
  2100. argv[4] = devnam;
  2101. argv[5] = strspeed;
  2102. argv[6] = NULL;
  2103. auth_script_pid = run_program(script, argv, 0, auth_script_done, NULL, 0);
  2104. }