source: MondoRescue/branches/2.2.5/mindi-busybox/libpwdgrp/uidgid_get.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

  • Property svn:eol-style set to native
File size: 3.7 KB
RevLine 
[1765]1/*
2Copyright (c) 2001-2006, Gerrit Pape
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include "libbb.h"
29
30/* Always sets uid and gid */
31int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
32{
33 struct passwd *pwd;
34 struct group *gr;
35 char *user, *group;
36 unsigned n;
37
38 user = (char*)ug;
39 group = strchr(ug, ':');
40 if (group) {
41 int sz = (++group) - ug;
42 user = alloca(sz);
43 /* copies sz-1 bytes, stores terminating '\0' */
44 safe_strncpy(user, ug, sz);
45 }
46 if (numeric_ok) {
47 n = bb_strtou(user, NULL, 10);
48 if (!errno) {
49 u->uid = n;
50 pwd = getpwuid(n);
51 /* If we have e.g. "500" string without user */
52 /* with uid 500 in /etc/passwd, we set gid == uid */
53 u->gid = pwd ? pwd->pw_gid : n;
54 goto skip;
55 }
56 }
57 /* Either it is not numeric, or caller disallows numeric username */
58 pwd = getpwnam(user);
59 if (!pwd)
60 return 0;
61 u->uid = pwd->pw_uid;
62 u->gid = pwd->pw_gid;
63
64 skip:
65 if (group) {
66 if (numeric_ok) {
67 n = bb_strtou(group, NULL, 10);
68 if (!errno) {
69 u->gid = n;
70 return 1;
71 }
72 }
73 gr = getgrnam(group);
74 if (!gr) return 0;
75 u->gid = gr->gr_gid;
76 }
77 return 1;
78}
79
80/* chown-like:
81 * "user" sets uid only,
82 * ":group" sets gid only
83 * "user:" sets uid and gid (to user's primary group id)
84 * "user:group" sets uid and gid
85 * ('unset' uid or gid is actually set to -1)
86 */
87void parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
88{
89 char *group;
90
91 u->uid = -1;
92 u->gid = -1;
93
94 /* Check if there is a group name */
95 group = strchr(user_group, '.'); /* deprecated? */
96 if (!group)
97 group = strchr(user_group, ':');
98 else
99 *group = ':'; /* replace '.' with ':' */
100
101 /* Parse "user[:[group]]" */
102 if (!group) { /* "user" */
103 u->uid = get_ug_id(user_group, xuname2uid);
104 } else if (group == user_group) { /* ":group" */
105 u->gid = get_ug_id(group + 1, xgroup2gid);
106 } else {
107 if (!group[1]) /* "user:" */
108 *group = '\0';
109 if (!get_uidgid(u, user_group, 1))
110 bb_error_msg_and_die("unknown user/group %s", user_group);
111 }
112}
113
114#if 0
115#include <stdio.h>
116int main()
117{
118 unsigned u;
119 struct bb_uidgid_t ug;
120 u = get_uidgid(&ug, "apache", 0);
121 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
122 ug.uid = ug.gid = 1111;
123 u = get_uidgid(&ug, "apache", 0);
124 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
125 ug.uid = ug.gid = 1111;
126 u = get_uidgid(&ug, "apache:users", 0);
127 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
128 ug.uid = ug.gid = 1111;
129 u = get_uidgid(&ug, "apache:users", 0);
130 printf("%u = %u:%u\n", u, ug.uid, ug.gid);
131 return 0;
132}
133#endif
Note: See TracBrowser for help on using the repository browser.