Clamav杀毒软件源码分析笔记[三]

 
  Clamav杀毒软件源码分析笔记[三]


刺猬@http://blog.csdn.net/littlehedgehog





[获取配置文件信息]


说来比较打击积极性的是我们还得做一番比较麻烦的但是却又没多少挑战性的事业,因为如果clamd这个服务端全部都用命令行来指定信息的话,Clamav估计会没有市场,因为打字就会累死管理员。我们要设置比如像多久升级一次啊,升级服务器在哪里下载这些东西这些乱七八糟的东西,我们都要给程序指定。去看看配置文件吧,想想看如果那些都要靠人来输入,这容易让人抓狂。但是配置文件这个是必须要看的。我们捡一个例子来说:



  1. # Maximal size of the log file.
  2. # Value of 0 disables the limit.
  3. # You may use 'M' or 'm' for megabytes (1M = 1m = 1048576 bytes)
  4. # and 'K' or 'k' for kilobytes (1K = 1k = 1024 bytes). To specify the size
  5. # in bytes just don't use modifiers.
  6. # Default: 1M
  7. #LogFileMaxSize 2M

就看最后一行了吧,这里是设置log日志文件的大小,参数就是LogFileMaxSize,而参数值就是2M,中间用空格隔开。这里设置log文件因为clamd是后台作业的,它就像哑巴一样,没有终端可以提供给它打印信息。当然它在后台还是可以打开文件的,所以就让它往log文件里面写内容,我们事后来查


