File: ltspfsd_functions.c

package info (click to toggle)
ltspfs 1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 356 kB
  • sloc: ansic: 1,957; sh: 232; python: 135; makefile: 37
file content (1030 lines) | stat: -rw-r--r-- 24,410 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
/*
 * Copyright Scott Balneaves, sbalneav@ltsp.org, 2006, 2007

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, you can find it on the World Wide
 * Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA 02110-1301, USA.

 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <utime.h>
#include <sys/statfs.h>
#include <unistd.h>
#include <rpc/xdr.h>
#include "ltspfs.h"
#include "ltspfsd_functions.h"
#include "common.h"

extern int mounted;

char *ltspfs_opcode_str[] = {
    "LTSPFS_GETATTR",
    "LTSPFS_READLINK",
    "LTSPFS_READDIR",
    "LTSPFS_MKNOD",
    "LTSPFS_MKDIR",
    "LTSPFS_SYMLINK",
    "LTSPFS_UNLINK",
    "LTSPFS_RMDIR",
    "LTSPFS_RENAME",
    "LTSPFS_LINK",
    "LTSPFS_CHMOD",
    "LTSPFS_CHOWN",
    "LTSPFS_TRUNCATE",
    "LTSPFS_UTIME",
    "LTSPFS_OPEN",
    "LTSPFS_READ",
    "LTSPFS_WRITE",
    "LTSPFS_STATFS",
    "LTSPFS_RELEASE",
    "LTSPFS_RSYNC",
    "LTSPFS_SETXATTR",
    "LTSPFS_GETXATTR",
    "LTSPFS_LISTXATTR",
    "LTSPFS_REMOVEXATTR",
    "LTSPFS_XAUTH",
    "LTSPFS_MOUNT",
    "LTSPFS_PING",
    "LTSPFS_QUIT"
};

/*
 * eacces:
 *
 * This function stub is assigned to several callouts when read-only mode
 * is enabled.  It basically just returns EACCES to the client FUSE program.
 */

void eacces(int sockfd)
{
    if (debug)
        info("eacces called\n");
    errno = EACCES;
    status_return(sockfd, FAIL);
}

/*
 * Helper routine to do get a path from the XDR stream, and do all the path
 * adjustment.
 */

int get_fn(int sockfd, XDR * in, char *path)
{
    char *pathptr = path;
    int mpl;

    mpl = strlen(mountpoint);

    strncpy(path, mountpoint, mpl);

    pathptr += mpl;

    if (!xdr_string(in, &pathptr, (PATH_MAX - mpl))) {
        eacces(sockfd);
        return FAIL;
    } else
        return OK;
}

/*
 * ltspfs_dispatch:
 *
 * The callout dispatcher.  Once a command's been read from the socket,
 * this function figures out which command it is, and dispatches the
 * correct handler function.
 */

