Remove the use of MAXHOSTNAMELEN macro.
* comsat/comsat.c (hostname): Change type. (comsat_init): Use mu_get_host_name. * comsat/comsat.h (hostname): Change declaration. * libmailutils/locker.c (lock_dotlock): Use mu_get_host_name. * libmailutils/mutil.c (mu_get_host_name): Rewrite. * pop3d/pop3d.c (pop3d_mainloop): Remove unnecessary call to gethostbyname. * pop3d/pop3d.h (MAXHOSTNAMELEN): Remove definition.
Showing
6 changed files
with
88 additions
and
46 deletions
... | @@ -97,12 +97,8 @@ static const char *comsat_argp_capa[] = { | ... | @@ -97,12 +97,8 @@ static const char *comsat_argp_capa[] = { |
97 | #define NOT_HERE 1 | 97 | #define NOT_HERE 1 |
98 | #define PERMISSION_DENIED 2 | 98 | #define PERMISSION_DENIED 2 |
99 | 99 | ||
100 | #ifndef MAXHOSTNAMELEN | ||
101 | # define MAXHOSTNAMELEN 64 | ||
102 | #endif | ||
103 | |||
104 | int maxlines = 5; | 100 | int maxlines = 5; |
105 | char hostname[MAXHOSTNAMELEN]; | 101 | char *hostname; |
106 | const char *username; | 102 | const char *username; |
107 | int require_tty; | 103 | int require_tty; |
108 | mu_m_server_t server; | 104 | mu_m_server_t server; |
... | @@ -190,11 +186,17 @@ sig_hup (int sig) | ... | @@ -190,11 +186,17 @@ sig_hup (int sig) |
190 | void | 186 | void |
191 | comsat_init () | 187 | comsat_init () |
192 | { | 188 | { |
189 | int rc; | ||
190 | |||
193 | /* Register mailbox formats */ | 191 | /* Register mailbox formats */ |
194 | mu_register_all_mbox_formats (); | 192 | mu_register_all_mbox_formats (); |
195 | 193 | ||
196 | gethostname (hostname, sizeof hostname); | 194 | rc = mu_get_host_name (&hostname); |
197 | 195 | if (rc) | |
196 | { | ||
197 | mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, rc); | ||
198 | exit (EXIT_FAILURE); | ||
199 | } | ||
198 | /* Set signal handlers */ | 200 | /* Set signal handlers */ |
199 | signal (SIGTTOU, SIG_IGN); | 201 | signal (SIGTTOU, SIG_IGN); |
200 | signal (SIGCHLD, SIG_IGN); | 202 | signal (SIGCHLD, SIG_IGN); |
... | @@ -556,7 +558,7 @@ main (int argc, char **argv) | ... | @@ -556,7 +558,7 @@ main (int argc, char **argv) |
556 | 558 | ||
557 | if (mu_app_init (&argp, comsat_argp_capa, comsat_cfg_param, argc, argv, 0, | 559 | if (mu_app_init (&argp, comsat_argp_capa, comsat_cfg_param, argc, argv, 0, |
558 | &ind, server)) | 560 | &ind, server)) |
559 | exit (1); | 561 | exit (EXIT_FAILURE); |
560 | 562 | ||
561 | if (test_mode) | 563 | if (test_mode) |
562 | { | 564 | { | ... | ... |
... | @@ -75,7 +75,7 @@ extern time_t overflow_control_interval; | ... | @@ -75,7 +75,7 @@ extern time_t overflow_control_interval; |
75 | extern time_t overflow_delay_time; | 75 | extern time_t overflow_delay_time; |
76 | extern int maxlines; | 76 | extern int maxlines; |
77 | extern const char *username; | 77 | extern const char *username; |
78 | extern char hostname[]; | 78 | extern char *hostname; |
79 | extern struct daemon_param daemon_param; | 79 | extern struct daemon_param daemon_param; |
80 | 80 | ||
81 | void run_user_action (FILE *tty, const char *cr, mu_message_t msg); | 81 | void run_user_action (FILE *tty, const char *cr, mu_message_t msg); | ... | ... |
... | @@ -704,14 +704,11 @@ destroy_dotlock (mu_locker_t locker) | ... | @@ -704,14 +704,11 @@ destroy_dotlock (mu_locker_t locker) |
704 | free (locker->data.dot.nfslock); | 704 | free (locker->data.dot.nfslock); |
705 | } | 705 | } |
706 | 706 | ||
707 | #ifndef MAXHOSTNAMELEN | ||
708 | # define MAXHOSTNAMELEN 256 | ||
709 | #endif | ||
710 | |||
711 | static int | 707 | static int |
712 | lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) | 708 | lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) |
713 | { | 709 | { |
714 | char host[MAXHOSTNAMELEN + 1] = "localhost"; | 710 | int rc; |
711 | char *host = NULL; | ||
715 | char pid[11]; /* 10 is strlen(2^32 = 4294967296) */ | 712 | char pid[11]; /* 10 is strlen(2^32 = 4294967296) */ |
716 | char now[11]; | 713 | char now[11]; |
717 | size_t sz = 0; | 714 | size_t sz = 0; |
... | @@ -729,8 +726,9 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) | ... | @@ -729,8 +726,9 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) |
729 | 726 | ||
730 | /* build the NFS hitching-post to the lock file */ | 727 | /* build the NFS hitching-post to the lock file */ |
731 | 728 | ||
732 | gethostname (host, sizeof (host)); | 729 | rc = mu_get_host_name (&host); |
733 | host[MAXHOSTNAMELEN] = 0; | 730 | if (rc) |
731 | return rc; | ||
734 | 732 | ||
735 | snprintf (now, sizeof (now), "%lu", (unsigned long) time (0)); | 733 | snprintf (now, sizeof (now), "%lu", (unsigned long) time (0)); |
736 | now[sizeof (now) - 1] = 0; | 734 | now[sizeof (now) - 1] = 0; |
... | @@ -746,10 +744,14 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) | ... | @@ -746,10 +744,14 @@ lock_dotlock (mu_locker_t locker, enum mu_locker_mode mode) |
746 | locker->data.dot.nfslock = malloc (sz); | 744 | locker->data.dot.nfslock = malloc (sz); |
747 | 745 | ||
748 | if (!locker->data.dot.nfslock) | 746 | if (!locker->data.dot.nfslock) |
747 | { | ||
748 | free (host); | ||
749 | return ENOMEM; | 749 | return ENOMEM; |
750 | } | ||
750 | 751 | ||
751 | snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s", | 752 | snprintf (locker->data.dot.nfslock, sz, "%s.%s.%s.%s", |
752 | locker->file, pid, now, host); | 753 | locker->file, pid, now, host); |
754 | free (host); | ||
753 | 755 | ||
754 | fd = open (locker->data.dot.nfslock, | 756 | fd = open (locker->data.dot.nfslock, |
755 | O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR); | 757 | O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR); | ... | ... |
... | @@ -291,28 +291,78 @@ mu_cpystr (char *dst, const char *src, size_t size) | ... | @@ -291,28 +291,78 @@ mu_cpystr (char *dst, const char *src, size_t size) |
291 | return len; | 291 | return len; |
292 | } | 292 | } |
293 | 293 | ||
294 | #ifndef MAXHOSTNAMELEN | ||
295 | # define MAXHOSTNAMELEN 64 | ||
296 | #endif | ||
297 | |||
294 | int | 298 | int |
295 | mu_get_host_name (char **host) | 299 | mu_get_host_name (char **host) |
296 | { | 300 | { |
297 | char hostname[MAXHOSTNAMELEN + 1]; | 301 | char *hostname = NULL; |
298 | struct hostent *hp = NULL; | 302 | size_t size = 0; |
299 | char *domain = NULL; | 303 | char *p; |
300 | |||
301 | gethostname (hostname, sizeof hostname); | ||
302 | hostname[sizeof (hostname) - 1] = 0; | ||
303 | 304 | ||
304 | if ((hp = gethostbyname (hostname))) | 305 | while (1) |
305 | domain = hp->h_name; | 306 | { |
307 | if (size == 0) | ||
308 | { | ||
309 | size = MAXHOSTNAMELEN; | ||
310 | p = malloc (size); | ||
311 | } | ||
306 | else | 312 | else |
307 | domain = hostname; | 313 | { |
308 | 314 | size_t ns = size * 2; | |
309 | domain = strdup (domain); | 315 | if (ns < size) |
310 | 316 | { | |
311 | if (!domain) | 317 | free (hostname); |
312 | return ENOMEM; | 318 | return ENOMEM; |
319 | } | ||
320 | size = ns; | ||
321 | p = realloc (hostname, size); | ||
322 | } | ||
323 | if (!p) | ||
324 | { | ||
325 | free (hostname); | ||
326 | return ENOMEM; | ||
327 | } | ||
328 | hostname = p; | ||
329 | hostname[size - 1] = 0; | ||
330 | if (gethostname (hostname, size - 1) == 0) | ||
331 | { | ||
332 | if (!hostname[size - 1]) | ||
333 | break; | ||
334 | } | ||
335 | else if (errno != 0 && errno != ENAMETOOLONG && errno != EINVAL | ||
336 | && errno != ENOMEM) | ||
337 | { | ||
338 | int rc = errno; | ||
339 | free (hostname); | ||
340 | return rc; | ||
341 | } | ||
342 | } | ||
313 | 343 | ||
314 | *host = domain; | 344 | /* Try to return fully qualified host name */ |
345 | if (!strchr (hostname, '.')) | ||
346 | { | ||
347 | struct hostent *hp = gethostbyname (hostname); | ||
348 | if (hp) | ||
349 | { | ||
350 | size_t len = strlen (hp->h_name); | ||
351 | if (size < len + 1) | ||
352 | { | ||
353 | p = realloc (hostname, len + 1); | ||
354 | if (!p) | ||
355 | { | ||
356 | free (hostname); | ||
357 | return ENOMEM; | ||
358 | } | ||
359 | hostname = p; | ||
360 | } | ||
361 | strcpy (hostname, hp->h_name); | ||
362 | } | ||
363 | } | ||
315 | 364 | ||
365 | *host = hostname; | ||
316 | return 0; | 366 | return 0; |
317 | } | 367 | } |
318 | 368 | ... | ... |
... | @@ -227,18 +227,12 @@ pop3d_mainloop (int ifd, int ofd) | ... | @@ -227,18 +227,12 @@ pop3d_mainloop (int ifd, int ofd) |
227 | /* Prepare the shared secret for APOP. */ | 227 | /* Prepare the shared secret for APOP. */ |
228 | { | 228 | { |
229 | char *local_hostname; | 229 | char *local_hostname; |
230 | local_hostname = mu_alloc (MAXHOSTNAMELEN + 1); | ||
231 | 230 | ||
232 | /* Get our canonical hostname. */ | 231 | status = mu_get_host_name (&local_hostname); |
233 | { | 232 | if (status) |
234 | struct hostent *htbuf; | ||
235 | gethostname (local_hostname, MAXHOSTNAMELEN); | ||
236 | htbuf = gethostbyname (local_hostname); | ||
237 | if (htbuf) | ||
238 | { | 233 | { |
239 | free (local_hostname); | 234 | mu_diag_funcall (MU_DIAG_ERROR, "mu_get_host_name", NULL, status); |
240 | local_hostname = strdup (htbuf->h_name); | 235 | exit (EXIT_FAILURE); |
241 | } | ||
242 | } | 236 | } |
243 | 237 | ||
244 | md5shared = mu_alloc (strlen (local_hostname) + 51); | 238 | md5shared = mu_alloc (strlen (local_hostname) + 51); | ... | ... |
... | @@ -133,12 +133,6 @@ extern int expire_on_exit; | ... | @@ -133,12 +133,6 @@ extern int expire_on_exit; |
133 | #include <shadow.h> | 133 | #include <shadow.h> |
134 | #endif | 134 | #endif |
135 | 135 | ||
136 | #ifndef MAXHOSTNAMELEN | ||
137 | /* Maximum length of a hostname (is this defined somewhere else?). */ | ||
138 | /* MAXHOSTNAMELEN is already defined on Solaris. */ | ||
139 | # define MAXHOSTNAMELEN 64 | ||
140 | #endif | ||
141 | |||
142 | #define POP3_ATTRIBUTE_DELE 0x0001 | 136 | #define POP3_ATTRIBUTE_DELE 0x0001 |
143 | #define POP3_ATTRIBUTE_RETR 0x0010 | 137 | #define POP3_ATTRIBUTE_RETR 0x0010 |
144 | 138 | ... | ... |
-
Please register or sign in to post a comment