af97903277f70c15a8606c10ed817dd3af4578c4
[jelmer/samba4-debian.git] / source / 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 2 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, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "dynconfig.h"
27 #include "system/network.h"
28 #include "system/filesys.h"
29
30 /**
31  * @file
32  * @brief Misc utility functions
33  */
34
35
36 /**
37   see if a string matches either our primary or one of our secondary 
38   netbios aliases. do a case insensitive match
39 */
40 _PUBLIC_ BOOL is_myname(const char *name)
41 {
42         const char **aliases;
43         int i;
44
45         if (strcasecmp(name, lp_netbios_name()) == 0) {
46                 return True;
47         }
48
49         aliases = lp_netbios_aliases();
50         for (i=0; aliases && aliases[i]; i++) {
51                 if (strcasecmp(name, aliases[i]) == 0) {
52                         return True;
53                 }
54         }
55
56         return False;
57 }
58
59
60 /**
61  A useful function for returning a path in the Samba lock directory.
62 **/
63 _PUBLIC_ char *lock_path(TALLOC_CTX* mem_ctx, const char *name)
64 {
65         char *fname, *dname;
66         if (name == NULL) {
67                 return NULL;
68         }
69         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
70                 return talloc_strdup(mem_ctx, name);
71         }
72
73         dname = talloc_strdup(mem_ctx, lp_lockdir());
74         trim_string(dname,"","/");
75         
76         if (!directory_exist(dname)) {
77                 mkdir(dname,0755);
78         }
79         
80         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
81
82         talloc_free(dname);
83
84         return fname;
85 }
86
87
88 /**
89  A useful function for returning a path in the Samba piddir directory.
90 **/
91 static char *pid_path(TALLOC_CTX* mem_ctx, const char *name)
92 {
93         char *fname, *dname;
94
95         dname = talloc_strdup(mem_ctx, lp_piddir());
96         trim_string(dname,"","/");
97         
98         if (!directory_exist(dname)) {
99                 mkdir(dname,0755);
100         }
101         
102         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
103
104         talloc_free(dname);
105
106         return fname;
107 }
108
109
110 /**
111  * @brief Returns an absolute path to a file in the Samba lib directory.
112  *
113  * @param name File to find, relative to DATADIR.
114  *
115  * @retval Pointer to a talloc'ed string containing the full path.
116  **/
117
118 _PUBLIC_ char *data_path(TALLOC_CTX* mem_ctx, const char *name)
119 {
120         char *fname;
121         fname = talloc_asprintf(mem_ctx, "%s/%s", dyn_DATADIR, name);
122         return fname;
123 }
124
125 /**
126  * @brief Returns an absolute path to a file in the Samba private directory.
127  *
128  * @param name File to find, relative to PRIVATEDIR.
129  * if name is not relative, then use it as-is
130  *
131  * @retval Pointer to a talloc'ed string containing the full path.
132  **/
133 _PUBLIC_ char *private_path(TALLOC_CTX* mem_ctx, const char *name)
134 {
135         char *fname;
136         if (name == NULL) {
137                 return NULL;
138         }
139         if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
140                 return talloc_strdup(mem_ctx, name);
141         }
142         fname = talloc_asprintf(mem_ctx, "%s/%s", lp_private_dir(), name);
143         return fname;
144 }
145
146 /**
147   return a path in the smbd.tmp directory, where all temporary file
148   for smbd go. If NULL is passed for name then return the directory 
149   path itself
150 */
151 _PUBLIC_ char *smbd_tmp_path(TALLOC_CTX *mem_ctx, const char *name)
152 {
153         char *fname, *dname;
154
155         dname = pid_path(mem_ctx, "smbd.tmp");
156         if (!directory_exist(dname)) {
157                 mkdir(dname,0755);
158         }
159
160         if (name == NULL) {
161                 return dname;
162         }
163
164         fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
165         talloc_free(dname);
166
167         return fname;
168 }
169
170 static char *modules_path(TALLOC_CTX* mem_ctx, const char *name)
171 {
172         return talloc_asprintf(mem_ctx, "%s/%s", lp_modulesdir(), name);
173 }
174
175 /**
176  * Load the initialization functions from DSO files for a specific subsystem.
177  *
178  * Will return an array of function pointers to initialization functions
179  */
180
181 _PUBLIC_ init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
182 {
183         char *path = modules_path(mem_ctx, subsystem);
184         init_module_fn *ret;
185
186         ret = load_modules(mem_ctx, path);
187
188         talloc_free(path);
189
190         return ret;
191 }
192
193