+ free(passbuf);
+ return rc;
+}
+
+int ldap_delete_account(char *account)
+{
+ char dn[MAXLEN];
+ int rc;
+
+ if(!admin_bind && LDAP_SUCCESS != ( rc = ldap_do_admin_bind())) {
+ log_module(MAIN_LOG, LOG_ERROR, "failed to bind as admin");
+ return rc;
+ }
+
+ memset(dn, 0, MAXLEN);
+ snprintf(dn, MAXLEN-1, nickserv_conf.ldap_dn_fmt, account);
+ return(ldap_delete_s(ld, dn));
+}
+
+int ldap_rename_account(char *oldaccount, char *newaccount)
+{
+ char dn[MAXLEN], newdn[MAXLEN];
+ int rc;
+
+ if(!admin_bind && LDAP_SUCCESS != ( rc = ldap_do_admin_bind())) {
+ log_module(MAIN_LOG, LOG_ERROR, "failed to bind as admin");
+ return rc;
+ }
+
+ memset(dn, 0, MAXLEN);
+ memset(newdn, 0, MAXLEN);
+ snprintf(dn, MAXLEN-1, nickserv_conf.ldap_dn_fmt, oldaccount);
+ strcat(newdn, nickserv_conf.ldap_field_account);
+ strcat(newdn, "=");
+ strcat(newdn, newaccount);
+ rc = ldap_modrdn2_s(ld, dn, newdn, true);
+ if(rc != LDAP_SUCCESS) {
+ log_module(MAIN_LOG, LOG_ERROR, "Error modifying ldap account: %s -- %s", oldaccount, ldap_err2string(rc));
+ //return rc;
+ }
+ return rc;
+
+}
+
+LDAPMod **make_mods_modify(const char *password, const char *email, int *num_mods_ret)
+{
+ static char *password_vals[] = { NULL, NULL };
+ static char *email_vals[] = { NULL, NULL };
+ int num_mods = 0;
+ int i;
+ /* TODO: take this from nickserv_conf.ldap_add_objects */
+ LDAPMod **mods;
+
+ password_vals[0] = (char *) password;
+ email_vals[0] = (char *) email;
+
+ if(!(nickserv_conf.ldap_field_password && *nickserv_conf.ldap_field_password))
+ return 0; /* password required */
+ /*
+ if(email && *email && nickserv_conf.ldap_field_email && *nickserv_conf.ldap_field_email)
+ num_mods++;
+ */
+ if(password)
+ num_mods++;
+ if(email)
+ num_mods++;
+
+ mods = ( LDAPMod ** ) malloc(( num_mods + 1 ) * sizeof( LDAPMod * ));
+ for( i = 0; i < num_mods; i++) {
+ mods[i] = (LDAPMod *) malloc(sizeof(LDAPMod));
+ memset(mods[i], 0, sizeof(LDAPMod));
+ }
+
+ i = 0;
+ if(nickserv_conf.ldap_field_password && *nickserv_conf.ldap_field_password &&
+ password) {
+ mods[i]->mod_op = LDAP_MOD_REPLACE;
+ mods[i]->mod_type = strdup(nickserv_conf.ldap_field_password);
+ mods[i]->mod_values = password_vals;
+ i++;
+ }
+
+ if(nickserv_conf.ldap_field_email && *nickserv_conf.ldap_field_email && email) {
+ mods[i]->mod_op = LDAP_MOD_REPLACE;
+ mods[i]->mod_type = strdup(nickserv_conf.ldap_field_email);
+ mods[i]->mod_values = email_vals;
+ i++;
+ }
+ mods[i] = NULL;
+ *num_mods_ret = num_mods;
+ return mods;
+}
+
+
+/* Save email or password to server
+ *
+ * password - UNENCRYPTED password. This function encrypts if libs are available
+ * email - email address
+ *
+ * NULL to make no change
+ */
+int ldap_do_modify(const char *account, const char *password, const char *email)
+{
+ char dn[MAXLEN];
+ LDAPMod **mods;
+ int rc, i;
+ int num_mods;
+ char *passbuf = NULL;
+
+ if(!admin_bind && LDAP_SUCCESS != ( rc = ldap_do_admin_bind())) {
+ log_module(MAIN_LOG, LOG_ERROR, "failed to bind as admin");
+ return rc;
+ }
+
+ if(password) {
+ passbuf = make_password(password);
+ }
+
+ snprintf(dn, MAXLEN-1, nickserv_conf.ldap_dn_fmt, account);
+ mods = make_mods_modify(passbuf, email, &num_mods);
+ if(!mods) {
+ log_module(MAIN_LOG, LOG_ERROR, "Error building mods for ldap_do_modify");
+ return LDAP_OTHER;
+ }
+ rc = ldap_modify_s(ld, dn, mods);
+ if(rc != LDAP_SUCCESS) {
+ log_module(MAIN_LOG, LOG_ERROR, "Error modifying ldap account: %s -- %s", account, ldap_err2string(rc));
+ // return rc;
+ }
+ for(i = 0; i < num_mods; i++) {
+ free(mods[i]->mod_type);
+ free(mods[i]);
+ }
+ free(mods);
+ if(passbuf)
+ free(passbuf);
+ return rc;