login: re-enable Ctrl-^C before execing shell.
[oweals/busybox.git] / libbb / bb_pwd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * password utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13 #include "libbb.h"
14
15   /* 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
32 /* gets a groupname given a gid */
33 char * bb_getgrgid(char *group, long gid, int bufsize)
34 {
35         struct group *mygroup = getgrgid(gid);
36
37         return bb_getug(group, (mygroup) ?
38                         mygroup->gr_name : (char *)mygroup, gid, bufsize, 'g');
39 }
40
41 /* returns a gid given a group name */
42 long 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 }
52
53 /* returns a uid given a username */
54 long 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 }
64
65  /* Hacked by Tito Ragusa (c) 2004 <farmatito@tiscali.it> to make it more
66   * flexible :
67   *
68   * if bufsize is > 0 char *name cannot be set to NULL.
69   *                   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
82 /* gets a username given a uid */
83 char * 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 }
90
91  /*
92   * if bufsize is > 0 char *buffer cannot be set to NULL.
93   *                   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
105 /* internal function for bb_getpwuid and bb_getgrgid */
106 char * 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 }
119
120 unsigned long get_ug_id(const char *s,
121                 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 }