tests: Emulate mkdtemp when not available
[openafs.git] / tests / auth / writekeyfile.c
1 /* This is a simple program which originally produced the KeyFile used
2  * by the test suite. The contents of that file shouldn't be regenerated,
3  * though, as the purpose of the tests using that file is to ensure that we
4  * can still read old KeyFiles.
5  */
6
7 #include <afsconfig.h>
8 #include <afs/param.h>
9 #include <afs/cellconfig.h>
10 #include <afs/afsutil.h>
11
12 #include <roken.h>
13
14 int
15 main(int argc, char **argv)
16 {
17     struct afsconf_dir *dir;
18     char buffer[1024];
19     char *block;
20     char *dirEnd;
21     FILE *file;
22     int in, out;
23     size_t len;
24     int code;
25
26     snprintf(buffer, sizeof(buffer), "%s/afs_XXXXXX", gettmpdir());
27     afstest_mkdtemp(buffer);
28     dirEnd = buffer + strlen(buffer);
29
30     /* Create a CellServDB file */
31     strcpy(dirEnd, "/CellServDB");
32     file = fopen(buffer, "w");
33     fprintf(file, ">example.org # An example cell\n");
34     fprintf(file, "127.0.0.1 #test.example.org\n");
35     fclose(file);
36
37     /* Create a ThisCell file */
38     strcpy(dirEnd, "/ThisCell");
39     file = fopen(buffer, "w");
40     fprintf(file, "example.org\n");
41     fclose(file);
42
43     *dirEnd='\0';
44     dir = afsconf_Open(strdup(buffer));
45     if (dir == NULL) {
46         fprintf(stderr, "Unable to open configuration directory\n");
47         exit(1);
48     }
49
50     afsconf_AddKey(dir, 1, "\x01\x02\x04\x08\x10\x20\x40\x80", 1);
51     afsconf_AddKey(dir, 2, "\x04\x04\x04\x04\x04\x04\x04\x04", 1);
52     afsconf_AddKey(dir, 4, "\x19\x16\xfe\xe6\xba\x77\x2f\xfd", 1);
53
54     afsconf_Close(dir);
55
56     /* Copy out the resulting keyfile into our homedirectory */
57     strcpy(dirEnd, "/KeyFile");
58     in = open(buffer, O_RDONLY);
59     out = open("KeyFile", O_WRONLY | O_CREAT, 0644);
60
61     block = malloc(1024);
62     do {
63         len = read(in, block, 1024);
64         if (len > 0)
65             write(out, block, len);
66     } while (len > 0);
67
68     if (len == -1) {
69         fprintf(stderr, "I/O error whilst copying file\n");
70         exit(1);
71     }
72
73     close(in);
74     close(out);
75
76     strcpy(dirEnd, "/KeyFile");
77     unlink(buffer);
78     strcpy(dirEnd, "/CellServDB");
79     unlink(buffer);
80     strcpy(dirEnd, "/ThisCell");
81     unlink(buffer);
82     strcpy(dirEnd, "/UserList");
83     unlink(buffer);
84     *dirEnd='\0';
85     rmdir(buffer);
86 }