show version
[tridge/junkcode.git] / id_create.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 int main(int argc, const char *argv[])
10 {
11         const char *name;
12         uid_t id, id2;
13         int fd;
14         struct stat st;
15
16         if (argc < 3) {
17                 printf("id_create <name> <uid>\n");
18                 exit(1);
19         }
20
21         name = argv[1];
22         id = atoi(argv[2]);
23
24         if (seteuid(id) != 0) {
25                 perror("seteuid");
26                 return -1;
27         }
28
29         id2 = geteuid();
30         if (id2 != id) {
31                 fprintf(stderr, "geteuid gave wrong id (%d)", id2);
32                 return -1;
33         }
34         
35         unlink(name);
36
37         fd = open(name, O_CREAT|O_EXCL|O_RDWR, 0644);
38         if (fd == -1) {
39                 perror("open");
40                 return -1;
41         }
42
43         fstat(fd, &st);
44
45         if (st.st_uid != id) {
46                 printf("Created with wrong uid (%d should be %d)!\n",
47                        st.st_uid, id);
48                 exit(1);
49         }
50
51         close(fd);
52         return 0;
53 }
54