Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

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?)

0
sevrin
Asked:
sevrin
  • 4
1 Solution
 
ecwCommented:
022 refers to group and others writable permission.  fileno is a standard function that returns the file descriptor of the FILE* passed.
0
 
ecwCommented:
Symbolically, 022 is;
 (S_IWGRP | S_IWOTH)
0
 
ecwCommented:
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.
0
 
sevrinAuthor Commented:
I want to give you the points for this, ecw, so perhaps you can reply so that I get the "question answered" message. Thanks.
0
 
ecwCommented:
Repeat of comments:-
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.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now