1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
diff -Naurp a/libs/numlib/changeuidgid.c b/libs/numlib/changeuidgid.c
--- a/libs/numlib/changeuidgid.c 2022-03-06 20:02:45.000000000 +0100
+++ b/libs/numlib/changeuidgid.c 2023-02-24 17:26:02.452835861 +0100
@@ -52,10 +52,11 @@ void libmail_changeuidgid(uid_t uid, gid
*/
uid_t libmail_getuid(const char *uname, gid_t *pw_gid)
{
- size_t bufsize;
+ int bufsize;
char *buf;
struct passwd pwbuf;
struct passwd *pw;
+ int s;
/*
** uname might be a pointer returned from a previous called to getpw(),
@@ -70,35 +71,39 @@ uid_t libmail_getuid(const char *uname,
}
strcpy(p, uname);
-#ifdef _SC_GETGR_R_SIZE_MAX
- bufsize = sysconf(_SC_GETGR_R_SIZE_MAX);
+#ifdef _SC_GETPW_R_SIZE_MAX
+ bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1) /* Value was indeterminate */
- {
#endif
- bufsize = 16384; /* Should be more than enough */
- }
-
- buf = malloc(bufsize);
- if (buf == NULL)
{
- perror("malloc");
- exit(1);
+ bufsize = 16384; /* Should be more than enough */
}
+ do {
+ buf = malloc(bufsize);
+ if (buf == NULL)
+ {
+ perror("malloc");
+ exit(1);
+ }
- errno=ENOENT;
-
- getpwnam_r(p, &pwbuf, buf, bufsize, &pw);
+ s = getpwnam_r(p, &pwbuf, buf, bufsize, &pw);
+ if (s == ERANGE) {
+ free(buf);
+ bufsize += 1024;
+ }
+ } while (s == ERANGE && bufsize <= 65536);
- free(buf);
+ free(p);
if (pw == 0)
{
- free(p);
- perror("getpwnam");
+ errno = s;
+ perror("getpwnam_r");
exit(1);
}
- free(p);
+
+ free(buf);
if ( pw_gid ) *pw_gid = pw->pw_gid;
@@ -149,7 +154,7 @@ gid_t libmail_getgid(const char *gname)
struct group grp;
struct group *result;
char *buf;
- size_t bufsize;
+ int bufsize;
int s;
char *p=malloc(strlen(gname)+1);
@@ -168,14 +173,21 @@ gid_t libmail_getgid(const char *gname)
bufsize = 16384; /* Should be more than enough */
}
- buf = malloc(bufsize);
- if (buf == NULL)
- {
- perror("malloc");
- exit(1);
- }
+ do {
+ buf = malloc(bufsize);
+ if (buf == NULL)
+ {
+ perror("malloc");
+ exit(1);
+ }
+
+ s = getgrnam_r(p, &grp, buf, bufsize, &result);
+ if (s == ERANGE) {
+ free(buf);
+ bufsize += 1024;
+ }
+ } while (s == ERANGE && bufsize <= 65536);
- s = getgrnam_r(p, &grp, buf, bufsize, &result);
free(p);
if (result == NULL)
@@ -187,7 +199,7 @@ gid_t libmail_getgid(const char *gname)
else
{
errno = s;
- perror("getpwnam_r");
+ perror("getgrnam_r");
}
exit(1);
}
|