pwenc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #if defined( _WIN32 )
  42. #include <sys/stat.h> /* for S_IREAD and S_IWRITE */
  43. #include <windows.h>
  44. #include <time.h>
  45. #include "proto-ntutil.h"
  46. #else
  47. #include <sys/socket.h>
  48. #include <sys/errno.h>
  49. #include <sys/param.h>
  50. #include <sys/types.h>
  51. #if defined(LINUX) /* I bet other Unix would like
  52. * this flag. But don't want to
  53. * break other builds so far */
  54. #include <unistd.h>
  55. #endif
  56. #endif
  57. #include <stdio.h>
  58. #include <string.h>
  59. #include <ctype.h>
  60. #include <stdlib.h>
  61. #include "ldap.h"
  62. #include "../slapi-plugin.h"
  63. #include "../slap.h"
  64. #include <nspr.h>
  65. #include <nss.h>
  66. #include "../../plugins/pwdstorage/pwdstorage.h"
  67. int ldap_syslog;
  68. int ldap_syslog_level;
  69. int slapd_ldap_debug = LDAP_DEBUG_ANY;
  70. #ifdef _WIN32
  71. int *module_ldap_debug;
  72. #endif
  73. int detached;
  74. FILE *error_logfp;
  75. FILE *access_logfp;
  76. struct pw_scheme *pwdhashscheme;
  77. int heflag = 0;
  78. static int slapd_config(const char *configdir, const char *configfile);
  79. static int entry_has_attr_and_value(Slapi_Entry *e, const char *attrname, char *value);
  80. static void
  81. usage( name )
  82. char *name;
  83. {
  84. fprintf( stderr, "usage: %s -D config-dir [-H] [-s scheme | -c comparepwd ] password...\n", name );
  85. exit( 1 );
  86. }
  87. /*
  88. * If global "heflag" is non-zero, un-hex-encode the string
  89. * and return a decoded copy. Otherwise return a copy of the
  90. * string.
  91. */
  92. static char *
  93. decode( char *orig )
  94. {
  95. char *r;
  96. if ( NULL == orig ) {
  97. return NULL;
  98. }
  99. r = slapi_ch_calloc( 1, strlen( orig ) + 2 );
  100. strcpy( r, orig );
  101. if ( heflag ) {
  102. char *s;
  103. for ( s = r; *s != '\0'; ++s ) {
  104. if ( *s == '%' && ldap_utf8isxdigit( s+1 ) && ldap_utf8isxdigit( s+2 )) {
  105. memmove( s, s + 1, 2 );
  106. s[ 2 ] = '\0';
  107. *s = strtoul( s, NULL, 16 );
  108. memmove( s + 1, s + 3, strlen( s + 3 ) + 1 );
  109. }
  110. }
  111. }
  112. return r;
  113. }
  114. static slapdFrontendConfig_t *
  115. init_config(char *configdir)
  116. {
  117. char *abs_configdir = NULL;
  118. char *configfile = NULL;
  119. char errorbuf[SLAPI_DSE_RETURNTEXT_SIZE];
  120. slapdFrontendConfig_t *slapdFrontendConfig = NULL;
  121. if (configdir == NULL) { /* use default */
  122. configdir = TEMPLATEDIR;
  123. configfile = "template-dse.ldif";
  124. }
  125. /* kexcoff: quite the same as slapd_bootstrap_config */
  126. FrontendConfig_init();
  127. abs_configdir = rel2abspath( configdir );
  128. if ( config_set_configdir( "configdir (-D)", abs_configdir,
  129. errorbuf, 1) != LDAP_SUCCESS ) {
  130. fprintf( stderr, "%s\n", errorbuf );
  131. return( NULL );
  132. }
  133. slapi_ch_free_string(&abs_configdir);
  134. slapdFrontendConfig = getFrontendConfig();
  135. if (0 == slapd_config(slapdFrontendConfig->configdir, configfile)) {
  136. fprintf(stderr,
  137. "The configuration files in directory %s could not be read or were not found. Please refer to the error log or output for more information.\n",
  138. slapdFrontendConfig->configdir);
  139. return(NULL);
  140. }
  141. return slapdFrontendConfig;
  142. }
  143. int
  144. main( argc, argv )
  145. int argc;
  146. char *argv[];
  147. {
  148. int i, rc;
  149. char *enc, *cmp, *name;
  150. struct pw_scheme *pwsp, *cmppwsp;
  151. extern int optind;
  152. char *cpwd = NULL; /* candidate password for comparison */
  153. slapdFrontendConfig_t *slapdFrontendConfig = NULL;
  154. char *opts = "Hs:c:D:";
  155. name = argv[ 0 ];
  156. pwsp = cmppwsp = NULL;
  157. #ifdef _WIN32
  158. module_ldap_debug = &slapd_ldap_debug;
  159. libldap_init_debug_level(&slapd_ldap_debug);
  160. #endif
  161. PR_Init( PR_USER_THREAD, PR_PRIORITY_NORMAL, 0 );
  162. /* Initialize NSS to make ds_salted_sha1_pw_enc() work */
  163. if (NSS_NoDB_Init(NULL) != SECSuccess) {
  164. fprintf( stderr, "Fatal error: unable to initialize the NSS subcomponent." );
  165. return( 1 );
  166. }
  167. while (( i = getopt( argc, argv, opts )) != EOF ) {
  168. switch ( i ) {
  169. case 'D':
  170. if (slapdFrontendConfig) {
  171. fprintf(stderr, "The -D configdir argument must be given only once, and must be the first argument given\n");
  172. usage(name);
  173. return 1;
  174. }
  175. if (!(slapdFrontendConfig = init_config(optarg))) {
  176. return(1);
  177. }
  178. break;
  179. case 's': /* set hash scheme */
  180. if (!slapdFrontendConfig) {
  181. if (!(slapdFrontendConfig = init_config(NULL))) {
  182. usage( name );
  183. return(1);
  184. }
  185. }
  186. if (( pwsp = pw_name2scheme( optarg )) == NULL ) {
  187. fprintf( stderr, "%s: unknown hash scheme \"%s\"\n", name,
  188. optarg );
  189. return( 1 );
  190. }
  191. break;
  192. case 'c': /* compare encoded password to password */
  193. if (!slapdFrontendConfig) {
  194. if (!(slapdFrontendConfig = init_config(NULL))) {
  195. usage( name );
  196. return(1);
  197. }
  198. }
  199. cpwd = optarg;
  200. break;
  201. case 'H': /* password(s) is(are) hex-encoded */
  202. if (!slapdFrontendConfig) {
  203. if (!(slapdFrontendConfig = init_config(NULL))) {
  204. usage( name );
  205. return(1);
  206. }
  207. }
  208. heflag = 1;
  209. break;
  210. default:
  211. usage( name );
  212. }
  213. }
  214. if (!slapdFrontendConfig) {
  215. if (!(slapdFrontendConfig = init_config(NULL))) {
  216. usage( name );
  217. return(1);
  218. }
  219. }
  220. if ( cpwd != NULL ) {
  221. cmppwsp = pw_val2scheme( decode( cpwd ), &cmp, 1 );
  222. }
  223. if ( cmppwsp != NULL && pwsp != NULL ) {
  224. fprintf( stderr, "%s: do not use -s with -c\n", name );
  225. usage( name );
  226. }
  227. if ( cmppwsp == NULL && pwsp == NULL ) {
  228. pwsp = pw_name2scheme( SALTED_SHA1_SCHEME_NAME );
  229. }
  230. if ( argc <= optind ) {
  231. usage( name );
  232. }
  233. if ( cmppwsp == NULL && pwsp->pws_enc == NULL ) {
  234. fprintf( stderr,
  235. "The scheme \"%s\" does not support password encoding.\n",
  236. pwsp->pws_name );
  237. return( 1 );
  238. }
  239. srand((int)time(NULL)); /* schemes such as crypt use random salt */
  240. for ( rc = 0; optind < argc && rc == 0; ++optind ) {
  241. if ( cmppwsp == NULL ) { /* encode passwords */
  242. if (( enc = (*pwsp->pws_enc)( decode( argv[ optind ] ))) == NULL ) {
  243. perror( name );
  244. return( 1 );
  245. }
  246. puts( enc );
  247. slapi_ch_free_string( &enc );
  248. } else { /* compare passwords */
  249. if (( rc = (*(cmppwsp->pws_cmp))( decode( argv[ optind ]), cmp )) == 0 ) {
  250. printf( "%s: password ok.\n", name );
  251. } else {
  252. printf( "%s: password does not match.\n", name );
  253. }
  254. }
  255. }
  256. return( rc == 0 ? 0 : 1 );
  257. }
  258. /* -------------------------------------------------------------- */
  259. /*
  260. kexcoff: quite similar to slapd_bootstrap_config() from the server,
  261. but it only loads password storage scheme plugins
  262. */
  263. static int
  264. slapd_config(const char *configdir, const char *givenconfigfile)
  265. {
  266. char configfile[MAXPATHLEN+1];
  267. PRFileInfo prfinfo;
  268. int rc = 0; /* Fail */
  269. int done = 0;
  270. PRInt32 nr = 0;
  271. PRFileDesc *prfd = 0;
  272. char *buf = 0;
  273. char *lastp = 0;
  274. char *entrystr = 0;
  275. if (!givenconfigfile) {
  276. givenconfigfile = CONFIG_FILENAME;
  277. }
  278. PR_snprintf(configfile, sizeof(configfile), "%s/%s", configdir, givenconfigfile);
  279. if ( (rc = PR_GetFileInfo( configfile, &prfinfo )) != PR_SUCCESS )
  280. {
  281. fprintf(stderr,
  282. "The given config file %s could not be accessed, error %d\n",
  283. configfile, rc);
  284. exit( 1 );
  285. }
  286. else if (( prfd = PR_Open( configfile, PR_RDONLY,
  287. SLAPD_DEFAULT_FILE_MODE )) == NULL )
  288. {
  289. fprintf(stderr,
  290. "The given config file %s could not be read\n",
  291. configfile);
  292. exit( 1 );
  293. }
  294. else
  295. {
  296. /* read the entire file into core */
  297. buf = slapi_ch_malloc( prfinfo.size + 1 );
  298. if (( nr = slapi_read_buffer( prfd, buf, prfinfo.size )) < 0 )
  299. {
  300. fprintf(stderr,
  301. "Could only read %d of %d bytes from config file %s\n",
  302. nr, prfinfo.size, configfile);
  303. exit( 1 );
  304. }
  305. (void)PR_Close(prfd);
  306. buf[ nr ] = '\0';
  307. if(!done)
  308. {
  309. /* Convert LDIF to entry structures */
  310. Slapi_DN plug_dn;
  311. slapi_sdn_init_dn_byref(&plug_dn, PLUGIN_BASE_DN);
  312. while ((entrystr = dse_read_next_entry(buf, &lastp)) != NULL)
  313. {
  314. /*
  315. * XXXmcs: it would be better to also pass
  316. * SLAPI_STR2ENTRY_REMOVEDUPVALS in the flags, but
  317. * duplicate value checking requires that the syntax
  318. * and schema subsystems be initialized... and they
  319. * are not yet.
  320. */
  321. Slapi_Entry *e = slapi_str2entry(entrystr,
  322. SLAPI_STR2ENTRY_NOT_WELL_FORMED_LDIF);
  323. if (e == NULL)
  324. {
  325. fprintf(stderr,
  326. "The entry [%s] in the configfile %s was empty or could not be parsed\n",
  327. entrystr, configfile);
  328. continue;
  329. }
  330. /* see if the entry is a child of the plugin base dn */
  331. if (slapi_sdn_isgrandparent(&plug_dn,
  332. slapi_entry_get_sdn_const(e)))
  333. {
  334. if ( entry_has_attr_and_value(e, ATTR_PLUGIN_TYPE, "pwdstoragescheme"))
  335. {
  336. /* add the syntax/matching/pwd storage scheme rule plugin */
  337. if (plugin_setup(e, 0, 0, 1))
  338. {
  339. fprintf(stderr,
  340. "The plugin entry [%s] in the configfile %s was invalid\n",
  341. slapi_entry_get_dn(e), configfile);
  342. exit(1); /* yes this sucks, but who knows what else would go on if I did the right thing */
  343. }
  344. else
  345. {
  346. e = 0; /* successful plugin_setup consumes entry */
  347. }
  348. }
  349. }
  350. if (e)
  351. slapi_entry_free(e);
  352. }
  353. /* kexcoff: initialize rootpwstoragescheme and pw_storagescheme
  354. * if not explicilty set in the config file
  355. */
  356. config_set_storagescheme();
  357. slapi_sdn_done(&plug_dn);
  358. rc= 1; /* OK */
  359. }
  360. slapi_ch_free_string(&buf);
  361. }
  362. return rc;
  363. }
  364. /*
  365. kexcoff: direclty copied fron the server code
  366. See if the given entry has an attribute with the given name and the
  367. given value; if value is NULL, just test for the presence of the given
  368. attribute; if value is an empty string (i.e. value[0] == 0),
  369. the first value in the attribute will be copied into the given buffer
  370. and returned
  371. */
  372. static int
  373. entry_has_attr_and_value(Slapi_Entry *e, const char *attrname,
  374. char *value)
  375. {
  376. int retval = 0;
  377. Slapi_Attr *attr = 0;
  378. if (!e || !attrname)
  379. return retval;
  380. /* see if the entry has the specified attribute name */
  381. if (!slapi_entry_attr_find(e, attrname, &attr) && attr)
  382. {
  383. /* if value is not null, see if the attribute has that
  384. value */
  385. if (!value)
  386. {
  387. retval = 1;
  388. }
  389. else
  390. {
  391. Slapi_Value *v = 0;
  392. int index = 0;
  393. for (index = slapi_attr_first_value(attr, &v);
  394. v && (index != -1);
  395. index = slapi_attr_next_value(attr, index, &v))
  396. {
  397. const char *s = slapi_value_get_string(v);
  398. if (!s)
  399. continue;
  400. if (!*value)
  401. {
  402. strcpy(value, s);
  403. retval = 1;
  404. break;
  405. }
  406. else if (!strcasecmp(s, value))
  407. {
  408. retval = 1;
  409. break;
  410. }
  411. }
  412. }
  413. }
  414. return retval;
  415. }