SystemUser

Offers interface for accessing current system user and authentication of users.

On Linux the implementation uses PAM so the authentication is not restricted to a local password file instead it can also be configured to use LDAP.

Summary
SystemUserOffers interface for accessing current system user and authentication of users.
CopyrightThis program is free software.
Files
C-kern/api/platform/sysuser.hHeader file SystemUser.
C-kern/platform/Linux/sysuser.cImplementation file SystemUser Linuximpl.
Types
struct sysuser_tExport sysuser_t into global namespace.
struct sysuser_id_tMake sysuser_id_t an alias of sys_userid_t.
struct sysuser_info_tExport sysuser_info_t into global namespace.
Functions
test
unittest_platform_sysuserTest sysuser_t functionality.
sysuser_id_tThis type is an alias of sys_userid_t.
lifetime
sysuser_id_FREEStatic initializer.
query
isadmin_sysuseridReturns true if this user is administrator (== root).
isequal_sysuseridCompares two sysuser_id_t for equality.
sysuser_tOffers functionality to manage system user.
currentContains the current user the process uses.
realuserContains user which started the process.
privilegeduserContains privileged user which is set at process creation from the system.
lifetime
sysuser_FREEStatic initializer.
init_sysuserInitializes system user of process at process start.
free_sysuserClears sysusr and resets system user ids.
query
isequal_sysuserReturns true if lsysusr equals rsysusr.
current_sysuserReturns the current active system user.
real_sysuserReturns sysuser_id_t of the user which started the process.
privileged_sysuserReturns sysuser_id_t of the user which has other privileges.
switch
switchtoprivilege_sysuserSwitches current user to privileged_sysuser.
switchtoreal_sysuserSwitches current user to real_sysuser.
set
setusers_sysuserChanges realuser and privileged user.
authentication
authenticate_sysuserChecks that a certain user / password combination is valid.
sysuser_info_tStores information about a sysuser_t.
sizeSize in bytes of allocated memory this structure uses.
lifetime
new_sysuserinfoReturns information about sysuser_id_t given as parameter.
delete_sysuserinfoFrees memory holding system user information.
query
name_sysuserinfoReturns user name stored in usrinfo.
inline implementation
sysuser_t
current_sysuserImplement sysuser_t.current_sysuser.
real_sysuserImplement sysuser_t.real_sysuser.
privileged_sysuserImplement sysuser_t.privileged_sysuser.
init_sysuserImplement sysuser_t.init_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)
free_sysuserImplement sysuser_t.free_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)
sysuser_info_t
name_sysuserinfoImplements sysuser_info_t.name_sysuserinfo.

Copyright

This program is free software.  You can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

Author

© 2012 Jörg Seebohn

Files

C-kern/api/platform/sysuser.h

Header file SystemUser.

C-kern/platform/Linux/sysuser.c

Implementation file SystemUser Linuximpl.

Types

struct sysuser_t

typedef struct sysuser_t sysuser_t

Export sysuser_t into global namespace.

struct sysuser_id_t

Make sysuser_id_t an alias of sys_userid_t.

struct sysuser_info_t

typedef struct sysuser_info_t sysuser_info_t

Export sysuser_info_t into global namespace.

Functions

Summary

test

unittest_platform_sysuser

int unittest_platform_sysuser(void)

Test sysuser_t functionality.

Configuration

The unit test uses username=”guest” and password=”GUEST” to test <authenticate_sysuser> for success.  Configure your system either with this test account or adapt the unit test with a valid username/password combination.  Also this test works only if unittest_platform_sysuser is started as user “root” or as “guest”.

sysuser_id_t

typedef sys_userid_t sysuser_id_t

This type is an alias of sys_userid_t.

Summary
lifetime
sysuser_id_FREEStatic initializer.
query
isadmin_sysuseridReturns true if this user is administrator (== root).
isequal_sysuseridCompares two sysuser_id_t for equality.

lifetime

sysuser_id_FREE

#define sysuser_id_FREE sys_userid_FREE

Static initializer.

query

isadmin_sysuserid

bool isadmin_sysuserid(sysuser_id_t uid)

Returns true if this user is administrator (== root).

isequal_sysuserid

bool isequal_sysuserid(sysuser_id_t luid,
sysuser_id_t ruid)

Compares two sysuser_id_t for equality.

sysuser_t

struct sysuser_t

Offers functionality to manage system user.

The current implementation supports the idea (see <initonce_sysuser>) of two users.

The user returned from privileged_sysuser has higher or special privileges so the process can accomplish system administration tasks for which the real user has not enough rights.

Summary
currentContains the current user the process uses.
realuserContains user which started the process.
privilegeduserContains privileged user which is set at process creation from the system.
lifetime
sysuser_FREEStatic initializer.
init_sysuserInitializes system user of process at process start.
free_sysuserClears sysusr and resets system user ids.
query
isequal_sysuserReturns true if lsysusr equals rsysusr.
current_sysuserReturns the current active system user.
real_sysuserReturns sysuser_id_t of the user which started the process.
privileged_sysuserReturns sysuser_id_t of the user which has other privileges.
switch
switchtoprivilege_sysuserSwitches current user to privileged_sysuser.
switchtoreal_sysuserSwitches current user to real_sysuser.
set
setusers_sysuserChanges realuser and privileged user.
authentication
authenticate_sysuserChecks that a certain user / password combination is valid.

