Blame os/linux.c

c95639
c95639
/* mlogin: minimalistic login                */
c95639
/* Copyright (c) 2019  Ørjan Malde           */
c95639
/* Released under LGPL, see COPYRIGHT.MLOGIN */
c95639
c95639
c95639
c95639
int switch_user_context(struct passwd* pwd, const char* username)
c95639
{
c95639
	if(!getuid()) {
c95639
		if(initgroups(username, pwd->pw_gid) == -1) {
c95639
			printf("initgroups failed: %s", strerror(errno));
c95639
			exit(1);
c95639
		}
c95639
	}
c95639
c95639
        /* is this safe? not sure. */
c95639
	if(setgid(pwd->pw_gid))
c95639
		return false;
c95639
	if(setuid(pwd->pw_uid))
c95639
		return 1;
c95639
	return 0;
c95639
}