1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <errno.h>
#include <unistd.h>
#include <strings.h>
#include <sys/fs_reparse.h>
#include <smbsrv/libsmb.h>
#include <syslog.h>
static int smb_reparse_init(const char *, nvlist_t **);
static void smb_reparse_free(nvlist_t *);
static int smb_reparse_set(const char *, nvlist_t *);
/*
* Checks the status of the object specified by 'path'
*
* Returns 0 and fills 'stat' with the proper status on
* success, otherwise returns an error code.
*/
int
smb_reparse_stat(const char *path, uint32_t *stat)
{
struct stat statbuf;
char symbuf[MAXREPARSELEN];
int rptaglen;
if (lstat(path, &statbuf) != 0) {
if (errno == ENOENT) {
*stat = SMB_REPARSE_NOTFOUND;
return (0);
}
return (errno);
}
if ((statbuf.st_mode & S_IFMT) != S_IFLNK) {
*stat = SMB_REPARSE_NOTREPARSE;
return (0);
}
bzero(symbuf, MAXREPARSELEN);
if (readlink(path, symbuf, MAXREPARSELEN) == -1)
return (errno);
rptaglen = strlen(FS_REPARSE_TAG_STR);
if (strncmp(symbuf, FS_REPARSE_TAG_STR, rptaglen) != 0)
*stat = SMB_REPARSE_NOTREPARSE;
else
*stat = SMB_REPARSE_ISREPARSE;
return (0);
}
/*
* If the reparse point specified by the path already exists
* it is updated by given service type and its data. Update means
* that if such service type does not already exist, it is added
* otherwise it is overwritten by given data.
*
* If the reparse point does not exist, one is created with given
* service type and its data.
*/
int
smb_reparse_svcadd(const char *path, const char *svctype, const char *svcdata)
{
nvlist_t *nvl;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
if ((rc = reparse_add(nvl, svctype, svcdata)) != 0) {
smb_reparse_free(nvl);
return (rc);
}
rc = smb_reparse_set(path, nvl);
smb_reparse_free(nvl);
return (rc);
}
/*
* Removes the entry for the given service type from the
* specified reparse point. If there is no service entry
* left, the reparse point object will be deleted.
*/
int
smb_reparse_svcdel(const char *path, const char *svctype)
{
nvlist_t *nvl;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
if ((rc = reparse_remove(nvl, svctype)) != 0) {
smb_reparse_free(nvl);
return (rc);
}
if (nvlist_next_nvpair(nvl, NULL) == NULL) {
/* list is empty remove the object */
rc = reparse_delete(path);
if ((rc != 0) && (rc == ENOENT))
rc = 0;
} else {
rc = smb_reparse_set(path, nvl);
}
smb_reparse_free(nvl);
return (rc);
}
/*
* Obtains data of the given service type from the specified
* reparse point. Function allocates the memory needed to hold
* the service data so the caller must free this memory by
* calling free().
*
* If 'svcdata' is NULL, successful return means that the reparse
* point contains a record for the given service type.
*/
int
smb_reparse_svcget(const char *path, const char *svctype, char **svcdata)
{
nvlist_t *nvl;
nvpair_t *nvp;
char *stype, *sdata;
int rc;
if ((rc = smb_reparse_init(path, &nvl)) != 0)
return (rc);
rc = ENODATA;
nvp = nvlist_next_nvpair(nvl, NULL);
while (nvp != NULL) {
stype = nvpair_name(nvp);
if ((stype != NULL) && (strcasecmp(stype, svctype) == 0)) {
if ((rc = nvpair_value_string(nvp, &sdata)) != 0)
break;
if (svcdata != NULL) {
if ((*svcdata = strdup(sdata)) == NULL)
rc = ENOMEM;
}
rc = 0;
break;
}
nvp = nvlist_next_nvpair(nvl, nvp);
}
smb_reparse_free(nvl);
return (rc);
}
/*
* Initializes the given nvpair list.
*
* This function assumes that the object specified by this path
* is a reparse point, so it does not do any verification.
*
* If specified reparse point does not exist the function
* returns successfully with an empty nvpair list.
*
* If the object exists and readlink is successful then nvpair
* list is polulated with the reparse service information, otherwise
* an error code is returned.
*/
static int
smb_reparse_init(const char *path, nvlist_t **nvl)
{
char rp_data[MAXREPARSELEN];
int rc;
if ((*nvl = reparse_init()) == NULL)
return (ENOMEM);
bzero(rp_data, MAXREPARSELEN);
if ((rc = readlink(path, rp_data, MAXREPARSELEN)) == -1) {
if (errno == ENOENT)
return (0);
reparse_free(*nvl);
return (errno);
}
if ((rc = reparse_parse(rp_data, *nvl)) != 0) {
reparse_free(*nvl);
return (rc);
}
return (0);
}
/*
* Frees given nvlist
*/
static void
smb_reparse_free(nvlist_t *nvl)
{
reparse_free(nvl);
}
/*
* Create a reparse point with given services in the passed
* nvlist. If the reparse point already exists, it will be
* deleted and a new one with the given data is created.
*/
static int
smb_reparse_set(const char *path, nvlist_t *nvl)
{
char *rp_data;
int rc;
if ((rc = reparse_unparse(nvl, &rp_data)) != 0)
return (rc);
rc = reparse_delete(path);
if ((rc != 0) && (rc != ENOENT)) {
free(rp_data);
return (rc);
}
rc = reparse_create(path, rp_data);
free(rp_data);
return (rc);
}
|