21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In our example code, we call endpwent( ) after every lookup operation, but this isn’t<br />

necessary if you need to perform multiple lookups. In fact, if you know you will be<br />

performing a large number of lookups, always calling endpwent( ) after each one is<br />

wasteful. Any number of lookup operations may be performed safely before eventually<br />

calling endpwent( ).<br />

Looking up group information is similar to looking up user information. The header<br />

file grp.h contains the declarations for the needed functions and data types. Two<br />

functions similar to getpwnam( ) and getpwuid( ) also exist for groups:<br />

#include <br />

#include <br />

struct group *getgrgid(gid_t gid);<br />

struct group *getgrnam(const char *name);<br />

These two functions behave as their user counterparts do. Thus, we can use them to<br />

perform name-to-numeric-identifier mappings, and vice versa. Just as user information<br />

lookups require a call to endpwent( ) to clean up any resources allocated during<br />

the lookup, group information lookups require a call to endgrent( ) to do the same.<br />

#include <br />

#include <br />

#include <br />

int spc_group_getname(gid_t gid, char **name) {<br />

struct group *gr;<br />

if (!(gr = getgruid(gid)) ) {<br />

endgrent( );<br />

return -1;<br />

}<br />

*name = strdup(gr->gr_name);<br />

endgrent( );<br />

return 0;<br />

}<br />

int spc_group_getgid(char *name, gid_t *gid) {<br />

struct group *gr;<br />

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

endgrent( );<br />

return -1;<br />

}<br />

*gid = gr->gr_gid;<br />

endgrent( );<br />

return 0;<br />

}<br />

Groups may contain more than a single user. Theoretically, groups may contain any<br />

number of members, but be aware that some implementations may impose artificial<br />

limits on the number of users that may belong to a group.<br />

374 | Chapter 8: Authentication and Key Exchange<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!