open.itworld.com
  Search  
Security Home Page Security Webcasts Security White Papers Security Newsletters Security News Open Topics Careers ITworld Voices ITwhirled The Security site of ITworld.com
Changing a File's Owner and Group
LINUX TIPS AND TRICKS --- 09/06/2002

Danny Kalev

Last week we learned how to manipulate a process's uid and gid. Today I will show how to change a file's owner and group. 

On this topic

The uid and gid of a file are stored in its inode. When you change a file's uid or gid, the change is reflected in all the hard links that are mapped to that file.

The chown() Syscall
The chown() syscall is declared in <unistd.h>. It has the following prototype:

int chown(const char *path, uid_t owner, gid_t group);

Notice that this function is used for changing both the owner and the group of a file. The first argument is the file's name or path. The second and third arguments specify the new owner and group for the file. Passing -1 as an owner or group indicates that the original value remains unchanged. Only a root user may change a file's owner. For security reasons, the setuid bit of the file is cleared whenever its owner is changed.

To change a file's group, the user must be the file's owner or a root user. In addition, the owner must belong to the group to which he or she is changing the file. If the file's group-execute bit is set, the setgid bit of that file is cleared for security reasons.

The fchown() Syscall
Linux defines another syscall for changing a file's owner and group called fchown(). This function is declared in <unistd.h> as follows:

int fchown(int fd, uid_t owner, gid_t group);

In terms of functionality, fchown() is identical to chown() except that it takes a file descriptor as the first argument rather than a filename.

A Note on a Previous Newsletter
The ANSI function tmpnam() discussed in the newsletter from August 16th is deprecated. The use of this function is considered unsafe since there's no guarantee the generated filenames are accessed by the process that called tmpnam(). That said, this limitation is a quality-of-implementation issue rather than an inherent flaw in the ANSI specification. Still, the recommendation is to use the POSIX mkstemp() function instead of tmpnam().

 

Danny Kalev is a system analyst and software engineer with more than 10 years of experience, specializing in C++ and object-oriented analysis and design on various platforms including VMS, DOS, Windows, Unix, and Linux. His technical interests involve code optimization, networking, and distributed computing. He is also a member of the ANSI C++ standardization committee and the author of ANSI/ISO C++ Professional Programmer's Handbook (Que, 1999). Danny can be reached at Danny.Kalev@itworld.com.



Advertisements
Sponsored links
Bring harmony to your mix of UNIX-Linux-Windows computing environments
KODAK i1400 Series Scanners stand up to the challenge
Top 5 Reasons to Combine App Performance and Security
Locate Hidden Software on business PCs with this free tool
 Home   Newsletters  LINUX TIPS AND TRICKS
www.itworld.com    open.itworld.com     security.itworld.com     smallbusiness.itworld.com
storage.itworld.com     utilitycomputing.itworld.com     wireless.itworld.com

 
Contact Us   About Us   Privacy Policy    Terms of Service   Reprints  

CIO   Computerworld   CSO   GamePro   Games.net   IDG Connect   IDG World Expo   Infoworld   ITworld   JavaWorld   LinuxWorld  MacUser   Macworld   Network World   PC World   Playlist  

Copyright © Computerworld, Inc. All rights reserved

Reproduction in whole or in part in any form or medium without express written permission of Computerworld Inc. is prohibited. Computerworld and Computerworld.com and the respective logos are trademarks of International Data Group Inc.