blob: 3af1994d59ee9d806474eb327704d3e33be48d6d [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
14/*
15 * if bufsize is > 0 char *buffer cannot be set to NULL.
16 * If idname is not NULL it is written on the static
17 * allocated buffer (and a pointer to it is returned).
18 * if idname is NULL, id as string is written to the static
19 * allocated buffer and NULL is returned.
20 * if bufsize is = 0 char *buffer can be set to NULL.
21 * If idname exists a pointer to it is returned,
22 * else NULL is returned.
23 * if bufsize is < 0 char *buffer can be set to NULL.
24 * If idname exists a pointer to it is returned,
25 * else an error message is printed and the program exits.
26 */
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000027
28/* internal function for bb_getpwuid and bb_getgrgid */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000029static char* bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000030{
Denis Vlasenko219d14d2007-03-24 15:40:16 +000031 if (bufsize > 0) {
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000032 assert(buffer != NULL);
33 if (idname) {
Denis Vlasenkobecd8c52006-12-01 21:34:20 +000034 return safe_strncpy(buffer, idname, bufsize);
35 }
36 snprintf(buffer, bufsize, "%ld", id);
37 } else if (bufsize < 0 && !idname) {
38 bb_error_msg_and_die("unknown %cid %ld", prefix, id);
39 }
40 return idname;
41}
42
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000043/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
44 * flexible :
45 *
46 * if bufsize is > 0 char *group cannot be set to NULL.
47 * On success groupname is written on static allocated buffer
48 * group (and a pointer to it is returned).
49 * On failure gid as string is written to static allocated
50 * buffer group and NULL is returned.
51 * if bufsize is = 0 char *group can be set to NULL.
52 * On success groupname is returned.
53 * On failure NULL is returned.
54 * if bufsize is < 0 char *group can be set to NULL.
55 * On success groupname is returned.
56 * On failure an error message is printed and
57 * the program exits.
58 */
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000059
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000060/* gets a groupname given a gid */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000061char* bb_getgrgid(char *group, long gid, int bufsize)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000062{
63 struct group *mygroup = getgrgid(gid);
64
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000065 return bb_getug(group,
66 mygroup ? mygroup->gr_name : (char *)mygroup,
67 gid, bufsize, 'g');
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000068}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000069
70/* returns a gid given a group name */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000071long xgroup2gid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000072{
73 struct group *mygroup;
74
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000075 mygroup = getgrnam(name);
76 if (mygroup == NULL)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000077 bb_error_msg_and_die("unknown group name: %s", name);
78
Denis Vlasenkod9e15f22006-11-27 16:49:55 +000079 return mygroup->gr_gid;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000080}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000081
82/* returns a uid given a username */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000083long xuname2uid(const char *name)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000084{
85 struct passwd *myuser;
86
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000087 myuser = getpwnam(name);
88 if (myuser == NULL)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000089 bb_error_msg_and_die("unknown user name: %s", name);
90
91 return myuser->pw_uid;
92}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000093
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +000094/* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
95 * flexible :
96 *
97 * if bufsize is > 0 char *name cannot be set to NULL.
98 * On success username is written on the static allocated
99 * buffer name (and a pointer to it is returned).
100 * On failure uid as string is written to the static
101 * allocated buffer name and NULL is returned.
102 * if bufsize is = 0 char *name can be set to NULL.
103 * On success username is returned.
104 * On failure NULL is returned.
105 * if bufsize is < 0 char *name can be set to NULL
106 * On success username is returned.
107 * On failure an error message is printed and
108 * the program exits.
109 */
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000110
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000111/* gets a username given a uid */
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000112char* bb_getpwuid(char *name, long uid, int bufsize)
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000113{
114 struct passwd *myuser = getpwuid(uid);
115
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000116 return bb_getug(name, myuser ? myuser->pw_name : (char *)myuser,
117 uid, bufsize, 'u');
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000118}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000119
Rob Landleyca087712006-03-29 16:52:56 +0000120unsigned long get_ug_id(const char *s,
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000121 long (*xname2id)(const char *))
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000122{
123 unsigned long r;
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000124
Denis Vlasenkobecd8c52006-12-01 21:34:20 +0000125 r = bb_strtoul(s, NULL, 10);
126 if (errno)
Denis Vlasenko9a44c4f2006-12-28 05:44:47 +0000127 return xname2id(s);
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000128 return r;
129}