migratecred.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <limits.h>
  41. #include <stdarg.h>
  42. #include <stdlib.h>
  43. #ifndef _WIN32
  44. #include <sys/param.h> /* MAXPATHLEN */
  45. #endif
  46. #include "../plugins/rever/rever.h"
  47. #include "getopt_ext.h"
  48. static void usage(char *name)
  49. {
  50. fprintf(stderr, "usage: %s -o 5.0InstancePath -n 5.1InstancePath -c 5.0Credential\n", name);
  51. exit(1);
  52. }
  53. #ifdef _WIN32
  54. /* converts '\' chars to '/' */
  55. static void dostounixpath(char *szText)
  56. {
  57. if(szText)
  58. {
  59. while(*szText)
  60. {
  61. if( *szText == '\\' )
  62. *szText = '/';
  63. szText++;
  64. }
  65. }
  66. }
  67. #endif
  68. /* Script used during 5.0 to 5.1 migration: replication and
  69. chaining backend credentials must be converted.
  70. Assumption: the built-in des-plugin.so lib has been used
  71. in 5.0 and is used in 5.1
  72. Usage: migrateCred
  73. -o <5.0 instance path>
  74. -n <5.1 instance path>
  75. -c <5.0 credential, with prefix>
  76. Return 5.1 credential with prefix
  77. */
  78. int
  79. main( int argc, char **argv)
  80. {
  81. char *cmd = argv[0];
  82. char *oldpath = NULL;
  83. char *newpath = NULL;
  84. char *prefixCred = NULL;
  85. char *cred = NULL;
  86. char *newcred = NULL;
  87. migrate_fn_type fct = NULL;
  88. char libpath[MAXPATHLEN];
  89. char *shared_lib;
  90. char *opts = "o:n:c:";
  91. int i;
  92. while (( i = getopt( argc, argv, opts )) != EOF )
  93. {
  94. switch (i)
  95. {
  96. case 'o':
  97. oldpath = strdup(optarg);
  98. #ifdef _WIN32
  99. dostounixpath(oldpath);
  100. #endif /* _WIN32 */
  101. break;
  102. case 'n':
  103. newpath = strdup(optarg);
  104. #ifdef _WIN32
  105. dostounixpath(newpath);
  106. #endif /* _WIN32 */
  107. break;
  108. case 'c':
  109. {
  110. char *end = NULL;
  111. int namelen;
  112. /* cred has the prefix, remove it before decoding */
  113. prefixCred = strdup(optarg);
  114. if ((*prefixCred == PWD_HASH_PREFIX_START) &&
  115. ((end = strchr(prefixCred, PWD_HASH_PREFIX_END)) != NULL) &&
  116. ((namelen = end - prefixCred - 1 ) <= (3*PWD_MAX_NAME_LEN)) )
  117. {
  118. cred = prefixCred + namelen + 2;
  119. }
  120. else
  121. {
  122. fprintf(stderr, "Invalid -c argument: %s (wrong prefix?).\n", prefixCred);
  123. }
  124. }
  125. break;
  126. default:
  127. usage(cmd);
  128. }
  129. }
  130. if ( !oldpath || !newpath || !cred )
  131. {
  132. usage(cmd);
  133. }
  134. #if defined( XP_WIN32 )
  135. shared_lib = ".dll";
  136. #else
  137. #ifdef HPUX
  138. #ifdef __ia64
  139. shared_lib = ".so";
  140. #else
  141. shared_lib = ".sl";
  142. #endif
  143. #else
  144. #ifdef AIX
  145. #if OSVERSION >= 4200
  146. shared_lib = ".so";
  147. #else
  148. shared_lib = "_shr.a";
  149. #endif
  150. #else
  151. shared_lib = ".so";
  152. #endif
  153. #endif
  154. #endif
  155. sprintf(libpath, "%s/../lib/des-plugin%s", newpath, shared_lib);
  156. fct = (migrate_fn_type)sym_load(libpath, "migrateCredentials",
  157. "DES Plugin", 1 /* report errors */ );
  158. if ( fct == NULL )
  159. {
  160. return(1);
  161. }
  162. newcred = (fct)(oldpath, newpath, cred);
  163. fprintf(stdout, "%s", newcred);
  164. return(0);
  165. }