ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/conf_db.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/conf_db.c (file contents):
Revision 1621 by michael, Wed Oct 31 23:11:40 2012 UTC vs.
Revision 1622 by michael, Thu Nov 1 13:16:37 2012 UTC

# Line 32 | Line 32
32   #include "log.h"
33   #include "send.h"
34   #include "irc_string.h"
35 + #include "conf.h"
36 + #include "hostmask.h"
37 + #include "resv.h"
38  
39  
40   /*! \brief Return the version number on the file.  Return 0 if there is no version
# Line 101 | Line 104 | open_db_read(const char *service, const
104      int errno_save = errno;
105  
106      if (errno != ENOENT)
107 <      ilog(LOG_TYPE_IRCD, "Can not read %s database %s", service,
107 >      ilog(LOG_TYPE_IRCD, "Can't read %s database %s", service,
108             f->filename);
109  
110      MyFree(f);
# Line 157 | Line 160 | open_db_write(const char *service, const
160  
161      if (!walloped++)
162        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
163 <                           "Can not back up %s database %s",
163 >                           "Can't back up %s database %s",
164                             service, filename);
165  
166      errno = errno_save;
167 <    ilog(LOG_TYPE_IRCD, "Can not back up %s database %s", service, filename);
167 >    ilog(LOG_TYPE_IRCD, "Can't back up %s database %s", service, filename);
168  
169      if (f->backupfp)
170        fclose(f->backupfp);
# Line 198 | Line 201 | open_db_write(const char *service, const
201      }
202  
203      if (f->backupname[0] && rename(f->backupname, filename) < 0)
204 <      ilog(LOG_TYPE_IRCD, "Cannot restore backup copy of %s",
204 >      ilog(LOG_TYPE_IRCD, "Can't restore backup copy of %s",
205             filename);
206  
207       MyFree(f);
# Line 592 | Line 595 | write_string(const char *s, struct dbFIL
595  
596    return 0;
597   }
598 +
599 + #define SAFE_READ(x) do {                             \
600 +    if ((x) < 0) {                                    \
601 +        break;                                        \
602 +    }                                                 \
603 + } while (0)
604 +
605 + #define SAFE_WRITE(x,db) do {                         \
606 +    if ((x) < 0) {                                    \
607 +        restore_db(f);                                \
608 +        ilog(LOG_TYPE_IRCD, "Write error on %s", db); \
609 +        return;                                       \
610 +    }                                                 \
611 + } while (0)
612 +
613 + void
614 + save_kline_database(void)
615 + {
616 +  unsigned int i = 0;
617 +  uint32_t cnt = 0, version = 0;
618 +  struct dbFILE *f = NULL;
619 +  dlink_node *ptr = NULL;
620 +
621 +  if (!(f = open_db("kline", KPATH, "w", KLINE_DB_VERSION)))
622 +    return;
623 +
624 +  for (i = 0; i < ATABLE_SIZE; ++i)
625 +  {
626 +    DLINK_FOREACH(ptr, atable[i].head)
627 +    {
628 +      struct AddressRec *arec = ptr->data;
629 +
630 +      if (arec->type == CONF_KLINE && !IsConfMain(arec->aconf))
631 +        ++cnt;
632 +    }
633 +  }
634 +
635 +  SAFE_WRITE(write_uint32(cnt, f), KPATH);
636 +
637 +  for (i = 0; i < ATABLE_SIZE; ++i)
638 +  {
639 +    DLINK_FOREACH(ptr, atable[i].head)
640 +    {
641 +      struct AddressRec *arec = ptr->data;
642 +
643 +      if (arec->type == CONF_KLINE && !IsConfMain(arec->aconf))
644 +      {
645 +        SAFE_WRITE(write_string(arec->aconf->user, f), KPATH);
646 +        SAFE_WRITE(write_string(arec->aconf->host, f), KPATH);
647 +        SAFE_WRITE(write_string(arec->aconf->reason, f), KPATH);
648 +        SAFE_WRITE(write_uint64(arec->aconf->setat, f), KPATH);
649 +        SAFE_WRITE(write_uint64(arec->aconf->hold, f), KPATH);
650 +      }
651 +    }
652 +  }
653 +
654 +  close_db(f);
655 + }
656 +
657 + void
658 + load_kline_database(void)
659 + {
660 +  unsigned int i = 0;
661 +  uint32_t cnt = 0, version = 0;
662 +  uint64_t tmp64 = 0;
663 +  struct dbFILE *f = NULL;
664 +  dlink_node *ptr = NULL, *ptr_next = NULL;
665 +
666 +  if (!(f = open_db("kline", KPATH, "r", KLINE_DB_VERSION)))
667 +    return;
668 +
669 +  if ((version = get_file_version(f) < 1))
670 +  {
671 +    close_db(f);
672 +    return;
673 +  }
674 +
675 +  read_uint32(&cnt, f);
676 +
677 +  for (i = 0; i < cnt; ++i)
678 +  {
679 +    struct AccessItem *aconf = map_to_conf(make_conf_item(KLINE_TYPE));
680 +
681 +    SAFE_READ(read_string(&aconf->user, f));
682 +    SAFE_READ(read_string(&aconf->host, f));
683 +    SAFE_READ(read_string(&aconf->reason, f));
684 +
685 +    SAFE_READ(read_uint64(&tmp64, f));
686 +    aconf->setat = tmp64;
687 +
688 +    SAFE_READ(read_uint64(&tmp64, f));
689 +    aconf->hold = tmp64;
690 +
691 +    if (aconf->hold)
692 +      SetConfTemporary(aconf);
693 +
694 +    add_conf_by_address(CONF_KLINE, aconf);
695 +  }
696 +
697 +  close_db(f);
698 + }
699 +
700 + void
701 + save_dline_database(void)
702 + {
703 +  unsigned int i = 0;
704 +  uint32_t cnt = 0, version = 0;
705 +  struct dbFILE *f = NULL;
706 +  dlink_node *ptr = NULL;
707 +
708 +  if (!(f = open_db("dline", DLPATH, "w", KLINE_DB_VERSION)))
709 +    return;
710 +
711 +  for (i = 0; i < ATABLE_SIZE; ++i)
712 +  {
713 +    DLINK_FOREACH(ptr, atable[i].head)
714 +    {
715 +      struct AddressRec *arec = ptr->data;
716 +
717 +      if (arec->type == CONF_DLINE && !IsConfMain(arec->aconf))
718 +        ++cnt;
719 +    }
720 +  }
721 +
722 +  SAFE_WRITE(write_uint32(cnt, f), DLPATH);
723 +
724 +  for (i = 0; i < ATABLE_SIZE; ++i)
725 +  {
726 +    DLINK_FOREACH(ptr, atable[i].head)
727 +    {
728 +      struct AddressRec *arec = ptr->data;
729 +
730 +      if (arec->type == CONF_DLINE && !IsConfMain(arec->aconf))
731 +      {
732 +        SAFE_WRITE(write_string(arec->aconf->host, f), DLPATH);
733 +        SAFE_WRITE(write_string(arec->aconf->reason, f), DLPATH);
734 +        SAFE_WRITE(write_uint64(arec->aconf->setat, f), DLPATH);
735 +        SAFE_WRITE(write_uint64(arec->aconf->hold, f), DLPATH);
736 +      }
737 +    }
738 +  }
739 +
740 +  close_db(f);
741 + }
742 +
743 + void
744 + load_dline_database(void)
745 + {
746 +  unsigned int i = 0;
747 +  uint32_t cnt = 0, version = 0;
748 +  uint64_t tmp64 = 0;
749 +  struct dbFILE *f = NULL;
750 +  dlink_node *ptr = NULL, *ptr_next = NULL;
751 +
752 +  if (!(f = open_db("dline", DLPATH, "r", KLINE_DB_VERSION)))
753 +    return;
754 +
755 +  if ((version = get_file_version(f) < 1))
756 +  {
757 +    close_db(f);
758 +    return;
759 +  }
760 +
761 +  read_uint32(&cnt, f);
762 +
763 +  for (i = 0; i < cnt; ++i)
764 +  {
765 +    struct AccessItem *aconf = map_to_conf(make_conf_item(DLINE_TYPE));
766 +
767 +    SAFE_READ(read_string(&aconf->host, f));
768 +    SAFE_READ(read_string(&aconf->reason, f));
769 +
770 +    SAFE_READ(read_uint64(&tmp64, f));
771 +    aconf->setat = tmp64;
772 +
773 +    SAFE_READ(read_uint64(&tmp64, f));
774 +    aconf->hold = tmp64;
775 +
776 +    if (aconf->hold)
777 +      SetConfTemporary(aconf);
778 +
779 +    add_conf_by_address(CONF_DLINE, aconf);
780 +  }
781 +
782 +  close_db(f);
783 + }
784 +
785 + void
786 + save_gline_database(void)
787 + {
788 +  unsigned int i = 0;
789 +  uint32_t cnt = 0, version = 0;
790 +  struct dbFILE *f = NULL;
791 +  dlink_node *ptr = NULL;
792 +
793 +  if (!(f = open_db("gline", GPATH, "w", KLINE_DB_VERSION)))
794 +    return;
795 +
796 +  for (i = 0; i < ATABLE_SIZE; ++i)
797 +  {
798 +    DLINK_FOREACH(ptr, atable[i].head)
799 +    {
800 +      struct AddressRec *arec = ptr->data;
801 +
802 +      if (arec->type == CONF_GLINE && !IsConfMain(arec->aconf))
803 +        ++cnt;
804 +    }
805 +  }
806 +
807 +  SAFE_WRITE(write_uint32(cnt, f), GPATH);
808 +
809 +  for (i = 0; i < ATABLE_SIZE; ++i)
810 +  {
811 +    DLINK_FOREACH(ptr, atable[i].head)
812 +    {
813 +      struct AddressRec *arec = ptr->data;
814 +
815 +      if (arec->type == CONF_GLINE && !IsConfMain(arec->aconf))
816 +      {
817 +        SAFE_WRITE(write_string(arec->aconf->user, f), GPATH);
818 +        SAFE_WRITE(write_string(arec->aconf->host, f), GPATH);
819 +        SAFE_WRITE(write_string(arec->aconf->reason, f), GPATH);
820 +        SAFE_WRITE(write_uint64(arec->aconf->setat, f), GPATH);
821 +        SAFE_WRITE(write_uint64(arec->aconf->hold, f), GPATH);
822 +      }
823 +    }
824 +  }
825 +
826 +  close_db(f);
827 + }
828 +
829 + void
830 + load_gline_database(void)
831 + {
832 +  unsigned int i = 0;
833 +  uint32_t cnt = 0, version = 0;
834 +  uint64_t tmp64 = 0;
835 +  struct dbFILE *f = NULL;
836 +  dlink_node *ptr = NULL, *ptr_next = NULL;
837 +
838 +  if (!(f = open_db("gline", GPATH, "r", KLINE_DB_VERSION)))
839 +    return;
840 +
841 +  if ((version = get_file_version(f) < 1))
842 +  {
843 +    close_db(f);
844 +    return;
845 +  }
846 +
847 +  read_uint32(&cnt, f);
848 +
849 +  for (i = 0; i < cnt; ++i)
850 +  {
851 +    struct AccessItem *aconf = map_to_conf(make_conf_item(GLINE_TYPE));
852 +
853 +    SAFE_READ(read_string(&aconf->user, f));
854 +    SAFE_READ(read_string(&aconf->host, f));
855 +    SAFE_READ(read_string(&aconf->reason, f));
856 +
857 +    SAFE_READ(read_uint64(&tmp64, f));
858 +    aconf->setat = tmp64;
859 +
860 +    SAFE_READ(read_uint64(&tmp64, f));
861 +    aconf->hold = tmp64;
862 +
863 +    if (aconf->hold)
864 +      SetConfTemporary(aconf);
865 +
866 +    add_conf_by_address(CONF_GLINE, aconf);
867 +  }
868 +
869 +  close_db(f);
870 + }
871 +
872 + void
873 + save_resv_database(void)
874 + {
875 +  unsigned int i = 0;
876 +  uint32_t cnt = 0, version = 0;
877 +  struct dbFILE *f = NULL;
878 +  dlink_node *ptr = NULL;
879 +  struct ConfItem *conf;
880 +  struct ResvChannel *resv_cp;
881 +  struct MatchItem *resv_np;
882 +
883 +  if (!(f = open_db("resv", RESVPATH, "w", KLINE_DB_VERSION)))
884 +    return;
885 +
886 +  DLINK_FOREACH(ptr, resv_channel_list.head)
887 +  {
888 +    resv_cp = ptr->data;
889 +
890 +    if (!resv_cp->conf)
891 +      ++cnt;
892 +  }
893 +
894 +  DLINK_FOREACH(ptr, nresv_items.head)
895 +  {
896 +    resv_np = map_to_conf(ptr->data);
897 +
898 +    if (!resv_np->action)
899 +      ++cnt;
900 +  }
901 +
902 +  SAFE_WRITE(write_uint32(cnt, f), RESVPATH);
903 +
904 +
905 +  DLINK_FOREACH(ptr, resv_channel_list.head)
906 +  {
907 +    resv_cp = ptr->data;
908 +
909 +    SAFE_WRITE(write_string(resv_cp->name, f), RESVPATH);
910 +    SAFE_WRITE(write_string(resv_cp->reason, f), RESVPATH);
911 +    SAFE_WRITE(write_uint64(resv_cp->setat, f), RESVPATH);
912 +    SAFE_WRITE(write_uint64(resv_cp->hold, f), RESVPATH);
913 +  }
914 +
915 +  DLINK_FOREACH(ptr, nresv_items.head)
916 +  {
917 +    conf = ptr->data;
918 +    resv_np = map_to_conf(conf);
919 +
920 +    SAFE_WRITE(write_string(conf->name, f), RESVPATH);
921 +    SAFE_WRITE(write_string(resv_np->reason, f), RESVPATH);
922 +    SAFE_WRITE(write_uint64(resv_np->setat, f), RESVPATH);
923 +    SAFE_WRITE(write_uint64(resv_np->hold, f), RESVPATH);
924 +  }
925 +
926 +  close_db(f);
927 + }
928 +
929 + void
930 + load_resv_database(void)
931 + {
932 +  unsigned int i = 0;
933 +  uint32_t cnt = 0, version = 0;
934 +  uint64_t tmp64_hold = 0, tmp64_setat = 0;
935 +  struct dbFILE *f = NULL;
936 +  dlink_node *ptr = NULL, *ptr_next = NULL;
937 +  char *name = NULL;
938 +  char *reason = NULL;
939 +  struct ConfItem *conf;
940 +  struct ResvChannel *resv_cp;
941 +  struct MatchItem *resv_np;
942 +
943 +  if (!(f = open_db("resv", RESVPATH, "r", KLINE_DB_VERSION)))
944 +    return;
945 +
946 +  if ((version = get_file_version(f) < 1))
947 +  {
948 +    close_db(f);
949 +    return;
950 +  }
951 +
952 +  read_uint32(&cnt, f);
953 +
954 +  for (i = 0; i < cnt; ++i)
955 +  {
956 +    SAFE_READ(read_string(&name, f));
957 +    SAFE_READ(read_string(&reason, f));
958 +    SAFE_READ(read_uint64(&tmp64_setat, f));
959 +    SAFE_READ(read_uint64(&tmp64_hold, f));
960 +
961 +    if (IsChanPrefix(*name))
962 +    {
963 +      if ((conf = create_channel_resv(name, reason, 0)) == NULL)
964 +        continue;
965 +
966 +      resv_cp = map_to_conf(conf);
967 +      resv_cp->setat = tmp64_setat;
968 +      resv_cp->hold = tmp64_hold;
969 +
970 +      if (resv_cp->hold)
971 +        add_temp_line(conf);
972 +    }
973 +    else
974 +    {
975 +      if ((conf = create_nick_resv(name, reason, 0)) == NULL)
976 +        continue;
977 +
978 +      resv_np = map_to_conf(conf);
979 +      resv_np->setat = tmp64_setat;
980 +      resv_np->hold = tmp64_hold;
981 +
982 +      if (resv_np->hold)
983 +        add_temp_line(conf);
984 +    }
985 +
986 +    MyFree(name);
987 +    MyFree(reason);
988 +  }
989 +
990 +  close_db(f);
991 + }
992 +
993 + void
994 + save_xline_database(void)
995 + {
996 +  unsigned int i = 0;
997 +  uint32_t cnt = 0, version = 0;
998 +  struct dbFILE *f = NULL;
999 +  dlink_node *ptr = NULL;
1000 +  struct ConfItem *conf = NULL;
1001 +  struct MatchItem *xconf = NULL;
1002 +
1003 +  if (!(f = open_db("xline", XPATH, "w", KLINE_DB_VERSION)))
1004 +    return;
1005 +
1006 +  DLINK_FOREACH(ptr, xconf_items.head)
1007 +  {
1008 +    conf = ptr->data;
1009 +
1010 +    if (!IsConfMain(conf))
1011 +      ++cnt;
1012 +  }
1013 +
1014 +  SAFE_WRITE(write_uint32(cnt, f), XPATH);
1015 +
1016 +
1017 +  DLINK_FOREACH(ptr, xconf_items.head)
1018 +  {
1019 +    conf = ptr->data;
1020 +    xconf = map_to_conf(conf);
1021 +
1022 +    SAFE_WRITE(write_string(conf->name, f), XPATH);
1023 +    SAFE_WRITE(write_string(xconf->reason, f), XPATH);
1024 +    SAFE_WRITE(write_uint64(xconf->setat, f), XPATH);
1025 +    SAFE_WRITE(write_uint64(xconf->hold, f), XPATH);
1026 +  }
1027 +
1028 +  close_db(f);
1029 + }
1030 +
1031 + void
1032 + load_xline_database(void)
1033 + {
1034 +  unsigned int i = 0;
1035 +  uint32_t cnt = 0, version = 0;
1036 +  uint64_t tmp64_hold = 0, tmp64_setat = 0;
1037 +  struct dbFILE *f = NULL;
1038 +  dlink_node *ptr = NULL, *ptr_next = NULL;
1039 +  char *name = NULL;
1040 +  char *reason = NULL;
1041 +  struct ConfItem *conf = NULL;
1042 +  struct MatchItem *xconf = NULL;
1043 +
1044 +  if (!(f = open_db("xline", XPATH, "r", KLINE_DB_VERSION)))
1045 +    return;
1046 +
1047 +  if ((version = get_file_version(f) < 1))
1048 +  {
1049 +    close_db(f);
1050 +    return;
1051 +  }
1052 +
1053 +  read_uint32(&cnt, f);
1054 +
1055 +  for (i = 0; i < cnt; ++i)
1056 +  {
1057 +    SAFE_READ(read_string(&name, f));
1058 +    SAFE_READ(read_string(&reason, f));
1059 +    SAFE_READ(read_uint64(&tmp64_setat, f));
1060 +    SAFE_READ(read_uint64(&tmp64_hold, f));
1061 +
1062 +    conf = make_conf_item(XLINE_TYPE);
1063 +    xconf = map_to_conf(conf);
1064 +
1065 +    conf->name = name;
1066 +    xconf->reason = reason;
1067 +    xconf->setat = tmp64_setat;
1068 +    xconf->hold = tmp64_hold;
1069 +
1070 +    if (xconf->hold)
1071 +      SetConfTemporary(conf);
1072 +  }
1073 +
1074 +  close_db(f);
1075 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines