From 533bacb3b36e382d8e4c8cc1832ce9c37859eb3f Mon Sep 17 00:00:00 2001 From: rbasch Date: Wed, 15 Mar 2000 22:43:31 +0000 Subject: [PATCH] Win32 portability mods for Pismere. --- clients/addusr/addusr.c | 2 +- clients/blanche/blanche.c | 34 +++---- clients/lib/pobox.c | 4 + clients/mailmaint/mailmaint.c | 135 +++++++++++++++++++++++--- clients/moira/acl.c | 6 +- clients/moira/attach.c | 7 +- clients/moira/cluster.c | 17 +++- clients/moira/lists.c | 1 - clients/moira/main.c | 66 ++++++++----- clients/moira/menu.c | 42 +++++++- clients/moira/misc.c | 2 + clients/moira/pobox.c | 1 - clients/moira/printer.c | 7 +- clients/moira/user.c | 8 +- clients/moira/utils.c | 6 ++ clients/mrcheck/mrcheck.c | 10 +- clients/mrtest/mrtest.c | 80 +++++++++++++--- clients/passwd/chfn.c | 8 +- clients/passwd/chpobox.c | 7 +- clients/stella/stella.c | 32 ++++--- configure | 176 +++++++++++++++++++++++++++++++++- configure.in | 6 +- include/moira.h | 10 +- lib/fixhost.c | 25 ++++- lib/mr_call.c | 25 +++-- lib/mr_connect.c | 110 ++++++++++++--------- lib/mr_private.h | 4 +- 27 files changed, 646 insertions(+), 185 deletions(-) diff --git a/clients/addusr/addusr.c b/clients/addusr/addusr.c index eef20b87..fe1ffc16 100644 --- a/clients/addusr/addusr.c +++ b/clients/addusr/addusr.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) int status, lineno; char **arg = argv, *qargv[U_END]; char *p, *first, *middle, *last, *id, *login, *server; - char buf[BUFSIZ], idbuf[32]; + char buf[BUFSIZ]; FILE *input; /* clear all flags & lists */ diff --git a/clients/blanche/blanche.c b/clients/blanche/blanche.c index e573a4ef..b6239bfa 100644 --- a/clients/blanche/blanche.c +++ b/clients/blanche/blanche.c @@ -826,39 +826,40 @@ int main(int argc, char **argv) void usage(char **argv) { +#define USAGE_OPTIONS_FORMAT " %-39s%s\n" fprintf(stderr, "Usage: %s listname [options]\n", argv[0]); fprintf(stderr, "Options are\n"); - fprintf(stderr, " %-39s%-39s\n", "-v | -verbose", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose", "-C | -create"); - fprintf(stderr, " %-39s%-39s\n", "-m | -members", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-m | -members", "-R | -rename newname"); - fprintf(stderr, " %-39s%-39s\n", "-u | -users", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -users", "-P | -public"); - fprintf(stderr, " %-39s%-39s\n", "-l | -lists", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-l | -lists", "-NP | -private"); - fprintf(stderr, " %-39s%-39s\n", "-s | -strings", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-s | -strings", "-A | -active"); - fprintf(stderr, " %-39s%-39s\n", "-k | -kerberos", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-k | -kerberos", "-I | -inactive"); - fprintf(stderr, " %-39s%-39s\n", "-i | -info", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info", "-V | -visible"); - fprintf(stderr, " %-39s%-39s\n", "-r | -recursive", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-r | -recursive", "-H | -hidden"); - fprintf(stderr, " %-39s%-39s\n", "-a | -add member", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -add member", "-M | -mail"); - fprintf(stderr, " %-39s%-39s\n", "-d | -delete member", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -delete member", "-NM | -notmail"); - fprintf(stderr, " %-39s%-39s\n", "-al | -addlist filename", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-al | -addlist filename", "-G | -group"); - fprintf(stderr, " %-39s%-39s\n", "-dl | -deletelist filename", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dl | -deletelist filename", "-NG | -notgroup"); - fprintf(stderr, " %-39s%-39s\n", "-f | -file filename", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-f | -file filename", "-D | -desc description"); - fprintf(stderr, " %-39s%-39s\n", "-at | -addtagged member tag", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-at | -addtagged member tag", "-O | -owner owner"); - fprintf(stderr, " %-39s%-39s\n", "-ct | -changetag member tag", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ct | -changetag member tag", "-t | -tags"); - fprintf(stderr, " %-39s%-39s\n", "-n | -noauth", + fprintf(stderr, USAGE_OPTIONS_FORMAT, "-n | -noauth", "-db | -database host[:port]"); exit(1); } @@ -869,7 +870,6 @@ void usage(char **argv) void show_list_member(struct member *memberstruct) { char *s = ""; - char *tag; switch (memberstruct->type) { diff --git a/clients/lib/pobox.c b/clients/lib/pobox.c index c9c2c124..b8c94759 100644 --- a/clients/lib/pobox.c +++ b/clients/lib/pobox.c @@ -26,6 +26,10 @@ static int save_alias_value(int argc, char **argv, void *sq); extern char *whoami; +#ifdef _WIN32 +#define strtok_r(s, tokens, resume) strtok(s, tokens) +#endif /* _WIN32 */ + int mrcl_validate_pobox_smtp(char *user, char *address, char **ret) { char *addr, *retaddr, *p, *lasts = NULL; diff --git a/clients/mailmaint/mailmaint.c b/clients/mailmaint/mailmaint.c index cc6bbb9b..50afe3a9 100644 --- a/clients/mailmaint/mailmaint.c +++ b/clients/mailmaint/mailmaint.c @@ -16,16 +16,44 @@ #include #ifdef HAVE_CURSES +#ifdef _WIN32 +#include +#ifdef MOUSE_MOVED +#undef MOUSE_MOVED +#endif +#endif /*_WIN32*/ #include #endif -#include #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif #include +#ifdef _WIN32 +#define INPUT_MASK 0xff +#ifdef getchar +#undef getchar +#endif +#define getchar() _getch() +#ifdef title +#undef title +#endif +static void DELETE_A_CHAR(void) +{ + int x, y; + getsyx(&y, &x); + x -= 1; + mvdelch(y,x); +} +#else /* !_WIN32 */ +#define INPUT_MASK 0x7f +#define DELETE_A_CHAR() printf("\b \b"); +#endif /* !_WIN32 */ + RCSID("$Header$"); #define STARTCOL 0 @@ -90,6 +118,7 @@ void end_display(void); void display_menu(MENU *menu); void pack_main_menu(void); void pack_help_menu(void); +void free_menu(MENU* menu); void highlight(MENU *menu); void title(char *buff); void center_text(int row, char *buff); @@ -159,11 +188,13 @@ int main(int argc, char *argv[]) display_buff("to at least 24 lines and 60 columns.\n"); exit(0); } + raw(); noecho(); old_hook = set_com_err_hook(menu_err_hook); position[0] = oldpos[0] = 1; level = 0; + pack_main_menu(); pack_help_menu(); display_menu(main_menu); @@ -171,6 +202,26 @@ int main(int argc, char *argv[]) cls(); endwin(); set_com_err_hook(old_hook); + + free_menu(main_menu); + free_menu(help_menu); + + if (current_li->acl_type) + free(current_li->acl_type); + if (current_li->acl_name) + free(current_li->acl_name); + if (current_li->desc) + free(current_li->desc); + if (current_li->modtime) + free(current_li->modtime); + if (current_li->modby) + free(current_li->modby); + if (current_li->modwith) + free(current_li->modwith); + free(current_li); + + mr_disconnect(); + exit(0); punt: @@ -191,7 +242,7 @@ void get_main_input(void) currow = DISPROW + 2; page = 1; toggle = num_members = moreflg = 0; - c = getchar() & 0x7f; /* mask parity bit */ + c = getchar() & INPUT_MASK; /* mask parity bit */ if (c == '\r' || c == '\n') { if (position[level] == 7) @@ -245,11 +296,12 @@ void get_main_input(void) if (retflg) delete_member(); break; +#ifndef _WIN32 case 27: /* escape */ - c = getchar() & 0x7f; + c = getchar() & INPUT_MASK; if (c == 91) { - c = getchar() & 0x7f; + c = getchar() & INPUT_MASK; if (c == 65) /* up arrow */ { position[level]--; @@ -266,6 +318,25 @@ void get_main_input(void) } } } +#else /* _WIN32 */ + case 0xe0: + c = getchar() & INPUT_MASK; + if (c == 0x48) /* up arrow */ + { + position[level]--; + if (!position[level]) + position[level] = 7; + } + else + { + if (c == 0x50) /* down arrow */ + { + position[level]++; + if (position[level] > 7) + position[level] = 1; + } + } +#endif /* _WIN32 */ break; default: printf("%c", 7); @@ -311,6 +382,7 @@ void show_list_info(void) show_text(currow, STARTCOL, "Press any Key to continue..."); getchar(); } + free(buf); clrwin(DISPROW); } @@ -318,14 +390,16 @@ void show_list_info(void) void display_buff(char *buf) { int i, cnt; - char *printbuf; + char *printbuf = NULL; int maxcol; + int len; maxcol = COLS; cnt = 0; printbuf = calloc(maxcol, 1); - for (i = 0; i <= strlen(buf); i++) + len = strlen(buf); + for (i = 0; i <= len; i++) { printbuf[cnt] = buf[i]; cnt++; @@ -337,11 +411,12 @@ void display_buff(char *buf) printbuf = calloc(maxcol, 1); } } - if (strlen(buf) % maxcol != 0) + if (len % maxcol != 0) { start_display_buff(printbuf); - free(printbuf); } + if (printbuf) + free(printbuf); return; } @@ -404,6 +479,7 @@ void add_member(void) show_text(DISPROW + 4, STARTCOL, "Press any Key to continue..."); getchar(); } + free(buf); clrwin(DISPROW); } @@ -434,7 +510,11 @@ void delete_member(void) currow = DISPROW + 4; show_text(DISPROW + 4, STARTCOL, "Press any Key to continue..."); getchar(); + free(argv[0]); + free(argv[1]); + free(argv[2]); } + free(buf); clrwin(DISPROW); } @@ -460,6 +540,7 @@ void list_by_member(void) show_text(currow, STARTCOL, "Press any Key to continue..."); getchar(); clrwin(DISPROW); + free(buf); } /****************************************************/ @@ -468,7 +549,7 @@ void show_all(void) char c; show_text(DISPROW, STARTCOL, "This function may take a while... proceed? [n] "); - c = getchar() & 0x7f; + c = getchar() & INPUT_MASK; if (c == 'y' || c == 'Y') { move(DISPROW + 1, STARTCOL); @@ -557,12 +638,14 @@ void list_members(void) show_text(currow, STARTCOL, "Press any key to continue..."); getchar(); clrwin(DISPROW); - return; + goto cleanup; } end_display(); - return; + goto cleanup; } clrwin(DISPROW); + cleanup: + free(buf); } /****************************************************/ @@ -598,7 +681,7 @@ void start_display(char *buff) erase_line(currow, STARTCOL); show_text(currow, STARTCOL, "Flushing query..."); moreflg = 1; - return; + goto cleanup; } clrwin(DISPROW + 2); currow = DISPROW + 2; @@ -616,6 +699,8 @@ void start_display(char *buff) currow++; } toggle = !toggle; + cleanup: + free(buffer); } /****************************************************/ @@ -637,6 +722,7 @@ void end_display(void) show_text(currow, STARTCOL, "Press any key to continue..."); getchar(); clrwin(DISPROW); + free(buffer); } /****************************************************/ @@ -692,6 +778,7 @@ void pack_main_menu(void) main_menu->items[4] = strdup("5. Add yourself to a mailing list."); main_menu->items[5] = strdup("6. Delete yourself from a mailing list."); main_menu->items[6] = strdup("q. Quit."); + free(buf); } /****************************************************/ @@ -709,6 +796,17 @@ void pack_help_menu(void) help_menu->items[4] = strdup("Press any key to continue."); } +/****************************************************/ +void free_menu(MENU* menu) +{ + int i; + for (i = 0; i < menu->num_items; i++) + free(menu->items[i]); + free(menu->items); + free(menu->title); + free(menu); +} + /****************************************************/ void highlight(MENU *menu) { @@ -843,10 +941,11 @@ int Prompt(char *prompt, char *buf, int buflen, int crok) addstr(prompt); refresh(); + for (p = buf; abs(strlen(p) - strlen(buf)) <= buflen;) { refresh(); - c = getchar() & 0x7f; + c = getchar() & INPUT_MASK; switch (c) { case CTL('C'): @@ -871,16 +970,22 @@ int Prompt(char *prompt, char *buf, int buflen, int crok) if (p > buf) { p--; - printf("\b \b"); + DELETE_A_CHAR(); } break; case CTL('U'): case CTL('G'): case CTL('['): while (p-- > buf) - printf("\b \b"); + DELETE_A_CHAR(); p = buf; break; +#ifdef _WIN32 + case 0xe0: + c = getchar() & INPUT_MASK; + putchar(CTL('G')); + break; +#endif /*_WIN32*/ default: if (abs(strlen(p) - strlen(buf)) >= buflen) { diff --git a/clients/moira/acl.c b/clients/moira/acl.c index 3009def2..dd2f9ff9 100644 --- a/clients/moira/acl.c +++ b/clients/moira/acl.c @@ -79,7 +79,6 @@ static char *PrintACLInfo(char **info) { static char name[BUFSIZ]; char buf[BUFSIZ]; - int status; if (!info) /* If no informaion */ { @@ -109,9 +108,6 @@ static char *PrintACLInfo(char **info) static char **AskACLInfo(char **info) { char temp_buf[BUFSIZ]; - char *args[3]; - char *s, *d; - int status; Put_message(""); info[ACL_HOST] = canonicalize_hostname(info[ACL_HOST]); @@ -191,7 +187,7 @@ int DeleteACL(int argc, char **argv) int AddACL(int argc, char **argv) { - char *info[MAX_ARGS_SIZE], **args, *host; + char *info[MAX_ARGS_SIZE], **args; int stat; argv[1] = canonicalize_hostname(strdup(argv[1])); diff --git a/clients/moira/attach.c b/clients/moira/attach.c index b70c39cd..715ab681 100644 --- a/clients/moira/attach.c +++ b/clients/moira/attach.c @@ -28,7 +28,9 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif /* HAVE_UNISTD_H */ RCSID("$Header$"); @@ -234,9 +236,11 @@ static char *PrintFSInfo(char **info) return info[FS_NAME]; } - char *canonicalize_cell(char *c) { +#ifdef _WIN32 + return c; +#else /* !_WIN32 */ struct stat stbuf; char path[512]; int count; @@ -250,6 +254,7 @@ char *canonicalize_cell(char *c) path[count] = 0; free(c); return strdup(path); +#endif /* _WIN32 */ } diff --git a/clients/moira/cluster.c b/clients/moira/cluster.c index b1c4fd63..c4dbaa53 100644 --- a/clients/moira/cluster.c +++ b/clients/moira/cluster.c @@ -23,11 +23,16 @@ #include "globals.h" #include + +#ifdef HAVE_UNAME #include +#endif +#ifndef _WIN32 #include #include #include +#endif /* _WIN32 */ #include #include @@ -584,7 +589,7 @@ char **AskMCDInfo(char **info, int type, Bool name) return NULL; if (GetAddressFromUser("Network mask", &info[SN_MASK]) == SUB_ERROR) return NULL; - if (atoi(info[SN_LOW]) == ntohl(inet_addr(S_DEFAULT_LOW))) + if (atoi(info[SN_LOW]) == (int)ntohl(inet_addr(S_DEFAULT_LOW))) { struct in_addr low; unsigned long mask, addr; @@ -600,7 +605,7 @@ char **AskMCDInfo(char **info, int type, Bool name) if (GetAddressFromUser("Lowest assignable address", &info[SN_LOW]) == SUB_ERROR) return NULL; - if (atoi(info[SN_HIGH]) == ntohl(inet_addr(S_DEFAULT_HIGH))) + if (atoi(info[SN_HIGH]) == (int)ntohl(inet_addr(S_DEFAULT_HIGH))) { struct in_addr high; unsigned long mask, addr; @@ -997,10 +1002,16 @@ char *partial_canonicalize_hostname(char *s) else { struct hostent *hp; +#ifdef HAVE_UNAME struct utsname name; - uname(&name); hp = gethostbyname(name.nodename); +#else + char name[256]; + gethostname(name, sizeof(name)); + name[sizeof(name)-1] = 0; + hp = gethostbyname(name); +#endif /* HAVE_UNAME */ cp = strchr(hp->h_name, '.'); if (cp) def_domain = strdup(++cp); diff --git a/clients/moira/lists.c b/clients/moira/lists.c index 337c22fb..6952a625 100644 --- a/clients/moira/lists.c +++ b/clients/moira/lists.c @@ -732,7 +732,6 @@ int TagMember(int argc, char **argv) { char *args[10]; int status; - char temp_buf[BUFSIZ]; if (GetMemberInfo("tag", args) == SUB_ERROR) return DM_NORMAL; diff --git a/clients/moira/main.c b/clients/moira/main.c index 1bb5e1f1..d6371399 100644 --- a/clients/moira/main.c +++ b/clients/moira/main.c @@ -19,20 +19,20 @@ #include "f_defs.h" #include "globals.h" -#include #include #include #include +#ifdef HAVE_UNISTD_H #include - -#include +#endif /* HAVE_UNISTD_H */ RCSID("$Header$"); static void ErrorExit(char *buf, int status); static void Usage(void); -static void Signal_Handler(void); -static void CatchInterrupt(void); +static void Signal_Handler(int sig); +static void CatchInterrupt(int sig); +static void SetHandlers(void); char *whoami; /* used by menu.c ugh!!! */ char *moira_server; @@ -54,8 +54,7 @@ int main(int argc, char **argv) { int status; Menu *menu; - char **arg, pname[ANAME_SZ]; - struct sigaction act; + char **arg; if (!(program_name = strrchr(argv[0], '/'))) program_name = argv[0]; @@ -132,21 +131,7 @@ int main(int argc, char **argv) * These signals should not be set until just before we fire up the menu * system. */ - - sigemptyset(&act.sa_mask); - act.sa_flags = 0; - act.sa_handler = Signal_Handler; - sigaction(SIGHUP, &act, NULL); - sigaction(SIGQUIT, &act, NULL); -#ifdef HAVE_CURSES - if (use_menu) - sigaction(SIGINT, &act, NULL); - else -#endif - { - act.sa_handler = CatchInterrupt; - sigaction(SIGINT, &act, NULL); - } + SetHandlers(); if (!strcmp(program_name, "listmaint")) menu = &list_menu; @@ -205,7 +190,7 @@ static void Usage(void) * Returns: doesn't */ -static void Signal_Handler(void) +static void Signal_Handler(int sig) { Put_message("Signal caught - exiting"); #ifdef HAVE_CURSES @@ -217,8 +202,41 @@ static void Signal_Handler(void) } -static void CatchInterrupt(void) +static void CatchInterrupt(int sig) { Put_message("Interrupt! Press RETURN to continue"); interrupt = 1; } + +#ifdef HAVE_POSIX_SIGNALS +static void SetHandlers(void) +{ + struct sigaction act; + + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + act.sa_handler = Signal_Handler; + sigaction(SIGHUP, &act, NULL); + sigaction(SIGQUIT, &act, NULL); +#ifdef HAVE_CURSES + if (use_menu) + sigaction(SIGINT, &act, NULL); + else +#endif + { + act.sa_handler = CatchInterrupt; + sigaction(SIGINT, &act, NULL); + } +} +#else +static void SetHandlers(void) +{ + signal(SIGTERM, Signal_Handler); +#ifdef HAVE_CURSES + if (use_menu) + signal(SIGINT, Signal_Handler); + else +#endif + signal(SIGINT, CatchInterrupt); +} +#endif diff --git a/clients/moira/menu.c b/clients/moira/menu.c index e171f544..e7ee80a6 100644 --- a/clients/moira/menu.c +++ b/clients/moira/menu.c @@ -19,7 +19,19 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif + +#ifdef _WIN32 +#include +#include +#ifdef getchar +#undef getchar +#endif +#define getchar() _getch() +#define getpid _getpid +#endif /* _WIN32 */ RCSID("$Header$"); @@ -457,11 +469,11 @@ void refresh_screen(void) /* Prompt the user for input in the input window of cur_ms */ int Prompt_input(char *prompt, char *buf, int buflen) { +#ifdef HAVE_CURSES int c; char *p; int y, x, oldx, oldy; -#ifdef HAVE_CURSES if (cur_ms != NULLMS) { more_flg = 1; @@ -641,9 +653,6 @@ void Put_message(char *msg) /* Will be truncated to COLS characters. */ void Put_line(char *msg) { - int y, x, i; - char *msg1, chr; - if (!more_flg) return; @@ -655,6 +664,9 @@ void Put_line(char *msg) #ifdef HAVE_CURSES if (cur_ms != NULLMS) { + int x, y; + char chr; + wstandout(cur_ms->ms_input); wprintw(cur_ms->ms_input, "---More---"); wstandend(cur_ms->ms_input); @@ -684,6 +696,9 @@ void Put_line(char *msg) #ifdef HAVE_CURSES if (cur_ms != NULLMS) { + int i; + char *msg1; + msg1 = calloc(COLS, 1); strncpy(msg1, msg, COLS - 1); for (i = strlen(msg1); i < COLS - 1; i++) @@ -766,13 +781,30 @@ struct menu_line *Find_command(Menu *m, char *command) return find_command_from(command, m, MAX_MENU_DEPTH); } +static char *get_tmp_dir(void) +{ +#ifdef _WIN32 + static char tmp[BUFSIZ]; + DWORD len; + if (!tmp[0]) + { + len = GetTempPath(sizeof(tmp), tmp); + if (!len || (len > sizeof(tmp))) + strcpy(tmp, "."); + } + return tmp; +#else + return "/var/tmp"; +#endif +} + int toggle_logging(int argc, char *argv[]) { char buf[BUFSIZ]; if (!log_file) { - sprintf(buf, "/var/tmp/%s-log.%ld", whoami, (long)getpid()); + sprintf(buf, "%s/%s-log.%ld", get_tmp_dir(), whoami, (long)getpid()); /* open the file */ log_file = fopen(buf, "a"); diff --git a/clients/moira/misc.c b/clients/moira/misc.c index 64d923c6..bba985f0 100644 --- a/clients/moira/misc.c +++ b/clients/moira/misc.c @@ -23,10 +23,12 @@ #include "globals.h" #include +#ifndef _WIN32 #include #include #include #include +#endif /* _WIN32 */ #include #include diff --git a/clients/moira/pobox.c b/clients/moira/pobox.c index ee86c403..4fc105a3 100644 --- a/clients/moira/pobox.c +++ b/clients/moira/pobox.c @@ -395,7 +395,6 @@ int SetUserPOBox(int argc, char **argv) { int status; char *type, temp_buf[BUFSIZ], *local_user, *args[10], *box; - char *temp_box; struct mqelem *top = NULL; local_user = argv[1]; diff --git a/clients/moira/printer.c b/clients/moira/printer.c index 5637ef40..8b0f031c 100644 --- a/clients/moira/printer.c +++ b/clients/moira/printer.c @@ -207,7 +207,6 @@ static char **AskPrnInfo(char **info) { char temp_buf[BUFSIZ]; char *args[3], *lpc_acl; - char *s, *d; int status; Put_message(""); @@ -526,7 +525,6 @@ int GetPrintSrv(int argc, char **argv) static char *PrintPrintSrvInfo(char **info) { char buf[BUFSIZ]; - int status; if (!info) /* If no informaion */ { @@ -575,9 +573,6 @@ static char **SetPrintSrvDefaults(char **info, char *name) static char **AskPrintSrvInfo(char **info) { char buf[BUFSIZ]; - char *args[3], *lpc_acl; - char *s, *d; - int status; Put_message(""); sprintf(buf, "Print Server entry for %s.", info[PRINTSERVER_HOST]); @@ -644,7 +639,7 @@ int AddPrintSrv(int argc, char **argv) int ChangePrintSrv(int argc, char **argv) { - char *name, **args; + char *name; struct mqelem *elem = NULL; int stat; diff --git a/clients/moira/user.c b/clients/moira/user.c index 4503c5e7..56a42d41 100644 --- a/clients/moira/user.c +++ b/clients/moira/user.c @@ -19,8 +19,6 @@ #include "f_defs.h" #include "globals.h" -#include - #include #include #include @@ -188,7 +186,7 @@ void CorrectCapitalization(char **name) char **AskUserInfo(char **info, Bool name) { - int i, state; + int state; char temp_buf[BUFSIZ], *newname; if (name) @@ -713,7 +711,7 @@ static void RealDeactivateUser(char **info, Bool one_item) FreeAndClear(&args[L_MODTIME], TRUE); FreeAndClear(&args[L_MODBY], TRUE); FreeAndClear(&args[L_MODWITH], TRUE); - SlipInNewName(args, args[L_NAME]); + SlipInNewName(args, strdup(args[L_NAME])); if ((status = do_mr_query("update_list", CountArgs(args), args, NULL, NULL))) { @@ -749,7 +747,7 @@ static void RealDeactivateUser(char **info, Bool one_item) FreeAndClear(&args[FS_MODTIME], TRUE); FreeAndClear(&args[FS_MODBY], TRUE); FreeAndClear(&args[FS_MODWITH], TRUE); - SlipInNewName(args, args[FS_NAME]); + SlipInNewName(args, strdup(args[FS_NAME])); if ((status = do_mr_query("update_filesys", CountArgs(args), args, NULL, NULL))) { diff --git a/clients/moira/utils.c b/clients/moira/utils.c index 716ade74..0a688e04 100644 --- a/clients/moira/utils.c +++ b/clients/moira/utils.c @@ -21,9 +21,11 @@ #include +#ifndef _WIN32 #include #include #include /* for gethostbyname. */ +#endif /* _WIN32 */ #include #include @@ -777,11 +779,15 @@ int GetTypeFromUser(char *prompt, char *tname, char **pointer) strcat(buffer, " ("); for (elem = GetTypeValues(tname); elem; elem = elem->q_forw) { + /* Make sure we don't blow up and get too long a prompt */ + if (strlen(buffer) > 64) + break; strcat(buffer, elem->q_data); if (elem->q_forw) strcat(buffer, ", "); } strcat(buffer, ")"); + /* Trim the prompt if it is too long */ if (strlen(buffer) > 64) sprintf(buffer, "%s (? for help)", prompt); if (GetValueFromUser(buffer, pointer) == SUB_ERROR) diff --git a/clients/mrcheck/mrcheck.c b/clients/mrcheck/mrcheck.c index 8d64a42f..e8285688 100644 --- a/clients/mrcheck/mrcheck.c +++ b/clients/mrcheck/mrcheck.c @@ -12,8 +12,6 @@ #include #include -#include - #include #include #include @@ -30,7 +28,7 @@ void usage(void); char *whoami; static int count = 0; -static struct timeval now; +static time_t now; struct service { char name[17]; @@ -78,7 +76,7 @@ int process_server(int argc, char **argv, void *sqv) disp_svc(argv, "Should this be enabled?\n"); else if (atoi(argv[SVC_ENABLE]) && 60 * atoi(argv[SVC_INTERVAL]) + 86400 + atoi(argv[SVC_DFCHECK]) - < now.tv_sec) + < now) disp_svc(argv, "Service has not been updated\n"); return MR_CONT; @@ -130,7 +128,7 @@ int process_host(int argc, char **argv, void *sqv) disp_sh(argv, "Should this be enabled?\n"); else if (atoi(argv[SH_ENABLE]) && update_int && 60 * atoi(update_int) + 86400 + atoi(argv[SH_LASTSUCCESS]) - < now.tv_sec) + < now) disp_sh(argv, "Host has not been updated\n"); return MR_CONT; @@ -187,7 +185,7 @@ int main(int argc, char *argv[]) goto punt; } - gettimeofday(&now, 0); + now = time(NULL); sq = sq_create(); /* Check services first */ diff --git a/clients/mrtest/mrtest.c b/clients/mrtest/mrtest.c index 268d784d..0f928875 100644 --- a/clients/mrtest/mrtest.c +++ b/clients/mrtest/mrtest.c @@ -17,7 +17,26 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif + +#ifdef HAVE_GETOPT_H +#include +#endif + +#ifdef _WIN32 +#include +#include +#define dup _dup +#define dup2 _dup2 +#define isatty _isatty +#define close _close +#define open _open +#define sigjmp_buf jmp_buf +#define siglongjmp longjmp +#define sigsetjmp(env, save) setjmp(env) +#endif /* _WIN32 */ #ifdef HAVE_READLINE #include "readline/readline.h" @@ -29,11 +48,12 @@ RCSID("$Header$"); int recursion = 0, quote_output = 0, interactive; int count, quit = 0, cancel = 0; char *whoami; + sigjmp_buf jb; #define MAXARGS 20 -void discard_input(void); +void discard_input(int sig); char *mr_gets(char *prompt, char *buf, size_t len); void execute_line(char *cmdbuf); int parse(char *buf, char *argv[MAXARGS]); @@ -51,11 +71,12 @@ void test_dcm(void); void test_script(int argc, char **argv); void test_list_requests(void); void test_version(int argc, char **argv); +void set_signal_handler(int, void (*handler)(int)); +void set_signal_blocking(int, int); int main(int argc, char **argv) { char cmdbuf[BUFSIZ]; - struct sigaction action; int c; whoami = argv[0]; @@ -83,10 +104,7 @@ int main(int argc, char **argv) rl_bind_key('\t', rl_insert); #endif - action.sa_handler = discard_input; - action.sa_flags = 0; - sigemptyset(&action.sa_mask); - sigaction(SIGINT, &action, NULL); + set_signal_handler(SIGINT, discard_input); sigsetjmp(jb, 1); while (!quit) @@ -99,7 +117,7 @@ int main(int argc, char **argv) exit(0); } -void discard_input(void) +void discard_input(int sig) { putc('\n', stdout); @@ -432,7 +450,6 @@ int print_reply(int argc, char **argv, void *help) void test_query(int argc, char **argv) { int status, help; - sigset_t sigs; if (argc < 2) { @@ -444,11 +461,9 @@ void test_query(int argc, char **argv) count = 0; /* Don't allow ^C during the query: it will confuse libmoira's internal state. (Yay static variables) */ - sigemptyset(&sigs); - sigaddset(&sigs, SIGINT); - sigprocmask(SIG_BLOCK, &sigs, NULL); + set_signal_blocking(SIGINT, 1); status = mr_query(argv[1], argc - 2, argv + 2, print_reply, &help); - sigprocmask(SIG_UNBLOCK, &sigs, NULL); + set_signal_blocking(SIGINT, 0); printf("%d tuple%s\n", count, ((count == 1) ? "" : "s")); if (status) com_err("moira (query)", status, ""); @@ -520,3 +535,44 @@ void test_version(int argc, char **argv) if (status) com_err("moira (version)", status, ""); } + +#ifdef HAVE_POSIX_SIGNALS + +void set_signal_handler(int sig, void (*handler)(int)) +{ + struct sigaction action; + + sigemptyset(&action.sa_mask); + action.sa_flags = 0; + action.sa_handler = handler; + sigaction(sig, &action, NULL); +} + +void set_signal_blocking(int sig, int block) +{ + sigset_t sigs; + sigemptyset(&sigs); + sigaddset(&sigs, sig); + sigprocmask(block ? SIG_BLOCK : SIG_UNBLOCK, &sigs, NULL); +} + +#else + +void set_signal_handler(int sig, void (*handler)(int)) +{ + signal(sig, handler); +} + +#ifdef _WIN32 +BOOL WINAPI blocking_handler(DWORD dwCtrlType) +{ + return(TRUE); +} + +void set_signal_blocking(int sig, int block) +{ + SetConsoleCtrlHandler(blocking_handler, block ? TRUE : FALSE); +} +#endif /* _WIN32 */ + +#endif /* HAVE_POSIX_SIGNALS */ diff --git a/clients/passwd/chfn.c b/clients/passwd/chfn.c index 5d6ac260..1943739a 100644 --- a/clients/passwd/chfn.c +++ b/clients/passwd/chfn.c @@ -75,7 +75,6 @@ int chfn(char *uname) int status; /* general purpose exit status */ int q_argc; /* argc for mr_query */ char *q_argv[F_END]; /* argv for mr_query */ - char *motd; /* for Moira server status */ int i; struct finger_info old_info; @@ -175,6 +174,7 @@ char *ask(char *question, char *def_val, int phone_num) char *result; int i; int dashes = FALSE; + int len; #define BLANK "none" @@ -192,7 +192,8 @@ char *ask(char *question, char *def_val, int phone_num) else result = buf; - for (i = 0; i < strlen(buf); i++) + len = strlen(buf); + for (i = 0; i < len; i++) { switch (buf[i]) { @@ -222,7 +223,8 @@ char *ask(char *question, char *def_val, int phone_num) if (phone_num && ok) { - for (i = 0; i < strlen(result); i++) + len = strlen(result); + for (i = 0; i < len; i++) { if (!isdigit(result[i]) && (result[i] != '-')) { diff --git a/clients/passwd/chpobox.c b/clients/passwd/chpobox.c index 78a657da..d76fffeb 100644 --- a/clients/passwd/chpobox.c +++ b/clients/passwd/chpobox.c @@ -26,11 +26,15 @@ #include #include -#include #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_GETOPT_H +#include +#endif RCSID("$Header$"); @@ -43,7 +47,6 @@ static int match; int main(int argc, char *argv[]) { - struct passwd *pwd; char *mrarg[3]; char *address, *uname; int c, setflag, splitflag, prevflag, status; diff --git a/clients/stella/stella.c b/clients/stella/stella.c index 1281c2dc..71b7ed1b 100644 --- a/clients/stella/stella.c +++ b/clients/stella/stella.c @@ -78,7 +78,7 @@ int main(int argc, char **argv) { int status, success; char **arg = argv; - char *server = NULL, *p; + char *server = NULL; /* clear all flags & lists */ info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0; @@ -138,12 +138,14 @@ int main(int argc, char **argv) else if (argis("S", "status")) { if (arg - argv < argc - 1) { int i; + int len; arg++; update_flag++; h_status = *arg; - for(i=0; i&6 echo "configure:2452: checking for $ac_func" >&5 @@ -2501,6 +2501,179 @@ fi done +echo $ac_n "checking for sigaction""... $ac_c" 1>&6 +echo "configure:2506: checking for sigaction" >&5 +if eval "test \"`echo '$''{'ac_cv_func_sigaction'+set}'`\" = set"; then + echo $ac_n "(cached) $ac_c" 1>&6 +else + cat > conftest.$ac_ext < +/* Override any gcc2 internal prototype to avoid an error. */ +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char sigaction(); + +int main() { + +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined (__stub_sigaction) || defined (__stub___sigaction) +choke me +#else +sigaction(); +#endif + +; return 0; } +EOF +if { (eval echo configure:2534: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then + rm -rf conftest* + eval "ac_cv_func_sigaction=yes" +else + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + eval "ac_cv_func_sigaction=no" +fi +rm -f conftest* +fi + +if eval "test \"`echo '$ac_cv_func_'sigaction`\" = yes"; then + echo "$ac_t""yes" 1>&6 + cat >> confdefs.h <<\EOF +#define HAVE_POSIX_SIGNALS 1 +EOF + +else + echo "$ac_t""no" 1>&6 +fi + + +echo $ac_n "checking how to run the C preprocessor""... $ac_c" 1>&6 +echo "configure:2558: checking how to run the C preprocessor" >&5 +# On Suns, sometimes $CPP names a directory. +if test -n "$CPP" && test -d "$CPP"; then + CPP= +fi +if test -z "$CPP"; then +if eval "test \"`echo '$''{'ac_cv_prog_CPP'+set}'`\" = set"; then + echo $ac_n "(cached) $ac_c" 1>&6 +else + # This must be in double quotes, not single quotes, because CPP may get + # substituted into the Makefile and "${CC-cc}" will confuse make. + CPP="${CC-cc} -E" + # On the NeXT, cc -E runs the code through the compiler's parser, + # not just through cpp. + cat > conftest.$ac_ext < +Syntax Error +EOF +ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" +{ (eval echo configure:2579: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` +if test -z "$ac_err"; then + : +else + echo "$ac_err" >&5 + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + CPP="${CC-cc} -E -traditional-cpp" + cat > conftest.$ac_ext < +Syntax Error +EOF +ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" +{ (eval echo configure:2596: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` +if test -z "$ac_err"; then + : +else + echo "$ac_err" >&5 + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + CPP="${CC-cc} -nologo -E" + cat > conftest.$ac_ext < +Syntax Error +EOF +ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" +{ (eval echo configure:2613: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` +if test -z "$ac_err"; then + : +else + echo "$ac_err" >&5 + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + CPP=/lib/cpp +fi +rm -f conftest* +fi +rm -f conftest* +fi +rm -f conftest* + ac_cv_prog_CPP="$CPP" +fi + CPP="$ac_cv_prog_CPP" +else + ac_cv_prog_CPP="$CPP" +fi +echo "$ac_t""$CPP" 1>&6 + +for ac_hdr in unistd.h getopt.h +do +ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'` +echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6 +echo "configure:2641: checking for $ac_hdr" >&5 +if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then + echo $ac_n "(cached) $ac_c" 1>&6 +else + cat > conftest.$ac_ext < +EOF +ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out" +{ (eval echo configure:2651: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; } +ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"` +if test -z "$ac_err"; then + rm -rf conftest* + eval "ac_cv_header_$ac_safe=yes" +else + echo "$ac_err" >&5 + echo "configure: failed program was:" >&5 + cat conftest.$ac_ext >&5 + rm -rf conftest* + eval "ac_cv_header_$ac_safe=no" +fi +rm -f conftest* +fi +if eval "test \"`echo '$ac_cv_header_'$ac_safe`\" = yes"; then + echo "$ac_t""yes" 1>&6 + ac_tr_hdr=HAVE_`echo $ac_hdr | sed 'y%abcdefghijklmnopqrstuvwxyz./-%ABCDEFGHIJKLMNOPQRSTUVWXYZ___%'` + cat >> confdefs.h <&6 +fi +done + + case $PRO_C_INCLUDES in *INCLUDE='$(srcdir)'*) ;; @@ -2771,6 +2944,7 @@ s%@READLINE_LIBS@%$READLINE_LIBS%g s%@CURSES_CPPFLAGS@%$CURSES_CPPFLAGS%g s%@CURSES_LIBS@%$CURSES_LIBS%g s%@CURSES_SUBDIRS@%$CURSES_SUBDIRS%g +s%@CPP@%$CPP%g s%@KLIBS@%$KLIBS%g s%@MR_LIBDEP@%$MR_LIBDEP%g diff --git a/configure.in b/configure.in index ab760569..63c57460 100755 --- a/configure.in +++ b/configure.in @@ -352,7 +352,11 @@ AC_SUBST(CURSES_LIBS) AC_SUBST(CURSES_SUBDIRS) -AC_CHECK_FUNCS(getusershell strlcpy strlcat) +AC_CHECK_FUNCS(getusershell strlcpy strlcat uname) + +AC_CHECK_FUNC(sigaction, AC_DEFINE(HAVE_POSIX_SIGNALS)) + +AC_CHECK_HEADERS(unistd.h getopt.h) MR_EXTEND(PRO_C_INCLUDES, INCLUDE='$(srcdir)') MR_INCLUDE('$(SRCTOP)/include') diff --git a/include/moira.h b/include/moira.h index 2563650a..47aba26a 100644 --- a/include/moira.h +++ b/include/moira.h @@ -7,13 +7,20 @@ #ifndef _moira_h_ #define _moira_h_ -/* return values from queries (and error codes) */ +#ifdef _WIN32 +#include +#ifndef strcasecmp +#define strcasecmp stricmp +#endif +#endif /*_WIN32 */ +/* return values from queries (and error codes) */ #include #include #include "mr_et.h" #include "krb_et.h" #include "ureg_err.h" + #define MR_SUCCESS 0 /* Query was successful */ #define MR_VERSION_1 1 /* Version in use from 7/87 to 4/88 */ @@ -125,6 +132,7 @@ void sq_destroy(struct save_queue *sq); char *strtrim(char *s); char *uppercase(char *s); char *lowercase(char *s); + #ifndef HAVE_STRLCPY size_t strlcpy(char *dst, const char *src, size_t size); #endif diff --git a/lib/fixhost.c b/lib/fixhost.c index 6bdb70f9..ae574ada 100644 --- a/lib/fixhost.c +++ b/lib/fixhost.c @@ -11,11 +11,16 @@ #include #include -#include + +#ifdef HAVE_UNAME #include +#endif +#ifndef _WIN32 +#include #include #include +#endif /* _WIN32 */ #include #include @@ -24,6 +29,20 @@ RCSID("$Header$"); +static struct hostent *local_gethostbyname(void) +{ +#ifdef HAVE_UNAME + struct utsname name; + uname(&name); + return gethostbyname(name.nodename); +#else + char hostname[128]; + gethostname(hostname, sizeof(hostname)); + hostname[sizeof(hostname)-1] = 0; + return gethostbyname(hostname); +#endif +} + static char *local_domain(void) { static char *domain = NULL; @@ -42,9 +61,7 @@ static char *local_domain(void) } else { - struct utsname name; - uname(&name); - hp = gethostbyname(name.nodename); + hp = local_gethostbyname(); if (hp) { cp = strchr(hp->h_name, '.'); diff --git a/lib/mr_call.c b/lib/mr_call.c index f6f2bfd1..f226e682 100644 --- a/lib/mr_call.c +++ b/lib/mr_call.c @@ -12,10 +12,15 @@ #include "mr_private.h" #include -#include #include #include +#ifndef _WIN32 +#include +#endif /* _WIN32 */ + +#ifdef HAVE_UNISTD_H #include +#endif RCSID("$Header$"); @@ -53,7 +58,8 @@ int mr_do_call(struct mr_params *params, struct mr_params *reply) int mr_send(int fd, struct mr_params *params) { - u_long length, written; + u_long length; + int written; int i, *argl; char *buf, *p; @@ -99,12 +105,12 @@ int mr_send(int fd, struct mr_params *params) length = p - buf; putlong(buf, length); - written = write(fd, buf, length); + written = send(fd, buf, length, 0); free(buf); if (!params->mr_argl) free(argl); - if (written != length) + if (written != (int)length) return MR_ABORTED; else return MR_SUCCESS; @@ -127,10 +133,11 @@ int mr_receive(int fd, struct mr_params *reply) * on failure, or -1 if the packet hasn't been completely received * yet. */ + int mr_cont_receive(int fd, struct mr_params *reply) { u_long length, data; - ssize_t size, more; + int size, more; char *p, *end; int i; @@ -138,7 +145,7 @@ int mr_cont_receive(int fd, struct mr_params *reply) { char lbuf[4]; - size = read(fd, lbuf, 4); + size = recv(fd, lbuf, 4, 0); if (size != 4) return size ? MR_ABORTED : MR_NOT_CONNECTED; getlong(lbuf, length); @@ -155,8 +162,8 @@ int mr_cont_receive(int fd, struct mr_params *reply) else getlong(reply->mr_flattened, length); - more = read(fd, reply->mr_flattened + reply->mr_filled, - length - reply->mr_filled); + more = recv(fd, reply->mr_flattened + reply->mr_filled, + length - reply->mr_filled, 0); if (more == -1) { mr_destroy_reply(*reply); @@ -177,7 +184,7 @@ int mr_cont_receive(int fd, struct mr_params *reply) getlong(reply->mr_flattened + 8, reply->u.mr_status); getlong(reply->mr_flattened + 12, reply->mr_argc); - if (reply->mr_argc > (length - 16) / 8) + if (reply->mr_argc > ((int)length - 16) / 8) { mr_destroy_reply(*reply); return MR_INTERNAL; diff --git a/lib/mr_connect.c b/lib/mr_connect.c index 26aaf0ed..5077a5ce 100644 --- a/lib/mr_connect.c +++ b/lib/mr_connect.c @@ -14,15 +14,26 @@ #include "mr_private.h" #include -#include - -#include -#include #include #include #include + +#ifdef HAVE_UNISTD_H #include +#endif + +#ifndef _WIN32 +#include +#include +#include +#ifndef closesocket +#define closesocket close +#endif +#ifndef SOCKET_ERROR +#define SOCKET_ERROR -1 +#endif +#endif #ifdef HAVE_HESIOD #include @@ -30,6 +41,9 @@ RCSID("$Header$"); +#define DEFAULT_SERV "moira_db" +#define DEFAULT_PORT 775 + int _mr_conn = 0; static char *mr_server_host = NULL; @@ -119,76 +133,80 @@ int mr_connect(char *server) int mr_connect_internal(char *server, char *port) { - int fd, size, more; + int size, more; struct sockaddr_in target; struct hostent *shost; char actualresponse[53]; + char *host = NULL; + int fd = SOCKET_ERROR; + int ok = 0; shost = gethostbyname(server); if (!shost) - return 0; + goto cleanup; + + /* Get the host info in case some library decides to clobber shost. */ + memcpy(&target.sin_addr, shost->h_addr, shost->h_length); + target.sin_family = shost->h_addrtype; + host = strdup(shost->h_name); if (port[0] == '#') - target.sin_port = htons(atoi(port + 1)); + target.sin_port = htons((unsigned short)atoi(port + 1)); else { struct servent *s; + target.sin_port = 0; s = getservbyname(port, "tcp"); if (s) target.sin_port = s->s_port; - else #ifdef HAVE_HESIOD + if (!target.sin_port) { s = hes_getservbyname(port, "tcp"); if (s) target.sin_port = s->s_port; - else - return 0; } -#else - return 0; #endif + if (!target.sin_port && !strcasecmp(port, DEFAULT_SERV)) + target.sin_port = htons(DEFAULT_PORT); + if (!target.sin_port) + goto cleanup; } - memcpy(&target.sin_addr, shost->h_addr, shost->h_length); - target.sin_family = shost->h_addrtype; - fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) - return 0; + goto cleanup; if (connect(fd, (struct sockaddr *)&target, sizeof(target)) < 0) - { - close(fd); - return 0; - } + goto cleanup; /* Do magic mrgdb initialization */ - size = write(fd, challenge, sizeof(challenge)); + size = send(fd, challenge, sizeof(challenge), 0); if (size != sizeof(challenge)) - { - close(fd); - return 0; - } + goto cleanup; for (size = 0; size < sizeof(actualresponse); size += more) { - more = read(fd, actualresponse + size, sizeof(actualresponse) - size); + more = recv(fd, actualresponse + size, sizeof(actualresponse) - size, 0); if (more <= 0) break; } if (size != sizeof(actualresponse)) - { - close(fd); - return 0; - } + goto cleanup; if (memcmp(actualresponse, response, sizeof(actualresponse))) + goto cleanup; + + ok = 1; + mr_server_host = host; + + cleanup: + if (!ok) { - close(fd); + if (host) + free(host); + if (fd != SOCKET_ERROR) + closesocket(fd); return 0; } - - mr_server_host = strdup(shost->h_name); - /* You win */ return fd; } @@ -196,7 +214,7 @@ int mr_connect_internal(char *server, char *port) int mr_disconnect(void) { CHECK_CONNECTED; - close(_mr_conn); + closesocket(_mr_conn); _mr_conn = 0; free(mr_server_host); mr_server_host = NULL; @@ -258,17 +276,17 @@ int mr_listen(char *port) return -1; if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int)) < 0) { - close(s); + closesocket(s); return -1; } if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) { - close(s); + closesocket(s); return -1; } if (listen(s, 5) < 0) { - close(s); + closesocket(s); return -1; } @@ -311,9 +329,9 @@ int mr_cont_accept(int conn, char **buf, int *nread) if (!*buf) { char lbuf[4]; - if (read(conn, lbuf, 4) != 4) + if (recv(conn, lbuf, 4, 0) != 4) { - close(conn); + closesocket(conn); return 0; } getlong(lbuf, len); @@ -322,7 +340,7 @@ int mr_cont_accept(int conn, char **buf, int *nread) *buf = malloc(len); if (!*buf || len < 58) { - close(conn); + closesocket(conn); free(*buf); return 0; } @@ -333,11 +351,11 @@ int mr_cont_accept(int conn, char **buf, int *nread) else getlong(*buf, len); - more = read(conn, *buf + *nread, len - *nread); + more = recv(conn, *buf + *nread, len - *nread, 0); if (more == -1 && errno != EINTR) { - close(conn); + closesocket(conn); free(*buf); return 0; } @@ -349,7 +367,7 @@ int mr_cont_accept(int conn, char **buf, int *nread) if (memcmp(*buf + 4, challenge + 4, 34)) { - close(conn); + closesocket(conn); free(*buf); return 0; } @@ -357,9 +375,9 @@ int mr_cont_accept(int conn, char **buf, int *nread) /* good enough */ free(*buf); - if (write(conn, response, sizeof(response)) != sizeof(response)) + if (send(conn, response, sizeof(response), 0) != sizeof(response)) { - close(conn); + closesocket(conn); return 0; } return conn; diff --git a/lib/mr_private.h b/lib/mr_private.h index 90a3b304..0838b40e 100644 --- a/lib/mr_private.h +++ b/lib/mr_private.h @@ -12,8 +12,10 @@ #include +#ifndef _WIN32 #include #include +#endif /*_WIN32*/ extern int _mr_conn, mr_inited; @@ -32,7 +34,7 @@ typedef struct mr_params { #define CHECK_CONNECTED if (!_mr_conn) return MR_NOT_CONNECTED #define getlong(cp, l) do { l = ((((unsigned char *)cp)[0] * 256 + ((unsigned char *)cp)[1]) * 256 + ((unsigned char *)cp)[2]) * 256 + ((unsigned char *)cp)[3]; } while(0) -#define putlong(cp, l) do { ((unsigned char *)cp)[0] = l >> 24; ((unsigned char *)cp)[1] = l >> 16; ((unsigned char *)cp)[2] = l >> 8; ((unsigned char *)cp)[3] = l; } while(0) +#define putlong(cp, l) do { ((unsigned char *)cp)[0] = (unsigned char)(l >> 24); ((unsigned char *)cp)[1] = (unsigned char)(l >> 16); ((unsigned char *)cp)[2] = (unsigned char)(l >> 8); ((unsigned char *)cp)[3] = (unsigned char)(l); } while(0) /* prototypes from mr_call.h */ int mr_do_call(struct mr_params *params, struct mr_params *reply); -- 2.45.2