user |
Each user account has a unique
identifier with associated information, such as a name and a domain. This
information is usually stored in an operating system database, such as /etc/passwd
or the Network Information Service (NIS) in UNIX, or the Security Account
Manager (SAM) in Windows NT. The os_user class
provides a portable method for accessing and manipulating this information.
You can create os_user
objects from either an account name or an operating system-specific identifier.
Once these objects are constructed, you can query information about the user
such as domain and group values.
The following example demonstrates
the os_user class on a UNIX system.
#include <iostream>
#include <string>
#include <ospace/security.h>
void
main()
{
os_security_toolkit initialize;
// Construct user object to reference the user with name "root".
os_user user1( string( "root" ) );
// Construct user object to reference the user with UID 100.
os_user user2( 100 );
cout << "user1 = " << user1 << end1;
cout << "user2 = " << user2 << end1;
cout << user1 << " == " << user2 << " = ";
cout << (user1 == user2) << endl;
cout << user1 << " == " << user1 << " = ";
cout << (user1 == user1) << endl;
}
user1 = os_user( root, 0 )
user2 = os_user( fred, 100 )
os_user( root, 0 ) == os_user( fred, 100 ) = 0
os_user( root, 0 ) == os_user( root, 0 ) = 1
The following UNIX-specific
example displays all groups to which the user "fred" belongs. This
example also invokes the static function users()
to obtain a list of all users in the system.
#include <iostream>
#include <ospace/security.h>
void
main()
{
os_security_toolkit initialize;
os_user user( "fred" );
vector< gid_t > groups = user.groups();
cout << user << " belongs to the following groups: " << endl;
vector< gid_t >::iterator i;
for ( i = groups.begin(); i != groups.end(); i++ )
cout << " " << os_group( *i ) << endl;
cout << endl;
vector< uid_t > users = os_user::users();
cout << "List of all the users in the user database:" << endl;
vector< uid_t >::iterator j;
for ( j = users.begin(); j != users.end(); j++ )
cout << " " << os_user( *j ) << endl;
}
os_user( fred, 100 ) belongs to the following groups:
os_group( other, 20 )
os_group( cavemen, 21 )
List of all the users in the user database:
os_user( root, 0 )
os_user( sys, 2 )
os_user( fred, 100 )
The next example shows how account names are case-insensitive on the Windows NT platform. On most UNIX platforms, user names are case-sensitive.
#include <iostream>
#include <ospace/security.h>
void
main()
{
os_security_toolkit initialize;
os_user user1( "administrator" );
os_user user2( "Administrator" );
cout
<< "User names are case-insensitive under Windows NT:"
<< endl << '\t'
<< "os_user( \"administrator\" ) == os_user( \"Administrator\" ) = "
<< ( user1 == user2 ) << endl;
}
User names are case-insensitive under Windows NT:
os_user( "administrator" ) == os_user( "Administrator" ) = 1
Copyright©1994-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.