blob: 32ff83a423e33bb94cb073f5e197bf02edc72004 [file] [log] [blame]
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +00001/* vi: set sw=4 ts=4: */
2/*
3 * password utility routines.
4 *
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenko0c68a872008-12-02 22:56:59 +00006 * Copyright (C) 2008 by Tito Ragusa <farmatito@tiscali.it>
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +00009 */
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000010#include "libbb.h"
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000011
Denis Vlasenko0c68a872008-12-02 22:56:59 +000012/* TODO: maybe change API to return malloced data?
13 * This will allow to stop using libc functions returning
14 * pointers to static data (getpwuid)
Denis Vlasenko3734b942007-07-27 11:20:10 +000015 */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000016
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000017struct passwd* FAST_FUNC xgetpwnam(const char *name)
18{
19 struct passwd *pw = getpwnam(name);
20 if (!pw)
21 bb_error_msg_and_die("unknown user %s", name);
22 return pw;
23}
24
Denis Vlasenko15437e32008-12-05 16:23:06 +000025struct group* FAST_FUNC xgetgrnam(const char *name)
26{
27 struct group *gr = getgrnam(name);
28 if (!gr)
29 bb_error_msg_and_die("unknown group %s", name);
30 return gr;
31}
32
Denis Vlasenko0c68a872008-12-02 22:56:59 +000033struct passwd* FAST_FUNC xgetpwuid(uid_t uid)
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000034{
Denys Vlasenko39194f02017-08-03 19:00:01 +020035 /* Note: used in nofork applets (whoami), be careful not to leak anything */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000036 struct passwd *pw = getpwuid(uid);
37 if (!pw)
38 bb_error_msg_and_die("unknown uid %u", (unsigned)uid);
39 return pw;
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000040}
41
Denis Vlasenko0c68a872008-12-02 22:56:59 +000042struct group* FAST_FUNC xgetgrgid(gid_t gid)
Denis Vlasenko3734b942007-07-27 11:20:10 +000043{
Denis Vlasenko0c68a872008-12-02 22:56:59 +000044 struct group *gr = getgrgid(gid);
45 if (!gr)
46 bb_error_msg_and_die("unknown gid %u", (unsigned)gid);
47 return gr;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000048}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000049
Denis Vlasenko0c68a872008-12-02 22:56:59 +000050char* FAST_FUNC xuid2uname(uid_t uid)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000051{
Denys Vlasenko39194f02017-08-03 19:00:01 +020052 /* Note: used in nofork applets (whoami), be careful not to leak anything */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000053 struct passwd *pw = xgetpwuid(uid);
54 return pw->pw_name;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000055}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000056
Denis Vlasenko0c68a872008-12-02 22:56:59 +000057char* FAST_FUNC xgid2group(gid_t gid)
58{
59 struct group *gr = xgetgrgid(gid);
60 return gr->gr_name;
61}
62
63char* FAST_FUNC uid2uname(uid_t uid)
64{
65 struct passwd *pw = getpwuid(uid);
66 return (pw) ? pw->pw_name : NULL;
67}
68
69char* FAST_FUNC gid2group(gid_t gid)
70{
71 struct group *gr = getgrgid(gid);
72 return (gr) ? gr->gr_name : NULL;
73}
74
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010075char* FAST_FUNC uid2uname_utoa(uid_t uid)
Denis Vlasenko0c68a872008-12-02 22:56:59 +000076{
77 char *name = uid2uname(uid);
78 return (name) ? name : utoa(uid);
79}
80
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010081char* FAST_FUNC gid2group_utoa(gid_t gid)
Denis Vlasenko0c68a872008-12-02 22:56:59 +000082{
83 char *name = gid2group(gid);
84 return (name) ? name : utoa(gid);
85}
86
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000087long FAST_FUNC xuname2uid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000088{
89 struct passwd *myuser;
90
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000091 myuser = xgetpwnam(name);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000092 return myuser->pw_uid;
93}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000094
Denis Vlasenko0c68a872008-12-02 22:56:59 +000095long FAST_FUNC xgroup2gid(const char *name)
96{
97 struct group *mygroup;
98
Denis Vlasenko15437e32008-12-05 16:23:06 +000099 mygroup = xgetgrnam(name);
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000100 return mygroup->gr_gid;
101}
102
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000103unsigned long FAST_FUNC get_ug_id(const char *s,
104 long FAST_FUNC (*xname2id)(const char *))
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000105{
106 unsigned long r;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000107
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000108 r = bb_strtoul(s, NULL, 10);
109 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000110 return xname2id(s);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000111 return r;
112}