migratecred.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <limits.h>
  15. #include <stdarg.h>
  16. #include <stdlib.h>
  17. #ifdef HAVE_UNISTD_H
  18. #include <unistd.h>
  19. #endif
  20. #include <sys/param.h> /* MAXPATHLEN */
  21. #include "../plugins/rever/rever.h"
  22. #include "getopt_ext.h"
  23. static void usage(char *name)
  24. {
  25. fprintf(stderr, "usage: %s -o OldInstancePath -n NewInstancePath -c OldCredential [-p NewPluginPath]\n", name);
  26. fprintf(stderr, "New plugin path defaults to [%s] if not given\n", PLUGINDIR);
  27. exit(1);
  28. }
  29. /* Script used during migration: replication and
  30. chaining backend credentials must be converted.
  31. Assumption: the built-in des-plugin.so lib has been used
  32. in the old version and is used in the new version
  33. Usage: migrateCred
  34. -o <old instance path>
  35. -n <new instance path>
  36. -c <old credential, with prefix>
  37. Return new credential with prefix
  38. */
  39. int
  40. main( int argc, char **argv)
  41. {
  42. char *cmd = argv[0];
  43. char *oldpath = NULL;
  44. char *newpath = NULL;
  45. char *pluginpath = NULL;
  46. char *prefixCred = NULL;
  47. char *cred = NULL;
  48. char *newcred = NULL;
  49. migrate_fn_type fct = NULL;
  50. char libpath[MAXPATHLEN];
  51. char *shared_lib;
  52. char *opts = "o:n:c:p:";
  53. int i;
  54. while (( i = getopt( argc, argv, opts )) != EOF )
  55. {
  56. switch (i)
  57. {
  58. case 'o':
  59. oldpath = strdup(optarg);
  60. break;
  61. case 'n':
  62. newpath = strdup(optarg);
  63. break;
  64. case 'c':
  65. {
  66. char *end = NULL;
  67. int namelen;
  68. /* cred has the prefix, remove it before decoding */
  69. prefixCred = strdup(optarg);
  70. if (prefixCred && (*prefixCred == PWD_HASH_PREFIX_START) &&
  71. ((end = strchr(prefixCred, PWD_HASH_PREFIX_END)) != NULL) &&
  72. ((namelen = end - prefixCred - 1 ) <= (3*PWD_MAX_NAME_LEN)) )
  73. {
  74. cred = prefixCred + namelen + 2;
  75. }
  76. else
  77. {
  78. fprintf(stderr, "Invalid -c argument: %s (wrong prefix?).\n", prefixCred);
  79. }
  80. }
  81. break;
  82. case 'p':
  83. pluginpath = strdup(optarg);
  84. break;
  85. default:
  86. usage(cmd);
  87. }
  88. }
  89. if ( !oldpath || !newpath || !cred )
  90. {
  91. free(oldpath);
  92. free(newpath);
  93. free(prefixCred);
  94. free(pluginpath);
  95. usage(cmd);
  96. }
  97. #ifdef HPUX
  98. #ifdef __ia64
  99. shared_lib = ".so";
  100. #else
  101. shared_lib = ".sl";
  102. #endif
  103. #else
  104. shared_lib = ".so";
  105. #endif
  106. if (!pluginpath) {
  107. pluginpath = strdup(PLUGINDIR);
  108. }
  109. if (access(pluginpath, R_OK)) {
  110. snprintf(libpath, sizeof(libpath), "%s/../lib/des-plugin%s", newpath, shared_lib);
  111. libpath[sizeof(libpath)-1] = 0;
  112. } else {
  113. snprintf(libpath, sizeof(libpath), "%s/libdes-plugin%s", pluginpath, shared_lib);
  114. libpath[sizeof(libpath)-1] = 0;
  115. }
  116. fct = (migrate_fn_type)sym_load(libpath, "migrateCredentials",
  117. "DES Plugin", 1 /* report errors */ );
  118. if ( fct == NULL )
  119. {
  120. free(oldpath);
  121. free(newpath);
  122. free(prefixCred);
  123. free(pluginpath);
  124. usage(cmd);
  125. return(1);
  126. }
  127. newcred = (fct)(oldpath, newpath, cred);
  128. fprintf(stdout, "%s", newcred);
  129. free(oldpath);
  130. free(newpath);
  131. free(prefixCred);
  132. free(pluginpath);
  133. return(0);
  134. }