fe-auth.c

来自「PostgreSQL7.4.6 for Linux」· C语言 代码 · 共 764 行 · 第 1/2 页

C
764
字号
	/*	 * libpq uses a non-blocking socket. But kerberos needs a blocking	 * socket, and we have to block somehow to do mutual authentication	 * anyway. So we temporarily make it blocking.	 */	flags = fcntl(sock, F_GETFL);	if (flags < 0 || fcntl(sock, F_SETFL, (long) (flags & ~O_NONBLOCK)))	{		char		sebuf[256];		snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 libpq_gettext("could not set socket to blocking mode: %s\n"), pqStrerror(errno, sebuf, sizeof(sebuf)));		krb5_free_principal(pg_krb5_context, server);		return STATUS_ERROR;	}	retval = krb5_sendauth(pg_krb5_context, &auth_context,						   (krb5_pointer) & sock, PG_KRB_SRVNAM,						   pg_krb5_client, server,						   AP_OPTS_MUTUAL_REQUIRED,						   NULL, 0,		/* no creds, use ccache instead */						   pg_krb5_ccache, &err_ret, NULL, NULL);	if (retval)	{		if (retval == KRB5_SENDAUTH_REJECTED && err_ret)		{#if defined(HAVE_KRB5_ERROR_TEXT_DATA)			snprintf(PQerrormsg, PQERRORMSG_LENGTH,			  libpq_gettext("Kerberos 5 authentication rejected: %*s\n"),					 (int) err_ret->text.length, err_ret->text.data);#elif defined(HAVE_KRB5_ERROR_E_DATA)			snprintf(PQerrormsg, PQERRORMSG_LENGTH,			  libpq_gettext("Kerberos 5 authentication rejected: %*s\n"),					 (int) err_ret->e_data->length,					 (const char *) err_ret->e_data->data);#else#error "bogus configuration"#endif		}		else		{			snprintf(PQerrormsg, PQERRORMSG_LENGTH,					 "krb5_sendauth: %s\n", error_message(retval));		}		if (err_ret)			krb5_free_error(pg_krb5_context, err_ret);		ret = STATUS_ERROR;	}	krb5_free_principal(pg_krb5_context, server);	if (fcntl(sock, F_SETFL, (long) flags))	{		char		sebuf[256];		snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 libpq_gettext("could not restore non-blocking mode on socket: %s\n"),				 pqStrerror(errno, sebuf, sizeof(sebuf)));		ret = STATUS_ERROR;	}	return ret;}#endif   /* KRB5 *//* * Respond to AUTH_REQ_SCM_CREDS challenge. * * Note: current backends will not use this challenge if HAVE_GETPEEREID * or SO_PEERCRED is defined, but pre-7.4 backends might, so compile the * code anyway. */static intpg_local_sendauth(char *PQerrormsg, PGconn *conn){#if defined(HAVE_STRUCT_CMSGCRED) || defined(HAVE_STRUCT_FCRED) || \	(defined(HAVE_STRUCT_SOCKCRED) && defined(LOCAL_CREDS))	char		buf;	struct iovec iov;	struct msghdr msg;#ifdef HAVE_STRUCT_CMSGCRED	/* Prevent padding */	char		cmsgmem[sizeof(struct cmsghdr) + sizeof(struct cmsgcred)];	/* Point to start of first structure */	struct cmsghdr *cmsg = (struct cmsghdr *) cmsgmem;#endif	/*	 * The backend doesn't care what we send here, but it wants exactly	 * one character to force recvmsg() to block and wait for us.	 */	buf = '\0';	iov.iov_base = &buf;	iov.iov_len = 1;	memset(&msg, 0, sizeof(msg));	msg.msg_iov = &iov;	msg.msg_iovlen = 1;#ifdef HAVE_STRUCT_CMSGCRED	/* Create control header, FreeBSD */	msg.msg_control = cmsg;	msg.msg_controllen = sizeof(cmsgmem);	memset(cmsg, 0, sizeof(cmsgmem));	cmsg->cmsg_len = sizeof(cmsgmem);	cmsg->cmsg_level = SOL_SOCKET;	cmsg->cmsg_type = SCM_CREDS;#endif	if (sendmsg(conn->sock, &msg, 0) == -1)	{		char		sebuf[256];		snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 "pg_local_sendauth: sendmsg: %s\n",				 pqStrerror(errno, sebuf, sizeof(sebuf)));		return STATUS_ERROR;	}	return STATUS_OK;#else	snprintf(PQerrormsg, PQERRORMSG_LENGTH,		libpq_gettext("SCM_CRED authentication method not supported\n"));	return STATUS_ERROR;#endif}static intpg_password_sendauth(PGconn *conn, const char *password, AuthRequest areq){	int			ret;	char	   *crypt_pwd;	/* Encrypt the password if needed. */	switch (areq)	{		case AUTH_REQ_MD5:			{				char	   *crypt_pwd2;				if (!(crypt_pwd = malloc(MD5_PASSWD_LEN + 1)) ||					!(crypt_pwd2 = malloc(MD5_PASSWD_LEN + 1)))				{					perror("malloc");					return STATUS_ERROR;				}				if (!EncryptMD5(password, conn->pguser,								strlen(conn->pguser), crypt_pwd2))				{					free(crypt_pwd);					free(crypt_pwd2);					return STATUS_ERROR;				}				if (!EncryptMD5(crypt_pwd2 + strlen("md5"), conn->md5Salt,								sizeof(conn->md5Salt), crypt_pwd))				{					free(crypt_pwd);					free(crypt_pwd2);					return STATUS_ERROR;				}				free(crypt_pwd2);				break;			}		case AUTH_REQ_CRYPT:			{				char		salt[3];				StrNCpy(salt, conn->cryptSalt, 3);				crypt_pwd = crypt(password, salt);				break;			}		case AUTH_REQ_PASSWORD:			/* discard const so we can assign it */			crypt_pwd = (char *) password;			break;		default:			return STATUS_ERROR;	}	/* Packet has a message type as of protocol 3.0 */	if (PG_PROTOCOL_MAJOR(conn->pversion) >= 3)		ret = pqPacketSend(conn, 'p', crypt_pwd, strlen(crypt_pwd) + 1);	else		ret = pqPacketSend(conn, 0, crypt_pwd, strlen(crypt_pwd) + 1);	if (areq == AUTH_REQ_MD5)		free(crypt_pwd);	return ret;}/* * fe_sendauth -- client demux routine for outgoing authentication information */intfe_sendauth(AuthRequest areq, PGconn *conn, const char *hostname,			const char *password, char *PQerrormsg){#if !defined(KRB4) && !defined(KRB5)	(void) hostname;			/* not used */#endif	switch (areq)	{		case AUTH_REQ_OK:			break;		case AUTH_REQ_KRB4:#ifdef KRB4			if (pg_krb4_sendauth(PQerrormsg, conn->sock,							   (struct sockaddr_in *) & conn->laddr.addr,							   (struct sockaddr_in *) & conn->raddr.addr,								 hostname) != STATUS_OK)			{				snprintf(PQerrormsg, PQERRORMSG_LENGTH,					libpq_gettext("Kerberos 4 authentication failed\n"));				return STATUS_ERROR;			}			break;#else			snprintf(PQerrormsg, PQERRORMSG_LENGTH,			 libpq_gettext("Kerberos 4 authentication not supported\n"));			return STATUS_ERROR;#endif		case AUTH_REQ_KRB5:#ifdef KRB5			if (pg_krb5_sendauth(PQerrormsg, conn->sock,								 hostname) != STATUS_OK)			{				snprintf(PQerrormsg, PQERRORMSG_LENGTH,					libpq_gettext("Kerberos 5 authentication failed\n"));				return STATUS_ERROR;			}			break;#else			snprintf(PQerrormsg, PQERRORMSG_LENGTH,			 libpq_gettext("Kerberos 5 authentication not supported\n"));			return STATUS_ERROR;#endif		case AUTH_REQ_MD5:		case AUTH_REQ_CRYPT:		case AUTH_REQ_PASSWORD:			if (password == NULL || *password == '\0')			{				(void) snprintf(PQerrormsg, PQERRORMSG_LENGTH,								"fe_sendauth: no password supplied\n");				return STATUS_ERROR;			}			if (pg_password_sendauth(conn, password, areq) != STATUS_OK)			{				(void) snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 "fe_sendauth: error sending password authentication\n");				return STATUS_ERROR;			}			break;		case AUTH_REQ_SCM_CREDS:			if (pg_local_sendauth(PQerrormsg, conn) != STATUS_OK)				return STATUS_ERROR;			break;		default:			snprintf(PQerrormsg, PQERRORMSG_LENGTH,					 libpq_gettext("authentication method %u not supported\n"), areq);			return STATUS_ERROR;	}	return STATUS_OK;}/* * fe_setauthsvc * fe_getauthsvc * * Set/return the authentication service currently selected for use by the * frontend. (You can only use one in the frontend, obviously.) * * NB: This is not thread-safe if different threads try to select different * authentication services!  It's OK for fe_getauthsvc to select the default, * since that will be the same for all threads, but direct application use * of fe_setauthsvc is not thread-safe.  However, use of fe_setauthsvc is * deprecated anyway... */static int	pg_authsvc = -1;voidfe_setauthsvc(const char *name, char *PQerrormsg){	int			i;	for (i = 0; i < n_authsvcs; ++i)		if (strcmp(name, authsvcs[i].name) == 0)		{			pg_authsvc = i;			break;		}	if (i == n_authsvcs)	{		snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 libpq_gettext("invalid authentication service name \"%s\", ignored\n"),				 name);	}	return;}MsgTypefe_getauthsvc(char *PQerrormsg){	if (pg_authsvc < 0 || pg_authsvc >= n_authsvcs)	{		fe_setauthsvc(DEFAULT_CLIENT_AUTHSVC, PQerrormsg);		if (pg_authsvc < 0 || pg_authsvc >= n_authsvcs)		{			/* Can only get here if DEFAULT_CLIENT_AUTHSVC is misdefined */			return 0;		}	}	return authsvcs[pg_authsvc].msgtype;}/* * fe_getauthname -- returns a pointer to dynamic space containing whatever *					 name the user has authenticated to the system * if there is an error, return the error message in PQerrormsg */char *fe_getauthname(char *PQerrormsg){	const char *name = (char *) NULL;	char	   *authn = (char *) NULL;	MsgType		authsvc;	authsvc = fe_getauthsvc(PQerrormsg);	/* this just guards against broken DEFAULT_CLIENT_AUTHSVC, see above */	if (authsvc == 0)		return NULL;			/* leave original error message in place */#ifdef KRB4	if (authsvc == STARTUP_KRB4_MSG)		name = pg_krb4_authname(PQerrormsg);#endif#ifdef KRB5	if (authsvc == STARTUP_KRB5_MSG)		name = pg_krb5_authname(PQerrormsg);#endif	if (authsvc == STARTUP_MSG		|| (authsvc == STARTUP_KRB4_MSG && !name)		|| (authsvc == STARTUP_KRB5_MSG && !name))	{#ifdef WIN32		char		username[128];		DWORD		namesize = sizeof(username) - 1;		if (GetUserName(username, &namesize))			name = username;#else		char		pwdbuf[BUFSIZ];		struct passwd pwdstr;		struct passwd *pw = NULL;		if (pqGetpwuid(geteuid(), &pwdstr,					   pwdbuf, sizeof(pwdbuf), &pw) == 0)			name = pw->pw_name;#endif	}	if (authsvc != STARTUP_MSG && authsvc != STARTUP_KRB4_MSG && authsvc != STARTUP_KRB5_MSG)		snprintf(PQerrormsg, PQERRORMSG_LENGTH,				 libpq_gettext("fe_getauthname: invalid authentication system: %d\n"),				 authsvc);	if (name && (authn = (char *) malloc(strlen(name) + 1)))		strcpy(authn, name);	return authn;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?