2 * Routines for imap packet dissection
3 * Copyright 1999, Richard Sharpe <rsharpe@ns.aus.com>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * Copied from packet-tftp.c
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <epan/packet.h>
31 #include <epan/strutil.h>
32 #include <epan/wmem/wmem.h>
33 #include "packet-ssl.h"
38 static int proto_imap
= -1;
39 static int hf_imap_isrequest
= -1;
40 static int hf_imap_line
= -1;
41 static int hf_imap_request
= -1;
42 static int hf_imap_request_tag
= -1;
43 static int hf_imap_response
= -1;
44 static int hf_imap_response_tag
= -1;
45 static int hf_imap_request_command
= -1;
46 static int hf_imap_response_status
= -1;
47 static int hf_imap_request_folder
= -1;
48 static int hf_imap_request_uid
= -1;
50 static gint ett_imap
= -1;
51 static gint ett_imap_reqresp
= -1;
53 static dissector_handle_t imap_handle
;
55 #define TCP_PORT_IMAP 143
56 #define TCP_PORT_SSL_IMAP 993
57 #define MAX_BUFFER 1024
60 dissect_imap(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
)
63 proto_tree
*imap_tree
, *reqresp_tree
;
64 proto_item
*ti
, *hidden_item
;
67 gint folder_offset
= 0;
69 const guchar
*uid_line
;
70 const guchar
*folder_line
;
76 const guchar
*next_token
;
77 const guchar
*uid_next_token
;
78 const guchar
*folder_next_token
;
80 guchar
*command_token
;
84 tokenbuf
= (guchar
*)wmem_alloc(wmem_packet_scope(), MAX_BUFFER
);
85 command_token
= (guchar
*)wmem_alloc(wmem_packet_scope(), MAX_BUFFER
);
86 memset(tokenbuf
, '\0', MAX_BUFFER
);
87 memset(command_token
, '\0', MAX_BUFFER
);
92 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "IMAP");
95 if (pinfo
->match_uint
== pinfo
->destport
)
101 * Put the first line from the buffer into the summary
102 * (but leave out the line terminator).
104 linelen
= tvb_find_line_end(tvb
, offset
, -1, &next_offset
, FALSE
);
105 line
= tvb_get_ptr(tvb
, offset
, linelen
);
107 col_add_fstr(pinfo
->cinfo
, COL_INFO
, "%s: %s",
108 is_request
? "Request" : "Response",
109 format_text(line
, linelen
));
112 ti
= proto_tree_add_item(tree
, proto_imap
, tvb
, offset
, -1, ENC_NA
);
113 imap_tree
= proto_item_add_subtree(ti
, ett_imap
);
115 hidden_item
= proto_tree_add_boolean(imap_tree
, hf_imap_isrequest
, tvb
, 0, 0, is_request
);
116 PROTO_ITEM_SET_HIDDEN(hidden_item
);
118 while(tvb_length_remaining(tvb
, offset
) > 0) {
121 * Find the end of each line
123 * Note that "tvb_find_line_end()" will return a value that is
124 * not longer than what's in the buffer, so the "tvb_get_ptr()"
125 * call won't throw an exception.
127 linelen
= tvb_find_line_end(tvb
, offset
, -1, &next_offset
, FALSE
);
128 line
= tvb_get_ptr(tvb
, offset
, linelen
);
131 * Put the line into the protocol tree.
133 ti
= proto_tree_add_item(imap_tree
, hf_imap_line
, tvb
, offset
,
134 next_offset
- offset
, ENC_ASCII
|ENC_NA
);
136 reqresp_tree
= proto_item_add_subtree(ti
, ett_imap_reqresp
);
139 * Check that the line doesn't begin with '*', because that's a continuation line.
140 * Otherwise if a tag is present then extract tokens.
142 if ( (line
) && ((line
[0] != '*') || (TRUE
== is_request
)) ) {
144 * Show each line as tags + requests or replies.
148 * Extract the first token, and, if there is a first
149 * token, add it as the request or reply tag.
151 tokenlen
= get_token_len(line
, line
+ linelen
, &next_token
);
153 proto_tree_add_item(reqresp_tree
, (is_request
) ? hf_imap_request_tag
: hf_imap_response_tag
,
154 tvb
, offset
, tokenlen
, ENC_ASCII
|ENC_NA
);
156 offset
+= (gint
) (next_token
- line
);
157 linelen
-= (int) (next_token
- line
);
162 * Extract second token, and, if there is a second
163 * token, and it's not uid, add it as the request or reply command.
165 tokenlen
= get_token_len(line
, line
+ linelen
, &next_token
);
167 for (iter
= 0; iter
< tokenlen
&& iter
< MAX_BUFFER
-1; iter
++) {
168 tokenbuf
[iter
] = tolower(line
[iter
]);
170 if ( TRUE
== is_request
&& strncmp(tokenbuf
,"uid",tokenlen
) == 0) {
171 proto_tree_add_item(reqresp_tree
, hf_imap_request_uid
, tvb
, offset
, tokenlen
, ENC_ASCII
|ENC_NA
);
173 * UID is a precursor to a command, if following the tag,
174 * so move to next token to grab the actual command.
177 uid_offset
+= (gint
) (next_token
- line
);
178 uid_line
= next_token
;
179 uid_tokenlen
= get_token_len(uid_line
, uid_line
+ (linelen
- tokenlen
), &uid_next_token
);
181 proto_tree_add_item(reqresp_tree
, hf_imap_request_command
,
182 tvb
, uid_offset
, uid_tokenlen
, ENC_ASCII
|ENC_NA
);
185 * Save command string to do specialized processing.
187 for (iter
= 0; iter
< uid_tokenlen
&& iter
< MAX_BUFFER
-1; iter
++) {
188 command_token
[iter
] = tolower(uid_line
[iter
]);
190 commandlen
= uid_tokenlen
;
192 folder_offset
= uid_offset
;
193 folder_offset
+= (gint
) (uid_next_token
- uid_line
);
194 folder_line
= uid_next_token
;
195 folder_tokenlen
= get_token_len(folder_line
, folder_line
+ (linelen
- tokenlen
- uid_tokenlen
), &folder_next_token
);
199 * Not a UID request so perform normal parsing.
201 proto_tree_add_item(reqresp_tree
, (is_request
) ? hf_imap_request_command
: hf_imap_response_status
,
202 tvb
, offset
, tokenlen
, ENC_ASCII
|ENC_NA
);
206 * Save command string to do specialized processing.
208 for (iter
= 0; iter
< tokenlen
&& iter
< 256; iter
++) {
209 command_token
[iter
] = tolower(line
[iter
]);
211 commandlen
= tokenlen
;
213 folder_offset
= offset
;
214 folder_offset
+= (gint
) (next_token
- line
);
215 folder_line
= next_token
;
216 folder_tokenlen
= get_token_len(folder_line
, folder_line
+ (linelen
- tokenlen
- 1), &folder_next_token
);
220 if (commandlen
> 0 && (
221 strncmp(command_token
, "select", commandlen
) == 0 ||
222 strncmp(command_token
, "examine", commandlen
) == 0 ||
223 strncmp(command_token
, "create", commandlen
) == 0 ||
224 strncmp(command_token
, "delete", commandlen
) == 0 ||
225 strncmp(command_token
, "rename", commandlen
) == 0 ||
226 strncmp(command_token
, "subscribe", commandlen
) == 0 ||
227 strncmp(command_token
, "unsubscribe", commandlen
) == 0 ||
228 strncmp(command_token
, "status", commandlen
) == 0 ||
229 strncmp(command_token
, "append", commandlen
) == 0 ||
230 strncmp(command_token
, "search", commandlen
) == 0)) {
232 * These commands support folder as an argument,
233 * so parse out the folder name.
235 if (folder_tokenlen
!= 0)
236 proto_tree_add_item(reqresp_tree
, hf_imap_request_folder
, tvb
, folder_offset
, folder_tokenlen
, ENC_ASCII
|ENC_NA
);
239 if ( is_request
&& (NULL
!= folder_line
) &&
240 strncmp(command_token
, "copy", commandlen
) == 0) {
242 * Handle the copy command separately since folder
243 * is the second argument for this command.
245 folder_offset
+= (gint
) (folder_next_token
- folder_line
);
246 folder_line
= folder_next_token
;
247 folder_tokenlen
= get_token_len(folder_line
, folder_line
+ (linelen
- tokenlen
), &folder_next_token
);
249 if (folder_tokenlen
!= 0)
250 proto_tree_add_item(reqresp_tree
, hf_imap_request_folder
, tvb
, folder_offset
, folder_tokenlen
, ENC_ASCII
|ENC_NA
);
256 * Add the rest of the line as request or reply data.
259 proto_tree_add_item(reqresp_tree
, (is_request
) ? hf_imap_request
: hf_imap_response
,
260 tvb
, offset
, linelen
, ENC_ASCII
|ENC_NA
);
265 offset
= next_offset
; /* Skip over last line and \r\n at the end of it */
271 proto_register_imap(void)
273 static hf_register_info hf
[] = {
274 { &hf_imap_isrequest
, { "Request", "imap.isrequest", FT_BOOLEAN
, BASE_NONE
, NULL
, 0x0, "TRUE if IMAP request, FALSE otherwise", HFILL
}},
275 { &hf_imap_line
, { "Line", "imap.line", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "A line of an IMAP message", HFILL
}},
276 { &hf_imap_request
, { "Request", "imap.request", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "Remainder of request line", HFILL
}},
277 { &hf_imap_request_tag
, { "Request Tag", "imap.request_tag", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "First token of request line", HFILL
}},
278 { &hf_imap_response
, { "Response", "imap.response", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "Remainder of response line", HFILL
}},
279 { &hf_imap_response_tag
, { "Response Tag", "imap.response_tag", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "First token of response line", HFILL
}},
280 { &hf_imap_request_command
, { "Request Command", "imap.request.command", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "Request command name", HFILL
}},
281 { &hf_imap_response_status
, { "Response Status", "imap.response.status", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "Response status code", HFILL
}},
282 { &hf_imap_request_folder
, { "Request Folder", "imap.request.folder", FT_STRINGZ
, BASE_NONE
, NULL
, 0x0, "Request command folder", HFILL
}},
283 { &hf_imap_request_uid
, { "Request isUID", "imap.request.command.uid", FT_BOOLEAN
, BASE_NONE
, NULL
, 0x0, "Request command uid", HFILL
}}
286 static gint
*ett
[] = {
291 proto_imap
= proto_register_protocol("Internet Message Access Protocol",
294 imap_handle
= register_dissector("imap", dissect_imap
, proto_imap
);
296 proto_register_field_array(proto_imap
, hf
, array_length(hf
));
297 proto_register_subtree_array(ett
, array_length(ett
));
301 proto_reg_handoff_imap(void)
303 dissector_add_uint("tcp.port", TCP_PORT_IMAP
, imap_handle
);
304 ssl_dissector_add(TCP_PORT_SSL_IMAP
, "imap", TRUE
);