- Timestamp:
- Feb 24, 2009, 7:53:43 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_portcheck.c
r212 r218 283 283 #endif 284 284 285 static char * sh_getrpcbynumber (int number, char * buf, size_t len) 286 { 287 FILE * fp; 288 289 if (NULL != (fp = fopen(_("/etc/rpc"), "r"))) 290 { 291 sh_string * s = sh_string_new(0); 292 while (0 < sh_string_read(s, fp, 1024)) 293 { 294 char * p = sh_string_str(s); 295 while (*p && (*p == ' ' || *p == '\t')) ++p; /* skip whitespace */ 296 if (*p == '\0' || *p == '#') 297 continue; /* skip comment */ 298 else 299 { 300 size_t lengths[3]; 301 unsigned int fields = 3; 302 char * q = sh_string_str(s); 303 char ** splits = split_array_ws(q, &fields, lengths); 304 305 if (fields >= 2) 306 { 307 int n = atoi(splits[1]); 308 if (n == number) 309 { 310 sl_strlcpy(buf, splits[0], len); 311 SH_FREE(splits); 312 sh_string_destroy(&s); 313 fclose(fp); 314 return buf; 315 } 316 } 317 SH_FREE(splits); 318 } 319 } 320 sh_string_destroy(&s); 321 fclose(fp); 322 } 323 return NULL; 324 } 325 326 static char * sh_getservbyport (int port, const char * proto_in, char * buf, size_t len) 327 { 328 FILE * fp; 329 char proto[8]; 330 331 sl_strlcpy(proto, proto_in, sizeof(proto)); 332 333 if (NULL != (fp = fopen(_("/etc/services"), "r"))) 334 { 335 sh_string * s = sh_string_new(0); 336 while (0 < sh_string_read(s, fp, 1024)) 337 { 338 char * p = sh_string_str(s); 339 while (*p && (*p == ' ' || *p == '\t')) ++p; /* skip whitespace */ 340 if (*p == '\0' || *p == '#') 341 continue; /* skip comment */ 342 else 343 { 344 size_t lengths[3]; 345 unsigned int fields = 3; 346 char * q = sh_string_str(s); 347 char ** splits = split_array_ws(q, &fields, lengths); 348 349 if (fields >= 2) 350 { 351 char * end; 352 long n = strtol(splits[1], &end, 10); 353 if (n == port && end && (*end == '/' || *end == ',')) 354 { 355 ++end; 356 if (0 == strcmp(end, proto)) 357 { 358 sl_strlcpy(buf, splits[0], len); 359 SH_FREE(splits); 360 sh_string_destroy(&s); 361 fclose(fp); 362 return buf; 363 } 364 } 365 } 366 SH_FREE(splits); 367 } 368 } 369 sh_string_destroy(&s); 370 fclose(fp); 371 } 372 return NULL; 373 } 374 285 375 static void sh_portchk_add_to_list (int proto, 286 int port, struct in_addr haddr, char * service, 376 int port, struct in_addr haddr, 377 char * service, 287 378 int flag, int status) 288 379 { … … 648 739 { 649 740 static char buf[256]; 650 struct servent * service = getservbyport(htons(port), SH_PROTO_STR(proto)); 651 652 if (service && service->s_name && service->s_name[0] != '\0') 653 { 654 snprintf (buf, sizeof(buf), _("maybe_%s"), service->s_name); 655 } 656 else 741 char * service = sh_getservbyport(port, SH_PROTO_STR(proto), buf, sizeof(buf)); 742 743 if (!service) 657 744 { 658 745 snprintf (buf, sizeof(buf), "%s",_("unknown")); … … 669 756 { 670 757 struct pmaplist * head; 671 struct rpcent*r;758 char *r; 672 759 static char buf[256]; 673 760 … … 681 768 (port == (int)head->pml_map.pm_port)) 682 769 { 683 r = getrpcbynumber((int)head->pml_map.pm_prog); 684 if (r && r->r_name && r->r_name[0] != '\0') 770 r = sh_getrpcbynumber((int)head->pml_map.pm_prog, 771 buf, sizeof(buf)); 772 if (r) 685 773 { 686 snprintf (buf, sizeof(buf), "%s", r->r_name);687 774 return buf; 688 775 } … … 1568 1655 struct in_addr haddr_local; 1569 1656 struct sh_portentry * portent; 1657 char buf[256]; 1658 char * p; 1659 1660 p = sh_getrpcbynumber(0, buf, sizeof(buf)); 1661 CuAssertTrue(tc, p == NULL); 1662 1663 p = sh_getrpcbynumber(100000, buf, sizeof(buf)); 1664 CuAssertPtrNotNull(tc, p); 1665 CuAssertTrue(tc, (0 == strcmp(p, "portmapper") || 0 == strcmp(p, "rpcbind"))); 1666 CuAssertTrue(tc, (0 == strcmp(buf, "portmapper") || 0 == strcmp(p, "rpcbind"))); 1667 1668 p = sh_getrpcbynumber(100007, buf, sizeof(buf)); 1669 CuAssertPtrNotNull(tc, p); 1670 CuAssertTrue(tc, 0 == strcmp(p, "ypbind")); 1671 CuAssertTrue(tc, 0 == strcmp(buf, "ypbind")); 1672 1673 p = sh_getservbyport(0, SH_PROTO_STR(IPPROTO_TCP), buf, sizeof(buf)); 1674 CuAssertTrue(tc, p == NULL); 1675 1676 p = sh_getservbyport(22, SH_PROTO_STR(IPPROTO_TCP), buf, sizeof(buf)); 1677 CuAssertPtrNotNull(tc, p); 1678 CuAssertTrue(tc, 0 == strcmp(p, "ssh")); 1679 CuAssertTrue(tc, 0 == strcmp(buf, "ssh")); 1680 1681 p = sh_getservbyport(13, SH_PROTO_STR(IPPROTO_UDP), buf, sizeof(buf)); 1682 CuAssertPtrNotNull(tc, p); 1683 CuAssertTrue(tc, 0 == strcmp(p, "daytime")); 1684 CuAssertTrue(tc, 0 == strcmp(buf, "daytime")); 1570 1685 1571 1686 CuAssertTrue(tc, 0 != inet_aton("127.0.0.1", &haddr_local));
Note:
See TracChangeset
for help on using the changeset viewer.