blob: 1776fa4f6a58b75c51c6b963154b71cdcdad6970 [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 <stdio.h>
11#include <string.h>
12#include <assert.h>
13#include "libbb.h"
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000014
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000015 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
16 * flexible :
17 *
18 * if bufsize is > 0 char *group cannot be set to NULL.
19 * On success groupname is written on static allocated buffer
20 * group (and a pointer to it is returned).
21 * On failure gid as string is written to static allocated
22 * buffer group and NULL is returned.
23 * if bufsize is = 0 char *group can be set to NULL.
24 * On success groupname is returned.
25 * On failure NULL is returned.
26 * if bufsize is < 0 char *group can be set to NULL.
27 * On success groupname is returned.
28 * On failure an error message is printed and
29 * the program exits.
30 */
31
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000032/* gets a groupname given a gid */
33char * bb_getgrgid(char *group, long gid, int bufsize)
34{
35 struct group *mygroup = getgrgid(gid);
36
Denis Vlasenkocf944462006-10-03 19:02:20 +000037 return bb_getug(group, (mygroup) ?
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000038 mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g');
39}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000040
41/* returns a gid given a group name */
42long bb_xgetgrnam(const char *name)
43{
44 struct group *mygroup;
45
46 mygroup = getgrnam(name);
47 if (mygroup==NULL)
48 bb_error_msg_and_die("unknown group name: %s", name);
49
50 return (mygroup->gr_gid);
51}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000052
53/* returns a uid given a username */
54long bb_xgetpwnam(const char *name)
55{
56 struct passwd *myuser;
57
58 myuser = getpwnam(name);
59 if (myuser==NULL)
60 bb_error_msg_and_die("unknown user name: %s", name);
61
62 return myuser->pw_uid;
63}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000064
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000065 /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
66 * flexible :
67 *
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000068 * if bufsize is > 0 char *name cannot be set to NULL.
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000069 * On success username is written on the static allocated
70 * buffer name (and a pointer to it is returned).
71 * On failure uid as string is written to the static
72 * allocated buffer name and NULL is returned.
73 * if bufsize is = 0 char *name can be set to NULL.
74 * On success username is returned.
75 * On failure NULL is returned.
76 * if bufsize is < 0 char *name can be set to NULL
77 * On success username is returned.
78 * On failure an error message is printed and
79 * the program exits.
80 */
81
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000082/* gets a username given a uid */
83char * bb_getpwuid(char *name, long uid, int bufsize)
84{
85 struct passwd *myuser = getpwuid(uid);
86
87 return bb_getug(name, (myuser) ?
88 myuser->pw_name : (char *)myuser , uid, bufsize, 'u');
89}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000090
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000091 /*
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000092 * if bufsize is > 0 char *buffer cannot be set to NULL.
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +000093 * If idname is not NULL it is written on the static
94 * allocated buffer (and a pointer to it is returned).
95 * if idname is NULL, id as string is written to the static
96 * allocated buffer and NULL is returned.
97 * if bufsize is = 0 char *buffer can be set to NULL.
98 * If idname exists a pointer to it is returned,
99 * else NULL is returned.
100 * if bufsize is < 0 char *buffer can be set to NULL.
101 * If idname exists a pointer to it is returned,
102 * else an error message is printed and the program exits.
103 */
104
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000105/* internal function for bb_getpwuid and bb_getgrgid */
106char * bb_getug(char *buffer, char *idname, long id, int bufsize, char prefix)
107{
108 if(bufsize > 0 ) {
109 assert(buffer!=NULL);
110 if(idname) {
111 return safe_strncpy(buffer, idname, bufsize);
112 }
113 snprintf(buffer, bufsize, "%ld", id);
114 } else if(bufsize < 0 && !idname) {
115 bb_error_msg_and_die("unknown %cid %ld", prefix, id);
116 }
117 return idname;
118}
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000119
Rob Landleyca087712006-03-29 16:52:56 +0000120unsigned long get_ug_id(const char *s,
Bernhard Reutner-Fischerc5280e82005-09-20 21:09:31 +0000121 long (*__bb_getxxnam)(const char *))
122{
123 unsigned long r;
124 char *p;
125
126 r = strtoul(s, &p, 10);
127 if (*p || (s == p)) {
128 r = __bb_getxxnam(s);
129 }
130
131 return r;
132}