LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   setgid() calls get error " (https://www.linuxquestions.org/questions/programming-9/setgid-calls-get-error-822296/)

sun_peixing 07-26-2010 03:35 PM

setgid() calls get error "
 
Hi All:

I have a simple program attached below. I login as root, compiled and ran setgid_test.c as root also. The program simply tries to copy a file "seed_file" to a new "test_file". The new "test_file" should have user id: 40088, group id "40000". When I ran a.out, setuid() worked, but setgid() failed. I have to have this work, because I always login to a root, but will create files with different uids and gids to test a quota system.

Please note that both uid and gid I used are valid. I have NIS client setup properly.

[root@rtplab101 test_dir]# ypcat passwd|grep 40088 |grep 40000
dvt_usr40088:aK5r4VXKZIBUo:40088:40000:quota_testing:/:/bin/csh

I am running Redhat 5 on my Linux client.
Linux rtplab101 2.6.18-92.el5PAE #1 SMP Tue Apr 29 13:31:02 EDT 2008 i686 i686 i386 GNU/Linux

Very appreciate your help.

Peixing

-rwxr-xr-x 1 root root 2406185 Jul 26 10:56 seed_file
-rw-r--r-- 1 root root 632 Jul 26 2010 setgid_test.c
-rwxr-xr-x 1 root root 5928 Jul 26 2010 a.out

./a.out
setgid failed: errno=1, Operation not permitted

ls -l test_file /* get the righ uid, but setgid failed */
-rw-r--r-- 1 40088 root 2408448 Jul 26 2010 test_file

====setgid_test.c===================
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define BUFFER_SIZE 8192

const char *seed_file = "seed_file";
char *buffer[BUFFER_SIZE];

int main(){
FILE *fpin;
FILE *fpout;

if(setuid(40088)){
printf("setuid failed: %s\n", strerror(errno));
}
if(setgid(40000)){
printf("setgid failed: errno=%d, %s\n", errno, strerror(errno));
}

fpin = fopen(seed_file, "r");
fpout = fopen("test_file", "w");

while(!feof(fpin)){
fread(buffer, BUFFER_SIZE, 1, fpin);
fwrite(buffer, BUFFER_SIZE, 1, fpout);
}

fclose(fpin);
fclose(fpout);
}

pixellany 07-27-2010 09:08 AM

Moved to Programming


All times are GMT -5. The time now is 05:32 AM.