]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/getpwent.c
2 * Copyright (c) 2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1997 Apple Computer, Inc. (unpublished)
27 * /etc/passwd file access routines.
28 * Just read from the /etc/passwd file and skip the dbm database, since
29 * Directory Service does all flat file lookups when the system is multi-user.
30 * These routines are only used in single-user mode.
32 * 17 Apr 1997 file created - Marc Majka
40 #include <netinet/in.h>
41 #include <arpa/inet.h>
44 #define forever for (;;)
50 static struct passwd _pw
= { 0 };
51 static FILE *_pfp
= NULL
;
52 static int _pwFileFormat
= 1;
58 static struct hostent _h
= { 0 };
59 static FILE *_hfp
= NULL
;
62 __private_extern__
void LI_files_setpwent();
63 __private_extern__
void LI_files_endhostent();
70 if (l
== NULL
) return;
71 for (i
= 0; l
[i
] != NULL
; i
++)
73 if (l
[i
] != NULL
) free(l
[i
]);
76 if (l
!= NULL
) free(l
);
84 if (l
== NULL
) return 0;
85 for (i
= 0; l
[i
] != NULL
; i
++);
95 if (s
== NULL
) return NULL
;
105 insertString(char *s
, char **l
, unsigned int x
)
109 if (s
== NULL
) return l
;
112 l
= (char **)malloc(2 * sizeof(char *));
113 l
[0] = copyString(s
);
118 for (i
= 0; l
[i
] != NULL
; i
++);
119 len
= i
+ 1; /* count the NULL on the end of the list too! */
121 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
123 if ((x
>= (len
- 1)) || (x
== (unsigned int)-1))
125 l
[len
- 1] = copyString(s
);
130 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
131 l
[x
] = copyString(s
);
136 appendString(char *s
, char **l
)
138 return insertString(s
, l
, (unsigned int)-1);
142 tokenize(const char *data
, const char *sep
)
144 char **tokens
= NULL
;
150 if (data
== NULL
) return NULL
;
153 tokens
= appendString((char *)data
, tokens
);
163 /* skip leading white space */
164 while ((p
[0] == ' ') || (p
[0] == '\t') || (p
[0] == '\n')) p
++;
166 /* check for end of line */
167 if (p
[0] == '\0') break;
172 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
174 if (p
[0] == sep
[j
] || (p
[0] == '\0')) scanning
= 0;
177 while (scanning
== 1)
181 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
183 if (p
[0] == sep
[j
] || (p
[0] == '\0')) scanning
= 0;
187 /* back over trailing whitespace */
189 if (i
> -1) { /* did we actually copy anything? */
190 while ((buf
[i
] == ' ') || (buf
[i
] == '\t') || (buf
[i
] == '\n')) i
--;
194 tokens
= appendString(buf
, tokens
);
196 /* check for end of line */
197 if (p
[0] == '\0') break;
201 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
210 if ((scanning
== 0) && p
[0] == '\0')
212 /* line ended at a separator - add a null member */
213 tokens
= appendString("", tokens
);
230 if ((s
== NULL
) || (s
[0] == '\0')) return NULL
;
232 if (s
[0] != '#') s
[strlen(s
) - 1] = '\0';
243 if (_pw
.pw_name
!= NULL
) free(_pw
.pw_name
);
244 if (_pw
.pw_passwd
!= NULL
) free(_pw
.pw_passwd
);
245 if (_pw
.pw_class
!= NULL
) free(_pw
.pw_class
);
246 if (_pw
.pw_gecos
!= NULL
) free(_pw
.pw_gecos
);
247 if (_pw
.pw_dir
!= NULL
) free(_pw
.pw_dir
);
248 if (_pw
.pw_shell
!= NULL
) free(_pw
.pw_shell
);
251 _pw
.pw_passwd
= NULL
;
258 static struct passwd
*
259 LI_files_parse_user(char *data
)
264 if (data
== NULL
) return NULL
;
266 tokens
= tokenize(data
, ":");
267 ntokens
= listLength(tokens
);
268 if (( _pwFileFormat
&& (ntokens
!= 10)) ||
269 (!_pwFileFormat
&& (ntokens
!= 7)))
275 LI_files_free_user();
277 _pw
.pw_name
= tokens
[0];
278 _pw
.pw_passwd
= tokens
[1];
279 _pw
.pw_uid
= atoi(tokens
[2]);
281 _pw
.pw_gid
= atoi(tokens
[3]);
286 _pw
.pw_class
= tokens
[4];
287 _pw
.pw_change
= atoi(tokens
[5]);
289 _pw
.pw_expire
= atoi(tokens
[6]);
291 _pw
.pw_gecos
= tokens
[7];
292 _pw
.pw_dir
= tokens
[8];
293 _pw
.pw_shell
= tokens
[9];
297 _pw
.pw_class
= copyString("");
300 _pw
.pw_gecos
= tokens
[4];
301 _pw
.pw_dir
= tokens
[5];
302 _pw
.pw_shell
= tokens
[6];
310 static struct passwd
*
311 LI_files_getpw(const char *name
, uid_t uid
, int which
)
316 if (_pfp
== NULL
) LI_files_setpwent();
317 if (_pfp
== NULL
) return NULL
;
319 if (which
!= _PWENT_
) rewind(_pfp
);
323 line
= getLine(_pfp
);
324 if (line
== NULL
) break;
333 pw
= LI_files_parse_user(line
);
337 if (pw
== NULL
) continue;
339 if (which
== _PWENT_
) return pw
;
341 if (((which
== _PWNAM_
) && (!strcmp(name
, pw
->pw_name
))) || ((which
== _PWUID_
) && (uid
== pw
->pw_uid
)))
357 __private_extern__
struct passwd
*
360 return LI_files_getpw(NULL
, 0, _PWENT_
);
363 __private_extern__
struct passwd
*
364 LI_files_getpwnam(const char *name
)
366 return LI_files_getpw(name
, 0, _PWNAM_
);
369 __private_extern__
struct passwd
*
370 LI_files_getpwuid(uid_t uid
)
372 return LI_files_getpw(NULL
, uid
, _PWUID_
);
376 setpassent(int stayopen
)
381 __private_extern__
void
389 pwFile
= _PATH_MASTERPASSWD
;
393 pwFile
= _PATH_PASSWD
;
397 _pfp
= fopen(pwFile
, "r");
402 __private_extern__
void
419 if (_h
.h_name
!= NULL
) free(_h
.h_name
);
421 if (_h
.h_aliases
!= NULL
)
423 for (i
= 0; _h
.h_aliases
[i
] != NULL
; i
++) free(_h
.h_aliases
[i
]);
427 if (_h
.h_addr_list
!= NULL
)
429 for (i
= 0; _h
.h_addr_list
[i
] != NULL
; i
++) free(_h
.h_addr_list
[i
]);
430 free(_h
.h_addr_list
);
437 _h
.h_addr_list
= NULL
;
440 static struct hostent
*
441 LI_files_parse_host(char *data
)
443 char **tokens
, *addrstr
;
448 if (data
== NULL
) return NULL
;
450 tokens
= tokenize(data
, " ");
451 ntokens
= listLength(tokens
);
458 LI_files_free_host();
461 if (inet_pton(AF_INET
, tokens
[0], &a4
) == 1) af
= AF_INET
;
462 else if (inet_pton(AF_INET6
, tokens
[0], &a6
) == 1) af
= AF_INET6
;
475 _h
.h_length
= sizeof(struct in_addr
);
476 _h
.h_addr_list
= (char **)calloc(2, sizeof(char *));
477 _h
.h_addr_list
[0] = (char *)calloc(1, _h
.h_length
);
478 memcpy(_h
.h_addr_list
[0], &a4
, _h
.h_length
);
482 _h
.h_length
= sizeof(struct in6_addr
);
483 _h
.h_addr_list
= (char **)calloc(2, sizeof(char *));
484 _h
.h_addr_list
[0] = (char *)calloc(1, _h
.h_length
);
485 memcpy(_h
.h_addr_list
[0], &a6
, _h
.h_length
);
488 _h
.h_name
= tokens
[1];
490 _h
.h_aliases
= (char **)calloc(ntokens
- 1, sizeof(char *));
491 for (i
= 2; i
< ntokens
; i
++) _h
.h_aliases
[i
-2] = tokens
[i
];
499 static struct hostent
*
500 LI_files_get_host(const char *name
, const void *addr
, int af
, int which
)
506 if ((which
== _HADDR_
) && (addr
== NULL
)) return NULL
;
508 if (_hfp
== NULL
) _hfp
= fopen(_PATH_HOSTS
, "r");
509 if (_hfp
== NULL
) return NULL
;
511 if (which
!= _HENT_
) rewind(_hfp
);
515 line
= getLine(_hfp
);
516 if (line
== NULL
) break;
525 h
= LI_files_parse_host(line
);
529 if (h
== NULL
) continue;
531 if (which
== _HENT_
) return h
;
535 if ((which
== _HNAM_
) && (af
== h
->h_addrtype
))
537 if (!strcmp(name
, h
->h_name
)) got_host
= 1;
538 else if (h
->h_aliases
!= NULL
)
540 for (i
= 0; (h
->h_aliases
[i
] != NULL
) && (got_host
== 0); i
++)
541 if (!strcmp(name
, h
->h_aliases
[i
])) got_host
= 1;
545 if ((which
== _HADDR_
) && (h
->h_addrtype
== af
))
547 for (i
= 0; (h
->h_addr_list
[i
] != NULL
) && (got_host
== 0); i
++)
548 if (memcmp(addr
, h
->h_addr_list
[i
], h
->h_length
) == 0) got_host
= 1;
567 __private_extern__
struct hostent
*
568 LI_files_gethostbyname(const char *name
)
570 return LI_files_get_host(name
, NULL
, AF_INET
, _HNAM_
);
573 __private_extern__
struct hostent
*
574 LI_files_gethostbyname2(const char *name
, int af
)
576 return LI_files_get_host(name
, NULL
, af
, _HNAM_
);
579 __private_extern__
struct hostent
*
580 LI_files_gethostbyaddr(const void *addr
, socklen_t len
, int type
)
582 if ((type
== AF_INET
) || (type
== AF_INET6
)) return LI_files_get_host(NULL
, addr
, type
, _HADDR_
);
586 __private_extern__
struct hostent
*
587 LI_files_gethostent()
589 return LI_files_get_host(NULL
, NULL
, AF_UNSPEC
, _HENT_
);
592 __private_extern__
void
593 LI_files_sethostent(int stayopen
)
597 __private_extern__
void
598 LI_files_endhostent()