comparison dcclib/parse_word.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 /* Distributed Checksum Clearinghouse
2 *
3 * Copyright (c) 2008 by Rhyolite Software, LLC
4 *
5 * This agreement is not applicable to any entity which sells anti-spam
6 * solutions to others or provides an anti-spam solution as part of a
7 * security solution sold to other entities, or to a private network
8 * which employs the DCC or uses data provided by operation of the DCC
9 * but does not provide corresponding data to other users.
10 *
11 * Permission to use, copy, modify, and distribute this software without
12 * changes for any purpose with or without fee is hereby granted, provided
13 * that the above copyright notice and this permission notice appear in all
14 * copies and any distributed versions or copies are either unchanged
15 * or not called anything similar to "DCC" or "Distributed Checksum
16 * Clearinghouse".
17 *
18 * Parties not eligible to receive a license under this agreement can
19 * obtain a commercial license to use DCC by contacting Rhyolite Software
20 * at sales@rhyolite.com.
21 *
22 * A commercial license would be for Distributed Checksum and Reputation
23 * Clearinghouse software. That software includes additional features. This
24 * free license for Distributed ChecksumClearinghouse Software does not in any
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse
26 * software
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
35 * SOFTWARE.
36 *
37 * Rhyolite Software DCC 1.3.103-1.24 $Revision$
38 */
39
40 #include "dcc_defs.h"
41
42
43 /* Get a "word" ended by whitespace, while honoring backslashes
44 * on exit the remainder of the line is trimmed of leading whitespace */
45 const char * /* 0 or remainder of line */
46 dcc_parse_word(DCC_EMSG emsg,
47 char *tgt, /* copy word to here if not null */
48 int tgt_len, /* includes trailing '\0' */
49 const char *line, /* line of words */
50 const char *fieldname,
51 const char *fnm, int lno)
52 {
53 DCC_FNM_LNO_BUF fnm_buf;
54 const char *p;
55 char c;
56
57 if (!tgt_len && tgt)
58 dcc_logbad(EX_SOFTWARE, "bad tgt_len for dcc_get_word(%s)%s",
59 fieldname, fnm_lno(&fnm_buf, fnm, lno));
60
61 if (!line) {
62 if (tgt)
63 *tgt = '\0';
64 if (fieldname)
65 dcc_pemsg(EX_USAGE, emsg, "%s missing%s",
66 fieldname, fnm_lno(&fnm_buf, fnm, lno));
67 return 0;
68 }
69
70 line = line+strspn(line, DCC_WHITESPACE); /* skip leading blanks */
71
72 p = line;
73 do {
74 c = *p;
75
76 if (c != '\0') {
77 ++p;
78 if (c == '\\' && *p != '\0') {
79 /* recognize and convert escape sequences to
80 * their real equivalents */
81 if ((c = *p++) == 'n') {
82 c = '\n';
83 } else if (c == 'r') {
84 c = '\r';
85 } else if (c == 't') {
86 c = '\t';
87 } else if (c == 'b') {
88 c = '\b';
89 } else if (c >= '0' && c <= '7') {
90 c -= '0';
91 if (*p >= '0' && *p <= '7') {
92 c = (c<<3)+(*p++ - '0');
93 if (*p >= '0' && *p <= '7')
94 c = (c<<3)+(*p++ - '0');
95 }
96 }
97 } else if (strchr(DCC_WHITESPACE, c)) {
98 /* Stop on the first whitespace or delimiter */
99 c = '\0';
100 /* Skip trailing whitespace */
101 p += strspn(p, DCC_WHITESPACE);
102 }
103 }
104
105 if (tgt) {
106 if (!tgt_len) {
107 if (fieldname)
108 dcc_pemsg(EX_USAGE, emsg,
109 "%s \"%.*s...\" too long%s",
110 fieldname,
111 min(16, (int)(p-line)), line,
112 fnm_lno(&fnm_buf, fnm, lno));
113 return 0;
114 }
115 *tgt++ = c;
116 --tgt_len;
117 }
118 } while (c != '\0');
119
120 return p;
121 }
122
123
124
125 /* get a password */
126 const char * /* 0 or remainder of line */
127 parse_passwd(DCC_EMSG emsg,
128 DCC_PASSWD passwd, /* copy password here */
129 const char *line, /* line of words */
130 const char *fieldname,
131 const char *fnm, int lno)
132 {
133 char buf[sizeof(DCC_PASSWD)+1];
134 const char *result;
135
136 memset(buf, 0, sizeof(buf));
137 result = dcc_parse_word(emsg, buf, sizeof(buf),
138 line, fieldname, fnm, lno);
139 if (result)
140 memcpy(passwd, buf, sizeof(DCC_PASSWD));
141 else
142 memset(passwd, 0, sizeof(DCC_PASSWD));
143 return result;
144 }
145
146
147
148 /* see if a string starts with a word possibly followed by a comma */
149 u_char /* 1=yes */
150 dcc_ck_word_comma(const char **parg, const char *word)
151 {
152 u_int word_len = strlen(word);
153 const char *arg = *parg;
154
155 if (strncasecmp(arg, word, word_len))
156 return 0;
157 arg += word_len;
158 if (*arg == '\0') {
159 *parg = arg;
160 return 1;
161 }
162 if (*arg == ',') {
163 *parg = arg+1;
164 return 1;
165 }
166 return 0;
167 }