Skip to content

Commit cea1ff7

Browse files
pks-tgitster
authored andcommitted
imap-send: drop global imap_server_conf variable
In "imap-send.c", we have a global `sturct imap_server_conf` variable that keeps track of the configuration of the IMAP server. This variable is being populated mostly via the Git configuration. Refactor the code to allocate the structure on the stack instead of having it globally. This change allows us to track its lifetime more closely. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c777560 commit cea1ff7

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

imap-send.c

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ struct imap_server_conf {
8282
char *auth_method;
8383
};
8484

85-
static struct imap_server_conf server = {
86-
.ssl_verify = 1,
87-
};
88-
8985
struct imap_socket {
9086
int fd[2];
9187
SSL *ssl;
@@ -110,6 +106,7 @@ struct imap {
110106
};
111107

112108
struct imap_store {
109+
const struct imap_server_conf *cfg;
113110
/* currently open mailbox */
114111
const char *name; /* foreign! maybe preset? */
115112
int uidvalidity;
@@ -194,8 +191,8 @@ static void socket_perror(const char *func, struct imap_socket *sock, int ret)
194191

195192
#ifdef NO_OPENSSL
196193
static int ssl_socket_connect(struct imap_socket *sock UNUSED,
197-
int use_tls_only UNUSED,
198-
int verify UNUSED)
194+
const struct imap_server_conf *cfg,
195+
int use_tls_only UNUSED)
199196
{
200197
fprintf(stderr, "SSL requested but SSL support not compiled in\n");
201198
return -1;
@@ -250,7 +247,9 @@ static int verify_hostname(X509 *cert, const char *hostname)
250247
cname, hostname);
251248
}
252249

