Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2017
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.08 KB | None | 0 0
  1. /* ns_ldap.c - Allows auth via an ldap server
  2.  
  3. *
  4.  
  5. * (C) 2003-2009 Anope Team
  6.  
  7. * Contact us at team@anope.org
  8.  
  9. *
  10.  
  11. * Please read COPYING and README for further details.
  12.  
  13. *
  14.  
  15. * Send bug reports to the Anope Coder instead of the module
  16.  
  17. * author, because any changes since the inclusion into anope
  18.  
  19. * are not supported by the original author.
  20.  
  21. *
  22.  
  23. */
  24.  
  25. /*************************************************************************/
  26.  
  27.  
  28.  
  29. #include "module.h"
  30.  
  31. #include <ldap.h>
  32.  
  33. #include <vector>
  34.  
  35. #include <list>
  36.  
  37.  
  38.  
  39. #define AUTHOR "Rob"
  40.  
  41. #define VERSION "$Id$"
  42.  
  43.  
  44.  
  45. /* Default database name */
  46.  
  47. #define DEFAULT_DB_NAME "ns_ldap.db"
  48.  
  49.  
  50.  
  51. extern int allow_ignore;
  52.  
  53.  
  54.  
  55. int mLoadData();
  56.  
  57. int mLoadConfig();
  58.  
  59.  
  60.  
  61. /*************************************************************************/
  62.  
  63.  
  64.  
  65. std::string NSLdapDBName = "ns_ldap.db";
  66.  
  67.  
  68.  
  69. static Module *me;
  70.  
  71.  
  72.  
  73. /*************************************************************************/
  74.  
  75.  
  76.  
  77. class LdapSingleton
  78.  
  79. {
  80.  
  81. public:
  82.  
  83. static LdapSingleton* instance()
  84.  
  85. {
  86.  
  87. static LdapSingleton* pInstance = NULL;
  88.  
  89. if (pInstance == NULL)
  90.  
  91. {
  92.  
  93. pInstance = new LdapSingleton();
  94.  
  95. }
  96.  
  97. return pInstance;
  98.  
  99. }
  100.  
  101. virtual ~LdapSingleton() { }
  102.  
  103.  
  104.  
  105.  
  106.  
  107. std::string getLastEmail() {
  108.  
  109. return lastEmail;
  110.  
  111. }
  112.  
  113.  
  114.  
  115. bool validatePassword(std::string username, std::string password) {
  116.  
  117. bool valid = false;
  118.  
  119.  
  120.  
  121. if (con == NULL) { connectToLdap(); }
  122.  
  123. if (con == NULL) { return valid; }
  124.  
  125.  
  126.  
  127. std::vector<std::string> attrs;
  128.  
  129. attrs.push_back("email");
  130.  
  131.  
  132.  
  133. if(bindToLdap()) {
  134.  
  135. LDAPMessage *res = doLdapSearch(userDN,userPrefix+username,attrs);
  136.  
  137. if(res) {
  138.  
  139. if(ldap_count_entries(con, res) == 1) {
  140.  
  141. LDAPMessage *entry = NULL;
  142.  
  143. if ((entry = ldap_first_entry(con, res)) != NULL) {
  144.  
  145. if(doLdapAuth(username,password,entry)) {
  146.  
  147. lastEmail = getAttribute(entry,"email");
  148.  
  149. valid = true;
  150.  
  151. }
  152.  
  153. }
  154.  
  155. }
  156.  
  157. ldap_msgfree(res);
  158.  
  159. }
  160.  
  161. unbindFromLdap();
  162.  
  163. }
  164.  
  165. return valid;
  166.  
  167. }
  168.  
  169.  
  170.  
  171. std::list<std::string> getGroups(std::string uid) {
  172.  
  173. std::list<std::string> ret;
  174.  
  175.  
  176.  
  177. if (con == NULL) { connectToLdap(); }
  178.  
  179. if (con == NULL) { return ret; }
  180.  
  181.  
  182.  
  183.  
  184.  
  185. if(bindToLdap()) {
  186.  
  187. LDAPMessage *res = doLdapSearch(groupDN,groupPrefix+userPrefix+uid+","+userDN);
  188.  
  189. if(res) {
  190.  
  191. LDAPMessage *entry = NULL;
  192.  
  193. for(entry = ldap_first_entry(con, res) ; entry != NULL ; entry = ldap_next_entry(con, entry)) {
  194.  
  195. std::string group = getAttribute(entry,"cn");
  196.  
  197. if(group!="") {
  198.  
  199. ret.push_back(group);
  200.  
  201. }
  202.  
  203. }
  204.  
  205. ldap_msgfree(res);
  206.  
  207. }
  208.  
  209. unbindFromLdap();
  210.  
  211. }
  212.  
  213.  
  214.  
  215. return ret;
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. private:
  224.  
  225. LdapSingleton()
  226.  
  227. {
  228.  
  229. con = NULL;
  230.  
  231. ldapURI="ldap://";
  232.  
  233. userDN="";
  234.  
  235. groupDN="";
  236.  
  237. ldapDN="";
  238.  
  239. ldapPW="";
  240.  
  241. userPrefix="uid=";
  242.  
  243. groupPrefix="member=";
  244.  
  245. searchscope = LDAP_SCOPE_SUBTREE;
  246.  
  247. lastEmail = "";
  248.  
  249. }
  250.  
  251.  
  252.  
  253. void connectToLdap()
  254.  
  255. {
  256.  
  257. int ret = -1;
  258.  
  259. int version = LDAP_VERSION3;
  260.  
  261.  
  262.  
  263. ret = ldap_initialize(&con, ldapURI.c_str());
  264.  
  265. if (ret == LDAP_SUCCESS)
  266.  
  267. {
  268.  
  269. ret = ldap_set_option( con, LDAP_OPT_PROTOCOL_VERSION, &version );
  270.  
  271. if (ret != LDAP_OPT_SUCCESS)
  272.  
  273. {
  274.  
  275. ldap_unbind_ext(con, NULL, NULL);
  276.  
  277. con = NULL;
  278.  
  279. }
  280.  
  281. }
  282.  
  283. else
  284.  
  285. {
  286.  
  287. con = NULL;
  288.  
  289. }
  290.  
  291. }
  292.  
  293.  
  294.  
  295. bool doLdapAuth(std::string username, std::string password, LDAPMessage *entry) {
  296.  
  297. bool valid = false;
  298.  
  299. struct berval cred;
  300.  
  301.  
  302.  
  303. cred.bv_val = strdup(password.c_str());
  304.  
  305. cred.bv_len = password.length();
  306.  
  307.  
  308.  
  309. if (ldap_sasl_bind_s(con, ldap_get_dn(con, entry), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL) == LDAP_SUCCESS) {
  310.  
  311. valid = true;
  312.  
  313. } else {
  314.  
  315. valid = false;
  316.  
  317. }
  318.  
  319. free(cred.bv_val);
  320.  
  321.  
  322.  
  323. return valid;
  324.  
  325. }
  326.  
  327.  
  328.  
  329. LDAPMessage *doLdapSearch(std::string DN, std::string filter) {
  330.  
  331. LDAPMessage *res = NULL;
  332.  
  333. if ( ldap_search_ext_s(con, DN.c_str(), searchscope, filter.c_str(), NULL, 0, NULL, NULL, NULL, 0, &res) != LDAP_SUCCESS) {
  334.  
  335. res = NULL;
  336.  
  337. }
  338.  
  339. return res;
  340.  
  341. }
  342.  
  343.  
  344.  
  345. LDAPMessage *doLdapSearch(std::string DN, std::string filter, std::vector<std::string> attrs) {
  346.  
  347. LDAPMessage *res = NULL;
  348.  
  349. const int numAttrs = attrs.size();
  350.  
  351. char **c_attrs;
  352.  
  353.  
  354.  
  355. int i = 0;
  356.  
  357. c_attrs = (char **)malloc(sizeof(char *)*(numAttrs+1));
  358.  
  359. for(std::vector<std::string>::iterator it = attrs.begin() ; it != attrs.end() ; ++it) {
  360.  
  361. c_attrs[i] = sstrdup((*it).c_str());
  362.  
  363. ++i;
  364.  
  365. }
  366.  
  367. c_attrs[i] = NULL;
  368.  
  369.  
  370.  
  371. if ( ldap_search_ext_s(con, DN.c_str(), searchscope, filter.c_str(), c_attrs, 0, NULL, NULL, NULL, 0, &res) != LDAP_SUCCESS) {
  372.  
  373. res = NULL;
  374.  
  375. }
  376.  
  377.  
  378.  
  379. for(i = 0 ; i < numAttrs ; ++i) {
  380.  
  381. free(c_attrs[i]);
  382.  
  383. }
  384.  
  385. free(c_attrs);
  386.  
  387.  
  388.  
  389. return res;
  390.  
  391. }
  392.  
  393.  
  394.  
  395. bool bindToLdap() {
  396.  
  397. return bindToLdap(ldapDN,ldapPW);
  398.  
  399. }
  400.  
  401.  
  402.  
  403. bool bindToLdap(std::string bindDN, std::string password) {
  404.  
  405. struct berval cred;
  406.  
  407. bool bound = false;
  408.  
  409.  
  410.  
  411. cred.bv_len = password.length();
  412.  
  413. cred.bv_val = strdup(password.c_str());
  414.  
  415.  
  416.  
  417. if (con == NULL) { connectToLdap(); }
  418.  
  419. if (con == NULL) { return bound; }
  420.  
  421.  
  422.  
  423. if ( ldap_sasl_bind_s(con, bindDN.c_str(), LDAP_SASL_SIMPLE, &cred, NULL, NULL, NULL) == LDAP_SUCCESS) {
  424.  
  425. bound = true;
  426.  
  427. } else {
  428.  
  429. Alog() << "Search bind failed.";
  430.  
  431. }
  432.  
  433.  
  434.  
  435. if(cred.bv_val) {
  436.  
  437. free(cred.bv_val);
  438.  
  439. }
  440.  
  441. return bound;
  442.  
  443. }
  444.  
  445.  
  446.  
  447. void unbindFromLdap() {
  448.  
  449. if(con) {
  450.  
  451. ldap_unbind_ext(con, NULL, NULL);
  452.  
  453. con = NULL;
  454.  
  455. }
  456.  
  457. }
  458.  
  459.  
  460.  
  461. std::string getAttribute(LDAPMessage *e, std::string name) {
  462.  
  463. BerElement *ber;
  464.  
  465. struct berval **vals;
  466.  
  467. char *a;
  468.  
  469. std::string ret;
  470.  
  471.  
  472.  
  473. for ( a = ldap_first_attribute( con, e, &ber ) ; a != NULL; a = ldap_next_attribute( con, e, ber ) )
  474.  
  475. {
  476.  
  477. /* For each attribute, print the attribute name and values. */
  478.  
  479. if( (vals = ldap_get_values_len(con,e,a))) {
  480.  
  481. int size = ldap_count_values_len(vals);
  482.  
  483. for(int i = 0; i < size ; i++) {
  484.  
  485. if(strcmp(a,name.c_str())==0) {
  486.  
  487. ret = vals[i]->bv_val;
  488.  
  489. break;
  490.  
  491. }
  492.  
  493. }
  494.  
  495. ldap_value_free_len(vals);
  496.  
  497. }
  498.  
  499. ldap_memfree( a );
  500.  
  501. }
  502.  
  503. return ret;
  504.  
  505.  
  506.  
  507. }
  508.  
  509.  
  510.  
  511. LDAP *con;
  512.  
  513. std::string ldapURI;
  514.  
  515. std::string ldapDN;
  516.  
  517. std::string userDN;
  518.  
  519. std::string groupDN;
  520.  
  521. std::string ldapPW;
  522.  
  523. std::string userPrefix;
  524.  
  525. std::string groupPrefix;
  526.  
  527. int searchscope;
  528.  
  529.  
  530.  
  531. std::string lastEmail;
  532.  
  533. };
  534.  
  535.  
  536.  
  537.  
  538.  
  539. class CommandNSLdapUid : public Command
  540.  
  541. {
  542.  
  543. private:
  544.  
  545. CommandReturn DoSet(User *u, std::vector<std::string> &params)
  546.  
  547. {
  548.  
  549. if (params.size() != 2)
  550.  
  551. {
  552.  
  553. OnSyntaxError(u);
  554.  
  555. return MOD_CONT;
  556.  
  557. }
  558.  
  559. NickAlias *na = NULL;
  560.  
  561. if (u->IsIdentified())
  562.  
  563. {
  564.  
  565. if ((na = findnick(u->nick)))
  566.  
  567. {
  568.  
  569. char *uid = sstrdup(params[1].c_str());
  570.  
  571. if (na->nc->Extend("ns_ldap_uid", new ExtensibleItemPointerArray<char>(uid)))
  572.  
  573. {
  574.  
  575. u->SendMessage(Config.s_NickServ, "LDAP uid [%s] has been linked to this nick",uid);
  576.  
  577. }
  578.  
  579. else
  580.  
  581. {
  582.  
  583. free(uid);
  584.  
  585. u->SendMessage(Config.s_NickServ, "An LDAP uid has already been linked to this nick, please remove it first.");
  586.  
  587. }
  588.  
  589. }
  590.  
  591. else
  592.  
  593. {
  594.  
  595. notice_lang(Config.s_NickServ, u, NICK_X_NOT_REGISTERED, u->nick);
  596.  
  597. }
  598.  
  599. }
  600.  
  601. else
  602.  
  603. {
  604.  
  605. notice_lang(Config.s_NickServ,u,NICK_IDENTIFY_REQUIRED,Config.s_NickServ);
  606.  
  607. }
  608.  
  609. return MOD_CONT;
  610.  
  611. }
  612.  
  613.  
  614.  
  615. CommandReturn DoDel(User *u, std::vector<std::string> &params)
  616.  
  617. {
  618.  
  619. NickAlias *na = NULL;
  620.  
  621. char *c;
  622.  
  623. if ((na = findnick(u->nick)))
  624.  
  625. {
  626.  
  627. if (na->nc->GetExtArray("ns_ldap_uid", c))
  628.  
  629. {
  630.  
  631. delete [] c;
  632.  
  633. na->nc->Shrink("ns_ldap_uid");
  634.  
  635. u->SendMessage(Config.s_NickServ, "LDAP uid has been unlinked from this nick.");
  636.  
  637. }
  638.  
  639. else
  640.  
  641. {
  642.  
  643. u->SendMessage(Config.s_NickServ, "No LDAP uid has been linked to this nick.");
  644.  
  645. }
  646.  
  647. }
  648.  
  649. else
  650.  
  651. {
  652.  
  653. notice_lang(Config.s_NickServ, u, NICK_X_NOT_REGISTERED, u->nick);
  654.  
  655. }
  656.  
  657. return MOD_CONT;
  658.  
  659. }
  660.  
  661.  
  662.  
  663. CommandReturn DoView(User *u, std::vector<std::string> &params)
  664.  
  665. {
  666.  
  667. NickAlias *na = NULL;
  668.  
  669. char *c;
  670.  
  671. if ((na = findnick(u->nick)))
  672.  
  673. {
  674.  
  675. if (na->nc->GetExtArray("ns_ldap_uid", c))
  676.  
  677. {
  678.  
  679. u->SendMessage(Config.s_NickServ, "LDAP uid [%s] is linked to this nick.",c);
  680.  
  681. }
  682.  
  683. else
  684.  
  685. {
  686.  
  687. u->SendMessage(Config.s_NickServ, "No LDAP uid has been linked to this nick.");
  688.  
  689. }
  690.  
  691. }
  692.  
  693. else
  694.  
  695. {
  696.  
  697. notice_lang(Config.s_NickServ, u, NICK_X_NOT_REGISTERED, u->nick);
  698.  
  699. }
  700.  
  701. return MOD_CONT;
  702.  
  703. }
  704.  
  705.  
  706.  
  707. public:
  708.  
  709. CommandNSLdapUid() : Command("ldap", 1, 2)
  710.  
  711. {
  712.  
  713.  
  714.  
  715. }
  716.  
  717.  
  718.  
  719. CommandReturn Execute(User *u, std::vector<std::string> &params)
  720.  
  721. {
  722.  
  723. const char *cmd = params[0].c_str();
  724.  
  725.  
  726.  
  727. if (!strcasecmp(cmd, "SET"))
  728.  
  729. return this->DoSet(u, params);
  730.  
  731. else if (!strcasecmp(cmd, "DEL"))
  732.  
  733. return this->DoDel(u, params);
  734.  
  735. else if (!strcasecmp(cmd, "VIEW"))
  736.  
  737. return this->DoView(u,params);
  738.  
  739. else
  740.  
  741. this->OnSyntaxError(u);
  742.  
  743. return MOD_CONT;
  744.  
  745. }
  746.  
  747.  
  748.  
  749. bool OnHelp(User *u, const std::string &subcommand)
  750.  
  751. {
  752.  
  753. OnSyntaxError(u);
  754.  
  755. u->SendMessage(Config.s_NickServ, "Sets the uid for the ldap account when");
  756.  
  757. u->SendMessage(Config.s_NickServ, "using the lid command.");
  758.  
  759. u->SendMessage(Config.s_NickServ, "LDAP SET uid - Sets the user id");
  760.  
  761. u->SendMessage(Config.s_NickServ, "LDAP DEL - Clears any uid currently set");
  762.  
  763. u->SendMessage(Config.s_NickServ, "LDAP VIEW - View the current UID");
  764.  
  765. return true;
  766.  
  767. }
  768.  
  769.  
  770.  
  771. void OnSyntaxError(User *u)
  772.  
  773. {
  774.  
  775. u->SendMessage(Config.s_NickServ, "Syntax: LDAP [SET|DEL|VIEW] <ldap_uid>");
  776.  
  777. }
  778.  
  779. };
  780.  
  781.  
  782.  
  783. class CommandNSLdapId : public Command
  784.  
  785. {
  786.  
  787. private:
  788.  
  789. CommandReturn DoId(User *u, std::vector<std::string> &params)
  790.  
  791. {
  792.  
  793.  
  794.  
  795. return MOD_CONT;
  796.  
  797. }
  798.  
  799.  
  800.  
  801. public:
  802.  
  803. CommandNSLdapId(const std::string &cname) : Command(cname, 1, 1)
  804.  
  805. {
  806.  
  807. this->SetFlag(CFLAG_ALLOW_UNREGISTERED);
  808.  
  809. }
  810.  
  811.  
  812.  
  813. void NickNotRegistered(User *u, std::vector<std::string> &params)
  814.  
  815. {
  816.  
  817. NickRequest *nr = NULL;
  818.  
  819. if ((nr = findrequestnick(u->nick)))
  820.  
  821. notice_lang(Config.s_NickServ, u, NICK_IS_PREREG);
  822.  
  823. else
  824.  
  825. {
  826.  
  827. if (LdapSingleton::instance()->validatePassword(u->nick,params[0]) == 1)
  828.  
  829. {
  830.  
  831. Command *c = findCommand(NICKSERV,"register");
  832.  
  833. Alog() << "ns_ldap: Successfully identified" << u->nick << "using ldap";
  834.  
  835. if (c)
  836.  
  837. {
  838.  
  839. std::vector<std::string> param2;
  840.  
  841. param2.push_back(params[0]);
  842.  
  843. param2.push_back(LdapSingleton::instance()->getLastEmail());
  844.  
  845. int retVal = c->Execute(u, param2);
  846.  
  847. if (retVal == MOD_CONT)
  848.  
  849. {
  850.  
  851. Command *current = c->next;
  852.  
  853. while (current && retVal == MOD_CONT)
  854.  
  855. {
  856.  
  857. retVal = current->Execute(u, param2);
  858.  
  859. current = current->next;
  860.  
  861. }
  862.  
  863. }
  864.  
  865. char *uid = sstrdup(u->nick);
  866.  
  867. NickAlias *na;
  868.  
  869. if( (na = findnick(u->nick)) ) {
  870.  
  871. na->nc->Extend("ns_ldap_uid", new ExtensibleItemPointerArray<char>(uid));
  872.  
  873. OnSuccessfullID(na);
  874.  
  875. }
  876.  
  877. }
  878.  
  879. else
  880.  
  881. {
  882.  
  883. Alog() << "ns_ldap: oh-ho unable to find 'register' in the nickserv command table! is it loaded?";
  884.  
  885. }
  886.  
  887. } else {
  888.  
  889. notice_lang(Config.s_NickServ, u, NICK_NOT_REGISTERED);
  890.  
  891. }
  892.  
  893. }
  894.  
  895. }
  896.  
  897.  
  898.  
  899. CommandReturn Execute(User *u, std::vector<std::string> &params)
  900.  
  901. {
  902.  
  903. allow_ignore = 0;
  904.  
  905. char *username = NULL;
  906.  
  907. std::string password;
  908.  
  909. NickAlias *na = NULL;
  910.  
  911.  
  912.  
  913. password = params[0];
  914.  
  915.  
  916.  
  917. if ((na = findnick(u->nick)))
  918.  
  919. {
  920.  
  921. if (u->IsIdentified())
  922.  
  923. {
  924.  
  925. notice_lang(Config.s_NickServ, u, NICK_ALREADY_IDENTIFIED);
  926.  
  927. }
  928.  
  929. else
  930.  
  931. {
  932.  
  933. if (na->nc->GetExtArray("ns_ldap_uid", username))
  934.  
  935. {
  936.  
  937.  
  938.  
  939. if (LdapSingleton::instance()->validatePassword(username,password) == 1)
  940.  
  941. {
  942.  
  943. Alog() << "ns_ldap: Successfully identified " << username << " using ldap";
  944.  
  945.  
  946.  
  947. std::vector<std::string> param2;
  948.  
  949. std::string pass = na->nc->pass;
  950.  
  951. param2.push_back(pass);
  952.  
  953.  
  954.  
  955. int retVal = MOD_CONT;
  956.  
  957. Command *current = this->next;
  958.  
  959. while (current && retVal == MOD_CONT)
  960.  
  961. {
  962.  
  963. retVal = current->Execute(u, param2);
  964.  
  965. current = current->next;
  966.  
  967. }
  968.  
  969. OnSuccessfullID(na);
  970.  
  971. }
  972.  
  973. else
  974.  
  975. {
  976.  
  977. notice_lang(Config.s_NickServ, u, NICK_IDENTIFY_FAILED);
  978.  
  979. Alog() << "ns_ldap: Failed to identify " << username << " using ldap";
  980.  
  981. }
  982.  
  983. }
  984.  
  985. else { /* Let normal services ID take this one... */
  986.  
  987. int retVal = MOD_CONT;
  988.  
  989. Command *current = this->next;
  990.  
  991. while (current && retVal == MOD_CONT) {
  992.  
  993. retVal = current->Execute(u, params);
  994.  
  995. current = current->next;
  996.  
  997. }
  998.  
  999.  
  1000.  
  1001. }
  1002.  
  1003. }
  1004.  
  1005. }
  1006.  
  1007. else
  1008.  
  1009. {
  1010.  
  1011. NickNotRegistered(u,params);
  1012.  
  1013. }
  1014.  
  1015.  
  1016.  
  1017. return MOD_STOP;
  1018.  
  1019. }
  1020.  
  1021.  
  1022.  
  1023. void OnSuccessfullID(NickAlias *na) {
  1024.  
  1025. char *username = NULL;
  1026.  
  1027. na->nc->GetExtArray("ns_ldap_uid", username);
  1028.  
  1029. std::stringstream istream;
  1030.  
  1031. std::list<std::string> group = LdapSingleton::instance()->getGroups(username);
  1032.  
  1033. for(std::list<std::string>::iterator it = group.begin() ; it != group.end() ; ++it) {
  1034.  
  1035. istream << "[" << (*it) << "]";
  1036.  
  1037. }
  1038.  
  1039. std::string group_list = istream.str().c_str();
  1040.  
  1041. if(group_list != "") {
  1042.  
  1043. na->nc->Extend("ns_ldap_groups", new ExtensibleItemPointerArray<char>(sstrdup(group_list.c_str())));
  1044.  
  1045. }
  1046.  
  1047. }
  1048.  
  1049.  
  1050.  
  1051. bool OnHelp(User *u, const std::string &subcommand)
  1052.  
  1053. {
  1054.  
  1055. OnSyntaxError(u);
  1056.  
  1057. u->SendMessage(Config.s_NickServ, "Identify to this nick using your LDAP account");
  1058.  
  1059. u->SendMessage(Config.s_NickServ, "in order for this to work you must have");
  1060.  
  1061. u->SendMessage(Config.s_NickServ, "previosuly setup your luid with the ldap command.");
  1062.  
  1063. return true;
  1064.  
  1065. }
  1066.  
  1067.  
  1068.  
  1069. void OnSyntaxError(User *u)
  1070.  
  1071. {
  1072.  
  1073. u->SendMessage(Config.s_NickServ, "Syntax: LID ldap_password");
  1074.  
  1075. }
  1076.  
  1077. };
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083. class NSLdap : public Module
  1084.  
  1085. {
  1086.  
  1087. public:
  1088.  
  1089. NSLdap(const std::string &modname, const std::string &creator) : Module(modname, creator)
  1090.  
  1091. {
  1092.  
  1093. int status;
  1094.  
  1095. me = this;
  1096.  
  1097.  
  1098.  
  1099. this->SetAuthor(AUTHOR);
  1100.  
  1101. this->SetVersion(VERSION);
  1102.  
  1103. this->SetType(SUPPORTED);
  1104.  
  1105.  
  1106.  
  1107. mLoadConfig();
  1108.  
  1109.  
  1110.  
  1111. mLoadData();
  1112.  
  1113.  
  1114.  
  1115. status = this->AddCommand(NICKSERV, new CommandNSLdapUid());
  1116.  
  1117. status = this->AddCommand(NICKSERV, new CommandNSLdapId("id"));
  1118.  
  1119. status = this->AddCommand(NICKSERV, new CommandNSLdapId("identify"));
  1120.  
  1121.  
  1122.  
  1123. ModuleManager::Attach(I_OnSaveDatabase, this);
  1124.  
  1125. ModuleManager::Attach(I_OnReload, this);
  1126.  
  1127. }
  1128.  
  1129.  
  1130.  
  1131. ~NSLdap() {
  1132.  
  1133.  
  1134.  
  1135. }
  1136.  
  1137.  
  1138.  
  1139. void OnReload(bool starting)
  1140.  
  1141. {
  1142.  
  1143. Alog() << "ns_ldap: Reloading configuration directives...";
  1144.  
  1145. int ret = mLoadConfig();
  1146.  
  1147.  
  1148.  
  1149. if (ret)
  1150.  
  1151. Alog() << "ns_ldap: ERROR: An error has occured while reloading the configuration file";
  1152.  
  1153. }
  1154.  
  1155.  
  1156.  
  1157. void OnSaveDatabase() {
  1158.  
  1159. NickCore *nc = NULL;
  1160.  
  1161. int i = 0;
  1162.  
  1163. FILE *out;
  1164.  
  1165. if (!(out = fopen(NSLdapDBName.c_str(), "w"))) {
  1166.  
  1167. Alog() << "os_info: ERROR: can not open the database file!";
  1168.  
  1169. ircdproto->SendGlobops(findbot(Config.s_OperServ), "ns_ldap: ERROR: can not open the database file (%s)!",NSLdapDBName.c_str());
  1170.  
  1171. } else {
  1172.  
  1173. for (i = 0; i < 1024; ++i) {
  1174.  
  1175. for (nc = nclists[i]; nc; nc = nc->next) {
  1176.  
  1177. char *c;
  1178.  
  1179. if (nc->GetExtArray("ns_ldap_uid", c))
  1180.  
  1181. fprintf(out, "%s %s\n", nc->display, c);
  1182.  
  1183. }
  1184.  
  1185. }
  1186.  
  1187. fclose(out);
  1188.  
  1189. }
  1190.  
  1191. }
  1192.  
  1193. };
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199. /**
  1200.  
  1201. * Stolen from os_info, should really make this better.
  1202.  
  1203. **/
  1204.  
  1205. int mLoadData() {
  1206.  
  1207. int ret = 0;
  1208.  
  1209. FILE *in;
  1210.  
  1211. NickAlias *na = NULL;
  1212.  
  1213. char *nick = NULL;
  1214.  
  1215. char *uid = NULL;
  1216.  
  1217. char buffer[2000];
  1218.  
  1219. if (!(in = fopen(NSLdapDBName.c_str(), "r"))) {
  1220.  
  1221. ret = 1;
  1222.  
  1223. } else {
  1224.  
  1225. while (fgets(buffer, 1500, in)) {
  1226.  
  1227. if( (nick = myStrGetToken(buffer, ' ', 0)) ) {
  1228.  
  1229. if( (uid = myStrGetToken(buffer, ' ', 1)) ) {
  1230.  
  1231. int len = strlen(uid);
  1232.  
  1233. uid[len - 1] = '\0';
  1234.  
  1235. if ((na = findnick(nick))) {
  1236.  
  1237. Alog() << "Added Nick [" << nick << "] Uid [" << uid << "]";
  1238.  
  1239. na->nc->Extend("ns_ldap_uid", new ExtensibleItemPointerArray<char>(strdup(uid)));
  1240.  
  1241. }
  1242.  
  1243. delete [] uid;
  1244.  
  1245. }
  1246.  
  1247. delete [] nick;
  1248.  
  1249. }
  1250.  
  1251.  
  1252.  
  1253. }
  1254.  
  1255. fclose(in);
  1256.  
  1257. }
  1258.  
  1259. return ret;
  1260.  
  1261. }
  1262.  
  1263.  
  1264.  
  1265. int mLoadConfig()
  1266.  
  1267. {
  1268.  
  1269. ConfigReader config;
  1270.  
  1271. NSLdapDBName = config.ReadValue("ns_ldap", "database", "ns_ldap.db", 0);
  1272.  
  1273. Alog() << "ns_ldap: Directive NSLdapDBName loaded (" << NSLdapDBName.c_str() << ")...";
  1274.  
  1275. return 0;
  1276.  
  1277. }
  1278.  
  1279.  
  1280.  
  1281. MODULE_INIT(NSLdap)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement