doreq.pm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. ########################################################################
  2. # doreq.pm
  3. #
  4. # These routines handle HTTP requests that are not
  5. # just to display a page.
  6. #
  7. # See COPYING for licensing information.
  8. #
  9. ########################################################################
  10. # Perl modules
  11. use strict;
  12. use Socket;
  13. # global variables
  14. use vars qw($conf);
  15. use vars qw($pref $userinfo $dominfo $remote_ip);
  16. use vars qw($reqparm $thiscgi $thishost);
  17. # GnuDIP common subroutines
  18. use gdiplib;
  19. use gdipcgi_cmn;
  20. use htmlgen;
  21. use mailgen;
  22. use gdipmailchk;
  23. ########################################################################
  24. # NOTE
  25. # Each pg_? routine generates a page and does an "exit".
  26. # So they DO NOT RETURN.
  27. ########################################################################
  28. ########################################################################
  29. # self registration
  30. ########################################################################
  31. sub do_self {
  32. # check a valid domain is specified
  33. pg_error('no_domain') if !$$reqparm{'new_domain'};
  34. $dominfo = getdomain($$reqparm{'new_domain'});
  35. pg_error('unknown_dom') if !$dominfo;
  36. # this domain allows addself?
  37. pg_error('no_domaddself') if $$dominfo{'addself'} eq 'NO';
  38. # check syntax of new user name
  39. pg_error('no_username') if !$$reqparm{'new_username'};
  40. pg_error('bad_username') if !validdomcomp($$reqparm{'new_username'});
  41. # restricted user?
  42. chkrestrict($$reqparm{'new_username'});
  43. # check syntax of passwords
  44. pg_error('no_password') if !$$reqparm{'new_password'};
  45. pg_error('no_password') if !$$reqparm{'new_password1'};
  46. pg_error('not_same')
  47. if $$reqparm{'new_password'} ne $$reqparm{'new_password1'};
  48. # check already exists?
  49. $userinfo = getuser($$reqparm{'new_username'}, $$reqparm{'new_domain'});
  50. pg_error('user_exists') if $userinfo;
  51. # check for admin by same name
  52. $userinfo = getuser($$reqparm{'new_username'}, '');
  53. pg_error('restricted_user') if $userinfo;
  54. default_empty('new_email');
  55. # don't have to send E-mail?
  56. if ($$pref{'REQUIRE_EMAIL'} ne 'YES') {
  57. $userinfo = createuser(
  58. $$reqparm{'new_username'},
  59. $$reqparm{'new_domain'},
  60. md5_hex($$reqparm{'new_password'}),
  61. 'USER',
  62. $$reqparm{'new_email'},
  63. );
  64. writelog(
  65. "User $$reqparm{'new_username'}.$$reqparm{'new_domain'} self registered"
  66. );
  67. pg_didself();
  68. }
  69. pg_error('no_email') if $$reqparm{'new_email'} eq '';
  70. pg_error('bad_email') if !validemail($$reqparm{'new_email'});
  71. # check not a robot
  72. mchk_check();
  73. mail_self(
  74. $$reqparm{'new_username'},
  75. $$reqparm{'new_domain'},
  76. md5_hex($$reqparm{'new_password'}),
  77. $$reqparm{'new_email'},
  78. );
  79. pg_selfemail($$reqparm{'new_email'});
  80. }
  81. ########################################################################
  82. # self registration after E-mail sent
  83. ########################################################################
  84. sub do_selfcreate {
  85. # self registration allowed?
  86. pg_error('no_addself') if $$pref{'ADD_SELF'} ne 'YES';
  87. # split query string parameter on commas
  88. my ($username, $domain, $password, $email, $checkval) =
  89. split(/,/, $$reqparm{'selfcreate'});
  90. $username = '' if !defined($username);
  91. $domain = '' if !defined($domain);
  92. $password = '' if !defined($password);
  93. $email = '' if !defined($email);
  94. $checkval = '' if !defined($checkval);
  95. # validate the signature
  96. my $check = md5_base64(
  97. "$username.$domain.$password.$email.$$pref{'SERVER_KEY'}"
  98. );
  99. pg_error('bad_request') if $check ne $checkval;
  100. # check a valid domain is specified
  101. $dominfo = getdomain($domain);
  102. pg_error('unknown_dom') if !$dominfo;
  103. # this domain allows addself?
  104. pg_error('no_add_self') if $$dominfo{'addself'} eq 'NO';
  105. # restricted user?
  106. chkrestrict($username);
  107. # check already exists?
  108. $userinfo = getuser($username, $domain);
  109. pg_error('user_exists') if $userinfo;
  110. # check for admin by same name
  111. $userinfo = getuser($username, '');
  112. pg_error('restricted_user') if $userinfo;
  113. $userinfo = createuser(
  114. $username,
  115. $domain,
  116. $password,
  117. 'USER',
  118. $email,
  119. );
  120. writelog(
  121. "User $username.$domain self registered"
  122. );
  123. pg_didself();
  124. }
  125. ########################################################################
  126. # do AutoURL
  127. ########################################################################
  128. sub do_autourl {
  129. # retrieve any cookies passed to us
  130. my $cookie = parse_cookies($ENV{'HTTP_COOKIE'});
  131. # look for cookies we need here
  132. my $cookieuser = $$cookie{'gnudipuser'};
  133. my $cookiedomain = $$cookie{'gnudipdomain'};
  134. my $cookiepass = $$cookie{'gnudippass'};
  135. # did we get all cookies?
  136. pg_error('no_cookie')
  137. if !$cookieuser or
  138. !defined($cookiedomain) or
  139. !$cookiepass;
  140. # auto URL disabled?
  141. if ($$pref{'ALLOW_AUTO_URL'} ne 'YES') {
  142. writelog(
  143. "Auto URL attempt - user: $cookieuser - domain: $cookiedomain"
  144. );
  145. removecookies();
  146. pg_error("no_autourl");
  147. }
  148. # login
  149. $userinfo = getuser($cookieuser, $cookiedomain);
  150. pg_error('bad_cookie')
  151. if !$userinfo or $cookiepass ne $$userinfo{'password'};
  152. # need an E-mail adddress?
  153. if ($$pref{'REQUIRE_EMAIL'} eq 'YES' and
  154. $$userinfo{'level'} ne 'ADMIN' and
  155. $$userinfo{'email'} eq '') {
  156. # generate a logon ID
  157. $$reqparm{'logonid'} = randomsalt();
  158. pg_needemail();
  159. }
  160. # is the IP address an acceptable one?
  161. pg_error('bad_IP') if !validip($remote_ip);
  162. # TTL value
  163. my $TTL = 0;
  164. $TTL = $$conf{'TTL'} if $$conf{'TTL'};
  165. $TTL = $$conf{"TTL.$cookiedomain"}
  166. if $$conf{"TTL.$cookiedomain"};
  167. $TTL = $$conf{"TTL.$cookieuser.$cookiedomain"}
  168. if $$conf{"TTL.$cookieuser.$cookiedomain"};
  169. # IP address changed?
  170. if ($remote_ip ne $$userinfo{'currentip'}) {
  171. donsupdate ($cookiedomain,
  172. "update delete $cookieuser.$cookiedomain A",
  173. "update add $cookieuser.$cookiedomain $TTL A $remote_ip"
  174. );
  175. writelog(
  176. "User $cookieuser.$cookiedomain successful update to ip $remote_ip (autourl)"
  177. );
  178. } else {
  179. writelog(
  180. "User $cookieuser.$cookiedomain remains at ip $remote_ip (autourl)"
  181. );
  182. }
  183. # update database
  184. $$userinfo{'currentip'} = $remote_ip;
  185. updateuser($userinfo);
  186. # this was test?
  187. pg_goodautourl() if $$reqparm{'testautourl'};
  188. # redirect?
  189. pg_noforwardurl() if !$$userinfo{'forwardurl'};
  190. print "Location: $$userinfo{'forwardurl'}\n\n";
  191. exit;
  192. }
  193. #######################################################################
  194. # remove Auto URL
  195. #######################################################################
  196. sub do_removeautourl {
  197. # update user information
  198. $$userinfo{'autourlon'} = 'NO';
  199. updateuser($userinfo);
  200. # generate empty cookies
  201. removecookies();
  202. # message
  203. pg_msg(qq*
  204. Auto URL Removal Successful
  205. *,qq*
  206. Any Auto URL cookies in your browser have been removed.
  207. *);
  208. exit;
  209. }
  210. ########################################################################
  211. # login
  212. ########################################################################
  213. sub do_login {
  214. # entered necessary info?
  215. pg_error('no_username') if ! $$reqparm{'username'};
  216. pg_error('no_password') if ! $$reqparm{'password'};
  217. # try for an admin first, then normal user
  218. $userinfo = getuser($$reqparm{'username'}, '');
  219. if ($userinfo) {
  220. $$reqparm{'domain'} = '';
  221. } else {
  222. $userinfo =
  223. getuser($$reqparm{'username'}, $$reqparm{'domain'});
  224. }
  225. # user name matches with case sensitivity?
  226. pg_error('nouser')
  227. if !$userinfo or
  228. $$userinfo{'username'} ne $$reqparm{'username'};
  229. # password disabled?
  230. pg_error('dispass')
  231. if $$userinfo{'password'} eq '';
  232. # hash the password, if not already hashed, and check it
  233. if ($$reqparm{'login'} ne 'enc') {
  234. $$reqparm{'password'} = md5_hex($$reqparm{'password'});
  235. }
  236. pg_error('badpass')
  237. if $$userinfo{'password'} ne $$reqparm{'password'};
  238. # get information for user's domain
  239. if ($$reqparm{'domain'}) {
  240. $dominfo = getdomain($$reqparm{'domain'});
  241. pg_error('unknown_dom') if !$dominfo;
  242. }
  243. # generate a logon ID
  244. $$reqparm{'logonid'} = randomsalt();
  245. # need an E-mail address?
  246. if ($$pref{'REQUIRE_EMAIL'} eq 'YES' and
  247. $$userinfo{'level'} ne 'ADMIN' and
  248. $$userinfo{'email'} eq '') {
  249. pg_needemail();
  250. }
  251. pg_options();
  252. }
  253. ########################################################################
  254. # send Quick login URL
  255. ########################################################################
  256. sub do_sendURL {
  257. # sending quick login URL allowed?
  258. pg_error('no_sendURL') if $$pref{'SEND_URL'} ne 'YES';
  259. # entered necessary info?
  260. pg_error('no_username') if ! $$reqparm{'sendURL_username'};
  261. # try for an admin first, then normal user
  262. $userinfo = getuser($$reqparm{'sendURL_username'}, '');
  263. if ($userinfo) {
  264. $$reqparm{'domain'} = '';
  265. } else {
  266. $userinfo =
  267. getuser($$reqparm{'sendURL_username'}, $$reqparm{'domain'});
  268. }
  269. # user name matches with case sensitivity?
  270. pg_error('nouser')
  271. if !$userinfo or
  272. $$userinfo{'username'} ne $$reqparm{'sendURL_username'};
  273. # get information for user's domain
  274. if ($$reqparm{'domain'}) {
  275. $dominfo = getdomain($$reqparm{'domain'});
  276. pg_error('unknown_dom') if !$dominfo;
  277. }
  278. # password disabled?
  279. pg_error('dispass') if $$userinfo{'password'} eq '';
  280. # no E-mail address?
  281. pg_error('no_useremail') if $$userinfo{'email'} eq '';
  282. # check not a robot
  283. mchk_check();
  284. # send the E-mail
  285. mail_quick(
  286. $$userinfo{'username'},
  287. $$userinfo{'domain'},
  288. $$userinfo{'password'},
  289. $$userinfo{'email'},
  290. );
  291. # message
  292. pg_sentURL($$userinfo{'email'});
  293. }
  294. ########################################################################
  295. # update IP address
  296. ########################################################################
  297. sub do_updatehost {
  298. # valid syntax?
  299. pg_error('bad_IP_syntax') if !validdotquad($$reqparm{'updateaddr'});
  300. # is the IP address an acceptable one?
  301. pg_error('bad_IP') if !validip($$reqparm{'updateaddr'});
  302. # TTL value
  303. my $TTL = 0;
  304. $TTL = $$conf{'TTL'} if $$conf{'TTL'};
  305. $TTL = $$conf{"TTL.$$userinfo{'domain'}"}
  306. if $$conf{"TTL.$$userinfo{'domain'}"};
  307. $TTL = $$conf{"TTL.$$userinfo{'username'}.$$userinfo{'domain'}"}
  308. if $$conf{"TTL.$$userinfo{'username'}.$$userinfo{'domain'}"};
  309. # IP address changed?
  310. if ($$reqparm{'updateaddr'} ne $$userinfo{'currentip'}) {
  311. donsupdate ($$userinfo{'domain'},
  312. "update delete $$userinfo{'username'}.$$userinfo{'domain'}. A",
  313. "update add $$userinfo{'username'}.$$userinfo{'domain'}. $TTL " .
  314. "A $$reqparm{'updateaddr'}"
  315. );
  316. writelog(
  317. "User $$userinfo{'username'}.$$userinfo{'domain'} " .
  318. "successful update to ip $$reqparm{'updateaddr'} (manual)");
  319. } else {
  320. writelog(
  321. "User $$userinfo{'username'}.$$userinfo{'domain'} " .
  322. "remains at ip $$userinfo{'currentip'} (manual)");
  323. }
  324. # update database
  325. $$userinfo{'currentip'} = $$reqparm{'updateaddr'};
  326. updateuser($userinfo);
  327. # reshow page
  328. pg_options();
  329. }
  330. ########################################################################
  331. # offline
  332. ########################################################################
  333. sub do_offline {
  334. # had an IP address?
  335. if ($$userinfo{'currentip'} ne '0.0.0.0') {
  336. donsupdate ($$reqparm{'domain'},
  337. "update delete $$userinfo{'username'}.$$userinfo{'domain'}. A"
  338. );
  339. writelog(
  340. "User $$userinfo{'username'}.$$userinfo{'domain'} " .
  341. "successful remove from ip $$userinfo{'currentip'} (manual)");
  342. } else {
  343. writelog(
  344. "User $$userinfo{'username'}.$$userinfo{'domain'} " .
  345. "remains removed (manual)")
  346. }
  347. # update database
  348. $$userinfo{'currentip'} = '0.0.0.0';
  349. updateuser($userinfo);
  350. # reshow page
  351. pg_options();
  352. }
  353. ########################################################################
  354. # delete current user
  355. ########################################################################
  356. sub do_delthisuser {
  357. # self deleteion allowed?
  358. pg_error('no_delself') if $$pref{'DELETE_SELF'} ne 'YES';
  359. # remove from DNS
  360. if ($$userinfo{'level'} eq 'USER') {
  361. # send update to DNS server
  362. donsupdate ($$userinfo{'domain'},
  363. "update delete $$userinfo{'username'}.$$userinfo{'domain'}.",
  364. "update delete *.$$userinfo{'username'}.$$userinfo{'domain'}.");
  365. writelog(
  366. "User $$userinfo{'username'}.$$userinfo{'domain'} " .
  367. "complete remove from DNS (delete)");
  368. }
  369. # update database
  370. deleteuser($userinfo);
  371. writelog(
  372. "User $$userinfo{'username'}.$$userinfo{'domain'} deleted by self"
  373. );
  374. # message
  375. pg_usergone();
  376. }
  377. ########################################################################
  378. # update current user
  379. ########################################################################
  380. sub do_updatesettings {
  381. do_cmn_edituser($userinfo, $dominfo);
  382. }
  383. ########################################################################
  384. # save system settings
  385. ########################################################################
  386. sub do_syssettings {
  387. # must be admin
  388. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  389. # ensure initialized
  390. default_NO('ADD_SELF');
  391. default_NO('DELETE_SELF');
  392. default_NO('SEND_URL');
  393. default_NO('REQUIRE_EMAIL');
  394. default_NO('NO_ROBOTS');
  395. default_NO('ALLOW_CHANGE_HOSTNAME');
  396. default_NO('ALLOW_CHANGE_DOMAIN');
  397. default_NO('ALLOW_AUTO_URL');
  398. default_NO('SHOW_DOMAINLIST');
  399. default_empty('PAGE_TIMEOUT');
  400. default_empty('RESTRICTED_USERS');
  401. default_NO('ALLOW_WILD');
  402. default_NO('ALLOW_WILD_USER');
  403. default_NO('ALLOW_MX');
  404. default_NO('ALLOW_MX_USER');
  405. # remove spaces
  406. $$reqparm{'RESTRICTED_USERS'} =~ s/ //g;
  407. # combine into one value
  408. $$reqparm{'ALLOW_WILD'} = 'USER'
  409. if $$reqparm{'ALLOW_WILD'} eq 'NO' and
  410. $$reqparm{'ALLOW_WILD_USER'} eq 'YES';
  411. # combine into one value
  412. $$reqparm{'ALLOW_MX'} = 'USER'
  413. if $$reqparm{'ALLOW_MX'} eq 'NO' and
  414. $$reqparm{'ALLOW_MX_USER'} eq 'YES';
  415. # update values in global variable
  416. setpref('ADD_SELF');
  417. setpref('DELETE_SELF');
  418. setpref('SEND_URL');
  419. setpref('REQUIRE_EMAIL');
  420. setpref('NO_ROBOTS');
  421. setpref('ALLOW_CHANGE_HOSTNAME');
  422. setpref('ALLOW_CHANGE_DOMAIN');
  423. setpref('ALLOW_AUTO_URL');
  424. setpref('RESTRICTED_USERS');
  425. setpref('ALLOW_WILD');
  426. setpref('ALLOW_MX');
  427. setpref('PAGE_TIMEOUT');
  428. setpref('SHOW_DOMAINLIST');
  429. # update database
  430. updateprefs($pref);
  431. # reshow page
  432. pg_syssettings();
  433. }
  434. sub setpref {
  435. my $key = shift;
  436. $$pref{$key} = $$reqparm{$key};
  437. }
  438. ########################################################################
  439. # add domain
  440. ########################################################################
  441. sub do_adddomain {
  442. # must be admin
  443. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  444. # check domain name
  445. pg_error('no_domain') if !$$reqparm{'adddomain_new_domain'};
  446. $$reqparm{'new_domain'} = validdomain($$reqparm{'adddomain_new_domain'});
  447. pg_error('bad_domain') if !$$reqparm{'new_domain'};
  448. # remove trailing period
  449. $$reqparm{'new_domain'} =~ s/\.$//;
  450. # already exists?
  451. my $dinfo = getdomain($$reqparm{'new_domain'});
  452. pg_error('domain_exists') if $dinfo;
  453. # ensure initialized
  454. default_NO('ALLOW_CHANGEPASS');
  455. default_NO('ADDSELF');
  456. # update database
  457. createdomain(
  458. $$reqparm{'new_domain'},
  459. $$reqparm{'ALLOW_CHANGEPASS'},
  460. $$reqparm{'ADDSELF'}
  461. );
  462. # show domain list
  463. pg_managedomains();
  464. }
  465. ########################################################################
  466. # update domain
  467. ########################################################################
  468. sub do_editdomain {
  469. # must be admin
  470. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  471. # check domain name
  472. pg_error('no_domain') if !$$reqparm{'new_domain'};
  473. # ensure initialized
  474. default_NO('ADDSELF');
  475. default_NO('ALLOW_CHANGEPASS');
  476. # get from database and update
  477. my $dinfo = getdomain($$reqparm{'editdom'});
  478. pg_error('bad_edit_domain') if !$dinfo;
  479. # update in variable
  480. $$dinfo{'domain'} = $$reqparm{'new_domain'};
  481. $$dinfo{'changepass'} = $$reqparm{'ALLOW_CHANGEPASS'};
  482. $$dinfo{'addself'} = $$reqparm{'ADDSELF'};
  483. # update database
  484. updatedomain($dinfo);
  485. # reshow page
  486. pg_editdomain($dinfo);
  487. }
  488. ########################################################################
  489. # delete domains
  490. ########################################################################
  491. sub do_deldomain {
  492. # must be admin
  493. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  494. # any domains to delete?
  495. pg_managedomains() if !$$reqparm{'deldom'};
  496. # delete selected domains
  497. foreach my $deldom (split(/\0/, $$reqparm{'deldom'})) {
  498. # get from database
  499. my $dinfo = getdomain($deldom);
  500. # delete from database
  501. deletedomain($dinfo) if $dinfo;
  502. }
  503. # show domain list
  504. pg_managedomains();
  505. }
  506. ########################################################################
  507. # add user
  508. ########################################################################
  509. sub do_adduser {
  510. # must be admin
  511. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  512. # ensure initialised
  513. $$reqparm{'disable'} = '' if ! defined $$reqparm{'disable'};
  514. # entered necessary data?
  515. pg_error('no_username') if !$$reqparm{'new_username'};
  516. if (!$$reqparm{'disable'}) {
  517. pg_error('no_password') if !$$reqparm{'new_password'};
  518. pg_error('no_password') if !$$reqparm{'new_password1'};
  519. }
  520. # check user name
  521. pg_error('bad_username') if !validdomcomp($$reqparm{'new_username'});
  522. # check password
  523. pg_error('not_same')
  524. if !$$reqparm{'disable'} and
  525. $$reqparm{'new_password'} ne $$reqparm{'new_password1'};
  526. # check already exists
  527. my $uinfo = getuser(
  528. $$reqparm{'new_username'}, $$reqparm{'new_domain'});
  529. pg_error('user_exists') if $uinfo;
  530. # check for admin by same name
  531. $uinfo = getuser($$reqparm{'new_username'}, '');
  532. pg_error('user_exists') if $uinfo;
  533. # check user level
  534. $$reqparm{'user_level'} = 'USER' if !$$reqparm{'user_level'};
  535. # ensure initialized
  536. default_empty('new_email');
  537. # no domain name for admin user
  538. $$reqparm{'new_domain'} = '' if $$reqparm{'user_level'} eq 'ADMIN';
  539. # update database
  540. my $password = '';
  541. $password = md5_hex($$reqparm{'new_password'})
  542. if ! $$reqparm{'disable'};
  543. createuser(
  544. $$reqparm{'new_username'},
  545. $$reqparm{'new_domain'},
  546. $password,
  547. $$reqparm{'user_level'},
  548. $$reqparm{'new_email'}
  549. );
  550. writelog(
  551. "User $$reqparm{'new_username'}.$$reqparm{'new_domain'} added by administrator"
  552. );
  553. # show user list with just the new user
  554. $$reqparm{'user_pattern'} = $$reqparm{'new_username'};
  555. pg_manageusers();
  556. }
  557. ########################################################################
  558. # manage users
  559. ########################################################################
  560. sub do_manageusers {
  561. # must be admin
  562. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  563. # ensure initialized
  564. default_empty('user_pattern');
  565. # remove white space from start and end of user pattern
  566. $$reqparm{'user_pattern'} =~ s/^\s+//;
  567. $$reqparm{'user_pattern'} =~ s/\s+$//;
  568. # figure out what key and what order to sort
  569. sub setby {
  570. my $sortby = shift;
  571. if ($$reqparm{'sortby'} ne $sortby) {
  572. $$reqparm{'orderby'} = 'asc';
  573. } elsif (!$$reqparm{'do_deluser'}) {
  574. if ($$reqparm{'orderby'} eq 'asc') {
  575. $$reqparm{'orderby'} = 'desc';
  576. } else {
  577. $$reqparm{'orderby'} = 'asc';
  578. }
  579. }
  580. $$reqparm{'sortby'} = $sortby;
  581. }
  582. default_empty('sortby');
  583. $$reqparm{'sortby'} = 'username' if !$$reqparm{'sortby'};
  584. default_empty('orderby');
  585. $$reqparm{'orderby'} = 'desc' if $$reqparm{'orderby'} ne 'asc';
  586. if ($$reqparm{'manage_users_sortby_currentip'}) {
  587. setby('currentip');
  588. } elsif ($$reqparm{'manage_users_sortby_domain'}) {
  589. setby('domain');
  590. } elsif ($$reqparm{'manage_users_sortby_updated'}) {
  591. setby('updated');
  592. } elsif ($$reqparm{'manage_users_sortby_level'}) {
  593. setby('level');
  594. } elsif ($$reqparm{'manage_users_sortby_email'}) {
  595. setby('email');
  596. } else {
  597. setby('username');
  598. }
  599. # (re)show page
  600. pg_manageusers();
  601. }
  602. ########################################################################
  603. # edit user (by admin)
  604. ########################################################################
  605. sub do_edituser {
  606. # must be admin
  607. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  608. # ensure initialized
  609. default_empty('edituser');
  610. # get from database
  611. my $uinfo = getuserbyid($$reqparm{'edituser'});
  612. pg_error('bad_edit_user') if !$uinfo;
  613. my $dinfo = getdomain($$uinfo{'domain'});
  614. # show common page user edit page
  615. do_cmn_edituser($uinfo, $dinfo);
  616. }
  617. ########################################################################
  618. # common user edit
  619. # - cases distinguished by: $$reqparm{'do_edituser'}
  620. ########################################################################
  621. sub do_cmn_edituser {
  622. my $uinfo = shift;
  623. my $dinfo = shift;
  624. # save for DNS routine
  625. my %userhash = %$uinfo;
  626. my $oldinfo = \%userhash;
  627. # check for valid new domain
  628. my $domchange = '';
  629. $$reqparm{'new_domain'} = $$uinfo{'domain'}
  630. if !$$reqparm{'new_domain'};
  631. if ($$reqparm{'new_domain'} ne $$uinfo{'domain'}) {
  632. pg_error('no_domain_change')
  633. if !$$reqparm{'do_edituser'} and
  634. $$pref{'ALLOW_CHANGE_DOMAIN'} eq 'NO';
  635. $dinfo = getdomain($$reqparm{'new_domain'});
  636. pg_error('unknown_dom') if !$dinfo;
  637. pg_error('no_dom_domain_change')
  638. if !$$reqparm{'do_edituser'} and $$dinfo{'addself'} ne 'YES';
  639. $domchange = 1;
  640. $$uinfo{'domain'} = $$reqparm{'new_domain'};
  641. }
  642. # check for valid new user name
  643. pg_error('no_username') if !$$reqparm{'new_username'};
  644. my $userchange = '';
  645. if ($$reqparm{'new_username'} ne $$uinfo{'username'}) {
  646. pg_error('no_changehostname')
  647. if !$$reqparm{'do_edituser'} and
  648. $$pref{'ALLOW_CHANGE_HOSTNAME'} eq 'NO' and
  649. $$uinfo{'level'} eq 'USER';
  650. pg_error('bad_username') if !validdomcomp($$reqparm{'new_username'});
  651. # restricted user?
  652. chkrestrict($$reqparm{'new_username'});
  653. $$uinfo{'username'} = $$reqparm{'new_username'};
  654. $userchange = 1;
  655. }
  656. # check for valid new password
  657. my $passchange = '';
  658. $$reqparm{'disable'} = '' if ! defined $$reqparm{'disable'};
  659. if ($$reqparm{'do_edituser'} and $$reqparm{'disable'}) {
  660. $$uinfo{'password'} = '';
  661. $passchange = 1;
  662. } elsif ($$reqparm{'new_password'} or $$reqparm{'new_password1'}) {
  663. pg_error('no_changepass')
  664. if !$$reqparm{'do_edituser'} and
  665. $$uinfo{'level'} eq 'USER' and
  666. ( !$dinfo or $$dinfo{'changepass'} eq 'NO');
  667. pg_error('no_password') if !$$reqparm{'new_password'};
  668. pg_error('no_password') if !$$reqparm{'new_password1'};
  669. pg_error('not_same')
  670. if $$reqparm{'new_password'} ne $$reqparm{'new_password1'};
  671. $$uinfo{'password'} = md5_hex($$reqparm{'new_password'});
  672. $passchange = 1;
  673. }
  674. # new user/domain user already exists?
  675. if ($userchange or $domchange) {
  676. # check already exists?
  677. my $uinfo = getuser($$reqparm{'new_username'}, $$reqparm{'new_domain'});
  678. pg_error('user_exists') if $uinfo;
  679. # check for admin by same name
  680. $uinfo = getuser($$reqparm{'new_username'}, '');
  681. pg_error('restricted_user') if $uinfo;
  682. }
  683. # E-mail
  684. default_empty('new_email');
  685. if ($$userinfo{'level'} eq 'ADMIN' or
  686. $$reqparm{'do_edituser'} or
  687. $$pref{'REQUIRE_EMAIL'} ne 'YES') {
  688. pg_error('bad_email')
  689. if $$reqparm{'new_email'} ne '' and
  690. $$reqparm{'new_email'} ne $$uinfo{'email'} and
  691. !validemail($$reqparm{'new_email'});
  692. $$uinfo{'email'} = $$reqparm{'new_email'};
  693. }
  694. # Forward URL
  695. default_empty('forwardurl');
  696. $$uinfo{'forwardurl'} = $$reqparm{'forwardurl'};
  697. # check/validate wildcard flag
  698. default_NO('wildcard');
  699. $$reqparm{'wildcard'} = 'NO'
  700. if $$pref{'ALLOW_WILD'} eq 'NO' or
  701. $$pref{'ALLOW_WILD'} eq 'USER' and
  702. $$uinfo{'allowwild'} eq 'NO';
  703. $$uinfo{'wildcard'} = $$reqparm{'wildcard'};
  704. # check/validate MX value
  705. default_empty('new_MXvalue');
  706. if ($$pref{'ALLOW_MX'} eq 'NO' or
  707. $$pref{'ALLOW_MX'} eq 'USER' and
  708. $$uinfo{'allowmx'} eq 'NO') {
  709. $$reqparm{'new_MXvalue'} = '';
  710. } elsif ($$reqparm{'new_MXvalue'} ne '') {
  711. my $tinydns = '';
  712. $tinydns = $$conf{'tinydns'} if defined $$conf{'tinydns'};
  713. $tinydns = $$conf{"tinydns.$$uinfo{'domain'}"}
  714. if defined $$conf{"tinydns.$$uinfo{'domain'}"};
  715. if ( $tinydns eq 'YES' ) {
  716. $$reqparm{'new_MXvalue'} = validdotquad($$reqparm{'new_MXvalue'});
  717. pg_error('bad_MX_IP') if !$$reqparm{'new_MXvalue'};
  718. } else {
  719. $$reqparm{'new_MXvalue'} = validdomain($$reqparm{'new_MXvalue'});
  720. pg_error('bad_MX_dom') if !$$reqparm{'new_MXvalue'};
  721. }
  722. }
  723. $$uinfo{'MXvalue'} = $$reqparm{'new_MXvalue'};
  724. # check/validate MX backup flag
  725. default_NO('MXbackup');
  726. $$reqparm{'MXbackup'} = 'NO'
  727. if $$pref{'ALLOW_MX'} eq 'NO' or
  728. $$pref{'ALLOW_MX'} eq 'USER' and
  729. $$uinfo{'allowmx'} eq 'NO';
  730. $$uinfo{'MXbackup'} = $$reqparm{'MXbackup'};
  731. # editing a user as admin?
  732. if ($$reqparm{'do_edituser'}) {
  733. # check IP address
  734. default_empty('new_IPaddress');
  735. my $currentip = $$uinfo{'currentip'};
  736. $currentip = '' if $currentip eq '0.0.0.0';
  737. if ($$reqparm{'new_IPaddress'} ne $currentip) {
  738. if ($$reqparm{'new_IPaddress'} eq '') {
  739. $$uinfo{'currentip'} = '0.0.0.0';
  740. } else {
  741. $$reqparm{'new_IPaddress'} = validdotquad($$reqparm{'new_IPaddress'});
  742. pg_error('bad_IP_syntax') if !$$reqparm{'new_IPaddress'};
  743. $$uinfo{'currentip'} = $$reqparm{'new_IPaddress'};
  744. }
  745. }
  746. # wildcards allowed user by user?
  747. if ($$pref{'ALLOW_WILD'} eq 'USER') {
  748. # check/validate allow wildcard flag
  749. default_NO('allow_wildcard');
  750. $$reqparm{'allow_wildcard'} = 'NO'
  751. if $$pref{'ALLOW_MX'} ne 'USER';
  752. $$uinfo{'allowwild'} = $$reqparm{'allow_wildcard'};
  753. $$uinfo{'wildcard'} = 'NO' if $$uinfo{'allowwild'} eq 'NO';
  754. }
  755. # MX allowed user by user?
  756. if ($$pref{'ALLOW_WILD'} eq 'USER') {
  757. # check/validate allow MX flag
  758. default_NO('allow_mx');
  759. $$reqparm{'allow_mx'} = 'NO'
  760. if $$pref{'ALLOW_MX'} ne 'USER';
  761. $$uinfo{'allowmx'} = $$reqparm{'allow_mx'};
  762. if ($$uinfo{'allowmx'} eq 'NO') {
  763. $$uinfo{'MXvalue'} = '';
  764. $$uinfo{'MXbackup'} = 'NO';
  765. }
  766. }
  767. }
  768. # send updates to DNS server?
  769. needDNSupdate($oldinfo, $uinfo) if $$uinfo{'level'} ne 'ADMIN';
  770. # update database
  771. updateuser($uinfo);
  772. # editing our self? update global variable
  773. if (!$$reqparm{'do_edituser'}) {
  774. $$reqparm{'username'} = $$userinfo{'username'};
  775. $$reqparm{'password'} = $$userinfo{'password'};
  776. $$reqparm{'domain'} = $$userinfo{'domain'};
  777. }
  778. # reshow page
  779. pg_cmn_edituser($uinfo);
  780. }
  781. ########################################################################
  782. # E-mail address entry
  783. ########################################################################
  784. sub do_needemail {
  785. # ensure initialized
  786. default_empty('new_needemail');
  787. # check E-mail address
  788. pg_error('no_email') if $$reqparm{'new_needemail'} eq '';
  789. pg_error('bad_email') if !validemail($$reqparm{'new_needemail'});
  790. # check not a robot
  791. mchk_check();
  792. # send the E-mail
  793. mail_newemail(
  794. $$userinfo{'username'},
  795. $$userinfo{'domain'},
  796. $$userinfo{'password'},
  797. $$reqparm{'new_needemail'},
  798. );
  799. # reshow page
  800. pg_newemail($$reqparm{'new_needemail'});
  801. }
  802. ########################################################################
  803. # E-mail update after E-mail sent
  804. ########################################################################
  805. sub do_newemail {
  806. # split query string parameter on commas
  807. my ($username, $domain, $password, $email, $checkval) =
  808. split(/,/, $$reqparm{'newemail'});
  809. # ensure initialized
  810. $username = '' if !defined($username);
  811. $domain = '' if !defined($domain);
  812. $password = '' if !defined($password);
  813. $email = '' if !defined($email);
  814. $checkval = '' if !defined($checkval);
  815. # validate the signature
  816. my $check = md5_base64(
  817. "$username.$domain.$password.$email.$$pref{'SERVER_KEY'}"
  818. );
  819. pg_error('bad_request') if $check ne $checkval;
  820. # validate/retrieve the user
  821. my $uinfo = getuser($username, $domain);
  822. pg_error('nouser')
  823. if !$uinfo or $$uinfo{'username'} ne $username;
  824. pg_error('badpass') if $$uinfo{'password'} ne $password;
  825. # update the email
  826. $$uinfo{'email'} = $email;
  827. updateuser($uinfo);
  828. # message
  829. pg_didemail();
  830. }
  831. ########################################################################
  832. # delete users
  833. ########################################################################
  834. sub do_deluser {
  835. # must be admin
  836. pg_error('not_admin') if $$userinfo{'level'} ne 'ADMIN';
  837. # any users to delete?
  838. pg_manageusers() if !$$reqparm{'deluser'};
  839. # delete selected users
  840. foreach my $deluser (split(/\0/, $$reqparm{'deluser'})) {
  841. # get from database
  842. my $uinfo = getuserbyid($deluser);
  843. if ($uinfo) {
  844. # not admin?
  845. if ($$uinfo{'level'} eq 'USER') {
  846. # domain still exists?
  847. my $dinfo = getdomain($$uinfo{'domain'});
  848. if ($dinfo) {
  849. # remove from DNS
  850. donsupdate ($$uinfo{'domain'},
  851. "update delete $$uinfo{'username'}.$$uinfo{'domain'}.",
  852. "update delete *.$$uinfo{'username'}.$$uinfo{'domain'}.");
  853. writelog(
  854. "User $$uinfo{'username'}.$$uinfo{'domain'} complete remove from DNS (delete)");
  855. }
  856. }
  857. # update database
  858. deleteuser($uinfo);
  859. writelog(
  860. "User $$uinfo{'username'}.$$uinfo{'domain'} deleted by administrator"
  861. );
  862. }
  863. }
  864. # reshow page
  865. pg_manageusers();
  866. }
  867. ########################################################################
  868. # subroutines
  869. ########################################################################
  870. # remove autoURL cookies
  871. sub removecookies {
  872. printcookie('gnudipuser', '', '-1s');
  873. printcookie('gnudipdomain', '', '-1s');
  874. printcookie('gnudippass', '', '-1s');
  875. }
  876. # check for a restricted user
  877. sub chkrestrict {
  878. my $username = shift;
  879. # split system parameter on commas and check each
  880. foreach my $check
  881. (split(/\,/, $$pref{'RESTRICTED_USERS'})) {
  882. # massage retricted user template into valid Perl regular expression
  883. $check =~ s/\*/\(\.\*\)/g;
  884. $check =~ s/\?/\(\.\)/g;
  885. # check for a match
  886. pg_error('restricted_user') if $username =~ /^$check\b/;
  887. }
  888. }
  889. ########################################################################
  890. # must return 1
  891. ########################################################################
  892. 1;