21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The group structure that is returned by either getgrnam( ) or getgrgid( ) contains a<br />

field called gr_mem that is an array of strings containing the names of all the member<br />

users. The last element in the array will always be a NULL pointer. Determining<br />

whether a user is a member of a group is a simple matter of iterating over the elements<br />

in the array, comparing each one to the name of the user for which to look:<br />

#include <br />

#include <br />

#include <br />

int spc_group_ismember(char *group_name, char *user_name) {<br />

int i;<br />

struct group *gr;<br />

if (!(gr = getgrnam(group_name))) {<br />

endgrent( );<br />

return 0;<br />

}<br />

for (i = 0; gr->gr_mem[i]; i++)<br />

if (!strcmp(user_name, gr->gr_mem[i])) {<br />

endgrent( );<br />

return 1;<br />

}<br />

endgrent( );<br />

return 0;<br />

}<br />

8.3 Getting User and Group Information on<br />

Windows<br />

<strong>Problem</strong><br />

You need to discover information about a user or group, and you have a username or<br />

user ID or a group name or ID.<br />

Solution<br />

Windows identifies users and groups using security identifiers (SIDs), which are<br />

unique, variably sized values assigned by an authority such as the local machine or a<br />

Windows NT server domain. Functions and data structures typically represent users<br />

and groups using SIDs, rather than using names.<br />

The Win32 API provides numerous functions for manipulating SIDs, but of particular<br />

interest to us in this recipe are the functions LookupAccountName( ) and<br />

LookupAccountSid( ), which are used to map between names and SIDs.<br />

Getting User and Group Information on Windows | 375<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!