这里是分析的主函数,贴着方便分析。


  1. //分析配置文件 message表示是否要显示信息
  2. struct cfgstruct *parsecfg(const char *cfgfile, int messages)
  3. {
  4.     char buff[LINE_LENGTH], *name, *arg;
  5.     FILE *fs;
  6.     int line = 0, i, found, ctype, calc;
  7.     struct cfgstruct *copt = NULL;
  8.     struct cfgoption *pt;
  9.     struct cfgoption cfg_options[] =
  10.     {
  11.         {"LogFile", OPT_STR
  12.         },
  13.         {"LogFileUnlock", OPT_NOARG},
  14.         {"LogFileMaxSize", OPT_COMPSIZE},
  15.         {"LogTime", OPT_NOARG},
  16.         {"LogClean", OPT_NOARG},
  17.         {"LogVerbose", OPT_NOARG}, /* clamd + freshclam */
  18.         {"LogSyslog", OPT_NOARG},
  19.         {"LogFacility", OPT_STR},
  20.         {"PidFile", OPT_STR},
  21.         {"TemporaryDirectory", OPT_STR},
  22.         {"DisableDefaultScanOptions", OPT_NOARG},
  23.         {"ScanPE", OPT_NOARG},
  24.         {"DetectBrokenExecutables", OPT_NOARG},
  25.         {"ScanMail", OPT_NOARG},
  26.         {"MailFollowURLs", OPT_NOARG},
  27.         {"ScanHTML", OPT_NOARG},
  28.         {"ScanOLE2", OPT_NOARG},
  29.         {"ScanArchive", OPT_NOARG},
  30.         {"ScanRAR", OPT_NOARG},
  31.         {"ArchiveMaxFileSize", OPT_COMPSIZE},
  32.         {"ArchiveMaxRecursion", OPT_NUM},
  33.         {"ArchiveMaxFiles", OPT_NUM},
  34.         {"ArchiveMaxCompressionRatio", OPT_NUM},
  35.         {"ArchiveLimitMemoryUsage", OPT_NOARG},
  36.         {"ArchiveBlockEncrypted", OPT_NOARG},
  37.         {"ArchiveBlockMax", OPT_NOARG},
  38.         {"DataDirectory", OPT_STR}, /* obsolete */
  39.         {"DatabaseDirectory", OPT_STR}, /* clamd + freshclam */
  40.         {"TCPAddr", OPT_STR},
  41.         {"TCPSocket", OPT_NUM},
  42.         {"LocalSocket", OPT_STR},
  43.         {"MaxConnectionQueueLength", OPT_NUM},
  44.         {"StreamMaxLength", OPT_COMPSIZE},
  45.         {"StreamMinPort", OPT_NUM},
  46.         {"StreamMaxPort", OPT_NUM},
  47.         {"MaxThreads", OPT_NUM},
  48.         {"ReadTimeout", OPT_NUM},
  49.         {"IdleTimeout", OPT_NUM},
  50.         {"MaxDirectoryRecursion", OPT_NUM},
  51.         {"FollowDirectorySymlinks", OPT_NOARG},
  52.         {"FollowFileSymlinks", OPT_NOARG},
  53.         {"ExitOnOOM", OPT_NOARG},
  54.         {"Foreground", OPT_NOARG}, /* clamd + freshclam */
  55.         {"Debug", OPT_NOARG},
  56.         {"LeaveTemporaryFiles", OPT_NOARG},
  57.         {"FixStaleSocket", OPT_NOARG},
  58.         {"User", OPT_STR},
  59.         {"AllowSupplementaryGroups", OPT_NOARG},
  60.         {"SelfCheck", OPT_NUM},
  61.         {"VirusEvent", OPT_FULLSTR},
  62.         {"ClamukoScanOnLine", OPT_NOARG}, /* old name */
  63.         {"ClamukoScanOnAccess", OPT_NOARG},
  64.         {"ClamukoScanOnOpen", OPT_NOARG},
  65.         {"ClamukoScanOnClose", OPT_NOARG},
  66.         {"ClamukoScanOnExec", OPT_NOARG},
  67.         {"ClamukoIncludePath", OPT_STR},
  68.         {"ClamukoExcludePath", OPT_STR},
  69.         {"ClamukoMaxFileSize", OPT_COMPSIZE},
  70.         {"ClamukoScanArchive", OPT_NOARG},
  71.         {"DatabaseOwner", OPT_STR}, /* freshclam */
  72.         {"Checks", OPT_NUM}, /* freshclam */
  73.         {"UpdateLogFile", OPT_STR}, /* freshclam */
  74.         {"DNSDatabaseInfo", OPT_STR}, /* freshclam */
  75.         {"DatabaseMirror", OPT_STR}, /* freshclam */
  76.         {"MaxAttempts", OPT_NUM}, /* freshclam */
  77.         {"HTTPProxyServer", OPT_STR}, /* freshclam */
  78.         {"HTTPProxyPort", OPT_NUM}, /* freshclam */
  79.         {"HTTPProxyUsername", OPT_STR}, /* freshclam */
  80.         {"HTTPProxyPassword", OPT_STR}, /* freshclam */
  81.         {"NotifyClamd", OPT_OPTARG}, /* freshclam */
  82.         {"OnUpdateExecute", OPT_FULLSTR}, /* freshclam */
  83.         {"OnErrorExecute", OPT_FULLSTR}, /* freshclam */
  84.         {"LocalIPAddress", OPT_STR}, /* freshclam */
  85.         {0, 0}
  86.     };
  87.     if ((fs = fopen(cfgfile, "r")) == NULL)
  88.         return NULL;
  89.     while (fgets(buff, LINE_LENGTH, fs)) //每次读一行
  90.     {
  91.         line++;
  92.         if (buff[0] == '#')
  93.             continue;
  94.         if (!strncmp("Example", buff, 7))   //clamd.conf 里面本来有一个example,提供给用户照着样子设置  同时就要求用户设置好了后要把example注释掉(前面加一个#)
  95.         {
  96.             if (messages)
  97.                 fprintf(stderr, "ERROR: Please edit the example config file %s./n", cfgfile);
  98.             fclose(fs);
  99.             return NULL;
  100.         }
  101.         if ((name = cli_strtok(buff, 0, " /r/n")))
  102.         {
  103.             arg = cli_strtok(buff, 1, " /r/n");
  104.             found = 0;
  105.             for (i = 0; ; i++)
  106.             {
  107.                 pt = &cfg_options[i];       //从选项数组中依次寻找
  108.                 if (pt->name)
  109.                 {
  110.                     if (!strcmp(name, pt->name))
  111.                     {
  112.                         found = 1;
  113.                         switch (pt->argtype)
  114.                         {
  115.                         case OPT_STR:       //字符串参数
  116.                             if (!arg)
  117.                             {
  118.                                 if (messages)
  119.                                     fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires string as argument./n", line, name);
  120.                                 fclose(fs);
  121.                                 return NULL;
  122.                             }
  123.                             copt = regcfg(copt, name, arg, 0);
  124.                             break;
  125.                         case OPT_FULLSTR:   //占一行的字符串参数 
  126.                             if (!arg)
  127.                             {
  128.                                 if (messages)
  129.                                     fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires string as argument./n", line, name);
  130.                                 fclose(fs);
  131.                                 return NULL;
  132.                             }
  133.                             /* FIXME: this one is an ugly hack of the above case */
  134.                             free(arg);
  135.                             arg = strstr(buff, " ");
  136.                             arg = strdup(++arg);
  137.                             copt = regcfg(copt, name, arg, 0);
  138.                             break;
  139.                         case OPT_NUM:       //参数是数字
  140.                             if (!arg || !isnumb(arg))
  141.                             {
  142.                                 if (messages)
  143.                                     fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires numerical argument./n", line, name);
  144.                                 fclose(fs);
  145.                                 return NULL;
  146.                             }
  147.                             copt = regcfg(copt, name, NULL, atoi(arg));
  148.                             free(arg);
  149.                             break;
  150.                         case OPT_COMPSIZE:      //表示转换Kb和Mb到byte  这里是处理用户设置的一些文件大小选项
  151.                             if (!arg)
  152.                             {
  153.                                 if (messages)
  154.                                     fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires argument./n", line, name);
  155.                                 fclose(fs);
  156.                                 return NULL;
  157.                             }
  158.                             ctype = tolower(arg[strlen(arg) - 1]);      //全设置为小写 方便下面比较
  159.                             if (ctype == 'm' || ctype == 'k')
  160.                             {
  161.                                 char *cpy = (char *) mcalloc(strlen(arg), sizeof(char));
  162.                                 strncpy(cpy, arg, strlen(arg) - 1);
  163.                                 if (!isnumb(cpy))       //如果不是数字
  164.                                 {
  165.                                     if (messages)
  166.                                         fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires numerical (raw/K/M) argument./n", line, name);
  167.                                     fclose(fs);
  168.                                     return NULL;
  169.                                 }
  170.                                 if (ctype == 'm')   //mb转化为b
  171.                                     calc = atoi(cpy) * 1024 * 1024;
  172.                                 else
  173.                                     calc = atoi(cpy) * 1024;
  174.                                 free(cpy);
  175.                             }
  176.                             else
  177.                             {
  178.                                 if (!isnumb(arg))
  179.                                 {
  180.                                     if (messages)
  181.                                         fprintf(stderr, "ERROR: Parse error at line %d: Option %s requires numerical (raw/K/M) argument./n", line, name);
  182.                                     fclose(fs);
  183.                                     return NULL;
  184.                                 }
  185.                                 calc = atoi(arg);
  186.                             }
  187.                             copt = regcfg(copt, name, NULL, calc);
  188.                             free(arg);
  189.                             break;
  190.                         case OPT_NOARG:
  191.                             if (arg)
  192.                             {
  193.                                 if (messages)
  194.                                     fprintf(stderr, "ERROR: Parse error at line %d: Option %s doesn't support arguments (got '%s')./n", line, name, arg);
  195.                                 fclose(fs);
  196.                                 return NULL;
  197.                             }
  198.                             copt = regcfg(copt, name, NULL, 0);
  199.                             break;
  200.                         case OPT_OPTARG:
  201.                             copt = regcfg(copt, name, arg, 0);
  202.                             break;
  203.                         default:
  204.                             if (messages)
  205.                                 fprintf(stderr, "ERROR: Parse error at line %d: Option %s is of unknown type %d/n", line, name, pt->argtype);
  206.                             free(name);
  207.                             free(arg);
  208.                             break;
  209.                         }
  210.                     }
  211.                 }
  212.                 else
  213.                     break;
  214.             }
  215.             if (!found)
  216.             {
  217.                 if (messages)
  218.                     fprintf(stderr, "ERROR: Parse error at line %d: Unknown option %s./n", line, name);
  219.                 fclose(fs);
  220.                 return NULL;
  221.             }
  222.         }
  223.     }
  224.     fclose(fs);
  225.     return copt;
  226. }


