📄 auth.c
字号:
/* * auth.c - PPP authentication and phase control. * * Copyright (c) 1993-2002 Paul Mackerras. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. The name(s) of the authors of this software must not be used to * endorse or promote products derived from this software without * prior written permission. * * 3. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by Paul Mackerras * <paulus@samba.org>". * * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Derived from main.c, which is: * * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The name "Carnegie Mellon University" must not be used to * endorse or promote products derived from this software without * prior written permission. For permission or any legal * details, please contact * Office of Technology Transfer * Carnegie Mellon University * 5000 Forbes Avenue * Pittsburgh, PA 15213-3890 * (412) 268-4387, fax: (412) 268-7395 * tech-transfer@andrew.cmu.edu * * 4. Redistributions of any form whatsoever must retain the following * acknowledgment: * "This product includes software developed by Computing Services * at Carnegie Mellon University (http://www.cmu.edu/computing/)." * * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */#define RCSID "$Id: auth.c,v 1.112 2006/06/18 11:26:00 paulus Exp $"#include <stdio.h>#include <stddef.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <pwd.h>#include <grp.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/socket.h>#include <utmp.h>#include <fcntl.h>#if defined(_PATH_LASTLOG) && defined(__linux__)#include <lastlog.h>#endif#include <netdb.h>#include <netinet/in.h>#include <arpa/inet.h>#ifdef USE_PAM#include <security/pam_appl.h>#endif#ifdef HAS_SHADOW#include <shadow.h>#ifndef PW_PPP#define PW_PPP PW_LOGIN#endif#endif#include <time.h>#include "pppd.h"#include "fsm.h"#include "lcp.h"#include "ccp.h"#include "ecp.h"#include "ipcp.h"#include "upap.h"#include "chap-new.h"#include "eap.h"#ifdef CBCP_SUPPORT#include "cbcp.h"#endif#include "pathnames.h"static const char rcsid[] = RCSID;/* Bits in scan_authfile return value */#define NONWILD_SERVER 1#define NONWILD_CLIENT 2#define ISWILD(word) (word[0] == '*' && word[1] == 0)/* The name by which the peer authenticated itself to us. */char peer_authname[MAXNAMELEN];/* Records which authentication operations haven't completed yet. */static int auth_pending[NUM_PPP];/* Records which authentication operations have been completed. */int auth_done[NUM_PPP];/* Set if we have successfully called plogin() */static int logged_in;/* List of addresses which the peer may use. */static struct permitted_ip *addresses[NUM_PPP];/* Wordlist giving addresses which the peer may use without authenticating itself. */static struct wordlist *noauth_addrs;/* Remote telephone number, if available */char remote_number[MAXNAMELEN];/* Wordlist giving remote telephone numbers which may connect. */static struct wordlist *permitted_numbers;/* Extra options to apply, from the secrets file entry for the peer. */static struct wordlist *extra_options;/* Number of network protocols which we have opened. */static int num_np_open;/* Number of network protocols which have come up. */static int num_np_up;/* Set if we got the contents of passwd[] from the pap-secrets file. */static int passwd_from_file;/* Set if we require authentication only because we have a default route. */static bool default_auth;/* Hook to enable a plugin to control the idle time limit */int (*idle_time_hook) __P((struct ppp_idle *)) = NULL;/* Hook for a plugin to say whether we can possibly authenticate any peer */int (*pap_check_hook) __P((void)) = NULL;/* Hook for a plugin to check the PAP user and password */int (*pap_auth_hook) __P((char *user, char *passwd, char **msgp, struct wordlist **paddrs, struct wordlist **popts)) = NULL;/* Hook for a plugin to know about the PAP user logout */void (*pap_logout_hook) __P((void)) = NULL;/* Hook for a plugin to get the PAP password for authenticating us */int (*pap_passwd_hook) __P((char *user, char *passwd)) = NULL;/* Hook for a plugin to say if we can possibly authenticate a peer using CHAP */int (*chap_check_hook) __P((void)) = NULL;/* Hook for a plugin to get the CHAP password for authenticating us */int (*chap_passwd_hook) __P((char *user, char *passwd)) = NULL;/* Hook for a plugin to say whether it is OK if the peer refuses to authenticate. */int (*null_auth_hook) __P((struct wordlist **paddrs, struct wordlist **popts)) = NULL;int (*allowed_address_hook) __P((u_int32_t addr)) = NULL;/* A notifier for when the peer has authenticated itself, and we are proceeding to the network phase. */struct notifier *auth_up_notifier = NULL;/* A notifier for when the link goes down. */struct notifier *link_down_notifier = NULL;/* * This is used to ensure that we don't start an auth-up/down * script while one is already running. */enum script_state { s_down, s_up};static enum script_state auth_state = s_down;static enum script_state auth_script_state = s_down;static pid_t auth_script_pid = 0;static int used_login; /* peer authenticated against login database *//* * Option variables. */bool uselogin = 0; /* Use /etc/passwd for checking PAP */bool cryptpap = 0; /* Passwords in pap-secrets are encrypted */bool refuse_pap = 0; /* Don't wanna auth. ourselves with PAP */bool refuse_chap = 0; /* Don't wanna auth. ourselves with CHAP */bool refuse_eap = 0; /* Don't wanna auth. ourselves with EAP */#ifdef CHAPMSbool refuse_mschap = 0; /* Don't wanna auth. ourselves with MS-CHAP */bool refuse_mschap_v2 = 0; /* Don't wanna auth. ourselves with MS-CHAPv2 */#elsebool refuse_mschap = 1; /* Don't wanna auth. ourselves with MS-CHAP */bool refuse_mschap_v2 = 1; /* Don't wanna auth. ourselves with MS-CHAPv2 */#endifbool usehostname = 0; /* Use hostname for our_name */bool auth_required = 0; /* Always require authentication from peer */bool allow_any_ip = 0; /* Allow peer to use any IP address */bool explicit_remote = 0; /* User specified explicit remote name */char remote_name[MAXNAMELEN]; /* Peer's name for authentication */static char *uafname; /* name of most recent +ua file */extern char *crypt __P((const char *, const char *));/* Prototypes for procedures local to this file. */static void network_phase __P((int));static void check_idle __P((void *));static void connect_time_expired __P((void *));static int plogin __P((char *, char *, char **));static void plogout __P((void));static int null_login __P((int));static int get_pap_passwd __P((char *));static int have_pap_secret __P((int *));static int have_chap_secret __P((char *, char *, int, int *));static int have_srp_secret __P((char *client, char *server, int need_ip, int *lacks_ipp));static int ip_addr_check __P((u_int32_t, struct permitted_ip *));static int scan_authfile __P((FILE *, char *, char *, char *, struct wordlist **, struct wordlist **, char *, int));static void free_wordlist __P((struct wordlist *));static void auth_script __P((char *));static void auth_script_done __P((void *));static void set_allowed_addrs __P((int, struct wordlist *, struct wordlist *));static int some_ip_ok __P((struct wordlist *));static int setupapfile __P((char **));static int privgroup __P((char **));static int set_noauth_addr __P((char **));static int set_permitted_number __P((char **));static void check_access __P((FILE *, char *));static int wordlist_count __P((struct wordlist *));#ifdef MAXOCTETSstatic void check_maxoctets __P((void *));#endif/* * Authentication-related options. */option_t auth_options[] = { { "auth", o_bool, &auth_required, "Require authentication from peer", OPT_PRIO | 1 }, { "noauth", o_bool, &auth_required, "Don't require peer to authenticate", OPT_PRIOSUB | OPT_PRIV, &allow_any_ip }, { "require-pap", o_bool, &lcp_wantoptions[0].neg_upap, "Require PAP authentication from peer", OPT_PRIOSUB | 1, &auth_required }, { "+pap", o_bool, &lcp_wantoptions[0].neg_upap, "Require PAP authentication from peer", OPT_ALIAS | OPT_PRIOSUB | 1, &auth_required }, { "require-chap", o_bool, &auth_required, "Require CHAP authentication from peer", OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5, &lcp_wantoptions[0].chap_mdtype }, { "+chap", o_bool, &auth_required, "Require CHAP authentication from peer", OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MD5, &lcp_wantoptions[0].chap_mdtype },#ifdef CHAPMS { "require-mschap", o_bool, &auth_required, "Require MS-CHAP authentication from peer", OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT, &lcp_wantoptions[0].chap_mdtype }, { "+mschap", o_bool, &auth_required, "Require MS-CHAP authentication from peer", OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT, &lcp_wantoptions[0].chap_mdtype }, { "require-mschap-v2", o_bool, &auth_required, "Require MS-CHAPv2 authentication from peer", OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2, &lcp_wantoptions[0].chap_mdtype }, { "+mschap-v2", o_bool, &auth_required, "Require MS-CHAPv2 authentication from peer", OPT_ALIAS | OPT_PRIOSUB | OPT_A2OR | MDTYPE_MICROSOFT_V2, &lcp_wantoptions[0].chap_mdtype },#endif { "refuse-pap", o_bool, &refuse_pap, "Don't agree to auth to peer with PAP", 1 }, { "-pap", o_bool, &refuse_pap, "Don't allow PAP authentication with peer", OPT_ALIAS | 1 }, { "refuse-chap", o_bool, &refuse_chap, "Don't agree to auth to peer with CHAP", OPT_A2CLRB | MDTYPE_MD5, &lcp_allowoptions[0].chap_mdtype }, { "-chap", o_bool, &refuse_chap, "Don't allow CHAP authentication with peer", OPT_ALIAS | OPT_A2CLRB | MDTYPE_MD5, &lcp_allowoptions[0].chap_mdtype },#ifdef CHAPMS { "refuse-mschap", o_bool, &refuse_mschap, "Don't agree to auth to peer with MS-CHAP", OPT_A2CLRB | MDTYPE_MICROSOFT, &lcp_allowoptions[0].chap_mdtype }, { "-mschap", o_bool, &refuse_mschap, "Don't allow MS-CHAP authentication with peer", OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT, &lcp_allowoptions[0].chap_mdtype }, { "refuse-mschap-v2", o_bool, &refuse_mschap_v2, "Don't agree to auth to peer with MS-CHAPv2", OPT_A2CLRB | MDTYPE_MICROSOFT_V2, &lcp_allowoptions[0].chap_mdtype }, { "-mschap-v2", o_bool, &refuse_mschap_v2, "Don't allow MS-CHAPv2 authentication with peer", OPT_ALIAS | OPT_A2CLRB | MDTYPE_MICROSOFT_V2, &lcp_allowoptions[0].chap_mdtype },#endif { "require-eap", o_bool, &lcp_wantoptions[0].neg_eap, "Require EAP authentication from peer", OPT_PRIOSUB | 1, &auth_required }, { "refuse-eap", o_bool, &refuse_eap, "Don't agree to authenticate to peer with EAP", 1 }, { "name", o_string, our_name, "Set local name for authentication", OPT_PRIO | OPT_PRIV | OPT_STATIC, NULL, MAXNAMELEN }, /*/{ "+ua", o_special, (void *)setupapfile, "Get PAP user and password from file", OPT_PRIO | OPT_A2STRVAL, &uafname },*/ { "user", o_string, user, "Set name for auth with peer", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN }, { "password", o_string, passwd, "Password for authenticating us to the peer", OPT_PRIO | OPT_STATIC | OPT_HIDE, NULL, MAXSECRETLEN }, { "usehostname", o_bool, &usehostname, "Must use hostname for authentication", 1 }, { "remotename", o_string, remote_name, "Set remote name for authentication", OPT_PRIO | OPT_STATIC, &explicit_remote, MAXNAMELEN }, { "login", o_bool, &uselogin, "Use system password database for PAP", 1 }, { "papcrypt", o_bool, &cryptpap, "PAP passwords are encrypted", 1 }, /*/{ "privgroup", o_special, (void *)privgroup, "Allow group members to use privileged options", OPT_PRIV | OPT_A2LIST }, { "allow-ip", o_special, (void *)set_noauth_addr, "Set IP address(es) which can be used without authentication", OPT_PRIV | OPT_A2LIST },*/ { "remotenumber", o_string, remote_number, "Set remote telephone number for authentication", OPT_PRIO | OPT_STATIC, NULL, MAXNAMELEN }, /*/{ "allow-number", o_special, (void *)set_permitted_number, "Set telephone number(s) which are allowed to connect", OPT_PRIV | OPT_A2LIST },*/ { NULL }};#ifdef INCLUDE/* * setupapfile - specifies UPAP info for authenticating with peer. */static intsetupapfile(argv) char **argv;{ FILE *ufile; int l; uid_t euid; char u[MAXNAMELEN], p[MAXSECRETLEN]; char *fname; lcp_allowoptions[0].neg_upap = 1; /* open user info file */ fname = strdup(*argv); if (fname == NULL) novm("+ua file name"); euid = geteuid(); if (seteuid(getuid()) == -1) { option_error("unable to reset uid before opening %s: %m", fname); return 0; } ufile = fopen(fname, "r"); if (seteuid(euid) == -1) fatal("unable to regain privileges: %m"); if (ufile == NULL) { option_error("unable to open user login data file %s", fname); return 0; } check_access(ufile, fname); uafname = fname; /* get username */ if (fgets(u, MAXNAMELEN - 1, ufile) == NULL || fgets(p, MAXSECRETLEN - 1, ufile) == NULL) { fclose(ufile); option_error("unable to read user login data file %s", fname); return 0; } fclose(ufile); /* get rid of newlines */ l = strlen(u); if (l > 0 && u[l-1] == '\n') u[l-1] = 0; l = strlen(p); if (l > 0 && p[l-1] == '\n') p[l-1] = 0; if (override_value("user", option_priority, fname)) strlcpy(user, u, sizeof(user)); if (override_value("passwd", option_priority, fname)) strlcpy(passwd, p, sizeof(passwd)); return (1);}/* * privgroup - allow members of the group to have privileged access. */static intprivgroup(argv) char **argv;{ struct group *g; int i; g = getgrnam(*argv); if (g == 0) { option_error("group %s is unknown", *argv); return 0; } for (i = 0; i < ngroups; ++i) { if (groups[i] == g->gr_gid) { privileged = 1; break; } } return 1;}/* * set_noauth_addr - set address(es) that can be used without authentication. * Equivalent to specifying an entry like `"" * "" addr' in pap-secrets. */static intset_noauth_addr(argv) char **argv;{ char *addr = *argv; int l = strlen(addr) + 1; struct wordlist *wp; wp = (struct wordlist *) malloc(sizeof(struct wordlist) + l); if (wp == NULL) novm("allow-ip argument"); wp->word = (char *) (wp + 1); wp->next = noauth_addrs; BCOPY(addr, wp->word, l); noauth_addrs = wp; return 1;}/* * set_permitted_number - set remote telephone number(s) that may connect. */static intset_permitted_number(argv)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -