]>
git.saurik.com Git - apple/system_cmds.git/blob - chkpasswd.tproj/netinfo_passwd.c
2 * Copyright (c) 1999 Apple Computer, 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.0 (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@
27 #include <sys/param.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
38 #include <netinfo/ni.h>
40 extern void checkpasswd(char *, char *);
43 sys_ismyaddress(unsigned long addr
)
47 char buf
[1024]; /* XXX */
50 struct sockaddr_in
*sin
;
53 if (addr
== htonl(INADDR_LOOPBACK
)) return 1;
55 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
57 if (sock
< 0) return 0;
59 ifc
.ifc_len
= sizeof(buf
);
62 if (ioctl(sock
, SIOCGIFCONF
, (char *)&ifc
) < 0)
70 while (offset
<= ifc
.ifc_len
)
72 ifr
= (struct ifreq
*)(ifc
.ifc_buf
+ offset
);
73 offset
+= IFNAMSIZ
+ ifr
->ifr_addr
.sa_len
;
75 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) continue;
76 if (ioctl(sock
, SIOCGIFFLAGS
, ifr
) < 0) continue;
78 sin
= (struct sockaddr_in
*)&ifr
->ifr_addr
;
79 if ((ifr
->ifr_flags
& IFF_UP
) &&
80 (!(ifr
->ifr_flags
& IFF_LOOPBACK
)) &&
81 (sin
->sin_addr
.s_addr
== addr
))
93 is_root_on_master(void *d
)
96 char myhostname
[MAXHOSTNAMELEN
+ 1];
102 struct sockaddr_in addr
;
106 if (uid
!= 0) return 0;
108 gethostname(myhostname
, MAXHOSTNAMELEN
);
109 p
= strchr(myhostname
, '.');
110 if (p
!= NULL
) *p
= '\0';
112 status
= ni_root(d
, &dir
);
113 if (status
!= NI_OK
) return 0;
115 status
= ni_read(d
, &dir
, &pl
);
116 if (status
!= NI_OK
) return 0;
118 where
= ni_proplist_match(pl
, "master", NULL
);
119 if (where
== NI_INDEX_NULL
)
121 ni_proplist_free(&pl
);
125 if (pl
.ni_proplist_val
[where
].nip_val
.ni_namelist_len
== 0)
127 ni_proplist_free(&pl
);
128 fprintf(stderr
, "No value for NetInfo master property\n");
132 p
= strchr(pl
.ni_proplist_val
[where
].nip_val
.ni_namelist_val
[0], '/');
133 if (p
!= NULL
) *p
= '\0';
135 p
= strchr(pl
.ni_proplist_val
[where
].nip_val
.ni_namelist_val
[0], '.');
136 if (p
!= NULL
) *p
= '\0';
138 if (!strcmp(pl
.ni_proplist_val
[where
].nip_val
.ni_namelist_val
[0], myhostname
))
140 ni_proplist_free(&pl
);
144 if (!strcmp(pl
.ni_proplist_val
[where
].nip_val
.ni_namelist_val
[0], "localhost"))
146 ni_proplist_free(&pl
);
147 ni_addrtag(d
, &addr
, &tag
);
148 if (sys_ismyaddress(addr
.sin_addr
.s_addr
)) return 1;
151 ni_proplist_free(&pl
);
164 status
= ni_open(NULL
, ".", &d
);
165 while (status
== NI_OK
)
168 status
= ni_lookupprop(d
, &dir
, "security_options", &nl
);
171 where
= ni_namelist_match(nl
, "secure_passwords");
172 if (where
!= NI_INDEX_NULL
)
180 status
= ni_open(d1
, "..", &d
);
188 parse_server_tag(char *str
, struct sockaddr_in
*server
, char **t
)
190 /* utility to parse a server/tag string */
193 char *host
, *tag
, *slash
;
194 struct hostent
*hent
;
198 /* find the "/" character */
199 slash
= index(str
, '/');
201 /* check to see if the "/" is missing */
204 fprintf(stderr
, "incorrect format \"%s\" for domain name\n", str
);
208 /* find the location of the '/' */
211 /* check if host string is empty */
214 fprintf(stderr
, "incorrect format \"%s\" for domain name\n", str
);
215 fprintf(stderr
, "no server name specified\n");
219 /* check if tag string is empty */
222 fprintf(stderr
, "incorrect format \"%s\" for domain name\n", str
);
223 fprintf(stderr
, "no tag specified\n");
227 /* allocate some space for the host and tag */
228 host
= (char *)malloc(i
+ 1);
229 *t
= (char *)malloc(len
- i
);
232 /* copy out the host */
233 strncpy(host
, str
, i
);
236 /* copy out the tag */
237 strcpy(tag
, slash
+ 1);
239 /* try interpreting the host portion as an address */
240 server
->sin_addr
.s_addr
= inet_addr(host
);
242 if (server
->sin_addr
.s_addr
== -1)
244 /* This isn't a valid address. Is it a known hostname? */
245 hent
= gethostbyname(host
);
248 /* found a host with that name */
249 bcopy(hent
->h_addr
, &server
->sin_addr
, hent
->h_length
);
253 fprintf(stderr
, "Can't find address for %s\n", host
);
264 domain_for_user(char *uname
, char *locn
, ni_id
*dir
)
269 struct sockaddr_in server
;
274 * Find the user in NetInfo.
276 upath
= malloc(8 + strlen(uname
));
277 sprintf(upath
, "/users/%s", uname
);
283 if (locn
[0] == '/') bytag
= 0;
284 else if (!strncmp(locn
, "./", 2)) bytag
= 0;
285 else if (!strncmp(locn
, "../", 3)) bytag
= 0;
289 parse_server_tag(locn
, &server
, &tag
);
290 d
= ni_connect(&server
, tag
);
291 if (d
== (void *)NULL
) return (void *)NULL
;
293 else status
= ni_open(NULL
, locn
, &d
);
294 status
= ni_pathsearch(d
, dir
, upath
);
297 if (status
== NI_OK
) return d
;
303 status
= ni_open(NULL
, ".", &d
);
304 while (status
== NI_OK
)
306 status
= ni_pathsearch(d
, dir
, upath
);
307 if (status
== NI_OK
) break;
309 status
= ni_open(d1
, "..", &d
);
315 if (status
== NI_OK
) return d
;
320 netinfo_check_passwd(char *uname
, char *locn
)
328 d
= domain_for_user(uname
, locn
, &dir
);
329 if (d
== (void *)NULL
)
331 fprintf(stderr
, "user %s not found in NetInfo\n", uname
);
336 * Read the passwd and uid from NetInfo.
338 status
= ni_lookupprop(d
, &dir
, "passwd", &nl
);
339 if (status
== NI_NOPROP
) nl
.ni_namelist_len
= 0;
340 else if (status
!= NI_OK
)
343 fprintf(stderr
, "NetInfo read failed: %s\n", ni_error(status
));
348 if (nl
.ni_namelist_len
> 0) oldpw
= nl
.ni_namelist_val
[0];
350 checkpasswd(uname
, oldpw
);