Skip to content

Commit f8b05f4

Browse files
committed
Avoid recursion in getrpcent() and a overlapping strcpy
1 parent 9096691 commit f8b05f4

File tree

3 files changed

+122
-118
lines changed

3 files changed

+122
-118
lines changed

include/rpc/netdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct rpcent
4747
{
4848
char *r_name; /* Name of server for this rpc program. */
4949
char **r_aliases; /* Alias list. */
50-
int r_number; /* RPC program number. */
50+
long r_number; /* RPC program number. */
5151
};
5252

5353
extern void setrpcent (int __stayopen) __THROW;

socket/MISCFILES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ MISCFILES = \
77
bind.3 \
88
connect.3 \
99
getpeername.3 \
10+
getrpcent.3 \
1011
getsockname.3 \
1112
getsockopt.3 \
1213
listen.3 \

socket/getrpcent.c

Lines changed: 120 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* @(#)getrpcent.c 2.2 88/07/29 4.0 RPCSRC */
22
#if !defined(lint) && defined(SCCSIDS)
3-
static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro";
3+
static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro";
44
#endif
55

66
/*
@@ -42,192 +42,195 @@ static char sccsid[] = "@(#)getrpcent.c 1.9 87/08/11 Copyr 1984 Sun Micro";
4242
#include <netdb.h>
4343
#include <sys/socket.h>
4444

45-
extern void __setrpcent (int __stayopen) __THROW;
46-
extern void __endrpcent (void) __THROW;
47-
extern struct rpcent *__getrpcbyname (__const char *__name) __THROW;
48-
extern struct rpcent *__getrpcbynumber (int __number) __THROW;
49-
extern struct rpcent *__getrpcent (void) __THROW;
50-
51-
#if __GNUC_PREREQ(8, 0)
52-
# pragma GCC diagnostic ignored "-Wstringop-truncation"
45+
#if defined(__PUREC__) && !defined(_SYS_POLL_H)
46+
struct pollfd { int dummy; };
5347
#endif
5448

5549

5650
/*
5751
* Internet version.
5852
*/
59-
struct rpcdata {
60-
FILE *rpcf;
61-
char *current;
62-
int currentlen;
63-
int stayopen;
53+
static struct rpcdata
54+
{
55+
FILE *rpcf;
56+
int stayopen;
6457
#define MAXALIASES 35
65-
char *rpc_aliases[MAXALIASES];
66-
struct rpcent rpc;
67-
char line[BUFSIZ+1];
68-
char *domain;
58+
char *rpc_aliases[MAXALIASES];
59+
struct rpcent rpc;
60+
char line[BUFSIZ + 1];
61+
char *domain;
6962
} *rpcdata;
7063

71-
static char RPCDB[] = "/etc/rpc";
64+
static char const RPCDB[] = "U:\\etc\\rpc";
65+
static char const RPCDB2[] = "C:\\etc\\rpc";
7266

7367
static struct rpcdata *_rpcdata(void)
7468
{
75-
register struct rpcdata *d = rpcdata;
76-
77-
if (d == 0) {
78-
d = (struct rpcdata *)calloc(1, sizeof (struct rpcdata));
79-
rpcdata = d;
80-
}
81-
return (d);
82-
}
83-
84-
struct rpcent *__getrpcbynumber (int number)
85-
{
86-
register struct rpcdata *d = _rpcdata();
87-
register struct rpcent *p;
69+
struct rpcdata *d = rpcdata;
8870

8971
if (d == 0)
90-
return (0);
91-
__setrpcent(0);
92-
while ((p = __getrpcent())) {
93-
if (p->r_number == number)
94-
break;
72+
{
73+
d = (struct rpcdata *) calloc(1, sizeof(struct rpcdata));
74+
rpcdata = d;
9575
}
96-
__endrpcent();
97-
return (p);
76+
return d;
9877
}
99-
weak_alias (__getrpcbynumber, getrpcbynumber)
10078

101-
struct rpcent *__getrpcbyname (const char *name)
102-
{
103-
struct rpcent *rpc;
104-
char **rp;
105-
106-
__setrpcent(0);
107-
while((rpc = __getrpcent())) {
108-
if (strcmp(rpc->r_name, name) == 0)
109-
return (rpc);
110-
for (rp = rpc->r_aliases; *rp != NULL; rp++) {
111-
if (strcmp(*rp, name) == 0)
112-
return (rpc);
113-
}
114-
}
115-
__endrpcent();
116-
return (NULL);
117-
}
118-
weak_alias (__getrpcbyname, getrpcbyname)
11979

120-
void __setrpcent (int f)
80+
void setrpcent(int f)
12181
{
122-
register struct rpcdata *d = _rpcdata();
82+
struct rpcdata *d = _rpcdata();
12383

12484
if (d == 0)
12585
return;
12686
if (d->rpcf == NULL)
87+
{
12788
d->rpcf = fopen(RPCDB, "r");
128-
else
89+
if (d->rpcf == NULL)
90+
d->rpcf = fopen(RPCDB2, "r");
91+
} else
92+
{
12993
rewind(d->rpcf);
130-
if (d->current)
131-
free(d->current);
132-
d->current = NULL;
94+
}
13395
d->stayopen |= f;
13496
}
135-
weak_alias (__setrpcent, setrpcent)
13697

137-
void __endrpcent (void)
98+
99+
void endrpcent(void)
138100
{
139-
register struct rpcdata *d = _rpcdata();
101+
struct rpcdata *d = _rpcdata();
140102

141103
if (d == 0)
142104
return;
143-
if (d->current && !d->stayopen) {
144-
free(d->current);
145-
d->current = NULL;
146-
}
147-
if (d->rpcf && !d->stayopen) {
105+
if (d->rpcf && !d->stayopen)
106+
{
148107
fclose(d->rpcf);
149108
d->rpcf = NULL;
150109
}
151110
}
152-
weak_alias (__endrpcent, endrpcent)
153111

154-
static struct rpcent *interpret (char *val, int len);
155112

156-
struct rpcent *__getrpcent (void)
113+
static int interpret(struct rpcdata *d)
157114
{
158-
register struct rpcdata *d = _rpcdata();
159-
160-
if (d == 0)
161-
return(NULL);
162-
if (d->rpcf == NULL && (d->rpcf = fopen(RPCDB, "r")) == NULL)
163-
return (NULL);
164-
if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
165-
return (NULL);
166-
return interpret(d->line, strlen(d->line));
167-
}
168-
weak_alias (__getrpcent, getrpcent)
169-
170-
static struct rpcent *interpret (char *val, int len)
171-
{
172-
register struct rpcdata *d = _rpcdata();
173115
char *p;
174-
register char *cp, **q;
116+
char *cp;
117+
char **q;
175118

176-
if (d == 0)
177-
return NULL;
178-
strncpy(d->line, val, len);
179119
p = d->line;
180-
d->line[len] = '\n';
181120
if (*p == '#')
182-
return (__getrpcent());
183-
cp = index(p, '#');
121+
return 0;
122+
cp = strchr(p, '#');
184123
if (cp == NULL)
185-
{
186-
cp = index(p, '\n');
187-
if (cp == NULL)
188-
return (__getrpcent());
189-
}
190-
*cp = '\0';
191-
cp = index(p, ' ');
124+
cp = strchr(p, '\n');
125+
if (cp != NULL)
126+
*cp = '\0';
127+
if (*p == '\0')
128+
return 0;
129+
cp = strchr(p, ' ');
192130
if (cp == NULL)
193131
{
194-
cp = index(p, '\t');
132+
cp = strchr(p, '\t');
195133
if (cp == NULL)
196-
return (__getrpcent());
134+
return 0;
197135
}
198136
*cp++ = '\0';
199137
/* THIS STUFF IS INTERNET SPECIFIC */
200138
d->rpc.r_name = d->line;
201139
while (*cp == ' ' || *cp == '\t')
202140
cp++;
203-
d->rpc.r_number = atoi(cp);
141+
d->rpc.r_number = strtol(cp, NULL, 10);
204142
q = d->rpc.r_aliases = d->rpc_aliases;
205-
cp = index(p, ' ');
143+
cp = strchr(p, ' ');
206144
if (cp != NULL)
145+
{
207146
*cp++ = '\0';
208-
else
147+
} else
209148
{
210-
cp = index(p, '\t');
149+
cp = strchr(p, '\t');
211150
if (cp != NULL)
212151
*cp++ = '\0';
213152
}
214-
while (cp && *cp) {
215-
if (*cp == ' ' || *cp == '\t') {
153+
while (cp && *cp)
154+
{
155+
if (*cp == ' ' || *cp == '\t')
156+
{
216157
cp++;
217158
continue;
218159
}
219160
if (q < &(d->rpc_aliases[MAXALIASES - 1]))
220161
*q++ = cp;
221-
cp = index(p, ' ');
162+
cp = strchr(p, ' ');
222163
if (cp != NULL)
164+
{
223165
*cp++ = '\0';
224-
else
225-
{
226-
cp = index(p, '\t');
166+
} else
167+
{
168+
cp = strchr(p, '\t');
227169
if (cp != NULL)
228170
*cp++ = '\0';
229171
}
230172
}
231173
*q = NULL;
232-
return (&d->rpc);
174+
return 1;
175+
}
176+
177+
178+
struct rpcent *getrpcent(void)
179+
{
180+
struct rpcdata *d = _rpcdata();
181+
182+
if (d == 0)
183+
return NULL;
184+
if (d->rpcf == NULL)
185+
setrpcent(0);
186+
if (d->rpcf == NULL)
187+
return NULL;
188+
for (;;)
189+
{
190+
if (fgets(d->line, BUFSIZ, d->rpcf) == NULL)
191+
break;
192+
if (interpret(d))
193+
return &d->rpc;
194+
}
195+
return NULL;
196+
}
197+
198+
199+
struct rpcent *getrpcbynumber(int number)
200+
{
201+
struct rpcdata *d = _rpcdata();
202+
struct rpcent *p;
203+
204+
if (d == 0)
205+
return 0;
206+
setrpcent(0);
207+
while ((p = getrpcent()) != NULL)
208+
{
209+
if (p->r_number == number)
210+
break;
211+
}
212+
endrpcent();
213+
/* FIXME there is no way for the application to free the rpcdata buffer */
214+
return p;
215+
}
216+
217+
218+
struct rpcent *getrpcbyname(const char *name)
219+
{
220+
struct rpcent *rpc;
221+
char **rp;
222+
223+
setrpcent(0);
224+
while ((rpc = getrpcent()) != NULL)
225+
{
226+
if (strcmp(rpc->r_name, name) == 0)
227+
return rpc;
228+
for (rp = rpc->r_aliases; *rp != NULL; rp++)
229+
{
230+
if (strcmp(*rp, name) == 0)
231+
return rpc;
232+
}
233+
}
234+
endrpcent();
235+
return NULL;
233236
}

0 commit comments

Comments
 (0)