blob: dca0a150bea3fb1bfddb15d6303706cd793f1a2f [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 */
10
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000011#include "libbb.h"
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000012
Denis Vlasenko0c68a872008-12-02 22:56:59 +000013/* TODO: maybe change API to return malloced data?
14 * This will allow to stop using libc functions returning
15 * pointers to static data (getpwuid)
Denis Vlasenko3734b942007-07-27 11:20:10 +000016 */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000017
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000018struct passwd* FAST_FUNC xgetpwnam(const char *name)
19{
20 struct passwd *pw = getpwnam(name);
21 if (!pw)
22 bb_error_msg_and_die("unknown user %s", name);
23 return pw;
24}
25
Denis Vlasenko15437e32008-12-05 16:23:06 +000026struct group* FAST_FUNC xgetgrnam(const char *name)
27{
28 struct group *gr = getgrnam(name);
29 if (!gr)
30 bb_error_msg_and_die("unknown group %s", name);
31 return gr;
32}
33
Denis Vlasenko0c68a872008-12-02 22:56:59 +000034struct passwd* FAST_FUNC xgetpwuid(uid_t uid)
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000035{
Denys Vlasenko39194f02017-08-03 19:00:01 +020036 /* Note: used in nofork applets (whoami), be careful not to leak anything */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000037 struct passwd *pw = getpwuid(uid);
38 if (!pw)
39 bb_error_msg_and_die("unknown uid %u", (unsigned)uid);
40 return pw;
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000041}
42
Denis Vlasenko0c68a872008-12-02 22:56:59 +000043struct group* FAST_FUNC xgetgrgid(gid_t gid)
Denis Vlasenko3734b942007-07-27 11:20:10 +000044{
Denis Vlasenko0c68a872008-12-02 22:56:59 +000045 struct group *gr = getgrgid(gid);
46 if (!gr)
47 bb_error_msg_and_die("unknown gid %u", (unsigned)gid);
48 return gr;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000049}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000050
Denis Vlasenko0c68a872008-12-02 22:56:59 +000051char* FAST_FUNC xuid2uname(uid_t uid)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000052{
Denys Vlasenko39194f02017-08-03 19:00:01 +020053 /* Note: used in nofork applets (whoami), be careful not to leak anything */
Denis Vlasenko0c68a872008-12-02 22:56:59 +000054 struct passwd *pw = xgetpwuid(uid);
55 return pw->pw_name;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000056}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000057
Denis Vlasenko0c68a872008-12-02 22:56:59 +000058char* FAST_FUNC xgid2group(gid_t gid)
59{
60 struct group *gr = xgetgrgid(gid);
61 return gr->gr_name;
62}
63
64char* FAST_FUNC uid2uname(uid_t uid)
65{
66 struct passwd *pw = getpwuid(uid);
67 return (pw) ? pw->pw_name : NULL;
68}
69
70char* FAST_FUNC gid2group(gid_t gid)
71{
72 struct group *gr = getgrgid(gid);
73 return (gr) ? gr->gr_name : NULL;
74}
75
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010076char* FAST_FUNC uid2uname_utoa(uid_t uid)
Denis Vlasenko0c68a872008-12-02 22:56:59 +000077{
78 char *name = uid2uname(uid);
79 return (name) ? name : utoa(uid);
80}
81
Denys Vlasenko7d65abe2011-03-01 16:27:13 +010082char* FAST_FUNC gid2group_utoa(gid_t gid)
Denis Vlasenko0c68a872008-12-02 22:56:59 +000083{
84 char *name = gid2group(gid);
85 return (name) ? name : utoa(gid);
86}
87
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000088long FAST_FUNC xuname2uid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000089{
90 struct passwd *myuser;
91
Denis Vlasenkod7a805e2008-12-03 19:05:55 +000092 myuser = xgetpwnam(name);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000093 return myuser->pw_uid;
94}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000095
Denis Vlasenko0c68a872008-12-02 22:56:59 +000096long FAST_FUNC xgroup2gid(const char *name)
97{
98 struct group *mygroup;
99
Denis Vlasenko15437e32008-12-05 16:23:06 +0000100 mygroup = xgetgrnam(name);
Denis Vlasenko0c68a872008-12-02 22:56:59 +0000101 return mygroup->gr_gid;
102}
103
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000104unsigned long FAST_FUNC get_ug_id(const char *s,
105 long FAST_FUNC (*xname2id)(const char *))
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000106{
107 unsigned long r;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000108
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000109 r = bb_strtoul(s, NULL, 10);
110 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000111 return xname2id(s);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000112 return r;
113}