Magellan Linux

Annotation of /tags/mkinitrd-6_2_0/libpwdgrp/uidgid_get.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (hide annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (17 years ago) by niro
Original Path: trunk/mkinitrd-magellan/busybox/libpwdgrp/uidgid_get.c
File MIME type: text/plain
File size: 2861 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 niro 532 /*
2     Copyright (c) 2001-2006, Gerrit Pape
3     All rights reserved.
4    
5     Redistribution and use in source and binary forms, with or without
6     modification, 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    
16     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17     WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19     EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21     PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22     OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23     WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25     ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26     */
27    
28     #include "busybox.h"
29    
30     int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
31     {
32     struct passwd *pwd;
33     struct group *gr;
34     char *user, *group;
35     unsigned n;
36    
37     user = (char*)ug;
38     group = strchr(ug, ':');
39     if (group) {
40     int sz = (++group) - ug;
41     user = alloca(sz);
42     /* copies sz-1 bytes, stores terminating '\0' */
43     safe_strncpy(user, ug, sz);
44     }
45     if (numeric_ok) {
46     n = bb_strtou(user, NULL, 10);
47     if (!errno) {
48     u->uid = n;
49     pwd = getpwuid(n);
50     /* If we have e.g. "500" string without user */
51     /* with uid 500 in /etc/passwd, we set gid == uid */
52     u->gid = pwd ? pwd->pw_gid : n;
53     goto skip;
54     }
55     }
56     pwd = getpwnam(user);
57     if (!pwd)
58     return 0;
59     u->uid = pwd->pw_uid;
60     u->gid = pwd->pw_gid;
61    
62     skip:
63     if (group) {
64     if (numeric_ok) {
65     n = bb_strtou(group, NULL, 10);
66     if (!errno) {
67     u->gid = n;
68     return 1;
69     }
70     }
71     gr = getgrnam(group);
72     if (!gr) return 0;
73     u->gid = gr->gr_gid;
74     }
75     return 1;
76     }
77    
78     #if 0
79     #include <stdio.h>
80     int main()
81     {
82     unsigned u;
83     struct bb_uidgid_t ug;
84     u = get_uidgid(&ug, "apache", 0);
85     printf("%u = %u:%u\n", u, ug.uid, ug.gid);
86     ug.uid = ug.gid = 1111;
87     u = get_uidgid(&ug, "apache", 0);
88     printf("%u = %u:%u\n", u, ug.uid, ug.gid);
89     ug.uid = ug.gid = 1111;
90     u = get_uidgid(&ug, "apache:users", 0);
91     printf("%u = %u:%u\n", u, ug.uid, ug.gid);
92     ug.uid = ug.gid = 1111;
93     u = get_uidgid(&ug, "apache:users", 0);
94     printf("%u = %u:%u\n", u, ug.uid, ug.gid);
95     return 0;
96     }
97     #endif