s3:libsmb: allow store_cldap_reply() to work with a ipv6 response
[samba.git] / source3 / modules / lib_vxfs.c
1 /*
2  Unix SMB/CIFS implementation.
3  Wrap VxFS xattr calls.
4
5  Copyright (C) Veritas Technologies LLC <www.veritas.com> 2016
6
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version.
11
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  GNU General Public License for more details.
16
17  You should have received a copy of the GNU General Public License
18  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "string.h"
25 #include "vfs_vxfs.h"
26
27 /*
28  * Available under GPL at
29  * http://www.veritas.com/community/downloads/vxfsmisc-library
30  */
31 #define LIBVXFS "/usr/lib64/vxfsmisc.so"
32
33
34 static int (*vxfs_setxattr_fd_func) (int fd, const char *name,
35                                      const void *value, size_t len, int flags);
36 static int (*vxfs_getxattr_fd_func) (int fd, const char *name, void *value,
37                                      size_t *len);
38 static int (*vxfs_removexattr_fd_func) (int fd, const char *name);
39 static int (*vxfs_listxattr_fd_func) (int fd, void *value, size_t *len);
40 static int (*vxfs_setwxattr_fd_func) (int fd);
41 static int (*vxfs_checkwxattr_fd_func) (int fd);
42
43 int vxfs_setxattr_fd(int fd, const char *name, const void *value,
44                      size_t len, int flags)
45 {
46         int ret = -1;
47
48         DBG_DEBUG("In vxfs_setxattr_fd fd %d name %s len %zu flags %d\n",
49                 fd, name, len, flags);
50         if (vxfs_setxattr_fd_func == NULL) {
51                 errno = ENOSYS;
52                 return ret;
53         }
54
55         DBG_DEBUG("Calling vxfs_setxattr_fd\n");
56         ret = vxfs_setxattr_fd_func(fd, name, value, len, flags);
57         DBG_DEBUG("vxfs_setxattr_fd ret = %d \n", ret);
58         if (ret) {
59                 errno = ret;
60                 ret = -1;
61         }
62
63         return ret;
64 }
65
66 int vxfs_getxattr_fd(int fd, const char *name, void *value, size_t len)
67 {
68         int ret;
69         size_t size = len;
70         DBG_DEBUG("In vxfs_getxattr_fd fd %d name %s len %zu\n",
71                 fd, name, len);
72
73         if (vxfs_getxattr_fd_func == NULL) {
74                 errno = ENOSYS;
75                 return -1;
76         }
77
78         DBG_DEBUG("Calling vxfs_getxattr_fd with %s\n", name);
79         ret = vxfs_getxattr_fd_func(fd, name, value, &size);
80         DBG_DEBUG("vxfs_getxattr_fd ret = %d\n", ret);
81         if (ret) {
82                 errno = ret;
83                 if (ret == EFBIG) {
84                         errno = ERANGE;
85                 }
86                 return -1;
87         }
88         DBG_DEBUG("vxfs_getxattr_fd done with size %zu\n", size);
89
90         return size;
91 }
92
93 int vxfs_getxattr_path(const char *path, const char *name, void *value,
94                        size_t len)
95 {
96         int ret, fd = -1;
97         DBG_DEBUG("In vxfs_getxattr_path path %s name %s len %zu\n",
98                 path, name, len);
99
100         fd = open(path, O_RDONLY);
101         if (fd == -1) {
102                 DBG_DEBUG("file not opened: vxfs_getxattr_path for %s\n",
103                            path);
104                 return -1;
105         }
106
107         ret = vxfs_getxattr_fd(fd, name, value, len);
108         close(fd);
109
110         return ret;
111 }
112
113 int vxfs_removexattr_fd(int fd, const char *name)
114 {
115         int ret = 0;
116         DBG_DEBUG("In vxfs_removexattr_fd fd %d name %s\n", fd, name);
117
118         if (vxfs_removexattr_fd_func == NULL) {
119                 errno = ENOSYS;
120                 return -1;
121         }
122
123         DBG_DEBUG("Calling vxfs_removexattr_fd with %s\n", name);
124         ret = vxfs_removexattr_fd_func(fd, name);
125         if (ret) {
126                 errno = ret;
127                 ret = -1;
128         }
129
130         return ret;
131 }
132
133 int vxfs_listxattr_fd(int fd, char *list, size_t size)
134 {
135         int ret;
136         size_t len = size;
137         DBG_DEBUG("In vxfs_listxattr_fd fd %d list %s size %zu\n", fd, list, size);
138
139         if (vxfs_listxattr_fd_func == NULL) {
140                 errno = ENOSYS;
141                 return -1;
142         }
143
144         ret = vxfs_listxattr_fd_func(fd, list, &len);
145         DBG_DEBUG("vxfs_listxattr_fd: returned ret = %d\n", ret);
146         DBG_DEBUG("In vxfs_listxattr_fd done with len %zu\n", len);
147         if (ret) {
148                 errno = ret;
149                 if (ret == EFBIG) {
150                         errno = ERANGE;
151                 }
152                 return -1;
153         }
154
155         return len;
156 }
157
158 int vxfs_setwxattr_fd(int fd)
159 {
160         int ret = 0;
161         DBG_DEBUG("In vxfs_setwxattr_fd fd %d\n", fd);
162
163         if (vxfs_setwxattr_fd_func == NULL) {
164                 errno = ENOSYS;
165                 return -1;
166         }
167         ret = vxfs_setwxattr_fd_func(fd);
168         DBG_DEBUG("ret = %d\n", ret);
169         if (ret != 0) {
170                 errno = ret;
171                 ret = -1;
172         }
173
174         return ret;
175 }
176
177 int vxfs_setwxattr_path(const char *path, bool is_dir)
178 {
179         int ret, fd = -1;
180         DBG_DEBUG("In vxfs_setwxattr_path path %s is_dir %d\n", path, is_dir);
181
182         if (is_dir) {
183                 fd = open(path, O_RDONLY|O_DIRECTORY);
184         } else {
185                 fd = open(path, O_WRONLY);
186         }
187         if (fd == -1) {
188                 DBG_DEBUG("file %s not opened, errno:%s\n",
189                            path, strerror(errno));
190                 return -1;
191         }
192
193         ret = vxfs_setwxattr_fd(fd);
194         DBG_DEBUG("ret = %d\n", ret);
195         close(fd);
196
197         return ret;
198 }
199
200 int vxfs_checkwxattr_fd(int fd)
201 {
202         int ret;
203         DBG_DEBUG("In vxfs_checkwxattr_fd fd %d\n", fd);
204
205         if (vxfs_checkwxattr_fd_func == NULL) {
206                 errno = ENOSYS;
207                 return -1;
208         }
209         ret = vxfs_checkwxattr_fd_func(fd);
210         DBG_DEBUG("ret = %d\n", ret);
211         if (ret != 0) {
212                 errno = ret;
213                 ret = -1;
214         }
215         return ret;
216 }
217
218 int vxfs_checkwxattr_path(const char *path)
219 {
220         int ret, fd = -1;
221         DBG_DEBUG("In vxfs_checkwxattr_path path %s\n", path);
222
223         fd = open(path, O_RDONLY);
224
225         if (fd == -1) {
226                 DBG_DEBUG("file %s not opened, errno:%s\n",
227                            path, strerror(errno));
228                 return -1;
229         }
230         ret = vxfs_checkwxattr_fd(fd);
231         close(fd);
232
233         return ret;
234 }
235
236 static bool load_lib_vxfs_function(void *lib_handle, void *fn_ptr,
237                                    const char *fnc_name)
238 {
239         void **vlib_handle = (void **)lib_handle;
240         void **fn_pointer = (void **)fn_ptr;
241
242         *fn_pointer = dlsym(*vlib_handle, fnc_name);
243         if (*fn_pointer == NULL) {
244                 DEBUG(10, ("Cannot find symbol for %s\n", fnc_name));
245                 return true;
246         }
247
248         return false;
249 }
250
251 void vxfs_init()
252 {
253         static void *lib_handle = NULL;
254
255         if (lib_handle != NULL ) {
256                 return;
257         }
258
259         lib_handle = dlopen(LIBVXFS, RTLD_LAZY);
260         if (lib_handle == NULL) {
261                 DEBUG(10, ("Cannot get lib handle\n"));
262                 return;
263         }
264
265         DEBUG(10, ("Calling vxfs_init\n"));
266         load_lib_vxfs_function(&lib_handle, &vxfs_setxattr_fd_func,
267                                "vxfs_nxattr_set");
268         load_lib_vxfs_function(&lib_handle, &vxfs_getxattr_fd_func,
269                                "vxfs_nxattr_get");
270         load_lib_vxfs_function(&lib_handle, &vxfs_removexattr_fd_func,
271                                "vxfs_nxattr_remove");
272         load_lib_vxfs_function(&lib_handle, &vxfs_listxattr_fd_func,
273                                "vxfs_nxattr_list");
274         load_lib_vxfs_function(&lib_handle, &vxfs_setwxattr_fd_func,
275                                "vxfs_wattr_set");
276         load_lib_vxfs_function(&lib_handle, &vxfs_checkwxattr_fd_func,
277                                "vxfs_wattr_check");
278
279 }