1 //----------------------------------------------------------------------------
3 // Copyright (C) Intel Corporation, 2004 - 2006.
7 // Contents: Sample code for an Intel® AMT Network client.
9 // Notes: This file contains the function implementations
10 // used throughout the code of the all sample applications.
12 //----------------------------------------------------------------------------
17 #include "CommonDefinitions.h"
20 #define strncasecmp _strnicmp
23 // Constant definitions
24 static const char *URL_SEPARATOR = "://";
25 static const int NUM_OF_BYTES = 4;
26 static const int IP_BYTE_MASK = 0xFF;
27 static const int IP_BYTE_SHIFT = 0x8;
30 * Gets command line parameter that describes type of action to perform
32 * commandLineLength - number of the command line parameters
33 * argv - array of strings that holds command line parameters
34 * numOfArgs - minimal number of command line parameters
35 * option - pointer to the string that will hold command line
36 * parameter that describes type of action to perform
37 * commandLine - pointer to the array of strings that will hold
38 * command line parameters without application name
39 * and parameter that describes type of action to perform
44 bool GetOption(int *commandLineLength, char *argv[], int numOfArgs,
45 char **option, char **commandLine[])
48 *commandLineLength < numOfArgs)
54 *commandLine = &argv[1];
55 // Skip the application name
56 (*commandLineLength) --;
58 if( argv[1][0] == '-')
60 if(strcmp(argv[1],CERT_NAME) && strcmp(argv[1],USER) &&
61 strcmp(argv[1],VERBOSE)
63 && strcmp(argv[1],LOCAL)
68 *commandLine = &argv[2];
69 (*commandLineLength) --;
78 * Parses the user input text
80 * commandLineLength - number of arguments
81 * commandLine - command line arguments received
82 * target - pointer to the string that will hold the target url
83 * certName - pointer to the string that will hold client certificate's name
84 * verbose - pointer to the verbose flag
85 * username - pointer to the string that will hold the username
86 * password - pointer to the string that will hold the password
91 bool ParseCommandLine(int commandLineLength,char* commandLine[],char **target,
92 char **certName, bool *local, bool *krb, bool *verbose,
93 char **username, char **password)
97 * Parses the user input text
99 * commandLineLength - number of arguments
100 * commandLine - command line arguments received
101 * target - pointer to the string that will hold the target url
102 * certName - pointer to the string that will hold client certificate's name*
103 * certPass - pointer to the string that will hold passphrase for decryption
104 * the file that contains private key associated with the given
106 * verbose - pointer to the verbose flag
107 * username - pointer to the string that will hold the username
108 * password - pointer to the string that will hold the password
113 bool ParseCommandLine(int commandLineLength,char* commandLine[],char **target,
114 char **certName,char **certPass, bool *verbose,
115 char **username, char **password)
124 if (verbose != NULL) {
128 for(int i = 0; i < commandLineLength; i++)
130 if( !strcmp(commandLine[i], CERT_NAME) )
132 if( i+1 < commandLineLength && ! *certName )
134 *certName = commandLine[++i];
142 else if( !strcmp(commandLine[i], CERT_PASS) )
144 if( i+1 < commandLineLength && ! *certPass )
146 *certPass = commandLine[++i];
154 else if( !strcmp(commandLine[i], VERBOSE) && verbose != NULL)
159 else if( !strcmp(commandLine[i], LOCAL) && local != NULL)
164 else if( !strcmp(commandLine[i], USER) )
167 if( i+1 < commandLineLength && ! *username )
169 *username = commandLine[++i];
176 else if( !strcmp(commandLine[i], PASS) )
179 if( i+1 < commandLineLength && ! *password )
181 *password = commandLine[++i];
190 //this is a target URL argument
191 if( !ValidateIP(commandLine[i]) )
195 *target = commandLine[i];
198 if( !*target || (!strncasecmp(*target, "http:", 5) && *certName != 0 ))
202 if( (*username && !*password) || (!*username && *password))
207 else if(*username && *password)
221 * Checks if IP address exists in the URI
223 * uri - pointer to the string that represents the URI
228 bool ValidateIP(const char *uri)
230 if( strncasecmp(uri, "http:", 5 ) && strncasecmp(uri, "https:", 6 ) )
236 // pointer to the start of the IP address
237 const char *start = strstr(uri,URL_SEPARATOR);
240 start += (sizeof(URL_SEPARATOR) - 1);
242 // pointer to the end of the IP address
243 const char *end = strchr(start,':');
245 if(end != NULL && start != end)
263 * Gets an input string from the user
264 * Assumptions: caller pre-allocates string, and user must not overflow (!)
266 * msg - message string
267 * s - pre-allocated input string that will represent the binary data
268 * hidden - echo input string (true / false)
270 void GetString(char *msg, char *s, bool hidden)
273 // Output informative message
281 char c; // for the next input character
282 int count = 0; // number of characters
284 // Get input string character by character
291 if (c == '\r' || // carriage return
292 c == '\n' || // new line
294 count == MAX_LINE_LEN ) // maximal line length
299 else if ( c==3 ) // <CTRL> + C
303 else if ( c == '\b' ) // backspace
317 // The password should be hidden
335 if(fgets(s,MAX_LINE_LEN,stdin))
337 char *tmp = strchr(s,'\n');
346 strcpy(s,getpass(""));
352 * Receives yes or no answer from the user.
354 * msg - pointer to the string that holds a question
356 * true - if answer is 'y' (yes)
359 bool DisplayWarning(const char *msg)
365 if(c != 'y' && c != 'n')
367 printf("Illegal choice. Aborting.\n");
380 * Gets a number from the user
382 * number - pointer to the variable that will be hold a number
387 bool GetNumber(int *number)
391 if(c != '\n' && c != '\r')
399 * Checks the exit status of the SOAP query
401 * res - exit statis of the gSOAP function call
402 * status - status codes that are returned by
403 * Intel(R) AMT network API
404 * message - string that holds the function name
406 * true - if the query succeed
407 * false - if the query failed
409 bool CheckReturnStatus(unsigned int res, unsigned long status,
414 printf("\nError: failed while calling %s\n", message);
418 printf("Status = %lu\n", status);
420 wchar_t err[MAX_LINE_LEN];
421 GetStatusString(status,err,MAX_LINE_LEN);
428 printf("SOAP failure: error code = %u\n", res);
436 * Changes the service name at the URI
438 * uri - null-terminated string which holds the whole URI as supplied
439 * at the command line
440 * newService - string which represents the new service name
441 * newUri - pre-allocated string which will hold the new service URI
443 bool ChangeService(const char *uri, const char *newService, char *newUri)
445 const char *tmp = strrchr(uri,'/');
451 int n = tmp - uri + 1;
453 strncpy(newUri,uri,n);
454 newUri[strlen(uri) - strlen(tmp) + 1] = '\0';
455 //check new Uri allocated
456 if(&(newUri[n]) == NULL)
460 //copy newService to end of string instead '\o'
461 strcpy(&(newUri[n]),newService);
467 * Replaces substring within given string
469 * oldString - pointer to the old null-terminated string
470 * oldSubstr - substring that will be replaced
471 * newSubstr - string which represents the new substring
472 * newString - pre-allocated buffer which will hold the new string
474 void ReplaceSubstring(const char *oldString,const char *oldSubstr,
475 const char *newSubstr, char *newString)
478 const char *tmp = strstr(oldString,oldSubstr);
482 printf("Error: cannot find '%s' within '%s'\n",oldSubstr,oldString);
486 int len1 = strlen(oldSubstr);
487 int len2 = strlen(newSubstr);
490 // Copy string to the buffer with appropriate change
491 for(i = 0, k = 0; oldString[i] != '\0'; i ++, k ++)
493 if(tmp == &oldString[i])
495 for(j = 0; j < len2; k ++, j ++)
497 newString[k] = newSubstr[j];
501 newString[k] = oldString[i];
503 newString[k] = oldString[i];
507 * Converts the IP address from the dot-separated format
508 * to the usigned long number
510 * address - number that will hold a converted IP address
511 * bytes - array for IP address's bytes
513 void SetIPAddress(unsigned long &address, unsigned long bytes[])
517 for(i = 0; i < NUM_OF_BYTES - 1; i ++)
526 * Extracts bytes from the IP address for the dot-separated representation
528 * address - number that holds a IP address
529 * bytes - array that will hold IP address bytes
531 void NumberToIP(unsigned long address, unsigned long bytes[])
534 for(i = NUM_OF_BYTES - 1; i > 0; i --)
536 bytes[i] = address & IP_BYTE_MASK;
537 address >>= IP_BYTE_SHIFT;
539 bytes[i] = address & IP_BYTE_MASK;
544 * Converts a number to a string
545 * Arguments & Return values:
546 * number - number which will convert
547 * string - string which will hold the converted number
549 void NumberToString(unsigned long number, char *string)
553 char tmp[MAX_LINE_LEN];
556 res = div(number,10);
557 tmp[i++] = (char)('0' + res.rem);
561 for(i --; i >= 0; i --)
570 * Converts the IP address to the dot-separated string form
571 * Arguments & Return values:
572 * address - number which holds the IP address
573 * string - string which will represent the IP address in
574 * the dot-separated format
576 void IpToString(unsigned long address, char *string)
578 unsigned long bytes[NUM_OF_BYTES];
579 NumberToIP(address,bytes);//one,two,three,four);
581 char tmp[MAX_LINE_LEN];
584 for(int i = 0; i < NUM_OF_BYTES; i ++)
586 NumberToString(bytes[i],tmp);
597 * Converts the string that represents dot-separated
598 * format of the IP address to the number
600 * string - string which represents IP address in
601 * the dot-separated format
602 * address - number which will hold the converted IP address
604 void StringToIP(const char *string, unsigned long &address)
606 // local copy of the string
607 char tmpStr[MAX_LINE_LEN];
608 strncpy(tmpStr,string,MAX_LINE_LEN - 1);
609 tmpStr[MAX_LINE_LEN - 1] = '\0';
611 char *tmpPtr1 = NULL;
612 char *tmpPtr2 = tmpStr;
613 unsigned long bytes[NUM_OF_BYTES];
616 for(i = 0; i < NUM_OF_BYTES - 1; i ++)
618 if((tmpPtr1 = strstr(tmpPtr2,".")) == NULL)
624 bytes[i] = atol(tmpPtr2);
625 tmpPtr2 = tmpPtr1 + 1;
627 bytes[i] = atol(tmpPtr2);
629 SetIPAddress(address,bytes);//one,two,three,four);
634 printf("Error: invalid format\n");
639 * Converts the bunary guid to a string according to the representation
640 * described in the CDE 1.1 RPC specification
641 * Arguments & Return values:
642 * guid - a pointer to the 16 byte guid
643 * string - string which will hold the guid string
645 void GuidToString(const unsigned char* guid, char* string)
647 if(guid != NULL && string != NULL)
649 int j = sprintf(string, "%02X%02X%02X%02X", guid[3], guid[2], guid[1], guid[0]);
650 j += sprintf(string + j, "-%02X%02X", guid[5], guid[4]);
651 j += sprintf(string + j, "-%02X%02X", guid[7], guid[6]);
652 j += sprintf(string + j, "-%02X%02X-", guid[8], guid[9]);
653 for(int i = 10; i <= 15; i ++)
655 j += sprintf(string + j, "%02X", guid[i]);
661 * Extracts IP address from the URI
663 * uri - pointer to the string that represents the URI
668 bool ExtractIPFromUri(const char *uri, char *baseUrl)
673 // pointer to the start of the IP address
674 const char *start = strstr(uri,URL_SEPARATOR);
677 start += (sizeof(URL_SEPARATOR) - 1);
679 // pointer to the end of the IP address
680 const char *end = strchr(start,':');
682 if(end != NULL && start != end)
690 *ip = '\0'; // NULL termination
706 * prints "success" to output
708 * printS - prints only if true
710 void PrintSuccess(bool printS)
719 * prints function call to output
721 * message - name of function
723 void FunctionCall(const char *message)
725 printf("\nCalling function %s... ", message);
729 * Performs testing of the URL
731 * targetUrl - string that represents URL of the target machine
732 * isEmulator - pointer to the variable that will be 1 if the targetUrl
733 * points to the Intel® AMT device or 0 if the targetUrl
734 * points to the Intel® AMT Emulator
736 void IsEmulator(const char *targetUrl, int *isEmulator)
740 if((strstr(targetUrl,".asmx")) == (targetUrl + strlen(targetUrl) - 5))
747 * Prints notes for usage by authentication options
749 void PrintAuthenticationNote()
752 printf("\t %s <username> %s <password>: for digest authentication.\n",USER,PASS);
754 printf("\n\t If %s <username> %s <password> are defined the Digest authentication" ,USER,PASS);
755 printf(" scheme is used,\n\t otherwise the Kerberos authentication scheme will be attempted.\n");
757 printf("\nClient authentication options (TLS Mutual Authentication mode only):\n");
759 printf("\t %s: If option defined <name> specify the full path of the ",
761 printf("file containing client certificate and private keys.\n");
762 printf("\t %s: If option defined <pass> specify the passphrase ",
764 printf("that protects the private key. ");
766 printf("\t %s: If option defined, <name> specifies the client certificate's",
768 printf(" Common Name (CN).\n");
770 printf("\t\t If option is not specified the sample application will search the certificate store \n");
771 printf("\t\t for a client certificate matching Intel(R) AMT requirements. \n");
772 printf("\t\t The first one found will be used for authentication.\n");