/usr/src/condor/chirp/chirp_client.c is in htcondor 8.6.8~dfsg.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 | /***************************************************************
*
* Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
* University of Wisconsin-Madison, WI.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
/*
Chirp C Client
*/
#include "chirp_protocol.h"
#include "chirp_client.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
/* Sockets */
#if defined(WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#else
#define SOCKET int
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET (-1)
#define closesocket close
#include <unistd.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
static SOCKET tcp_connect( const char *host, int port );
static void chirp_fatal_request( const char *name );
static void chirp_fatal_response(void);
static int get_result( FILE *s );
static int convert_result( int response );
static int simple_command(struct chirp_client *c,char const *fmt,...);
static void vsprintf_chirp(char *command,char const *fmt,va_list args);
static char const *read_url_param(char const *url,char *buffer,size_t length);
static int sockets_initialized = 0;
static int initialize_sockets(void);
//static int shutdown_sockets();
struct chirp_client {
FILE *rstream;
FILE *wstream;
};
/*
chirp_client_connect_url()
Sets path_part to the position of the start of the path information
in the URL and connects to the specified Chirp server.
URL format:
chirp:host.name:port/path (absolute path)
chirp:host.name:port./path (relative path)
chirp:/path (absolute path)
chirp:./path (relative path)
chirp:path (sloppy relative path)
Note that the initial part of a sloppy relative path can be confused
for a host:port specification if it happens to look like one. Example:
'chirp:strange:1/file'. For this reason, it is better to use one of
the non-sloppy formats.
In all of the above URL formats, any number of extra connection
parameters may be inserted before the path part (including the leading
'/' or '.'). These are of the form ";parameter=value" where parameter
and value are encoded using the standard Mime type
application/x-www-form-url encoded, just like the parameters in a typical
HTTP GET CGI request, but using ';' instead of '&' as the delimiter.
At this time, no connection parameters are defined, and any that
are supplied are simply ignored.
*/
static int
initialize_sockets()
{
#if defined(WIN32)
int err;
WORD wVersionRequested;
WSADATA wsaData;
#endif
if(sockets_initialized)
return 1;
#if defined(WIN32)
wVersionRequested = MAKEWORD( 2, 0 );
err = WSAStartup( wVersionRequested, &wsaData );
if(err)
return 0;
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0)
{
WSACleanup();
return 0;
}
#endif
sockets_initialized = 1;
return 1;
}
/* This function doesn't seem to be used anywhere. */
#if 0
static int
shutdown_sockets()
{
if(!sockets_initialized)
return 1;
#if defined(WIN32)
WSACleanup();
#endif
sockets_initialized = 0;
return 1;
}
#endif
DLLEXPORT struct chirp_client *
chirp_client_connect_url( const char *url, const char **path_part)
{
struct chirp_client *client;
char const *str;
char *host = NULL;
int port = 0;
if(strncmp(url,"chirp:",6)) {
//bare file name
*path_part = url;
return chirp_client_connect_default();
}
url += 6; // "chirp:"
if(*url != '/' && *url != '\\' && *url != ';' && *url != '.' \
&& (str = strchr(url,':')))
{
char *end;
port = strtol(str+1,&end,10);
if(port && end > str+1 &&
(*end == '\0' || *end == '/' || *end == '\\' ||
*end == '.' || *end == ';'))
{
//URL form chirp:host.name:port...
//Note that we try to avoid getting here on a "sloppy"
//relative path that happens to contain a ':' but
//which is not followed by a valid port/path.
host = (char *)malloc(str-url+1);
if ( ! host) {
errno = ENOMEM;
return NULL;
}
strncpy(host,url,str-url);
host[str-url] = '\0';
url = end;
}
}
while(*url == ';') { //parse connection parameters
char param[CHIRP_LINE_MAX];
char value[CHIRP_LINE_MAX];
url = read_url_param(++url,param,sizeof(param));
if(!url) {
errno = EINVAL;
if (host) free(host);
return NULL;
}
if(*url == '=') {
url = read_url_param(++url,value,sizeof(value));
if(!url) {
errno = EINVAL;
if (host) free(host);
return NULL;
}
}
else *value = '\0';
//No connection parameters are defined at this time!
//Handle them here when they are defined.
}
*path_part = url;
if(!host) { //URL must be in form 'chirp:path'
client = chirp_client_connect_default();
}
else {
client = chirp_client_connect(host,port);
}
free(host);
return client;
}
DLLEXPORT struct chirp_client *
chirp_client_connect_default()
{
FILE *file;
int fields;
int save_errno;
struct chirp_client *client;
char *default_filename;
char host[CHIRP_LINE_MAX];
char cookie[CHIRP_LINE_MAX];
int port;
int result;
if (!(default_filename = getenv("_CONDOR_CHIRP_CONFIG"))) {
default_filename = ".chirp.config";
}
file = fopen(default_filename,"r");
if(!file) return 0;
fields = fscanf(file,"%s %d %s",host,&port,cookie);
fclose(file);
if(fields!=3) {
errno = EINVAL;
return 0;
}
client = chirp_client_connect(host,port);
if(!client) return 0;
result = chirp_client_cookie(client,cookie);
if(result!=0) {
save_errno = errno;
chirp_client_disconnect(client);
errno = save_errno;
return 0;
}
return client;
}
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996) // the seek, read, open, close, fileno, etc are deprecated, use _seek, etc instead.
#endif
DLLEXPORT struct chirp_client *
chirp_client_connect( const char *host, int port )
{
struct chirp_client *c;
int save_errno;
SOCKET fd;
int osfh;
c = (struct chirp_client*)malloc(sizeof(*c));
if(!c) return 0;
fd = tcp_connect(host,port);
if(fd == INVALID_SOCKET) {
save_errno = errno;
free(c);
errno = save_errno;
return 0;
}
#if defined(WIN32)
// This allows WinNT to get a file handle from a socket
// Note: this will not work on win95/98
osfh = _open_osfhandle(fd, _O_RDWR | _O_BINARY);
if(osfh < 0) {
closesocket(fd);
return 0;
}
#else
osfh = fd;
#endif
c->rstream = fdopen(osfh,"r");
if(!c->rstream) {
save_errno = errno;
closesocket(fd);
free(c);
errno = save_errno;
return 0;
}
c->wstream = fdopen(osfh,"w");
if(!c->wstream) {
save_errno = errno;
fclose(c->rstream);
closesocket(fd);
free(c);
errno = save_errno;
return 0;
}
setbuf(c->rstream, NULL);
setbuf(c->wstream, NULL);
//setvbuf( c->rstream, NULL, _IONBF, 0 );
//setvbuf( c->rstream, NULL, _IONBF, 0 );
return c;
}
#ifdef _MSC_VER
#pragma warning(pop) // the seek, read, open, close, fileno, etc are deprecated, use _seek, etc instead.
#endif
DLLEXPORT void
chirp_client_disconnect( struct chirp_client *c )
{
fclose(c->rstream);
//TJ 2012 - fclose closes the stream and file, but rstream and wstream share a file so the second fclose ABORTs on Windows.
//fclose(c->wstream);
free(c);
}
DLLEXPORT int
chirp_client_cookie( struct chirp_client *c, const char *cookie )
{
return simple_command(c,"cookie %s\n",cookie);
}
DLLEXPORT int
chirp_client_login( struct chirp_client *c, const char *name, const char *password )
{
return simple_command(c,"login %s %s\n",name,password);
}
DLLEXPORT int
chirp_client_lookup( struct chirp_client *c, const char *logical_name, char **url )
{
int result;
int actual;
result = simple_command(c,"lookup %s\n",logical_name);
if(result>0) {
*url = (char*)malloc(result);
if(*url) {
actual = fread(*url,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("lookup");
} else {
chirp_fatal_request("lookup");
}
}
return result;
}
DLLEXPORT int
chirp_client_constrain( struct chirp_client *c, const char *expr)
{
return simple_command(c,"constrain %s\n",expr);
}
DLLEXPORT int
chirp_client_get_job_attr( struct chirp_client *c, const char *name, char **expr )
{
int result;
int actual;
result = simple_command(c,"get_job_attr %s\n",name);
if(result>0) {
*expr = (char*)malloc(result);
if(*expr) {
actual = fread(*expr,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("get_job_attr");
} else {
chirp_fatal_request("get_job_attr");
}
}
return result;
}
DLLEXPORT int
chirp_client_get_job_attr_delayed( struct chirp_client *c, const char *name, char **expr )
{
int result;
int actual;
result = simple_command(c,"get_job_attr_delayed %s\n",name);
if(result>0) {
*expr = (char*)malloc(result);
if(*expr) {
actual = fread(*expr,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("get_job_attr");
} else {
chirp_fatal_request("get_job_attr");
}
}
return result;
}
DLLEXPORT int
chirp_client_set_job_attr( struct chirp_client *c, const char *name, const char *expr )
{
return simple_command(c,"set_job_attr %s %s\n",name,expr);
}
DLLEXPORT int
chirp_client_set_job_attr_delayed( struct chirp_client *c, const char *name, const char *expr )
{
return simple_command(c,"set_job_attr_delayed %s %s\n",name,expr);
}
DLLEXPORT int
chirp_client_open( struct chirp_client *c, const char *path, const char *flags, int mode )
{
int result = -1;
result = simple_command(c,"open %s %s %d\n",path,flags,mode);
if(result >= 0) {
struct chirp_stat buf;
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_stat(line, &buf) != 0) {
chirp_fatal_request("get_stat");
}
}
return result;
}
DLLEXPORT int
chirp_client_close( struct chirp_client *c, int fd )
{
return simple_command(c,"close %d\n",fd);
}
DLLEXPORT int
chirp_client_read( struct chirp_client *c, int fd, void *buffer, int length )
{
int result;
int actual;
result = simple_command(c,"read %d %d\n",fd,length);
if( result>0 ) {
actual = fread(buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("read");
}
return result;
}
DLLEXPORT int
chirp_client_write( struct chirp_client *c, int fd, const void *buffer, int length )
{
int actual;
int result;
char command[CHIRP_LINE_MAX];
sprintf(command, "write %d %d\n", fd, length);
#ifdef DEBUG_CHIRP
printf("chirp sending: %s", command);
#endif
result = fputs(command, c->wstream);
if(result < 0) chirp_fatal_request("write");
result = fflush(c->wstream);
if(result < 0) chirp_fatal_request("write");
actual = fwrite(buffer,1,length,c->wstream);
if(actual!=length) chirp_fatal_request("write");
return convert_result(get_result(c->rstream));
}
DLLEXPORT int
chirp_client_unlink( struct chirp_client *c, const char *path )
{
return simple_command(c,"unlink %s\n",path);
}
DLLEXPORT int
chirp_client_rename( struct chirp_client *c, const char *oldpath, const char *newpath )
{
return simple_command(c,"rename %s %s\n",oldpath,newpath);
}
DLLEXPORT int
chirp_client_fsync( struct chirp_client *c, int fd )
{
return simple_command(c,"fsync %d\n",fd);
}
DLLEXPORT int
chirp_client_lseek( struct chirp_client *c, int fd, int offset, int whence )
{
return simple_command(c,"lseek %d %d %d\n",fd,offset,whence);
}
DLLEXPORT int
chirp_client_mkdir( struct chirp_client *c, char const *name, int mode )
{
return simple_command(c,"mkdir %s %d\n",name,mode);
}
DLLEXPORT int
chirp_client_rmdir( struct chirp_client *c, char const *name )
{
return simple_command(c,"rmdir %s\n",name);
}
DLLEXPORT int
chirp_client_ulog( struct chirp_client *c, char const *message )
{
return simple_command(c,"ulog %s\n",message);
}
DLLEXPORT int
chirp_client_phase( struct chirp_client *c, char const *phase )
{
return simple_command(c,"phase %s\n",phase);
}
DLLEXPORT int
chirp_client_pread( struct chirp_client *c, int fd, void *buffer, int length,
int offset )
{
int result;
int actual;
result = simple_command(c,"pread %d %d %d\n",fd, length, offset);
if(result > 0) {
actual = fread(buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("pread");
}
return result;
}
DLLEXPORT int
chirp_client_pwrite( struct chirp_client *c, int fd, const void *buffer,
int length, int offset )
{
int actual;
int result;
char command[CHIRP_LINE_MAX];
sprintf(command, "pwrite %d %d %d\n", fd, length, offset);
#ifdef DEBUG_CHIRP
printf("chirp sending: %s", command);
#endif
result = fputs(command, c->wstream);
if(result < 0) chirp_fatal_request("pwrite");
result = fflush(c->wstream);
if(result < 0) chirp_fatal_request("pwrite");
actual = fwrite(buffer,1,length,c->wstream);
if(actual != length) chirp_fatal_request("pwrite");
return convert_result(get_result(c->rstream));
}
DLLEXPORT int
chirp_client_sread( struct chirp_client *c, int fd, void *buffer, int length,
int offset, int stride_length, int stride_skip )
{
int result;
int actual;
result = simple_command(c,"sread %d %d %d %d %d\n",fd, length, offset,
stride_length, stride_skip);
if(result > 0) {
actual = fread(buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("sread");
}
return result;
}
DLLEXPORT int
chirp_client_swrite( struct chirp_client *c, int fd, const void *buffer,
int length, int offset, int stride_length, int stride_skip )
{
int actual;
int result;
char command[CHIRP_LINE_MAX];
sprintf(command, "swrite %d %d %d %d %d\n", fd, length, offset,
stride_length, stride_skip);
#ifdef DEBUG_CHIRP
printf("chirp sending: %s", command);
#endif
result = fputs(command, c->wstream);
if(result < 0) chirp_fatal_request("swrite");
result = fflush(c->wstream);
if(result < 0) chirp_fatal_request("swrite");
actual = fwrite(buffer,1,length,c->wstream);
if(actual != length) chirp_fatal_request("swrite");
return convert_result(get_result(c->rstream));
}
DLLEXPORT int
chirp_client_rmall( struct chirp_client *c, const char *path ) {
return simple_command(c,"rmall %s\n",path);
}
DLLEXPORT int
chirp_client_fstat( struct chirp_client *c, int fd, struct chirp_stat *buf ) {
int result;
result = simple_command(c,"fstat %d\n",fd);
if( result == 0 ) {
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_stat(line, buf) == -1) {
chirp_fatal_request("get_stat");
}
}
return result;
}
DLLEXPORT int
chirp_client_fstatfs( struct chirp_client *c, int fd,
struct chirp_statfs *buf)
{
int result;
result = simple_command(c,"fstatfs %d\n",fd);
if(result == 0) {
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_statfs(line, buf) == -1) {
chirp_fatal_request("get_statfs");
}
}
return result;
}
DLLEXPORT int
chirp_client_fchown( struct chirp_client *c, int fd, int uid, int gid ) {
return simple_command(c,"fchown %d %d %d\n", fd, uid, gid);
}
DLLEXPORT int
chirp_client_fchmod( struct chirp_client *c, int fd, int mode ) {
return simple_command(c,"fchmod %d %d\n", fd, mode);
}
DLLEXPORT int
chirp_client_ftruncate( struct chirp_client *c, int fd, int length ) {
return simple_command(c,"ftruncate %d %d\n", fd, length);
}
DLLEXPORT int
chirp_client_getfile_buffer( struct chirp_client *c, const char *path,
char **buffer )
{
int result, actual=0;
result = simple_command(c,"getfile %s\n",path);
if(result>=0) {
*buffer = (char*)malloc(result+1);
if(*buffer) {
actual = fread(*buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("getfile");
(*buffer)[result] = '\0';
}
else {
chirp_fatal_request("getfile");
}
}
else {
chirp_fatal_request("getfile");
}
return actual;
}
DLLEXPORT int
chirp_client_putfile_buffer( struct chirp_client *c, const char *path,
const char *buffer, int mode, int length )
{
int result, actual;
// Send request to put file
result = simple_command(c, "putfile %s %d %d\n", path, mode, length);
if(result < 0) chirp_fatal_request("putfile");
result = fflush(c->wstream);
if(result < 0) chirp_fatal_request("putfile");
// Now send actual file
actual = fwrite(buffer,1,length,c->wstream);
if(actual!=length) chirp_fatal_request("putfile");
return actual;
}
DLLEXPORT int
chirp_client_getlongdir( struct chirp_client *c, const char *path,
char **buffer)
{
int result, actual;
result = simple_command(c, "getlongdir %s\n", path);
if(result< 0) chirp_fatal_request("getlongdir");
*buffer = (char*)malloc(result+1);
if(*buffer) {
actual = fread(*buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("getlongdir");
(*buffer)[result] = '\0';
} else {
chirp_fatal_request("getlongdir");
}
return result;
}
DLLEXPORT int
chirp_client_getdir( struct chirp_client *c, const char *path, char **buffer) {
int result, actual;
result = simple_command(c, "getdir %s\n", path);
if(result < 0) chirp_fatal_request("getdir");
*buffer = (char*)malloc(result+1);
if(*buffer) {
actual = fread(*buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("getdir");
(*buffer)[result] = '\0';
} else {
chirp_fatal_request("getdir");
}
return result;
}
DLLEXPORT int
chirp_client_whoami( struct chirp_client *c, char *buffer, int length) {
int result, actual;
result = simple_command(c, "whoami %d\n", length);
if(result > 0) {
actual = fread(buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("whoami");
}
return result;
}
DLLEXPORT int
chirp_client_whoareyou( struct chirp_client *c, const char *rhost,
char *buffer, int length )
{
int result, actual;
result = simple_command(c, "whoareyou %s %d\n", rhost, length);
if(result > 0) {
actual = fread(buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("whoareyou");
}
return result;
}
DLLEXPORT int
chirp_client_link( struct chirp_client *c, const char *path,
const char *newpath )
{
return simple_command(c,"link %s %s\n",path,newpath);
}
DLLEXPORT int
chirp_client_symlink( struct chirp_client *c, const char *path,
const char *newpath )
{
return simple_command(c,"symlink %s %s\n",path,newpath);
}
DLLEXPORT int
chirp_client_readlink( struct chirp_client *c, const char *path, int length,
char **buffer )
{
int result, actual;
result = simple_command(c,"readlink %s %d\n",path, length);
if(result > 0) {
*buffer = (char*)malloc(result);
actual = fread(*buffer,1,result,c->rstream);
if(actual!=result) chirp_fatal_request("readlink");
}
return result;
}
DLLEXPORT int
chirp_client_stat( struct chirp_client *c, const char *path,
struct chirp_stat *buf )
{
int result;
result = simple_command(c,"stat %s\n",path);
if(result == 0) {
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_stat(line, buf) == -1) {
chirp_fatal_request("get_stat");
}
}
return result;
}
DLLEXPORT int
chirp_client_lstat( struct chirp_client *c, const char *path,
struct chirp_stat *buf )
{
int result;
result = simple_command(c,"lstat %s\n",path);
if(result == 0) {
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_stat(line, buf) == -1) {
chirp_fatal_request("get_stat");
}
}
return result;
}
DLLEXPORT int
chirp_client_statfs( struct chirp_client *c, const char *path,
struct chirp_statfs *buf )
{
int result;
result = simple_command(c,"statfs %s\n",path);
if(result == 0) {
char line[CHIRP_LINE_MAX];
if(fgets(line,CHIRP_LINE_MAX,c->rstream) == NULL) {
chirp_fatal_request("fgets");
}
if(get_statfs(line, buf) == -1) {
chirp_fatal_request("get_statfs");
}
}
return result;
}
DLLEXPORT int
chirp_client_access( struct chirp_client *c, const char *path, int mode ) {
return simple_command(c,"access %s %d\n",path,mode);
}
DLLEXPORT int
chirp_client_chmod( struct chirp_client *c, const char *path, int mode ) {
return simple_command(c,"chmod %s %d\n",path,mode);
}
DLLEXPORT int
chirp_client_chown( struct chirp_client *c, const char *path, int uid, int gid )
{
return simple_command(c,"chown %s %d %d\n",path,uid,gid);
}
DLLEXPORT int
chirp_client_lchown( struct chirp_client *c, const char *path, int uid,
int gid )
{
return simple_command(c,"lchown %s %d %d\n",path,uid,gid);
}
DLLEXPORT int
chirp_client_truncate( struct chirp_client *c, const char *path, int length ) {
return simple_command(c,"truncate %s %d\n",path,length);
}
DLLEXPORT int
chirp_client_utime( struct chirp_client *c, const char *path, int actime,
int modtime )
{
return simple_command(c,"utime %s %d %d\n",path,actime,modtime);
}
static int
convert_result( int result )
{
if(result>=0) {
return result;
} else {
switch(result) {
case CHIRP_ERROR_NOT_AUTHENTICATED:
case CHIRP_ERROR_NOT_AUTHORIZED:
errno = EACCES;
break;
case CHIRP_ERROR_DOESNT_EXIST:
errno = ENOENT;
break;
case CHIRP_ERROR_ALREADY_EXISTS:
errno = EEXIST;
break;
case CHIRP_ERROR_TOO_BIG:
errno = EFBIG;
break;
case CHIRP_ERROR_NO_SPACE:
errno = ENOSPC;
break;
case CHIRP_ERROR_NO_MEMORY:
errno = ENOMEM;
break;
case CHIRP_ERROR_INVALID_REQUEST:
errno = EINVAL;
break;
case CHIRP_ERROR_TOO_MANY_OPEN:
errno = EMFILE;
break;
case CHIRP_ERROR_BUSY:
errno = EBUSY;
break;
case CHIRP_ERROR_TRY_AGAIN:
errno = EAGAIN;
break;
case CHIRP_ERROR_BAD_FD:
errno = EBADF;
break;
case CHIRP_ERROR_IS_DIR:
errno = EISDIR;
break;
case CHIRP_ERROR_NOT_DIR:
errno = ENOTDIR;
break;
case CHIRP_ERROR_NOT_EMPTY:
errno = ENOTEMPTY;
break;
case CHIRP_ERROR_CROSS_DEVICE_LINK:
errno = EXDEV;
break;
case CHIRP_ERROR_UNKNOWN:
chirp_fatal_response();
break;
}
return -1;
}
}
static int
get_result( FILE *s )
{
char line[CHIRP_LINE_MAX];
char *c;
int result;
int fields;
c = fgets(line,CHIRP_LINE_MAX,s);
if(!c) chirp_fatal_response();
fields = sscanf(line,"%d",&result);
if(fields!=1) chirp_fatal_response();
#ifdef DEBUG_CHIRP
fprintf(stderr, "chirp received: %s\n",line);
#endif
return result;
}
static void
chirp_fatal_request( const char *name )
{
fprintf(stderr,"chirp: couldn't %s: %s\n",name,strerror(errno));
abort();
}
static
void chirp_fatal_response()
{
fprintf(stderr,"chirp: couldn't get response from server: %s\n",strerror(errno));
abort();
}
static SOCKET
tcp_connect( const char *host, int port )
{
struct addrinfo* result = 0;
int success;
SOCKET fd;
struct addrinfo hint;
union {
struct sockaddr_in6 v6;
struct sockaddr_in v4;
struct sockaddr_storage storage;
} sa;
if(!initialize_sockets())
return INVALID_SOCKET;
// Ubuntu 10 and 12 don't implement AI_ADDRCONFIG the same way
// everyone else does, so we have to turn it off explicitly.
memset( & hint, 0, sizeof(hint) );
hint.ai_family = AF_UNSPEC;
hint.ai_flags = AI_CANONNAME;
success = getaddrinfo(host, NULL, &hint, &result);
if (success != 0)
return INVALID_SOCKET;
if (result == NULL)
return INVALID_SOCKET;
memcpy(&sa.storage, result->ai_addr, result->ai_addrlen );
switch(result->ai_family) {
case AF_INET: sa.v4.sin_port = htons(port); break;
case AF_INET6: sa.v6.sin6_port = htons(port); break;
default:
freeaddrinfo( result );
return INVALID_SOCKET;
}
#if defined(WIN32)
// Create the socket with no overlapped I/0 so we can later associate the socket
// with a valid file descripter using _open_osfhandle.
fd = WSASocket(result->ai_family, SOCK_STREAM, 0, NULL, 0, 0);
#else
fd = socket( result->ai_family, SOCK_STREAM, 0 );
#endif
if(fd == INVALID_SOCKET) {
freeaddrinfo(result);
return INVALID_SOCKET;
}
success = connect( fd, (struct sockaddr*)&sa.storage, result->ai_addrlen );
freeaddrinfo(result);
if(success == SOCKET_ERROR) {
closesocket(fd);
return INVALID_SOCKET;
}
return fd;
}
/*
vsprintf_chirp -- simple sprintf capabilities with character escaping
The following format characters are interpreted:
%d -- decimal
%s -- word (whitespace is escaped)
%% -- output %
*/
void
vsprintf_chirp(char *command,char const *fmt,va_list args)
{
char *c;
char const *f;
c = command;
f = fmt;
while(*f) {
if(*f == '%') {
switch(*(++f)) {
case 'd':
f++;
sprintf(c,"%d",va_arg(args,int));
c += strlen(c);
break;
case 's': {
char const *w = va_arg(args,char const *);
f++;
while(*w) {
switch(*w) {
case ' ':
case '\t':
case '\n':
case '\r':
case '\\':
*(c++) = '\\';
/*fall through*/
default:
*(c++) = *(w++);
}
}
break;
}
case '%':
*(c++) = *(f++);
break;
default:
fprintf(stderr, "vsprintf_chirp error\n");
chirp_fatal_request(f);
}
} else {
*(c++) = *(f++);
}
}
*(c++) = '\0';
}
int
simple_command(struct chirp_client *c,char const *fmt,...)
{
int result;
char command[CHIRP_LINE_MAX];
va_list args;
va_start(args,fmt);
vsprintf_chirp(command,fmt,args);
va_end(args);
#ifdef DEBUG_CHIRP
fprintf(stderr, "chirp sending: %s",command);
#endif
result = fputs(command,c->wstream);
if(result < 0) chirp_fatal_request(fmt);
result = fflush(c->wstream);
if(result == EOF) chirp_fatal_request(fmt);
return convert_result(get_result(c->rstream));
}
char const *
read_url_param(char const *url,char *buffer,size_t length)
{
size_t bufpos = 0;
while(*url != '\0' && *url != '.' && *url != '/'
&& *url != '=' && *url != ';' && *url != '\\')
{
if(bufpos >= length) return NULL;
switch(*url) {
case '+':
buffer[bufpos++] = ' ';
break;
case '%': { //form-url-encoded escape sequence
//following two characters are hex digits
char d = tolower(*(++url));
if(d >= '0' && d <= '9') d -= '0';
else if(d >= 'a' && d <= 'f') d = d - 'a' + 0xA;
else return NULL; //invalid hex digit
buffer[bufpos] = d<<4;
d = tolower(*(++url));
if(d >= '0' && d <= '9') d -= '0';
else if(d >= 'a' && d <= 'f') d = d - 'a' + 0xA;
else return NULL; //invalid hex digit
buffer[bufpos++] |= d;
break;
}
default:
buffer[bufpos++] = *url;
break;
}
url++;
}
if(bufpos >= length) return NULL;
buffer[bufpos] = '\0';
return url;
}
int get_stat( const char *line, struct chirp_stat *info ) {
int fields;
memset(info,0,sizeof(*info));
fields = sscanf(line,"%ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld %ld\n",
&info->cst_dev,
&info->cst_ino,
&info->cst_mode,
&info->cst_nlink,
&info->cst_uid,
&info->cst_gid,
&info->cst_rdev,
&info->cst_size,
&info->cst_blksize,
&info->cst_blocks,
&info->cst_atime,
&info->cst_mtime,
&info->cst_ctime);
if(fields!=13) {
return -1;
}
return 0;
}
int get_statfs( const char *line, struct chirp_statfs *info ) {
int fields;
memset(info,0,sizeof(*info));
fields = sscanf(line,"%ld %ld %ld %ld %ld %ld %ld\n",
&info->f_type,
&info->f_bsize,
&info->f_blocks,
&info->f_bfree,
&info->f_bavail,
&info->f_files,
&info->f_ffree);
if(fields!=7) {
return -1;
}
return 0;
}
|