2 * Winbind ID Mapping Plugin
3 * Copyright (C) 2012 Jeff Layton (jlayton@samba.org)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #endif /* HAVE_CONFIG_H */
32 #include "cifsidmap.h"
34 static const char **plugin_errmsg;
37 * Winbind keeps wbcDomainSid fields in host-endian. Copy fields from the
38 * csid to the wsid, while converting the subauthority fields from LE.
41 csid_to_wsid(struct wbcDomainSid *wsid, const struct cifs_sid *csid)
44 uint8_t num_subauth = (csid->num_subauth <= WBC_MAXSUBAUTHS) ?
45 csid->num_subauth : WBC_MAXSUBAUTHS;
47 wsid->sid_rev_num = csid->revision;
48 wsid->num_auths = num_subauth;
49 for (i = 0; i < NUM_AUTHS; i++)
50 wsid->id_auth[i] = csid->authority[i];
51 for (i = 0; i < num_subauth; i++)
52 wsid->sub_auths[i] = le32toh(csid->sub_auth[i]);
56 * Winbind keeps wbcDomainSid fields in host-endian. Copy fields from the
57 * wsid to the csid, while converting the subauthority fields to LE.
60 wsid_to_csid(struct cifs_sid *csid, struct wbcDomainSid *wsid)
63 uint8_t num_subauth = (wsid->num_auths <= SID_MAX_SUB_AUTHORITIES) ?
64 wsid->num_auths : SID_MAX_SUB_AUTHORITIES;
66 csid->revision = wsid->sid_rev_num;
67 csid->num_subauth = num_subauth;
68 for (i = 0; i < NUM_AUTHS; i++)
69 csid->authority[i] = wsid->id_auth[i];
70 for (i = 0; i < num_subauth; i++)
71 csid->sub_auth[i] = htole32(wsid->sub_auths[i]);
75 cifs_idmap_sid_to_str(void *handle __attribute__ ((unused)),
76 const struct cifs_sid *csid, char **string)
82 enum wbcSidType sntype;
83 struct wbcDomainSid wsid;
86 csid_to_wsid(&wsid, csid);
88 wbcrc = wbcLookupSid(&wsid, &domain, &name, &sntype);
89 if (!WBC_ERROR_IS_OK(wbcrc)) {
90 *plugin_errmsg = wbcErrorString(wbcrc);
94 /* +1 for '\\' and +1 for NULL terminator */
95 len = strlen(domain) + 1 + strlen(name) + 1;
97 *string = malloc(len);
99 *plugin_errmsg = "Unable to allocate memory";
104 rc = snprintf(*string, len, "%s\\%s", domain, name);
105 if (rc >= (long)len) {
107 *plugin_errmsg = "Resulting string was truncated";
114 wbcFreeMemory(domain);
120 cifs_idmap_str_to_sid(void *handle __attribute__ ((unused)),
121 const char *orig, struct cifs_sid *csid)
124 char *name, *domain, *sidstr;
125 enum wbcSidType type;
126 struct wbcDomainSid wsid;
128 sidstr = strdup(orig);
130 *plugin_errmsg = "Unable to copy string";
134 name = strchr(sidstr, '\\');
136 /* might be a raw string representation of SID */
137 wbcrc = wbcStringToSid(sidstr, &wsid);
138 if (WBC_ERROR_IS_OK(wbcrc))
149 wbcrc = wbcLookupName(domain, name, &wsid, &type);
150 /* FIXME: map these to better POSIX error codes? */
151 if (!WBC_ERROR_IS_OK(wbcrc)) {
152 *plugin_errmsg = wbcErrorString(wbcrc);
158 wsid_to_csid(csid, &wsid);
164 * For the winbind plugin, we don't need to do anything special on
168 cifs_idmap_init_plugin(void **handle __attribute__((unused)), const char **errmsg)
170 plugin_errmsg = errmsg;
175 cifs_idmap_exit_plugin(void *handle __attribute__((unused)))