r23792: convert Samba4 to GPLv3
[metze/samba/wip.git] / source4 / param / util.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    Copyright (C) Jeremy Allison 2001-2002
6    Copyright (C) Simo Sorce 2001
7    Copyright (C) Jim McDonough (jmcd@us.ibm.com)  2003.
8    Copyright (C) James J Myers 2003
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "dynconfig.h"
26 #include "system/network.h"
27 #include "system/filesys.h"
28
29 /**
30  * @file
31  * @brief Misc utility functions
32  */
33
34
35 /**
36   see if a string matches either our primary or one of our secondary 
37   netbios aliases. do a case insensitive match
38 */
39 _PUBLIC_ BOOL is_myname(const char *name)
40 {
41         const char **aliases;
42         int i;
43
44         if (strcasecmp(name, lp_netbios_name()) == 0) {
45                 return True;
46         }
47
48         aliases = lp_netbios_aliases();
49         for (i=0; aliases && aliases[i]; i++) {
50                 if (strcasecmp(name, aliases[i]) == 0) {
51                         return True;
52                 }
53         }
54
55         return False;
56 }
57
58
59 /**
60  A useful function for returning a path in the Samba lock directory.
61 **/
62 _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
63 {
64         char *fname, *dname;
65         if (name == NULL) {
66                 return NULL;
67         }
68         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
69                 return talloc_strdup(mem_ctx, name);
70         }
71
72         dname = talloc_strdup(mem_ctx, lp_lockdir());
73         trim_string(dname,"","/");
74         
75         if (!directory_exist(dname)) {
76                 mkdir(dname,0755);
77         }
78         
79         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
80
81         talloc_free(dname);
82
83         return fname;
84 }
85
86
87 /**
88  A useful function for returning a path in the Samba piddir directory.
89 **/
90 static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
91 {
92         char *fname, *dname;
93
94         dname = talloc_strdup(mem_ctx, lp_piddir());
95         trim_string(dname,"","/");
96         
97         if (!directory_exist(dname)) {
98                 mkdir(dname,0755);
99         }
100         
101         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
102
103         talloc_free(dname);
104
105         return fname;
106 }
107
108
109 /**
110  * @brief Returns an absolute path to a file in the Samba lib directory.
111  *
112  * @param name File to find, relative to DATADIR.
113  *
114  * @retval Pointer to a talloc'ed string containing the full path.
115  **/
116
117 _PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
118 {
119         char *fname;
120         fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_DATADIR, name);
121         return fname;
122 }
123
124 /**
125  * @brief Returns an absolute path to a file in the directory containing the current config file
126  *
127  * @param name File to find, relative to the config file directory.
128  *
129  * @retval Pointer to a talloc'ed string containing the full path.
130  **/
131
132 _PUBLIC_ char *config_path(TALLOC_CTX* mem_ctx, const char *name)
133 {
134         char *fname, *config_dir, *p;
135         config_dir = talloc_strdup(mem_ctx, lp_configfile());
136         p = strrchr(config_dir, '/');
137         if (!p) {
138                 return NULL;
139         }
140         p[0] = '\0';
141         fname = talloc_asprintf(mem_ctx, "%s/%s", config_dir, name);
142         talloc_free(config_dir);
143         return fname;
144 }
145
146 /**
147  * @brief Returns an absolute path to a file in the Samba private directory.
148  *
149  * @param name File to find, relative to PRIVATEDIR.
150  * if name is not relative, then use it as-is
151  *
152  * @retval Pointer to a talloc'ed string containing the full path.
153  **/
154 _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
155 {
156         char *fname;
157         if (name == NULL) {
158                 return NULL;
159         }
160         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
161                 return talloc_strdup(mem_ctx, name);
162         }
163         fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
164         return fname;
165 }
166
167 /**
168   return a path in the smbd.tmp directory, where all temporary file
169   for smbd go. If NULL is passed for name then return the directory 
170   path itself
171 */
172 _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
173 {
174         char *fname, *dname;
175
176         dname = pid_path(mem_ctx, "smbd.tmp");
177         if (!directory_exist(dname)) {
178                 mkdir(dname,0755);
179         }
180
181         if (name == NULL) {
182                 return dname;
183         }
184
185         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
186         talloc_free(dname);
187
188         return fname;
189 }
190
191 static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
192 {
193         const char *env_moduledir = getenv("LD_SAMBA_MODULE_PATH");
194         return talloc_asprintf(mem_ctx, "%s/%s", 
195                                                    env_moduledir?env_moduledir:lp_modulesdir(), 
196                                                    name);
197 }
198
199 /**
200  * Load the initialization functions from DSO files for a specific subsystem.
201  *
202  * Will return an array of function pointers to initialization functions
203  */
204
205 _PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
206 {
207         char *path = modules_path(mem_ctx, subsystem);
208         init_module_fn *ret;
209
210         ret = load_modules(mem_ctx, path);
211
212         talloc_free(path);
213
214         return ret;
215 }
216
217