1 
2 #ifndef _AFP_H_
3 #define _AFP_H_
4 
5 #include <arpa/inet.h>
6 #include <pthread.h>
7 #include <netdb.h>
8 #include <sys/statvfs.h>
9 #include <pwd.h>
10 #include <afpfs-ng/afp_protocol.h>
11 #include <afpfs-ng/libafpclient.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <unistd.h>
15 #include <netinet/in.h>
16 
17 
18 #define AFPFS_VERSION "0.8.1"
19 
20 /* This is the maximum AFP version this library supports */
21 #define AFP_MAX_SUPPORTED_VERSION 32
22 
23 /* afp_url is used to pass locations around */
24 struct afp_url {
25 	enum {TCPIP,AT} protocol;
26 	char username[AFP_MAX_USERNAME_LEN];
27 	char uamname[50];
28 	char password[AFP_MAX_PASSWORD_LEN];
29 	char servername[AFP_SERVER_NAME_UTF8_LEN];
30 	int port;
31 	char volumename[AFP_VOLUME_NAME_UTF8_LEN];
32 	char path[AFP_MAX_PATH];
33 
34 	int requested_version;
35 	char zone[AFP_ZONE_LEN]; /* Only used for Appletalk */
36 	char volpassword[9];;
37 };
38 
39 struct afp_token {
40 	unsigned int length;
41 	char data[AFP_TOKEN_MAX_LEN];
42 };
43 
44 #define SERVER_MAX_VERSIONS 10
45 #define SERVER_MAX_UAMS 10
46 
47 struct afp_rx_buffer {
48 	unsigned int size;
49 	unsigned int maxsize;
50 	char * data;
51 	int errorcode;
52 };
53 
54 
55 struct afp_file_info {
56 	unsigned short attributes;
57 	unsigned int did;
58 	unsigned int creation_date;
59 	unsigned int modification_date;
60 	unsigned int backup_date;
61 	unsigned int fileid;
62 	unsigned short offspring;
63 	char sync;
64 	char finderinfo[32];
65 	char name[AFP_MAX_PATH];
66 	char basename[AFP_MAX_PATH];
67 	char translated_name[AFP_MAX_PATH];
68 	struct afp_unixprivs unixprivs;
69 	unsigned int accessrights;
70 	struct afp_file_info * next;
71 	struct afp_file_info * largelist_next;
72 	unsigned char isdir;
73 	unsigned long long size;
74 	unsigned short resourcesize;
75 	unsigned int resource;
76 	unsigned short forkid;
77 	struct afp_icon * icon;
78 	int eof;
79 };
80 
81 
82 #define VOLUME_EXTRA_FLAGS_VOL_CHMOD_KNOWN 0x1
83 #define VOLUME_EXTRA_FLAGS_VOL_CHMOD_BROKEN 0x2
84 #define VOLUME_EXTRA_FLAGS_SHOW_APPLEDOUBLE 0x4
85 #define VOLUME_EXTRA_FLAGS_VOL_SUPPORTS_UNIX 0x8
86 #define VOLUME_EXTRA_FLAGS_NO_LOCKING 0x10
87 #define VOLUME_EXTRA_FLAGS_IGNORE_UNIXPRIVS 0x20
88 #define VOLUME_EXTRA_FLAGS_READONLY 0x40
89 
90 #define AFP_VOLUME_UNMOUNTED 0
91 #define AFP_VOLUME_MOUNTED 1
92 #define AFP_VOLUME_UNMOUNTING 2
93 
94 struct afp_volume {
95 	unsigned short volid;
96 	char flags;  /* This is from afpGetSrvrParms */
97 	unsigned short attributes; /* This is from VolOpen */
98 	unsigned short signature;  /* This is fixed or variable */
99 	unsigned int creation_date;
100 	unsigned int modification_date;
101 	unsigned int backup_date;
102 	struct statvfs stat;
103 	unsigned char mounted;
104 	char mountpoint[255];
105 	struct afp_server * server;
106 	char volume_name[AFP_VOLUME_NAME_LEN];
107 	char volume_name_printable[AFP_VOLUME_NAME_UTF8_LEN];
108 	unsigned short dtrefnum;
109 	char volpassword[AFP_VOLPASS_LEN];
110 	unsigned int extra_flags; /* This is an afpfs-ng specific field */
111 
112 	/* Our directory ID cache */
113 	struct did_cache_entry * did_cache_base;
114 	pthread_mutex_t did_cache_mutex;
115 
116 	/* Our journal of open forks */
117 	struct afp_file_info * open_forks;
118 	pthread_mutex_t open_forks_mutex;
119 
120 	/* Used to trigger startup */
121         pthread_cond_t  startup_condition_cond;
122 
123 	struct {
124 		uint64_t hits;
125 		uint64_t misses;
126 		uint64_t expired;
127 		uint64_t force_removed;
128 	} did_cache_stats;
129 
130 	void * priv;  /* This is a private structure for fuse/cmdline, etc */
131 	pthread_t thread; /* This is the per-volume thread */
132 
133 	int mapping;
134 
135 };
136 
137 #define SERVER_STATE_CONNECTED 1
138 #define SERVER_STATE_DISCONNECTED 2
139 
140 enum server_type{
141 	AFPFS_SERVER_TYPE_UNKNOWN,
142 	AFPFS_SERVER_TYPE_NETATALK,
143 	AFPFS_SERVER_TYPE_AIRPORT,
144 	AFPFS_SERVER_TYPE_MACINTOSH,
145 };
146 
147 #define is_netatalk(x) ( (x)->machine_type == AFPFS_SERVER_TYPE_NETATALK )
148 #define is_airport(x) ( (x)->machine_type == AFPFS_SERVER_TYPE_AIRPORT )
149 #define is_macintosh(x) ( (x)->machine_type == AFPFS_SERVER_TYPE_MACINTOSH )
150 
151 
152 
153 struct afp_versions {
154         char        *av_name;
155         int         av_number;
156 };
157 extern struct afp_versions afp_versions[];
158 
159 struct afp_server {
160 
161 	/* Our buffer sizes */
162 	unsigned int tx_quantum;
163 	unsigned int rx_quantum;
164 
165 	unsigned int tx_delay;
166 
167 	/* Connection information */
168 	struct sockaddr_in address;
169 	int fd;
170 
171 	/* Some stats, for information only */
172 	struct {
173 		uint64_t runt_packets;
174 		uint64_t incoming_dsi;
175 		uint64_t rx_bytes;
176 		uint64_t tx_bytes;
177 		uint64_t requests_pending;
178 	} stats;
179 
180 	/* General information */
181 	char server_name[AFP_SERVER_NAME_LEN];
182 	char server_name_utf8[AFP_SERVER_NAME_UTF8_LEN];
183         char server_name_printable[AFP_SERVER_NAME_UTF8_LEN];
184 
185 	char machine_type[17];
186 	char icon[256];
187 	char signature[16];
188 	unsigned short flags;
189 	int connect_state;
190 	enum server_type server_type;
191 
192 	/* This is the time we connected */
193 	time_t connect_time;
194 
195 	/* UAMs */
196 	unsigned int supported_uams;
197 	unsigned int using_uam;
198 
199 	/* Authentication */
200 	char username[AFP_MAX_USERNAME_LEN];
201 	char password[AFP_MAX_PASSWORD_LEN];
202 
203 	/* Session */
204 	struct afp_token token;
205 	char need_resume;
206 
207 	/* Versions */
208 	unsigned char requested_version;
209 	unsigned char versions[SERVER_MAX_VERSIONS];
210 	struct afp_versions *using_version;
211 
212 	/* Volumes */
213 	unsigned char num_volumes;
214 	struct afp_volume * volumes;
215 
216 	void * dsi;
217 	unsigned int exit_flag;
218 
219 	/* Our DSI request queue */
220 	pthread_mutex_t requestid_mutex;
221 	pthread_mutex_t request_queue_mutex;
222 	unsigned short lastrequestid;
223 	unsigned short expectedrequestid;
224 	struct dsi_request * command_requests;
225 
226 
227 	char loginmesg[200];
228 	char servermesg[200];
229 	char path_encoding;
230 
231 	/* This is the data for the incoming buffer */
232 	char * incoming_buffer;
233 	int data_read;
234 	int bufsize;
235 
236 	/* And this is for the outgoing queue */
237 	pthread_mutex_t send_mutex;
238 
239 	/* This is for user mapping */
240 	struct passwd passwd;
241 	unsigned int server_uid, server_gid;
242 	int server_gid_valid;
243 
244 	struct afp_server *next;
245 
246 	/* These are for DSI attention packets */
247 	unsigned int attention_quantum;
248 	unsigned int attention_len;
249 	char * attention_buffer;
250 
251 };
252 
253 struct afp_extattr_info {
254 	unsigned int maxsize;
255 	unsigned int size;
256 	char data[1024];
257 };
258 struct afp_comment {
259 	unsigned int maxsize;
260 	unsigned int size;
261 	char *data;
262 };
263 
264 struct afp_icon {
265 	unsigned int maxsize;
266 	unsigned int size;
267 	char *data;
268 };
269 
270 #define AFP_DEFAULT_ATTENTION_QUANTUM 1024
271 
272 void afp_unixpriv_to_stat(struct afp_file_info *fp,
273 	struct stat *stat);
274 
275 int init_uams(void) ;
276 
277 unsigned int find_uam_by_name(const char * name);
278 char * uam_bitmap_to_string(unsigned int bitmap);
279 
280 
281 char * get_uam_names_list(void);
282 
283 unsigned int default_uams_mask(void);
284 
285 struct afp_volume * find_volume_by_name(struct afp_server * server,
286         const char * volname);
287 
288 struct afp_connection_request {
289         unsigned int uam_mask;
290 	struct afp_url url;
291 };
292 
293 void afp_default_url(struct afp_url *url);
294 int afp_parse_url(struct afp_url * url, const char * toparse, int verbose);
295 void afp_print_url(struct afp_url * url);
296 int afp_url_validate(char * url_string, struct afp_url * valid_url);
297 
298 int afp_list_volnames(struct afp_server * server, char * names, int max);
299 
300 /* User mapping */
301 int afp_detect_mapping(struct afp_volume * volume);
302 
303 /* These are some functions that help with simple status text generation */
304 
305 int afp_status_header(char * text, int * len);
306 int afp_status_server(struct afp_server * s,char * text, int * len);
307 
308 
309 struct afp_server * afp_server_full_connect(void * priv, struct afp_connection_request * req);
310 
311 void * just_end_it_now(void *other);
312 void add_fd_and_signal(int fd);
313 void loop_disconnect(struct afp_server *s);
314 void afp_wait_for_started_loop(void);
315 
316 
317 struct afp_versions * pick_version(unsigned char *versions,
318 	unsigned char requested) ;
319 int pick_uam(unsigned int u1, unsigned int u2);
320 
321 int afp_server_login(struct afp_server *server,
322         char * mesg, unsigned int *l, unsigned int max);
323 
324 
325 int afp_dologin(struct afp_server *server,
326 	unsigned int uam, char * username, char * passwd);
327 
328 void afp_free_server(struct afp_server **server);
329 
330 struct afp_server * afp_server_init(struct sockaddr_in * address);
331 int afp_get_address(void * priv, const char * hostname, unsigned int port,
332 	struct sockaddr_in * address);
333 
334 
335 int afp_main_loop(int command_fd);
336 int afp_main_quick_startup(pthread_t * thread);
337 
338 int afp_server_destroy(struct afp_server *s) ;
339 int afp_server_reconnect(struct afp_server * s, char * mesg,
340         unsigned int *l, unsigned int max);
341 int afp_server_connect(struct afp_server *s, int full);
342 
343 struct afp_server * afp_server_complete_connection(
344 	void * priv,
345 	struct afp_server * server,
346 	struct sockaddr_in * address, unsigned char * versions,
347 	unsigned int uams, char * username, char * password,
348 	unsigned int requested_version, unsigned int uam_mask);
349 
350 int afp_connect_volume(struct afp_volume * volume, struct afp_server * server,
351 	char * mesg, unsigned int * l, unsigned int max);
352 int something_is_mounted(struct afp_server * server);
353 
354 int add_cache_entry(struct afp_file_info * file) ;
355 struct afp_file_info * get_cache_by_name(char * name);
356 struct afp_server * find_server_by_address(struct sockaddr_in * address);
357 struct afp_server * find_server_by_signature(char * signature);
358 struct afp_server * find_server_by_name(char * name);
359 int server_still_valid(struct afp_server * server);
360 
361 
362 struct afp_server * get_server_base(void);
363 int afp_server_remove(struct afp_server * server);
364 
365 int afp_unmount_volume(struct afp_volume * volume);
366 int afp_unmount_all_volumes(struct afp_server * server);
367 
368 #define volume_is_readonly(x) (((x)->attributes&kReadOnly) || \
369 	((x)->extra_flags & VOLUME_EXTRA_FLAGS_READONLY))
370 
371 int afp_opendt(struct afp_volume *volume, unsigned short * refnum);
372 
373 int afp_closedt(struct afp_server * server, unsigned short refnum);
374 
375 int afp_getcomment(struct afp_volume *volume, unsigned int did,
376         const char * pathname, struct afp_comment * comment);
377 
378 int afp_addcomment(struct afp_volume *volume, unsigned int did,
379         const char * pathname, char * comment,uint64_t *size);
380 
381 int afp_geticon(struct afp_volume * volume, unsigned int filecreator,
382         unsigned int filetype, unsigned char icontype,
383 	unsigned short length, struct afp_icon * icon);
384 
385 /* Things you want to do to a server */
386 
387 int afp_getsrvrmsg(struct afp_server *server, unsigned short messagetype,unsigned char utf8, unsigned char block, char * mesg);
388 
389 int afp_login(struct afp_server *server, char * uaname,
390         char * userauthinfo, unsigned int userauthinfo_len,
391 	struct afp_rx_buffer *rx);
392 
393 int afp_changepassword(struct afp_server *server, char * uaname,
394         char * userauthinfo, unsigned int userauthinfo_len,
395 	struct afp_rx_buffer *rx);
396 
397 int afp_logincont(struct afp_server *server, unsigned short id,
398         char * userauthinfo, unsigned int userauthinfo_len,
399 	struct afp_rx_buffer *rx);
400 
401 int afp_getsessiontoken(struct afp_server * server, int type,
402         unsigned int timestamp, struct afp_token *outgoing_token,
403         struct afp_token * incoming_token);
404 
405 int afp_getsrvrparms(struct afp_server *server);
406 
407 int afp_logout(struct afp_server *server,unsigned char wait);
408 
409 int afp_mapname(struct afp_server * server, unsigned char subfunction,
410         char * name, unsigned int * id);
411 
412 int afp_mapid(struct afp_server * server, unsigned char subfunction,
413 	unsigned int id, char *name);
414 
415 int afp_getuserinfo(struct afp_server * server, int thisuser,
416 	unsigned int userid, unsigned short bitmap,
417 	unsigned int *newuid, unsigned int *newgid);
418 
419 int afp_zzzzz(struct afp_server *server);
420 
421 int afp_volopen(struct afp_volume * volume,
422 		unsigned short bitmap, char * password);
423 
424 int afp_flush(struct afp_volume * volume);
425 
426 int afp_getfiledirparms(struct afp_volume *volume, unsigned int did,
427 	unsigned int filebitmap, unsigned int dirbitmap, const char * pathname,
428 	struct afp_file_info *fp);
429 
430 int afp_enumerate(struct afp_volume * volume,
431 	unsigned int dirid,
432 	unsigned int filebitmap, unsigned int dirbitmap,
433         unsigned short reqcount,
434         unsigned short startindex,
435         char * path,
436 	struct afp_file_info ** file_p);
437 
438 int afp_enumerateext2(struct afp_volume * volume,
439 	unsigned int dirid,
440 	unsigned int filebitmap, unsigned int dirbitmap,
441         unsigned short reqcount,
442         unsigned long startindex,
443         char * path,
444 	struct afp_file_info ** file_p);
445 
446 int afp_openfork(struct afp_volume * volume,
447         unsigned char forktype,
448         unsigned int dirid,
449         unsigned short accessmode,
450         char * filename,
451 	struct afp_file_info *fp);
452 
453 int afp_read(struct afp_volume * volume, unsigned short forkid,
454                 uint32_t offset,
455                 uint32_t count, struct afp_rx_buffer * rx);
456 
457 int afp_readext(struct afp_volume * volume, unsigned short forkid,
458                 uint64_t offset,
459                 uint64_t count, struct afp_rx_buffer * rx);
460 
461 int afp_getvolparms(struct afp_volume * volume, unsigned short bitmap);
462 
463 
464 int afp_createdir(struct afp_volume * volume, unsigned int dirid, const char * pathname, unsigned int *did_p);
465 
466 int afp_delete(struct afp_volume * volume,
467         unsigned int dirid, char * pathname);
468 
469 
470 int afp_createfile(struct afp_volume * volume, unsigned char flag,
471         unsigned int did, char * pathname);
472 
473 int afp_write(struct afp_volume * volume, unsigned short forkid,
474         uint32_t offset, uint32_t reqcount,
475         char * data, uint32_t * written);
476 
477 int afp_writeext(struct afp_volume * volume, unsigned short forkid,
478         uint64_t offset, uint64_t reqcount,
479         char * data, uint64_t * written);
480 
481 int afp_flushfork(struct afp_volume * volume, unsigned short forkid);
482 
483 int afp_closefork(struct afp_volume * volume, unsigned short forkid);
484 int afp_setfileparms(struct afp_volume * volume,
485         unsigned int dirid, const char * pathname, unsigned short bitmap,
486         struct afp_file_info *fp);
487 int afp_setfiledirparms(struct afp_volume * volume,
488         unsigned int dirid, const char * pathname, unsigned short bitmap,
489         struct afp_file_info *fp);
490 
491 int afp_setdirparms(struct afp_volume * volume,
492         unsigned int dirid, const char * pathname, unsigned short bitmap,
493         struct afp_file_info *fp);
494 
495 int afp_volclose(struct afp_volume * volume);
496 
497 
498 int afp_setforkparms(struct afp_volume *volume,
499         unsigned short forkid, unsigned short bitmap, unsigned long len);
500 
501 int afp_byterangelock(struct afp_volume * volume,
502         unsigned char flag,
503         unsigned short forkid,
504         uint32_t offset,
505         uint32_t len, uint32_t *generated_offset);
506 
507 int afp_byterangelockext(struct afp_volume * volume,
508         unsigned char flag,
509         unsigned short forkid,
510         uint64_t offset,
511         uint64_t len, uint64_t *generated_offset);
512 
513 int afp_moveandrename(struct afp_volume *volume,
514 	unsigned int src_did,
515 	unsigned int dst_did,
516 	char * src_path, char * dst_path, char *new_name);
517 
518 int afp_rename(struct afp_volume * volume,
519         unsigned int dirid,
520         char * path_from, char * path_to);
521 
522 int afp_listextattr(struct afp_volume * volume,
523         unsigned int dirid, unsigned short bitmap,
524         char * pathname, struct afp_extattr_info * info);
525 
526 /* This is a currently undocumented command */
527 int afp_newcommand76(struct afp_volume * volume, unsigned int dlen, char * data);
528 
529 /* For debugging */
530 char * afp_get_command_name(char code);
531 
532 
533 #endif
534