这里作者在解析配置文件时用了自己的一个函数:char *cli_strtok(const char *line, int fieldno, const char *delim) 注意这个函数的用意是指在line中查找第fieldno个指定字符分解符delim,这里的delim是个字符串。为什么不用strtok呢,因为strtok只能提供分界字符,而这里我们需要的是多个可能的分界符。仍举刚才的例子: LogFileMaxSize 2M ,这里我们要挖掘出参数LogFileMaxSize ,参数值2M,中间可能是隔一个空格,也可能是其它字符,比如’/r'等,所以我们传递进去分界符是" /r/n". 看看源代码吧



  1. /*
  2. * char *cli_strok(const char *line, int fieldno, char *delim)
  3. * Return a copy of field <fieldno> from the string <line>, where
  4. * fields are delimited by any char from <delim>, or NULL if <line>
  5. * doesn't have <fieldno> fields or not enough memory is available.
  6. * The caller has to free() the result afterwards.
  7. */
  8. char *cli_strtok(const char *line, int fieldno, const char *delim)
  9. {
  10.     int counter = 0, i, j;
  11.     char *buffer = NULL;


  12.     /* step to arg # <fieldno> */
  13.     for (i=0; line[i] && counter != fieldno; i++)   //count 是计数器 这个其实不说也知道
  14.     {
  15.         if (strchr(delim, line[i]))         //注意 是在delim里面查找当前字符line[i]
  16.         {
  17.             counter++;
  18.             while(line[i+1] && strchr(delim, line[i+1]))        //这里是过滤多个 比如你连续输入了多个空格
  19.             {
  20.                 i++;
  21.             }
  22.         }
  23.     }
  24.     if (!line[i]) 
  25.     {
  26.         /* end of buffer before field reached */
  27.         return NULL;
  28.     }

  29.     for (j=i; line[j]; j++)         //这里的i已经是空格后面一个字符了 这里我们是为了选取从这个空格到下个空格之间的字符串
  30.     {
  31.         if (strchr(delim, line[j]))
  32.         {
  33.             break;
  34.         }
  35.     }
  36.     if (i == j) 
  37.     {
  38.         return NULL;
  39.     }
  40.     buffer = malloc(j-i+1);     //开辟空间
  41.     if(!buffer)
  42.         return NULL;
  43.     strncpy(buffer, line+i, j-i);
  44.     buffer[j-i] = '/0';

  45.     return buffer;
  46. }