void ltspfs_dispatch(int sockfd, XDR * in)
{
    int packet_type;

    if (!xdr_int(in, &packet_type)) {
        if (debug)
            info("Packet type decode failed!\n");
        close(sockfd);
        exit(1);
    }

    if (debug)
        info("Packet type: %s\n", ltspfs_opcode_str[packet_type]);

    if (noauth) {
        authenticated++;
    }

    if (!authenticated) {                        /* Haven't authenticated yet */
        switch (packet_type) {
        case LTSPFS_XAUTH:
            handle_auth(sockfd, in);
            break;
        default:
            status_return(sockfd, FAIL);
        }
    } else if (!mountpoint) {
        switch (packet_type) {
        case LTSPFS_MOUNT:
            handle_mount(sockfd, in);            /* Haven't mounted yet */
            break;
        default:
            status_return(sockfd, FAIL);
        }
    } else if (packet_type == LTSPFS_PING) {
        ltspfs_ping(sockfd);
    } else {
        if (!mounted)
            am_mount(mountpoint);                /* this will return */

        switch (packet_type) {
        case LTSPFS_GETATTR:
            ltspfs_getattr(sockfd, in);
            break;
        case LTSPFS_READLINK:
            ltspfs_readlink(sockfd, in);
            break;
        case LTSPFS_READDIR:
            ltspfs_readdir(sockfd, in);
            break;
        case LTSPFS_MKNOD:
            ltspfs_mknod(sockfd, in);
            break;
        case LTSPFS_MKDIR:
            ltspfs_mkdir(sockfd, in);
            break;
        case LTSPFS_SYMLINK:
            ltspfs_symlink(sockfd, in);
            break;
        case LTSPFS_UNLINK:
            ltspfs_unlink(sockfd, in);
            break;
        case LTSPFS_RMDIR:
            ltspfs_rmdir(sockfd, in);
            break;
        case LTSPFS_RENAME:
            ltspfs_rename(sockfd, in);
            break;
        case LTSPFS_LINK:
            ltspfs_link(sockfd, in);
            break;
        case LTSPFS_CHMOD:
            ltspfs_chmod(sockfd, in);
            break;
        case LTSPFS_CHOWN:
            ltspfs_chown(sockfd, in);
            break;
        case LTSPFS_TRUNCATE:
            ltspfs_truncate(sockfd, in);
            break;
        case LTSPFS_UTIME:
            ltspfs_utime(sockfd, in);
            break;
        case LTSPFS_OPEN:
            ltspfs_open(sockfd, in);
            break;
        case LTSPFS_READ:
            ltspfs_read(sockfd, in);
            break;
        case LTSPFS_WRITE:
            ltspfs_write(sockfd, in);
            break;
        case LTSPFS_STATFS:
            ltspfs_statfs(sockfd, in);
            break;
        case LTSPFS_RELEASE:
        case LTSPFS_RSYNC:
        case LTSPFS_SETXATTR:
        case LTSPFS_GETXATTR:
        case LTSPFS_LISTXATTR:
        case LTSPFS_REMOVEXATTR:
            break;
        case LTSPFS_QUIT:
            ltspfs_quit(sockfd);
            break;
        default:
            status_return(sockfd, FAIL);
            if (debug)
                info("Invalid command: %d\n", packet_type);
        }
    }
}

/*
 * ltspfs_getattr:
 *
 * sends the output of the lstat() system call back to the client FUSE program
 */

void ltspfs_getattr(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char output[LTSP_MAXBUF];
    int i;
    struct stat stbuf;
    unsigned int nlink;

    if (get_fn(sockfd, in, path)) {
        if (debug)
            info("get_fn failed\n");
        eacces(sockfd);
        return;
    }

    if (lstat(path, &stbuf) == -1) {
        status_return(sockfd, FAIL);
        return;
    }

    xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
    i = 0;
    xdr_int(&out, &i);                           /* First, the dummy length */
    xdr_int(&out, &i);                           /* Then the 0 status return */
    xdr_u_longlong_t(&out, &(stbuf.st_dev));     /* device */
    xdr_u_longlong_t(&out, &(stbuf.st_ino));     /* inode */
    xdr_u_int(&out, &(stbuf.st_mode));           /* protection */
    xdr_u_int(&out, &nlink);
    stbuf.st_nlink = (nlink_t) nlink;
    xdr_u_int(&out, &(stbuf.st_uid));            /* user ID of owner */
    xdr_u_int(&out, &(stbuf.st_gid));            /* group ID of owner */
    xdr_u_longlong_t(&out, &(stbuf.st_rdev));    /* device type */
    xdr_longlong_t(&out, &(stbuf.st_size));      /* total size, in bytes */
    xdr_long(&out, &(stbuf.st_blksize));         /* blocksize for fs I/O */
    xdr_longlong_t(&out, &(stbuf.st_blocks));    /* num of blocks allocated */
    xdr_long(&out, &(stbuf.st_atime));           /* time of last access */
    xdr_long(&out, &(stbuf.st_mtime));           /* time of last modification */
    xdr_long(&out, &(stbuf.st_ctime));           /* time of last status chng */
    i = xdr_getpos(&out);                        /* Get our position */
    xdr_setpos(&out, 0);                         /* Rewind to the beginning */
    xdr_int(&out, &i);                           /* Rewrite w/ proper length */
    xdr_destroy(&out);

    if (debug)
        info("returning OK");

    writen(sockfd, output, i);
}

