加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgres 之 initdb 源码分析 五

发布时间:2020-12-13 17:28:39 所属栏目:百科 来源:网络整理
导读:2.8 函数check_authmethod_unspecified() 当不指定认证方式时,默认为trust,并在initdb时输出warning信息。 static voidcheck_authmethod_unspecified(const char **authmethod){if (*authmethod == NULL || strlen(*authmethod) == 0){authwarning = _("n

2.8 函数check_authmethod_unspecified()

当不指定认证方式时,默认为trust,并在initdb时输出warning信息。

static void
check_authmethod_unspecified(const char **authmethod)
{
	if (*authmethod == NULL || strlen(*authmethod) == 0)
	{
		authwarning = _("nWARNING: enabling "trust" authentication for local connectionsn"
						"You can change this by editing pg_hba.conf or using the option -A,orn"
			"--auth-local and --auth-host,the next time you run initdb.n");
		*authmethod = "trust";
	}
}

2.9 函数check_authmethod_valid

检查local (SOCK通信)和host(TCP/IP通信)方式下,认证方式是否为指定的几个之内

static const char *auth_methods_local[] = {"trust","reject","md5","password","peer","radius",

static void
check_authmethod_valid(const char *authmethod,const char **valid_methods,const char *conntype)
{
	const char **p;

	for (p = valid_methods; *p; p++)
	{
		if (strcmp(authmethod,*p) == 0)
			return;
		/* with space = param */
		if (strchr(authmethod,' '))
			if (strncmp(authmethod,*p,(authmethod - strchr(authmethod,' '))) == 0)
				return;
	}

	fprintf(stderr,_("%s: invalid authentication method "%s" for "%s" connectionsn"),progname,authmethod,conntype);
	exit(1);
}


2.10 函数check_need_password

在initdb时,如果指定-A 为md5 或password ,如果不指定-W 则报错
[wln@localhost bin]$ ./initdb -A md5 -D ./data
initdb: must specify a password for the superuser to enable md5 authentication
static void
check_need_password(const char *authmethodlocal,const char *authmethodhost)
{
	if ((strcmp(authmethodlocal,"md5") == 0 ||
		 strcmp(authmethodlocal,"password") == 0) &&
		(strcmp(authmethodhost,"md5") == 0 ||
		 strcmp(authmethodhost,"password") == 0) &&
		!(pwprompt || pwfilename))
	{
		fprintf(stderr,_("%s: must specify a password for the superuser to enable %s authenticationn"),(strcmp(authmethodlocal,"md5") == 0 ||
				 strcmp(authmethodlocal,"password") == 0)
				? authmethodlocal
				: authmethodhost);
		exit(1);
	}
}

2.11 函数get_restricted_token(void)

由于针对WIN32 ,忽略

2.12 函数 setup_bin_paths()

查找initdb所需要的postgres可执行文件及其附属文件

void
setup_bin_paths(const char *argv0)
{
	int			ret;

	if ((ret = find_other_exec(argv0,"postgres",PG_BACKEND_VERSIONSTR,backend_exec)) < 0)
	{
		char		full_path[MAXPGPATH];

		if (find_my_exec(argv0,full_path) < 0)
			strlcpy(full_path,sizeof(full_path));

		if (ret == -1)
			fprintf(stderr,_("The program "postgres" is needed by %s "
					  "but was not found in then"
					  "same directory as "%s".n"
					  "Check your installation.n"),full_path);
		else
			fprintf(stderr,_("The program "postgres" was found by "%s"n"
					  "but was not the same version as %s.n"
					  "Check your installation.n"),full_path,progname);
		exit(1);
	}

	/* store binary directory */
	strcpy(bin_path,backend_exec);
	*last_dir_separator(bin_path) = '';
	canonicalize_path(bin_path);

	if (!share_path)
	{
		share_path = pg_malloc(MAXPGPATH);
		get_share_path(backend_exec,share_path);
	}
	else if (!is_absolute_path(share_path))
	{
		fprintf(stderr,_("%s: input file location must be an absolute pathn"),progname);
		exit(1);
	}

	canonicalize_path(share_path);
}


2.13 函数 get_id()

得到执行initdb程序的用户名

/*
 * find the current user
 *
 * on unix make sure it isn't really root
 */
static char *
get_id(void)
{
#ifndef WIN32

	struct passwd *pw;

	if (geteuid() == 0)			/* 0 is root's uid */
	{
		fprintf(stderr,_("%s: cannot be run as rootn"
				  "Please log in (using,e.g.,"su") as the "
				  "(unprivileged) user that willn"
				  "own the server process.n"),progname);
		exit(1);
	}

	pw = getpwuid(geteuid());
	if (!pw)
	{
		fprintf(stderr,_("%s: could not obtain information about current user: %sn"),strerror(errno));
		exit(1);
	}
#else							/* the windows code */

	struct passwd_win32
	{
		int			pw_uid;
		char		pw_name[128];
	}			pass_win32;
	struct passwd_win32 *pw = &pass_win32;
	DWORD		pwname_size = sizeof(pass_win32.pw_name) - 1;

	pw->pw_uid = 1;
	if (!GetUserName(pw->pw_name,&pwname_size))
	{
		fprintf(stderr,_("%s: could not get current user name: %sn"),strerror(errno));
		exit(1);
	}
#endif

	return pg_strdup(pw->pw_name);
}

2.13.1 此时得到initdb输出的第一行信息

printf(_("The files belonging to this database system will be owned "
"by user "%s".n"
"This user must also own the server process.nn"),
effective_user);

[wln@localhost bin]$ ./initdb -D ./data 
The files belonging to this database system will be owned by user "wln".
This user must also own the server process.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读