同样的在处理配置文件时候我们也是采用了链表的形式,把参数和参数值以及参数类别加入节点在组装成链表. 来先看看节点,这个稍有不同.

  1. struct cfgstruct
  2. {
  3.     char *optname;      //选项名
  4.     char *strarg;       //参数(字符串)
  5.     int numarg;         //参数(数字)  参数有字符串和数字两种形式 所以我们要分开装
  6.     struct cfgstruct *nextarg;      //下一个参数值
  7.     struct cfgstruct *next;         //下一个参数    这两者又是什么关系?  I hear you ask  注意 一个参数可能有多个参数值 所以这里作者这样设置
  8.     /*
  9.         --->(参数类型一)--->(参数类型二)--->(参数类型三)....
  10.             |
  11.             |
  12.             参数一
  13.             |
  14.             |
  15.             参数二
  16.             可以这么理解 next是连接参数主干的  而nextarg是分支
  17.     */
  18. };

因为一个参数可能有多个参数值,所以形成这种变态的树形结构. 但是无伤大雅的,我们接着看如何把节点加入链表,作者命名为register,比较专业...

  1. struct cfgstruct *regcfg(struct cfgstruct *copt, char *optname, char *strarg, int numarg)
  2. {
  3.     struct cfgstruct *newnode, *pt;
  4.     newnode = (struct cfgstruct *) mmalloc(sizeof(struct cfgstruct));
  5.     newnode->optname = optname;
  6.     newnode->nextarg = NULL;
  7.     newnode->next = NULL;
  8.     /*下面是根据不同类型参数值(数字还是字符串 To be or Not to be ?)*/
  9.     if (strarg)
  10.         newnode->strarg = strarg;
  11.     else
  12.     {
  13.         newnode->strarg = NULL;
  14.         newnode->numarg = numarg;
  15.     }
  16.     if ((pt = cfgopt(copt, optname)))   //在链表中搜索
  17.     {
  18.         while (pt->nextarg)             //如果找到了一个和该参数相同类型的节点  那我们加入到它的nextarg中去  在分支上做文章
  19.             pt = pt->nextarg;
  20.         pt->nextarg = newnode;
  21.         return copt;
  22.     }
  23.     else                                //没找到 在链表主干上添上一笔
  24.     {
  25.         newnode->next = copt;
  26.         return newnode;                 //特别要注意这个返回值  它表明我们这个链表是个栈链表  也就是所caller里面的那个copt永远都是指向链表开头的  这也就是为什么我们能搜索的原因(cfpopt)
  27.     }
  28. }


我最后专门强调了这个返回值,因为这个是一个单链表,我们必须要把握住链表的头部,俗话说擒贼先擒王嘛. 比如这里我们要搜索,那肯定要从头部开始了.




  1. struct cfgstruct *cfgopt(const struct cfgstruct *copt, const char *optname)
  2. {
  3.     struct cfgstruct *handler;

  4.     handler = (struct cfgstruct *) copt;        //从链表开头搜索  
  5.     /*为了找到这个参数 我们不惜用上了死循环*/
  6.     while (1)
  7.     {
  8.         if (handler)
  9.         {
  10.             if (handler->optname)
  11.                 if (!strcmp(handler->optname, optname))     //参数名相同 表示找到了
  12.                     return handler;
  13.         }
  14.         else
  15.             break;      //链表为空 说明一个问题——到头了
  16.         handler = handler->next;
  17.     }

  18.     return NULL;
  19. }




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值