current

sysuser_id_t current

Contains the current user the process uses.  It is either set to the value of realuser or privilegeduser.

realuser

sysuser_id_t realuser

Contains user which started the process.

privilegeduser

sysuser_id_t privilegeduser

Contains privileged user which is set at process creation from the system.

lifetime

sysuser_FREE

#define sysuser_FREE { sysuser_id_FREE, sysuser_id_FREE, sysuser_id_FREE }

Static initializer.  Sets user to invalid value.

init_sysuser

int init_sysuser(/*out*/sysuser_t *sysusr)

Initializes system user of process at process start.

Posix (Linux)

On Posix like system a process can have an effective user id which is different from the real user id which started the process.  The effectice user id is set from the system to the owner of the program file if the setuid bit is set.

This function sets the effective user id to the real user id but remembers it.  To get privileged rights call switchtoprivilege_sysuser.

free_sysuser

int free_sysuser(sysuser_t *sysusr)

Clears sysusr and resets system user ids.  The system user ids are set to the values before init_sysuser was called.

query

isequal_sysuser

bool isequal_sysuser(const sysuser_t *lsysusr,
const sysuser_t *rsysusr)

Returns true if lsysusr equals rsysusr.

current_sysuser

sysuser_id_t current_sysuser(sysuser_t *sysusr)

Returns the current active system user.

real_sysuser

sysuser_id_t real_sysuser(sysuser_t *sysusr)

Returns sysuser_id_t of the user which started the process.

privileged_sysuser

sysuser_id_t privileged_sysuser(sysuser_t *sysusr)

Returns sysuser_id_t of the user which has other privileges.  If this user is equal to real_sysuser the process has no special privileges.  A privileged user is not necessarily an administrator but it can.

switch

switchtoprivilege_sysuser

int switchtoprivilege_sysuser(sysuser_t *sysusr)

Switches current user to privileged_sysuser.  See current_sysuser.

switchtoreal_sysuser

int switchtoreal_sysuser(sysuser_t *sysusr)

Switches current user to real_sysuser.  See current_sysuser.

set

setusers_sysuser

int setusers_sysuser(sysuser_t *sysusr,
sysuser_id_t realuser,
sysuser_id_t privilegeduser)

Changes realuser and privileged user.  See real_sysuser and privileged_sysuser and current_sysuser.  If you set privilegeduser to the same value as realuser you will give up your privileges.  The current user is switch to the real user.  If you want to change the user ids to arbitrary values other than realuser or privilegeduser this call will only work if current_sysuser is admin.

authentication

authenticate_sysuser

int authenticate_sysuser(const char *username,
const char *password)

Checks that a certain user / password combination is valid.

Return codes

0Success
EACCESUser unknown or password wrong.
ENOMEMBuffer allocation failed.
ERANGENumber of tries of wrong authentication reqeusts reached.
EPERMNot enough rights to authenticate user.
ENOTRECOVERABLECaller should exit process after this value has been returned.
ELIBACCShared system library not found.

Some underlying implementations are returns EACCES in case of EPERM or other error codes.

Uses authentication service of the operating system to authenticate system users.  This means loading shated libraries into the running process.  It is best to shield the caller by spawing a child process which does the authentication (see process_t).  Only username / password combinations can be authenticated which are known to the underlying operating system.  The user of the running process is not changed.  To authenticate other users than the calling user the running process needs to have spedial rights.  On Linux it should be running as root.

Linux specific

The configuration file for PAM is stored in /etc/pam.d/service_name alternatively in /etc/pam.conf.  The fallback to /etc/pam.d/other is made if no service with this name exists.  Currently the service name “passwd” (see sysuser_SYS_SERVICE_NAME) is used therefore the configuration file /etc/pam.d/passwd is used which is the configuration of the passwd command to change the own password.  This configuration should always exist.

sysuser_info_t

struct sysuser_info_t

Stores information about a sysuser_t.

Summary
sizeSize in bytes of allocated memory this structure uses.
lifetime
new_sysuserinfoReturns information about sysuser_id_t given as parameter.
delete_sysuserinfoFrees memory holding system user information.
query
name_sysuserinfoReturns user name stored in usrinfo.

size

size_t size

Size in bytes of allocated memory this structure uses.

lifetime

new_sysuserinfo

int new_sysuserinfo(/*out*/sysuser_info_t **usrinfo,
sysuser_id_t uid)

Returns information about sysuser_id_t given as parameter.  The system database is searched for an entry.

If no one exists ENOENT is returned and no error log is written in this case.

delete_sysuserinfo