253-
static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int verify)
250+
static int ssl_socket_connect(struct imap_socket *sock,
251+
const struct imap_server_conf *cfg,
252+
int use_tls_only)
254253
{
255254
#if (OPENSSL_VERSION_NUMBER >= 0x10000000L)
256255
const SSL_METHOD *meth;
@@ -279,7 +278,7 @@ static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int ve
279278
if (use_tls_only)
280279
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
281280

282-
if (verify)
281+
if (cfg->ssl_verify)
283282
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
284283

285284
if (!SSL_CTX_set_default_verify_paths(ctx)) {
@@ -306,9 +305,9 @@ static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int ve
306305
* OpenSSL does not document this function, but the implementation
307306
* returns 1 on success, 0 on failure after calling SSLerr().
308307
*/
309-
ret = SSL_set_tlsext_host_name(sock->ssl, server.host);
308+
ret = SSL_set_tlsext_host_name(sock->ssl, cfg->host);
310309
if (ret != 1)
311-
warning("SSL_set_tlsext_host_name(%s) failed.", server.host);
310+
warning("SSL_set_tlsext_host_name(%s) failed.", cfg->host);
312311
#endif
313312

314313
ret = SSL_connect(sock->ssl);
@@ -317,12 +316,12 @@ static int ssl_socket_connect(struct imap_socket *sock, int use_tls_only, int ve
317316
return -1;
318317
}
319318

320-
if (verify) {
319+
if (cfg->ssl_verify) {
321320
/* make sure the hostname matches that of the certificate */
322321
cert = SSL_get_peer_certificate(sock->ssl);
323322
if (!cert)
324323
return error("unable to get peer certificate.");
325-
if (verify_hostname(cert, server.host) < 0)
324+
if (verify_hostname(cert, cfg->host) < 0)
326325
return -1;
327326
}
328327

@@ -895,7 +894,7 @@ static int auth_cram_md5(struct imap_store *ctx, const char *prompt)
895894
int ret;
896895
char *response;
897896

898-
response = cram(prompt, server.user, server.pass);
897+
response = cram(prompt, ctx->cfg->user, ctx->cfg->pass);
899898

900899
ret = socket_write(&ctx->imap->buf.sock, response, strlen(response));
901900
if (ret != strlen(response))
@@ -935,6 +934,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c
935934

936935
CALLOC_ARRAY(ctx, 1);
937936

937+
ctx->cfg = srvc;
938938
ctx->imap = CALLOC_ARRAY(imap, 1);
939939
imap->buf.sock.fd[0] = imap->buf.sock.fd[1] = -1;
940940
imap->in_progress_append = &imap->in_progress;
@@ -1035,7 +1035,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c
10351035
imap->buf.sock.fd[1] = dup(s);
10361036

10371037
if (srvc->use_ssl &&
1038-
ssl_socket_connect(&imap->buf.sock, 0, srvc->ssl_verify)) {
1038+
ssl_socket_connect(&imap->buf.sock, srvc, 0)) {
10391039
close(s);
10401040
goto bail;
10411041
}
@@ -1068,8 +1068,7 @@ static struct imap_store *imap_open_store(struct imap_server_conf *srvc, const c
10681068
if (!srvc->use_ssl && CAP(STARTTLS)) {
10691069
if (imap_exec(ctx, NULL, "STARTTLS") != RESP_OK)
10701070
goto bail;
1071-
if (ssl_socket_connect(&imap->buf.sock, 1,
1072-
srvc->ssl_verify))
1071+
if (ssl_socket_connect(&imap->buf.sock, srvc, 1))
10731072
goto bail;
10741073
/* capabilities may have changed, so get the new capabilities */
10751074
if (imap_exec(ctx, NULL, "CAPABILITY") != RESP_OK)
@@ -1299,23 +1298,24 @@ static int split_msg(struct strbuf *all_msgs, struct strbuf *msg, int *ofs)
12991298
static int git_imap_config(const char *var, const char *val,
13001299
const struct config_context *ctx, void *cb)
13011300
{
1301+
struct imap_server_conf *cfg = cb;
13021302

13031303
if (!strcmp("imap.sslverify", var))
1304-
server.ssl_verify = git_config_bool(var, val);
1304+
cfg->ssl_verify = git_config_bool(var, val);
13051305
else if (!strcmp("imap.preformattedhtml", var))
1306-
server.use_html = git_config_bool(var, val);
1306+
cfg->use_html = git_config_bool(var, val);
13071307
else if (!strcmp("imap.folder", var))
1308-
return git_config_string(&server.folder, var, val);
1308+
return git_config_string(&cfg->folder, var, val);
13091309
else if (!strcmp("imap.user", var))
1310-
return git_config_string(&server.user, var, val);
1310+
return git_config_string(&cfg->user, var, val);
13111311
else if (!strcmp("imap.pass", var))
1312-
return git_config_string(&server.pass, var, val);
1312+
return git_config_string(&cfg->pass, var, val);
13131313
else if (!strcmp("imap.tunnel", var))
1314-
return git_config_string(&server.tunnel, var, val);
1314+
return git_config_string(&cfg->tunnel, var, val);
13151315
else if (!strcmp("imap.authmethod", var))
1316-
return git_config_string(&server.auth_method, var, val);
1316+
return git_config_string(&cfg->auth_method, var, val);
13171317
else if (!strcmp("imap.port", var))
1318-
server.port = git_config_int(var, val, ctx->kvi);
1318+
cfg->port = git_config_int(var, val, ctx->kvi);
13191319
else if (!strcmp("imap.host", var)) {
13201320
if (!val) {
13211321
return config_error_nonbool(var);
@@ -1324,11 +1324,11 @@ static int git_imap_config(const char *var, const char *val,
13241324
val += 5;
13251325
else if (starts_with(val, "imaps:")) {
13261326
val += 6;
1327-
server.use_ssl = 1;
1327+
cfg->use_ssl = 1;
13281328
}
13291329
if (starts_with(val, "//"))
13301330
val += 2;
1331-
server.host = xstrdup(val);
1331+
cfg->host = xstrdup(val);
13321332
}
13331333
} else
13341334
return git_default_config(var, val, ctx, cb);
@@ -1497,12 +1497,15 @@ static int curl_append_msgs_to_imap(struct imap_server_conf *server,
14971497

14981498
int cmd_main(int argc, const char **argv)
14991499
{
1500+
struct imap_server_conf server = {
1501+
.ssl_verify = 1,
1502+
};
15001503
struct strbuf all_msgs = STRBUF_INIT;
15011504
int total;
15021505
int nongit_ok;
15031506

15041507
setup_git_directory_gently(&nongit_ok);
1505-
git_config(git_imap_config, NULL);
1508+
git_config(git_imap_config, &server);
15061509

15071510
argc = parse_options(argc, (const char **)argv, "", imap_send_options, imap_send_usage, 0);
15081511

0 commit comments

Comments
 (0)