Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
7.4 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. newdsn.c
  5. Abstract:
  6. This module creates a data source given information from DSNFORM.EXE
  7. Author:
  8. Kyle Geiger
  9. Revision History:
  10. --*/
  11. #include <windows.h>
  12. #include <stdio.h>
  13. #include <odbcinst.h>
  14. #include "dynodbc.h"
  15. #include "cgi.h"
  16. #include "html.h"
  17. #include "resource.h"
  18. # define MAX_DATA 1024
  19. #define SUCCESS(rc) (!((rc)>>1))
  20. int __cdecl
  21. main( int argc, char * argv[])
  22. {
  23. BOOL rc; // Return code for ODBC functions
  24. char rgchQuery[MAX_DATA];
  25. long dwLen;
  26. char * p;
  27. char szDriver[MAX_DATA];
  28. char * pszDriver;
  29. char szAttr[MAX_DATA];
  30. char * pszAttr;
  31. char szAttr2[MAX_DATA];
  32. char * pszAttr2;
  33. BOOL fCreateDB=FALSE;
  34. char szAccessDriver[MAX_DATA];
  35. char szDsn[MAX_DATA];
  36. char szSQLServer[MAX_DATA];
  37. char szServer[MAX_DATA];
  38. char szNewDB[MAX_DATA];
  39. char szDBQ[MAX_DATA];
  40. char szDBQEqual[MAX_DATA];
  41. char szGeneral[MAX_DATA];
  42. char szTmp[MAX_DATA];
  43. char szSuccessful[MAX_DATA];
  44. char szFail[MAX_DATA];
  45. HINSTANCE hInst = GetModuleHandle(NULL);
  46. if ( !DynLoadODBC())
  47. return (1);
  48. // get dsn name, custom attributes, and attribute string from form
  49. dwLen = GetEnvironmentVariableA( PSZ_QUERY_STRING_A, rgchQuery, MAX_DATA);
  50. if (!dwLen)
  51. {
  52. // debugging cases
  53. /*
  54. strcpy(rgchQuery, "driver=SQL%20Server&dsn=foo2&attr=server%3D%7Bkyleg0%7D%3Bdbq%3Dpubs");
  55. strcpy(rgchQuery, "driver=Microsoft Access Driver (*.mdb)&dsn=foo4&dbq=c%3A%5Cfoo4.mdb&newdb=CREATE_DB&attr=");
  56. strcpy(rgchQuery, "driver=Microsoft Access Driver (*.mdb)&dsn=foo5&dbq=c%3A%5Cfoo4.mdb&newdb=dbq&attr=");
  57. */
  58. LoadString(hInst, IDS_ACCESS_DRIVER, szAccessDriver, sizeof(szAccessDriver));
  59. strcpy(rgchQuery, szAccessDriver);
  60. dwLen=strlen(rgchQuery);
  61. }
  62. // get rid of percent junk
  63. TranslateEscapes2(rgchQuery, dwLen);
  64. LoadString(hInst, IDS_DRIVER, szDriver, sizeof(szDriver));
  65. // find driver name from URL
  66. p=strstr(rgchQuery, szDriver)+7;
  67. pszDriver=szDriver;
  68. for(;p && *p && *p!='&'; p++)
  69. *pszDriver++= *p;
  70. *pszDriver++='\0';
  71. LoadString(hInst, IDS_DSN, szDsn, sizeof(szDsn));
  72. // find dsn name from URL, put in attribute string
  73. p=strstr(rgchQuery, szDsn);
  74. pszAttr=szAttr;
  75. for(; p && *p && *p!='&'; p++)
  76. *pszAttr++= *p;
  77. *pszAttr++='\0';
  78. LoadString(hInst, IDS_SQL_SERVER, szSQLServer, sizeof(szSQLServer));
  79. LoadString(hInst, IDS_SERVER, szServer, sizeof(szServer));
  80. LoadString(hInst, IDS_ACCESS_DRIVER_1, szAccessDriver, sizeof(szAccessDriver));
  81. LoadString(hInst, IDS_NEWDB, szNewDB, sizeof(szNewDB));
  82. LoadString(hInst, IDS_DBQ, szDBQ, sizeof(szDBQ));
  83. LoadString(hInst, IDS_DBQ_EQUAL, szDBQEqual, sizeof(szDBQEqual));
  84. LoadString(hInst, IDS_GENERAL, szGeneral, sizeof(szGeneral));
  85. // do special case processing for certain drivers (sql server, access, ddp, other)
  86. if (!strcmp(szDriver, szSQLServer)) {
  87. // find server name from URL, put in attribute string
  88. p=strstr(rgchQuery, szServer);
  89. for(; p && *p && *p!='&'; p++)
  90. *pszAttr++= *p;
  91. *pszAttr++='\0';
  92. }
  93. else if (!strcmp(szDriver, szAccessDriver)) {
  94. // find database name from URL, put in attribute string
  95. // the radio button group 'newdb' return either CREATE_DB for a new database
  96. // or DBQ for an existing MDB file
  97. // the edit control for the database name ('dbq') is appended after the CREATE_DB
  98. // or DBQ attribute
  99. p=strstr(rgchQuery, szNewDB)+6;
  100. fCreateDB=(*p=='C');
  101. // if creating a database, also need to add the dsn pointing to it
  102. // this requires two different attribute strings, one like:
  103. // dsn=foo;CREATE_DB=<filename>, where the dsn is ignored
  104. // which is derived from
  105. // dsn=foo&newdb=CREATE_DB&dbq=<filename>
  106. // and
  107. // dsn=foo;dbq=<filename>
  108. // which is derived from
  109. // dsn=foo&newdb=dbq&dbq=<filename>
  110. if (fCreateDB) {
  111. strcpy(szAttr2, szAttr);
  112. for(; p && *p && *p!='&'; p++)
  113. *pszAttr++= *p;
  114. // assert: szAttr= "driver=foo\0dsn=bar\0CREATE_DB"
  115. // assert: szAttr2= "driver=foo\0dsn=bar\0"
  116. p = strstr(rgchQuery, szDBQEqual)+3;
  117. pszAttr2=szAttr2+strlen(szAttr2)+1;
  118. strcpy(pszAttr2,szDBQ);
  119. // assert: szAttr2= "driver=foo\0dsn=bar\0dbq"
  120. pszAttr2+=3;
  121. for(; p && *p && *p!='&'; p++) {
  122. *pszAttr++= *p;
  123. *pszAttr2++= *p;
  124. }
  125. // assert: szAttr= "driver=foo\0dsn=bar\0CREATE_DB=<filename>"
  126. // assert: szAttr2= "driver=foo\0dsn=bar\0dbq=<filename>"
  127. strcpy(pszAttr, szGeneral);
  128. pszAttr+=9;
  129. *pszAttr2++='\0';
  130. *pszAttr2='\0';
  131. // assert: szAttr= "driver=foo\0dsn=bar\0CREATE_DB=<filename> General"
  132. }
  133. else {
  134. p = strstr(rgchQuery, szDBQEqual);
  135. for(; p && *p && *p!='&'; p++)
  136. *pszAttr++= *p;
  137. }
  138. }
  139. LoadString(hInst, IDS_ATTR, szTmp, sizeof(szTmp));
  140. // now add any additional items from attribute string
  141. p=strstr(rgchQuery, szTmp);
  142. if (p != NULL) {
  143. p+=5;
  144. for(; p && *p && *p!='&'; p++) {
  145. if ( *p == ';' ) {
  146. *pszAttr++='\0';
  147. } else if ( *p == '+') {
  148. *pszAttr++=' ';
  149. } else {
  150. *pszAttr++ = *p;
  151. }
  152. }
  153. }
  154. *pszAttr='\0';
  155. // call ODBC to add the data source
  156. rc= SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, szDriver, szAttr);
  157. LoadString(hInst, IDS_CREATE_DB, szTmp, sizeof(szTmp));
  158. // special case for Access: if just created a database, now need to add the DSN
  159. if (rc && strstr(rgchQuery, szTmp)) {
  160. rc= SQLConfigDataSource(NULL, ODBC_ADD_SYS_DSN, szDriver, szAttr2);
  161. }
  162. LoadString(hInst, IDS_START_ODBC, szTmp, sizeof(szTmp));
  163. StartHTML(szTmp, 1);
  164. LoadString(hInst, IDS_DATASOURCE_CREATE, szTmp, sizeof(szTmp));
  165. LoadString(hInst, IDS_SUCCESSFUL, szSuccessful, sizeof(szSuccessful));
  166. LoadString(hInst, IDS_FAILED, szFail, sizeof(szFail));
  167. printf( szTmp, (rc) ? szSuccessful : szFail );
  168. EndHTML();
  169. return (1);
  170. } // main()