Mercurial > flash_v2
annotate packages/net/ppp/current/src/auth.c @ 1777:c16341b1bac6 default tip
* Added execute permissions to files missed in conversion from CVS
| author | alexs |
|---|---|
| date | Mon, 12 Oct 2009 02:26:09 +0100 |
| parents | 5629b7c30ed2 |
| children |
| rev | line source |
|---|---|
| 1586 | 1 //========================================================================== |
| 2 // | |
| 3 // src/auth.c | |
| 4 // | |
| 5 //========================================================================== | |
| 6 //####ECOSGPLCOPYRIGHTBEGIN#### | |
| 7 // ------------------------------------------- | |
| 8 // This file is part of eCos, the Embedded Configurable Operating System. | |
| 9 // Portions created by Nick Garnett are | |
| 10 // Copyright (C) 2003 eCosCentric Ltd. | |
| 11 // | |
| 12 // eCos is free software; you can redistribute it and/or modify it under | |
| 13 // the terms of the GNU General Public License as published by the Free | |
| 14 // Software Foundation; either version 2 or (at your option) any later version. | |
| 15 // | |
| 16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY | |
| 17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 19 // for more details. | |
| 20 // | |
| 21 // You should have received a copy of the GNU General Public License along | |
| 22 // with eCos; if not, write to the Free Software Foundation, Inc., | |
| 23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
| 24 // | |
| 25 // As a special exception, if other files instantiate templates or use macros | |
| 26 // or inline functions from this file, or you compile this file and link it | |
| 27 // with other works to produce a work based on this file, this file does not | |
| 28 // by itself cause the resulting work to be covered by the GNU General Public | |
| 29 // License. However the source code for this file must still be made available | |
| 30 // in accordance with section (3) of the GNU General Public License. | |
| 31 // | |
| 32 // This exception does not invalidate any other reasons why a work based on | |
| 33 // this file might be covered by the GNU General Public License. | |
| 34 // | |
| 35 // ------------------------------------------- | |
| 36 //####ECOSGPLCOPYRIGHTEND#### | |
| 37 //####BSDCOPYRIGHTBEGIN#### | |
| 38 // | |
| 39 // ------------------------------------------- | |
| 40 // | |
| 41 // Portions of this software may have been derived from OpenBSD, | |
| 42 // FreeBSD or other sources, and are covered by the appropriate | |
| 43 // copyright disclaimers included herein. | |
| 44 // | |
| 45 // ------------------------------------------- | |
| 46 // | |
| 47 //####BSDCOPYRIGHTEND#### | |
| 48 //========================================================================== | |
| 49 | |
| 50 /* | |
| 51 * auth.c - PPP authentication and phase control. | |
| 52 * | |
| 53 * Copyright (c) 1993 The Australian National University. | |
| 54 * All rights reserved. | |
| 55 * | |
| 56 * Redistribution and use in source and binary forms are permitted | |
| 57 * provided that the above copyright notice and this paragraph are | |
| 58 * duplicated in all such forms and that any documentation, | |
| 59 * advertising materials, and other materials related to such | |
| 60 * distribution and use acknowledge that the software was developed | |
| 61 * by the Australian National University. The name of the University | |
| 62 * may not be used to endorse or promote products derived from this | |
| 63 * software without specific prior written permission. | |
| 64 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
| 65 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
| 66 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
| 67 * | |
| 68 * Copyright (c) 1989 Carnegie Mellon University. | |
| 69 * All rights reserved. | |
| 70 * | |
| 71 * Redistribution and use in source and binary forms are permitted | |
| 72 * provided that the above copyright notice and this paragraph are | |
| 73 * duplicated in all such forms and that any documentation, | |
| 74 * advertising materials, and other materials related to such | |
| 75 * distribution and use acknowledge that the software was developed | |
| 76 * by Carnegie Mellon University. The name of the | |
| 77 * University may not be used to endorse or promote products derived | |
| 78 * from this software without specific prior written permission. | |
| 79 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
| 80 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
| 81 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
| 82 */ | |
| 83 | |
| 84 #ifndef lint | |
| 85 //static char rcsid[] = "$FreeBSD: src/usr.sbin/pppd/auth.c,v 1.24.2.1 2000/12/11 01:03:37 obrien Exp $"; | |
| 86 #endif | |
| 87 | |
| 88 #include <stdio.h> | |
| 89 #include <stddef.h> | |
| 90 #include <stdlib.h> | |
| 91 #include <unistd.h> | |
| 92 #include <cyg/ppp/syslog.h> | |
| 1628 | 93 #ifndef __ECOS |
| 1586 | 94 #include <paths.h> |
| 1628 | 95 #endif |
| 1586 | 96 //#include <pwd.h> |
| 97 #include <string.h> | |
| 98 #include <sys/types.h> | |
| 99 #include <sys/stat.h> | |
| 100 #include <sys/socket.h> | |
| 101 //#include <utmp.h> | |
| 102 #include <fcntl.h> | |
| 103 #if defined(_PATH_LASTLOG) && defined(_linux_) | |
| 104 #include <lastlog.h> | |
| 105 #endif | |
| 106 | |
| 107 #include <netdb.h> | |
| 108 #include <netinet/in.h> | |
| 109 #include <arpa/inet.h> | |
| 110 #include <sys/time.h> | |
| 111 //#include <utmp.h> | |
| 112 | |
| 113 #ifdef USE_PAM | |
| 114 #include <security/pam_appl.h> | |
| 115 #endif | |
| 116 | |
| 117 #ifdef HAS_SHADOW | |
| 118 #include <shadow.h> | |
| 119 #ifndef PW_PPP | |
| 120 #define PW_PPP PW_LOGIN | |
| 121 #endif | |
| 122 #endif | |
| 123 | |
| 124 #include "cyg/ppp/pppd.h" | |
| 125 #include "cyg/ppp/fsm.h" | |
| 126 #include "cyg/ppp/lcp.h" | |
| 127 #include "cyg/ppp/ipcp.h" | |
| 128 #include "cyg/ppp/upap.h" | |
| 129 #include "cyg/ppp/chap.h" | |
| 130 #ifdef CBCP_SUPPORT | |
| 131 #include "cyg/ppp/cbcp.h" | |
| 132 #endif | |
| 133 //#include "cyg/ppp/pathnames.h" | |
| 134 | |
| 135 /* Used for storing a sequence of words. Usually malloced. */ | |
| 136 struct wordlist { | |
| 137 struct wordlist *next; | |
| 138 char word[1]; | |
| 139 }; | |
| 140 | |
| 141 /* Bits in scan_authfile return value */ | |
| 142 #define NONWILD_SERVER 1 | |
| 143 #define NONWILD_CLIENT 2 | |
| 144 | |
| 145 #define ISWILD(word) (word[0] == '*' && word[1] == 0) | |
| 146 | |
| 147 #define FALSE 0 | |
| 148 #define TRUE 1 | |
| 149 | |
| 150 /* The name by which the peer authenticated itself to us. */ | |
| 151 char peer_authname[MAXNAMELEN]; | |
| 152 | |
| 153 /* Records which authentication operations haven't completed yet. */ | |
| 154 static int auth_pending[NUM_PPP]; | |
| 155 | |
| 156 /* Set if we have successfully called plogin() */ | |
| 157 //static int logged_in; | |
| 158 | |
| 159 /* Set if not wild or blank */ | |
| 160 //static int non_wildclient; | |
| 161 | |
| 162 /* Set if we have run the /etc/ppp/auth-up script. */ | |
| 163 //static int did_authup; | |
| 164 | |
| 165 /* List of addresses which the peer may use. */ | |
| 166 static struct wordlist *addresses[NUM_PPP]; | |
| 167 | |
| 168 /* Number of network protocols which we have opened. */ | |
| 169 static int num_np_open; | |
| 170 | |
| 171 /* Number of network protocols which have come up. */ | |
| 172 static int num_np_up; | |
| 173 | |
| 174 /* Set if we got the contents of passwd[] from the pap-secrets file. */ | |
| 175 static int passwd_from_file; | |
| 176 | |
| 177 /* Bits in auth_pending[] */ | |
| 178 #define PAP_WITHPEER 1 | |
| 179 #define PAP_PEER 2 | |
| 180 #define CHAP_WITHPEER 4 | |
| 181 #define CHAP_PEER 8 | |
| 182 | |
| 183 extern char *crypt __P((const char *, const char *)); | |
| 184 | |
| 185 /* Prototypes for procedures local to this file. */ | |
| 186 | |
| 187 static void network_phase __P((int)); | |
| 188 static void check_idle __P((void *)); | |
| 189 static void connect_time_expired __P((void *)); | |
| 190 static int null_login __P((int)); | |
| 191 static int get_pap_passwd __P((char *)); | |
| 192 static int have_pap_secret __P((void)); | |
| 193 static int have_chap_secret __P((char *, char *, u_int32_t)); | |
| 194 static int ip_addr_check __P((u_int32_t, struct wordlist *)); | |
| 195 | |
| 196 static void auth_set_ip_addr __P((int)); | |
| 197 | |
| 198 /* | |
| 199 * An Open on LCP has requested a change from Dead to Establish phase. | |
| 200 * Do what's necessary to bring the physical layer up. | |
| 201 */ | |
| 202 void | |
| 203 link_required(unit) | |
| 204 int unit; | |
| 205 { | |
| 206 } | |
| 207 | |
| 208 /* | |
| 209 * LCP has terminated the link; go to the Dead phase and take the | |
| 210 * physical layer down. | |
| 211 */ | |
| 212 void | |
| 213 link_terminated(unit) | |
| 214 int unit; | |
| 215 { | |
| 216 extern time_t etime, stime; | |
| 217 extern int minutes; | |
| 218 | |
| 219 db_printf("%s()\n",__PRETTY_FUNCTION__); | |
| 220 if (phase == PHASE_DEAD) | |
| 221 return; | |
| 222 phase = PHASE_DEAD; | |
| 223 etime = time((time_t *) NULL); | |
| 224 minutes = (etime-stime)/60; | |
| 225 syslog(LOG_NOTICE, "Connection terminated, connected for %d minutes\n", | |
| 226 minutes > 1 ? minutes : 1); | |
| 227 } | |
| 228 | |
| 229 /* | |
| 230 * LCP has gone down; it will either die or try to re-establish. | |
| 231 */ | |
| 232 void | |
| 233 link_down(unit) | |
| 234 int unit; | |
| 235 { | |
| 236 int i; | |
| 237 struct protent *protp; | |
| 238 | |
| 239 db_printf("%s()\n",__PRETTY_FUNCTION__); | |
| 240 for (i = 0; (protp = protocols[i]) != NULL; ++i) { | |
| 241 if (!protp->enabled_flag) | |
| 242 continue; | |
| 243 if (protp->protocol != PPP_LCP && protp->lowerdown != NULL) | |
| 244 (*protp->lowerdown)(unit); | |
| 245 if (protp->protocol < 0xC000 && protp->close != NULL) | |
| 246 (*protp->close)(unit, "LCP down"); | |
| 247 } | |
| 248 num_np_open = 0; | |
| 249 num_np_up = 0; | |
| 250 if (phase != PHASE_DEAD) | |
| 251 phase = PHASE_TERMINATE; | |
| 252 } | |
| 253 | |
| 254 /* | |
| 255 * The link is established. | |
| 256 * Proceed to the Dead, Authenticate or Network phase as appropriate. | |
| 257 */ | |
| 258 void | |
| 259 link_established(unit) | |
| 260 int unit; | |
| 261 { | |
| 262 int auth; | |
| 263 lcp_options *wo = &lcp_wantoptions[unit]; | |
| 264 lcp_options *go = &lcp_gotoptions[unit]; | |
| 265 lcp_options *ho = &lcp_hisoptions[unit]; | |
| 266 int i; | |
| 267 struct protent *protp; | |
| 268 | |
| 269 /* | |
| 270 * Tell higher-level protocols that LCP is up. | |
| 271 */ | |
| 272 for (i = 0; (protp = protocols[i]) != NULL; ++i) | |
| 273 if (protp->protocol != PPP_LCP && protp->enabled_flag | |
| 274 && protp->lowerup != NULL) | |
| 275 (*protp->lowerup)(unit); | |
| 276 | |
| 277 if (auth_required && !(go->neg_chap || go->neg_upap)) { | |
| 278 /* | |
| 279 * We wanted the peer to authenticate itself, and it refused: | |
| 280 * treat it as though it authenticated with PAP using a username | |
| 281 * of "" and a password of "". If that's not OK, boot it out. | |
| 282 */ | |
| 283 if (!wo->neg_upap || !null_login(unit)) { | |
| 284 syslog(LOG_WARNING, "peer refused to authenticate"); | |
| 285 lcp_close(unit, "peer refused to authenticate"); | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
286 cyg_ppp_stats.auth_failures++; |
| 1586 | 287 return; |
| 288 } | |
| 289 } | |
| 290 | |
| 291 phase = PHASE_AUTHENTICATE; | |
| 292 auth = 0; | |
| 293 if (go->neg_chap) { | |
| 294 ChapAuthPeer(unit, our_name, go->chap_mdtype); | |
| 295 auth |= CHAP_PEER; | |
| 296 } else if (go->neg_upap) { | |
| 297 upap_authpeer(unit); | |
| 298 auth |= PAP_PEER; | |
| 299 } | |
| 300 if (ho->neg_chap) { | |
| 301 ChapAuthWithPeer(unit, user, ho->chap_mdtype); | |
| 302 auth |= CHAP_WITHPEER; | |
| 303 } else if (ho->neg_upap) { | |
| 304 if (passwd[0] == 0) { | |
| 305 passwd_from_file = 1; | |
| 306 if (!get_pap_passwd(passwd)) | |
| 307 syslog(LOG_ERR, "No secret found for PAP login"); | |
| 308 } | |
| 309 upap_authwithpeer(unit, user, passwd); | |
| 310 auth |= PAP_WITHPEER; | |
| 311 } | |
| 312 auth_pending[unit] = auth; | |
| 313 | |
| 314 if (!auth) | |
| 315 network_phase(unit); | |
| 316 } | |
| 317 | |
| 318 /* | |
| 319 * Proceed to the network phase. | |
| 320 */ | |
| 321 static void | |
| 322 network_phase(unit) | |
| 323 int unit; | |
| 324 { | |
| 325 int i; | |
| 326 struct protent *protp; | |
| 327 | |
| 328 #ifdef CBCP_SUPPORT | |
| 329 /* | |
| 330 * If we negotiated callback, do it now. | |
| 331 */ | |
| 332 if (go->neg_cbcp) { | |
| 333 phase = PHASE_CALLBACK; | |
| 334 (*cbcp_protent.open)(unit); | |
| 335 return; | |
| 336 } | |
| 337 #endif | |
| 338 | |
| 339 phase = PHASE_NETWORK; | |
| 340 for (i = 0; (protp = protocols[i]) != NULL; ++i) | |
| 341 if (protp->protocol < 0xC000 && protp->enabled_flag | |
| 342 && protp->open != NULL) { | |
| 343 (*protp->open)(unit); | |
| 344 if (protp->protocol != PPP_CCP) | |
| 345 ++num_np_open; | |
| 346 } | |
| 347 | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
348 if (num_np_open == 0) { |
| 1586 | 349 /* nothing to do */ |
| 350 lcp_close(0, "No network protocols running"); | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
351 cyg_ppp_stats.no_proto++; |
|
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
352 } |
| 1586 | 353 } |
| 354 | |
| 355 /* | |
| 356 * The peer has failed to authenticate himself using `protocol'. | |
| 357 */ | |
| 358 void | |
| 359 auth_peer_fail(unit, protocol) | |
| 360 int unit, protocol; | |
| 361 { | |
| 362 /* | |
| 363 * Authentication failure: take the link down | |
| 364 */ | |
| 365 lcp_close(unit, "Authentication failed"); | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
366 cyg_ppp_stats.auth_failures++; |
| 1586 | 367 } |
| 368 | |
| 369 /* | |
| 370 * The peer has been successfully authenticated using `protocol'. | |
| 371 */ | |
| 372 void | |
| 373 auth_peer_success(unit, protocol, name, namelen) | |
| 374 int unit, protocol; | |
| 375 char *name; | |
| 376 int namelen; | |
| 377 { | |
| 378 int bit; | |
| 379 | |
| 380 switch (protocol) { | |
| 381 case PPP_CHAP: | |
| 382 bit = CHAP_PEER; | |
| 383 break; | |
| 384 case PPP_PAP: | |
| 385 bit = PAP_PEER; | |
| 386 break; | |
| 387 default: | |
| 388 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x", | |
| 389 protocol); | |
| 390 return; | |
| 391 } | |
| 392 | |
| 393 /* | |
| 394 * Save the authenticated name of the peer for later. | |
| 395 */ | |
| 396 if (namelen > sizeof(peer_authname) - 1) | |
| 397 namelen = sizeof(peer_authname) - 1; | |
| 398 BCOPY(name, peer_authname, namelen); | |
| 399 peer_authname[namelen] = 0; | |
| 400 | |
| 401 /* | |
| 402 * If we have overridden addresses based on auth info | |
| 403 * then set that information now before continuing. | |
| 404 */ | |
| 405 auth_set_ip_addr(unit); | |
| 406 | |
| 407 /* | |
| 408 * If there is no more authentication still to be done, | |
| 409 * proceed to the network (or callback) phase. | |
| 410 */ | |
| 411 if ((auth_pending[unit] &= ~bit) == 0) | |
| 412 network_phase(unit); | |
| 413 } | |
| 414 | |
| 415 /* | |
| 416 * We have failed to authenticate ourselves to the peer using `protocol'. | |
| 417 */ | |
| 418 void | |
| 419 auth_withpeer_fail(unit, protocol) | |
| 420 int unit, protocol; | |
| 421 { | |
| 422 if (passwd_from_file) | |
| 423 BZERO(passwd, MAXSECRETLEN); | |
| 424 /* | |
| 425 * We've failed to authenticate ourselves to our peer. | |
| 426 * He'll probably take the link down, and there's not much | |
| 427 * we can do except wait for that. | |
| 428 */ | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
429 cyg_ppp_stats.auth_failures++; |
| 1586 | 430 } |
| 431 | |
| 432 /* | |
| 433 * We have successfully authenticated ourselves with the peer using `protocol'. | |
| 434 */ | |
| 435 void | |
| 436 auth_withpeer_success(unit, protocol) | |
| 437 int unit, protocol; | |
| 438 { | |
| 439 int bit; | |
| 440 | |
| 441 switch (protocol) { | |
| 442 case PPP_CHAP: | |
| 443 bit = CHAP_WITHPEER; | |
| 444 break; | |
| 445 case PPP_PAP: | |
| 446 if (passwd_from_file) | |
| 447 BZERO(passwd, MAXSECRETLEN); | |
| 448 bit = PAP_WITHPEER; | |
| 449 break; | |
| 450 default: | |
| 451 syslog(LOG_WARNING, "auth_peer_success: unknown protocol %x", | |
| 452 protocol); | |
| 453 bit = 0; | |
| 454 } | |
| 455 | |
| 456 /* | |
| 457 * If we have overridden addresses based on auth info | |
| 458 * then set that information now before continuing. | |
| 459 */ | |
| 460 auth_set_ip_addr(unit); | |
| 461 | |
| 462 /* | |
| 463 * If there is no more authentication still being done, | |
| 464 * proceed to the network (or callback) phase. | |
| 465 */ | |
| 466 if ((auth_pending[unit] &= ~bit) == 0) | |
| 467 network_phase(unit); | |
| 468 } | |
| 469 | |
| 470 | |
| 471 /* | |
| 472 * np_up - a network protocol has come up. | |
| 473 */ | |
| 474 void | |
| 475 np_up(unit, proto) | |
| 476 int unit, proto; | |
| 477 { | |
| 478 if (num_np_up == 0) { | |
| 479 /* | |
| 480 * At this point we consider that the link has come up successfully. | |
| 481 */ | |
| 482 need_holdoff = 0; | |
| 483 | |
| 484 if (idle_time_limit > 0) | |
| 485 TIMEOUT(check_idle, NULL, idle_time_limit); | |
| 486 | |
| 487 /* | |
| 488 * Set a timeout to close the connection once the maximum | |
| 489 * connect time has expired. | |
| 490 */ | |
| 491 if (maxconnect > 0) | |
| 492 TIMEOUT(connect_time_expired, 0, maxconnect); | |
| 493 | |
| 494 } | |
| 495 ++num_np_up; | |
| 496 } | |
| 497 | |
| 498 /* | |
| 499 * np_down - a network protocol has gone down. | |
| 500 */ | |
| 501 void | |
| 502 np_down(unit, proto) | |
| 503 int unit, proto; | |
| 504 { | |
| 505 if (--num_np_up == 0 && idle_time_limit > 0) { | |
| 506 UNTIMEOUT(check_idle, NULL); | |
| 507 } | |
| 508 } | |
| 509 | |
| 510 /* | |
| 511 * np_finished - a network protocol has finished using the link. | |
| 512 */ | |
| 513 void | |
| 514 np_finished(unit, proto) | |
| 515 int unit, proto; | |
| 516 { | |
| 517 if (--num_np_open <= 0) { | |
| 518 /* no further use for the link: shut up shop. */ | |
| 519 lcp_close(0, "No network protocols running"); | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
520 cyg_ppp_stats.no_proto++; |
| 1586 | 521 } |
| 522 } | |
| 523 | |
| 524 /* | |
| 525 * check_idle - check whether the link has been idle for long | |
| 526 * enough that we can shut it down. | |
| 527 */ | |
| 528 static void | |
| 529 check_idle(arg) | |
| 530 void *arg; | |
| 531 { | |
| 532 struct ppp_idle idle; | |
| 533 time_t itime; | |
| 534 | |
| 535 if (!get_idle_time(0, &idle)) | |
| 536 return; | |
| 537 itime = MIN(idle.xmit_idle, idle.recv_idle); | |
| 538 if (itime >= idle_time_limit) { | |
| 539 /* link is idle: shut it down. */ | |
| 540 syslog(LOG_INFO, "Terminating connection due to lack of activity."); | |
| 541 lcp_close(0, "Link inactive"); | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
542 cyg_ppp_stats.idle_timeout++; |
| 1586 | 543 } else { |
| 544 TIMEOUT(check_idle, NULL, idle_time_limit - itime); | |
| 545 } | |
| 546 } | |
| 547 | |
| 548 /* | |
| 549 * connect_time_expired - log a message and close the connection. | |
| 550 */ | |
| 551 static void | |
| 552 connect_time_expired(arg) | |
| 553 void *arg; | |
| 554 { | |
| 555 syslog(LOG_INFO, "Connect time expired"); | |
| 556 lcp_close(0, "Connect time expired"); /* Close connection */ | |
|
1694
5629b7c30ed2
Various patches from Matt Jerdonek and Oyvind Harboe:
nickg
parents:
1628
diff
changeset
|
557 cyg_ppp_stats.connect_time_expired++; |
| 1586 | 558 } |
| 559 | |
| 560 /* | |
| 561 * auth_check_options - called to check authentication options. | |
| 562 */ | |
| 563 void | |
| 564 auth_check_options() | |
| 565 { | |
| 566 lcp_options *wo = &lcp_wantoptions[0]; | |
| 567 int can_auth; | |
| 568 ipcp_options *ipwo = &ipcp_wantoptions[0]; | |
| 569 u_int32_t remote; | |
| 570 | |
| 571 /* Default our_name to hostname, and user to our_name */ | |
| 572 if (our_name[0] == 0 || usehostname) | |
| 573 strcpy(our_name, cyg_ppp_hostname); | |
| 574 if (user[0] == 0) | |
| 575 strcpy(user, our_name); | |
| 576 | |
| 577 /* If authentication is required, ask peer for CHAP or PAP. */ | |
| 578 if (auth_required && !wo->neg_chap && !wo->neg_upap) { | |
| 579 wo->neg_chap = 1; | |
| 580 wo->neg_upap = 1; | |
| 581 } | |
| 582 | |
| 583 /* | |
| 584 * Check whether we have appropriate secrets to use | |
| 585 * to authenticate the peer. | |
| 586 */ | |
| 587 can_auth = wo->neg_upap && (uselogin || have_pap_secret()); | |
| 588 if (!can_auth && wo->neg_chap) { | |
| 589 remote = ipwo->accept_remote? 0: ipwo->hisaddr; | |
| 590 can_auth = have_chap_secret(remote_name, our_name, remote); | |
| 591 } | |
| 592 | |
| 593 if (auth_required && !can_auth) { | |
| 594 option_error("peer authentication required but no suitable secret(s) found\n"); | |
| 595 if (remote_name[0] == 0) | |
| 596 option_error("for authenticating any peer to us (%s)\n", our_name); | |
| 597 else | |
| 598 option_error("for authenticating peer %s to us (%s)\n", | |
| 599 remote_name, our_name); | |
| 600 exit(1); | |
| 601 } | |
| 602 | |
| 603 /* | |
| 604 * Check whether the user tried to override certain values | |
| 605 * set by root. | |
| 606 */ | |
| 607 if (!auth_required && auth_req_info.priv > 0) { | |
| 608 if (!default_device && devnam_info.priv == 0) { | |
| 609 option_error("can't override device name when noauth option used"); | |
| 610 exit(1); | |
| 611 } | |
| 612 if ((connector != NULL && connector_info.priv == 0) | |
| 613 || (disconnector != NULL && disconnector_info.priv == 0) | |
| 614 || (welcomer != NULL && welcomer_info.priv == 0)) { | |
| 615 option_error("can't override connect, disconnect or welcome"); | |
| 616 option_error("option values when noauth option used"); | |
| 617 exit(1); | |
| 618 } | |
| 619 } | |
| 620 } | |
| 621 | |
| 622 /* | |
| 623 * auth_reset - called when LCP is starting negotiations to recheck | |
| 624 * authentication options, i.e. whether we have appropriate secrets | |
| 625 * to use for authenticating ourselves and/or the peer. | |
| 626 */ | |
| 627 void | |
| 628 auth_reset(unit) | |
| 629 int unit; | |
| 630 { | |
| 631 lcp_options *go = &lcp_gotoptions[unit]; | |
| 632 lcp_options *ao = &lcp_allowoptions[0]; | |
| 633 ipcp_options *ipwo = &ipcp_wantoptions[0]; | |
| 634 u_int32_t remote; | |
| 635 ao->neg_upap = !refuse_pap && (passwd[0] != 0 || get_pap_passwd(NULL)); | |
| 636 ao->neg_chap = !refuse_chap | |
| 637 && have_chap_secret(user, remote_name, (u_int32_t)0); | |
| 638 if (go->neg_upap && !uselogin && !have_pap_secret()) | |
| 639 go->neg_upap = 0; | |
| 640 if (go->neg_chap) { | |
| 641 remote = ipwo->accept_remote? 0: ipwo->hisaddr; | |
| 642 if (!have_chap_secret(remote_name, our_name, remote)) | |
| 643 go->neg_chap = 0; | |
| 644 } | |
| 645 } | |
| 646 | |
| 647 | |
| 648 /* | |
| 649 * check_passwd - Check the user name and passwd against the PAP secrets | |
| 650 * file. If requested, also check against the system password database, | |
| 651 * and login the user if OK. | |
| 652 * | |
| 653 * returns: | |
| 654 * UPAP_AUTHNAK: Authentication failed. | |
| 655 * UPAP_AUTHACK: Authentication succeeded. | |
| 656 * In either case, msg points to an appropriate message. | |
| 657 */ | |
| 658 int | |
| 659 check_passwd(unit, auser, userlen, apasswd, passwdlen, msg, msglen) | |
| 660 int unit; | |
| 661 char *auser; | |
| 662 int userlen; | |
| 663 char *apasswd; | |
| 664 int passwdlen; | |
| 665 char **msg; | |
| 666 int *msglen; | |
| 667 { | |
| 668 db_printf("%s called\n", __PRETTY_FUNCTION__); | |
| 669 return 0; | |
| 670 } | |
| 671 | |
| 672 /* | |
| 673 * null_login - Check if a username of "" and a password of "" are | |
| 674 * acceptable, and iff so, set the list of acceptable IP addresses | |
| 675 * and return 1. | |
| 676 */ | |
| 677 static int | |
| 678 null_login(unit) | |
| 679 int unit; | |
| 680 { | |
| 681 db_printf("%s called\n", __PRETTY_FUNCTION__); | |
| 682 return 0; | |
| 683 } | |
| 684 | |
| 685 | |
| 686 /* | |
| 687 * get_pap_passwd - get a password for authenticating ourselves with | |
| 688 * our peer using PAP. Returns 1 on success, 0 if no suitable password | |
| 689 * could be found. | |
| 690 */ | |
| 691 static int | |
| 692 get_pap_passwd(passwd) | |
| 693 char *passwd; | |
| 694 { | |
| 695 db_printf("%s called\n", __PRETTY_FUNCTION__); | |
| 696 return 0; | |
| 697 } | |
| 698 | |
| 699 | |
| 700 /* | |
| 701 * have_pap_secret - check whether we have a PAP file with any | |
| 702 * secrets that we could possibly use for authenticating the peer. | |
| 703 */ | |
| 704 static int | |
| 705 have_pap_secret() | |
| 706 { | |
| 707 db_printf("%s called\n", __PRETTY_FUNCTION__); | |
| 708 return 0; | |
| 709 } | |
| 710 | |
| 711 | |
| 712 /* | |
| 713 * have_chap_secret - check whether we have a CHAP file with a | |
| 714 * secret that we could possibly use for authenticating `client' | |
| 715 * on `server'. Either can be the null string, meaning we don't | |
| 716 * know the identity yet. | |
| 717 */ | |
| 718 static int | |
| 719 have_chap_secret(client, server, remote) | |
| 720 char *client; | |
| 721 char *server; | |
| 722 u_int32_t remote; | |
| 723 { | |
| 724 // db_printf("%s(%s,%s,%d) called\n", __PRETTY_FUNCTION__,client,server,remote); | |
| 725 return 1; | |
| 726 } | |
| 727 | |
| 728 | |
| 729 /* | |
| 730 * get_secret - open the CHAP secret file and return the secret | |
| 731 * for authenticating the given client on the given server. | |
| 732 * (We could be either client or server). | |
| 733 */ | |
| 734 int | |
| 735 get_secret(unit, client, server, secret, secret_len, save_addrs) | |
| 736 int unit; | |
| 737 char *client; | |
| 738 char *server; | |
| 739 char *secret; | |
| 740 int *secret_len; | |
| 741 int save_addrs; | |
| 742 { | |
| 743 db_printf("%s(%d,%s,%s,%08x,%d,%d) called\n", __PRETTY_FUNCTION__, | |
| 744 unit, client, server, secret, secret_len, save_addrs); | |
| 745 // We use the PAP password as the CHAP secret also. | |
| 746 strncpy( secret, passwd, MAXNAMELEN ); | |
| 747 *secret_len = strlen(secret); | |
| 748 return 1; | |
| 749 } | |
| 750 | |
| 751 static void | |
| 752 auth_set_ip_addr(unit) | |
| 753 int unit; | |
| 754 { | |
| 755 db_printf("%s called\n", __PRETTY_FUNCTION__); | |
| 756 } | |
| 757 | |
| 758 /* | |
| 759 * auth_ip_addr - check whether the peer is authorized to use | |
| 760 * a given IP address. Returns 1 if authorized, 0 otherwise. | |
| 761 */ | |
| 762 int | |
| 763 auth_ip_addr(unit, addr) | |
| 764 int unit; | |
| 765 u_int32_t addr; | |
| 766 { | |
| 767 return ip_addr_check(addr, addresses[unit]); | |
| 768 } | |
| 769 | |
| 770 static int | |
| 771 ip_addr_check(addr, addrs) | |
| 772 u_int32_t addr; | |
| 773 struct wordlist *addrs; | |
| 774 { | |
| 775 int x, y; | |
| 776 // u_int32_t a, mask, ah; | |
| 777 u_int32_t a = -1, mask; | |
| 778 int accept; | |
| 779 char *ptr_word, *ptr_mask; | |
| 780 // struct hostent *hp; | |
| 781 // struct netent *np; | |
| 782 | |
| 783 /* don't allow loopback or multicast address */ | |
| 784 if (bad_ip_adrs(addr)) | |
| 785 return 0; | |
| 786 | |
| 787 if (addrs == NULL) | |
| 788 return !auth_required; /* no addresses authorized */ | |
| 789 | |
| 790 x = y = 0; | |
| 791 for (; addrs != NULL; addrs = addrs->next) { | |
| 792 y++; | |
| 793 /* "-" means no addresses authorized, "*" means any address allowed */ | |
| 794 ptr_word = addrs->word; | |
| 795 if (strcmp(ptr_word, "-") == 0) | |
| 796 break; | |
| 797 if (strcmp(ptr_word, "*") == 0) | |
| 798 return 1; | |
| 799 | |
| 800 /* | |
| 801 * A colon in the string means that we wish to force a specific | |
| 802 * local:remote address, but we ignore these for now. | |
| 803 */ | |
| 804 if (strchr(addrs->word, ':') != NULL) | |
| 805 x++; | |
| 806 else { | |
| 807 | |
| 808 accept = 1; | |
| 809 if (*ptr_word == '!') { | |
| 810 accept = 0; | |
| 811 ++ptr_word; | |
| 812 } | |
| 813 | |
| 814 mask = ~ (u_int32_t) 0; | |
| 815 ptr_mask = strchr (ptr_word, '/'); | |
| 816 if (ptr_mask != NULL) { | |
| 817 int bit_count; | |
| 818 | |
| 819 bit_count = (int) strtol (ptr_mask+1, (char **) 0, 10); | |
| 820 if (bit_count <= 0 || bit_count > 32) { | |
| 821 syslog (LOG_WARNING, | |
| 822 "invalid address length %s in auth. address list", | |
| 823 ptr_mask); | |
| 824 continue; | |
| 825 } | |
| 826 *ptr_mask = '\0'; | |
| 827 mask <<= 32 - bit_count; | |
| 828 } | |
| 829 | |
| 830 #ifndef __ECOS | |
| 831 hp = gethostbyname(ptr_word); | |
| 832 if (hp != NULL && hp->h_addrtype == AF_INET) { | |
| 833 a = *(u_int32_t *)hp->h_addr; | |
| 834 } else { | |
| 835 np = getnetbyname (ptr_word); | |
| 836 if (np != NULL && np->n_addrtype == AF_INET) { | |
| 837 a = htonl (*(u_int32_t *)np->n_net); | |
| 838 if (ptr_mask == NULL) { | |
| 839 /* calculate appropriate mask for net */ | |
| 840 ah = ntohl(a); | |
| 841 if (IN_CLASSA(ah)) | |
| 842 mask = IN_CLASSA_NET; | |
| 843 else if (IN_CLASSB(ah)) | |
| 844 mask = IN_CLASSB_NET; | |
| 845 else if (IN_CLASSC(ah)) | |
| 846 mask = IN_CLASSC_NET; | |
| 847 } | |
| 848 } else { | |
| 849 a = inet_addr (ptr_word); | |
| 850 } | |
| 851 } | |
| 852 #endif | |
| 853 | |
| 854 if (ptr_mask != NULL) | |
| 855 *ptr_mask = '/'; | |
| 856 | |
| 857 if (a == (u_int32_t)-1L) | |
| 858 syslog (LOG_WARNING, | |
| 859 "unknown host %s in auth. address list", | |
| 860 addrs->word); | |
| 861 else | |
| 862 /* Here a and addr are in network byte order, | |
| 863 and mask is in host order. */ | |
| 864 if (((addr ^ a) & htonl(mask)) == 0) | |
| 865 return accept; | |
| 866 } /* else */ | |
| 867 } | |
| 868 return x == y; /* not in list => can't have it */ | |
| 869 } | |
| 870 | |
| 871 /* | |
| 872 * bad_ip_adrs - return 1 if the IP address is one we don't want | |
| 873 * to use, such as an address in the loopback net or a multicast address. | |
| 874 * addr is in network byte order. | |
| 875 */ | |
| 876 int | |
| 877 bad_ip_adrs(addr) | |
| 878 u_int32_t addr; | |
| 879 { | |
| 880 addr = ntohl(addr); | |
| 881 return (addr >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET | |
| 882 || IN_MULTICAST(addr) || IN_BADCLASS(addr); | |
| 883 } | |
| 884 | |
| 885 /* | |
| 886 * check_access - complain if a secret file has too-liberal permissions. | |
| 887 */ | |
| 888 void | |
| 889 check_access(f, filename) | |
| 890 FILE *f; | |
| 891 char *filename; | |
| 892 { | |
| 893 struct stat sbuf; | |
| 894 | |
| 895 if (fstat(fileno(f), &sbuf) < 0) { | |
| 896 syslog(LOG_WARNING, "cannot stat secret file %s: %m", filename); | |
| 897 } else if ((sbuf.st_mode & (S_IRWXG | S_IRWXO)) != 0) { | |
| 898 syslog(LOG_WARNING, "Warning - secret file %s has world and/or group access", filename); | |
| 899 } | |
| 900 } |
