1 /*
2  *  Copyright conserver.com, 2000
3  *
4  *  Maintainer/Enhancer: Bryan Stansell (bryan@conserver.com)
5  */
6 
7 #include <stdarg.h>
8 #if HAVE_OPENSSL
9 # include <openssl/ssl.h>
10 # include <openssl/bn.h>
11 # include <openssl/dh.h>
12 # include <openssl/err.h>
13 # if OPENSSL_VERSION_NUMBER < 0x10100000L
14 #  define TLS_method SSLv23_method
15 #  define CIPHER_SEC0
16 # else
17 #  define CIPHER_SEC0 ":@SECLEVEL=0"
18 # endif/* OPENSSL_VERSION_NUMBER < 0x10100000L */
19 #endif
20 #if HAVE_GSSAPI
21 # include <gssapi/gssapi.h>
22 #endif
23 
24 /* communication constants
25  */
26 #define OB_IAC		0xff	/* quote char                   */
27 #define OB_EXEC		'E'	/* exec a command on the client */
28 #define OB_GOTO		'G'	/* goto next console            */
29 #define OB_SUSP		'Z'	/* suspended by server          */
30 #define OB_ABRT		'.'	/* abort                        */
31 
32 /* Struct to wrap information about a "file"...
33  * This can be a socket, local file, whatever.  We do this so
34  * we can add encryption to sockets (and generalize I/O).
35  */
36 enum consFileType {
37     simpleFile,
38     simpleSocket,
39     simplePipe,
40 #if HAVE_OPENSSL
41     SSLSocket,
42 #endif
43     nothing
44 };
45 
46 typedef enum IOState {
47     ISDISCONNECTED = 0,
48     INCONNECT,
49     ISNORMAL,
50 #if HAVE_OPENSSL
51     INSSLACCEPT,
52     INSSLSHUTDOWN,
53 #endif
54 #if HAVE_GSSAPI
55     INGSSACCEPT,
56 #endif
57     ISFLUSHING
58 } IOSTATE;
59 
60 typedef enum flag {
61     FLAGUNKNOWN = 0,
62     FLAGTRUE,
63     FLAGFALSE
64 } FLAG;
65 
66 
67 typedef struct dynamicString {
68     char *string;
69     int used;
70     int allocated;
71     struct dynamicString *next;
72     struct dynamicString *prev;
73 } STRING;
74 
75 typedef struct consFile {
76     /* Standard socket type stuff */
77     enum consFileType ftype;
78     int fd;
79     int fdout;			/* only used when a simplePipe */
80     STRING *wbuf;
81     FLAG errored;
82     FLAG quoteiac;
83     FLAG sawiac;
84     FLAG sawiacsusp;
85     FLAG sawiacexec;
86     FLAG sawiacabrt;
87     FLAG sawiacgoto;
88 #if HAVE_OPENSSL
89     /* SSL stuff */
90     SSL *ssl;
91     FLAG waitForWrite;
92     FLAG waitForRead;
93 #endif
94     /* Add crypto stuff to suit */
95 #if DEBUG_CONSFILE_IO
96     int debugrfd;
97     int debugwfd;
98 #endif
99 } CONSFILE;
100 
101 typedef struct item {
102     char *id;
103     void (*reg)(char *);
104 } ITEM;
105 
106 typedef struct section {
107     char *id;
108     void (*begin)(char *);
109     void (*end)(void);
110     void (*abort)(void);
111     void (*destroy)(void);
112     ITEM *items;
113 } SECTION;
114 
115 typedef enum substToken {
116     ISNOTHING = 0,
117     ISNUMBER,
118     ISSTRING
119 } SUBSTTOKEN;
120 
121 typedef struct subst {
122     /* function to retrieve a token type based on a character
123      */
124     SUBSTTOKEN (*token)(char);
125     /* data for callback function
126      */
127     void *data;
128     /* function to retrieve a value (as a char* or int or both) for
129      * a substitution
130      */
131     int (*value)(char, char **, int *);
132 } SUBST;
133 
134 extern int isMultiProc, fDebug, fVerbose, fErrorPrinted;
135 extern char *progname;
136 extern pid_t thepid;
137 #define MAXHOSTNAME 1024
138 extern char myHostname[];
139 #if !USE_IPV6
140 extern struct in_addr *myAddrs;
141 #endif
142 extern fd_set rinit;
143 extern fd_set winit;
144 extern int maxfd;
145 extern int debugLineNo;
146 extern char *debugFileName;
147 extern int line;		/* used by ParseFile */
148 extern char *file;		/* used by ParseFile */
149 extern SECTION sections[];	/* used by ParseFile */
150 extern int isMaster;
151 
152 extern const char *StrTime(time_t *);
153 extern void Debug(int, char *, ...);
154 extern void Error(char *, ...);
155 extern void Msg(char *, ...);
156 extern void Verbose(char *, ...);
157 extern void SimpleSignal(int, RETSIGTYPE(*)(int));
158 extern int GetMaxFiles();
159 extern char *FmtCtl(int, STRING *);
160 extern void FmtCtlStr(char *, int, STRING *);
161 extern CONSFILE *FileOpenFD(int, enum consFileType);
162 extern CONSFILE *FileOpenPipe(int, int);
163 extern CONSFILE *FileOpen(const char *, int, int);
164 extern int FileClose(CONSFILE **);
165 extern int FileRead(CONSFILE *, void *, int);
166 extern int FileWrite(CONSFILE *, FLAG, char *, int);
167 extern void FileVWrite(CONSFILE *, FLAG, char *, va_list);
168 extern void FilePrint(CONSFILE *, FLAG, char *, ...);
169 extern int FileStat(CONSFILE *, struct stat *);
170 extern int FileSeek(CONSFILE *, off_t, int);
171 extern int FileSend(CONSFILE *, const void *, size_t, int);
172 extern int FileFDNum(CONSFILE *);
173 extern int FileFDOutNum(CONSFILE *);
174 extern int FileUnopen(CONSFILE *);
175 extern void OutOfMem();
176 extern char *BuildTmpString(const char *);
177 extern char *BuildTmpStringChar(const char);
178 extern char *BuildTmpStringPrint(char *, ...);
179 extern char *BuildString(const char *, STRING *);
180 extern char *BuildStringChar(const char, STRING *);
181 extern char *BuildStringPrint(STRING *, char *, ...);
182 extern char *BuildStringN(const char *, int, STRING *);
183 extern char *ShiftString(STRING *, int);
184 extern void InitString(STRING *);
185 extern void DestroyString(STRING *);
186 extern void DestroyStrings(void);
187 extern STRING *AllocString(void);
188 extern char *ReadLine(FILE *, STRING *, int *);
189 extern enum consFileType FileGetType(CONSFILE *);
190 extern void FileSetType(CONSFILE *, enum consFileType);
191 extern void FileSetQuoteIAC(CONSFILE *, FLAG);
192 extern FLAG FileSawQuoteSusp(CONSFILE *);
193 extern FLAG FileSawQuoteExec(CONSFILE *);
194 extern FLAG FileSawQuoteAbrt(CONSFILE *);
195 extern FLAG FileSawQuoteGoto(CONSFILE *);
196 extern void Bye(int);
197 extern void DestroyDataStructures(void);
198 extern int IsMe(char *);
199 extern char *PruneSpace(char *);
200 extern int FileCanRead(CONSFILE *, fd_set *, fd_set *);
201 extern int FileCanWrite(CONSFILE *, fd_set *, fd_set *);
202 extern int FileBufEmpty(CONSFILE *);
203 extern int SetFlags(int, int, int);
204 extern char *StrDup(const char *);
205 extern int ParseIACBuf(CONSFILE *, void *, int *);
206 extern void *MemMove(void *, void *, size_t);
207 extern char *StringChar(STRING *, int, char);
208 extern void ParseFile(char *, FILE *, int);
209 #if !USE_IPV6
210 extern void ProbeInterfaces(in_addr_t);
211 #endif
212 extern void ProcessSubst(SUBST *, char **, char **, char *, char *);
213 extern char *MyVersion(void);
214 extern unsigned int AtoU(char *);
215 extern void StrCpy(char *, const char *, unsigned int);
216 #if HAVE_OPENSSL
217 extern SSL *FileGetSSL(CONSFILE *);
218 extern void FileSetSSL(CONSFILE *, SSL *);
219 extern int SSLVerifyCallback(int, X509_STORE_CTX *);
220 extern int FileSSLAccept(CONSFILE *);
221 extern int FileCanSSLAccept(CONSFILE *, fd_set *, fd_set *);
222 #endif
223