]> git.saurik.com Git - apple/network_cmds.git/blob - bootparams/bootparamd.tproj/bootparam_proc.c
b9e752e551c87f1722c69c875d96f319a2c5cc67
[apple/network_cmds.git] / bootparams / bootparamd.tproj / bootparam_proc.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * BOOTPARAMS server
25 * Copyright 1998, Apple Computer Inc. Unpublished.
26 *
27 * Written by Marc Majka
28 */
29
30 #include <rpc/rpc.h>
31 #include "bootparam_prot.h"
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netdb.h>
39 #include <bootparams.h>
40 #include <syslog.h>
41
42 extern int debug;
43 extern unsigned long route_addr;
44 extern char domain_name[];
45
46 struct hostent *h;
47 static char hostname[MAXHOSTNAMELEN];
48
49 bp_whoami_res *
50 bootparamproc_whoami_1_svc(whoami, req)
51 bp_whoami_arg *whoami;
52 struct svc_req *req;
53 {
54 long haddr;
55 static bp_whoami_res res;
56 struct bootparamsent *bp;
57
58 if (debug)
59 {
60 fprintf(stderr,"whoami %d.%d.%d.%d\n",
61 255 & whoami->client_address.bp_address_u.ip_addr.net,
62 255 & whoami->client_address.bp_address_u.ip_addr.host,
63 255 & whoami->client_address.bp_address_u.ip_addr.lh,
64 255 & whoami->client_address.bp_address_u.ip_addr.impno);
65 }
66
67 bcopy((char *)&whoami->client_address.bp_address_u.ip_addr,
68 (char *)&haddr, sizeof(haddr));
69 h = gethostbyaddr((char *)&haddr, sizeof(haddr), AF_INET);
70 if (h == NULL)
71 {
72 if (debug) fprintf(stderr,"whoami failed: gethostbyaddr\n");
73 return NULL;
74 }
75
76 /* check whether subsequent bpgetfile requests would succeed */
77 bp = bootparams_getbyname(h->h_name);
78 if (bp == NULL) {
79 if (debug)
80 fprintf(stderr, "whoami failed: bootparams_getbyname\n");
81 return NULL;
82 }
83
84 sprintf(hostname, "%s", h->h_name);
85 res.client_name = hostname;
86
87 res.domain_name = domain_name;
88
89 res.router_address.address_type = IP_ADDR_TYPE;
90 bcopy(&route_addr, &res.router_address.bp_address_u.ip_addr, 4);
91
92 if (debug)
93 {
94 fprintf(stderr, "whoami name=%s domain=%s router=%d.%d.%d.%d\n",
95 res.client_name,
96 res.domain_name,
97 255 & res.router_address.bp_address_u.ip_addr.net,
98 255 & res.router_address.bp_address_u.ip_addr.host,
99 255 & res.router_address.bp_address_u.ip_addr.lh,
100 255 & res.router_address.bp_address_u.ip_addr.impno);
101 }
102
103 return (&res);
104 }
105
106 bp_getfile_res *
107 bootparamproc_getfile_1_svc(getfile, req)
108 bp_getfile_arg *getfile;
109 struct svc_req *req;
110 {
111 static bp_getfile_res res;
112 struct bootparamsent *bp;
113 static char s[1024];
114 char *p;
115 int i, len;
116
117 if (debug)
118 {
119 fprintf(stderr, "getfile %s %s\n",
120 getfile->client_name, getfile->file_id);
121 }
122
123 bp = bootparams_getbyname(getfile->client_name);
124 if (bp == NULL)
125 {
126 if (debug)
127 {
128 fprintf(stderr, "can't find bootparams for %s\n",
129 getfile->client_name);
130 fprintf(stderr, "getfile failed\n");
131 }
132 return NULL;
133 }
134
135 len = strlen(getfile->file_id) + 1;
136 sprintf(s, "%s=", getfile->file_id);
137
138 for (i = 0; bp->bp_bootparams[i] != NULL; i++)
139 {
140 if (!strncmp(s, bp->bp_bootparams[i], len)) break;
141 }
142
143 if (bp->bp_bootparams[i] == NULL)
144 {
145 if (debug)
146 {
147 fprintf(stderr, "can't find bootparam %s\n", getfile->file_id);
148 fprintf(stderr, "getfile failed\n");
149 }
150 return NULL;
151 }
152
153 sprintf(s, bp->bp_bootparams[i] + len);
154 p = strchr(s, ':');
155 if ((p == NULL) || (p == s))
156 {
157 hostname[0] = '\0';
158 res.server_name = hostname;
159 res.server_address.bp_address_u.ip_addr.net = 0;
160 res.server_address.bp_address_u.ip_addr.host = 0;
161 res.server_address.bp_address_u.ip_addr.lh = 0;
162 res.server_address.bp_address_u.ip_addr.impno = 0;
163 res.server_address.address_type = 1;
164
165 if (p == NULL) res.server_path = s;
166 else res.server_path = s + 1;
167
168 if (debug)
169 {
170 fprintf(stderr, "getfile server=%s (%d.%d.%d.%d) path=%s\n",
171 res.server_name,
172 255 & res.server_address.bp_address_u.ip_addr.net,
173 255 & res.server_address.bp_address_u.ip_addr.host,
174 255 & res.server_address.bp_address_u.ip_addr.lh,
175 255 & res.server_address.bp_address_u.ip_addr.impno,
176 res.server_path);
177 }
178 return (&res);
179 }
180
181 *p = '\0';
182 p++;
183 h = gethostbyname(s);
184 if (h == NULL)
185 {
186 if (debug)
187 {
188 fprintf(stderr, "can't find server %s\n", s);
189 fprintf(stderr, "getfile failed\n");
190 }
191 return NULL;
192 }
193
194 res.server_name = s;
195 res.server_path = p;
196 bcopy(h->h_addr, &res.server_address.bp_address_u.ip_addr, h->h_length);
197 res.server_address.address_type = IP_ADDR_TYPE;
198
199 if (debug)
200 {
201 fprintf(stderr, "getfile server=%s (%d.%d.%d.%d) path=%s\n",
202 res.server_name,
203 255 & res.server_address.bp_address_u.ip_addr.net,
204 255 & res.server_address.bp_address_u.ip_addr.host,
205 255 & res.server_address.bp_address_u.ip_addr.lh,
206 255 & res.server_address.bp_address_u.ip_addr.impno,
207 res.server_path);
208 }
209
210 return (&res);
211 }