added custom error messages (fixed existing implementation)
[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     char str[600];
112 };
113 extern const struct iauth_result *iauth_query(struct iauth_client *cli);
114 extern char iauth_servname[IAUTH_DATALEN + 1];
115
116 /* Sends a specific request to the ircd.
117  * This is a less generic but easier to use interface
118  * for the iauth_[vf]send() commands. This interface also
119  * correctly sends statistics.
120  */
121
122 /* Operator Notification: > :<message text> */
123 extern void iauth_query_fnotify(const char *format, ...);
124 extern void iauth_query_vnotify(const char *format, va_list list);
125 /* Set Debug Level: G <level> */
126 extern void iauth_query_debug(unsigned int debuglevel);
127 /* Set Policy Options: O <options> */
128 extern void iauth_query_policy(const char *policy);
129 /* iauth Program Version: V :<version string> */
130 extern void iauth_query_version(const char *version);
131 /* Start of new configuration: a */
132 extern void iauth_query_newconf();
133 /* Configuration Information: A <hosts?> <module> :<options> */
134 extern void iauth_query_config(const char *hosts, const char *module, const char *value);
135 /* Start of new statistics: s */
136 extern void iauth_query_newstats();
137 /* Statistics Information: S <module> :<module information> */
138 extern void iauth_query_stats(const char *module, const char *value);
139 /* Forced Username: o <id> <remoteip> <remoteport> <username> */
140 extern void iauth_query_set_username(signed int id, const char *username);
141 /* Trusted Username: U <id> <remoteip> <remoteport> <username> */
142 extern void iauth_query_trust_username(signed int id, const char *username);
143 /* Untrusted Username: u <id> <remoteip> <remoteport> <username> */
144 extern void iauth_query_distrust_username(signed int id, const char *username);
145 /* Client Hostname: N <id> <remoteip> <remoteport> <hostname> */
146 extern void iauth_query_sethost(signed int id, const char *hostname);
147 /* Client IP Address: I <id> <currentip> <remoteport> <newip> */
148 extern void iauth_query_setip(signed int id, const char *ip);
149 /* Adjust User Mode: M <id> <remoteip> <remoteport> +<mode changes> */
150 extern void iauth_query_setmodes(signed int id, const char *modes);
151 /* Challenge User: C <id> <remoteip> <remoteport> :<challenge string> */
152 extern void iauth_query_challenge(signed int id, const char *challenge);
153 /* Quietly Kill Client: k <id> <remoteip> <remoteport> :<reason> */
154 extern void iauth_query_reject(signed int id, const char *reason);
155 /* Kill Client: K <id> <remoteip> <remoteport> :<reason> */
156 extern void iauth_query_kill(signed int id, const char *reason);
157 /* Done Checking: D <id> <remoteip> <remoteport> [class] */
158 extern void iauth_query_assign(signed int id, const char *cclass);
159 /* Registered User: R <id> <remoteip> <remoteport> <account> [class] */
160 extern void iauth_query_register(signed int id, const char *account, const char *cclass);
161
162 /* Subcommand handlers. */
163 extern void iauth_cmd_C(signed int id, char *arg);
164 extern void iauth_cmd_D(struct iauth_client *client);
165 extern void iauth_cmd_L(struct iauth_client *client, char *arg);
166 extern void iauth_cmd_H(struct iauth_client *client, char *arg);
167 extern void iauth_cmd_M(char *arg);
168 extern void iauth_cmd_N(struct iauth_client *client, char *arg);
169 extern void iauth_cmd_d(struct iauth_client *client);
170 extern void iauth_cmd_P(struct iauth_client *client, char *arg);
171 extern void iauth_cmd_U(struct iauth_client *client, char *arg);
172 extern void iauth_cmd_u(struct iauth_client *client, char *arg);
173 extern void iauth_cmd_n(struct iauth_client *client, char *arg);
174 extern void iauth_cmd_T(struct iauth_client *client);
175 extern void iauth_cmd_E(signed int id, char *arg);
176
177 /* Statistics */
178 extern void iauth_stats_report();
179 extern void iauth_stats_loc_allow();
180 extern void iauth_stats_loc_deny();
181 extern void iauth_stats_def_allow();
182 extern void iauth_stats_def_deny();
183