1 /* $OpenBSD: bootparamd.c,v 1.14 2002/07/19 02:18:06 deraadt Exp $ */ 2 3 /* 4 * This code is not copyright, and is placed in the public domain. 5 * Feel free to use and modify. Please send modifications and/or 6 * suggestions + bug fixes to Klas Heggemann <klas@nada.kth.se> 7 * 8 * Various small changes by Theo de Raadt <deraadt@fsa.ca> 9 * Parser rewritten (adding YP support) by Roland McGrath <roland@frob.com> 10 */ 11 12 #include <sys/types.h> 13 #include <sys/ioctl.h> 14 #include <sys/stat.h> 15 #include <sys/socket.h> 16 17 #include <rpc/rpc.h> 18 #include <rpcsvc/bootparam_prot.h> 19 #include <rpcsvc/ypclnt.h> 20 #include <rpcsvc/yp_prot.h> 21 #include <arpa/inet.h> 22 23 #include <stdio.h> 24 #include <netdb.h> 25 #include <ctype.h> 26 #include <syslog.h> 27 #include <string.h> 28 #include <unistd.h> 29 #include <err.h> 30 #include <stdlib.h> 31 32 #include "pathnames.h" 33 34 #define MAXLEN 800 35 36 struct hostent *he; 37 static char hostname[MAX_MACHINE_NAME]; 38 static char askname[MAX_MACHINE_NAME]; 39 static char domain_name[MAX_MACHINE_NAME]; 40 41 extern void bootparamprog_1(struct svc_req *, SVCXPRT *); 42 int lookup_bootparam(char *client, char *client_canonical, char *id, 43 char **server, char **path); 44 45 int _rpcsvcdirty = 0; 46 int _rpcpmstart = 0; 47 int debug = 0; 48 int dolog = 0; 49 struct in_addr route_addr; 50 struct sockaddr_in my_addr; 51 extern char *__progname; 52 char *bootpfile = _PATH_BOOTPARAMS; 53 54 extern char *optarg; 55 extern int optind; 56 57 void 58 usage(void) 59 { 60 fprintf(stderr, 61 "usage: rpc.bootparamd [-d] [-s] [-r router] [-f bootparmsfile]\n"); 62 exit(1); 63 } 64 65 66 /* 67 * ever familiar 68 */ 69 int 70 main(int argc, char *argv[]) 71 { 72 struct hostent *he; 73 struct stat buf; 74 SVCXPRT *transp; 75 int c; 76 77 while ((c = getopt(argc, argv, "dsr:f:")) != -1) 78 switch (c) { 79 case 'd': 80 debug = 1; 81 break; 82 case 'r': 83 if (inet_aton(optarg, &route_addr) == 1) 84 break; 85 he = gethostbyname(optarg); 86 if (!he) { 87 warnx("no such host: %s", optarg); 88 usage(); 89 } 90 bcopy(he->h_addr, (char *) &route_addr.s_addr, 91 sizeof(route_addr.s_addr)); 92 break; 93 case 'f': 94 bootpfile = optarg; 95 break; 96 case 's': 97 dolog = 1; 98 #ifndef LOG_DAEMON 99 openlog(__progname, 0, 0); 100 #else 101 openlog(__progname, 0, LOG_DAEMON); 102 setlogmask(LOG_UPTO(LOG_NOTICE)); 103 #endif 104 break; 105 default: 106 usage(); 107 } 108 109 if (stat(bootpfile, &buf)) 110 err(1, "%s", bootpfile); 111 112 if (!route_addr.s_addr) { 113 get_myaddress(&my_addr); 114 bcopy(&my_addr.sin_addr.s_addr, &route_addr.s_addr, 115 sizeof(route_addr.s_addr)); 116 } 117 if (!debug) { 118 if (daemon(0, 0)) 119 err(1, "can't detach from terminal"); 120 } 121 122 (void) pmap_unset(BOOTPARAMPROG, BOOTPARAMVERS); 123 124 transp = svcudp_create(RPC_ANYSOCK); 125 if (transp == NULL) 126 errx(1, "can't create udp service"); 127 128 if (!svc_register(transp, BOOTPARAMPROG, BOOTPARAMVERS, bootparamprog_1, 129 IPPROTO_UDP)) 130 errx(1, "unable to register BOOTPARAMPROG version %ld, udp", 131 BOOTPARAMVERS); 132 133 svc_run(); 134 errx(1, "svc_run returned"); 135 } 136 137 bp_whoami_res * 138 bootparamproc_whoami_1_svc(bp_whoami_arg *whoami, struct svc_req *rqstp) 139 { 140 in_addr_t haddr; 141 static bp_whoami_res res; 142 143 if (debug) 144 warnx("whoami got question for %d.%d.%d.%d", 145 255 & whoami->client_address.bp_address_u.ip_addr.net, 146 255 & whoami->client_address.bp_address_u.ip_addr.host, 147 255 & whoami->client_address.bp_address_u.ip_addr.lh, 148 255 & whoami->client_address.bp_address_u.ip_addr.impno); 149 if (dolog) 150 syslog(LOG_NOTICE, "whoami got question for %d.%d.%d.%d", 151 255 & whoami->client_address.bp_address_u.ip_addr.net, 152 255 & whoami->client_address.bp_address_u.ip_addr.host, 153 255 & whoami->client_address.bp_address_u.ip_addr.lh, 154 255 & whoami->client_address.bp_address_u.ip_addr.impno); 155 156 bcopy((char *) &whoami->client_address.bp_address_u.ip_addr, 157 &haddr, sizeof(haddr)); 158 he = gethostbyaddr((char *) &haddr, sizeof(haddr), AF_INET); 159 if (!he) 160 goto failed; 161 162 if (debug) 163 warnx("This is host %s", he->h_name); 164 if (dolog) 165 syslog(LOG_NOTICE, "This is host %s", he->h_name); 166 167 strlcpy(askname, he->h_name, sizeof askname); 168 if (!lookup_bootparam(askname, hostname, NULL, NULL, NULL)) { 169 res.client_name = hostname; 170 getdomainname(domain_name, MAX_MACHINE_NAME); 171 res.domain_name = domain_name; 172 173 if (res.router_address.address_type != IP_ADDR_TYPE) { 174 res.router_address.address_type = IP_ADDR_TYPE; 175 bcopy(&route_addr.s_addr, 176 &res.router_address.bp_address_u.ip_addr, 4); 177 } 178 if (debug) 179 warnx("Returning %s %s %d.%d.%d.%d", 180 res.client_name, res.domain_name, 181 255 & res.router_address.bp_address_u.ip_addr.net, 182 255 & res.router_address.bp_address_u.ip_addr.host, 183 255 & res.router_address.bp_address_u.ip_addr.lh, 184 255 & res.router_address.bp_address_u.ip_addr.impno); 185 if (dolog) 186 syslog(LOG_NOTICE, "Returning %s %s %d.%d.%d.%d", 187 res.client_name, res.domain_name, 188 255 & res.router_address.bp_address_u.ip_addr.net, 189 255 & res.router_address.bp_address_u.ip_addr.host, 190 255 & res.router_address.bp_address_u.ip_addr.lh, 191 255 & res.router_address.bp_address_u.ip_addr.impno); 192 return (&res); 193 } 194 failed: 195 if (debug) 196 warnx("whoami failed"); 197 if (dolog) 198 syslog(LOG_NOTICE, "whoami failed"); 199 return (NULL); 200 } 201 202 203 bp_getfile_res * 204 bootparamproc_getfile_1_svc(bp_getfile_arg *getfile, struct svc_req *rqstp) 205 { 206 static bp_getfile_res res; 207 int err; 208 209 if (debug) 210 warnx("getfile got question for \"%s\" and file \"%s\"", 211 getfile->client_name, getfile->file_id); 212 213 if (dolog) 214 syslog(LOG_NOTICE, 215 "getfile got question for \"%s\" and file \"%s\"", 216 getfile->client_name, getfile->file_id); 217 218 he = NULL; 219 he = gethostbyname(getfile->client_name); 220 if (!he) 221 goto failed; 222 223 strlcpy(askname, he->h_name, sizeof askname); 224 err = lookup_bootparam(askname, NULL, getfile->file_id, 225 &res.server_name, &res.server_path); 226 if (err == 0) { 227 he = gethostbyname(res.server_name); 228 if (!he) 229 goto failed; 230 bcopy(he->h_addr, &res.server_address.bp_address_u.ip_addr, 4); 231 res.server_address.address_type = IP_ADDR_TYPE; 232 } else if (err == ENOENT && !strcmp(getfile->file_id, "dump")) { 233 /* Special for dump, answer with null strings. */ 234 res.server_name[0] = '\0'; 235 res.server_path[0] = '\0'; 236 bzero(&res.server_address.bp_address_u.ip_addr, 4); 237 } else { 238 failed: 239 if (debug) 240 warnx("getfile failed for %s", getfile->client_name); 241 if (dolog) 242 syslog(LOG_NOTICE, 243 "getfile failed for %s", getfile->client_name); 244 return (NULL); 245 } 246 247 if (debug) 248 warnx("returning server:%s path:%s address: %d.%d.%d.%d", 249 res.server_name, res.server_path, 250 255 & res.server_address.bp_address_u.ip_addr.net, 251 255 & res.server_address.bp_address_u.ip_addr.host, 252 255 & res.server_address.bp_address_u.ip_addr.lh, 253 255 & res.server_address.bp_address_u.ip_addr.impno); 254 if (dolog) 255 syslog(LOG_NOTICE, 256 "returning server:%s path:%s address: %d.%d.%d.%d", 257 res.server_name, res.server_path, 258 255 & res.server_address.bp_address_u.ip_addr.net, 259 255 & res.server_address.bp_address_u.ip_addr.host, 260 255 & res.server_address.bp_address_u.ip_addr.lh, 261 255 & res.server_address.bp_address_u.ip_addr.impno); 262 return (&res); 263 } 264 265 int 266 lookup_bootparam(char *client, char *client_canonical, char *id, 267 char **server, char **path) 268 { 269 FILE *f = fopen(bootpfile, "r"); 270 #ifdef YP 271 static char *ypbuf = NULL; 272 static int ypbuflen = 0; 273 #endif 274 static char buf[BUFSIZ]; 275 char *bp, *word = NULL; 276 size_t idlen = id == NULL ? 0 : strlen(id); 277 int contin = 0, found = 0; 278 279 if (f == NULL) 280 return EINVAL; /* ? */ 281 282 while (fgets(buf, sizeof buf, f)) { 283 int wascontin = contin; 284 285 contin = buf[strlen(buf) - 2] == '\\'; 286 bp = buf + strspn(buf, " \t\n"); 287 288 switch (wascontin) { 289 case -1: 290 /* Continuation of uninteresting line */ 291 contin *= -1; 292 continue; 293 case 0: 294 /* New line */ 295 contin *= -1; 296 if (*bp == '#') 297 continue; 298 if ((word = strsep(&bp, " \t\n")) == NULL) 299 continue; 300 #ifdef YP 301 /* A + in the file means try YP now */ 302 if (!strcmp(word, "+")) { 303 char *ypdom; 304 305 if (yp_get_default_domain(&ypdom) || 306 yp_match(ypdom, "bootparams", client, 307 strlen(client), &ypbuf, &ypbuflen)) 308 continue; 309 bp = ypbuf; 310 word = client; 311 contin *= -1; 312 break; 313 } 314 #endif 315 /* See if this line's client is the one we are 316 * looking for */ 317 if (strcasecmp(word, client) != 0) { 318 /* 319 * If it didn't match, try getting the 320 * canonical host name of the client 321 * on this line and comparing that to 322 * the client we are looking for 323 */ 324 struct hostent *hp = gethostbyname(word); 325 if (hp == NULL || strcasecmp(hp->h_name, client)) 326 continue; 327 } 328 contin *= -1; 329 break; 330 case 1: 331 /* Continued line we want to parse below */ 332 break; 333 } 334 335 if (client_canonical) 336 strlcpy(client_canonical, word, MAX_MACHINE_NAME); 337 338 /* We have found a line for CLIENT */ 339 if (id == NULL) { 340 (void) fclose(f); 341 return 0; 342 } 343 344 /* Look for a value for the parameter named by ID */ 345 while ((word = strsep(&bp, " \t\n")) != NULL) { 346 if (!strncmp(word, id, idlen) && word[idlen] == '=') { 347 /* We have found the entry we want */ 348 *server = &word[idlen + 1]; 349 *path = strchr(*server, ':'); 350 if (*path == NULL) 351 /* Malformed entry */ 352 continue; 353 *(*path)++ = '\0'; 354 (void) fclose(f); 355 return 0; 356 } 357 } 358 359 found = 1; 360 } 361 362 (void) fclose(f); 363 return found ? ENOENT : EPERM; 364 } 365