aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-01-02 10:25:11 +0000
committerRon Yorston <rmy@pobox.com>2014-01-02 10:25:11 +0000
commitb8f278ee745778806118f57fb7884d205eba05ad (patch)
treefef237f6dd302c59918cf389a60c120e58d3e086 /libbb
parent3fd34651ea72ea1c335d3170f234cb0517fd897f (diff)
parent57434022cefde87133b8ad39fb3b79c1274e7684 (diff)
downloadbusybox-w32-b8f278ee745778806118f57fb7884d205eba05ad.tar.gz
busybox-w32-b8f278ee745778806118f57fb7884d205eba05ad.tar.bz2
busybox-w32-b8f278ee745778806118f57fb7884d205eba05ad.zip
Merge branch 'busybox' into merge
Conflicts: archival/Config.src shell/ash.c
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild.src2
-rw-r--r--libbb/bb_pwd.c48
-rw-r--r--libbb/correct_password.c23
-rw-r--r--libbb/fclose_nonstdin.c3
-rw-r--r--libbb/human_readable.c6
-rw-r--r--libbb/in_ether.c1
-rw-r--r--libbb/inet_common.c3
-rw-r--r--libbb/nuke_str.c21
-rw-r--r--libbb/pw_encrypt_md5.c2
-rw-r--r--libbb/xfuncs_printf.c45
-rw-r--r--libbb/xreadlink.c6
11 files changed, 137 insertions, 23 deletions
diff --git a/libbb/Kbuild.src b/libbb/Kbuild.src
index db79ff62b..adaab92d6 100644
--- a/libbb/Kbuild.src
+++ b/libbb/Kbuild.src
@@ -146,7 +146,7 @@ lib-$(CONFIG_DELUSER) += update_passwd.o
146lib-$(CONFIG_PASSWD) += pw_encrypt.o update_passwd.o obscure.o 146lib-$(CONFIG_PASSWD) += pw_encrypt.o update_passwd.o obscure.o
147lib-$(CONFIG_CHPASSWD) += pw_encrypt.o update_passwd.o 147lib-$(CONFIG_CHPASSWD) += pw_encrypt.o update_passwd.o
148lib-$(CONFIG_CRYPTPW) += pw_encrypt.o 148lib-$(CONFIG_CRYPTPW) += pw_encrypt.o
149lib-$(CONFIG_SULOGIN) += pw_encrypt.o 149lib-$(CONFIG_SULOGIN) += pw_encrypt.o correct_password.o
150lib-$(CONFIG_VLOCK) += pw_encrypt.o correct_password.o 150lib-$(CONFIG_VLOCK) += pw_encrypt.o correct_password.o
151lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o 151lib-$(CONFIG_SU) += pw_encrypt.o correct_password.o
152lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o 152lib-$(CONFIG_LOGIN) += pw_encrypt.o correct_password.o
diff --git a/libbb/bb_pwd.c b/libbb/bb_pwd.c
index 4829b723a..8250cd446 100644
--- a/libbb/bb_pwd.c
+++ b/libbb/bb_pwd.c
@@ -110,3 +110,51 @@ unsigned long FAST_FUNC get_ug_id(const char *s,
110 return xname2id(s); 110 return xname2id(s);
111 return r; 111 return r;
112} 112}
113
114/* Experimental "mallocing" API.
115 * The goal is nice: "we want to support a case when "guests" group is very large"
116 * but the code is butt-ugly.
117 */
118#if 0
119static char *find_latest(char last, char *cp)
120{
121 if (!cp)
122 return last;
123 cp += strlen(cp) + 1;
124 if (last < cp)
125 last = cp;
126 return last;
127}
128
129struct group* FAST_FUNC xmalloc_getgrnam(const char *name)
130{
131 struct {
132 struct group gr;
133 // May still be not enough!
134 char buf[64*1024 - sizeof(struct group) - 16];
135 } *s;
136 struct group *grp;
137 int r;
138 char *last;
139 char **gr_mem;
140
141 s = xmalloc(sizeof(*s));
142 r = getgrnam_r(name, &s->gr, s->buf, sizeof(s->buf), &grp);
143 if (!grp) {
144 free(s);
145 return grp;
146 }
147 last = find_latest(s->buf, grp->gr_name);
148 last = find_latest(last, grp->gr_passwd);
149 gr_mem = grp->gr_mem;
150 while (*gr_mem)
151 last = find_latest(last, *gr_mem++);
152 gr_mem++; /* points past NULL */
153 if (last < (char*)gr_mem)
154 last = (char*)gr_mem;
155//FIXME: what if we get not only truncated, but also moved here?
156// grp->gr_name pointer and friends are invalid now!!!
157 s = xrealloc(s, last - (char*)s);
158 return grp;
159}
160#endif
diff --git a/libbb/correct_password.c b/libbb/correct_password.c
index 7cabd33d0..acadf3914 100644
--- a/libbb/correct_password.c
+++ b/libbb/correct_password.c
@@ -31,12 +31,15 @@
31#include "libbb.h" 31#include "libbb.h"
32 32
33/* Ask the user for a password. 33/* Ask the user for a password.
34 * Return 1 without asking if PW has an empty password.
35 * Return -1 on EOF, error while reading input, or timeout.
34 * Return 1 if the user gives the correct password for entry PW, 36 * Return 1 if the user gives the correct password for entry PW,
35 * 0 if not. Return 1 without asking if PW has an empty password. 37 * 0 if not.
36 * 38 *
37 * NULL pw means "just fake it for login with bad username" */ 39 * NULL pw means "just fake it for login with bad username"
38 40 */
39int FAST_FUNC correct_password(const struct passwd *pw) 41int FAST_FUNC ask_and_check_password_extended(const struct passwd *pw,
42 int timeout, const char *prompt)
40{ 43{
41 char *unencrypted, *encrypted; 44 char *unencrypted, *encrypted;
42 const char *correct; 45 const char *correct;
@@ -65,13 +68,19 @@ int FAST_FUNC correct_password(const struct passwd *pw)
65 return 1; 68 return 1;
66 69
67 fake_it: 70 fake_it:
68 unencrypted = bb_ask_stdin("Password: "); 71 unencrypted = bb_ask(STDIN_FILENO, timeout, prompt);
69 if (!unencrypted) { 72 if (!unencrypted) {
70 return 0; 73 /* EOF (such as ^D) or error (such as ^C) or timeout */
74 return -1;
71 } 75 }
72 encrypted = pw_encrypt(unencrypted, correct, 1); 76 encrypted = pw_encrypt(unencrypted, correct, 1);
73 r = (strcmp(encrypted, correct) == 0); 77 r = (strcmp(encrypted, correct) == 0);
74 free(encrypted); 78 free(encrypted);
75 memset(unencrypted, 0, strlen(unencrypted)); 79 nuke_str(unencrypted);
76 return r; 80 return r;
77} 81}
82
83int FAST_FUNC ask_and_check_password(const struct passwd *pw)
84{
85 return ask_and_check_password_extended(pw, 0, "Password: ");
86}
diff --git a/libbb/fclose_nonstdin.c b/libbb/fclose_nonstdin.c
index 5ce9d5b48..1b1441347 100644
--- a/libbb/fclose_nonstdin.c
+++ b/libbb/fclose_nonstdin.c
@@ -18,7 +18,8 @@ int FAST_FUNC fclose_if_not_stdin(FILE *f)
18{ 18{
19 /* Some more paranoid applets want ferror() check too */ 19 /* Some more paranoid applets want ferror() check too */
20 int r = ferror(f); /* NB: does NOT set errno! */ 20 int r = ferror(f); /* NB: does NOT set errno! */
21 if (r) errno = EIO; /* so we'll help it */ 21 if (r)
22 errno = EIO; /* so we'll help it */
22 if (f != stdin) 23 if (f != stdin)
23 return (r | fclose(f)); /* fclose does set errno on error */ 24 return (r | fclose(f)); /* fclose does set errno on error */
24 return r; 25 return r;
diff --git a/libbb/human_readable.c b/libbb/human_readable.c
index 8b22b0cb5..0b2eb777e 100644
--- a/libbb/human_readable.c
+++ b/libbb/human_readable.c
@@ -94,7 +94,7 @@ const char* FAST_FUNC make_human_readable_str(unsigned long long val,
94 94
95/* Convert unsigned long long value into compact 5-char representation. 95/* Convert unsigned long long value into compact 5-char representation.
96 * String is not terminated (buf[5] is untouched) */ 96 * String is not terminated (buf[5] is untouched) */
97void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale) 97char* FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *scale)
98{ 98{
99 const char *fmt; 99 const char *fmt;
100 char c; 100 char c;
@@ -145,12 +145,13 @@ void FAST_FUNC smart_ulltoa5(unsigned long long ul, char buf[5], const char *sca
145 buf[3] = "0123456789"[v]; 145 buf[3] = "0123456789"[v];
146 buf[4] = scale[idx]; /* typically scale = " kmgt..." */ 146 buf[4] = scale[idx]; /* typically scale = " kmgt..." */
147 } 147 }
148 return buf + 5;
148} 149}
149 150
150/* Convert unsigned long long value into compact 4-char 151/* Convert unsigned long long value into compact 4-char
151 * representation. Examples: "1234", "1.2k", " 27M", "123T" 152 * representation. Examples: "1234", "1.2k", " 27M", "123T"
152 * String is not terminated (buf[4] is untouched) */ 153 * String is not terminated (buf[4] is untouched) */
153void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale) 154char* FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *scale)
154{ 155{
155 const char *fmt; 156 const char *fmt;
156 char c; 157 char c;
@@ -194,4 +195,5 @@ void FAST_FUNC smart_ulltoa4(unsigned long long ul, char buf[4], const char *sca
194 buf[2] = "0123456789"[v]; 195 buf[2] = "0123456789"[v];
195 buf[3] = scale[idx]; /* typically scale = " kmgt..." */ 196 buf[3] = scale[idx]; /* typically scale = " kmgt..." */
196 } 197 }
198 return buf + 4;
197} 199}
diff --git a/libbb/in_ether.c b/libbb/in_ether.c
index dadadbafe..1de383bde 100644
--- a/libbb/in_ether.c
+++ b/libbb/in_ether.c
@@ -3,6 +3,7 @@
3 * Utility routines. 3 * Utility routines.
4 */ 4 */
5 5
6//kbuild:lib-$(CONFIG_ARP) += in_ether.o
6//kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o 7//kbuild:lib-$(CONFIG_IFCONFIG) += in_ether.o
7//kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o 8//kbuild:lib-$(CONFIG_IFENSLAVE) += in_ether.o
8 9
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index 0f4fca1a2..b3e0802ee 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -175,8 +175,7 @@ int FAST_FUNC INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
175 return -1; 175 return -1;
176 } 176 }
177 memcpy(sin6, ai->ai_addr, sizeof(*sin6)); 177 memcpy(sin6, ai->ai_addr, sizeof(*sin6));
178 if (ai) 178 freeaddrinfo(ai);
179 freeaddrinfo(ai);
180 return 0; 179 return 0;
181} 180}
182 181
diff --git a/libbb/nuke_str.c b/libbb/nuke_str.c
new file mode 100644
index 000000000..56b808bc7
--- /dev/null
+++ b/libbb/nuke_str.c
@@ -0,0 +1,21 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) 2008 Denys Vlasenko
6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */
9
10//kbuild:lib-y += nuke_str.o
11
12#include "libbb.h"
13
14void FAST_FUNC nuke_str(char *str)
15{
16 if (str) {
17 while (*str)
18 *str++ = 0;
19 /* or: memset(str, 0, strlen(str)); - not as small as above */
20 }
21}
diff --git a/libbb/pw_encrypt_md5.c b/libbb/pw_encrypt_md5.c
index 889e09cab..1e52ecaea 100644
--- a/libbb/pw_encrypt_md5.c
+++ b/libbb/pw_encrypt_md5.c
@@ -86,7 +86,7 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
86 86
87 /* Get the length of the salt including "$1$" */ 87 /* Get the length of the salt including "$1$" */
88 sl = 3; 88 sl = 3;
89 while (salt[sl] && salt[sl] != '$' && sl < (3 + 8)) 89 while (sl < (3 + 8) && salt[sl] && salt[sl] != '$')
90 sl++; 90 sl++;
91 91
92 /* Hash. the password first, since that is what is most unknown */ 92 /* Hash. the password first, since that is what is most unknown */
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index a70683241..e4ac6a002 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -140,15 +140,6 @@ int FAST_FUNC xopen(const char *pathname, int flags)
140 return xopen3(pathname, flags, 0666); 140 return xopen3(pathname, flags, 0666);
141} 141}
142 142
143/* Die if we can't open an existing file readonly with O_NONBLOCK
144 * and return the fd.
145 * Note that for ioctl O_RDONLY is sufficient.
146 */
147int FAST_FUNC xopen_nonblocking(const char *pathname)
148{
149 return xopen(pathname, O_RDONLY | O_NONBLOCK);
150}
151
152// Warn if we can't open a file and return a fd. 143// Warn if we can't open a file and return a fd.
153int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode) 144int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
154{ 145{
@@ -167,6 +158,32 @@ int FAST_FUNC open_or_warn(const char *pathname, int flags)
167 return open3_or_warn(pathname, flags, 0666); 158 return open3_or_warn(pathname, flags, 0666);
168} 159}
169 160
161/* Die if we can't open an existing file readonly with O_NONBLOCK
162 * and return the fd.
163 * Note that for ioctl O_RDONLY is sufficient.
164 */
165int FAST_FUNC xopen_nonblocking(const char *pathname)
166{
167 return xopen(pathname, O_RDONLY | O_NONBLOCK);
168}
169
170int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g)
171{
172 int fd;
173 uid_t old_euid = geteuid();
174 gid_t old_egid = getegid();
175
176 xsetegid(g);
177 xseteuid(u);
178
179 fd = xopen(pathname, flags);
180
181 xseteuid(old_euid);
182 xsetegid(old_egid);
183
184 return fd;
185}
186
170void FAST_FUNC xunlink(const char *pathname) 187void FAST_FUNC xunlink(const char *pathname)
171{ 188{
172 if (unlink(pathname)) 189 if (unlink(pathname))
@@ -351,6 +368,16 @@ void FAST_FUNC xsetuid(uid_t uid)
351 if (setuid(uid)) bb_perror_msg_and_die("setuid"); 368 if (setuid(uid)) bb_perror_msg_and_die("setuid");
352} 369}
353 370
371void FAST_FUNC xsetegid(gid_t egid)
372{
373 if (setegid(egid)) bb_perror_msg_and_die("setegid");
374}
375
376void FAST_FUNC xseteuid(uid_t euid)
377{
378 if (seteuid(euid)) bb_perror_msg_and_die("seteuid");
379}
380
354// Die if we can't chdir to a new path. 381// Die if we can't chdir to a new path.
355void FAST_FUNC xchdir(const char *path) 382void FAST_FUNC xchdir(const char *path)
356{ 383{
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index bc1d0e665..ea09f20b8 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -8,6 +8,12 @@
8 8
9#include "libbb.h" 9#include "libbb.h"
10 10
11/* some systems (eg Hurd) does not have MAXSYMLINKS definition,
12 * set it to some reasonable value if it isn't defined */
13#ifndef MAXSYMLINKS
14# define MAXSYMLINKS 20
15#endif
16
11/* 17/*
12 * NOTE: This function returns a malloced char* that you will have to free 18 * NOTE: This function returns a malloced char* that you will have to free
13 * yourself. 19 * yourself.