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.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 * Glues the library routines to the stub routines
26 * Copyright (C) 1989 by NeXT, Inc.
30 #include <netinfo/ni.h>
31 #include <rpc/pmap_clnt.h>
32 #include <rpc/pmap_prot.h>
37 #include "sys_interfaces.h"
39 #define LOCAL_PORT 1033
41 #define NI_TIMEOUT_SHORT 5 /* 5 second timeout for transactions */
42 #define NI_TIMEOUT_LONG 60 /* 60 second timeout for writes */
43 #define NI_TRIES 5 /* number of retries per timeout (udp only) */
44 #define NI_SLEEPTIME 4 /* 4 second sleeptime, in case of errors */
45 #define NI_MAXSLEEPTIME 64 /* 64 second max sleep time */
46 #define NI_MAXCONNTRIES 2 /* Try to form a connection twice before sleeping */
48 /* Hack for determining if an IP address is a broadcast address. -GRS */
49 /* Note that addr is network byte order (big endian) - BKM */
51 #define IS_BROADCASTADDR(addr) (((unsigned char *) &addr)[0] == 0xFF)
53 #ifndef INADDR_LOOPBACK
54 #define INADDR_LOOPBACK (u_long)0x7f000001
56 #define debug(msg) syslog(LOG_ERR, msg)
58 #define clnt_debug(ni, msg) /* do nothing */
60 typedef struct ni_private
{
61 int naddrs
; /* number of addresses */
62 struct in_addr
*addrs
; /* addresses of servers - network byte order */
63 int whichwrite
; /* which one of the above is the master */
64 ni_name
*tags
; /* tags of servers */
65 int pid
; /* pid, to detect forks */
66 int tsock
; /* tcp socket */
67 int tport
; /* tcp local port name - host byte order */
68 CLIENT
*tc
; /* tcp client */
69 long tv_sec
; /* timeout for this call */
70 long rtv_sec
; /* read timeout - 0 if default */
71 long wtv_sec
; /* write timeout - 0 if default */
72 int abort
; /* abort on timeout? */
73 int needwrite
; /* need to lock writes? */
74 int uid
; /* user id */
75 ni_name passwd
; /* password */
78 #define NIP(ni) ((ni_private *)(ni))
80 static const ni_name NAME_NAME
= "name";
81 static const ni_name NAME_SERVES
= "serves";
82 static const ni_name NAME_MACHINES
= "machines";
83 static const ni_name NAME_IP_ADDRESS
= "ip_address";
84 static const ni_name NAME_MASTER
= "master";
85 static const ni_name NAME_USERS
= "users";
86 static const ni_name NAME_UID
= "uid";
88 typedef struct getreg_stuff
{
89 nibind_getregister_res res
;
94 static int socket_open(struct sockaddr_in
*raddr
, int, int, int, int, int);
98 * Keep track of our port, in case somebody closes our socket
106 struct sockaddr_in sin
;
109 sinlen
= sizeof(sin
);
110 if (getsockname(sock
, (struct sockaddr
*)&sin
, &sinlen
) == 0) {
111 if (sin
.sin_port
== 0) {
112 (void)bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
113 sinlen
= sizeof(sin
);
114 (void)getsockname(sock
, (struct sockaddr
*)&sin
,
117 return (ntohs(sin
.sin_port
));
128 if (ni
->passwd
!= NULL
&& ni
->tc
!= NULL
) {
129 auth_destroy(ni
->tc
->cl_auth
);
130 ni
->tc
->cl_auth
= authunix_create(ni
->passwd
, ni
->uid
, 0, 0,
143 tv
->tv_sec
= sec
/ tries
;
144 tv
->tv_usec
= ((sec
% tries
) * 1000000) / tries
;
158 ni
->tv_sec
= timeout
;
159 if (ni
->tc
!= NULL
) {
160 clnt_control(ni
->tc
, CLSET_TIMEOUT
, &tv
);
166 * Connect to a given address/tag
169 connectit(ni_private
*ni
)
171 struct sockaddr_in sin
;
176 nibind_getregister_res res
;
179 bzero(&sin
, sizeof(sin
));
181 sin
.sin_family
= AF_INET
;
183 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
186 ni_settimeout(ni
, tv
.tv_sec
);
187 fixtimeout(&tv
, ni
->tv_sec
, NI_TRIES
);
190 * If connecting to local domain, try using the "well-known" port first.
192 if (!strcmp(ni
->tags
[0], "local"))
194 interface_list_t
*ilist
;
196 ilist
= sys_interfaces();
197 if (sys_is_my_address(ilist
, &ni
->addrs
[0]))
199 sin
.sin_port
= htons(LOCAL_PORT
);
200 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
201 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
203 sys_interfaces_release(ilist
);
207 * If connecting to a domain other than the local domain,
208 * or if connection to local didn't work with local's well-known port,
209 * then go through portmap & nibindd to find the port and connect.
214 sin
.sin_addr
= ni
->addrs
[0];
216 sock
= socket_open(&sin
, NIBIND_PROG
, NIBIND_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_UDP
);
217 if (sock
< 0) return (0);
219 cl
= clntudp_create(&sin
, NIBIND_PROG
, NIBIND_VERS
, tv
, &sock
);
226 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
229 stat
= clnt_call(cl
, NIBIND_GETREGISTER
, xdr_ni_name
, &ni
->tags
[0], xdr_nibind_getregister_res
, &res
, tv
);
232 if (stat
!= RPC_SUCCESS
|| res
.status
!= NI_OK
) return (0);
234 sin
.sin_port
= htons(res
.nibind_getregister_res_u
.addrs
.tcp_port
);
235 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
238 if (sock
< 0) return (0);
240 cl
= clnttcp_create(&sin
, NI_PROG
, NI_VERS
, &sock
, 0, 0);
247 clnt_control(cl
, CLSET_TIMEOUT
, &tv
);
250 ni
->tport
= getmyport(sock
);
252 fcntl(ni
->tsock
, F_SETFD
, 1);
263 ((ni_private
*)ni
)->abort
= abort
;
273 ((ni_private
*)ni
)->wtv_sec
= timeout
;
283 ((ni_private
*)ni
)->rtv_sec
= timeout
;
293 ((ni_private
*)ni
)->needwrite
= needwrite
;
298 * Returns a client handle to the NetInfo server, if it's running
301 connectlocal(ni_private
*ni
)
306 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
307 ni
->addrs
[0].s_addr
= htonl(INADDR_LOOPBACK
);
308 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
309 ni
->tags
[0] = ni_name_dup("local");
312 while (!connectit(ni
))
316 syslog(LOG_ERR
, "NetInfo timeout connecting to local domain, sleeping");
326 syslog(LOG_ERR
, "NetInfo connection to local domain waking");
334 * Destroy the client handle
345 if (sock
>= 0 && getmyport(sock
) != port
) {
347 * Somebody else has the descriptor open. Do not close it,
353 auth_destroy(cl
->cl_auth
);
358 * It's ours and we can close it
366 * Reinitialize everything
373 if (ni
->tc
!= NULL
) {
374 clnt_kill(ni
->tc
, ni
->tsock
, ni
->tport
);
384 * Switch to a new server
392 struct in_addr tmp_addr
;
399 tmp_addr
= ni
->addrs
[0];
400 tmp_tag
= ni
->tags
[0];
402 ni
->addrs
[0] = ni
->addrs
[which
];
403 ni
->tags
[0] = ni
->tags
[which
];
405 ni
->addrs
[which
] = tmp_addr
;
406 ni
->tags
[which
] = tmp_tag
;
408 if (ni
->whichwrite
== 0) {
409 ni
->whichwrite
= which
;
411 else if (ni
->whichwrite
== which
) {
418 * Swap two servers' positions
427 struct in_addr tmp_addr
;
432 tmp_addr
= ni
->addrs
[a
];
433 tmp_tag
= ni
->tags
[a
];
435 ni
->addrs
[a
] = ni
->addrs
[b
];
436 ni
->tags
[a
] = ni
->tags
[b
];
438 ni
->addrs
[b
] = tmp_addr
;
439 ni
->tags
[b
] = tmp_tag
;
441 if (ni
->whichwrite
== a
) {
444 else if (ni
->whichwrite
== b
) {
451 * Callback routine for multi_call
452 * XXX: should save returned port numbers
457 struct sockaddr_in
*sin
,
461 getreg_stuff
*stuff
= (getreg_stuff
*)vstuff
;
463 if (stuff
->res
.status
!= NI_OK
) {
466 ni_switch(stuff
->ni
, which
);
475 shuffle(ni_private
*ni
)
481 if (ni
->naddrs
<= 1) return;
483 rfd
= open("/dev/random", O_RDONLY
, 0);
484 shuffle
= (int *)malloc(ni
->naddrs
* sizeof(int));
485 for (i
= 0; i
< ni
->naddrs
; i
++) shuffle
[i
] = i
;
486 for (i
= 0, j
= ni
->naddrs
; j
> 0; i
++, j
--) {
491 /* get a random number */
493 (read(rfd
, &rVal
, sizeof(rVal
)) != sizeof(rVal
))) {
494 /* if we could not read from /dev/random */
495 static int initialized
= 0;
498 srandom(gethostid() ^ time(NULL
));
504 rEnt
= (unsigned int)rVal
% j
; /* pick one of the remaining entries */
505 tEnt
= shuffle
[rEnt
]; /* grab the random entry */
506 shuffle
[rEnt
] = shuffle
[j
-1]; /* the last entry moves to the random slot */
507 shuffle
[j
-1] = tEnt
; /* the last slot gets the random entry */
508 ni_swap(ni
, rEnt
, j
-1); /* and swap the actual NI addresses */
511 if (rfd
> 0) (void)close(rfd
);
523 int sleeptime
= NI_SLEEPTIME
;
527 interface_list_t
*ilist
;
530 if (ni
->naddrs
== 1) {
537 * re-order the servers so that:
538 * servers on the local host are at the start of the list, then
539 * servers on the local network are next, then
540 * all other servers are next
543 ilist
= sys_interfaces();
551 * move local servers to the head of the list
554 for (i
= nlocal
; i
< ni
->naddrs
; i
++) {
555 if (sys_is_my_address(ilist
, &ni
->addrs
[i
]))
557 ni_swap(ni
, nlocal
, i
);
563 * move servers on this network to follow local servers
566 for (i
= nnetwork
; i
< ni
->naddrs
; i
++) {
567 if (sys_is_my_network(ilist
, &ni
->addrs
[i
]) ||
568 IS_BROADCASTADDR(ni
->addrs
[i
].s_addr
))
570 ni_swap(ni
, nnetwork
, i
);
575 sys_interfaces_release(ilist
);
580 * call local servers first
583 for (i
= 0; i
< nlocal
; i
++) {
584 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (local %d)",
585 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
587 stat
= multi_call(nlocal
, ni
->addrs
,
588 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
589 xdr_ni_name
, ni
->tags
,
591 xdr_nibind_getregister_res
,
594 if (stat
== RPC_SUCCESS
) {
600 * call local servers and this network's servers
602 if (nnetwork
> nlocal
) {
603 for (i
= 0; i
< nnetwork
; i
++) {
604 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (network %d)",
605 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
607 stat
= multi_call(nnetwork
, ni
->addrs
,
608 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
609 xdr_ni_name
, ni
->tags
,
611 xdr_nibind_getregister_res
,
614 if (stat
== RPC_SUCCESS
) {
622 for (i
= 0; i
< ni
->naddrs
; i
++) {
623 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (world %d)",
624 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
626 stat
= multi_call(ni
->naddrs
,
627 ni
->addrs
, NIBIND_PROG
, NIBIND_VERS
,
629 xdr_ni_name
, ni
->tags
,
631 xdr_nibind_getregister_res
,
633 ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
);
634 if (stat
== RPC_SUCCESS
) {
642 if (ni
->whichwrite
>= 0) {
644 "NetInfo connect timeout (domain with master %s/%s), sleeping",
645 inet_ntoa(ni
->addrs
[ni
->whichwrite
]), ni
->tags
[ni
->whichwrite
]);
648 syslog(LOG_ERR
, "NetInfo connect timeout (domain with server %s/%s), sleeping",
649 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
654 if (sleeptime
< NI_MAXSLEEPTIME
) {
655 sleeptime
*= 2; /* backoff */
659 syslog(LOG_INFO
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
662 syslog(LOG_ERR
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
669 * Confirm that our tcp socket is still valid
677 if (ni
->tsock
!= -1) {
678 if (getmyport(ni
->tsock
) == ni
->tport
) {
682 * Somebody closed our socket. Do not close it, it could
683 * be owned by somebody else now.
685 auth_destroy(ni
->tc
->cl_auth
);
686 clnt_destroy(ni
->tc
);
689 if (!needwrite
&& !rebind(ni
) && ni
->abort
) {
692 return (connectit(ni
));
712 if (ni
->naddrs
== 1) {
714 * One server - must be the master
719 needwrite
= ni
->needwrite
;
721 if (ni_root(ni
, &root
) != NI_OK
) {
722 ni
->needwrite
= needwrite
;
725 if (ni_lookupprop(ni
, &root
, NAME_MASTER
, &nl
) != NI_OK
) {
726 ni
->needwrite
= needwrite
;
729 if (nl
.ninl_len
== 0) {
730 ni
->needwrite
= needwrite
;
733 sep
= index(nl
.ninl_val
[0], '/');
735 ni
->needwrite
= needwrite
;
739 master
= nl
.ninl_val
[0];
740 if (ni_lookup(ni
, &root
, NAME_NAME
, NAME_MACHINES
, &idl
) != NI_OK
) {
741 ni
->needwrite
= needwrite
;
742 ni_namelist_free(&nl
);
745 if (idl
.niil_len
< 1) {
746 ni
->needwrite
= needwrite
;
749 id
.nii_object
= idl
.niil_val
[0];
750 ni_idlist_free(&idl
);
751 if (ni_lookup(ni
, &id
, NAME_NAME
, master
, &idl
) != NI_OK
) {
752 ni_namelist_free(&nl
);
753 ni
->needwrite
= needwrite
;
756 ni_namelist_free(&nl
);
757 if (idl
.niil_len
< 1) {
758 ni
->needwrite
= needwrite
;
761 id
.nii_object
= idl
.niil_val
[0];
762 ni_idlist_free(&idl
);
763 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) {
766 for (i
= 0; i
< nl
.ninl_len
; i
++) {
767 addr
.s_addr
= inet_addr(nl
.ninl_val
[i
]);
768 for (j
= 0; j
< ni
->naddrs
; j
++) {
769 if (addr
.s_addr
== ni
->addrs
[j
].s_addr
) {
771 ni_namelist_free(&nl
);
772 ni
->needwrite
= needwrite
;
777 ni
->needwrite
= needwrite
;
778 ni_namelist_free(&nl
);
797 if (getpid() != ni
->pid
) {
800 if (needwrite
|| ni
->needwrite
) {
801 if (ni
->whichwrite
>= 0) {
802 ni_switch(ni
, ni
->whichwrite
);
804 if (!setmaster(ni
)) {
807 ni_switch(ni
, ni
->whichwrite
);
810 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
811 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
814 ni_settimeout(ni
, (ni
->wtv_sec
== 0 ?
815 NI_TIMEOUT_LONG
: ni
->wtv_sec
));
818 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
819 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
823 * Try more than once, in case server closed connection.
825 for (i
= 0; i
< NI_MAXCONNTRIES
; i
++) {
826 if (!confirm_tcp(ni
, needwrite
)) {
829 if ((resp
= (*stub
)(args
, ni
->tc
)) != NULL
) {
831 syslog(LOG_ERR
, "NetInfo connected to %s/%s",
832 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
836 clnt_geterr(ni
->tc
, &err
);
837 if (err
.re_status
!= RPC_CANTRECV
) {
840 if (i
+ 1 < NI_MAXCONNTRIES
) {
842 * Server closed connection. Reinit and try
848 if (err
.re_status
== RPC_PROCUNAVAIL
) {
851 if (needwrite
|| ni
->abort
) {
853 * We time out for writes or if it is explicitly
860 "NetInfo connection failed for server %s/%s",
861 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
865 if (ni
->tc
!= NULL
) {
866 if (!(sleeptime
== 0 &&
867 err
.re_status
== RPC_TIMEDOUT
)) {
869 * Do not print message on
870 * first timeout. It is likely
871 * we will find another server soon.
872 * Let's not needlessly alarm the
875 syslog(LOG_ERR
, "%s on connection to %s/%s",
876 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
877 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
881 /* first attempt failed */
882 syslog(LOG_ERR
, "%s on initial connection to %s/%s",
883 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
884 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
888 "NetInfo connection failed for server %s/%s",
889 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
895 if (sleeptime
< NI_MAXSLEEPTIME
) {
896 sleeptime
*= 2; /* backoff */
900 * Do not sleep on the first timeout.
901 * It is likely we will find another server quickly.
903 sleeptime
= NI_SLEEPTIME
;
911 #define RCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
915 #define WCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
943 ni
= (ni_private
*)malloc(sizeof(*ni
));
950 ni
->tv_sec
= NI_TIMEOUT_SHORT
;
969 dupni
= (ni_private
*)ni_alloc();
972 dupni
->naddrs
= NIP(ni
)->naddrs
;
973 dupni
->whichwrite
= NIP(ni
)->whichwrite
;
974 if (dupni
->naddrs
> 0) {
975 dupni
->addrs
= ((struct in_addr
*)
976 malloc(NIP(ni
)->naddrs
* sizeof(struct in_addr
)));
977 bcopy(NIP(ni
)->addrs
, dupni
->addrs
,
978 NIP(ni
)->naddrs
* sizeof(struct in_addr
));
979 dupni
->tags
= ((ni_name
*)
980 malloc(NIP(ni
)->naddrs
* sizeof(ni_name
)));
981 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
982 dupni
->tags
[i
] = ni_name_dup(NIP(ni
)->tags
[i
]);
985 if (NIP(ni
)->passwd
!= NULL
) {
986 dupni
->passwd
= ni_name_dup(NIP(ni
)->passwd
);
988 return ((void *)dupni
);
999 int len
= strlen(domain
);
1002 sep
= index(domtag
, '/');
1006 if (strncmp(domain
, domtag
, len
) == 0 &&
1007 domtag
[len
] == '/') {
1008 *tag
= ni_name_dup(sep
+ 1);
1020 ni_private
*target_ni
1025 struct in_addr addr
;
1028 id
.nii_object
= ido
;
1029 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) {
1032 if (nl
.ninl_len
== 0) {
1036 if (target_ni
->naddrs
== 0) {
1038 (struct in_addr
*)malloc(nl
.ninl_len
* sizeof(struct in_addr
));
1040 (ni_name
*)malloc(nl
.ninl_len
* sizeof(ni_name
));
1043 (struct in_addr
*)realloc(target_ni
->addrs
,
1044 ((target_ni
->naddrs
+ nl
.ninl_len
) * sizeof(struct in_addr
)));
1046 (ni_name
*)realloc(target_ni
->tags
,
1047 ((target_ni
->naddrs
+ nl
.ninl_len
) * sizeof(ni_name
)));
1050 for (i
=0; i
<nl
.ninl_len
; i
++) {
1051 addr
.s_addr
= inet_addr(nl
.ninl_val
[i
]);
1052 target_ni
->addrs
[target_ni
->naddrs
] = addr
;
1053 target_ni
->tags
[target_ni
->naddrs
] = ni_name_dup(tag
);
1054 target_ni
->naddrs
++;
1057 ni_namelist_free(&nl
);
1066 ni_private
*target_ni
1072 ni_entrylist entries
;
1077 if (ni_root(ni
, &id
) != NI_OK
) {
1081 if (ni_lookup(ni
, &id
, NAME_NAME
, NAME_MACHINES
, &ids
) != NI_OK
) {
1085 id
.nii_object
= ids
.niil_val
[0];
1086 ni_idlist_free(&ids
);
1088 if (ni_list(ni
, &id
, NAME_SERVES
, &entries
) != NI_OK
) {
1092 for (i
= 0; i
< entries
.niel_len
; i
++) {
1093 if (entries
.niel_val
[i
].names
!= NULL
) {
1094 nl
= *entries
.niel_val
[i
].names
;
1095 for (j
= 0; j
< nl
.ninl_len
; j
++) {
1096 if (match(dom
, nl
.ninl_val
[j
], &tag
)) {
1098 entries
.niel_val
[i
].id
,
1110 ni_entrylist_free(&entries
);
1111 return (target_ni
->naddrs
> 0);
1121 ni_private
*target_ni
1127 if (ni_root(ni
, &id
) != NI_OK
) {
1130 if (ni_lookup(ni
, &id
, NAME_NAME
, NAME_MACHINES
, &ids
) != NI_OK
) {
1133 id
.nii_object
= ids
.niil_val
[0];
1134 ni_idlist_free(&ids
);
1136 if (ni_lookup(ni
, &id
, NAME_NAME
, hname
, &ids
) != NI_OK
) {
1139 id
.nii_object
= ids
.niil_val
[0];
1140 ni_idlist_free(&ids
);
1141 if (!addaddr(ni
, id
.nii_object
, tag
, target_ni
)) {
1150 getparent(ni_private
*oldni
, ni_private
**newni
)
1152 ni_rparent_res
*resp
;
1157 struct in_addr raddr
;
1164 * First, find our parent, any parent
1167 resp
= RCALLIT(oldni
, _ni_rparent_2
, NULL
);
1171 if (resp
->status
!= NI_NORESPONSE
) {
1175 syslog(LOG_ERR
, "NetInfo timeout finding server for parent of %s/%s, sleeping",
1176 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0]);
1179 sleep(NI_SLEEPTIME
);
1182 raddr
.s_addr
= htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1184 syslog(LOG_ERR
, "NetInfo %s/%s found parent %s/%s",
1185 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0],
1186 inet_ntoa(raddr
), resp
->ni_rparent_res_u
.binding
.tag
);
1188 if (resp
->status
!= NI_OK
) {
1189 return (resp
->status
);
1195 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
1196 ni
->addrs
[0].s_addr
=htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1197 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1198 ni
->tags
[0] = ni_name_dup(resp
->ni_rparent_res_u
.binding
.tag
);
1200 xdr_free(xdr_ni_rparent_res
, resp
);
1206 if (get_daddr(dupni
, ".", ni
)) {
1209 * Now make sure returned parent is head of
1212 for (i
= 0; i
< ni
->naddrs
; i
++) {
1213 if (ni
->addrs
[i
].s_addr
==
1214 dupni
->addrs
[0].s_addr
) {
1222 * Reuse dupni client info
1224 ni
->tsock
= dupni
->tsock
;
1225 ni
->tport
= dupni
->tport
;
1233 * If returned parent wasn't in list, it's a rogue.
1234 * Log an error and drop the connection.
1237 syslog(LOG_ERR
, "Rogue NetInfo server detected: %s/%s",
1238 inet_ntoa(dupni
->addrs
[0]), dupni
->tags
[0]);
1257 struct sockaddr_in
*sin
,
1264 NIP(ni
)->naddrs
= 1;
1265 NIP(ni
)->addrs
= (struct in_addr
*
1266 )malloc(sizeof(struct in_addr
));
1267 NIP(ni
)->addrs
[0] = sin
->sin_addr
;
1268 NIP(ni
)->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1269 NIP(ni
)->tags
[0] = ni_name_dup(tag
);
1277 struct sockaddr_in
*addr
,
1282 if (!confirm_tcp(ni
, 0)) {
1285 *tag
= ni_name_dup(NIP(ni
)->tags
[0]);
1286 addr
->sin_addr
= NIP(ni
)->addrs
[0];
1287 addr
->sin_port
= htons(NIP(ni
)->tport
);
1288 addr
->sin_family
= AF_INET
;
1289 bzero(addr
->sin_zero
, sizeof(addr
->sin_zero
));
1302 ni_name sep
, addr
, tag
;
1303 struct sockaddr_in sin
;
1306 if (oldni
== NULL
) {
1308 if (!connectlocal(ni
)) {
1312 if (strcmp(domain
, "..") == 0) {
1314 status
= getparent((ni_private
*)oldni
, &ni
);
1316 if (status
!= NI_OK
) {
1319 } else if ((sep
= index(domain
, '@')) != NULL
) {
1321 tag
= strncpy((char *)malloc(sep
-domain
+1), domain
, sep
-domain
);
1322 tag
[sep
-domain
] = '\0';
1323 addr
= strcpy ((char *)malloc(strlen(sep
+1)), sep
+1);
1324 sin
.sin_addr
.s_addr
= inet_addr(addr
);
1325 if (sin
.sin_addr
.s_addr
== INADDR_NONE
) {
1326 he
= gethostbyname(addr
);
1332 bcopy(he
->h_addr_list
[0], &sin
.sin_addr
.s_addr
, he
->h_length
);
1334 ni
= ni_connect(&sin
, tag
);
1337 } else if (strcmp(domain
, ".") != 0) {
1339 * nothing else makes sense
1345 if (strcmp(domain
, "..") == 0) {
1346 status
= getparent((ni_private
*)oldni
, &ni
);
1347 if (status
!= NI_OK
) {
1350 } else if ((sep
= index(domain
, '@')) != NULL
) {
1351 tag
= strncpy((char *)malloc(sep
-domain
+1), domain
, sep
-domain
);
1352 tag
[sep
-domain
] = '\0';
1353 addr
= strcpy ((char *)malloc(strlen(sep
+1)), sep
+1);
1354 sin
.sin_addr
.s_addr
= inet_addr(addr
);
1355 if (sin
.sin_addr
.s_addr
== INADDR_NONE
) {
1356 he
= gethostbyname(addr
);
1362 bcopy(he
->h_addr_list
[0], &sin
.sin_addr
.s_addr
, he
->h_length
);
1364 ni
= ni_connect(&sin
, tag
);
1371 if (!get_daddr(oldni
, (ni_name
)domain
, ni
)) {
1377 return ((void *)ni
);
1388 if (NIP(ni
)->tc
!= NULL
) {
1389 clnt_kill(NIP(ni
)->tc
, NIP(ni
)->tsock
, NIP(ni
)->tport
);
1391 if (NIP(ni
)->naddrs
> 0) {
1392 free(NIP(ni
)->addrs
);
1393 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
1394 ni_name_free(&NIP(ni
)->tags
[i
]);
1396 free(NIP(ni
)->tags
);
1398 if (NIP(ni
)->passwd
!= NULL
) {
1399 ni_name_free(&NIP(ni
)->passwd
);
1406 * The rest of these are just wrappers that end up doing
1407 * RPC calls to the local NetInfo server.
1417 if ((resp
= (ni_proplist
*)RCALLIT(ni
, _ni_statistics_2
, NULL
))
1434 if ((resp
= RCALLIT(ni
, _ni_root_2
, id
)) == NULL
) {
1435 clnt_debug(ni
, "_ni_root");
1438 if (resp
->status
== NI_OK
) {
1439 *id
= resp
->ni_id_res_u
.id
;
1441 return (resp
->status
);
1453 if ((resp
= RCALLIT(ni
, _ni_self_2
, id
)) == NULL
) {
1454 clnt_debug(ni
, "_ni_self");
1457 if (resp
->status
== NI_OK
) {
1458 *id
= resp
->ni_id_res_u
.id
;
1460 return (resp
->status
);
1468 ni_index
*parent_id_p
1471 ni_parent_res
*resp
;
1473 if ((resp
= RCALLIT(ni
, _ni_parent_2
, id
)) == NULL
) {
1474 clnt_debug(ni
, "_ni_parent");
1477 if (resp
->status
== NI_OK
) {
1478 *parent_id_p
= resp
->ni_parent_res_u
.stuff
.object_id
;
1479 *id
= resp
->ni_parent_res_u
.stuff
.self_id
;
1481 return (resp
->status
);
1492 ni_children_res
*resp
;
1494 if ((resp
= RCALLIT(ni
, _ni_children_2
, id
)) == NULL
) {
1495 clnt_debug(ni
, "_ni_children");
1498 if (resp
->status
== NI_OK
) {
1499 *children
= resp
->ni_children_res_u
.stuff
.children
;
1500 *id
= resp
->ni_children_res_u
.stuff
.self_id
;
1502 return (resp
->status
);
1515 ni_create_args args
;
1516 ni_create_res
*resp
;
1518 args
.id
= *parent_id
;
1521 args
.target_id
= NULL
;
1522 if ((resp
= WCALLIT(ni
, _ni_create_2
, &args
)) == NULL
) {
1523 clnt_debug(ni
, "_ni_create");
1526 if (resp
->status
== NI_OK
) {
1527 *child_id_p
= resp
->ni_create_res_u
.stuff
.id
;
1528 *parent_id
= resp
->ni_create_res_u
.stuff
.self_id
;
1530 return (resp
->status
);
1542 ni_destroy_args args
;
1544 args
.parent_id
= *parent_id
;
1545 args
.self_id
= self_id
;
1546 if ((resp
= WCALLIT(ni
, _ni_destroy_2
, &args
)) == NULL
) {
1547 clnt_debug(ni
, "_ni_destroy");
1550 if (resp
->status
== NI_OK
) {
1551 *parent_id
= resp
->ni_id_res_u
.id
;
1553 return (resp
->status
);
1564 ni_proplist_stuff args
;
1569 if ((resp
= WCALLIT(ni
, _ni_write_2
, &args
)) == NULL
) {
1570 clnt_debug(ni
, "_ni_write");
1573 if (resp
->status
== NI_OK
) {
1574 *self_id
= resp
->ni_id_res_u
.id
;
1576 return (resp
->status
);
1587 ni_proplist_res
*resp
;
1589 if ((resp
= RCALLIT(ni
, _ni_read_2
, self_id
)) == NULL
) {
1590 clnt_debug(ni
, "_ni_read");
1593 if (resp
->status
== NI_OK
) {
1594 *self_id
= resp
->ni_proplist_res_u
.stuff
.id
;
1595 *pl
= resp
->ni_proplist_res_u
.stuff
.props
;
1597 return (resp
->status
);
1605 ni_name_const pname
,
1610 ni_lookup_res
*resp
;
1611 ni_lookup_args args
;
1614 args
.key
= (ni_name
)pname
;
1615 args
.value
= (ni_name
)pval
;
1616 if ((resp
= RCALLIT(ni
, _ni_lookup_2
, &args
)) == NULL
) {
1617 clnt_debug(ni
, "_ni_lookup");
1620 if (resp
->status
== NI_OK
) {
1621 *hits
= resp
->ni_lookup_res_u
.stuff
.idlist
;
1622 *id
= resp
->ni_lookup_res_u
.stuff
.self_id
;
1624 return (resp
->status
);
1632 ni_name_const pname
,
1637 ni_proplist_res
*resp
;
1638 ni_lookup_args args
;
1641 args
.key
= (ni_name
)pname
;
1642 args
.value
= (ni_name
)pval
;
1643 if ((resp
= RCALLIT(ni
, _ni_lookupread_2
, &args
)) == NULL
) {
1644 clnt_debug(ni
, "_ni_lookupread");
1647 if (resp
->status
== NI_OK
) {
1648 *props
= resp
->ni_proplist_res_u
.stuff
.props
;
1649 *id
= resp
->ni_proplist_res_u
.stuff
.id
;
1651 return (resp
->status
);
1659 ni_name_const pname
,
1660 ni_entrylist
*entries
1667 args
.name
= (ni_name
)pname
;
1668 if ((resp
= RCALLIT(ni
, _ni_list_2
, &args
)) == NULL
) {
1669 clnt_debug(ni
, "_ni_list");
1672 if (resp
->status
== NI_OK
) {
1673 *entries
= resp
->ni_list_res_u
.stuff
.entries
;
1674 *id
= resp
->ni_list_res_u
.stuff
.self_id
;
1676 return (resp
->status
);
1684 ni_proplist_list
*entries
1687 ni_listall_res
*resp
;
1689 if ((resp
= RCALLIT(ni
, _ni_listall_2
, id
)) == NULL
) {
1690 clnt_debug(ni
, "_ni_listall");
1693 if (resp
->status
== NI_OK
) {
1694 *entries
= resp
->ni_listall_res_u
.stuff
.entries
;
1695 *id
= resp
->ni_listall_res_u
.stuff
.self_id
;
1697 return (resp
->status
);
1706 ni_namelist
*propval_p
1709 ni_namelist_res
*resp
;
1713 args
.prop_index
= which
;
1714 if ((resp
= RCALLIT(ni
, _ni_readprop_2
, &args
)) == NULL
) {
1715 clnt_debug(ni
, "_ni_readprop");
1718 if (resp
->status
== NI_OK
) {
1719 *propval_p
= resp
->ni_namelist_res_u
.stuff
.values
;
1720 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1722 return (resp
->status
);
1735 ni_writeprop_args args
;
1738 args
.prop_index
= which
;
1739 args
.values
= propval
;
1740 if ((resp
= WCALLIT(ni
, _ni_writeprop_2
, &args
)) == NULL
) {
1741 clnt_debug(ni
, "_ni_writeprop");
1744 if (resp
->status
== NI_OK
) {
1745 *id
= resp
->ni_id_res_u
.id
;
1747 return (resp
->status
);
1755 ni_namelist
*propnames
1758 ni_namelist_res
*resp
;
1760 if ((resp
= RCALLIT(ni
, _ni_listprops_2
, id
)) == NULL
) {
1761 clnt_debug(ni
, "_ni_listprops");
1764 if (resp
->status
== NI_OK
) {
1765 *propnames
= resp
->ni_namelist_res_u
.stuff
.values
;
1766 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1768 return (resp
->status
);
1781 ni_createprop_args args
;
1786 if ((resp
= WCALLIT(ni
, _ni_createprop_2
, &args
)) == NULL
) {
1787 clnt_debug(ni
, "_ni_createprop");
1790 if (resp
->status
== NI_OK
) {
1791 *id
= resp
->ni_id_res_u
.id
;
1793 return (resp
->status
);
1808 args
.prop_index
= which
;
1809 if ((resp
= WCALLIT(ni
, _ni_destroyprop_2
, &args
)) == NULL
) {
1810 clnt_debug(ni
, "_ni_destroyprop");
1813 if (resp
->status
== NI_OK
) {
1814 *id
= resp
->ni_id_res_u
.id
;
1816 return (resp
->status
);
1824 ni_index prop_index
,
1829 ni_propname_args args
;
1832 args
.prop_index
= prop_index
;
1833 args
.name
= (ni_name
)name
;
1834 if ((resp
= WCALLIT(ni
, _ni_renameprop_2
, &args
)) == NULL
) {
1835 clnt_debug(ni
, "_ni_renameprop");
1838 if (resp
->status
== NI_OK
) {
1839 *id
= resp
->ni_id_res_u
.id
;
1841 return (resp
->status
);
1849 ni_index prop_index
,
1855 ni_createname_args args
;
1858 args
.prop_index
= prop_index
;
1859 args
.name
= (ni_name
)name
;
1861 if ((resp
= WCALLIT(ni
, _ni_createname_2
, &args
)) == NULL
) {
1862 clnt_debug(ni
, "_ni_createname");
1865 if (resp
->status
== NI_OK
) {
1866 *id
= resp
->ni_id_res_u
.id
;
1868 return (resp
->status
);
1876 ni_index prop_index
,
1881 ni_nameindex_args args
;
1884 args
.prop_index
= prop_index
;
1885 args
.name_index
= name_index
;
1886 if ((resp
= WCALLIT(ni
, _ni_destroyname_2
, &args
)) == NULL
) {
1887 clnt_debug(ni
, "_ni_destroyname");
1890 if (resp
->status
== NI_OK
) {
1891 *id
= resp
->ni_id_res_u
.id
;
1893 return (resp
->status
);
1901 ni_index prop_index
,
1902 ni_index name_index
,
1907 ni_writename_args args
;
1910 args
.prop_index
= prop_index
;
1911 args
.name_index
= name_index
;
1912 args
.name
= (ni_name
)name
;
1913 if ((resp
= WCALLIT(ni
, _ni_writename_2
, &args
)) == NULL
) {
1914 clnt_debug(ni
, "_ni_writename");
1917 if (resp
->status
== NI_OK
) {
1918 *id
= resp
->ni_id_res_u
.id
;
1920 return (resp
->status
);
1928 ni_index prop_index
,
1929 ni_index name_index
,
1933 ni_readname_res
*resp
;
1934 ni_nameindex_args args
;
1937 args
.prop_index
= prop_index
;
1938 args
.name_index
= name_index
;
1939 if ((resp
= RCALLIT(ni
, _ni_readname_2
, &args
)) == NULL
) {
1940 clnt_debug(ni
, "_ni_readname");
1943 if (resp
->status
== NI_OK
) {
1944 *id
= resp
->ni_readname_res_u
.stuff
.id
;
1945 *name
= resp
->ni_readname_res_u
.stuff
.name
;
1947 return (resp
->status
);
1958 if ((resp
= (ni_status
*)RCALLIT(ni
, _ni_resync_2
, NULL
)) == NULL
) {
1977 NIP(ni
)->uid
= getuid();
1978 return (ni_setpassword(ni
, NULL
));
1981 if (ni_root(ni
, &id
) != NI_OK
) {
1984 if (ni_lookup(ni
, &id
, NAME_NAME
, NAME_USERS
, &ids
) != NI_OK
) {
1987 id
.nii_object
= ids
.niil_val
[0];
1988 ni_idlist_free(&ids
);
1990 if (ni_lookup(ni
, &id
, NAME_NAME
, user
, &ids
) != NI_OK
) {
1993 id
.nii_object
= ids
.niil_val
[0];
1994 ni_idlist_free(&ids
);
1995 if (ni_lookupprop(ni
, &id
, NAME_UID
, &nl
) != NI_OK
) {
1998 if (nl
.ninl_len
== 0) {
2001 for (p
= nl
.ninl_val
[0]; *p
; p
++) {
2003 ni_namelist_free(&nl
);
2007 NIP(ni
)->uid
= atoi(nl
.ninl_val
[0]);
2008 if (NIP(ni
)->passwd
== NULL
) {
2009 NIP(ni
)->passwd
= ni_name_dup("");
2011 createauth(NIP(ni
));
2019 ni_name_const passwd
2024 if (NIP(ni
)->passwd
!= NULL
) {
2025 ni_name_free(&NIP(ni
)->passwd
);
2027 if (passwd
== NULL
) {
2028 NIP(ni
)->passwd
= NULL
;
2029 if (NIP(ni
)->tc
!= NULL
) {
2030 auth_destroy(NIP(ni
)->tc
->cl_auth
);
2031 NIP(ni
)->tc
->cl_auth
= authnone_create();
2035 NIP(ni
)->passwd
= ni_name_dup(passwd
);
2037 * Our trivial encryption scheme
2039 for (p
= NIP(ni
)->passwd
; *p
; p
++) {
2042 createauth(NIP(ni
));
2047 extern int bindresvport(int, struct sockaddr_in
*);
2052 * The procedure pmap_getport_to below is derived
2053 * from Sun Microsystems RPC source code. As such the following
2054 * statement applies to it.:
2056 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
2057 * unrestricted use provided that this legend is included on all tape
2058 * media and as a part of the software program in whole or part. Users
2059 * may copy or modify Sun RPC without charge, but are not authorized
2060 * to license or distribute it to anyone else except as part of a product or
2061 * program developed by the user.
2063 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
2064 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
2065 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
2067 * Sun RPC is provided with no support and without any obligation on the
2068 * part of Sun Microsystems, Inc. to assist in its use, correction,
2069 * modification or enhancement.
2071 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2072 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2073 * OR ANY PART THEREOF.
2075 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2076 * or profits or other special, indirect and consequential damages, even if
2077 * Sun has been advised of the possibility of such damages.
2079 * Sun Microsystems, Inc.
2080 * 2550 Garcia Avenue
2081 * Mountain View, California 94043
2084 * Client interface to pmap rpc service.
2086 * Find the mapped port for program,version.
2087 * Calls the pmap service remotely to do the lookup.
2088 * Returns 0 if no map exists.
2091 pmap_getport_to(address
, program
, version
, protocol
, timeout_secs
, ntries
)
2092 struct sockaddr_in
*address
;
2101 register CLIENT
*client
;
2103 struct timeval timeout
;
2105 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
2109 address
->sin_port
= htons(PMAPPORT
);
2110 timeout
.tv_usec
= ((timeout_secs
% ntries
) * 1000000) / ntries
;
2111 timeout
.tv_sec
= (timeout_secs
/ ntries
);
2112 client
= clntudp_bufcreate(address
, PMAPPROG
,
2113 PMAPVERS
, timeout
, &sock
, RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
2114 if (client
!= (CLIENT
*)NULL
) {
2115 parms
.pm_prog
= program
;
2116 parms
.pm_vers
= version
;
2117 parms
.pm_prot
= protocol
;
2118 parms
.pm_port
= 0; /* not needed or used */
2119 timeout
.tv_usec
= 0;
2120 timeout
.tv_sec
= timeout_secs
;
2121 if (CLNT_CALL(client
, PMAPPROC_GETPORT
, xdr_pmap
, &parms
,
2122 xdr_u_short
, &port
, timeout
) != RPC_SUCCESS
){
2123 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
2124 clnt_geterr(client
, &rpc_createerr
.cf_error
);
2126 } else if (port
== 0) {
2127 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
2130 if (client
!= NULL
) {
2131 clnt_destroy(client
);
2134 address
->sin_port
= 0;
2140 * Open a socket, but do not use the default portmap timeout
2144 struct sockaddr_in
*raddr
,
2156 * If no port number given ask the pmap for one
2158 if (raddr
->sin_port
== 0) {
2160 if ((port
= pmap_getport_to(raddr
, prog
, vers
,
2161 IPPROTO_UDP
, timeout
,
2165 raddr
->sin_port
= htons(port
);
2168 sock
= socket(AF_INET
, proto
== IPPROTO_UDP
? SOCK_DGRAM
: SOCK_STREAM
,
2173 (void)bindresvport(sock
, (struct sockaddr_in
*)0);
2174 setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(int));
2175 if (proto
== IPPROTO_TCP
) {
2176 if (connect(sock
, (struct sockaddr
*)raddr
,
2177 sizeof(*raddr
)) < 0) {