]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/user-util.h
execute: split out creation of runtime dirs into its own functions
[thirdparty/systemd.git] / src / basic / user-util.h
CommitLineData
b1d4f8e1
LP
1#pragma once
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
b1d4f8e1 22#include <stdbool.h>
61755fda 23#include <stdint.h>
71d35b6b 24#include <sys/types.h>
ccabee0d 25#include <unistd.h>
b1d4f8e1
LP
26
27bool uid_is_valid(uid_t uid);
28
29static inline bool gid_is_valid(gid_t gid) {
30 return uid_is_valid((uid_t) gid);
31}
32
33int parse_uid(const char *s, uid_t* ret_uid);
34
35static inline int parse_gid(const char *s, gid_t *ret_gid) {
36 return parse_uid(s, (uid_t*) ret_gid);
37}
38
b1d4f8e1
LP
39char* getlogname_malloc(void);
40char* getusername_malloc(void);
41
42int get_user_creds(const char **username, uid_t *uid, gid_t *gid, const char **home, const char **shell);
43int get_group_creds(const char **groupname, gid_t *gid);
44
45char* uid_to_name(uid_t uid);
46char* gid_to_name(gid_t gid);
47
48int in_gid(gid_t gid);
49int in_group(const char *name);
50
51int get_home_dir(char **ret);
52int get_shell(char **_ret);
53
54int reset_uid_gid(void);
e929bee0
LP
55
56int take_etc_passwd_lock(const char *root);
ee104e11
LP
57
58#define UID_INVALID ((uid_t) -1)
59#define GID_INVALID ((gid_t) -1)
60
61755fda
ZJS
61/* Let's pick a UIDs within the 16bit range, so that we are compatible with containers using 16bit
62 * user namespacing. At least on Fedora normal users are allocated until UID 60000, hence do not
63 * allocate from below this. Also stay away from the upper end of the range as that is often used
64 * for overflow/nobody users. */
65#define DYNAMIC_UID_MIN ((uid_t) UINT32_C(0x0000EF00))
66#define DYNAMIC_UID_MAX ((uid_t) UINT32_C(0x0000FFEF))
67
68static inline bool uid_is_dynamic(uid_t uid) {
69 return DYNAMIC_UID_MIN <= uid && uid <= DYNAMIC_UID_MAX;
70}
71
72/* The following macros add 1 when converting things, since UID 0 is a valid UID, while the pointer
73 * NULL is special */
ee104e11
LP
74#define PTR_TO_UID(p) ((uid_t) (((uintptr_t) (p))-1))
75#define UID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
76
77#define PTR_TO_GID(p) ((gid_t) (((uintptr_t) (p))-1))
78#define GID_TO_PTR(u) ((void*) (((uintptr_t) (u))+1))
ccabee0d
LP
79
80static inline bool userns_supported(void) {
81 return access("/proc/self/uid_map", F_OK) >= 0;
82}
e4631b48
LP
83
84bool valid_user_group_name(const char *u);
85bool valid_user_group_name_or_id(const char *u);
86bool valid_gecos(const char *d);
87bool valid_home(const char *p);