*
* Copyright (C) 1996 Andrew Tridgell
* Copyright (C) 1996 Paul Mackerras
- * Copyright (C) 2004-2020 Wayne Davison
+ * Copyright (C) 2004-2022 Wayne Davison
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
extern int preserve_acls;
extern int numeric_ids;
extern int xmit_id0_names;
+extern pid_t namecvt_pid;
extern gid_t our_gid;
extern char *usermap;
extern char *groupmap;
static struct idlist *uidlist, *uidmap;
static struct idlist *gidlist, *gidmap;
+static inline int id_eq_uid(id_t id, uid_t uid)
+{
+ return id == (id_t)uid;
+}
+
+static inline int id_eq_gid(id_t id, gid_t gid)
+{
+ return id == (id_t)gid;
+}
+
static id_t id_parse(const char *num_str)
{
id_t tmp, num = 0;
/* turn a uid into a user name */
const char *uid_to_user(uid_t uid)
{
- struct passwd *pass = getpwuid(uid);
- if (pass)
- return strdup(pass->pw_name);
- return NULL;
+ const char *name = NULL;
+
+ if (namecvt_pid) {
+ id_t id = uid;
+ namecvt_call("uid", &name, &id);
+ } else {
+ struct passwd *pass = getpwuid(uid);
+ if (pass)
+ name = strdup(pass->pw_name);
+ }
+
+ return name;
}
/* turn a gid into a group name */
const char *gid_to_group(gid_t gid)
{
- struct group *grp = getgrgid(gid);
- if (grp)
- return strdup(grp->gr_name);
- return NULL;
+ const char *name = NULL;
+
+ if (namecvt_pid) {
+ id_t id = gid;
+ namecvt_call("gid", &name, &id);
+ } else {
+ struct group *grp = getgrgid(gid);
+ if (grp)
+ name = strdup(grp->gr_name);
+ }
+
+ return name;
}
/* Parse a user name or (optionally) a number into a uid */
int user_to_uid(const char *name, uid_t *uid_p, BOOL num_ok)
{
- struct passwd *pass;
if (!name || !*name)
return 0;
+
if (num_ok && name[strspn(name, "0123456789")] == '\0') {
*uid_p = id_parse(name);
return 1;
}
- if (!(pass = getpwnam(name)))
- return 0;
- *uid_p = pass->pw_uid;
+
+ if (namecvt_pid) {
+ id_t id;
+ if (!namecvt_call("usr", &name, &id))
+ return 0;
+ *uid_p = id;
+ } else {
+ struct passwd *pass = getpwnam(name);
+ if (!pass)
+ return 0;
+ *uid_p = pass->pw_uid;
+ }
+
return 1;
}
/* Parse a group name or (optionally) a number into a gid */
int group_to_gid(const char *name, gid_t *gid_p, BOOL num_ok)
{
- struct group *grp;
if (!name || !*name)
return 0;
+
if (num_ok && name[strspn(name, "0123456789")] == '\0') {
*gid_p = id_parse(name);
return 1;
}
- if (!(grp = getgrnam(name)))
- return 0;
- *gid_p = grp->gr_gid;
+
+ if (namecvt_pid) {
+ id_t id;
+ if (!namecvt_call("grp", &name, &id))
+ return 0;
+ *gid_p = id;
+ } else {
+ struct group *grp = getgrnam(name);
+ if (!grp)
+ return 0;
+ *gid_p = grp->gr_gid;
+ }
+
return 1;
}
ngroups = getgroups(ngroups, gidset);
/* The default gid might not be in the list on some systems. */
for (n = 0; n < ngroups; n++) {
- if (gidset[n] == our_gid)
+ if ((gid_t)gidset[n] == our_gid)
break;
}
if (n == ngroups)
last_in = gid;
for (n = 0; n < ngroups; n++) {
- if (gidset[n] == gid)
+ if ((gid_t)gidset[n] == gid)
return last_out = 1;
}
return last_out = 0;
else if (*name && id) {
if (idlist_ptr == &uidlist) {
uid_t uid;
- id2 = user_to_uid(name, &uid, False) ? uid : id;
+ id2 = user_to_uid(name, &uid, False) ? (id_t)uid : id;
} else {
gid_t gid;
- id2 = group_to_gid(name, &gid, False) ? gid : id;
+ id2 = group_to_gid(name, &gid, False) ? (id_t)gid : id;
}
} else
id2 = id;
static struct idlist *last = NULL;
struct idlist *list;
- if (last && uid == last->id)
+ if (last && id_eq_uid(last->id, uid))
return last->id2;
for (list = uidlist; list; list = list->next) {
- if (list->id == uid)
+ if (id_eq_uid(list->id, uid))
break;
}
static struct idlist *last = NULL;
struct idlist *list;
- if (last && gid == last->id)
+ if (last && id_eq_gid(last->id, gid))
list = last;
else {
for (list = gidlist; list; list = list->next) {
- if (list->id == gid)
+ if (id_eq_gid(list->id, gid))
break;
}
if (!list)
union name_or_id noiu;
for (list = uidlist; list; list = list->next) {
- if (list->id == uid)
+ if (id_eq_uid(list->id, uid))
return NULL;
}
union name_or_id noiu;
for (list = gidlist; list; list = list->next) {
- if (list->id == gid)
+ if (id_eq_gid(list->id, gid))
return NULL;
}