int delete_sysuserinfo(sysuser_info_t **usrinfo)

Frees memory holding system user information.

query

name_sysuserinfo

const char * name_sysuserinfo(sysuser_info_t *usrinfo)

Returns user name stored in usrinfo.

inline implementation

sysuser_t

current_sysuser

#define current_sysuser(sysusr) ((sysusr)->current)

Implement sysuser_t.current_sysuser.

real_sysuser

#define real_sysuser(sysusr) ((sysusr)->realuser)

Implement sysuser_t.real_sysuser.

privileged_sysuser

#define privileged_sysuser(sysusr) ((sysusr)->privilegeduser)

Implement sysuser_t.privileged_sysuser.

init_sysuser

#define init_sysuser(sysuser) ((*(sysuser)) = (sysuser_t) sysuser_FREE, 0)

Implement sysuser_t.init_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)

free_sysuser

#define free_sysuser(sysuser) ((*(sysuser)) = (sysuser_t) sysuser_FREE, 0)

Implement sysuser_t.free_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)

sysuser_info_t

name_sysuserinfo

#define name_sysuserinfo(usrinfo) ((usrinfo)->name)

Implements sysuser_info_t.name_sysuserinfo.

Offers interface for accessing current system user and authentication of users.
Implements SystemUser.
typedef struct sysuser_t sysuser_t
Export sysuser_t into global namespace.
struct sysuser_t
Offers functionality to manage system user.
typedef sys_userid_t sysuser_id_t
This type is an alias of sys_userid_t.
#define sys_userid_t uid_t
Chooses Posix system user id.
typedef struct sysuser_info_t sysuser_info_t
Export sysuser_info_t into global namespace.
struct sysuser_info_t
Stores information about a sysuser_t.
int unittest_platform_sysuser(void)
Test sysuser_t functionality.
#define sysuser_id_FREE sys_userid_FREE
Static initializer.
bool isadmin_sysuserid(sysuser_id_t uid)
Returns true if this user is administrator (== root).
bool isequal_sysuserid(sysuser_id_t luid,
sysuser_id_t ruid)
Compares two sysuser_id_t for equality.
sysuser_id_t current
Contains the current user the process uses.
sysuser_id_t realuser
Contains user which started the process.
sysuser_id_t privilegeduser
Contains privileged user which is set at process creation from the system.
#define sysuser_FREE { sysuser_id_FREE, sysuser_id_FREE, sysuser_id_FREE }
Static initializer.
int init_sysuser(/*out*/sysuser_t *sysusr)
Initializes system user of process at process start.
int free_sysuser(sysuser_t *sysusr)
Clears sysusr and resets system user ids.
bool isequal_sysuser(const sysuser_t *lsysusr,
const sysuser_t *rsysusr)
Returns true if lsysusr equals rsysusr.
sysuser_id_t current_sysuser(sysuser_t *sysusr)
Returns the current active system user.
sysuser_id_t real_sysuser(sysuser_t *sysusr)
Returns sysuser_id_t of the user which started the process.
sysuser_id_t privileged_sysuser(sysuser_t *sysusr)
Returns sysuser_id_t of the user which has other privileges.
int switchtoprivilege_sysuser(sysuser_t *sysusr)
Switches current user to privileged_sysuser.
int switchtoreal_sysuser(sysuser_t *sysusr)
Switches current user to real_sysuser.
int setusers_sysuser(sysuser_t *sysusr,
sysuser_id_t realuser,
sysuser_id_t privilegeduser)
Changes realuser and privileged user.
int authenticate_sysuser(const char *username,
const char *password)
Checks that a certain user / password combination is valid.
size_t size
Size in bytes of allocated memory this structure uses.
int new_sysuserinfo(/*out*/sysuser_info_t **usrinfo,
sysuser_id_t uid)
Returns information about sysuser_id_t given as parameter.
int delete_sysuserinfo(sysuser_info_t **usrinfo)
Frees memory holding system user information.
const char * name_sysuserinfo(sysuser_info_t *usrinfo)
Returns user name stored in usrinfo.
#define current_sysuser(sysusr) ((sysusr)->current)
Implement sysuser_t.current_sysuser.
#define real_sysuser(sysusr) ((sysusr)->realuser)
Implement sysuser_t.real_sysuser.
#define privileged_sysuser(sysusr) ((sysusr)->privilegeduser)
Implement sysuser_t.privileged_sysuser.
#define init_sysuser(sysuser) ((*(sysuser)) = (sysuser_t) sysuser_FREE, 0)
Implement sysuser_t.init_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)
#define free_sysuser(sysuser) ((*(sysuser)) = (sysuser_t) sysuser_FREE, 0)
Implement sysuser_t.free_sysuser as noop if !defined(KONFIG_SUBSYS_SYSUSER)
#define name_sysuserinfo(usrinfo) ((usrinfo)->name)
Implements sysuser_info_t.name_sysuserinfo.
#define sysuser_SYS_SERVICE_NAME "passwd"
The name of service used during authentication.
Close