/*
 * ltspfs_readlink:
 *
 * sends the link target returned by readlink() to the client FUSE program
 */

void ltspfs_readlink(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char buf[PATH_MAX];                          /* linkname */
    char output[LTSP_MAXBUF];
    char *bufptr = buf;
    int i;

    /* readlink doesn't terminate with a null */
    memset(buf, 0, PATH_MAX);

    if (get_fn(sockfd, in, path)) {              /* Get the link source */
        eacces(sockfd);
        return;
    }

    if (readlink(path, buf, PATH_MAX) == -1) {
        status_return(sockfd, FAIL);
        return;
    }

    if (!strncmp(buf, mountpoint, strlen(mountpoint)))  /* adjust link target */
        bufptr += strlen(mountpoint);

    xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
    i = 0;
    xdr_int(&out, &i);                           /* First, the dummy length */
    xdr_int(&out, &i);                           /* Then the 0 status return */
    xdr_string(&out, &bufptr, PATH_MAX);         /* Link target */
    i = xdr_getpos(&out);                        /* Get our position */
    xdr_setpos(&out, 0);                         /* Rewind to the beginning */
    xdr_int(&out, &i);                           /* Rewrite with proper length */
    xdr_destroy(&out);

    if (debug)
        info("returning ok", output);

    writen(sockfd, output, i);
}

/*
 * ltspfs_readdir:
 *
 * Returns the directory listing to the client FUSE program.  Output looks
 * like:
 *
 * 100|<inode1>|<type1>|<filename1>
 * 100|<inode2>|<type2>|<filename2>
 * ...
 * 100|<inodeN>|<typeN>|<filenameN>
 * 001
 */

void ltspfs_readdir(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char output[LTSP_MAXBUF];
    DIR *dp;
    char *nameptr;
    struct dirent *de;
    int i;

    if (get_fn(sockfd, in, path)) {              /* Get the dir name */
        eacces(sockfd);
        return;
    }

    dp = opendir(path);

    if (dp == NULL) {
        status_return(sockfd, FAIL);             /* opendir failed */
        return;
    }

    while ((de = readdir(dp)) != NULL) {
        xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
        i = 0;
        xdr_int(&out, &i);                       /* First, the dummy length */
        i = LTSP_STATUS_CONT;
        xdr_int(&out, &i);                       /* Then the 2 status return */
        xdr_u_longlong_t(&out, &(de->d_ino));    /* Inode */
        xdr_u_char(&out, &(de->d_type));         /* type */
        nameptr = de->d_name;
        xdr_string(&out, &nameptr, PATH_MAX);    /* filename */
        i = xdr_getpos(&out);                    /* Get our position */
        xdr_setpos(&out, 0);                     /* Rewind to the beginning */
        xdr_int(&out, &i);                       /* Rewrite w/ proper length */
        xdr_destroy(&out);

        if (debug)
            info("returning %s", de->d_name);

        writen(sockfd, output, i);
    }

    closedir(dp);

    status_return(sockfd, OK);
}

/*
 * ltspfs_mknod:
 *
 * Makes filesystem nodes (regular files, pipes, device nodes, etc).
 */

