venus: Make clang happy with strlcpy use
[openafs.git] / tests / common / config.c
1 /*
2  * Copyright (c) 2010 Your File System Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 /*!
26  * Common functions for building a configuration directory
27  */
28
29 #include <afsconfig.h>
30 #include <afs/param.h>
31 #include <roken.h>
32
33 #include <afs/cellconfig.h>
34 #include <afs/afsutil.h>
35
36 #include <hcrypto/des.h>
37
38 #include "common.h"
39
40 static FILE *
41 openConfigFile(char *dirname, char *filename) {
42     char *path = NULL;
43     FILE *file;
44
45     if (asprintf(&path, "%s/%s", dirname, filename) == -1)
46         return NULL;
47
48     file = fopen(path, "w");
49     free(path);
50     return file;
51 }
52
53 static void
54 unlinkConfigFile(char *dirname, char *filename) {
55     char *path;
56
57     if (asprintf(&path, "%s/%s", dirname, filename) != -1) {
58         unlink(path);
59         free(path);
60     }
61 }
62
63 /*!
64  * Build a test configuration directory, containing a CellServDB and ThisCell
65  * file for the "example.org" cell
66  *
67  * @return
68  * The path to the configuration directory. This should be freed by the caller
69  * using free()
70  *
71  */
72
73 char *
74 afstest_BuildTestConfig(void) {
75     char *dir = NULL;
76     FILE *file;
77     struct hostent *host;
78     char hostname[255];
79     struct in_addr iaddr;
80
81     if (asprintf(&dir, "%s/afs_XXXXXX", gettmpdir()) == -1)
82         goto fail;
83
84     if (mkdtemp(dir) == NULL)
85         goto fail;
86
87     /* Work out which IP address to use in our CellServDB. We figure this out
88      * according to the IP address which ubik is most likely to pick for one of
89      * our db servers */
90
91     gethostname(hostname, sizeof(hostname));
92     host = gethostbyname(hostname);
93     if (!host)
94         return NULL;
95
96     memcpy(&iaddr, host->h_addr, 4);
97
98     file = openConfigFile(dir, "CellServDB");
99     fprintf(file, ">example.org # An example cell\n");
100     fprintf(file, "%s #test.example.org\n", inet_ntoa(iaddr));
101     fclose(file);
102
103     /* Create a ThisCell file */
104     file = openConfigFile(dir, "ThisCell");
105     fprintf(file, "example.org");
106     fclose(file);
107
108     return dir;
109
110 fail:
111     if (dir)
112         free(dir);
113     return NULL;
114 }
115
116 /*!
117  * Delete at test configuration directory
118  */
119
120 void
121 afstest_UnlinkTestConfig(char *dir)
122 {
123     unlinkConfigFile(dir, "KeyFile");
124     unlinkConfigFile(dir, "CellServDB");
125     unlinkConfigFile(dir, "ThisCell");
126     unlinkConfigFile(dir, "UserList");
127     rmdir(dir);
128 }
129
130 int
131 afstest_AddDESKeyFile(struct afsconf_dir *dir)
132 {
133     char keymaterial[]="\x19\x17\xff\xe6\xbb\x77\x2e\xfc";
134
135     /* Make sure that it is actually a valid key */
136     DES_set_odd_parity((DES_cblock *)keymaterial);
137
138     return afsconf_AddKey(dir, 1, keymaterial, 1);
139 }