aboutsummaryrefslogtreecommitdiff
path: root/utility.c
diff options
context:
space:
mode:
Diffstat (limited to 'utility.c')
-rw-r--r--utility.c57
1 files changed, 42 insertions, 15 deletions
diff --git a/utility.c b/utility.c
index e6c87fc72..51c51333c 100644
--- a/utility.c
+++ b/utility.c
@@ -783,18 +783,27 @@ extern int parse_mode(const char *s, mode_t * theMode)
783 783
784 784
785 785
786#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR || defined BB_ID
786 787
787 788/* This parses entries in /etc/passwd and /etc/group. This is desirable
788#if defined BB_CHMOD_CHOWN_CHGRP || defined BB_PS || defined BB_LS || defined BB_TAR 789 * for BusyBox, since we want to avoid using the glibc NSS stuff, which
789 790 * increases target size and is often not needed or wanted for embedded
790/* Use this to avoid needing the glibc NSS stuff 791 * systems.
791 * This uses storage buf to hold things. 792 *
792 * */ 793 * /etc/passwd entries look like this:
793uid_t my_getid(const char *filename, char *name, uid_t id) 794 * root:x:0:0:root:/root:/bin/bash
795 * and /etc/group entries look like this:
796 * root:x:0:
797 *
798 * This uses buf as storage to hold things.
799 *
800 */
801uid_t my_getid(const char *filename, char *name, uid_t id, gid_t *gid)
794{ 802{
795 FILE *file; 803 FILE *file;
796 char *rname, *start, *end, buf[128]; 804 char *rname, *start, *end, buf[128];
797 uid_t rid; 805 id_t rid;
806 gid_t rgid = 0;
798 807
799 file = fopen(filename, "r"); 808 file = fopen(filename, "r");
800 if (file == NULL) { 809 if (file == NULL) {
@@ -806,6 +815,7 @@ uid_t my_getid(const char *filename, char *name, uid_t id)
806 if (buf[0] == '#') 815 if (buf[0] == '#')
807 continue; 816 continue;
808 817
818 /* username/group name */
809 start = buf; 819 start = buf;
810 end = strchr(start, ':'); 820 end = strchr(start, ':');
811 if (end == NULL) 821 if (end == NULL)
@@ -813,24 +823,32 @@ uid_t my_getid(const char *filename, char *name, uid_t id)
813 *end = '\0'; 823 *end = '\0';
814 rname = start; 824 rname = start;
815 825
826 /* password */
816 start = end + 1; 827 start = end + 1;
817 end = strchr(start, ':'); 828 end = strchr(start, ':');
818 if (end == NULL) 829 if (end == NULL)
819 continue; 830 continue;
820 831
832 /* uid in passwd, gid in group */
821 start = end + 1; 833 start = end + 1;
822 rid = (uid_t) strtol(start, &end, 10); 834 rid = (uid_t) strtol(start, &end, 10);
823 if (end == start) 835 if (end == start)
824 continue; 836 continue;
825 837
838 /* gid in passwd */
839 start = end + 1;
840 rgid = (gid_t) strtol(start, &end, 10);
841
826 if (name) { 842 if (name) {
827 if (0 == strcmp(rname, name)) { 843 if (0 == strcmp(rname, name)) {
844 if (gid) *gid = rgid;
828 fclose(file); 845 fclose(file);
829 return (rid); 846 return (rid);
830 } 847 }
831 } 848 }
832 if (id != -1 && id == rid) { 849 if (id != -1 && id == rid) {
833 strncpy(name, rname, 8); 850 strncpy(name, rname, 8);
851 if (gid) *gid = rgid;
834 fclose(file); 852 fclose(file);
835 return (TRUE); 853 return (TRUE);
836 } 854 }
@@ -839,30 +857,39 @@ uid_t my_getid(const char *filename, char *name, uid_t id)
839 return (-1); 857 return (-1);
840} 858}
841 859
860/* returns a uid given a username */
842uid_t my_getpwnam(char *name) 861uid_t my_getpwnam(char *name)
843{ 862{
844 return my_getid("/etc/passwd", name, -1); 863 return my_getid("/etc/passwd", name, -1, NULL);
845} 864}
846 865
866/* returns a gid given a group name */
847gid_t my_getgrnam(char *name) 867gid_t my_getgrnam(char *name)
848{ 868{
849 return my_getid("/etc/group", name, -1); 869 return my_getid("/etc/group", name, -1, NULL);
850} 870}
851 871
872/* gets a username given a uid */
852void my_getpwuid(char *name, uid_t uid) 873void my_getpwuid(char *name, uid_t uid)
853{ 874{
854 my_getid("/etc/passwd", name, uid); 875 my_getid("/etc/passwd", name, uid, NULL);
855} 876}
856 877
878/* gets a groupname given a gid */
857void my_getgrgid(char *group, gid_t gid) 879void my_getgrgid(char *group, gid_t gid)
858{ 880{
859 my_getid("/etc/group", group, gid); 881 my_getid("/etc/group", group, gid, NULL);
860} 882}
861 883
884/* gets a gid given a user name */
885gid_t my_getpwnamegid(char *name)
886{
887 gid_t gid;
888 my_getid("/etc/passwd", name, -1, &gid);
889 return gid;
890}
862 891
863#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR */ 892#endif /* BB_CHMOD_CHOWN_CHGRP || BB_PS || BB_LS || BB_TAR || BB_ID */
864
865
866 893
867 894
868#if (defined BB_CHVT) || (defined BB_DEALLOCVT) 895#if (defined BB_CHVT) || (defined BB_DEALLOCVT)