Commit c4264da4 c4264da48bb3c6f2989522d71cc9e915535ae9cd by Sergey Poznyakoff

Improved error logging. Fixed memory leak due to absence of mysql_close()

calls (noticeable on imap4d when doing multiple login attempts).
Provide configurable parameters to mysql_real_reconnect to allow connecting
to the remote database and/or non-standard ports.
1 parent fe6d6e0e
...@@ -40,26 +40,46 @@ getMpwnam (const char *username) ...@@ -40,26 +40,46 @@ getMpwnam (const char *username)
40 if (!m) 40 if (!m)
41 return NULL; 41 return NULL;
42 42
43 if (!mysql_real_connect (m, NULL, MUSER, MPASS, MDB, 0, NULL, 0)) 43 if (!mysql_real_connect (m, MHOST, MUSER, MPASS, MDB, MPORT,
44 return NULL; 44 MSOCKET, MFLAGS))
45 45 {
46 mu_error ("MySQL: connect failed: %s", mysql_error (m));
47 mysql_close (m);
48 return NULL;
49 }
50
46 asprintf (&QueryStr, 51 asprintf (&QueryStr,
47 "select %s,%s,%s,%s,%s from %s where %s = '%s'", 52 "select %s,%s,%s,%s,%s from %s where %s = '%s'",
48 Mpassword, Muid, Mgid, Mhomedir, Mshell, Mtable, 53 Mpassword, Muid, Mgid, Mhomedir, Mshell, Mtable,
49 Musername, username); 54 Musername, username);
50 55
51 if (!QueryStr) 56 if (!QueryStr)
52 return NULL; 57 {
58 mysql_close (m);
59 return NULL;
60 }
53 61
54 if (mysql_query (m, QueryStr) != 0) 62 if (mysql_query (m, QueryStr) != 0)
55 return NULL; 63 {
64 mu_error ("MySQL: query failed: %s", mysql_error (m));
65 mysql_close (m);
66 return NULL;
67 }
56 68
57 if ((res = mysql_store_result (m)) == NULL) 69 if ((res = mysql_store_result (m)) == NULL)
58 return NULL; 70 {
59 71 mu_error ("MySQL: can't store result: %s", mysql_error (m));
72 mysql_close (m);
73 return NULL;
74 }
75
60 if ((row = mysql_fetch_row (res)) == NULL) 76 if ((row = mysql_fetch_row (res)) == NULL)
61 return NULL; 77 {
62 78 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
79 mysql_close (m);
80 return NULL;
81 }
82
63 if (!tpw) 83 if (!tpw)
64 tpw = (struct passwd *)xmalloc (sizeof (struct passwd)); 84 tpw = (struct passwd *)xmalloc (sizeof (struct passwd));
65 tpw->pw_name = xmalloc (strlen (username)+1); 85 tpw->pw_name = xmalloc (strlen (username)+1);
...@@ -80,11 +100,11 @@ getMpwnam (const char *username) ...@@ -80,11 +100,11 @@ getMpwnam (const char *username)
80 tpw->pw_shell = xmalloc (strlen (row[4])+1); 100 tpw->pw_shell = xmalloc (strlen (row[4])+1);
81 strcpy (tpw->pw_shell, row[4]); 101 strcpy (tpw->pw_shell, row[4]);
82 102
83 mysql_free_result (res); 103 mysql_free_result (res);
104 mysql_close (m);
84 return tpw; 105 return tpw;
85 } 106 }
86 107
87
88 #ifdef HAVE_SHADOW_H 108 #ifdef HAVE_SHADOW_H
89 109
90 struct spwd * 110 struct spwd *
...@@ -107,21 +127,38 @@ getMspnam (const char *username) ...@@ -107,21 +127,38 @@ getMspnam (const char *username)
107 if (!m) 127 if (!m)
108 return NULL; 128 return NULL;
109 129
110 if (!mysql_real_connect (m, NULL, MUSER, MPASS, MDB, 0, NULL, 0)) 130 if (!mysql_real_connect (m, MHOST, MUSER, MPASS, MDB, MPORT,
111 return NULL; 131 MSOCKET, MFLAGS))
132 {
133 mu_error ("MySQL: connect failed: %s", mysql_error (m));
134 mysql_close (m);
135 return NULL;
136 }
112 137
113 asprintf (&QueryStr, 138 asprintf (&QueryStr,
114 "select %s from %s where %s = '%s'", 139 "select %s from %s where %s = '%s'",
115 Mpassword, Mtable, Musername, username); 140 Mpassword, Mtable, Musername, username);
116 141
117 if (mysql_query (m, QueryStr) != 0) 142 if (mysql_query (m, QueryStr) != 0)
118 return NULL; 143 {
119 144 mu_error ("MySQL: query failed: %s", mysql_error (m));
120 if ((res = mysql_store_result (m)) == NULL) 145 mysql_close (m);
121 return NULL; 146 return NULL;
147 }
122 148
149 if ((res = mysql_store_result (m)) == NULL)
150 {
151 mu_error ("MySQL: can't store result: %s", mysql_error (m));
152 mysql_close (m);
153 return NULL;
154 }
155
123 if ((row = mysql_fetch_row (res)) == NULL) 156 if ((row = mysql_fetch_row (res)) == NULL)
124 return NULL; 157 {
158 mu_error ("MySQL: can't fetch row: %s", mysql_error (m));
159 mysql_close (m);
160 return NULL;
161 }
125 162
126 if (!tpw) 163 if (!tpw)
127 tpw = (struct spwd *)xmalloc (sizeof (struct spwd)); 164 tpw = (struct spwd *)xmalloc (sizeof (struct spwd));
...@@ -137,7 +174,8 @@ getMspnam (const char *username) ...@@ -137,7 +174,8 @@ getMspnam (const char *username)
137 tpw->sp_max = 99999; 174 tpw->sp_max = 99999;
138 tpw->sp_warn = 7; 175 tpw->sp_warn = 7;
139 176
140 mysql_free_result (res); 177 mysql_free_result (res);
178 mysql_close (m);
141 return tpw; 179 return tpw;
142 } 180 }
143 181
......