pop3d: optimize apop support.
* pop3d/apop.c (pop3d_apopuser): Fix highly ineffective resource usage in both branches. * pop3d/pop3d.c: Remove misleading comment.
Showing
2 changed files
with
28 additions
and
46 deletions
... | @@ -41,11 +41,11 @@ | ... | @@ -41,11 +41,11 @@ |
41 | char * | 41 | char * |
42 | pop3d_apopuser (const char *user) | 42 | pop3d_apopuser (const char *user) |
43 | { | 43 | { |
44 | char *password; | 44 | char *password = NULL; |
45 | char buf[POP_MAXCMDLEN]; | 45 | |
46 | |||
47 | #ifdef USE_DBM | 46 | #ifdef USE_DBM |
48 | { | 47 | { |
48 | size_t len; | ||
49 | DBM_FILE db; | 49 | DBM_FILE db; |
50 | DBM_DATUM key, data; | 50 | DBM_DATUM key, data; |
51 | 51 | ||
... | @@ -60,11 +60,8 @@ pop3d_apopuser (const char *user) | ... | @@ -60,11 +60,8 @@ pop3d_apopuser (const char *user) |
60 | memset (&key, 0, sizeof key); | 60 | memset (&key, 0, sizeof key); |
61 | memset (&data, 0, sizeof data); | 61 | memset (&data, 0, sizeof data); |
62 | 62 | ||
63 | strncpy (buf, user, sizeof buf); | 63 | MU_DATUM_PTR (key) = user; |
64 | /* strncpy () is lame and does not NULL terminate. */ | 64 | MU_DATUM_SIZE (key) = strlen (user); |
65 | buf[sizeof (buf) - 1] = '\0'; | ||
66 | MU_DATUM_PTR(key) = buf; | ||
67 | MU_DATUM_SIZE(key) = strlen (buf); | ||
68 | 65 | ||
69 | rc = mu_dbm_fetch (db, key, &data); | 66 | rc = mu_dbm_fetch (db, key, &data); |
70 | mu_dbm_close (db); | 67 | mu_dbm_close (db); |
... | @@ -74,21 +71,23 @@ pop3d_apopuser (const char *user) | ... | @@ -74,21 +71,23 @@ pop3d_apopuser (const char *user) |
74 | _("cannot fetch APOP data: %s"), mu_strerror (errno)); | 71 | _("cannot fetch APOP data: %s"), mu_strerror (errno)); |
75 | return NULL; | 72 | return NULL; |
76 | } | 73 | } |
77 | password = calloc (MU_DATUM_SIZE(data) + 1, sizeof (*password)); | 74 | len = MU_DATUM_SIZE (data); |
75 | password = malloc (len + 1); | ||
78 | if (password == NULL) | 76 | if (password == NULL) |
79 | { | 77 | { |
80 | mu_dbm_datum_free (&data); | 78 | mu_dbm_datum_free (&data); |
81 | return NULL; | 79 | return NULL; |
82 | } | 80 | } |
83 | 81 | memcpy (password, MU_DATUM_PTR (data), len); | |
84 | sprintf (password, "%.*s", (int) MU_DATUM_SIZE(data), | 82 | password[len] = 0; |
85 | (char*) MU_DATUM_PTR(data)); | ||
86 | mu_dbm_datum_free (&data); | 83 | mu_dbm_datum_free (&data); |
87 | return password; | 84 | return password; |
88 | } | 85 | } |
89 | #else /* !USE_DBM */ | 86 | #else /* !USE_DBM */ |
90 | { | 87 | { |
91 | char *tmp; | 88 | char *buf = NULL; |
89 | size_t size = 0; | ||
90 | size_t ulen; | ||
92 | FILE *apop_file; | 91 | FILE *apop_file; |
93 | 92 | ||
94 | if (mu_check_perm (APOP_PASSFILE, 0600)) | 93 | if (mu_check_perm (APOP_PASSFILE, 0600)) |
... | @@ -101,44 +100,32 @@ pop3d_apopuser (const char *user) | ... | @@ -101,44 +100,32 @@ pop3d_apopuser (const char *user) |
101 | apop_file = fopen (APOP_PASSFILE, "r"); | 100 | apop_file = fopen (APOP_PASSFILE, "r"); |
102 | if (apop_file == NULL) | 101 | if (apop_file == NULL) |
103 | { | 102 | { |
104 | mu_diag_output (MU_DIAG_INFO, _("unable to open APOP password file %s"), | 103 | mu_diag_output (MU_DIAG_INFO, |
105 | strerror (errno)); | 104 | _("unable to open APOP password file %s: %s"), |
105 | APOP_PASSFILE, mu_strerror (errno)); | ||
106 | return NULL; | 106 | return NULL; |
107 | } | 107 | } |
108 | 108 | ||
109 | password = calloc (APOP_DIGEST, sizeof (*password)); | 109 | ulen = strlen (user); |
110 | if (password == NULL) | 110 | while (getline (&buf, &size, apop_file) > 0) |
111 | { | 111 | { |
112 | fclose (apop_file); | 112 | char *p, *start = mu_str_stripws (buf); |
113 | pop3d_abquit (ERR_NO_MEM); | ||
114 | } | ||
115 | 113 | ||
116 | while (fgets (buf, sizeof (buf) - 1, apop_file) != NULL) | 114 | if (!*start || *start == '#') |
117 | { | ||
118 | tmp = strchr (buf, ':'); | ||
119 | if (tmp == NULL) | ||
120 | continue; | 115 | continue; |
121 | *tmp++ = '\0'; | 116 | p = strchr (start, ':'); |
122 | 117 | if (!p) | |
123 | if (strncmp (user, buf, strlen (user))) | ||
124 | continue; | 118 | continue; |
125 | 119 | if (p - start == ulen && memcmp (user, start, ulen) == 0) | |
126 | strncpy (password, tmp, APOP_DIGEST); | 120 | { |
127 | /* strncpy () is lame and does not NULL terminate. */ | 121 | p = mu_str_skip_class (p + 1, MU_CTYPE_SPACE); |
128 | password[APOP_DIGEST - 1] = '\0'; | 122 | if (*p) |
129 | tmp = strchr (password, '\n'); | 123 | password = strdup (p); |
130 | if (tmp) | 124 | break; |
131 | *tmp = '\0'; | 125 | } |
132 | break; | ||
133 | } | 126 | } |
134 | 127 | ||
135 | fclose (apop_file); | 128 | fclose (apop_file); |
136 | if (*password == '\0') | ||
137 | { | ||
138 | free (password); | ||
139 | return NULL; | ||
140 | } | ||
141 | |||
142 | return password; | 129 | return password; |
143 | } | 130 | } |
144 | #endif | 131 | #endif | ... | ... |
... | @@ -261,11 +261,6 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) | ... | @@ -261,11 +261,6 @@ pop3d_mainloop (int fd, FILE *infile, FILE *outfile) |
261 | buf = pop3d_readline (buffer, sizeof (buffer)); | 261 | buf = pop3d_readline (buffer, sizeof (buffer)); |
262 | pop3d_parse_command (buf, &cmd, &arg); | 262 | pop3d_parse_command (buf, &cmd, &arg); |
263 | 263 | ||
264 | /* The mailbox size needs to be check to make sure that we are in | ||
265 | sync. Some other applications may not respect the *.lock or | ||
266 | the lock may be stale because downloading on slow modem. | ||
267 | We rely on the size of the mailbox for the check and bail if out | ||
268 | of sync. */ | ||
269 | if (state == TRANSACTION && !mu_mailbox_is_updated (mbox)) | 264 | if (state == TRANSACTION && !mu_mailbox_is_updated (mbox)) |
270 | { | 265 | { |
271 | static mu_off_t mailbox_size; | 266 | static mu_off_t mailbox_size; | ... | ... |
-
Please register or sign in to post a comment