domodify.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. /*
  39. * domodify.c -- LDAP modify CGI handler -- HTTP gateway
  40. */
  41. #include "dsgw.h"
  42. #include "dbtdsgw.h"
  43. #define DSGW_CHANGETYPE_UNKNOWN 0
  44. #define DSGW_CHANGETYPE_MODIFY 1
  45. #define DSGW_CHANGETYPE_ADD 2
  46. #define DSGW_CHANGETYPE_DELETE 3
  47. #define DSGW_CHANGETYPE_MODRDN 4
  48. static void post_request();
  49. static int entry_modify_or_add( LDAP *ld, char *dn, int add, int *pwdchangedp );
  50. static int entry_delete( LDAP *ld, char *dn );
  51. static int entry_modrdn( LDAP *ld, char *dn, char *newrdn, int deleteoldrdn );
  52. static int gather_passwd_changes( char *dn, LDAPMod ***pmodsp,
  53. int adding_entry, int *pwdchangedp );
  54. static void modify_error( int lderr, char *lderrtxt );
  55. static void addmodifyop( LDAPMod ***pmodsp, int modop, char *attr,
  56. char *value, int vlen );
  57. static void remove_modifyops( LDAPMod **pmods, char *attr );
  58. static int starts_with( char *s, char *startswith );
  59. static char **post2multilinevals( char *postedval );
  60. static char **post2vals( char *postedval );
  61. static int require_oldpasswd( char *modifydn );
  62. static int value_is_unique( LDAP *ld, char *dn, char *attr, char *value );
  63. static int verbose = 0;
  64. static int quiet = 0;
  65. static int display_results_inline = 0;
  66. int main( argc, argv, env )
  67. int argc;
  68. char *argv[];
  69. #ifdef DSGW_DEBUG
  70. char *env[];
  71. #endif
  72. {
  73. (void)dsgw_init( argc, argv, DSGW_METHOD_POST );
  74. dsgw_send_header();
  75. #ifdef DSGW_DEBUG
  76. dsgw_logstringarray( "env", env );
  77. #endif
  78. post_request();
  79. exit( 0 );
  80. }
  81. static void
  82. post_request()
  83. {
  84. LDAP *ld;
  85. int rc, changetype, dnlen, i, passwd_changed, discard_authcreds;
  86. char *s, *encodeddn, *dn, *newrdn, *changedesc, **rdns, **oldrdns,
  87. *jscomp, *entry_name, *new_name, *success_msg;
  88. char *old_dn;
  89. char buf[ 256 ];
  90. #if 0
  91. FILE *genfp;
  92. #endif
  93. passwd_changed = discard_authcreds = 0;
  94. s = dsgw_get_cgi_var( "changetype", DSGW_CGIVAR_REQUIRED );
  95. changedesc = XP_GetClientStr(DBT_Editing_);
  96. if ( strcasecmp( s, "modify" ) == 0 ) {
  97. changetype = DSGW_CHANGETYPE_MODIFY;
  98. } else if ( strcasecmp( s, "add" ) == 0 ) {
  99. changetype = DSGW_CHANGETYPE_ADD;
  100. changedesc = XP_GetClientStr(DBT_Adding_);
  101. } else if ( strcasecmp( s, "delete" ) == 0 ) {
  102. changetype = DSGW_CHANGETYPE_DELETE;
  103. changedesc = XP_GetClientStr(DBT_Deleting_);
  104. } else if ( strcasecmp( s, "modrdn" ) == 0 ) {
  105. changetype = DSGW_CHANGETYPE_MODRDN;
  106. changedesc = XP_GetClientStr(DBT_Renaming_);
  107. } else {
  108. changetype = DSGW_CHANGETYPE_UNKNOWN;
  109. }
  110. encodeddn = dsgw_get_cgi_var( "dn", DSGW_CGIVAR_REQUIRED );
  111. /* undo extra level of escaping on DN */
  112. dn = dsgw_ch_strdup( encodeddn );
  113. dsgw_form_unescape( dn );
  114. old_dn = dn;
  115. quiet = dsgw_get_boolean_var( "quiet", DSGW_CGIVAR_OPTIONAL, 0 );
  116. #if 0
  117. /*
  118. * If the "genscreen" form variable is set, it is the name of a
  119. * genscreen-compatible HTML template to display the domodify results
  120. * within. We replace the "DS_LAST_OP_INFO" directive with our own
  121. * "domodify" output. Presence of "genscreen" also turns on quiet mode.
  122. */
  123. if (( s = dsgw_get_cgi_var( "genscreen", DSGW_CGIVAR_OPTIONAL )) != NULL &&
  124. dsgw_genscreen_begin( s, &genfp, DRCT_DS_LAST_OP_INFO, 0 ) == 0 ) {
  125. quiet = display_results_inline = 1;
  126. }
  127. #endif
  128. verbose = dsgw_get_boolean_var( "verbose", DSGW_CGIVAR_OPTIONAL, 0 );
  129. if ( verbose ) {
  130. quiet = 0; /* verbose overrides quiet */
  131. }
  132. if ( dsgw_init_ldap( &ld, NULL, 0, 0) != DSGW_BOUND_ASUSER ) {
  133. dsgw_emitf( XP_GetClientStr(DBT_warningNoAuthenticationContinuin_) );
  134. }
  135. if ( !quiet ) {
  136. PR_snprintf( buf, 256,
  137. XP_GetClientStr(DBT_SDirectoryEntry_), changedesc );
  138. dsgw_html_begin( buf, 1 );
  139. } else {
  140. dsgw_html_begin( NULL, 0 );
  141. }
  142. dsgw_emits( "\n<FONT SIZE=+1>\n" );
  143. rdns = ldap_explode_dn( dn, 1 );
  144. if ( rdns == NULL || rdns[ 0 ] == NULL ) {
  145. entry_name = dn;
  146. } else {
  147. entry_name = dsgw_ch_strdup( rdns[ 0 ] );
  148. }
  149. new_name = success_msg = "";
  150. dsgw_emitf( "%s <B>%s</B>...\n</FONT>\n\n", changedesc, entry_name );
  151. if ( rdns != NULL ) {
  152. ldap_value_free( rdns );
  153. }
  154. if ( verbose ) {
  155. dsgw_emitf( XP_GetClientStr(DBT_PreEntryDnSPrePN_), dn );
  156. }
  157. /*
  158. * For end-user CGIs under admin server, if we're talking to a local DB,
  159. * then there's no access control, and therefore we need to disallow
  160. * people from changing entries other than their own. Do that check right
  161. * here.
  162. */
  163. if ( gc->gc_enduser && gc->gc_localdbconf != NULL ) {
  164. char *bdn;
  165. (void)dsgw_get_adm_identity( ld, NULL, &bdn, NULL, DSGW_ERROPT_EXIT );
  166. /* Make sure DN we're bound as matches the DN being modified */
  167. if ( dsgw_dn_cmp( dn, bdn ) == 0 ) {
  168. /* Not the same - generate an error and bail out */
  169. dsgw_error( DSGW_ERR_LOCALDB_PERMISSION_DENIED, NULL,
  170. DSGW_ERROPT_EXIT, 0, NULL );
  171. }
  172. }
  173. rc = LDAP_SUCCESS;
  174. switch( changetype ) {
  175. case DSGW_CHANGETYPE_MODIFY:
  176. if ( dsgw_get_boolean_var( "changed_DN", DSGW_CGIVAR_OPTIONAL, 0 )) {
  177. /* Collect all the inputs named "replace_DN_attr", where
  178. attr is an LDAP attribute type. Construct an AVA from
  179. each such input, and combine the AVAs to form newrdn.
  180. */
  181. auto int i = 0;
  182. auto char *varname, *val;
  183. auto size_t newrdn_len;
  184. newrdn = NULL;
  185. while ( (varname = dsgw_next_cgi_var( &i, &val )) != NULL) {
  186. if ( starts_with( varname, "replace_" )) {
  187. auto char* attr = varname;
  188. auto int is_rdn = 0;
  189. {
  190. auto char* p;
  191. while (( p = strchr( attr, '_' )) != NULL ) {
  192. attr = p + 1;
  193. if ( starts_with( attr, "DN_" )) {
  194. is_rdn = 1;
  195. } /* ignore any other prefixes */
  196. }
  197. }
  198. if (is_rdn && strlen(val) > 0) {
  199. auto const size_t attrlen = strlen (attr);
  200. auto const size_t val_len = strlen (val);
  201. auto const size_t ava_len = attrlen + 1 + val_len;
  202. auto char* ava;
  203. if (newrdn == NULL) {
  204. ava = newrdn = dsgw_ch_malloc (ava_len + 1);
  205. newrdn_len = ava_len;
  206. } else {
  207. newrdn = dsgw_ch_realloc (newrdn, newrdn_len + ava_len + 2);
  208. memcpy (newrdn + newrdn_len, "+", 1);
  209. ava = newrdn + newrdn_len + 1;
  210. newrdn_len += (ava_len + 1);
  211. }
  212. memcpy (ava, attr, attrlen);
  213. memcpy (ava + attrlen, "=", 1);
  214. memcpy (ava + attrlen + 1, val, val_len + 1);
  215. }
  216. }
  217. free (varname);
  218. }
  219. if (newrdn) goto continue_modrdn;
  220. /* else failed to compute newrdn */
  221. }
  222. break;
  223. case DSGW_CHANGETYPE_MODRDN:
  224. newrdn = dsgw_get_cgi_var( "newrdn", DSGW_CGIVAR_REQUIRED );
  225. continue_modrdn:
  226. dsgw_remove_leading_and_trailing_spaces( &newrdn );
  227. rc = entry_modrdn( ld, dn, newrdn, dsgw_get_boolean_var( "deleteoldrdn",
  228. DSGW_CGIVAR_OPTIONAL, 0 ));
  229. if ( rc == LDAP_SUCCESS ) {
  230. /* construct the new DN so we can insert correct "edit" link */
  231. if (( oldrdns = ldap_explode_dn( dn, 0 )) == NULL ) {
  232. dsgw_error( DSGW_ERR_NOMEMORY, NULL, DSGW_ERROPT_EXIT,
  233. 0, NULL );
  234. }
  235. dnlen = strlen( newrdn ) + 1; /* room for "," */
  236. for ( i = 1; oldrdns[ i ] != NULL; ++i ) {
  237. dnlen += ( 1 + strlen( oldrdns[ i ] ));
  238. }
  239. dn = dsgw_ch_malloc( dnlen + 1 );
  240. *dn = '\0';
  241. strcat( dn, newrdn );
  242. for ( i = 1; oldrdns[ i ] != NULL; ++i ) {
  243. strcat( dn, "," );
  244. strcat( dn, oldrdns[ i ] );
  245. }
  246. ldap_value_free( oldrdns );
  247. free( encodeddn );
  248. encodeddn = dsgw_strdup_escaped( dn );
  249. success_msg = XP_GetClientStr(DBT_renamedBSBToBSB_);
  250. if (( rdns = ldap_explode_rdn( newrdn, 1 )) == NULL
  251. || rdns[ 0 ] == NULL ) {
  252. new_name = newrdn;
  253. } else {
  254. new_name = dsgw_ch_strdup (rdns[ 0 ]);
  255. ldap_value_free( rdns );
  256. }
  257. }
  258. break;
  259. default:
  260. break;
  261. }
  262. switch( changetype ) {
  263. case DSGW_CHANGETYPE_MODIFY:
  264. if (rc != LDAP_SUCCESS) break;
  265. case DSGW_CHANGETYPE_ADD:
  266. rc = entry_modify_or_add( ld, dn, changetype == DSGW_CHANGETYPE_ADD,
  267. &passwd_changed );
  268. if ( changetype == DSGW_CHANGETYPE_MODIFY ) {
  269. success_msg = XP_GetClientStr(DBT_changesToBSBHaveBeenSaved_);
  270. } else {
  271. success_msg = XP_GetClientStr(DBT_BSBHasBeenAdded_);
  272. }
  273. break;
  274. case DSGW_CHANGETYPE_DELETE:
  275. rc = entry_delete( ld, dn );
  276. success_msg = XP_GetClientStr(DBT_BSBHasBeenDeleted_);
  277. break;
  278. case DSGW_CHANGETYPE_MODRDN:
  279. break;
  280. default:
  281. rc = LDAP_PARAM_ERROR;
  282. }
  283. /*
  284. * If we are not running under the admin. server AND the operation
  285. * succeeded and the user is bound as the entry they just changed,
  286. * AND one of these conditions is true:
  287. * 1. we changed the password
  288. * 2. we did a modrdn
  289. * 3. we deleted the entry
  290. * then the auth. credentials should be discarded. If we do discard, we
  291. * print an informative message for the user.
  292. */
  293. if ( !gc->gc_admserv && rc == LDAP_SUCCESS &&
  294. ( changetype == DSGW_CHANGETYPE_DELETE || dn != old_dn ||
  295. ( changetype == DSGW_CHANGETYPE_MODIFY && passwd_changed )) &&
  296. dsgw_bound_as_dn( old_dn, 0 )) {
  297. char *authck;
  298. /* first, remove the cookie from the cookie database (ignore errors) */
  299. if (( authck = dsgw_get_auth_cookie()) != NULL ) {
  300. (void)dsgw_delcookie( authck );
  301. }
  302. /* output JavaScript to clear the cookie in the user's browser */
  303. dsgw_emits( "<SCRIPT LANGUAGE=\"JavaScript\">\n" );
  304. dsgw_emits( "<!-- Hide from non-JavaScript browsers\n" );
  305. dsgw_emitf( "document.cookie = '%s=%s; path=/'\n",
  306. DSGW_AUTHCKNAME, DSGW_UNAUTHSTR );
  307. dsgw_emits( "// End Hiding -->\n</SCRIPT>\n" );
  308. dsgw_emitf( XP_GetClientStr(DBT_PBNoteBBecauseYouSTheEntryYouWer_),
  309. ( changetype == DSGW_CHANGETYPE_DELETE ) ? XP_GetClientStr(DBT_deleted_) :
  310. ( dn != old_dn ) ? XP_GetClientStr(DBT_renamed_) :
  311. XP_GetClientStr(DBT_changedThePasswordOf_) );
  312. }
  313. if ( rc == LDAP_SUCCESS ) {
  314. /*
  315. * check for "completion_javascript" form var and
  316. * execute it if present.
  317. */
  318. jscomp = dsgw_get_cgi_var( "completion_javascript",
  319. DSGW_CGIVAR_OPTIONAL );
  320. if ( jscomp != NULL ) {
  321. char *entry_name_js;
  322. char *new_name_js;
  323. entry_name_js = dsgw_escape_quotes( entry_name );
  324. new_name_js = dsgw_escape_quotes( new_name );
  325. dsgw_emits( "<SCRIPT LANGUAGE=\"JavaScript\">\n" );
  326. dsgw_emits( "dsmodify_info = '" );
  327. dsgw_emitf( success_msg, entry_name_js, new_name_js );
  328. dsgw_emits( "';\n" );
  329. dsgw_emitf( "dsmodify_dn = '%s';\n",
  330. ( changetype == DSGW_CHANGETYPE_DELETE ) ? "":
  331. encodeddn );
  332. dsgw_emitf( "eval('%s');\n", jscomp );
  333. dsgw_emits( "</SCRIPT>\n" );
  334. }
  335. } else {
  336. jscomp = NULL;
  337. }
  338. if (( jscomp == NULL || changetype == DSGW_CHANGETYPE_DELETE )
  339. && !gc->gc_admserv ) {
  340. dsgw_form_begin( NULL, NULL );
  341. dsgw_emits( "\n<CENTER><TABLE border=2 width=\"100%\"><TR>\n" );
  342. /*
  343. * Show framed button. If the modify succeeded, it is "Close".
  344. * If the modify failed, it is "Go Back."
  345. */
  346. dsgw_emits( "<TD WIDTH=\"100%\" ALIGN=\"center\">\n" );
  347. if ( rc == LDAP_SUCCESS ) {
  348. dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  349. "onClick=\"parent.close()\">\n",
  350. XP_GetClientStr(DBT_closeWindow_) );
  351. } else {
  352. dsgw_emitf( "<INPUT TYPE=\"button\" VALUE=\"%s\" "
  353. "onClick=\"history.back()\">\n",
  354. XP_GetClientStr(DBT_goBack_) );
  355. }
  356. dsgw_emits( "\n</TABLE></CENTER></FORM>\n" );
  357. }
  358. #if 0
  359. if ( display_results_inline && genfp != NULL ) {
  360. dsgw_emits( "<HR>\n" );
  361. dsgw_genscreen_continue( &genfp, NULL, 0 );
  362. } else if ( !quiet ) {
  363. dsgw_html_end();
  364. }
  365. #else
  366. if ( !quiet ) {
  367. dsgw_html_end();
  368. }
  369. #endif
  370. ldap_unbind( ld );
  371. if (old_dn != dn) free ( old_dn );
  372. free( dn );
  373. }
  374. static int
  375. entry_modify_or_add( LDAP *ld, char *dn, int add, int *pwdchangedp )
  376. {
  377. int lderr, i, j, opoffset, modop, mls, unique, unchanged_count;
  378. char *varname, *varvalue, *retval, *attr, *p, **vals, **unchanged_attrs;
  379. char *ntuserid = NULL;
  380. LDAPMod **pmods;
  381. int msgid;
  382. LDAPMessage *res = NULL;
  383. char *errmsg = NULL;
  384. int isNtUser = 0;
  385. pmods = NULL;
  386. unchanged_attrs = NULL;
  387. unchanged_count = 0;
  388. /*
  389. * Gather up password changes (if present in CGI POST)
  390. */
  391. if (( lderr = gather_passwd_changes( dn, &pmods, add, pwdchangedp ))
  392. != LDAP_SUCCESS ) {
  393. return( lderr );
  394. }
  395. if ( verbose ) {
  396. dsgw_emitf( "<PRE>\n" );
  397. }
  398. /*
  399. * Gather up other changes: each attribute value is POSTed in a variable
  400. * named:
  401. * add_[unique_]ATTR
  402. * replace_[unique_][DN_]ATTR
  403. * or delete_[unique_]ATTR
  404. *
  405. * where ATTR is the LDAP attribute name and "unique_" is optional (if
  406. * present, we check to make sure the value is not in use before accepting
  407. * a replace or add).
  408. *
  409. * Additionally, if a variable name changed_ATTR is POSTed and its value
  410. * is not "true", it is assumed that no values have changed for that
  411. * ATTRibute. If no "changed_ATTR" variable is POSTed, we assume that
  412. * ATTR has in fact changed.
  413. */
  414. i = 0;
  415. while (( varname = dsgw_next_cgi_var( &i, &varvalue )) != NULL ) {
  416. if ( varvalue != NULL && *varvalue == '\0' ) {
  417. varvalue = NULL;
  418. } else {
  419. dsgw_remove_leading_and_trailing_spaces( &varvalue );
  420. }
  421. opoffset = -1;
  422. if ( starts_with( varname, "add_" )) {
  423. modop = LDAP_MOD_ADD;
  424. opoffset = 4;
  425. attr = varname + opoffset;
  426. if (!isNtUser && (strcasecmp(DSGW_OC_NTUSER, attr) == 0)) {
  427. isNtUser = 1;
  428. }
  429. } else if ( starts_with( varname, "replace_" )) {
  430. modop = LDAP_MOD_REPLACE;
  431. opoffset = 8;
  432. attr = varname + opoffset;
  433. } else if ( starts_with( varname, "delete_" )) {
  434. modop = LDAP_MOD_DELETE;
  435. opoffset = 7;
  436. } else if ( !strcmp( varname, "changed_DN" )) {
  437. /* ignore it */
  438. } else if ( starts_with( varname, "changed_" )) {
  439. attr = varname + 8;
  440. if ( verbose && strcasecmp( varvalue, "true" ) == 0 ) {
  441. dsgw_emitf( XP_GetClientStr(DBT_attributeSWasChangedBrN_), attr );
  442. }
  443. if ( varvalue != NULL && strcasecmp( varvalue, "true" ) != 0 ) {
  444. unchanged_attrs = (char **)dsgw_ch_realloc( unchanged_attrs,
  445. ( 2 + unchanged_count ) * sizeof( char * ));
  446. unchanged_attrs[ unchanged_count++ ] = dsgw_ch_strdup( attr );
  447. unchanged_attrs[ unchanged_count ] = NULL;
  448. if ( pmods != NULL ) {
  449. remove_modifyops( pmods, attr );
  450. }
  451. }
  452. }
  453. if ( opoffset >= 0 ) {
  454. attr = varname + opoffset;
  455. mls = 0;
  456. unique = 0;
  457. while (( p = strchr( attr, '_' )) != NULL ) {
  458. if ( starts_with( attr, "mls_" )) {
  459. mls = 1;
  460. } else if ( starts_with( attr, "unique_" )) {
  461. unique = 1;
  462. } /* ignore any other prefixes */
  463. attr = p + 1;
  464. }
  465. for ( j = 0; j < unchanged_count; ++j ) {
  466. if ( strcasecmp( unchanged_attrs[ j ], attr ) == 0 ) {
  467. break;
  468. }
  469. }
  470. if ( j >= unchanged_count ) {
  471. if ( varvalue == NULL || *varvalue == '\0' ) {
  472. vals = NULL;
  473. varvalue = NULL;
  474. } else {
  475. varvalue = dsgw_ch_strdup( varvalue );
  476. if ( mls ) {
  477. vals = post2multilinevals( varvalue );
  478. } else {
  479. vals = post2vals( varvalue );
  480. }
  481. }
  482. if ( vals == NULL ) {
  483. if ( modop != LDAP_MOD_ADD ) {
  484. addmodifyop( &pmods, modop, attr, NULL, 0 );
  485. }
  486. } else {
  487. for ( j = 0; vals[ j ] != NULL; ++j ) {
  488. if ( unique && modop != LDAP_MOD_DELETE && ( lderr =
  489. value_is_unique( ld, dn, attr, vals[ j ] )) !=
  490. LDAP_SUCCESS ) {
  491. return( lderr );
  492. }
  493. if( isNtUser && (strcasecmp( DSGW_ATTRTYPE_NTUSERDOMAINID, attr) == 0)) {
  494. if( !ntuserid ) {
  495. ntuserid = strdup( vals[ j ] );
  496. }
  497. }
  498. addmodifyop( &pmods, modop, attr, vals[ j ],
  499. strlen( vals[ j ] ));
  500. }
  501. free( vals );
  502. }
  503. if ( varvalue != NULL ) {
  504. free( varvalue );
  505. }
  506. }
  507. }
  508. free( varname );
  509. }
  510. /* if the admin is adding an NT person, there must be an ntuserid */
  511. if( (isNtUser) && (ntuserid == NULL) ) {
  512. dsgw_error( DSGW_ERR_USERID_REQUIRED, NULL, 0, 0, NULL );
  513. return(LDAP_PARAM_ERROR);
  514. }
  515. /* if an ntuserid is being added, it must be the correct length */
  516. if( (isNtUser) && ntuserid && (strlen( ntuserid ) > MAX_NTUSERID_LEN)) {
  517. dsgw_error( DSGW_ERR_USERID_MAXLEN_EXCEEDED, NULL, 0, 0, NULL );
  518. return(LDAP_PARAM_ERROR);
  519. }
  520. if ( verbose && pmods != NULL ) {
  521. int j, notascii;
  522. unsigned long k;
  523. struct berval *bvp;
  524. for ( i = 0; pmods[ i ] != NULL; ++i ) {
  525. modop = pmods[ i ]->mod_op & ~LDAP_MOD_BVALUES;
  526. dsgw_emitf( "%s %s:\n", modop == LDAP_MOD_REPLACE ?
  527. "replace" : modop == LDAP_MOD_ADD ?
  528. "add" : "delete", pmods[ i ]->mod_type );
  529. if ( pmods[ i ]->mod_bvalues != NULL ) {
  530. for ( j = 0; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
  531. bvp = pmods[ i ]->mod_bvalues[ j ];
  532. notascii = 0;
  533. for ( k = 0; k < bvp->bv_len; ++k ) {
  534. if ( !isascii( bvp->bv_val[ k ] )) {
  535. notascii = 1;
  536. break;
  537. }
  538. }
  539. if ( notascii ) {
  540. dsgw_emitf( XP_GetClientStr(DBT_TnotAsciiLdBytesN_), bvp->bv_len );
  541. } else {
  542. dsgw_emitf( "\t\"%s\"\n", bvp->bv_val );
  543. }
  544. }
  545. }
  546. }
  547. }
  548. if ( verbose ) {
  549. dsgw_emitf( "</PRE>\n" );
  550. fflush( stdout );
  551. }
  552. dsgw_emitf( "<FONT SIZE=+1>\n" );
  553. /*
  554. * apply the changes using LDAP
  555. */
  556. if ( pmods == NULL ) {
  557. if ( add ) {
  558. dsgw_emits( XP_GetClientStr(DBT_noValuesWereEnteredPleaseTryAgai_) );
  559. lderr = LDAP_PARAM_ERROR;
  560. } else { /* no changes -- just report success */
  561. lderr = LDAP_SUCCESS;
  562. if ( !quiet ) {
  563. dsgw_emitf( XP_GetClientStr(DBT_PSuccessfullyEditedEntryYourChan_) );
  564. }
  565. }
  566. } else {
  567. if ( !quiet ) {
  568. dsgw_emitf( XP_GetClientStr(DBT_PSendingSToTheDirectoryServerN_),
  569. add ? XP_GetClientStr(DBT_information_) : XP_GetClientStr(DBT_changes_));
  570. fflush( stdout );
  571. }
  572. if ( add ) {
  573. lderr = ldap_add_ext( ld, dn, pmods, NULL, NULL, &msgid );
  574. } else {
  575. lderr = ldap_modify_ext( ld, dn, pmods, NULL, NULL, &msgid );
  576. }
  577. if( lderr == LDAP_SUCCESS ) {
  578. if(( lderr = ldap_result( ld, msgid, 1, (struct timeval *)NULL, &res )) == -1 ) {
  579. lderr = ldap_get_lderrno( ld, NULL, &errmsg );
  580. modify_error( lderr, errmsg );
  581. } else {
  582. lderr = ldap_result2error( ld, res, 1 );
  583. if ( lderr == LDAP_SUCCESS ) {
  584. if ( !quiet ) {
  585. if ( add ) {
  586. dsgw_emitf( XP_GetClientStr(DBT_PSuccessfullyAddedEntryN_) );
  587. } else {
  588. dsgw_emitf( XP_GetClientStr(DBT_PSuccessfullyEditedEntryYourChan_) );
  589. }
  590. }
  591. } else {
  592. (void)ldap_get_lderrno( ld, NULL, &errmsg );
  593. modify_error( lderr, errmsg );
  594. /* Do some checks for password policy infractions. */
  595. if( lderr == LDAP_CONSTRAINT_VIOLATION ) {
  596. if( errmsg && strstr( errmsg, "invalid password syntax" ) )
  597. dsgw_emitf( "<BR>(%s)", XP_GetClientStr(DBT_InvalidPasswordSyntax_) );
  598. else if( errmsg && strstr( errmsg, "password in history" ) )
  599. dsgw_emitf( "<BR>(%s)", XP_GetClientStr(DBT_PasswordInHistory_) );
  600. }
  601. }
  602. }
  603. } else {
  604. (void)ldap_get_lderrno( ld, NULL, &errmsg );
  605. modify_error( lderr, errmsg );
  606. }
  607. ldap_mods_free( pmods, 1 );
  608. }
  609. dsgw_emitf( "</FONT>\n" );
  610. return( lderr );
  611. }
  612. static int
  613. entry_delete( LDAP *ld, char *dn )
  614. {
  615. int lderr;
  616. char *errmsg = NULL;
  617. dsgw_emitf( "<FONT SIZE=+1>\n" );
  618. if (( lderr = ldap_delete_s( ld, dn )) == LDAP_SUCCESS ) {
  619. if ( !quiet ) {
  620. dsgw_emitf( XP_GetClientStr(DBT_PSuccessfullyDeletedEntryN_) );
  621. }
  622. } else {
  623. (void)ldap_get_lderrno( ld, NULL, &errmsg );
  624. modify_error( lderr, errmsg );
  625. }
  626. dsgw_emitf( "</FONT>\n" );
  627. return( lderr );
  628. }
  629. static int
  630. entry_modrdn( LDAP *ld, char *dn, char *newrdn, int deleteoldrdn )
  631. {
  632. int lderr;
  633. char *errmsg = NULL;
  634. if ( verbose ) {
  635. dsgw_emitf( XP_GetClientStr(DBT_PreTheNewNameForTheEntryIsSNPreH_),
  636. newrdn );
  637. }
  638. dsgw_emitf( "<FONT SIZE=+1>\n" );
  639. if (( lderr = ldap_modrdn2_s( ld, dn, newrdn, deleteoldrdn ))
  640. == LDAP_SUCCESS ) {
  641. if ( !quiet ) {
  642. dsgw_emitf( XP_GetClientStr(DBT_PSuccessfullyRenamedEntryN_) );
  643. }
  644. } else {
  645. (void)ldap_get_lderrno( ld, NULL, &errmsg );
  646. modify_error( lderr, errmsg );
  647. }
  648. dsgw_emitf( "</FONT>\n" );
  649. return( lderr );
  650. }
  651. static int
  652. gather_passwd_changes( char *dn, LDAPMod ***pmodsp, int adding_entry,
  653. int *pwdchangedp )
  654. {
  655. int lderr, lockpasswd;
  656. char *bindpasswd, *newpasswd, *newpasswdconfirm, *errstring;
  657. lockpasswd = dsgw_get_boolean_var( "lockpasswd", 0, 0 );
  658. if ( lockpasswd ) {
  659. /*
  660. * the userPassword attribute to a special value that no password
  661. * submitted by a user can ever match.
  662. */
  663. time_t curtime;
  664. struct tm *gmtp;
  665. char *tstr;
  666. /* get string representation of current GMT time */
  667. curtime = time( NULL );
  668. gmtp = gmtime( &curtime );
  669. tstr = asctime( gmtp );
  670. /* remove trailing newline */
  671. tstr[ strlen( tstr ) - 1 ] = '\0';
  672. /* allocate room for "{crypt}LOCKED [" + tstr + " GMT]" + zero byte */
  673. newpasswd = dsgw_ch_malloc( 15 + strlen( tstr ) + 5 + 1 );
  674. sprintf( newpasswd, XP_GetClientStr(DBT_CryptLockedSGmt_), tstr );
  675. } else if (( newpasswd = dsgw_get_cgi_var( "newpasswd",
  676. DSGW_CGIVAR_OPTIONAL )) == NULL ) {
  677. return( LDAP_SUCCESS ); /* not setting password -- nothing to do */
  678. }
  679. lderr = LDAP_PARAM_ERROR; /* pessimistic */
  680. if ( !adding_entry && ( bindpasswd = dsgw_get_cgi_var( "passwd",
  681. DSGW_CGIVAR_OPTIONAL )) == NULL && require_oldpasswd( dn )) {
  682. errstring = XP_GetClientStr(DBT_youMustProvideTheOldPassword_);
  683. } else if ( !lockpasswd &&
  684. (( newpasswdconfirm = dsgw_get_cgi_var( "newpasswdconfirm",
  685. DSGW_CGIVAR_OPTIONAL )) == NULL || strcmp( newpasswd,
  686. newpasswdconfirm ) != 0 )) {
  687. errstring = XP_GetClientStr(DBT_theNewAndConfirmingPasswordsDoNo_);
  688. } else {
  689. addmodifyop( pmodsp, adding_entry ? LDAP_MOD_ADD : LDAP_MOD_REPLACE,
  690. DSGW_ATTRTYPE_USERPASSWORD, newpasswd, strlen( newpasswd ));
  691. *pwdchangedp = 1;
  692. lderr = LDAP_SUCCESS;
  693. }
  694. if ( lderr != LDAP_SUCCESS ) {
  695. dsgw_emitf( "<FONT SIZE=+1>\n%s\n</FONT>\n", errstring );
  696. }
  697. return( lderr );
  698. }
  699. static void
  700. modify_error( int lderr, char *lderrtxt )
  701. {
  702. dsgw_error( DSGW_ERR_LDAPGENERAL, dsgw_ldaperr2string( lderr ),
  703. ( display_results_inline ? DSGW_ERROPT_INLINE : 0 ),
  704. lderr, lderrtxt );
  705. }
  706. /*
  707. * this "addmodifyop" routine is lifted with minor changes from
  708. * ldap/tools/ldapmodify.c
  709. */
  710. static void
  711. addmodifyop( LDAPMod ***pmodsp, int modop, char *attr, char *value, int vlen )
  712. {
  713. LDAPMod **pmods;
  714. int i, j;
  715. struct berval *bvp;
  716. if ( attr == NULL || *attr == '\0' ) {
  717. return;
  718. }
  719. pmods = *pmodsp;
  720. modop |= LDAP_MOD_BVALUES;
  721. i = 0;
  722. if ( pmods != NULL ) {
  723. for ( ; pmods[ i ] != NULL && pmods[ i ]->mod_type != NULL; ++i ) {
  724. if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 &&
  725. pmods[ i ]->mod_op == modop ) {
  726. break;
  727. }
  728. }
  729. }
  730. if ( pmods == NULL || pmods[ i ] == NULL ) {
  731. pmods = (LDAPMod **)dsgw_ch_realloc( pmods, (i + 2) *
  732. sizeof( LDAPMod * ));
  733. *pmodsp = pmods;
  734. pmods[ i + 1 ] = NULL;
  735. pmods[ i ] = (LDAPMod *)dsgw_ch_malloc( sizeof( LDAPMod ));
  736. memset( pmods[ i ], 0, sizeof( LDAPMod ));
  737. pmods[ i ]->mod_op = modop;
  738. pmods[ i ]->mod_type = dsgw_ch_strdup( attr );
  739. }
  740. if ( value != NULL ) {
  741. j = 0;
  742. if ( pmods[ i ]->mod_bvalues != NULL ) {
  743. for ( ; pmods[ i ]->mod_bvalues[ j ] != NULL; ++j ) {
  744. ;
  745. }
  746. }
  747. pmods[ i ]->mod_bvalues =
  748. (struct berval **)dsgw_ch_realloc( pmods[ i ]->mod_bvalues,
  749. (j + 2) * sizeof( struct berval * ));
  750. pmods[ i ]->mod_bvalues[ j + 1 ] = NULL;
  751. bvp = (struct berval *)dsgw_ch_malloc( sizeof( struct berval ));
  752. pmods[ i ]->mod_bvalues[ j ] = bvp;
  753. bvp->bv_len = vlen;
  754. bvp->bv_val = (char *)dsgw_ch_malloc( vlen + 1 );
  755. memcpy( bvp->bv_val, value, vlen );
  756. bvp->bv_val[ vlen ] = '\0';
  757. }
  758. }
  759. /* remove all modify ops that refer to "attr" */
  760. static void
  761. remove_modifyops( LDAPMod **pmods, char *attr )
  762. {
  763. int i, found_attr;
  764. if ( pmods == NULL ) {
  765. return;
  766. }
  767. do {
  768. found_attr = 0;
  769. for ( i = 0 ; pmods[ i ] != NULL; ++i ) {
  770. if ( strcasecmp( pmods[ i ]->mod_type, attr ) == 0 ) {
  771. found_attr = 1;
  772. break;
  773. }
  774. }
  775. if ( found_attr ) {
  776. if ( pmods[ i ]->mod_bvalues != NULL ) {
  777. ber_bvecfree( pmods[ i ]->mod_bvalues );
  778. }
  779. free( pmods[ i ] );
  780. for ( ; pmods[ i + 1 ] != NULL; ++i ) {
  781. pmods[ i ] = pmods[ i + 1 ];
  782. }
  783. pmods[ i ] = NULL;
  784. }
  785. } while ( found_attr );
  786. }
  787. static int
  788. starts_with( char *s, char *startswith )
  789. {
  790. int len;
  791. len = strlen( startswith );
  792. return ( strlen( s ) > len && strncmp( s, startswith, len ) == 0 );
  793. }
  794. /*
  795. * there is one value in "postedval" but newlines must be changed to "$",
  796. * '$' characters must be changed to \24, and '\' chars. changed to \5C
  797. */
  798. static char **
  799. post2multilinevals( char *postedval )
  800. {
  801. int specials;
  802. char *p, *r, **vals;
  803. vals = dsgw_ch_malloc( 2 * sizeof( char * ));
  804. vals[ 1 ] = NULL;
  805. specials = 0;
  806. for ( p = postedval; *p != '\0'; ++p ) {
  807. if ( *p == '$' || *p == '\\' || *p == '\n' || *p == '\r') {
  808. ++specials;
  809. }
  810. }
  811. /* allocate enough room to handle any necessary escaping */
  812. r = vals[ 0 ] = dsgw_ch_malloc( 2 * specials + strlen( postedval ) + 1 );
  813. /* copy and escape as appropriate */
  814. for ( p = postedval; *p != '\0'; ++p ) {
  815. if ( *p == '\n' || *p == '\r' ) { /* change to "$" */
  816. *r++ = '$';
  817. if ( *(p+1) != '\0' && *(p+1) != *p &&
  818. ( *(p+1) == '\n' || *(p+1) == '\r' )) {
  819. ++p; /* skip next char. if sequence is "\r\n" or "\n\r" */
  820. }
  821. } else if ( *p == '$' ) { /* change to "\24" */
  822. *r++ = '\\';
  823. *r++ = '2';
  824. *r++ = '4';
  825. } else {
  826. *r++ = *p;
  827. if ( *p == '\\' ) { /* change to "\5C" */
  828. *r++ = '5';
  829. *r++ = 'C';
  830. }
  831. }
  832. }
  833. *r = '\0';
  834. return( vals );
  835. }
  836. /* values are delimited by newlines, preceded by optional carriage returns */
  837. static char **
  838. post2vals( char *postedval )
  839. {
  840. int count, len;
  841. char *p, *q, **vals;
  842. vals = NULL;
  843. count = 0;
  844. for ( p = postedval; p != NULL && *p != '\0'; p = q ) {
  845. /* skip any leading CRs or NLs */
  846. while (( *p == '\n' || *p == '\r' ) && *p != '\0' ) {
  847. ++p;
  848. }
  849. if ( *p == '\0' ) {
  850. break;
  851. }
  852. /* find end of this line */
  853. if (( q = strchr( p, '\n' )) != NULL ) {
  854. *q++ = '\0';
  855. }
  856. /* remove CR, if any */
  857. len = strlen( p ) - 1;
  858. if ( p[ len ] == '\r' ) {
  859. p[ len ] = '\0';
  860. }
  861. /* add to values array */
  862. vals = dsgw_ch_realloc( vals, ( count + 2 ) * sizeof( char * ));
  863. vals[ count++ ] = p;
  864. }
  865. vals[ count ] = NULL;
  866. return( vals );
  867. }
  868. /*
  869. * Determine if we should insist that the old password for the entry
  870. * we are modifying (modifydn) be POSTed. The rule we use is simply
  871. * this: if the binddn and modifydn are the same, require the old
  872. * password. This allows directory admins. to reset passwords while
  873. * preventing normal users from having their password changed if they
  874. * just happen to walk away from their computer for a while when they
  875. * are authenticated to the gateway.
  876. */
  877. static int
  878. require_oldpasswd( char *modifydn )
  879. {
  880. return( dsgw_bound_as_dn( modifydn, 1 ));
  881. }
  882. /*
  883. * search directory to find out if an attribute value is unique. If the
  884. * value doesn't already exist or if it exists only in the same entry we
  885. * are changing, we return LDAP_SUCCESS. If it does exist, we return
  886. * LDAP_TYPE_OR_VALUE_EXISTS. If some other error occurs, we return another
  887. * LDAP error code.
  888. */
  889. static int
  890. value_is_unique( LDAP *ld, char *dn, char *attr, char *value )
  891. {
  892. int rc, count;
  893. char *attrs[2], *buf, *tmpdn, *attrdesc, *errmsg = NULL;
  894. LDAPMessage *res, *e;
  895. /* allocate room for "(attr=value)" filter */
  896. buf = dsgw_ch_malloc( strlen( attr ) + strlen( value ) + 4 );
  897. sprintf( buf, "(%s=%s)", attr, value );
  898. attrs[ 0 ] = attr;
  899. attrs[ 1 ] = NULL;
  900. rc = ldap_search_s( ld, gc->gc_ldapsearchbase, LDAP_SCOPE_SUBTREE,
  901. buf, attrs, 1, &res );
  902. free( buf );
  903. if ( rc != LDAP_SUCCESS || res == NULL ) {
  904. (void)ldap_get_lderrno( ld, NULL, &errmsg );
  905. modify_error( rc, errmsg );
  906. return( rc );
  907. }
  908. if (( count = ldap_count_entries( ld, res )) == 0 ) {
  909. rc = LDAP_SUCCESS;
  910. } else if ( count > 1 ) {
  911. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  912. } else { /* found one entry: see if it is the entry we are modifying */
  913. if (( e = ldap_first_entry( ld, res )) == NULL ||
  914. ( tmpdn = ldap_get_dn( ld, e )) == NULL ) {
  915. rc = ldap_get_lderrno( ld, NULL, NULL );
  916. } else if ( dsgw_dn_cmp( dn, tmpdn ) != 0 ) {
  917. rc = LDAP_SUCCESS; /* same entry */
  918. } else {
  919. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  920. }
  921. }
  922. ldap_msgfree( res );
  923. if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
  924. buf = dsgw_ch_malloc( strlen( attr ) + 6 ); /* room for "desc_" */
  925. sprintf( buf, "desc_%s", attr );
  926. if (( attrdesc = dsgw_get_cgi_var( buf, DSGW_CGIVAR_OPTIONAL ))
  927. == NULL ) {
  928. attrdesc = attr;
  929. }
  930. free( buf );
  931. dsgw_emits( "\n<FONT SIZE=+1>\n" );
  932. dsgw_emitf( XP_GetClientStr(DBT_BrTheSBSBIsAlreadyInUsePleaseCho_), attrdesc, value );
  933. dsgw_emits( "\n</FONT>\n" );
  934. }
  935. return( rc );
  936. }