| 1 | /* |
| 2 | * Copyright (c) 2004-2010, 2013, 2014, 2016, 2017 Apple 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 | /* |
| 25 | * Modification History |
| 26 | * |
| 27 | * August 5, 2004 Allan Nathanson <ajn@apple.com> |
| 28 | * - initial revision |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "scutil.h" |
| 33 | #include "net.h" |
| 34 | #include "net_service.h" |
| 35 | #include "net_interface.h" |
| 36 | #include "net_protocol.h" |
| 37 | #include "prefs.h" |
| 38 | |
| 39 | |
| 40 | /* -------------------- */ |
| 41 | |
| 42 | |
| 43 | static SCNetworkServiceRef |
| 44 | _find_service(char *match) |
| 45 | { |
| 46 | Boolean allowIndex = TRUE; |
| 47 | CFIndex i; |
| 48 | CFIndex n; |
| 49 | CFStringRef select_name = NULL; |
| 50 | SCNetworkServiceRef selected = NULL; |
| 51 | |
| 52 | if (services == NULL) { |
| 53 | if (net_set == NULL) { |
| 54 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | services = SCNetworkSetCopyServices(net_set); |
| 59 | if (services == NULL) { |
| 60 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | allowIndex = FALSE; |
| 65 | } |
| 66 | |
| 67 | // try to select the service by its serviceID |
| 68 | |
| 69 | select_name = CFStringCreateWithCString(NULL, match, kCFStringEncodingUTF8); |
| 70 | |
| 71 | n = CFArrayGetCount(services); |
| 72 | for (i = 0; i < n; i++) { |
| 73 | SCNetworkServiceRef service; |
| 74 | CFStringRef serviceID; |
| 75 | |
| 76 | service = CFArrayGetValueAtIndex(services, i); |
| 77 | serviceID = SCNetworkServiceGetServiceID(service); |
| 78 | if (CFEqual(select_name, serviceID)) { |
| 79 | selected = service; |
| 80 | goto done; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // try to select the service by its name |
| 85 | |
| 86 | for (i = 0; i < n; i++) { |
| 87 | SCNetworkServiceRef service; |
| 88 | CFStringRef serviceName; |
| 89 | |
| 90 | service = CFArrayGetValueAtIndex(services, i); |
| 91 | serviceName = SCNetworkServiceGetName(service); |
| 92 | if ((serviceName != NULL) && CFEqual(select_name, serviceName)) { |
| 93 | if (selected == NULL) { |
| 94 | selected = service; |
| 95 | } else { |
| 96 | // if multiple services match |
| 97 | selected = NULL; |
| 98 | SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); |
| 99 | goto done; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (selected != NULL) { |
| 105 | goto done; |
| 106 | } |
| 107 | |
| 108 | // try to select the service by its name (case insensitive) |
| 109 | |
| 110 | for (i = 0; i < n; i++) { |
| 111 | SCNetworkServiceRef service; |
| 112 | CFStringRef serviceName; |
| 113 | |
| 114 | service = CFArrayGetValueAtIndex(services, i); |
| 115 | serviceName = SCNetworkServiceGetName(service); |
| 116 | if ((serviceName != NULL) && |
| 117 | CFStringCompare(select_name, |
| 118 | serviceName, |
| 119 | kCFCompareCaseInsensitive) == kCFCompareEqualTo) { |
| 120 | if (selected == NULL) { |
| 121 | selected = service; |
| 122 | } else { |
| 123 | // if multiple services match |
| 124 | selected = NULL; |
| 125 | SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); |
| 126 | goto done; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if (selected != NULL) { |
| 132 | goto done; |
| 133 | } |
| 134 | |
| 135 | // try to select the service by its [BSD] interface name |
| 136 | |
| 137 | for (i = 0; i < n; i++) { |
| 138 | SCNetworkInterfaceRef interface; |
| 139 | CFStringRef interfaceName = NULL; |
| 140 | SCNetworkServiceRef service; |
| 141 | |
| 142 | service = CFArrayGetValueAtIndex(services, i); |
| 143 | |
| 144 | interface = SCNetworkServiceGetInterface(service); |
| 145 | while ((interface != NULL) && (interfaceName == NULL)) { |
| 146 | interfaceName = SCNetworkInterfaceGetBSDName(interface); |
| 147 | if (interfaceName == NULL) { |
| 148 | interface = SCNetworkInterfaceGetInterface(interface); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (interfaceName == NULL) { |
| 153 | continue; |
| 154 | } |
| 155 | |
| 156 | if (CFStringCompare(select_name, |
| 157 | interfaceName, |
| 158 | kCFCompareCaseInsensitive) == kCFCompareEqualTo) { |
| 159 | if (selected == NULL) { |
| 160 | selected = service; |
| 161 | } else { |
| 162 | // if multiple services match |
| 163 | selected = NULL; |
| 164 | SCPrint(TRUE, stdout, CFSTR("multiple services match\n")); |
| 165 | goto done; |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (selected != NULL) { |
| 171 | goto done; |
| 172 | } |
| 173 | |
| 174 | // try to select the service by its index |
| 175 | |
| 176 | if (allowIndex) { |
| 177 | char *end; |
| 178 | char *str = match; |
| 179 | long val; |
| 180 | |
| 181 | errno = 0; |
| 182 | val = strtol(str, &end, 10); |
| 183 | if ((*str != '\0') && (*end == '\0') && (errno == 0)) { |
| 184 | if ((val > 0) && (val <= n)) { |
| 185 | selected = CFArrayGetValueAtIndex(services, val - 1); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (selected != NULL) { |
| 191 | goto done; |
| 192 | } |
| 193 | |
| 194 | SCPrint(TRUE, stdout, CFSTR("no match, which service?\n")); |
| 195 | |
| 196 | done : |
| 197 | |
| 198 | if (select_name != NULL) CFRelease(select_name); |
| 199 | return selected; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | /* -------------------- */ |
| 204 | |
| 205 | |
| 206 | __private_extern__ |
| 207 | void |
| 208 | create_service(int argc, char **argv) |
| 209 | { |
| 210 | SCNetworkInterfaceRef interface; |
| 211 | CFStringRef interfaceName; |
| 212 | Boolean ok; |
| 213 | SCNetworkServiceRef service = NULL; |
| 214 | CFStringRef serviceName; |
| 215 | CFStringRef setName; |
| 216 | CFArrayRef supported; |
| 217 | |
| 218 | if (prefs == NULL) { |
| 219 | SCPrint(TRUE, stdout, CFSTR("network configuration not open\n")); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | if (net_set == NULL) { |
| 224 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (argc < 1) { |
| 229 | if (net_interface == NULL) { |
| 230 | SCPrint(TRUE, stdout, CFSTR("no network interface selected\n")); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | interface = net_interface; |
| 235 | } else { |
| 236 | int nArgs; |
| 237 | |
| 238 | interface = _find_interface(argc, argv, &nArgs); |
| 239 | argv += nArgs; |
| 240 | argc -= nArgs; |
| 241 | } |
| 242 | |
| 243 | if (interface == NULL) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | supported = SCNetworkInterfaceGetSupportedProtocolTypes(interface); |
| 248 | if (supported == NULL) { |
| 249 | SCPrint(TRUE, stdout, CFSTR("no network protocols are supported over this interface\n")); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | service = SCNetworkServiceCreate(prefs, interface); |
| 254 | if (service == NULL) { |
| 255 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 256 | goto done; |
| 257 | } |
| 258 | |
| 259 | if ((argc > 0) && (strlen(argv[0]) > 0)) { |
| 260 | Boolean ok; |
| 261 | |
| 262 | serviceName = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); |
| 263 | // argv++; |
| 264 | // argc--; |
| 265 | |
| 266 | ok = SCNetworkServiceSetName(service, serviceName); |
| 267 | CFRelease(serviceName); |
| 268 | if (!ok) { |
| 269 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 270 | (void)SCNetworkServiceRemove(service); |
| 271 | goto done; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | ok = SCNetworkServiceEstablishDefaultConfiguration(service); |
| 276 | if (!ok) { |
| 277 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 278 | (void)SCNetworkServiceRemove(service); |
| 279 | goto done; |
| 280 | } |
| 281 | |
| 282 | ok = SCNetworkSetAddService(net_set, service); |
| 283 | if (!ok) { |
| 284 | SCPrint(TRUE, stdout, CFSTR("service not created: %s\n"), SCErrorString(SCError())); |
| 285 | (void)SCNetworkServiceRemove(service); |
| 286 | goto done; |
| 287 | } |
| 288 | |
| 289 | _prefs_changed = TRUE; |
| 290 | |
| 291 | if (net_service != NULL) CFRelease(net_service); |
| 292 | net_service = CFRetain(service); |
| 293 | |
| 294 | serviceName = SCNetworkServiceGetName(service); |
| 295 | if (serviceName != NULL) { |
| 296 | SCPrint(TRUE, stdout, |
| 297 | CFSTR("service \"%@\" (%@) created and selected\n"), |
| 298 | serviceName, |
| 299 | SCNetworkServiceGetServiceID(service)); |
| 300 | } else { |
| 301 | SCPrint(TRUE, stdout, |
| 302 | CFSTR("service ID \"%@\" created and selected\n"), |
| 303 | SCNetworkServiceGetServiceID(service)); |
| 304 | } |
| 305 | |
| 306 | setName = SCNetworkSetGetName(net_set); |
| 307 | if (setName != NULL) { |
| 308 | SCPrint(TRUE, stdout, CFSTR("& added to set \"%@\"\n"), setName); |
| 309 | } else { |
| 310 | SCPrint(TRUE, stdout, CFSTR("& added to set ID \"%@\"\n"), |
| 311 | SCNetworkSetGetSetID(net_set)); |
| 312 | } |
| 313 | |
| 314 | if (net_interface != NULL) CFRelease(net_interface); |
| 315 | net_interface = SCNetworkServiceGetInterface(net_service); |
| 316 | if (net_interface != NULL) { |
| 317 | CFRetain(net_interface); |
| 318 | } |
| 319 | |
| 320 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); |
| 321 | if (interfaceName == NULL) { |
| 322 | interfaceName = SCNetworkInterfaceGetBSDName(interface); |
| 323 | } |
| 324 | if (interfaceName == NULL) { |
| 325 | interfaceName = SCNetworkInterfaceGetInterfaceType(interface); |
| 326 | } |
| 327 | |
| 328 | SCPrint(TRUE, stdout, |
| 329 | CFSTR("& interface \"%@\" selected\n"), |
| 330 | interfaceName); |
| 331 | |
| 332 | if (protocols != NULL) { |
| 333 | CFRelease(protocols); |
| 334 | protocols = NULL; |
| 335 | } |
| 336 | |
| 337 | if (net_protocol != NULL) { |
| 338 | CFRelease(net_protocol); |
| 339 | net_protocol = NULL; |
| 340 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); |
| 341 | } |
| 342 | |
| 343 | if (services != NULL) { |
| 344 | CFRelease(services); |
| 345 | services = NULL; |
| 346 | } |
| 347 | |
| 348 | done : |
| 349 | |
| 350 | if (service != NULL) CFRelease(service); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | __private_extern__ |
| 356 | void |
| 357 | disable_service(int argc, char **argv) |
| 358 | { |
| 359 | SCNetworkServiceRef service; |
| 360 | |
| 361 | if (argc == 1) { |
| 362 | service = _find_service(argv[0]); |
| 363 | } else { |
| 364 | if (net_service != NULL) { |
| 365 | service = net_service; |
| 366 | } else { |
| 367 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); |
| 368 | return; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (service == NULL) { |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | if (!SCNetworkServiceSetEnabled(service, FALSE)) { |
| 377 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | _prefs_changed = TRUE; |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | __private_extern__ |
| 387 | void |
| 388 | enable_service(int argc, char **argv) |
| 389 | { |
| 390 | SCNetworkServiceRef service; |
| 391 | |
| 392 | if (argc == 1) { |
| 393 | service = _find_service(argv[0]); |
| 394 | } else { |
| 395 | if (net_service != NULL) { |
| 396 | service = net_service; |
| 397 | } else { |
| 398 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); |
| 399 | return; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (service == NULL) { |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (!SCNetworkServiceSetEnabled(service, TRUE)) { |
| 408 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | _prefs_changed = TRUE; |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | __private_extern__ |
| 418 | void |
| 419 | remove_service(int argc, char **argv) |
| 420 | { |
| 421 | SCNetworkServiceRef service = NULL; |
| 422 | CFStringRef serviceName; |
| 423 | |
| 424 | if (argc == 1) { |
| 425 | service = _find_service(argv[0]); |
| 426 | } else { |
| 427 | if (net_service != NULL) { |
| 428 | service = net_service; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | if (service == NULL) { |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | CFRetain(service); |
| 437 | |
| 438 | if (!SCNetworkServiceRemove(service)) { |
| 439 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 440 | goto done; |
| 441 | } |
| 442 | |
| 443 | _prefs_changed = TRUE; |
| 444 | |
| 445 | serviceName = SCNetworkServiceGetName(service); |
| 446 | if (serviceName != NULL) { |
| 447 | SCPrint(TRUE, stdout, CFSTR("service \"%@\" removed\n"), serviceName); |
| 448 | } else { |
| 449 | SCPrint(TRUE, stdout, |
| 450 | CFSTR("service ID \"%@\" removed\n"), |
| 451 | SCNetworkServiceGetServiceID(service)); |
| 452 | } |
| 453 | |
| 454 | if ((net_service != NULL) && CFEqual(service, net_service)) { |
| 455 | CFRelease(net_service); |
| 456 | net_service = NULL; |
| 457 | SCPrint(TRUE, stdout, CFSTR("& no service selected\n")); |
| 458 | |
| 459 | if (protocols != NULL) { |
| 460 | CFRelease(protocols); |
| 461 | protocols = NULL; |
| 462 | } |
| 463 | |
| 464 | if (net_protocol != NULL) { |
| 465 | CFRelease(net_protocol); |
| 466 | net_protocol = NULL; |
| 467 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); |
| 468 | } |
| 469 | |
| 470 | if (net_interface != NULL) { |
| 471 | CFRelease(net_interface); |
| 472 | net_interface = NULL; |
| 473 | SCPrint(TRUE, stdout, CFSTR("& no interface selected\n")); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (services != NULL) { |
| 478 | CFRelease(services); |
| 479 | services = NULL; |
| 480 | } |
| 481 | |
| 482 | done : |
| 483 | |
| 484 | CFRelease(service); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | __private_extern__ |
| 490 | void |
| 491 | select_service(int argc, char **argv) |
| 492 | { |
| 493 | #pragma unused(argc) |
| 494 | SCNetworkInterfaceRef interface; |
| 495 | SCNetworkServiceRef service; |
| 496 | CFStringRef serviceName; |
| 497 | |
| 498 | service = _find_service(argv[0]); |
| 499 | |
| 500 | if (service == NULL) { |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | if (net_service != NULL) CFRelease(net_service); |
| 505 | net_service = CFRetain(service); |
| 506 | |
| 507 | serviceName = SCNetworkServiceGetName(service); |
| 508 | if (serviceName != NULL) { |
| 509 | SCPrint(TRUE, stdout, CFSTR("service \"%@\" selected\n"), serviceName); |
| 510 | } else { |
| 511 | SCPrint(TRUE, stdout, |
| 512 | CFSTR("service ID \"%@\" selected\n"), |
| 513 | SCNetworkServiceGetServiceID(service)); |
| 514 | } |
| 515 | |
| 516 | interface = SCNetworkServiceGetInterface(service); |
| 517 | if (interface != NULL) { |
| 518 | CFStringRef interfaceName; |
| 519 | |
| 520 | if (net_interface != NULL) CFRelease(net_interface); |
| 521 | net_interface = CFRetain(interface); |
| 522 | |
| 523 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); |
| 524 | if (interfaceName == NULL) { |
| 525 | interfaceName = SCNetworkInterfaceGetBSDName(interface); |
| 526 | } |
| 527 | if (interfaceName == NULL) { |
| 528 | interfaceName = SCNetworkInterfaceGetInterfaceType(interface); |
| 529 | } |
| 530 | |
| 531 | SCPrint(TRUE, stdout, |
| 532 | CFSTR("& interface \"%@\" selected\n"), |
| 533 | interfaceName); |
| 534 | } else { |
| 535 | if (net_interface != NULL) { |
| 536 | CFRelease(net_interface); |
| 537 | net_interface = NULL; |
| 538 | SCPrint(TRUE, stdout, CFSTR("& no interface selected\n")); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (protocols != NULL) { |
| 543 | CFRelease(protocols); |
| 544 | protocols = NULL; |
| 545 | } |
| 546 | |
| 547 | if (net_protocol != NULL) { |
| 548 | CFRelease(net_protocol); |
| 549 | net_protocol = NULL; |
| 550 | SCPrint(TRUE, stdout, CFSTR("& no protocol selected\n")); |
| 551 | } |
| 552 | |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | |
| 557 | __private_extern__ |
| 558 | void |
| 559 | set_service(int argc, char **argv) |
| 560 | { |
| 561 | Boolean ok; |
| 562 | |
| 563 | if (net_service == NULL) { |
| 564 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | if (argc < 1) { |
| 569 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); |
| 570 | return; |
| 571 | } |
| 572 | |
| 573 | while (argc > 0) { |
| 574 | char *command; |
| 575 | |
| 576 | command = argv[0]; |
| 577 | argv++; |
| 578 | argc--; |
| 579 | |
| 580 | if (strcmp(command, "name") == 0) { |
| 581 | CFStringRef serviceName; |
| 582 | |
| 583 | if (argc < 1) { |
| 584 | SCPrint(TRUE, stdout, CFSTR("name not specified\n")); |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | serviceName = (strlen(argv[0]) > 0) |
| 589 | ? CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8) |
| 590 | : NULL; |
| 591 | argv++; |
| 592 | argc--; |
| 593 | |
| 594 | ok = SCNetworkServiceSetName(net_service, serviceName); |
| 595 | if (serviceName != NULL) CFRelease(serviceName); |
| 596 | if (!ok) { |
| 597 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 598 | return; |
| 599 | } |
| 600 | |
| 601 | _prefs_changed = TRUE; |
| 602 | } else if (strcmp(command, "order") == 0) { |
| 603 | |
| 604 | char *end; |
| 605 | long newIndex; |
| 606 | CFIndex nServices; |
| 607 | char *str; |
| 608 | CFArrayRef services; |
| 609 | |
| 610 | services = SCNetworkSetCopyServices(net_set); |
| 611 | nServices = CFArrayGetCount(services); |
| 612 | CFRelease(services); |
| 613 | |
| 614 | if (argc < 1) { |
| 615 | SCPrint(TRUE, stdout, CFSTR("order not specified\n")); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | if (net_set == NULL) { |
| 620 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); |
| 621 | return; |
| 622 | } |
| 623 | |
| 624 | str = argv[0]; |
| 625 | argv++; |
| 626 | argc--; |
| 627 | |
| 628 | errno = 0; |
| 629 | newIndex = strtol(str, &end, 10); |
| 630 | if ((*str != '\0') && (*end == '\0') && (errno == 0)) { |
| 631 | if ((newIndex > 0) && (newIndex <= nServices)) { |
| 632 | CFIndex curIndex; |
| 633 | CFMutableArrayRef newOrder; |
| 634 | CFArrayRef order; |
| 635 | CFStringRef serviceID = SCNetworkServiceGetServiceID(net_service); |
| 636 | |
| 637 | order = SCNetworkSetGetServiceOrder(net_set); |
| 638 | if (order == NULL) { |
| 639 | newOrder = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks); |
| 640 | } else { |
| 641 | newOrder = CFArrayCreateMutableCopy(NULL, 0, order); |
| 642 | } |
| 643 | |
| 644 | curIndex = CFArrayGetFirstIndexOfValue(newOrder, |
| 645 | CFRangeMake(0, CFArrayGetCount(newOrder)), |
| 646 | serviceID); |
| 647 | if (curIndex != kCFNotFound) { |
| 648 | CFArrayRemoveValueAtIndex(newOrder, curIndex); |
| 649 | } |
| 650 | |
| 651 | if (newIndex <= CFArrayGetCount(newOrder)) { |
| 652 | CFArrayInsertValueAtIndex(newOrder, newIndex - 1, serviceID); |
| 653 | } else { |
| 654 | CFArrayAppendValue(newOrder, serviceID); |
| 655 | } |
| 656 | |
| 657 | ok = SCNetworkSetSetServiceOrder(net_set, newOrder); |
| 658 | CFRelease(newOrder); |
| 659 | if (!ok) { |
| 660 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | _prefs_changed = TRUE; |
| 665 | } else { |
| 666 | SCPrint(TRUE, stdout, CFSTR("set order to what?\n")); |
| 667 | return; |
| 668 | } |
| 669 | } else { |
| 670 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); |
| 671 | return; |
| 672 | } |
| 673 | } else if (strcmp(command, "rank") == 0) { |
| 674 | SCNetworkServicePrimaryRank rank = kSCNetworkServicePrimaryRankDefault; |
| 675 | SCNetworkServiceRef service = (argc < 2) ? net_service : NULL; |
| 676 | Boolean useActive = FALSE; |
| 677 | |
| 678 | if (argc < 1) { |
| 679 | SCPrint(TRUE, stdout, CFSTR("rank not specified\n")); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | if (strlen(argv[0]) > 0) { |
| 684 | if (strcasecmp(argv[0], "Never") == 0) { |
| 685 | rank = kSCNetworkServicePrimaryRankNever; |
| 686 | } else if ((service != net_service) && (strcasecmp(argv[0], "First") == 0)) { |
| 687 | rank = kSCNetworkServicePrimaryRankFirst; |
| 688 | } else if ((service != net_service) && (strcasecmp(argv[0], "Last") == 0)) { |
| 689 | rank = kSCNetworkServicePrimaryRankLast; |
| 690 | } else if ((service != net_service) && (strcasecmp(argv[0], "Scoped") == 0)) { |
| 691 | rank = kSCNetworkServicePrimaryRankScoped; |
| 692 | } else { |
| 693 | SCPrint(TRUE, stdout, CFSTR("rank not valid\n")); |
| 694 | return; |
| 695 | } |
| 696 | } |
| 697 | argv++; |
| 698 | argc--; |
| 699 | |
| 700 | if (service == NULL) { |
| 701 | CFStringRef serviceID; |
| 702 | SCDynamicStoreRef store; |
| 703 | |
| 704 | store = SCDynamicStoreCreate(NULL, |
| 705 | CFSTR("scutil (set primary rank)"), |
| 706 | NULL, |
| 707 | NULL); |
| 708 | serviceID = SCNetworkServiceGetServiceID(net_service); |
| 709 | service = _SCNetworkServiceCopyActive(store, serviceID); |
| 710 | CFRelease(store); |
| 711 | useActive = TRUE; |
| 712 | |
| 713 | argv++; |
| 714 | argc--; |
| 715 | } |
| 716 | |
| 717 | ok = SCNetworkServiceSetPrimaryRank(service, rank); |
| 718 | if (useActive) { |
| 719 | CFRelease(service); |
| 720 | } else if (ok) { |
| 721 | _prefs_changed = TRUE; |
| 722 | } |
| 723 | if (!ok) { |
| 724 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 725 | return; |
| 726 | } |
| 727 | } else if (strcmp(command, "id") == 0) { |
| 728 | CFStringRef serviceID; |
| 729 | |
| 730 | if ((argc < 1) || (strlen(argv[0]) == 0)) { |
| 731 | SCPrint(TRUE, stdout, CFSTR("set id not specified\n")); |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | serviceID = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingUTF8); |
| 736 | argv++; |
| 737 | argc--; |
| 738 | |
| 739 | ok = _SCNetworkServiceSetServiceID(net_service, serviceID); |
| 740 | CFRelease(serviceID); |
| 741 | if (!ok) { |
| 742 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | _prefs_changed = TRUE; |
| 747 | } else { |
| 748 | SCPrint(TRUE, stdout, CFSTR("set what?\n")); |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | |
| 756 | static void |
| 757 | __show_service_interface(SCNetworkServiceRef service, const char *prefix) |
| 758 | { |
| 759 | CFStringRef description; |
| 760 | SCNetworkInterfaceRef interface; |
| 761 | |
| 762 | interface = SCNetworkServiceGetInterface(service); |
| 763 | if (interface == NULL) { |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | description = _interface_description(interface); |
| 768 | SCPrint(TRUE, stdout, CFSTR("%s%@\n"), prefix, description); |
| 769 | CFRelease(description); |
| 770 | |
| 771 | return; |
| 772 | } |
| 773 | |
| 774 | static void |
| 775 | __show_service_protocols(SCNetworkServiceRef service, const char *prefix, Boolean skipEmpty) |
| 776 | { |
| 777 | CFIndex i; |
| 778 | CFIndex n; |
| 779 | CFArrayRef protocols; |
| 780 | |
| 781 | protocols = SCNetworkServiceCopyProtocols(service); |
| 782 | if (protocols == NULL) { |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | n = CFArrayGetCount(protocols); |
| 787 | if (n > 1) { |
| 788 | CFMutableArrayRef sorted; |
| 789 | |
| 790 | sorted = CFArrayCreateMutableCopy(NULL, 0, protocols); |
| 791 | CFArraySortValues(sorted, |
| 792 | CFRangeMake(0, n), |
| 793 | _SCNetworkProtocolCompare, |
| 794 | NULL); |
| 795 | CFRelease(protocols); |
| 796 | protocols = sorted; |
| 797 | } |
| 798 | |
| 799 | for (i = 0; i < n; i++) { |
| 800 | CFStringRef description; |
| 801 | SCNetworkProtocolRef protocol; |
| 802 | |
| 803 | protocol = CFArrayGetValueAtIndex(protocols, i); |
| 804 | description = _protocol_description(protocol, skipEmpty); |
| 805 | if (description != NULL) { |
| 806 | CFStringRef protocolType; |
| 807 | |
| 808 | protocolType = SCNetworkProtocolGetProtocolType(protocol); |
| 809 | SCPrint(TRUE, stdout, |
| 810 | CFSTR("%s%@%*s : %@\n"), |
| 811 | prefix, |
| 812 | protocolType, |
| 813 | (int)(sizeof("Interface") - CFStringGetLength(protocolType) - 1), |
| 814 | "", |
| 815 | description); |
| 816 | CFRelease(description); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | CFRelease(protocols); |
| 821 | return; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | __private_extern__ |
| 826 | void |
| 827 | show_service(int argc, char **argv) |
| 828 | { |
| 829 | SCNetworkInterfaceRef interface; |
| 830 | CFArrayRef protocols; |
| 831 | SCNetworkServiceRef service; |
| 832 | CFStringRef serviceName; |
| 833 | SCNetworkServicePrimaryRank serviceRank; |
| 834 | |
| 835 | if (argc == 1) { |
| 836 | service = _find_service(argv[0]); |
| 837 | } else { |
| 838 | if (net_service != NULL) { |
| 839 | service = net_service; |
| 840 | } else { |
| 841 | SCPrint(TRUE, stdout, CFSTR("service not selected\n")); |
| 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (service == NULL) { |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | SCPrint(TRUE, stdout, CFSTR("service id = %@\n"), SCNetworkServiceGetServiceID(service)); |
| 851 | |
| 852 | serviceName = SCNetworkServiceGetName(service); |
| 853 | SCPrint(TRUE, stdout, CFSTR("name = %@\n"), |
| 854 | (serviceName != NULL) ? serviceName : CFSTR("")); |
| 855 | |
| 856 | serviceRank = SCNetworkServiceGetPrimaryRank(service); |
| 857 | switch (serviceRank) { |
| 858 | case kSCNetworkServicePrimaryRankDefault : |
| 859 | // nothing to report |
| 860 | break; |
| 861 | case kSCNetworkServicePrimaryRankFirst : |
| 862 | SCPrint(TRUE, stdout, CFSTR("primary rank = %@\n"), kSCValNetServicePrimaryRankFirst); |
| 863 | break; |
| 864 | case kSCNetworkServicePrimaryRankLast : |
| 865 | SCPrint(TRUE, stdout, CFSTR("primary rank = %@\n"), kSCValNetServicePrimaryRankLast); |
| 866 | break; |
| 867 | case kSCNetworkServicePrimaryRankNever : |
| 868 | SCPrint(TRUE, stdout, CFSTR("primary rank = %@\n"), kSCValNetServicePrimaryRankNever); |
| 869 | break; |
| 870 | case kSCNetworkServicePrimaryRankScoped : |
| 871 | SCPrint(TRUE, stdout, CFSTR("primary rank = %@\n"), kSCValNetServicePrimaryRankScoped); |
| 872 | break; |
| 873 | default : |
| 874 | SCPrint(TRUE, stdout, CFSTR("primary rank = %d\n"), serviceRank); |
| 875 | break; |
| 876 | } |
| 877 | |
| 878 | interface = SCNetworkServiceGetInterface(service); |
| 879 | if (interface != NULL) { |
| 880 | CFStringRef interfaceName; |
| 881 | |
| 882 | interfaceName = SCNetworkInterfaceGetLocalizedDisplayName(interface); |
| 883 | if (interfaceName != NULL) { |
| 884 | CFRetain(interfaceName); |
| 885 | } else { |
| 886 | interfaceName = _interface_description(interface); |
| 887 | } |
| 888 | if (interfaceName != NULL) { |
| 889 | SCPrint(TRUE, stdout, CFSTR("interface = %@\n"), interfaceName); |
| 890 | CFRelease(interfaceName); |
| 891 | } |
| 892 | } else { |
| 893 | SCPrint(TRUE, stdout, CFSTR("\n No interface!\n\n")); |
| 894 | } |
| 895 | |
| 896 | protocols = SCNetworkServiceCopyProtocols(service); |
| 897 | if (protocols != NULL) { |
| 898 | CFIndex n; |
| 899 | |
| 900 | n = CFArrayGetCount(protocols); |
| 901 | if (n > 1) { |
| 902 | CFMutableArrayRef sorted; |
| 903 | |
| 904 | sorted = CFArrayCreateMutableCopy(NULL, 0, protocols); |
| 905 | CFArraySortValues(sorted, |
| 906 | CFRangeMake(0, n), |
| 907 | _SCNetworkProtocolCompare, |
| 908 | NULL); |
| 909 | CFRelease(protocols); |
| 910 | protocols = sorted; |
| 911 | } |
| 912 | |
| 913 | if (n > 0) { |
| 914 | CFIndex i; |
| 915 | |
| 916 | SCPrint(TRUE, stdout, CFSTR("configured protocols = ")); |
| 917 | for (i = 0; i < n; i++) { |
| 918 | SCNetworkProtocolRef protocol; |
| 919 | |
| 920 | protocol = CFArrayGetValueAtIndex(protocols, i); |
| 921 | SCPrint(TRUE, stdout, CFSTR("%s%@"), |
| 922 | (i == 0) ? "" : ", ", |
| 923 | SCNetworkProtocolGetProtocolType(protocol)); |
| 924 | } |
| 925 | SCPrint(TRUE, stdout, CFSTR("\n")); |
| 926 | |
| 927 | __show_service_protocols(service, " ", FALSE); |
| 928 | } else { |
| 929 | SCPrint(TRUE, stdout, CFSTR("no configured protocols\n")); |
| 930 | } |
| 931 | |
| 932 | CFRelease(protocols); |
| 933 | } |
| 934 | |
| 935 | if (_sc_debug) { |
| 936 | SCPrint(TRUE, stdout, CFSTR("\n%@\n"), service); |
| 937 | } |
| 938 | |
| 939 | return; |
| 940 | } |
| 941 | |
| 942 | |
| 943 | __private_extern__ |
| 944 | void |
| 945 | show_services(int argc, char **argv) |
| 946 | { |
| 947 | #pragma unused(argv) |
| 948 | CFIndex i; |
| 949 | CFIndex n; |
| 950 | |
| 951 | if (prefs == NULL) { |
| 952 | SCPrint(TRUE, stdout, CFSTR("network configuration not open\n")); |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | if (argc == 1) { |
| 957 | if (services != NULL) CFRelease(services); |
| 958 | services = SCNetworkServiceCopyAll(prefs); |
| 959 | } else { |
| 960 | if (net_set == NULL) { |
| 961 | SCPrint(TRUE, stdout, CFSTR("set not selected\n")); |
| 962 | return; |
| 963 | } |
| 964 | |
| 965 | if (services != NULL) CFRelease(services); |
| 966 | services = SCNetworkSetCopyServices(net_set); |
| 967 | n = (services != NULL) ? CFArrayGetCount(services) : 0; |
| 968 | if (n > 1) { |
| 969 | CFArrayRef order; |
| 970 | CFMutableArrayRef sorted; |
| 971 | |
| 972 | order = SCNetworkSetGetServiceOrder(net_set); |
| 973 | sorted = CFArrayCreateMutableCopy(NULL, 0, services); |
| 974 | CFArraySortValues(sorted, |
| 975 | CFRangeMake(0, CFArrayGetCount(sorted)), |
| 976 | _SCNetworkServiceCompare, |
| 977 | (void *)order); |
| 978 | CFRelease(services); |
| 979 | services = sorted; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | if (services == NULL) { |
| 984 | SCPrint(TRUE, stdout, CFSTR("%s\n"), SCErrorString(SCError())); |
| 985 | return; |
| 986 | } |
| 987 | |
| 988 | n = CFArrayGetCount(services); |
| 989 | for (i = 0; i < n; i++) { |
| 990 | SCNetworkServiceRef service; |
| 991 | CFStringRef serviceName; |
| 992 | CFStringRef serviceID; |
| 993 | |
| 994 | service = CFArrayGetValueAtIndex(services, i); |
| 995 | serviceID = SCNetworkServiceGetServiceID(service); |
| 996 | serviceName = SCNetworkServiceGetName(service); |
| 997 | if (serviceName == NULL) serviceName = CFSTR(""); |
| 998 | |
| 999 | SCPrint(TRUE, stdout, CFSTR("%c%2ld: %@%-*s (%@)%s\n"), |
| 1000 | ((net_service != NULL) && CFEqual(service, net_service)) ? '>' : ' ', |
| 1001 | i + 1, |
| 1002 | serviceName, |
| 1003 | (int)(30 - CFStringGetLength(serviceName)), |
| 1004 | " ", |
| 1005 | serviceID, |
| 1006 | SCNetworkServiceGetEnabled(service) ? "" : " *DISABLED*"); |
| 1007 | |
| 1008 | __show_service_interface(service, " Interface : "); |
| 1009 | __show_service_protocols(service, " ", TRUE); |
| 1010 | } |
| 1011 | |
| 1012 | return; |
| 1013 | } |