domodify.c 36 KB

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