void ltspfs_mknod(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    mode_t mode;
    dev_t rdev;

    if (!xdr_u_int(in, &mode)) {                 /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (!xdr_u_longlong_t(in, &rdev)) {          /* Get the device type */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the dir name */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, mknod(path, mode, rdev));
}

/*
 * ltspfs_mkdir:
 *
 * Makes directories
 */

void ltspfs_mkdir(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    mode_t mode;

    if (!xdr_u_int(in, &mode)) {                 /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the dir name */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, mkdir(path, mode));
}

/*
 * ltspfs_symlink:
 *
 * Makes symlinks
 */

void ltspfs_symlink(int sockfd, XDR * in)
{
    char from[PATH_MAX];
    char to[PATH_MAX];

    if (get_fn(sockfd, in, from)) {              /* Get the source link */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, to)) {                /* Get the destination link */
        eacces(sockfd);
        return;
    }


    status_return(sockfd, symlink(from, to));
}

/*
 * ltspfs_unlink:
 *
 * Deletes (unlinks) files.
 */

void ltspfs_unlink(int sockfd, XDR * in)
{
    char path[PATH_MAX];

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, unlink(path));
}

/*
 * ltspfs_rmdir:
 *
 * Removes directories.
 */

void ltspfs_rmdir(int sockfd, XDR * in)
{
    char path[PATH_MAX];

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, rmdir(path));
}

/*
 * ltspfs_rename:
 *
 * Renames files.
 */

void ltspfs_rename(int sockfd, XDR * in)
{
    char from[PATH_MAX];
    char to[PATH_MAX];

    if (get_fn(sockfd, in, from)) {              /* Get the source name */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, to)) {                /* Get the destination name */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, rename(from, to));
}

/*
 * ltspfs_link:
 *
 * Creates hard links.
 */

void ltspfs_link(int sockfd, XDR * in)
{
    char from[PATH_MAX];
    char to[PATH_MAX];

    if (get_fn(sockfd, in, from)) {              /* Get the source link */
        eacces(sockfd);
        return;
    }
    if (get_fn(sockfd, in, to)) {                /* Get the destination link */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, link(from, to));
}

/*
 * ltspfs_chmod:
 *
 * Changes permisions on files.
 */

void ltspfs_chmod(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    mode_t mode;

    if (!xdr_u_int(in, &mode)) {                 /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, chmod(path, mode));
}

/*
 * ltspfs_chown:
 *
 * Changes owners/groups on files.
 */

