migratecred.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <limits.h>
  44. #include <stdarg.h>
  45. #include <stdlib.h>
  46. #ifdef HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. #ifndef _WIN32
  50. #include <sys/param.h> /* MAXPATHLEN */
  51. #endif
  52. #include "../plugins/rever/rever.h"
  53. #include "getopt_ext.h"
  54. static void usage(char *name)
  55. {
  56. fprintf(stderr, "usage: %s -o OldInstancePath -n NewInstancePath -c OldCredential [-p NewPluginPath]\n", name);
  57. fprintf(stderr, "New plugin path defaults to [%s] if not given\n", PLUGINDIR);
  58. exit(1);
  59. }
  60. #ifdef _WIN32
  61. /* converts '\' chars to '/' */
  62. static void dostounixpath(char *szText)
  63. {
  64. if(szText)
  65. {
  66. while(*szText)
  67. {
  68. if( *szText == '\\' )
  69. *szText = '/';
  70. szText++;
  71. }
  72. }
  73. }
  74. #endif
  75. /* Script used during migration: replication and
  76. chaining backend credentials must be converted.
  77. Assumption: the built-in des-plugin.so lib has been used
  78. in the old version and is used in the new version
  79. Usage: migrateCred
  80. -o <old instance path>
  81. -n <new instance path>
  82. -c <old credential, with prefix>
  83. Return new credential with prefix
  84. */
  85. int
  86. main( int argc, char **argv)
  87. {
  88. char *cmd = argv[0];
  89. char *oldpath = NULL;
  90. char *newpath = NULL;
  91. char *pluginpath = NULL;
  92. char *prefixCred = NULL;
  93. char *cred = NULL;
  94. char *newcred = NULL;
  95. migrate_fn_type fct = NULL;
  96. char libpath[MAXPATHLEN];
  97. char *shared_lib;
  98. char *opts = "o:n:c:p:";
  99. int i;
  100. while (( i = getopt( argc, argv, opts )) != EOF )
  101. {
  102. switch (i)
  103. {
  104. case 'o':
  105. oldpath = strdup(optarg);
  106. #ifdef _WIN32
  107. dostounixpath(oldpath);
  108. #endif /* _WIN32 */
  109. break;
  110. case 'n':
  111. newpath = strdup(optarg);
  112. #ifdef _WIN32
  113. dostounixpath(newpath);
  114. #endif /* _WIN32 */
  115. break;
  116. case 'c':
  117. {
  118. char *end = NULL;
  119. int namelen;
  120. /* cred has the prefix, remove it before decoding */
  121. prefixCred = strdup(optarg);
  122. if ((*prefixCred == PWD_HASH_PREFIX_START) &&
  123. ((end = strchr(prefixCred, PWD_HASH_PREFIX_END)) != NULL) &&
  124. ((namelen = end - prefixCred - 1 ) <= (3*PWD_MAX_NAME_LEN)) )
  125. {
  126. cred = prefixCred + namelen + 2;
  127. }
  128. else
  129. {
  130. fprintf(stderr, "Invalid -c argument: %s (wrong prefix?).\n", prefixCred);
  131. }
  132. }
  133. break;
  134. case 'p':
  135. pluginpath = strdup(optarg);
  136. #ifdef _WIN32
  137. dostounixpath(pluginpath);
  138. #endif /* _WIN32 */
  139. break;
  140. default:
  141. usage(cmd);
  142. }
  143. }
  144. if ( !oldpath || !newpath || !cred )
  145. {
  146. usage(cmd);
  147. }
  148. #if defined( XP_WIN32 )
  149. shared_lib = ".dll";
  150. #else
  151. #ifdef HPUX
  152. #ifdef __ia64
  153. shared_lib = ".so";
  154. #else
  155. shared_lib = ".sl";
  156. #endif
  157. #else
  158. #ifdef AIX
  159. #if OSVERSION >= 4200
  160. shared_lib = ".so";
  161. #else
  162. shared_lib = "_shr.a";
  163. #endif
  164. #else
  165. shared_lib = ".so";
  166. #endif
  167. #endif
  168. #endif
  169. if (!pluginpath) {
  170. pluginpath = strdup(PLUGINDIR);
  171. #ifdef _WIN32
  172. dostounixpath(pluginpath);
  173. #endif /* _WIN32 */
  174. }
  175. if (access(pluginpath, R_OK)) {
  176. snprintf(libpath, sizeof(libpath), "%s/../lib/des-plugin%s", newpath, shared_lib);
  177. libpath[sizeof(libpath)-1] = 0;
  178. } else {
  179. snprintf(libpath, sizeof(libpath), "%s/libdes-plugin%s", pluginpath, shared_lib);
  180. libpath[sizeof(libpath)-1] = 0;
  181. }
  182. fct = (migrate_fn_type)sym_load(libpath, "migrateCredentials",
  183. "DES Plugin", 1 /* report errors */ );
  184. if ( fct == NULL )
  185. {
  186. usage(cmd);
  187. return(1);
  188. }
  189. newcred = (fct)(oldpath, newpath, cred);
  190. fprintf(stdout, "%s", newcred);
  191. return(0);
  192. }