File jabberd-verify.patch of Package jabberd
--- sm/mod_verify.c.orig
+++ sm/mod_verify.c
@@ -34,8 +34,8 @@ typedef struct _verify_st {
char *code;
} verify_t;
-static void print_instructions(pkt_t res);
-
+static void print_instructions(nad_t nad);
+static void print_error(nad_t nad);
static void send_email(verify_t *v, user_t user, pkt_t res, char *message)
{
@@ -47,12 +47,13 @@ static void send_email(verify_t *v, user
os_object_t o;
message = strdup(message);
- result = regcomp(&preg, "[a-z0-9._+-]+@[a-z0-9.-]+", REG_EXTENDED|REG_ICASE);
+ result = regcomp(&preg, "[a-z0-9._+-]+@[a-z0-9.-]+",
+ REG_EXTENDED|REG_ICASE);
result |= regexec(&preg, message, 1, match, 0);
regfree(&preg);
if (result != 0 || match[0].rm_so == -1) {
- print_instructions(res);
+ print_instructions(res->nad);
goto free;
}
@@ -66,22 +67,31 @@ static void send_email(verify_t *v, user
if (v->code != NULL)
free(v->code);
v->code = calloc(1,11);
- if ((pipe = popen("pwgen 10 1", "r")) == NULL) {
- log_write(user->sm->log, LOG_ERR, "Error generating email code for %s using 'pwgen'. %d:%s", v->email, errno, strerror(errno));
+ if ((pipe = popen("/usr/bin/pwgen 10 1", "r")) == NULL) {
+ log_write(user->sm->log, LOG_ERR,
+ "Error generating email code for %s using 'pwgen'. %d:%s",
+ v->email, errno, strerror(errno));
goto error;
}
if (fgets(v->code, 11, pipe) == NULL) {
- log_write(user->sm->log, LOG_ERR, "Error getting email code for %s from 'pwgen'. %d:%s", v->email, errno, strerror(errno));
+ log_write(user->sm->log, LOG_ERR,
+ "Error getting email code for %s from 'pwgen'. %d:%s",
+ v->email, errno, strerror(errno));
pclose(pipe);
goto error;
}
if (pclose(pipe) == -1) {
- log_write(user->sm->log, LOG_ERR, "Error closing email code for %s from 'pwgen'. %d:%s", v->email, errno, strerror(errno));
+ log_write(user->sm->log, LOG_ERR,
+ "Error closing email code for %s from 'pwgen'. %d:%s",
+ v->email, errno, strerror(errno));
goto error;
}
log_debug(ZONE, "code: >%s<", v->code);
- if ((pipe = popen("sendmail -t -F 'Jabber Server'", "w")) == NULL) {
- log_write(user->sm->log, LOG_ERR, "Error starting sendmail to %s. %d:%s", v->email, errno, strerror(errno));
+ pipe = popen("/usr/lib/sendmail -t -F 'Jabber Server'", "w");
+ if (pipe == NULL) {
+ log_write(user->sm->log, LOG_ERR,
+ "Error starting sendmail to %s. %d:%s",
+ v->email, errno, strerror(errno));
goto error;
}
@@ -90,32 +100,39 @@ static void send_email(verify_t *v, user
os_object_put(o, "email", v->email, os_type_STRING);
os_object_put(o, "code", v->code, os_type_STRING);
os_object_put(o, "state", &v->state, os_type_INTEGER);
- if (storage_replace(user->sm->st, "verify", jid_user(user->jid), NULL, os) != st_SUCCESS) {
- log_write(user->sm->log, LOG_ERR, "Error writing email code to DB for %s", v->email);
+ result = storage_replace(user->sm->st, "verify", jid_user(user->jid),
+ NULL, os);
+ os_free(os);
+ if (result != st_SUCCESS) {
+ log_write(user->sm->log, LOG_ERR,
+ "Error writing email code to DB for %s", v->email);
free(v->email);
free(v->code);
v->email=NULL;
v->code=NULL;
+ goto error;
}
- os_free(os);
- if (fprintf(pipe,
- "To: %s\n"
- "Subject: Jabberd email verification\n"
- "\n"
- "Please reply the following line to the jabber server to confirm your email address.\n\n"
- "code: %s\n"
- ".\n", v->email, v->code) < 0) {
- log_write(user->sm->log, LOG_ERR, "Error writing sendmail to %s. %d:%s", v->email, errno, strerror(errno));
- pclose(pipe);
- goto error;
+ result = fprintf(pipe,
+ "To: %s\n"
+ "Subject: Jabberd email verification\n"
+ "\n"
+ "To activate your account, please type or paste the "
+ "following line into the chat window that asked for "
+ "your email address.\n\n"
+ "code: %s\n"
+ ".\n", v->email, v->code);
+ if (result < 0) {
+ log_write(user->sm->log, LOG_ERR,
+ "Error writing sendmail to %s. %d:%s",
+ v->email, errno, strerror(errno));
}
if (pclose(pipe) == -1) {
- log_write(user->sm->log, LOG_ERR, "Error closing sendmail to %s. %d:%s", v->email, errno, strerror(errno));
+ log_write(user->sm->log, LOG_ERR,
+ "Error closing sendmail to %s. %d:%s",
+ v->email, errno, strerror(errno));
goto error;
}
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1),
- "subject", "Verification email sent");
nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
"A verification email has been sent to the specified "
"address. Please check your inbox and follow the "
@@ -123,12 +140,8 @@ static void send_email(verify_t *v, user
goto free;
error:
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1),
- "subject", "Error");
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
- "An error occurred while trying to send the verification email to you.\n"
- "Please try again later. If the problem persists, please contact the\n"
- "server admin.");
+ print_error(res->nad);
+
free:
free(message);
return;
@@ -138,9 +151,10 @@ static void check_code(verify_t *v, user
{
os_t os;
os_object_t o;
+ int result;
if (v->code == NULL) {
- print_instructions(res);
+ print_instructions(res->nad);
return;
}
if (strstr(message, v->code) != NULL) {
@@ -152,44 +166,56 @@ static void check_code(verify_t *v, user
os_object_put(o, "email", v->email, os_type_STRING);
os_object_put(o, "code", v->code, os_type_STRING);
os_object_put(o, "state", &v->state, os_type_INTEGER);
- if (storage_replace(user->sm->st, "verify", jid_user(user->jid), NULL, os) != st_SUCCESS) {
- log_write(user->sm->log, LOG_ERR, "Error writing verification state to DB for %s", v->email);
- }
+ result = storage_replace(user->sm->st, "verify", jid_user(user->jid),
+ NULL, os);
os_free(os);
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1),
- "subject", "Code accepted");
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
- "Your verification code has been accepted.\n"
- "You are now a verified user.");
+ if (result == st_SUCCESS) {
+ nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
+ "Thanks, your account has been activated.\n"
+ "You can now close this conversation.");
+ } else {
+ log_write(user->sm->log, LOG_ERR,
+ "Error writing verification state to DB for %s",
+ v->email);
+ print_error(res->nad);
+ }
} else {
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1),
- "subject", "Code rejected");
nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
"Your verification code did not match.\n"
"Please try to re-submit it, or send another \n"
- "\"email: \" line to gat a new code sent to you.");
+ "\"email: \" line to get a new code sent to you.");
}
}
-static void print_instructions(pkt_t res)
+static void print_instructions(nad_t nad)
{
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1),
- "subject", "Please enter your email address");
- nad_insert_elem(res->nad, 1, NAD_ENS(res->nad, 1), "body",
- "You are blocked from this jabber server until "
- "you have entered and validated your email address! "
- "To do this, please type in \"email: \" followed by "
- "your email address as a reply to this message, e.g.\n\n"
+ nad_insert_elem(nad, 1, NAD_ENS(nad, 1), "body",
+ "Thanks for registering with this Jabber server.\n\n"
+ "To complete your registration and enable your account "
+ "we have to verify your email address. To do this, "
+ "please type \"email: \" followed by your email "
+ "address as a reply to this message, e.g.\n\n"
"email: johndoe@example.com\n\n"
"A verification code with further instructions will then "
"be sent to that email address.");
}
+static void print_error(nad_t nad)
+{
+ nad_insert_elem(nad, 1, NAD_ENS(nad, 1), "body",
+ "An error occured while trying to send the verification "
+ "email to you, please try again later.\n"
+ "If the problem persists, please contact "
+ "the server admin.");
+
+}
+
static mod_ret_t _verify_in_sess(mod_instance_t mi, sess_t sess, pkt_t pkt)
{
pkt_t res;
nad_t nad = pkt->nad;
int body, message;
+ jid_t smjid;
char *cdata= NULL;
verify_t *v = sess->user->module_data[mi->mod->index];
@@ -211,14 +237,18 @@ static mod_ret_t _verify_in_sess(mod_ins
strncpy(cdata, NAD_CDATA(nad, body), len);
cdata[len] = '\0';
log_debug(ZONE, "---> %s <---", cdata);
- res = pkt_create(mi->mod->mm->sm, "message", NULL, jid_full(sess->jid),
- mi->mod->mm->sm->id);
+ smjid = jid_new(NULL, -1);
+ jid_reset_components(smjid, mi->mod->mm->sm->id,
+ sess->user->jid->domain, "");
+ res = pkt_create(mi->mod->mm->sm, "message", "chat",
+ jid_full(sess->jid), jid_user(smjid));
+ jid_free(smjid);
if (strstr(cdata, "email: ") == cdata) {
send_email(v, sess->user, res, cdata);
} else if (strstr(cdata, "code: ") == cdata) {
check_code(v, sess->user, res, cdata);
} else {
- print_instructions(res);
+ print_instructions(res->nad);
}
pkt_router(res);
free(cdata);
@@ -251,11 +281,15 @@ static int _verify_user_load(mod_instanc
os_t os;
os_object_t o;
int state;
+ int result;
log_debug(ZONE, "_verify_user_load: >%s<", jid_user(user->jid));
v = calloc(1, sizeof(struct _verify_st));
user->module_data[mi->mod->index] = v;
- if (storage_get(user->sm->st, "verify", jid_user(user->jid), NULL, &os) == st_SUCCESS) {
+
+ result = storage_get(user->sm->st, "verify", jid_user(user->jid),
+ NULL, &os);
+ if (result == st_SUCCESS) {
if (os_iter_first(os)) {
o = os_iter_object(os);
if (os_object_get_str(os, o, "email", &v->email) &&