void ltspfs_chown(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    uid_t uid;
    gid_t gid;

    if (!xdr_u_int(in, &uid)) {                  /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (!xdr_u_int(in, &gid)) {                  /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, chown(path, uid, gid));
}

/*
 * ltspfs_truncate:
 *
 * Truncates a file at offset bytes.
 */

void ltspfs_truncate(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    off_t offset;

    if (!xdr_longlong_t(in, &offset)) {          /* Get the mode */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, truncate(path, offset));
}

/*
 * ltspfs_utime:
 *
 * Updates the access time and modification time of a file
 */

void ltspfs_utime(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    struct utimbuf timbuf;

    if (!xdr_long(in, &timbuf.actime)) {         /* Get the actime */
        eacces(sockfd);
        return;
    }

    if (!xdr_long(in, &timbuf.modtime)) {        /* Get the modtime */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    status_return(sockfd, utime(path, &timbuf));
}

/*
 * ltspfs_open:
 *
 * Tests whether or not a file may be opened dependant on the flags.
 */

void ltspfs_open(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    int result;
    int flags;

    if (!xdr_int(in, &flags)) {                  /* Get the flags */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }

    /*
     * If we're a read-only filesystem, and we've been asked to open
     * O_WRONLY or O_RDWR, then return EACCES
     */

    if (readonly)
        if ((flags & O_WRONLY) || (flags & O_RDWR)) {
            eacces(sockfd);
            return;
        }

    /*
     * Otherwise, check the permissions with a test open, and return the
     * results if an error occurred.
     */

    result = open(path, flags);

    status_return(sockfd, result);

    if (result != -1)
        close(result);
}

/*
 * ltspfs_read:
 *
 * Reads blocks from a file.  Atomic: open-seek-read-close
 */

void ltspfs_read(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char output[LTSP_MAXBUF];
    int i;
    int fd;
    int result;
    unsigned int usize;
    size_t size;
    off_t offset;
    char *buf;

    if (!xdr_u_int(in, &usize)) {                 /* Get the size */
        eacces(sockfd);
        return;
    }

    size = (size_t) usize;

    if (!xdr_longlong_t(in, &offset)) {          /* Get the offset */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }


    buf = malloc(size);

    /*
     * Check result of malloc
     */

    if (!buf) {
        status_return(sockfd, FAIL);
        return;
    }

    fd = open(path, O_RDONLY);
    if (fd == -1) {
        status_return(sockfd, FAIL);
        free(buf);
        return;
    }

    lseek(fd, offset, SEEK_SET);
    result = read(fd, buf, size);

    if (result < 0)
        status_return(sockfd, FAIL);
    else {
        xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
        i = 0;
        xdr_int(&out, &i);                       /* dummy length */
        i = LTSP_STATUS_OK;                      /* OK status */
        xdr_int(&out, &i);
        xdr_int(&out, &result);                  /* Write out the result */
        i = xdr_getpos(&out);                    /* Get current position */
        xdr_setpos(&out, 0);                     /* rewind to the beginning */
        xdr_int(&out, &i);                       /* re-write proper length */
        xdr_destroy(&out);                       /* Clean up the XDR structs */

        if (debug)
            info("read returning %d bytes", result);

        writen(sockfd, output, i);               /* Write out status */

        writen(sockfd, buf, result);             /* Write out data payload */
    }

    close(fd);

    free(buf);
}

void ltspfs_write(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char output[LTSP_MAXBUF];
    int i;
    int fd;
    int result;
    size_t size;
    unsigned int usize;
    off_t offset;
    char *buf;

    if (!xdr_u_int(in, &usize)) {                /* Get the size */
        eacces(sockfd);
        return;
    }

    size = (size_t) usize;

    if (!xdr_longlong_t(in, &offset)) {          /* Get the offset */
        eacces(sockfd);
        return;
    }

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }


    buf = malloc(size);

    /*
     * Check result of malloc
     */

    if (!buf) {
        status_return(sockfd, FAIL);
        return;
    }

    fd = open(path, O_WRONLY);
    if (fd == -1) {
        status_return(sockfd, FAIL);
        free(buf);
        return;
    }

    readn(sockfd, buf, size);

    lseek(fd, offset, SEEK_SET);
    result = write(fd, buf, size);

    if (result < 0)
        status_return(sockfd, FAIL);
    else {
        xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
        i = 0;
        xdr_int(&out, &i);                       /* dummy length */
        i = LTSP_STATUS_OK;                      /* OK status */
        xdr_int(&out, &i);
        xdr_int(&out, &result);                  /* Write out the result */
        i = xdr_getpos(&out);                    /* Get current position */
        xdr_setpos(&out, 0);                     /* rewind to the beginning */
        xdr_int(&out, &i);                       /* re-write proper length */
        xdr_destroy(&out);                       /* Clean up the XDR structs */

        if (debug)
            info("returning %s", output);

        writen(sockfd, output, i);
    }

    close(fd);

    free(buf);
}

void ltspfs_statfs(int sockfd, XDR * in)
{
    XDR out;
    char path[PATH_MAX];
    char output[LTSP_MAXBUF];
    struct statfs stbuf;
    int i;
    int f_type, f_bsize, f_namelen;

    if (get_fn(sockfd, in, path)) {              /* Get the path */
        eacces(sockfd);
        return;
    }


    if (statfs(path, &stbuf) == -1) {
        status_return(sockfd, FAIL);
        return;
    }

    xdrmem_create(&out, output, LTSP_MAXBUF, XDR_ENCODE);
    i = 0;
    xdr_int(&out, &i);                           /* dummy length */
    i = LTSP_STATUS_OK;                          /* OK status */
    xdr_int(&out, &i);
    f_type = (int) stbuf.f_type;
    xdr_int(&out, &f_type);                      /* type of fs */
    f_bsize = (int) stbuf.f_bsize;
    xdr_int(&out, &f_bsize);                     /* optimal transfer block sz */
    xdr_u_longlong_t(&out, &stbuf.f_blocks);     /* total data blocks in fs */
    xdr_u_longlong_t(&out, &stbuf.f_bfree);      /* free blks in fs */
    xdr_u_longlong_t(&out, &stbuf.f_bavail);     /* free blks avail to non-su */
    xdr_u_longlong_t(&out, &stbuf.f_files);      /* total file nodes in fs */
    xdr_u_longlong_t(&out, &stbuf.f_ffree);      /* free file nodes in fs */
    f_namelen = (int) stbuf.f_namelen;
    xdr_int(&out, &f_namelen);                   /* max length of filenames */
    i = xdr_getpos(&out);                        /* Get current position */
    xdr_setpos(&out, 0);                         /* rewind to the beginning */
    xdr_int(&out, &i);                           /* re-write proper length */
    xdr_destroy(&out);                           /* Clean up the XDR structs */

    if (debug)
        info("returning OK");

    writen(sockfd, output, i);
}

/*
 * ltspfs_ping:
 *
 * Transmit a simple "000" status return to a ping request.
 */

void ltspfs_ping(int sockfd)
{
    status_return(sockfd, OK);

    if (debug)
        info("received ping packet\n");
}

void ltspfs_quit(int sockfd)
{
    /*
     * Close up shop and bail.
     */

    close(sockfd);
    exit(OK);
}

void handle_mount(int sockfd, XDR * in)
{
    char path[PATH_MAX];
    char *pathptr = path;

    /*
     * Get our mount point
     */

    if (!xdr_string(in, &pathptr, PATH_MAX)) {   /* Get the path */
        eacces(sockfd);
        return;
    }

    /*
     * Here's where you'd do sanity checking on the dir, checking an exports 
     * file, etc.  For now, we'll assume it's correct.
     */

    mountpoint = strdup(path);

    if (debug)
        info("mount: %s\n", mountpoint);

    status_return(sockfd, OK);
}

/*
 * handle_auth
 *
 * Read our secret code (generated with mcookie), and compare it to what the
 * client sent us.
 */

void handle_auth(int sockfd, XDR * in)
{
    char recdtoken[64];
    int auth_size;
    char authtoken[64];
    int authtokensize;
    int authfile, i;

    /*
     * Get our auth size.
     */

    if (!xdr_int(in, &auth_size)) {            /* Get the size */
        eacces(sockfd);
        return;
    }

    if (auth_size >= (int) sizeof recdtoken) {
        eacces(sockfd);
        return;
    }

    readn(sockfd, recdtoken, auth_size);         /* read packet */

    recdtoken[auth_size] = '\0';

    /*
     * Test our authentication.
     * our XAUTHORITY environment variable is pointing to the xauth record,
     * so if we've got a match, we the open should succeed.
     */

    authfile = open("/var/run/ltspfs_token", O_RDONLY);
    if (authfile < 0) {
        eacces(sockfd);
        return;
    }

    authtokensize = read(authfile, authtoken, sizeof authtoken);
    close(authfile);

    for (i = 0; i < authtokensize; i++)
        if (authtoken[i] == '\n')
            authtoken[i] = '\0';

    if (strncmp(authtoken, recdtoken, auth_size)) {
        eacces(sockfd);
        return;
    }

    status_return(sockfd, OK);                   /* Acknowledge auth */
    authenticated++;                             /* Set auth state */
}