4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright Milan Jurik 2012. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2015 Joyent, Inc.
26 */
27
28
29 /*
30 * libidmap API
31 */
32
33 #include <stdlib.h>
34 #include <sys/varargs.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <strings.h>
38 #include <ctype.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <dlfcn.h>
43 #include <libintl.h>
44 #include <syslog.h>
45 #include <assert.h>
46 #include "idmap_impl.h"
47 #include "idmap_cache.h"
48
49 static struct timeval TIMEOUT = { 25, 0 };
50
51 static int idmap_stat2errno(idmap_stat);
52 static idmap_stat idmap_strdupnull(char **, const char *);
53
54 #define __ITER_CREATE(itera, argu, ityp)\
55 itera = calloc(1, sizeof (*itera));\
56 if (itera == NULL) {\
57 errno = ENOMEM;\
58 return (IDMAP_ERR_MEMORY);\
59 }\
60 argu = calloc(1, sizeof (*argu));\
61 if (argu == NULL) {\
62 free(itera);\
63 errno = ENOMEM;\
64 return (IDMAP_ERR_MEMORY);\
65 }\
2247
2248 /*
2249 * Get winname given uid
2250 */
2251 idmap_stat
2252 idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
2253 {
2254 return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
2255 }
2256
2257
2258 /*
2259 * Get winname given gid
2260 */
2261 idmap_stat
2262 idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
2263 {
2264 return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
2265 }
2266
2267 idmap_stat
2268 idmap_flush(idmap_flush_op op)
2269 {
2270 idmap_retcode rc1, rc2;
2271
2272 rc1 = _idmap_clnt_call(IDMAP_FLUSH,
2273 (xdrproc_t)xdr_idmap_flush_op, (caddr_t)&op,
2274 (xdrproc_t)xdr_idmap_retcode, (caddr_t)&rc2, TIMEOUT);
2275
2276 if (rc1 != IDMAP_SUCCESS)
2277 return (rc1);
2278 return (rc2);
2279 }
2280
2281
2282 /*
2283 * syslog is the default logger.
2284 * It can be overwritten by supplying a logger
2285 * with idmap_set_logger()
2286 */
|
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright Milan Jurik 2012. All rights reserved.
24 * Copyright 2018 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2015 Joyent, Inc.
26 */
27
28
29 /*
30 * libidmap API
31 */
32
33 #include <stdlib.h>
34 #include <sys/varargs.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <strings.h>
38 #include <ctype.h>
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <dlfcn.h>
43 #include <libintl.h>
44 #include <syslog.h>
45 #include <assert.h>
46 #include <unistd.h>
47 #include <pwd.h>
48 #include <grp.h>
49 #include <netdb.h>
50 #include "idmap_impl.h"
51 #include "idmap_cache.h"
52
53 static struct timeval TIMEOUT = { 25, 0 };
54
55 static int idmap_stat2errno(idmap_stat);
56 static idmap_stat idmap_strdupnull(char **, const char *);
57
58 #define __ITER_CREATE(itera, argu, ityp)\
59 itera = calloc(1, sizeof (*itera));\
60 if (itera == NULL) {\
61 errno = ENOMEM;\
62 return (IDMAP_ERR_MEMORY);\
63 }\
64 argu = calloc(1, sizeof (*argu));\
65 if (argu == NULL) {\
66 free(itera);\
67 errno = ENOMEM;\
68 return (IDMAP_ERR_MEMORY);\
69 }\
2251
2252 /*
2253 * Get winname given uid
2254 */
2255 idmap_stat
2256 idmap_getwinnamebyuid(uid_t uid, int flag, char **name, char **domain)
2257 {
2258 return (idmap_getwinnamebypid(uid, 1, flag, name, domain));
2259 }
2260
2261
2262 /*
2263 * Get winname given gid
2264 */
2265 idmap_stat
2266 idmap_getwinnamebygid(gid_t gid, int flag, char **name, char **domain)
2267 {
2268 return (idmap_getwinnamebypid(gid, 0, flag, name, domain));
2269 }
2270
2271 /*
2272 * Get winname given SID
2273 */
2274 int
2275 idmap_getwinnamebysid(char *sid, int flag, char **name)
2276 {
2277 uid_t pid;
2278 idmap_get_handle_t *get_hdl = NULL;
2279 idmap_rid_t rid;
2280 idmap_stat stat;
2281 char *ridp = NULL;
2282 char *end;
2283 int is_user = 0;
2284 int rc;
2285
2286 if ((ridp = strrchr(sid, '-')) == NULL)
2287 return (IDMAP_ERR_SID);
2288
2289 *ridp = '\0';
2290
2291 errno = 0;
2292 rid = strtoul(ridp + 1, &end, 10);
2293
2294 if (errno != 0 || *end != '\0')
2295 return (IDMAP_ERR_SID);
2296
2297 rc = idmap_get_create(&get_hdl);
2298 if (rc != IDMAP_SUCCESS)
2299 return (rc);
2300
2301 rc = idmap_get_pidbysid(get_hdl, sid, rid, flag, &pid, &is_user, &stat);
2302 *ridp = '-';
2303
2304 if (rc == IDMAP_SUCCESS)
2305 rc = idmap_get_mappings(get_hdl);
2306
2307 if (rc == IDMAP_SUCCESS && stat != IDMAP_SUCCESS)
2308 rc = stat;
2309
2310 idmap_get_destroy(get_hdl);
2311 get_hdl = NULL;
2312
2313 if (rc == IDMAP_SUCCESS) {
2314 rc = idmap_getwinnamebypid(pid, is_user, flag, name, NULL);
2315
2316 if (rc == IDMAP_ERR_NORESULT && !IDMAP_ID_IS_EPHEMERAL(pid)) {
2317 /*
2318 * Unlike LSA, idmap doesn't map a winname for local
2319 * accounts. Recreate one.
2320 */
2321 char buf[1024];
2322 char hostname[MAXHOSTNAMELEN];
2323 struct group gr;
2324 struct passwd pwd;
2325 char *unixname = NULL;
2326
2327 if (is_user) {
2328 if (getpwuid_r(pid, &pwd, buf,
2329 sizeof (buf)) != NULL)
2330 unixname = pwd.pw_name;
2331 } else {
2332 if (getgrgid_r(pid, &gr, buf,
2333 sizeof (buf)) != NULL)
2334 unixname = gr.gr_name;
2335 }
2336
2337 if (unixname == NULL)
2338 return (rc);
2339
2340 hostname[0] = '\0';
2341 if (gethostname(hostname, sizeof (hostname)) == 0)
2342 hostname[MAXHOSTNAMELEN - 1] = '\0';
2343
2344 if (asprintf(name, "%s%s%s", unixname,
2345 (hostname[0] != '\0') ? "@" : "", hostname) >= 0)
2346 rc = IDMAP_SUCCESS;
2347 }
2348 }
2349
2350 return (rc);
2351 }
2352
2353 idmap_stat
2354 idmap_flush(idmap_flush_op op)
2355 {
2356 idmap_retcode rc1, rc2;
2357
2358 rc1 = _idmap_clnt_call(IDMAP_FLUSH,
2359 (xdrproc_t)xdr_idmap_flush_op, (caddr_t)&op,
2360 (xdrproc_t)xdr_idmap_retcode, (caddr_t)&rc2, TIMEOUT);
2361
2362 if (rc1 != IDMAP_SUCCESS)
2363 return (rc1);
2364 return (rc2);
2365 }
2366
2367
2368 /*
2369 * syslog is the default logger.
2370 * It can be overwritten by supplying a logger
2371 * with idmap_set_logger()
2372 */
|