1 /* $OpenBSD: utils.c,v 1.14 2015/08/20 22:39:29 deraadt Exp $ */ 2 /* $NetBSD: utils.c,v 1.5.2.1 1995/11/14 08:45:46 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1988, 1992 The University of Utah and the Center 6 * for Software Science (CSS). 7 * Copyright (c) 1992, 1993 8 * The Regents of the University of California. All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * the Center for Software Science of the University of Utah Computer 12 * Science Department. CSS requests users of this software to return 13 * to css-dist@cs.utah.edu any improvements that they make and grant 14 * CSS redistribution rights. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * from: @(#)utils.c 8.1 (Berkeley) 6/4/93 41 * 42 * From: Utah Hdr: utils.c 3.1 92/07/06 43 * Author: Jeff Forys, University of Utah CSS 44 */ 45 46 #include <fcntl.h> 47 #include <signal.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <syslog.h> 52 #include <time.h> 53 #include <unistd.h> 54 #include "defs.h" 55 56 /* 57 ** DispPkt -- Display the contents of an RMPCONN packet. 58 ** 59 ** Parameters: 60 ** rconn - packet to be displayed. 61 ** direct - direction packet is going (DIR_*). 62 ** 63 ** Returns: 64 ** Nothing. 65 ** 66 ** Side Effects: 67 ** None. 68 */ 69 void 70 DispPkt(RMPCONN *rconn, int direct) 71 { 72 static const char BootFmt[] = 73 "\t\tRetCode:%u SeqNo:%x SessID:%x Vers:%u"; 74 static const char ReadFmt[] = 75 "\t\tRetCode:%u Offset:%x SessID:%x\n"; 76 struct tm *tmp; 77 struct rmp_packet *rmp; 78 int i, omask; 79 u_int32_t t; 80 time_t tim; 81 82 /* 83 * Since we will be working with RmpConns as well as DbgFp, we 84 * must block signals that can affect either. 85 */ 86 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGUSR1)|sigmask(SIGUSR2)); 87 88 if (DbgFp == NULL) { /* sanity */ 89 (void) sigsetmask(omask); 90 return; 91 } 92 93 /* display direction packet is going using '>>>' or '<<<' */ 94 fputs((direct==DIR_RCVD)?"<<< ":(direct==DIR_SENT)?">>> ":"", DbgFp); 95 96 /* display packet timestamp */ 97 98 tim = rconn->tstamp.tv_sec; 99 tmp = localtime(&tim); 100 fprintf(DbgFp, "%02d:%02d:%02d.%06ld ", tmp->tm_hour, tmp->tm_min, 101 tmp->tm_sec, rconn->tstamp.tv_usec); 102 103 /* display src or dst addr and information about network interface */ 104 fprintf(DbgFp, "Addr: %s Intf: %s\n", EnetStr(rconn), IntfName); 105 106 rmp = &rconn->rmp; 107 108 /* display IEEE 802.2 Logical Link Control header */ 109 (void) fprintf(DbgFp, "\t802.2 LLC: DSAP:%x SSAP:%x CTRL:%x\n", 110 rmp->hp_llc.dsap, rmp->hp_llc.ssap, ntohs(rmp->hp_llc.cntrl)); 111 112 /* display HP extensions to 802.2 Logical Link Control header */ 113 (void) fprintf(DbgFp, "\tHP Ext: DXSAP:%x SXSAP:%x\n", 114 ntohs(rmp->hp_llc.dxsap), ntohs(rmp->hp_llc.sxsap)); 115 116 /* 117 * Display information about RMP packet using type field to 118 * determine what kind of packet this is. 119 */ 120 switch (rmp->r_type) { 121 case RMP_BOOT_REQ: /* boot request */ 122 (void) fprintf(DbgFp, "\tBoot Request:"); 123 GETWORD(rmp->r_brq.rmp_seqno, t); 124 if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) { 125 if (WORDZE(rmp->r_brq.rmp_seqno)) 126 fputs(" (Send Server ID)", DbgFp); 127 else 128 fprintf(DbgFp," (Send Filename #%u)",t); 129 } 130 (void) fputc('\n', DbgFp); 131 (void) fprintf(DbgFp, BootFmt, rmp->r_brq.rmp_retcode, 132 t, ntohs(rmp->r_brq.rmp_session), 133 ntohs(rmp->r_brq.rmp_version)); 134 (void) fprintf(DbgFp, "\n\t\tMachine Type: "); 135 for (i = 0; i < RMP_MACHLEN; i++) 136 (void) fputc(rmp->r_brq.rmp_machtype[i], DbgFp); 137 DspFlnm(rmp->r_brq.rmp_flnmsize, &rmp->r_brq.rmp_flnm); 138 break; 139 case RMP_BOOT_REPL: /* boot reply */ 140 fprintf(DbgFp, "\tBoot Reply:\n"); 141 GETWORD(rmp->r_brpl.rmp_seqno, t); 142 (void) fprintf(DbgFp, BootFmt, rmp->r_brpl.rmp_retcode, 143 t, ntohs(rmp->r_brpl.rmp_session), 144 ntohs(rmp->r_brpl.rmp_version)); 145 DspFlnm(rmp->r_brpl.rmp_flnmsize,&rmp->r_brpl.rmp_flnm); 146 break; 147 case RMP_READ_REQ: /* read request */ 148 (void) fprintf(DbgFp, "\tRead Request:\n"); 149 GETWORD(rmp->r_rrq.rmp_offset, t); 150 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrq.rmp_retcode, 151 t, ntohs(rmp->r_rrq.rmp_session)); 152 (void) fprintf(DbgFp, "\t\tNoOfBytes: %u\n", 153 ntohs(rmp->r_rrq.rmp_size)); 154 break; 155 case RMP_READ_REPL: /* read reply */ 156 (void) fprintf(DbgFp, "\tRead Reply:\n"); 157 GETWORD(rmp->r_rrpl.rmp_offset, t); 158 (void) fprintf(DbgFp, ReadFmt, rmp->r_rrpl.rmp_retcode, 159 t, ntohs(rmp->r_rrpl.rmp_session)); 160 (void) fprintf(DbgFp, "\t\tNoOfBytesSent: %ld\n", 161 (long)(rconn->rmplen - RMPREADSIZE(0))); 162 break; 163 case RMP_BOOT_DONE: /* boot complete */ 164 (void) fprintf(DbgFp, "\tBoot Complete:\n"); 165 (void) fprintf(DbgFp, "\t\tRetCode:%u SessID:%x\n", 166 rmp->r_done.rmp_retcode, 167 ntohs(rmp->r_done.rmp_session)); 168 break; 169 default: /* ??? */ 170 (void) fprintf(DbgFp, "\tUnknown Type:(%d)\n", 171 rmp->r_type); 172 break; 173 } 174 (void) fputc('\n', DbgFp); 175 (void) fflush(DbgFp); 176 177 (void) sigsetmask(omask); /* reset old signal mask */ 178 } 179 180 181 /* 182 ** GetEtherAddr -- convert an RMP (Ethernet) address into a string. 183 ** 184 ** An RMP BOOT packet has been received. Look at the type field 185 ** and process Boot Requests, Read Requests, and Boot Complete 186 ** packets. Any other type will be dropped with a warning msg. 187 ** 188 ** Parameters: 189 ** addr - array of RMP_ADDRLEN bytes. 190 ** 191 ** Returns: 192 ** Pointer to static string representation of `addr'. 193 ** 194 ** Side Effects: 195 ** None. 196 ** 197 ** Warnings: 198 ** - The return value points to a static buffer; it must 199 ** be copied if it's to be saved. 200 */ 201 char * 202 GetEtherAddr(u_int8_t *addr) 203 { 204 static char Hex[] = "0123456789abcdef"; 205 static char etherstr[RMP_ADDRLEN*3]; 206 int i; 207 char *cp; 208 209 /* 210 * For each byte in `addr', convert it to "<hexchar><hexchar>:". 211 * The last byte does not get a trailing `:' appended. 212 */ 213 i = 0; 214 cp = etherstr; 215 for(;;) { 216 *cp++ = Hex[*addr >> 4 & 0xf]; 217 *cp++ = Hex[*addr++ & 0xf]; 218 if (++i == RMP_ADDRLEN) 219 break; 220 *cp++ = ':'; 221 } 222 *cp = '\0'; 223 224 return(etherstr); 225 } 226 227 228 /* 229 ** DispFlnm -- Print a string of bytes to DbgFp (often, a file name). 230 ** 231 ** Parameters: 232 ** size - number of bytes to print. 233 ** flnm - address of first byte. 234 ** 235 ** Returns: 236 ** Nothing. 237 ** 238 ** Side Effects: 239 ** - Characters are sent to `DbgFp'. 240 */ 241 void 242 DspFlnm(u_int size, char *flnm) 243 { 244 int i; 245 246 (void) fprintf(DbgFp, "\n\t\tFile Name (%u): <", size); 247 for (i = 0; i < size; i++) 248 (void) fputc(*flnm++, DbgFp); 249 (void) fputs(">\n", DbgFp); 250 } 251 252 253 /* 254 ** NewClient -- allocate memory for a new CLIENT. 255 ** 256 ** Parameters: 257 ** addr - RMP (Ethernet) address of new client. 258 ** 259 ** Returns: 260 ** Ptr to new CLIENT or NULL if we ran out of memory. 261 ** 262 ** Side Effects: 263 ** - Memory will be malloc'd for the new CLIENT. 264 ** - If malloc() fails, a log message will be generated. 265 */ 266 CLIENT * 267 NewClient(u_int8_t *addr) 268 { 269 CLIENT *ctmp; 270 271 if ((ctmp = malloc(sizeof(CLIENT))) == NULL) { 272 syslog(LOG_ERR, "NewClient: out of memory (%s)", 273 GetEtherAddr(addr)); 274 return(NULL); 275 } 276 277 bzero(ctmp, sizeof(CLIENT)); 278 bcopy(addr, &ctmp->addr[0], RMP_ADDRLEN); 279 return(ctmp); 280 } 281 282 /* 283 ** FreeClient -- free linked list of Clients. 284 ** 285 ** Parameters: 286 ** None. 287 ** 288 ** Returns: 289 ** Nothing. 290 ** 291 ** Side Effects: 292 ** - All malloc'd memory associated with the linked list of 293 ** CLIENTS will be free'd; `Clients' will be set to NULL. 294 ** 295 ** Warnings: 296 ** - This routine must be called with SIGHUP blocked. 297 */ 298 void 299 FreeClients(void) 300 { 301 CLIENT *ctmp; 302 303 while (Clients != NULL) { 304 ctmp = Clients; 305 Clients = Clients->next; 306 FreeClient(ctmp); 307 } 308 } 309 310 /* 311 ** NewStr -- allocate memory for a character array. 312 ** 313 ** Parameters: 314 ** str - null terminated character array. 315 ** 316 ** Returns: 317 ** Ptr to new character array or NULL if we ran out of memory. 318 ** 319 ** Side Effects: 320 ** - Memory will be malloc'd for the new character array. 321 ** - If malloc() fails, a log message will be generated. 322 */ 323 char * 324 NewStr(char *str) 325 { 326 char *stmp; 327 328 stmp = strdup(str); 329 if (stmp == NULL) { 330 syslog(LOG_ERR, "NewStr: out of memory (%s)", str); 331 return(NULL); 332 } 333 return(stmp); 334 } 335 336 /* 337 ** To save time, NewConn and FreeConn maintain a cache of one RMPCONN 338 ** in `LastFree' (defined below). 339 */ 340 341 static RMPCONN *LastFree = NULL; 342 343 /* 344 ** NewConn -- allocate memory for a new RMPCONN connection. 345 ** 346 ** Parameters: 347 ** rconn - initialization template for new connection. 348 ** 349 ** Returns: 350 ** Ptr to new RMPCONN or NULL if we ran out of memory. 351 ** 352 ** Side Effects: 353 ** - Memory may be malloc'd for the new RMPCONN (if not cached). 354 ** - If malloc() fails, a log message will be generated. 355 */ 356 RMPCONN * 357 NewConn(RMPCONN *rconn) 358 { 359 RMPCONN *rtmp; 360 361 if (LastFree == NULL) { /* nothing cached; make a new one */ 362 if ((rtmp = malloc(sizeof(RMPCONN))) == NULL) { 363 syslog(LOG_ERR, "NewConn: out of memory (%s)", 364 EnetStr(rconn)); 365 return(NULL); 366 } 367 } else { /* use the cached RMPCONN */ 368 rtmp = LastFree; 369 LastFree = NULL; 370 } 371 372 /* 373 * Copy template into `rtmp', init file descriptor to `-1' and 374 * set ptr to next elem NULL. 375 */ 376 bcopy((char *)rconn, (char *)rtmp, sizeof(RMPCONN)); 377 rtmp->bootfd = -1; 378 rtmp->next = NULL; 379 380 return(rtmp); 381 } 382 383 /* 384 ** FreeConn -- Free memory associated with an RMPCONN connection. 385 ** 386 ** Parameters: 387 ** rtmp - ptr to RMPCONN to be free'd. 388 ** 389 ** Returns: 390 ** Nothing. 391 ** 392 ** Side Effects: 393 ** - Memory associated with `rtmp' may be free'd (or cached). 394 ** - File desc associated with `rtmp->bootfd' will be closed. 395 */ 396 void 397 FreeConn(RMPCONN *rtmp) 398 { 399 /* 400 * If the file descriptor is in use, close the file. 401 */ 402 if (rtmp->bootfd >= 0) { 403 (void) close(rtmp->bootfd); 404 rtmp->bootfd = -1; 405 } 406 407 if (LastFree == NULL) /* cache for next time */ 408 rtmp = LastFree; 409 else /* already one cached; free this one */ 410 free((char *)rtmp); 411 } 412 413 /* 414 ** FreeConns -- free linked list of RMPCONN connections. 415 ** 416 ** Parameters: 417 ** None. 418 ** 419 ** Returns: 420 ** Nothing. 421 ** 422 ** Side Effects: 423 ** - All malloc'd memory associated with the linked list of 424 ** connections will be free'd; `RmpConns' will be set to NULL. 425 ** - If LastFree is != NULL, it too will be free'd & NULL'd. 426 ** 427 ** Warnings: 428 ** - This routine must be called with SIGHUP blocked. 429 */ 430 void 431 FreeConns(void) 432 { 433 RMPCONN *rtmp; 434 435 while (RmpConns != NULL) { 436 rtmp = RmpConns; 437 RmpConns = RmpConns->next; 438 FreeConn(rtmp); 439 } 440 441 if (LastFree != NULL) { 442 free((char *)LastFree); 443 LastFree = NULL; 444 } 445 } 446 447 /* 448 ** AddConn -- Add a connection to the linked list of connections. 449 ** 450 ** Parameters: 451 ** rconn - connection to be added. 452 ** 453 ** Returns: 454 ** Nothing. 455 ** 456 ** Side Effects: 457 ** - RmpConn will point to new connection. 458 ** 459 ** Warnings: 460 ** - This routine must be called with SIGHUP blocked. 461 */ 462 void 463 AddConn(RMPCONN *rconn) 464 { 465 if (RmpConns != NULL) 466 rconn->next = RmpConns; 467 RmpConns = rconn; 468 } 469 470 /* 471 ** FindConn -- Find a connection in the linked list of connections. 472 ** 473 ** We use the RMP (Ethernet) address as the basis for determining 474 ** if this is the same connection. According to the Remote Maint 475 ** Protocol, we can only have one connection with any machine. 476 ** 477 ** Parameters: 478 ** rconn - connection to be found. 479 ** 480 ** Returns: 481 ** Matching connection from linked list or NULL if not found. 482 ** 483 ** Side Effects: 484 ** None. 485 ** 486 ** Warnings: 487 ** - This routine must be called with SIGHUP blocked. 488 */ 489 RMPCONN * 490 FindConn(RMPCONN *rconn) 491 { 492 RMPCONN *rtmp; 493 494 for (rtmp = RmpConns; rtmp != NULL; rtmp = rtmp->next) 495 if (bcmp((char *)&rconn->rmp.hp_hdr.saddr[0], 496 (char *)&rtmp->rmp.hp_hdr.saddr[0], RMP_ADDRLEN) == 0) 497 break; 498 499 return(rtmp); 500 } 501 502 /* 503 ** RemoveConn -- Remove a connection from the linked list of connections. 504 ** 505 ** Parameters: 506 ** rconn - connection to be removed. 507 ** 508 ** Returns: 509 ** Nothing. 510 ** 511 ** Side Effects: 512 ** - If found, an RMPCONN will cease to exist and it will 513 ** be removed from the linked list. 514 ** 515 ** Warnings: 516 ** - This routine must be called with SIGHUP blocked. 517 */ 518 void 519 RemoveConn(RMPCONN *rconn) 520 { 521 RMPCONN *thisrconn, *lastrconn; 522 523 if (RmpConns == rconn) { /* easy case */ 524 RmpConns = RmpConns->next; 525 FreeConn(rconn); 526 } else { /* must traverse linked list */ 527 lastrconn = RmpConns; /* set back ptr */ 528 thisrconn = lastrconn->next; /* set current ptr */ 529 while (thisrconn != NULL) { 530 if (rconn == thisrconn) { /* found it */ 531 lastrconn->next = thisrconn->next; 532 FreeConn(thisrconn); 533 break; 534 } 535 lastrconn = thisrconn; 536 thisrconn = thisrconn->next; 537 } 538 } 539 } 540