Link to home
Start Free TrialLog in
Avatar of sevrin
sevrin

asked on

masks and sys/stat.h

My question relates to the header file <sys/stat.h>. The following code snippet is from W. Richard Stevens' "Unix Network Programming" (it's part of an authentication program):

#include <sys/stat.h>
#include <pwd.h>
...

struct stat statbuff;
struct passwd *pwd;
register FILE *hostfp;
...

if ((fstat(fileno(hostfp), &statbuff) < 0 ||
      (statbuff.st_uid !=0 && statbuff.st_uid != pwd->pw_uid) ||
      (statbuff.st_mode & 022) {
            fclose(hostfp);
            return(-1);
            }

My question relates to the reference to 022. This appears to be some kind of mask - but what exactly is its role in the program? My sys/stat.h contains the following masks, none of which seems to correspond to the 022:

#define S_IFMT  00170000
#define S_IFSOCK 0140000
#define S_IFLNK       0120000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

Any enlightenment would be appreciated! (Also, I'm certain that Stevens has omitted to mention what "fileno", also in the above code snippet, is. It's not some standard library function, is it?)

Avatar of ecw
ecw

022 refers to group and others writable permission.  fileno is a standard function that returns the file descriptor of the FILE* passed.
Symbolically, 022 is;
 (S_IWGRP | S_IWOTH)
The code snippet I guess is from rlogind or rshd (remshd), and verifies that .rhosts is either owned by root or owned by the user, and is not writable by anyone other than the owner.
Avatar of sevrin

ASKER

I want to give you the points for this, ecw, so perhaps you can reply so that I get the "question answered" message. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of ecw
ecw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial