busybox/libpwdgrp/uidgid_get.c
<<
>>
Prefs
   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 FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug)
  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        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        /* it is not numeric */
  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                n = bb_strtou(group, NULL, 10);
  65                if (!errno) {
  66                        u->gid = n;
  67                        return 1;
  68                }
  69                gr = getgrnam(group);
  70                if (!gr)
  71                        return 0;
  72                u->gid = gr->gr_gid;
  73        }
  74        return 1;
  75}
  76void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
  77{
  78        if (!get_uidgid(u, ug))
  79                bb_error_msg_and_die("unknown user/group %s", ug);
  80}
  81
  82/* chown-like:
  83 * "user" sets uid only,
  84 * ":group" sets gid only
  85 * "user:" sets uid and gid (to user's primary group id)
  86 * "user:group" sets uid and gid
  87 * ('unset' uid or gid retains the value it has on entry)
  88 */
  89void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
  90{
  91        char *group;
  92
  93        u->uid = u->gid = (gid_t)-1L;
  94
  95        /* Check if there is a group name */
  96        group = strchr(user_group, ':');
  97
  98        /* Parse "user[:[group]]" */
  99        if (!group) { /* "user" */
 100                u->uid = get_ug_id(user_group, xuname2uid);
 101        } else if (group == user_group) { /* ":group" */
 102                u->gid = get_ug_id(group + 1, xgroup2gid);
 103        } else {
 104                if (!group[1]) /* "user:" */
 105                        *group = '\0';
 106                xget_uidgid(u, user_group);
 107        }
 108}
 109
 110#if 0
 111#include <stdio.h>
 112int main()
 113{
 114        unsigned u;
 115        struct bb_uidgid_t ug;
 116        u = get_uidgid(&ug, "apache");
 117        printf("%u = %u:%u\n", u, ug.uid, ug.gid);
 118        ug.uid = ug.gid = 1111;
 119        u = get_uidgid(&ug, "apache");
 120        printf("%u = %u:%u\n", u, ug.uid, ug.gid);
 121        ug.uid = ug.gid = 1111;
 122        u = get_uidgid(&ug, "apache:users");
 123        printf("%u = %u:%u\n", u, ug.uid, ug.gid);
 124        ug.uid = ug.gid = 1111;
 125        u = get_uidgid(&ug, "apache:users");
 126        printf("%u = %u:%u\n", u, ug.uid, ug.gid);
 127        return 0;
 128}
 129#endif
 130