blob: b03dc83996d838ca7b0dbd67a83568a2a9531391 [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>
6 *
Bernhard Reutner-Fischer7fee0c42006-09-13 16:39:19 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +00008 */
9
Denis Vlasenko7d219aa2006-10-05 10:17:08 +000010#include "libbb.h"
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000011
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000012#define assert(x) ((void)0)
13
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000014/* internal function for bb_getpwuid and bb_getgrgid */
Denis Vlasenko3734b942007-07-27 11:20:10 +000015/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
16 * flexible:
17 *
18 * bufsize > 0: If idname is not NULL it is copied to buffer,
19 * and buffer is returned. Else id as string is written
20 * to buffer, and NULL is returned.
21 *
22 * bufsize == 0: idname is returned.
23 *
24 * bufsize < 0: If idname is not NULL it is returned.
25 * Else an error message is printed and the program exits.
26 */
27static char* bb_getug(char *buffer, int bufsize, char *idname, long id, char prefix)
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000028{
Denis Vlasenko219d14d2007-03-24 15:40:16 +000029 if (bufsize > 0) {
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000030 assert(buffer != NULL);
31 if (idname) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000032 return safe_strncpy(buffer, idname, bufsize);
33 }
34 snprintf(buffer, bufsize, "%ld", id);
35 } else if (bufsize < 0 && !idname) {
36 bb_error_msg_and_die("unknown %cid %ld", prefix, id);
37 }
38 return idname;
39}
40
Denis Vlasenko3734b942007-07-27 11:20:10 +000041/* bb_getpwuid, bb_getgrgid:
42 * bb_getXXXid(buf, bufsz, id) - copy user/group name or id
43 * as a string to buf, return user/group name or NULL
44 * bb_getXXXid(NULL, 0, id) - return user/group name or NULL
45 * bb_getXXXid(NULL, -1, id) - return user/group name or exit
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000046 */
Denis Vlasenko3734b942007-07-27 11:20:10 +000047/* gets a username given a uid */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000048char* FAST_FUNC bb_getpwuid(char *name, int bufsize, long uid)
Denis Vlasenko3734b942007-07-27 11:20:10 +000049{
50 struct passwd *myuser = getpwuid(uid);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000051
Denis Vlasenko3734b942007-07-27 11:20:10 +000052 return bb_getug(name, bufsize,
53 (myuser ? myuser->pw_name : (char*)myuser),
54 uid, 'u');
55}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000056/* gets a groupname given a gid */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000057char* FAST_FUNC bb_getgrgid(char *group, int bufsize, long gid)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000058{
59 struct group *mygroup = getgrgid(gid);
60
Denis Vlasenko3734b942007-07-27 11:20:10 +000061 return bb_getug(group, bufsize,
62 (mygroup ? mygroup->gr_name : (char*)mygroup),
63 gid, 'g');
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000064}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000065
66/* returns a gid given a group name */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000067long FAST_FUNC xgroup2gid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000068{
69 struct group *mygroup;
70
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000071 mygroup = getgrnam(name);
72 if (mygroup == NULL)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000073 bb_error_msg_and_die("unknown group name: %s", name);
74
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000075 return mygroup->gr_gid;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000076}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000077
78/* returns a uid given a username */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000079long FAST_FUNC xuname2uid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000080{
81 struct passwd *myuser;
82
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000083 myuser = getpwnam(name);
84 if (myuser == NULL)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000085 bb_error_msg_and_die("unknown user name: %s", name);
86
87 return myuser->pw_uid;
88}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000089
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000090unsigned long FAST_FUNC get_ug_id(const char *s,
91 long FAST_FUNC (*xname2id)(const char *))
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000092{
93 unsigned long r;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000094
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000095 r = bb_strtoul(s, NULL, 10);
96 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000097 return xname2id(s);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000098 return r;
99}