📄 env_nt.c
字号:
/* ======================================================================== * Copyright 1988-2008 University of Washington * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * * ======================================================================== *//* * Program: NT environment routines * * Author: Mark Crispin * UW Technology * University of Washington * Seattle, WA 98195 * Internet: MRC@Washington.EDU * * Date: 1 August 1988 * Last Edited: 15 February 2008 */static char *myUserName = NIL; /* user name */static char *myLocalHost = NIL; /* local host name */static char *myHomeDir = NIL; /* home directory name */static char *myNewsrc = NIL; /* newsrc file name */static char *sysInbox = NIL; /* system inbox name */static long list_max_level = 5; /* maximum level of list recursion */ /* block environment init */static short block_env_init = NIL;static short no822tztext = NIL; /* disable RFC [2]822 timezone text */ /* home namespace */static NAMESPACE nshome = {"",'\\',NIL,NIL}; /* UNIX other user namespace */static NAMESPACE nsother = {"#user.",'\\',NIL,NIL}; /* namespace list */static NAMESPACE *nslist[3] = {&nshome,&nsother,NIL};static long alarm_countdown = 0;/* alarm count down */static void (*alarm_rang) (); /* alarm interrupt function */static unsigned int rndm = 0; /* initial `random' number */static int server_nli = 0; /* server and not logged in */static int logtry = 3; /* number of login tries */ /* block notification */static blocknotify_t mailblocknotify = mm_blocknotify; /* callback to get username */static userprompt_t mailusername = NIL;static long is_nt = -1; /* T if NT, NIL if not NT, -1 unknown */static HINSTANCE netapi = NIL;typedef NET_API_STATUS (CALLBACK *GETINFO) (LPCWSTR,LPCWSTR,DWORD,LPBYTE *);static GETINFO getinfo = NIL;#include "write.c" /* include safe writing routines */#include "pmatch.c" /* include wildcard pattern matcher *//* Get all authenticators */#include "auths.c"/* Environment manipulate parameters * Accepts: function code * function-dependent value * Returns: function-dependent return value */void *env_parameters (long function,void *value){ void *ret = NIL; switch ((int) function) { case GET_NAMESPACE: ret = (void *) nslist; break; case SET_USERPROMPT : mailusername = (userprompt_t) value; case GET_USERPROMPT : ret = (void *) mailusername; break; case SET_HOMEDIR: if (myHomeDir) fs_give ((void **) &myHomeDir); myHomeDir = cpystr ((char *) value); case GET_HOMEDIR: ret = (void *) myHomeDir; break; case SET_LOCALHOST: myLocalHost = cpystr ((char *) value); case GET_LOCALHOST: if (myLocalHost) fs_give ((void **) &myLocalHost); ret = (void *) myLocalHost; break; case SET_NEWSRC: if (myNewsrc) fs_give ((void **) &myNewsrc); myNewsrc = cpystr ((char *) value); case GET_NEWSRC: if (!myNewsrc) { /* set news file name if not defined */ char tmp[MAILTMPLEN]; sprintf (tmp,"%s\\NEWSRC",myhomedir ()); myNewsrc = cpystr (tmp); } ret = (void *) myNewsrc; break; case SET_SYSINBOX: if (sysInbox) fs_give ((void **) &sysInbox); sysInbox = cpystr ((char *) value); case GET_SYSINBOX: ret = (void *) sysInbox; break; case SET_LISTMAXLEVEL: list_max_level = (long) value; case GET_LISTMAXLEVEL: ret = (void *) list_max_level; break; case SET_DISABLE822TZTEXT: no822tztext = value ? T : NIL; case GET_DISABLE822TZTEXT: ret = (void *) (no822tztext ? VOIDT : NIL); break; case SET_BLOCKENVINIT: block_env_init = value ? T : NIL; case GET_BLOCKENVINIT: ret = (void *) (block_env_init ? VOIDT : NIL); break; case SET_BLOCKNOTIFY: mailblocknotify = (blocknotify_t) value; case GET_BLOCKNOTIFY: ret = (void *) mailblocknotify; break; } return ret;}/* Write current time * Accepts: destination string * optional format of day-of-week prefix * format of date and time * flag whether to append symbolic timezone */static void do_date (char *date,char *prefix,char *fmt,int suffix){ time_t tn = time (0); struct tm *t = gmtime (&tn); int zone = t->tm_hour * 60 + t->tm_min; int julian = t->tm_yday; t = localtime (&tn); /* get local time now */ /* minus UTC minutes since midnight */ zone = t->tm_hour * 60 + t->tm_min - zone; /* julian can be one of: * 36x local time is December 31, UTC is January 1, offset -24 hours * 1 local time is 1 day ahead of UTC, offset +24 hours * 0 local time is same day as UTC, no offset * -1 local time is 1 day behind UTC, offset -24 hours * -36x local time is January 1, UTC is December 31, offset +24 hours */ if (julian = t->tm_yday -julian) zone += ((julian < 0) == (abs (julian) == 1)) ? -24*60 : 24*60; if (prefix) { /* want day of week? */ sprintf (date,prefix,days[t->tm_wday]); date += strlen (date); /* make next sprintf append */ } /* output the date */ sprintf (date,fmt,t->tm_mday,months[t->tm_mon],t->tm_year+1900, t->tm_hour,t->tm_min,t->tm_sec,zone/60,abs (zone) % 60); if (suffix) { /* append timezone suffix if desired */ char *tz; tzset (); /* get timezone from TZ environment stuff */ tz = tzname[daylight ? (((struct tm *) t)->tm_isdst > 0) : 0]; if (tz && tz[0]) { char *s; for (s = tz; *s; s++) if (*s & 0x80) return; sprintf (date + strlen (date)," (%.50s)",tz); } }}/* Write current time in RFC 822 format * Accepts: destination string */void rfc822_date (char *date){ do_date (date,"%s, ","%d %s %d %02d:%02d:%02d %+03d%02d", no822tztext ? NIL : T);}/* Write current time in fixed-width RFC 822 format * Accepts: destination string */void rfc822_fixed_date (char *date){ do_date (date,NIL,"%02d %s %4d %02d:%02d:%02d %+03d%02d",NIL);}/* Write current time in internal format * Accepts: destination string */void internal_date (char *date){ do_date (date,NIL,"%02d-%s-%d %02d:%02d:%02d %+03d%02d",NIL);}/* Return random number */long random (void){ if (!rndm) srand (rndm = (unsigned) time (0L)); return (long) rand ();}/* Set alarm timer * Accepts: new value * Returns: old alarm value */long alarm (long seconds){ long ret = alarm_countdown; alarm_countdown = seconds; return ret;}/* The clock ticked */void CALLBACK clock_ticked (UINT IDEvent,UINT uReserved,DWORD dwUser, DWORD dwReserved1,DWORD dwReserved2){ if (alarm_rang && !--alarm_countdown) (*alarm_rang) ();}/* Initialize server * Accepts: server name for syslog or NIL * /etc/services service name or NIL * alternate /etc/services service name or NIL * clock interrupt handler * kiss-of-death interrupt handler * hangup interrupt handler * termination interrupt handler */void server_init (char *server,char *service,char *sslservice, void *clkint,void *kodint,void *hupint,void *trmint, void *staint){ if (!check_nt ()) { if (!auth_md5.server) fatal ("Can't run on Windows without MD5 database"); server_nli = T; /* Windows server not logged in */ } /* only do this if for init call */ if (server && service && sslservice) { long port; struct servent *sv; /* set server name in syslog */ openlog (server,LOG_PID,LOG_MAIL); fclose (stderr); /* possibly save a process ID */ /* Use SSL if SSL service, or if server starts with "s" and not service */ if (((port = tcp_serverport ()) >= 0)) { if ((sv = getservbyname (service,"tcp")) && (port == ntohs (sv->s_port))) syslog (LOG_DEBUG,"%s service init from %s",service,tcp_clientaddr ()); else if ((sv = getservbyname (sslservice,"tcp")) && (port == ntohs (sv->s_port))) { syslog (LOG_DEBUG,"%s SSL service init from %s",sslservice, tcp_clientaddr ()); ssl_server_init (server); } else { /* not service or SSL service port */ syslog (LOG_DEBUG,"port %ld service init from %s",port, tcp_clientaddr ()); if (*server == 's') ssl_server_init (server); } } /* make sure stdout does binary */ setmode (fileno (stdin),O_BINARY); setmode (fileno (stdout),O_BINARY); } alarm_rang = clkint; /* note the clock interrupt */ timeBeginPeriod (1000); /* set the timer interval */ timeSetEvent (1000,1000,clock_ticked,NIL,TIME_PERIODIC);}/* Wait for stdin input * Accepts: timeout in seconds * Returns: T if have input on stdin, else NIL */long server_input_wait (long seconds){ fd_set rfd,efd; struct timeval tmo; FD_ZERO (&rfd); FD_ZERO (&efd); FD_SET (0,&rfd); FD_SET (0,&efd); tmo.tv_sec = seconds; tmo.tv_usec = 0; return select (1,&rfd,0,&efd,&tmo) ? LONGT : NIL;}/* Server log in * Accepts: user name string * password string * authenticating user name string * argument count * argument vector * Returns: T if password validated, NIL otherwise */static int gotprivs = NIL; /* once-only flag to grab privileges */long server_login (char *user,char *pass,char *authuser,int argc,char *argv[]){ HANDLE hdl; LUID tcbpriv; TOKEN_PRIVILEGES tkp; char *s; /* need to get privileges? */ if (!gotprivs++ && check_nt ()) { /* hack for inetlisn */ if (argc >= 2) myClientHost = argv[1]; /* get process token and TCB priv value */ if (!(OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,&hdl) && LookupPrivilegeValue ((LPSTR) NIL,SE_TCB_NAME,&tcbpriv))) return NIL; tkp.PrivilegeCount = 1; /* want to enable this privilege */ tkp.Privileges[0].Luid = tcbpriv; tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; /* enable it */ AdjustTokenPrivileges (hdl,NIL,&tkp,sizeof (TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES) NIL,(PDWORD) NIL); /* make sure it won */ if (GetLastError() != ERROR_SUCCESS) return NIL; } /* cretins still haven't given up */ if ((strlen (user) >= MAILTMPLEN) || (authuser && (strlen (authuser) >= MAILTMPLEN))) syslog (LOG_ALERT,"SYSTEM BREAK-IN ATTEMPT, host=%.80s",tcp_clienthost ()); else if (logtry > 0) { /* still have available logins? */ /* authentication user not supported */ if (authuser && *authuser && compare_cstring (authuser,user)) mm_log ("Authentication id must match authorization id",ERROR); if (check_nt ()) { /* NT: authserver_login() call not supported */ if (!pass) mm_log ("Unsupported authentication mechanism",ERROR); else if (( /* try to login and impersonate the guy */#ifdef LOGIN32_LOGON_NETWORK LogonUser (user,".",pass,LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,&hdl) ||#endif LogonUser (user,".",pass,LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,&hdl) || LogonUser (user,".",pass,LOGON32_LOGON_BATCH, LOGON32_PROVIDER_DEFAULT,&hdl) || LogonUser (user,".",pass,LOGON32_LOGON_SERVICE, LOGON32_PROVIDER_DEFAULT,&hdl)) && ImpersonateLoggedOnUser (hdl)) return env_init (user,NIL); } else { /* Win9x: done if from authserver_login() */ if (!pass) server_nli = NIL; /* otherwise check MD5 database */ else if (s = auth_md5_pwd (user)) { /* change NLI state based on pwd match */ server_nli = strcmp (s,pass); memset (s,0,strlen (s));/* erase sensitive information */ fs_give ((void **) &s); /* flush erased password */ } /* success if no longer NLI */ if (!server_nli) return env_init (user,NIL); } } s = (logtry-- > 0) ? "Login failure" : "Excessive login attempts"; /* note the failure in the syslog */ syslog (LOG_INFO,"%s user=%.80s host=%.80s",s,user,tcp_clienthost ()); sleep (3); /* slow down possible cracker */ return NIL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -