2e0abf65a138eeb8a218a3a9730d0b70cb944bef
[iauth.git] / iauth.h
1 /*
2  * Written by David Herrmann.
3  * Dedicated to the Public Domain.
4  */
5
6 #include <stdlib.h>
7 #include <stdarg.h>
8 #include <string.h>
9
10 /* Global configuration.
11  * It is not recommended to change these values. They are set to
12  * an especially high value to support even the weirdest environments.
13  */
14 /* Maximal length of a line. */
15 #define IAUTH_LINE 4096
16 /* Maximal capacity. */
17 #define IAUTH_CAPMAX 1000000
18 /* Maximum length of accounts/classes/fakehosts etc.. */
19 #define IAUTH_DATALEN 256
20
21 /* 1 if debug mode is enabled, otherwise 0. */
22 extern unsigned int iauth_debug;
23
24 /* Log functions.
25  * These functions write status data into a log file.
26  * They all do the same but take either a formatted argument
27  * or a va_list buffer.
28  * Error functions log the message and terminate the application,
29  * log functions only log the message.
30  */
31 #define IAUTH_FATAL 0
32 #define IAUTH_WARNING 1
33 #define IAUTH_INFO 2
34 #define IAUTH_DEBUG 3
35 extern const char *iauth_logfile;
36 extern void iauth_ferror(const char *format, ...);
37 extern void iauth_verror(const char *format, va_list list);
38 extern void iauth_flog(unsigned int type, const char *format, ...);
39 extern void iauth_vlog(unsigned int type, const char *format, va_list list);
40 extern void iauth_eflog(const char *format, ...);
41 extern void iauth_evlog(const char *format, va_list list);
42
43 /* IO functions.
44  * These functions either write a line to the IAuth or read a
45  * single line from the IAuth.
46  */
47 extern char *iauth_read();
48 extern void iauth_fsend(const char *format, ...);
49 extern void iauth_vsend(const char *format, va_list list);
50
51 /* Allocates/frees memory.
52  * Aborts with an appropriate message if the allocation fails.
53  */
54 static inline void *iauth_malloc(size_t size) {
55     void *mem;
56     if(!(mem = malloc(size))) iauth_eflog("Memory allocation failed.");
57     memset(mem, 0, size);
58     return mem;
59 }
60 static inline char *iauth_strdup(const char *str) {
61     char *mem;
62     if(!(mem = strdup(str))) iauth_eflog("strdup() failed.");
63     return mem;
64 }
65 #define iauth_free(x) ((void)((x)?free(x):0))
66
67 /* User management.
68  * Adds or removes a user.
69  */
70 struct iauth_client {
71     signed int id;
72     char *ip;
73     unsigned short port;
74     char *lo_ip;
75     unsigned short lo_port;
76     char *host;
77     char *c_host;
78     char *c_serv;
79     char *nick;
80     char *username;
81     char *realname;
82     char *account;
83     char *fakehost;
84     char *cclass;
85     char *password;
86     char *ident;
87     unsigned int state_r : 1; /* Is in "registering" state. */
88     unsigned int state_h : 1; /* Is in "hurry" state. */
89 };
90 #define iauth_set(x, y) (iauth_free(x), (x = y))
91 extern struct iauth_client *iauth_clients;
92 extern unsigned int iauth_clients_size;
93 extern void iauth_setcap(unsigned int cap);
94 extern void iauth_addid(signed int id);
95 extern void iauth_delid(signed int id);
96
97 /* Request handler.
98  * The real requests are outsourced to a scriptfile.
99  * The iauth process spawns the scriptfile, passes the
100  * data as arguments and exspects the process to return
101  * the result on stdout.
102  * The scriptfile is spawned for every request.
103  */
104 extern char *iauth_scriptfile;
105 struct iauth_result {
106     char cclass[IAUTH_DATALEN + 1];
107     char ident[IAUTH_DATALEN + 1];
108     char host[IAUTH_DATALEN + 1];
109     char ip[IAUTH_DATALEN + 1];
110     char modes[IAUTH_DATALEN + 1];
111 };
112 extern const struct iauth_result *iauth_query(struct iauth_client *cli);
113 extern char iauth_servname[IAUTH_DATALEN + 1];
114
115 /* Sends a specific request to the ircd.
116  * This is a less generic but easier to use interface
117  * for the iauth_[vf]send() commands. This interface also
118  * correctly sends statistics.
119  */
120
121 /* Operator Notification: > :<message text> */
122 extern void iauth_query_fnotify(const char *format, ...);
123 extern void iauth_query_vnotify(const char *format, va_list list);
124 /* Set Debug Level: G <level> */
125 extern void iauth_query_debug(unsigned int debuglevel);
126 /* Set Policy Options: O <options> */
127 extern void iauth_query_policy(const char *policy);
128 /* iauth Program Version: V :<version string> */
129 extern void iauth_query_version(const char *version);
130 /* Start of new configuration: a */
131 extern void iauth_query_newconf();
132 /* Configuration Information: A <hosts?> <module> :<options> */
133 extern void iauth_query_config(const char *hosts, const char *module, const char *value);
134 /* Start of new statistics: s */
135 extern void iauth_query_newstats();
136 /* Statistics Information: S <module> :<module information> */
137 extern void iauth_query_stats(const char *module, const char *value);
138 /* Forced Username: o <id> <remoteip> <remoteport> <username> */
139 extern void iauth_query_set_username(signed int id, const char *username);
140 /* Trusted Username: U <id> <remoteip> <remoteport> <username> */
141 extern void iauth_query_trust_username(signed int id, const char *username);
142 /* Untrusted Username: u <id> <remoteip> <remoteport> <username> */
143 extern void iauth_query_distrust_username(signed int id, const char *username);
144 /* Client Hostname: N <id> <remoteip> <remoteport> <hostname> */
145 extern void iauth_query_sethost(signed int id, const char *hostname);
146 /* Client IP Address: I <id> <currentip> <remoteport> <newip> */
147 extern void iauth_query_setip(signed int id, const char *ip);
148 /* Adjust User Mode: M <id> <remoteip> <remoteport> +<mode changes> */
149 extern void iauth_query_setmodes(signed int id, const char *modes);
150 /* Challenge User: C <id> <remoteip> <remoteport> :<challenge string> */
151 extern void iauth_query_challenge(signed int id, const char *challenge);
152 /* Quietly Kill Client: k <id> <remoteip> <remoteport> :<reason> */
153 extern void iauth_query_reject(signed int id, const char *reason);
154 /* Kill Client: K <id> <remoteip> <remoteport> :<reason> */
155 extern void iauth_query_kill(signed int id, const char *reason);
156 /* Done Checking: D <id> <remoteip> <remoteport> [class] */
157 extern void iauth_query_assign(signed int id, const char *cclass);
158 /* Registered User: R <id> <remoteip> <remoteport> <account> [class] */
159 extern void iauth_query_register(signed int id, const char *account, const char *cclass);
160
161 /* Subcommand handlers. */
162 extern void iauth_cmd_C(signed int id, char *arg);
163 extern void iauth_cmd_D(struct iauth_client *client);
164 extern void iauth_cmd_L(struct iauth_client *client, char *arg);
165 extern void iauth_cmd_H(struct iauth_client *client, char *arg);
166 extern void iauth_cmd_M(char *arg);
167 extern void iauth_cmd_N(struct iauth_client *client, char *arg);
168 extern void iauth_cmd_d(struct iauth_client *client);
169 extern void iauth_cmd_P(struct iauth_client *client, char *arg);
170 extern void iauth_cmd_U(struct iauth_client *client, char *arg);
171 extern void iauth_cmd_u(struct iauth_client *client, char *arg);
172 extern void iauth_cmd_n(struct iauth_client *client, char *arg);
173 extern void iauth_cmd_T(struct iauth_client *client);
174 extern void iauth_cmd_E(signed int id, char *arg);
175
176 /* Statistics */
177 extern void iauth_stats_report();
178 extern void iauth_stats_loc_allow();
179 extern void iauth_stats_loc_deny();
180 extern void iauth_stats_def_allow();
181 extern void iauth_stats_def_deny();
182