ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
-#include "busybox.h"
+#include "libbb.h"
-int get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
+/* Always sets uid and gid */
+int FAST_FUNC get_uidgid(struct bb_uidgid_t *u, const char *ug, int numeric_ok)
{
struct passwd *pwd;
struct group *gr;
goto skip;
}
}
+ /* Either it is not numeric, or caller disallows numeric username */
pwd = getpwnam(user);
if (!pwd)
return 0;
}
}
gr = getgrnam(group);
- if (!gr) return 0;
+ if (!gr)
+ return 0;
u->gid = gr->gr_gid;
}
return 1;
}
+void FAST_FUNC xget_uidgid(struct bb_uidgid_t *u, const char *ug)
+{
+ if (!get_uidgid(u, ug, 1))
+ bb_error_msg_and_die("unknown user/group %s", ug);
+}
+
+/* chown-like:
+ * "user" sets uid only,
+ * ":group" sets gid only
+ * "user:" sets uid and gid (to user's primary group id)
+ * "user:group" sets uid and gid
+ * ('unset' uid or gid retains the value it has on entry)
+ */
+void FAST_FUNC parse_chown_usergroup_or_die(struct bb_uidgid_t *u, char *user_group)
+{
+ char *group;
+
+ /* Check if there is a group name */
+ group = strchr(user_group, '.'); /* deprecated? */
+ if (!group)
+ group = strchr(user_group, ':');
+ else
+ *group = ':'; /* replace '.' with ':' */
+
+ /* Parse "user[:[group]]" */
+ if (!group) { /* "user" */
+ u->uid = get_ug_id(user_group, xuname2uid);
+ } else if (group == user_group) { /* ":group" */
+ u->gid = get_ug_id(group + 1, xgroup2gid);
+ } else {
+ if (!group[1]) /* "user:" */
+ *group = '\0';
+ xget_uidgid(u, user_group);
+ }
+}
#if 0
#include <stdio.h>