-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathacct_maint.c
2364 lines (1988 loc) · 72 KB
/
acct_maint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* A module for simple account maintenance. */
#include <time.h>
#include <ctype.h>
#include <apr.h>
#include <apr_hash.h>
#include <apr_strings.h>
#include <httpd.h>
#include <http_log.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include "private.h"
#include "buffer.h"
#include "db.h"
#include "req.h"
#include "apache_util.h"
#include "util.h"
#include "db_xml.h"
#include "style.h"
#include "auth.h"
#include "xml_util.h"
#include "certs.h"
#include "aggregator.h"
#include "db_ops.h"
#include "proj.h"
#include "rating.h"
#include "hashtable.h"
#include "eigen.h"
#include "diary.h"
#include "site.h"
#include "foaf.h"
#include "acct_maint.h"
typedef struct _ProfileField ProfileField;
struct _ProfileField {
char *description;
char *attr_name;
int size;
int flags;
};
typedef enum {
PROFILE_PUBLIC = 1 << 0,
PROFILE_TEXTAREA = 1 << 1,
PROFILE_BOOLEAN = 1 << 2,
PROFILE_SYNDICATE = 1 << 3
} ProfileFlags;
ProfileField prof_fields[] = {
{ "Given (first) name", "givenname", 40, PROFILE_PUBLIC },
{ "Surname (last name)", "surname", 40, PROFILE_PUBLIC },
{ "Surname first?", "snf", 40, PROFILE_PUBLIC | PROFILE_BOOLEAN },
{ "Email", "email", 40, 0 },
{ "Homepage URL", "url", 40, PROFILE_PUBLIC },
{ "External FOAF URI", "foafuri", 40, 0 },
{ "Number of old messages to display", "numold", 4, 0 },
{ "Notes", "notes", 60015, PROFILE_PUBLIC | PROFILE_TEXTAREA },
{ "Syndicate your blog from another site?", "syndicate", 40, PROFILE_PUBLIC | PROFILE_BOOLEAN | PROFILE_SYNDICATE },
{ "RSS or ATOM feed URL of your blog", "feedurl", 60, PROFILE_PUBLIC | PROFILE_SYNDICATE },
{ NULL }
};
typedef struct _NodeInfo NodeInfo;
struct _NodeInfo {
const char *name;
const char *givenname;
const char *surname;
};
/**
* acct_kill: Remove a user account. Before an account is removed, all cert
* and cert-in records are cleared. An account or account-alias is accepted
* as an argument. Account profile, alias (if any), certs, staff records,
* diary entries, eigen data, and any entries in recent lists will be
* removed. Deletion is not reversible!
**/
static int
acct_kill(VirguleReq *vr, const char *u)
{
int n;
const char *user = NULL;
char *db_key, *db_key2, *user_alias, *diary;
apr_pool_t *p = vr->r->pool;
xmlDoc *profile, *staff, *entry, *agglist;
xmlNode *tree, *cert, *alias, *feed;
db_key = virgule_acct_dbkey(vr, u);
profile = virgule_db_xml_get(p, vr->db, db_key);
if (profile == NULL)
return FALSE;
alias = virgule_xml_find_child (profile->xmlRootNode, "alias");
if (alias != NULL) /* If this is the alias, get username and kill profile */
{
user = virgule_xml_get_prop (p, alias, (xmlChar *)"link");
virgule_db_xml_free (p, profile);
virgule_db_del (vr->db, db_key);
db_key = virgule_acct_dbkey (vr, user);
}
else /* If this is the username, check for lc alias */
{
user = u;
user_alias = apr_pstrdup (p, u);
ap_str_tolower (user_alias);
if (! (strcmp (user_alias,u) == 0))
{
db_key2 = virgule_acct_dbkey (vr, user_alias);
if (db_key2 != NULL)
virgule_db_del (vr->db, db_key2);
}
}
/* Clear cert records */
tree = virgule_xml_find_child (profile->xmlRootNode, "certs");
if (tree)
{
xmlChar *subject, *level;
for (cert = tree->children; cert != NULL; cert = cert->next)
if (cert->type == XML_ELEMENT_NODE && ! xmlStrcmp (cert->name, (xmlChar *)"cert"))
{
subject = xmlGetProp (cert, (xmlChar *)"subj");
level = xmlGetProp (cert, (xmlChar *)"level");
virgule_cert_set (vr, (char *)user, (char *)subject, CERT_LEVEL_NONE);
xmlFree(subject);
xmlFree(level);
}
}
/* Clear cert-in records */
tree = virgule_xml_find_child (profile->xmlRootNode, "certs-in");
if (tree)
{
xmlChar *issuer, *level;
for (cert = tree->children; cert != NULL; cert = cert->next)
if (cert->type == XML_ELEMENT_NODE && ! xmlStrcmp (cert->name, (xmlChar *)"cert"))
{
issuer = xmlGetProp (cert, (xmlChar *)"issuer");
level = xmlGetProp (cert, (xmlChar *)"level");
virgule_cert_set (vr, (char *)issuer, user, CERT_LEVEL_NONE);
xmlFree(issuer);
xmlFree(level);
}
}
/* Clear staff records */
db_key2 = apr_psprintf (p, "acct/%s/staff-person.xml", user);
staff = virgule_db_xml_get (p, vr->db, db_key2);
if (staff != NULL)
{
for (tree = staff->xmlRootNode->children; tree != NULL; tree = tree->next)
{
char *name;
name = virgule_xml_get_prop (p, tree, (xmlChar *)"name");
virgule_proj_set_relation(vr,name,user,"None");
}
virgule_db_xml_free (p, staff);
virgule_db_del (vr->db, db_key2);
}
/* Clear diary entries */
diary = apr_psprintf(p, "acct/%s/diary", user);
for (n = virgule_db_dir_max (vr->db, diary); n >= 0; n--)
{
db_key2 = apr_psprintf (p, "acct/%s/diary/_%d", user, n);
entry = virgule_db_xml_get (p, vr->db, db_key2);
if (entry != NULL)
{
virgule_db_del (vr->db, db_key2);
virgule_db_xml_free (p, entry);
}
}
/* <articlepointers>, <auth>, and <info> tags don't need attention */
/* Remove diary backup, if any */
diary = apr_psprintf (p, "acct/%s/diarybackup", user);
virgule_db_del (vr->db, diary);
/* Remove article index, if any */
db_key2 = apr_psprintf (p, "acct/%s/articles.xml", user);
virgule_db_del (vr->db, db_key2);
/* Remove user from recent lists (if present) */
virgule_remove_recent (vr, "recent/acct.xml", user);
virgule_remove_recent (vr, "recent/diary.xml", user);
/* Remove eigen data (if any) */
virgule_eigen_cleanup (vr, user);
/* Remove the profile and account */
virgule_db_del (vr->db, db_key);
virgule_db_xml_free(p, profile);
/* Remove blog feed buffer, if any */
db_key2 = apr_psprintf (p, "acct/%s/feed.xml", user);
virgule_db_del (vr->db, db_key2);
/* Remove from feedlist */
agglist = virgule_db_xml_get (vr->r->pool, vr->db, "feedlist");
if (agglist != NULL)
for (feed = agglist->xmlRootNode->children; feed != NULL; feed = feed->next)
{
if(!strcmp (user, virgule_xml_get_prop (vr->r->pool, feed, (xmlChar *)"user")))
{
xmlUnlinkNode (feed);
xmlFreeNode (feed);
virgule_db_xml_put (vr->r->pool, vr->db, "feedlist", agglist);
break;
}
}
return TRUE;
}
/**
* virgule_acct_flag_as_spam: Flag the passed user's account as spam. A tag
* will be added with the flagging user's name and a score of 1, 2, or 3
* depending on flagger's cert level. If a flag with the current users name
* already exist, no action will be taken (you can only flag an account as
* spam once. After adding the tag, the total spam score will be evaluated.
* If the spam score exceeds the configured threshold, the account is killed.
**/
static int
acct_flag_as_spam(VirguleReq *vr, const char *u)
{
int score = 0;
int flagged = FALSE;
char *db_key;
xmlDoc *profile;
xmlChar *scorestr;
xmlNode *spamtree;
xmlNode *flag;
xmlNode *spamscore;
virgule_auth_user (vr);
if(vr->u == NULL)
return virgule_send_error_page (vr, vERROR, "account", "You must be logged in to use the spam reporting system.\n");
if(u == NULL)
return virgule_send_error_page (vr, vERROR, "account", "The specified user account was not found.\n");
if (strcmp (virgule_req_get_tmetric_level (vr, u),
virgule_cert_level_to_name (vr, CERT_LEVEL_NONE)) != 0)
return virgule_send_error_page (vr, vERROR, "account", "Only untrusted observer accounts may be reported as spam.\n");
db_key = virgule_acct_dbkey (vr, u);
profile = virgule_db_xml_get (vr->r->pool, vr->db, db_key);
if (profile == NULL)
return virgule_send_error_page (vr, vERROR, "account", "The specified user account was not found.\n");
spamtree = virgule_xml_ensure_child (profile->xmlRootNode, "spamflags");
/* search for an existing flag from vr->u while tallying the spam score */
for (flag = spamtree->children; flag != NULL; flag = flag->next)
{
if (flag->type == XML_ELEMENT_NODE && !xmlStrcmp (flag->name, (xmlChar *)"flag"))
{
char *issuer = (char *)xmlGetProp (flag, (xmlChar *)"issuer");
if(issuer)
{
score += virgule_cert_level_from_name (vr,
virgule_req_get_tmetric_level (vr, issuer));
if(!strcmp (issuer, vr->u))
flagged = TRUE;
xmlFree (issuer);
}
}
}
if (flagged)
return virgule_send_error_page (vr, vERROR, "spam flag", "You have already flagged this account as spam.\n");
/* add a new spam flag and increment score */
flag = xmlNewChild (spamtree, NULL, (xmlChar *)"flag", NULL);
xmlSetProp (flag, (xmlChar *)"issuer", (xmlChar *)vr->u);
score += virgule_cert_level_from_name (vr, virgule_req_get_tmetric_level (vr, vr->u));
scorestr = (xmlChar *)apr_itoa (vr->r->pool, score);
spamscore = virgule_xml_find_child(profile->xmlRootNode, "spamscore");
if(spamscore == NULL)
spamscore = xmlNewChild (profile->xmlRootNode, NULL, (xmlChar *)"spamscore", scorestr);
else
xmlNodeSetContent (spamscore, scorestr);
virgule_db_xml_put (vr->r->pool, vr->db, db_key, profile);
if(score > vr->priv->acct_spam_threshold)
{
acct_kill(vr, u);
return virgule_send_error_page (vr, vINFO, "account deleted", "<blockquote><p>User account [%s] has been deleted.</p><p>Your spam report pushed the total spam score for this account to [%i].</p><p>Accounts are automatically deleted if their spam score exceeds the currently configured score threshold of [%i].</p><p>Thanks for taking the time to help keep %s free of spam!</p><blockquote>\n", u, score, vr->priv->acct_spam_threshold, vr->priv->site_name);
}
else
return virgule_send_error_page (vr, vINFO, "account flagged", "<blockquote><p>User account [%s] has been flagged as spam.</p><p>Total spam score for this account is [%i].</p><p>Accounts are automatically deleted if their spam score exceeds the currently configured score threshold of [%i].</p><p>Thanks for taking the time to help keep %s free of spam!</p><blockquote>\n", u, score, vr->priv->acct_spam_threshold, vr->priv->site_name);
}
/* update an arbitrary pointer */
int
virgule_acct_set_lastread(VirguleReq *vr, const char *section, const char *location, int last_read)
{
apr_pool_t *p = vr->r->pool;
Db *db = vr->db;
char *db_key;
xmlDoc *profile;
xmlNode *tree, *msgptr;
int status;
virgule_auth_user(vr);
if (vr->u == NULL)
return 0;
db_key = virgule_acct_dbkey (vr, vr->u);
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
return -1;
tree = virgule_xml_ensure_child (profile->xmlRootNode, apr_psprintf(p, "%spointers", section));
for (msgptr = tree->children; msgptr != NULL; msgptr = msgptr->next)
{
if (msgptr->type == XML_ELEMENT_NODE &&
!xmlStrcmp (msgptr->name, (xmlChar *)"lastread"))
{
char *old_msgptr = (char *)xmlGetProp (msgptr, (xmlChar *)"location");
if (old_msgptr)
{
if (!strcmp (old_msgptr, location))
{
xmlFree (old_msgptr);
break;
}
xmlFree (old_msgptr);
}
}
}
if (msgptr == NULL)
{
msgptr = xmlNewChild (tree, NULL, (xmlChar *)"lastread", NULL);
xmlSetProp (msgptr, (xmlChar *)"location", (xmlChar *)location);
}
xmlSetProp (msgptr, (xmlChar *)"num", (xmlChar *)apr_psprintf(p, "%d", last_read));
xmlSetProp (msgptr, (xmlChar *)"date", (xmlChar *)virgule_iso_now(p));
status = virgule_db_xml_put (p, db, db_key, profile);
virgule_db_xml_free (p, profile);
return status;
}
int
virgule_acct_get_lastread(VirguleReq *vr, const char *section, const char *location)
{
apr_pool_t *p = vr->r->pool;
Db *db = vr->db;
char *db_key;
xmlDoc *profile;
xmlNode *tree, *msgptr;
virgule_auth_user(vr);
if (vr->u == NULL)
return -1;
db_key = virgule_acct_dbkey (vr, vr->u);
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
return -1;
tree = virgule_xml_find_child (profile->xmlRootNode, apr_psprintf(p, "%spointers", section));
if (tree == NULL)
return -1;
for (msgptr = tree->children; msgptr != NULL; msgptr = msgptr->next)
{
if (msgptr->type == XML_ELEMENT_NODE &&
!xmlStrcmp (msgptr->name, (xmlChar *)"lastread"))
{
char *old_msgptr = (char *)xmlGetProp (msgptr, (xmlChar *)"location");
if (old_msgptr)
{
if (!strcmp (old_msgptr, location))
return atoi ((char *)xmlGetProp (msgptr, (xmlChar *)"num"));
}
}
}
return -1;
}
int
virgule_acct_get_num_old(VirguleReq *vr)
{
virgule_auth_user(vr);
if (vr->u)
{
apr_pool_t *p = vr->r->pool;
char *db_key;
xmlDoc *profile;
xmlNode *tree;
char *num_old;
db_key = virgule_acct_dbkey (vr, vr->u);
profile = virgule_db_xml_get (p, vr->db, db_key);
tree = virgule_xml_find_child (profile->xmlRootNode, "info");
num_old = (char *)xmlGetProp (tree, (xmlChar *)"numold");
if (num_old == NULL || *num_old == '\0')
return 30;
else
return atoi(num_old);
}
return -1;
}
char *
virgule_acct_get_lastread_date(VirguleReq *vr, const char *section, const char *location)
{
apr_pool_t *p = vr->r->pool;
Db *db = vr->db;
char *db_key;
xmlDoc *profile;
xmlNode *tree, *msgptr;
char *date;
virgule_auth_user(vr);
if (vr->u == NULL)
return NULL;
db_key = virgule_acct_dbkey (vr, vr->u);
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
return NULL;
tree = virgule_xml_find_child (profile->xmlRootNode, apr_psprintf(p, "%spointers", section));
if (tree == NULL)
return NULL;
for (msgptr = tree->children; msgptr != NULL; msgptr = msgptr->next)
{
if (msgptr->type == XML_ELEMENT_NODE &&
!xmlStrcmp (msgptr->name, (xmlChar *)"lastread"))
{
char *old_msgptr = (char *)xmlGetProp (msgptr, (xmlChar *)"location");
if (old_msgptr)
{
if (!strcmp (old_msgptr, location))
{
date = (char *)xmlGetProp (msgptr, (xmlChar *)"date");
if (date == NULL)
date = "1970-01-01 00:00:00";
virgule_db_xml_free (p, profile);
return date;
}
}
}
}
virgule_db_xml_free (p, profile);
return "1970-01-01 00:00:00";
}
/**
* validate_username: Ensure that username is valid.
* @u: Putative username.
*
* Return value: NULL if valid, or reason as string if not.
**/
char *
virgule_validate_username (VirguleReq *vr, const char *u)
{
int len;
int i;
if (u == NULL || !u[0])
return "You must specify a username.";
len = strlen (u);
if (len > 20)
return "The username must be 20 characters or less.";
if (vr->priv->allow_account_extendedcharset)
{
if (!isalnum(u[0]))
return "First character must be alphanumeric.";
for (i = 0; i < len; i++)
{
if (!isalnum (u[i]) && u[i]!='-' && u[i]!='_' && u[i]!=' ' && u[i]!= '.' )
return "The username must contain only alphanumeric, dash, underscore, space, or dot characters.";
}
}
else
{
for (i = 0; i < len; i++)
{
if (!isalnum (u[i]))
return "The username must contain only alphanumeric characters.";
}
}
return NULL;
}
/* Make the db key. Sanity check the username. */
char *
virgule_acct_dbkey (VirguleReq *vr, const char *u)
{
if (virgule_validate_username (vr, u) != NULL)
return NULL;
return apr_pstrcat (vr->r->pool, "acct/", u, "/profile.xml", NULL);
}
static void
acct_set_cookie (VirguleReq *vr, const char *u, const char *cookie,
time_t lifetime)
{
request_rec *r = vr->r;
char *id_cookie, *exp_date;
time_t exp_time;
id_cookie = apr_pstrcat (r->pool, u, ":", cookie, NULL);
exp_time = time (NULL) + lifetime;
exp_date = ap_ht_time (r->pool, (apr_time_t)exp_time * 1000000,
"%A, %d-%b-%Y %H:%M:%S %Z", 1);
apr_table_add (r->headers_out, "Set-Cookie",
apr_psprintf (r->pool, "id=%s; path=/; Expires=%s",
id_cookie, exp_date));
}
static int
acct_index_serve (VirguleReq *vr)
{
apr_pool_t *p = vr->r->pool;
int i;
virgule_auth_user (vr);
if (vr->u)
{
Buffer *b;
const char *level;
char *db_key;
xmlDoc *profile;
xmlNode *info, *aggregate, *tree;
char *value;
db_key = virgule_acct_dbkey (vr, vr->u);
profile = virgule_db_xml_get (p, vr->db, db_key);
info = virgule_xml_find_child (profile->xmlRootNode, "info");
aggregate = virgule_xml_find_child (profile->xmlRootNode, "aggregate");
level = virgule_req_get_tmetric_level (vr, vr->u);
if (virgule_set_temp_buffer (vr) != 0)
return HTTP_INTERNAL_SERVER_ERROR;
b = vr->b;
virgule_buffer_printf (b, "<p>Welcome, <em>%s</em>. The range of functions you "
"can access depends on your <a href=\"%s/certs.html\">certification</a> "
"level. Remember to certify any other users of this site that you "
"know. The trust metric system relies on user participation!</p>\n<p>",
vr->u, vr->prefix);
virgule_render_cert_level_begin (vr, vr->u, CERT_STYLE_SMALL);
virgule_buffer_printf (b, "You are currently certified at the %s level by the other users of this site.", level);
virgule_render_cert_level_end (vr, CERT_STYLE_SMALL);
virgule_buffer_puts (b, "</p><p>At this level you can:</p>\n<ul>");
virgule_buffer_printf (b, "<li>Link to your <a href=\"%s/person/%s\">public profile</a></li>",vr->prefix,ap_escape_uri(vr->r->pool,vr->u));
virgule_buffer_printf (b, "<li><a href=\"%s/diary/\">Post a blog entry</a></li>\n",vr->prefix);
if (virgule_req_ok_to_reply (vr))
{
virgule_buffer_puts (b, "<li>Post replies to articles</li>\n");
}
if (virgule_req_ok_to_create_project (vr))
{
virgule_buffer_printf (b, "<li><a href=\"%s/proj/new.html\">Create new a project</a></li>\n",vr->prefix);
virgule_buffer_printf (b, "<li>Edit your <a href=\"%s/proj/\">existing projects</a></li>\n",vr->prefix);
}
if (virgule_req_ok_to_post (vr))
{
virgule_buffer_printf (b, "<li><a href=\"%s/article/post.html\">Post an article</a></li>\n",vr->prefix);
}
if (virgule_req_ok_to_syndicate_blog (vr))
{
virgule_buffer_printf (b, "<li>Syndicate your blog from an external site</li>\n");
}
virgule_buffer_puts (b, "<li><a href=\"logout.html\">Logout</a></li>\n");
if (vr->priv->projstyle == PROJSTYLE_NICK)
virgule_buffer_puts (b, "<li><a href=\"/proj/updatepointers.html\">Mark all messags as read</a></li>\n");
virgule_buffer_puts (b, "</ul><p> Or you can update your account info: </p>\n");
virgule_buffer_puts (b, "<form method=\"POST\" action=\"update.html\" accept-charset=\"UTF-8\">\n");
for (i = 0; prof_fields[i].description; i++)
{
if (prof_fields[i].flags & PROFILE_SYNDICATE &&
!virgule_req_ok_to_syndicate_blog (vr))
continue;
if (vr->priv->projstyle != PROJSTYLE_NICK &&
!strcmp(prof_fields[i].attr_name, "numold"))
continue;
if (prof_fields[i].flags & PROFILE_SYNDICATE)
tree = aggregate;
else
tree = info;
value = NULL;
if (tree)
value = (char *)xmlGetProp (tree, (xmlChar *)prof_fields[i].attr_name);
virgule_buffer_printf (b, "<p> %s: <br>\n", prof_fields[i].description);
if (prof_fields[i].flags & PROFILE_BOOLEAN)
virgule_buffer_printf (b, "<input name=\"%s\" type=\"checkbox\" %s></p>\n",
prof_fields[i].attr_name,
value ? (strcmp (value, "on") ? "" : " checked") : "");
else if (prof_fields[i].flags & PROFILE_TEXTAREA)
virgule_buffer_printf (b, "<textarea name=\"%s\" cols=\"%d\" rows=\"%d\" wrap=\"hard\">%s</textarea></p>\n",
prof_fields[i].attr_name,
prof_fields[i].size / 1000,
prof_fields[i].size % 1000,
value ? ap_escape_html (p, value) : "");
else
virgule_buffer_printf (b, "<input name=\"%s\" size=\"%d\" value=\"%s\"> </p>\n",
prof_fields[i].attr_name, prof_fields[i].size,
value ? ap_escape_html (p, value) : "");
if (value != NULL)
xmlFree (value);
}
virgule_buffer_puts (b, " <input type=\"submit\" value=\"Update\">\n</form>\n");
virgule_set_main_buffer (vr);
return virgule_render_in_template (vr, "/templates/acct-info.xml", "content", "User Account Info");
}
else
{
return virgule_render_in_template (vr, "/templates/acct-login.xml", NULL, NULL);
}
}
static int
acct_newsub_serve (VirguleReq *vr)
{
request_rec *r = vr->r;
apr_pool_t *p = r->pool;
Db *db = vr->db;
apr_table_t *args;
const char *u, *pass, *pass2, *email, *subcode;
char *db_key, *db_key_lc;
xmlDoc *profile;
xmlNode *root, *tree;
int status;
char *cookie;
int i;
const char *date;
char *u_lc;
if (!vr->priv->allow_account_creation)
return virgule_send_error_page (vr, vERROR, "forbidden", "No new accounts may be created at this time.\n");
args = virgule_get_args_table (vr);
if (args == NULL)
return virgule_send_error_page (vr, vERROR, "form data", "Invalid form submission.\n");
u = apr_table_get (args, "u");
pass = apr_table_get (args, "pass");
pass2 = apr_table_get (args, "pass2");
email = apr_table_get (args, "email");
subcode = apr_table_get (args, "subcode");
if (subcode != NULL && subcode[0])
return virgule_send_error_page (vr, vERROR, "forbidden",
"Submission from a banned user agent or IP address.");
if (u == NULL || !u[0])
return virgule_send_error_page (vr, vERROR, "username",
"You must specify a username.");
if (strlen (u) > 20)
return virgule_send_error_page (vr, vERROR, "username",
"The username must be 20 characters or less.");
/* sanity check user name */
db_key = virgule_acct_dbkey (vr, u);
if (db_key == NULL && vr->priv->allow_account_extendedcharset)
return virgule_send_error_page (vr, vERROR, "username",
"Username must begin with an alphanumeric character and contain only alphanumerics, spaces, dashes, underscores, or periods.");
else if (db_key == NULL)
return virgule_send_error_page (vr, vERROR, "username",
"Username must contain only alphanumeric characters.");
u_lc = apr_pstrdup (p, u);
ap_str_tolower (u_lc);
db_key_lc = virgule_acct_dbkey (vr, u_lc);
profile = virgule_db_xml_get (p, db, db_key_lc);
if (profile != NULL)
return virgule_send_error_page (vr, vERROR,
"username",
"The user name <em>%s</em> is already in use.",
u);
if (pass == NULL || !pass[0])
return virgule_send_error_page (vr, vERROR, "password",
"You must specify a password.");
if (pass == NULL || pass2 == NULL)
return virgule_send_error_page (vr, vERROR,
"password",
"You must specify a password and enter it twice.");
if (!strcmp (pass, u))
return virgule_send_error_page (vr, vERROR,
"password",
"The username may not be used as the password.");
if (strcmp (pass, pass2))
return virgule_send_error_page (vr, vERROR,
"password",
"The passwords must match. Have a cup of coffee and try again.");
if (email == NULL || !email[0])
return virgule_send_error_page (vr, vERROR, "email address",
"You must specify a valid email address. The email address is needed for account authentication, and password recovery purposes.");
profile = virgule_db_xml_doc_new (p);
root = xmlNewDocNode (profile, NULL, (xmlChar *)"profile", NULL);
profile->xmlRootNode = root;
date = virgule_iso_now (p);
tree = xmlNewChild (root, NULL, (xmlChar *)"date", (xmlChar *)date);
tree = xmlNewChild (root, NULL, (xmlChar *)"auth", NULL);
xmlSetProp (tree, (xmlChar *)"pass", (xmlChar *)pass);
cookie = virgule_rand_cookie (p);
#if 0
virgule_buffer_printf (b, "Cookie is %s\n", cookie);
#endif
xmlSetProp (tree, (xmlChar *)"cookie", (xmlChar *)cookie);
tree = xmlNewChild (root, NULL, (xmlChar *)"info", NULL);
for (i = 0; prof_fields[i].description; i++)
{
const char *val;
val = apr_table_get (args, prof_fields[i].attr_name);
if (val == NULL)
continue;
if (virgule_is_input_valid(val))
xmlSetProp (tree, (xmlChar *)prof_fields[i].attr_name, (xmlChar *)val);
else
return virgule_send_error_page (vr, vERROR,
"invalid UTF-8",
"Only valid characters that use valid UTF-8 sequences may be submitted.");
}
status = virgule_db_xml_put (p, db, db_key, profile);
if (status)
return virgule_send_error_page (vr, vERROR,
"internal",
"There was an error storing the account profile.");
acct_set_cookie (vr, u, cookie, 86400 * 365);
vr->u = u;
virgule_add_recent (p, db, "recent/acct.xml", u, 100, 0);
/* store lower case alias if necessary */
if (! (strcmp (u_lc, u) == 0))
{
profile = virgule_db_xml_doc_new (p);
root = xmlNewDocNode (profile, NULL, (xmlChar *)"profile", NULL);
profile->xmlRootNode = root;
tree = xmlNewChild (root, NULL, (xmlChar *)"alias", NULL);
xmlSetProp (tree, (xmlChar *)"link", (xmlChar *)u);
status = virgule_db_xml_put (p, db, db_key_lc, profile);
}
return virgule_send_error_page (vr, vINFO,
"Account created",
"Account <a href=\"%s/person/%s/\">%s</a> created.\n",
vr->prefix, ap_escape_uri (vr->r->pool,u), u);
}
/* Success: return 1, set *ret1 to username and *ret2 to cookie
* Failure: return 0, set *ret1 and *ret2 to short and long error messages
* FIXME: this function's interface is _nasty_
*/
int
virgule_acct_login (VirguleReq *vr, const char *u, const char *pass,
const char **ret1, const char **ret2)
{
request_rec *r = vr->r;
apr_pool_t *p = r->pool;
Db *db = vr->db;
char *db_key, *stored_pass;
xmlDoc *profile;
xmlNode *root, *tree;
char *cookie;
const int n_iter_max = 10;
int i;
*ret1 = *ret2 = NULL;
if (u == NULL || !u[0])
{
*ret1 = "Specify a username";
*ret2 = "You must specify a username.";
return 0;
}
if (pass == NULL || !pass[0])
{
*ret1 = "Specify a password";
*ret2 = "You must specify a password.";
return 0;
}
for (i = 0; i < n_iter_max; i++)
{
/* sanity check user name */
db_key = virgule_acct_dbkey (vr, u);
if (db_key == NULL)
{
*ret1 = "Invalid username";
*ret2 = "Username must contain only alphanumeric characters.";
return 0;
}
profile = virgule_db_xml_get (p, db, db_key);
if (profile == NULL)
{
*ret1 = "Account does not exist";
*ret2 = apr_psprintf (p, "Account <em>%s</em> does not exist. Try the <a href=\"new.html\">new account creation</a> page.", u);
return 0;
}
#if 0
virgule_buffer_printf (b, "Profile: %s\n", profile->name);
return virgule_buffer_send_response (r, b);
#endif
root = profile->xmlRootNode;
tree = virgule_xml_find_child (root, "alias");
if (tree == NULL)
break;
u = virgule_xml_get_prop (p, tree, (xmlChar *)"link");
db_key = virgule_acct_dbkey (vr, u);
profile = virgule_db_xml_get (p, db, db_key);
}
if (i == n_iter_max)
{
*ret1 = "Alias loop";
*ret2 = apr_psprintf (p, "More than %d levels of alias indirection from %s, indicating an alias loop. This is a problem with the server.",
n_iter_max, u);
return 0;
}
tree = virgule_xml_find_child (root, "auth");
if (tree == NULL)
{
*ret1 = "Account is missing auth field";
*ret2 = apr_psprintf (p, "Account <em>%s</em> is missing its auth field. This is a problem with the server.", u);
return 0;
}
stored_pass = (char *)xmlGetProp (tree, (xmlChar *)"pass");
if (strcmp (pass, stored_pass))
{
xmlFree (stored_pass);
*ret1 = "Incorrect password";
*ret2 = "Incorrect password, try again.";
return 0;
}
xmlFree (stored_pass);
cookie = (char *)xmlGetProp (tree, (xmlChar *)"cookie");
*ret1 = apr_pstrdup (p, u);
*ret2 = apr_pstrdup (p, cookie);
xmlFree (cookie);
return 1;
}
/*
* Send an email to the given user reminding them of their password
*/
static int
send_email(VirguleReq *vr, const char *mail, const char *u, const char *pass)
{
FILE *fp;
char cmd[1024];
snprintf( cmd, sizeof(cmd)-1, "/usr/lib/sendmail -t -f %s", vr->priv->admin_email);
if ((fp = popen( cmd, "w")) == NULL)
return virgule_send_error_page (vr, vERROR, "SMTP", "There was an error sending mail to <em>%s</em>.\n", mail);
fprintf(fp,"To: %s\n", mail);
#if 1
fprintf(fp,"Bcc: %s\n", vr->priv->admin_email);
#endif
fprintf(fp,"From: %s\n", vr->priv->admin_email);
fprintf(fp,"Subject: Your %s password\n\n", vr->priv->site_name);
fprintf(fp,"Someone at IP address %s requested a ", vr->r->connection->remote_ip);
fprintf(fp,"password reminder for your %s account.\n\n", vr->priv->site_name);
fprintf(fp, "Your username is: %s\n", u);
fprintf(fp, "Your password is: %s\n\n", pass);
fprintf(fp, "If you did not request this reminder don't worry, as ");
fprintf(fp, "only you will receive this email.\n" );
pclose(fp);
return 1;
}
static int
acct_loginsub_serve (VirguleReq *vr)
{
request_rec *r = vr->r;
apr_table_t *args;
const char *u, *pass, *forgot;
const char *ret1, *ret2;
const char *cookie;
r->content_type = "text/plain; charset=UTF-8";
args = virgule_get_args_table (vr);
if (args == NULL)
return virgule_send_error_page (vr, vERROR, "form data", "Invalid form submission.\n");
u = apr_table_get (args, "u");
pass = apr_table_get (args, "pass");
forgot = apr_table_get (args, "forgot");
#if 0
virgule_buffer_printf (b, "Username: %s\n", u);
virgule_buffer_printf (b, "Password: %s\n", pass);
#endif
/* User has forgotten their password. */
if ( forgot != NULL )
{
char *db_key, *db_key_lc, *mail;
apr_pool_t *p = vr->r->pool;
xmlDoc *profile;
xmlNode *tree;
/* sanity check user name */
db_key = virgule_acct_dbkey(vr, u);
if (db_key == NULL)
{
return virgule_send_error_page (vr, vERROR, "username", "Username contains invalid characters.");
}
/* verify that user name is in DB */
profile = virgule_db_xml_get (p, vr->db, db_key);
if (profile == NULL)
{
return virgule_send_error_page (vr, vERROR, "username", "The specified account could not be found.");
}