xref: /aoo41x/main/sal/workben/tgetpwnam.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <pwd.h>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #include <time.h>
31cdf0e10cSrcweir #include <pthread.h>
32cdf0e10cSrcweir #include <netdb.h>
33cdf0e10cSrcweir #include <sys/socket.h>
34cdf0e10cSrcweir #include <netinet/in.h>
35cdf0e10cSrcweir #include <arpa/inet.h>
36cdf0e10cSrcweir #ifndef NETBSD
37cdf0e10cSrcweir #include <shadow.h>
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /* exercises some reentrant libc-fucntions */
41cdf0e10cSrcweir 
42cdf0e10cSrcweir extern "C"
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 	struct tm *localtime_r(const time_t *timep, struct tm *buffer);
45cdf0e10cSrcweir 	struct passwd* getpwnam_r(char*, struct passwd*, char *, int);
46cdf0e10cSrcweir 	struct spwd* getspnam_r(char*, struct spwd*, char *, int);
47cdf0e10cSrcweir 	struct hostent *gethostbyname_r(const char *name, struct hostent *result,
48cdf0e10cSrcweir 									char *buffer, int buflen, int *h_errnop);
49cdf0e10cSrcweir }
50cdf0e10cSrcweir 
51cdf0e10cSrcweir static int go;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
workfunc1(void *)55cdf0e10cSrcweir extern "C" void *workfunc1(void*)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 	char buffer[256];
58cdf0e10cSrcweir 	struct tm		sttm;
59cdf0e10cSrcweir 	time_t			nepoch;
60cdf0e10cSrcweir 	struct passwd 	stpwd;
61cdf0e10cSrcweir 	struct hostent	sthostent;
62cdf0e10cSrcweir 	int 			nerr;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	printf("starting thread 1 ...\n");
65cdf0e10cSrcweir 	while (go) {
66cdf0e10cSrcweir 		getpwnam_r("hr", &stpwd, buffer, sizeof(buffer));
67cdf0e10cSrcweir 		gethostbyname_r("blauwal", &sthostent, buffer, sizeof(buffer), &nerr);
68cdf0e10cSrcweir 		time(&nepoch);
69cdf0e10cSrcweir 		localtime_r(&nepoch, &sttm);
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 	return 0;
72cdf0e10cSrcweir }
73cdf0e10cSrcweir 
workfunc2(void *)74cdf0e10cSrcweir extern "C" void *workfunc2(void*)
75cdf0e10cSrcweir {
76cdf0e10cSrcweir 	char buffer[256];
77cdf0e10cSrcweir 	struct tm		sttm;
78cdf0e10cSrcweir 	time_t			nepoch;
79cdf0e10cSrcweir 	struct passwd 	stpwd;
80cdf0e10cSrcweir 	struct hostent	sthostent;
81cdf0e10cSrcweir 	int 			nerr;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	printf("starting thread 2 ...\n");
84cdf0e10cSrcweir 	while(go) {
85cdf0e10cSrcweir 		getpwnam_r("mh", &stpwd, buffer, sizeof(buffer));
86cdf0e10cSrcweir 		gethostbyname_r("hr-1242", &sthostent, buffer, sizeof(buffer), &nerr);
87cdf0e10cSrcweir 		time(&nepoch);
88cdf0e10cSrcweir 		localtime_r(&nepoch, &sttm);
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 	return 0;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir extern int h_errno;
95cdf0e10cSrcweir 
main(int argc,char * argv[])96cdf0e10cSrcweir int main(int argc, char *argv[])
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 	char buffer[256];
99cdf0e10cSrcweir 	struct tm		*ptm;
100cdf0e10cSrcweir 	time_t			nepoch;
101cdf0e10cSrcweir 	struct passwd 	*pwd, *pres1;
102cdf0e10cSrcweir #ifndef NETBSD
103cdf0e10cSrcweir 	struct spwd 	*spwd, *pres2;
104cdf0e10cSrcweir #endif
105cdf0e10cSrcweir 	struct hostent	*phostent, *pres3;
106cdf0e10cSrcweir 	char **p;
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 	pthread_t 		tid1,tid2;
109cdf0e10cSrcweir 	int 			res1,res2;
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	go = 1;
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	pthread_create(&tid1, NULL, workfunc1, &res1);
114cdf0e10cSrcweir 	pthread_create(&tid2, NULL, workfunc2, &res2);
115cdf0e10cSrcweir 
116cdf0e10cSrcweir 	pwd = (struct passwd*)malloc(sizeof(struct passwd));
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	pres1 = getpwnam_r("hr", pwd, buffer, sizeof(buffer));
119cdf0e10cSrcweir 
120cdf0e10cSrcweir 	sleep(3);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 	if (pres1) {
123cdf0e10cSrcweir 		printf("Name: %s\n", pwd->pw_name);
124cdf0e10cSrcweir 		printf("Passwd: %s\n", pwd->pw_passwd);
125cdf0e10cSrcweir 		printf("Uid: %d\n", pwd->pw_uid);
126cdf0e10cSrcweir 		printf("Gid: %d\n", pwd->pw_gid);
127cdf0e10cSrcweir #ifdef NETBSD
128cdf0e10cSrcweir 		printf("Change: %s", ctime(&pwd->pw_change));
129cdf0e10cSrcweir 		printf("Class: %s\n", pwd->pw_class);
130cdf0e10cSrcweir #else
131cdf0e10cSrcweir 		printf("Age: %s\n", pwd->pw_age);
132cdf0e10cSrcweir 		printf("Comment: %s\n", pwd->pw_comment);
133cdf0e10cSrcweir #endif
134cdf0e10cSrcweir 		printf("Gecos: %s\n", pwd->pw_gecos);
135cdf0e10cSrcweir 		printf("Dir: %s\n", pwd->pw_dir);
136cdf0e10cSrcweir 		printf("Shell: %s\n", pwd->pw_shell);
137cdf0e10cSrcweir 	}
138cdf0e10cSrcweir 	else
139cdf0e10cSrcweir 		printf("getpwnam_r() failed!\n");
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 	free(pwd);
142cdf0e10cSrcweir 
143cdf0e10cSrcweir #ifndef NETBSD
144cdf0e10cSrcweir 	spwd = (struct spwd*)malloc(sizeof(struct spwd));
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 	pres2 = getspnam_r("hr", spwd, buffer, sizeof(buffer));
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 	if (pres2) {
149cdf0e10cSrcweir 		printf("Name: %s\n", spwd->sp_namp);
150cdf0e10cSrcweir 		printf("Passwd: %s\n", spwd->sp_pwdp);
151cdf0e10cSrcweir 		printf("Last Change: %ld\n", spwd->sp_lstchg);
152cdf0e10cSrcweir 		printf("Min: %ld\n", spwd->sp_min);
153cdf0e10cSrcweir 		printf("Max: %ld\n", spwd->sp_max);
154cdf0e10cSrcweir 	}
155cdf0e10cSrcweir 	else
156cdf0e10cSrcweir 		printf("getspnam_r() failed!\n");
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 	free(spwd);
159cdf0e10cSrcweir #endif
160cdf0e10cSrcweir 
161cdf0e10cSrcweir 	ptm = (struct tm*)malloc(sizeof(struct tm));
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	time(&nepoch);
164cdf0e10cSrcweir 	localtime_r(&nepoch, ptm);
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 	printf("Seconds: %d\n", ptm->tm_sec);
167cdf0e10cSrcweir 	printf("Minutes: %d\n", ptm->tm_min);
168cdf0e10cSrcweir 	printf("Hour: %d\n", ptm->tm_hour);
169cdf0e10cSrcweir 	printf("Day of Month: %d\n", ptm->tm_mday);
170cdf0e10cSrcweir 	printf("Month: %d\n", ptm->tm_mon);
171cdf0e10cSrcweir 	printf("Year: %d\n", ptm->tm_year);
172cdf0e10cSrcweir 	printf("Day of week: %d\n", ptm->tm_wday);
173cdf0e10cSrcweir 	printf("Day in the year: %d\n", ptm->tm_yday);
174cdf0e10cSrcweir 	printf("Daylight saving time: %d\n", ptm->tm_isdst);
175cdf0e10cSrcweir #ifdef NETBSD
176cdf0e10cSrcweir 	printf("Timezone: %s\n", ptm->tm_zone);
177cdf0e10cSrcweir #else
178cdf0e10cSrcweir 	printf("Timezone: %s\n", ptm->tm_name);
179cdf0e10cSrcweir #endif
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 	free(ptm);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	phostent = (struct hostent*)malloc(sizeof(struct hostent));
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	pres3 = gethostbyname_r("blauwal", phostent, buffer, sizeof(buffer), h_errno);
186cdf0e10cSrcweir 
187cdf0e10cSrcweir 	if (pres3) {
188cdf0e10cSrcweir 		printf("Official Hostname: %s\n", phostent->h_name);
189cdf0e10cSrcweir 		for ( p = phostent->h_aliases; *p != NULL; p++ )
190cdf0e10cSrcweir 			printf("Alias: %s\n", *p);
191cdf0e10cSrcweir 		printf("Addresstype: %d\n", phostent->h_addrtype);
192cdf0e10cSrcweir 		printf("Address length: %d\n", phostent->h_length);
193cdf0e10cSrcweir 		if ( phostent->h_addrtype == AF_INET ) {
194cdf0e10cSrcweir 			for ( p = phostent->h_addr_list; *p != NULL; *p++ )
195cdf0e10cSrcweir 				printf("Address: %s\n", inet_ntoa(**((in_addr**)p)));
196cdf0e10cSrcweir 		}
197cdf0e10cSrcweir 	}
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 	/* test boundary conditions */
201cdf0e10cSrcweir 	char smallbuf[23]; /* buffer to small */
202cdf0e10cSrcweir 	pres3 = gethostbyname_r("blauwal", phostent, smallbuf, sizeof(smallbuf), h_errno);
203cdf0e10cSrcweir 	if (!pres3) {
204cdf0e10cSrcweir 		perror("Expect ERANGE");
205cdf0e10cSrcweir 	}
206cdf0e10cSrcweir 	else
207cdf0e10cSrcweir 	{
208cdf0e10cSrcweir 		printf("ERROR: Check for buffersize went wrong\n");
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir #ifdef NETBSD
212cdf0e10cSrcweir 	char exactbuf[35];
213cdf0e10cSrcweir #else
214cdf0e10cSrcweir 	char exactbuf[24]; /* should be exact the necessary size */
215cdf0e10cSrcweir #endif
216cdf0e10cSrcweir 	pres3 = gethostbyname_r("blauwal", phostent, exactbuf, sizeof(exactbuf), &h_errno);
217cdf0e10cSrcweir 	if (!pres3) {
218cdf0e10cSrcweir 		perror("Check with exact buffersize");
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 	else
221cdf0e10cSrcweir 	{
222cdf0e10cSrcweir 		printf("Boundary check ok\n");
223cdf0e10cSrcweir 	}
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 	/* test error conditions */
226cdf0e10cSrcweir 	pres3 = gethostbyname_r("nohost", phostent, buffer, sizeof(buffer), &h_errno);
227cdf0e10cSrcweir 	if (!pres3) {
228cdf0e10cSrcweir 		herror("Expect HOST_NOT_FOUND");
229cdf0e10cSrcweir 	}
230cdf0e10cSrcweir 	else
231cdf0e10cSrcweir 	{
232cdf0e10cSrcweir 		printf("failed to detect non existant host\n");
233cdf0e10cSrcweir 	}
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	free(phostent);
236cdf0e10cSrcweir 	go = 0; /* atomic enough for our purposes */
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	pthread_join(tid1, NULL);
239cdf0e10cSrcweir 	pthread_join(tid2, NULL);
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	exit(0);
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 
245