1 /* $NetBSD: cmds.c,v 1.20 2003/01/08 18:07:31 manu Exp $ */
4 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
40 * Copyright (c) 1985, 1988, 1990, 1992, 1993, 1994
41 * The Regents of the University of California. All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * Copyright (C) 1997 and 1998 WIDE Project.
74 * All rights reserved.
76 * Redistribution and use in source and binary forms, with or without
77 * modification, are permitted provided that the following conditions
79 * 1. Redistributions of source code must retain the above copyright
80 * notice, this list of conditions and the following disclaimer.
81 * 2. Redistributions in binary form must reproduce the above copyright
82 * notice, this list of conditions and the following disclaimer in the
83 * documentation and/or other materials provided with the distribution.
84 * 3. Neither the name of the project nor the names of its contributors
85 * may be used to endorse or promote products derived from this software
86 * without specific prior written permission.
88 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
89 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
92 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
102 #include <sys/cdefs.h>
104 __RCSID("$NetBSD: cmds.c,v 1.20 2003/01/08 18:07:31 manu Exp $");
105 #endif /* not lint */
107 #include <sys/param.h>
108 #include <sys/stat.h>
110 #include <arpa/ftp.h>
123 #include <krb5/krb5.h>
129 FE_MLSD = 1<<0, /* if op is MLSD (MLST otherwise ) */
130 FE_ISCURDIR = 1<<1, /* if name is the current directory */
134 const char *path; /* full pathname */
135 const char *display; /* name to display */
136 struct stat *stat; /* stat of path */
137 struct stat *pdirstat; /* stat of path's parent dir */
138 factflag_t flags; /* flags */
141 static void ack(const char *);
142 static void base64_encode(const char *, size_t, char *, int);
143 static void fact_type(const char *, FILE *, factelem *);
144 static void fact_size(const char *, FILE *, factelem *);
145 static void fact_modify(const char *, FILE *, factelem *);
146 static void fact_perm(const char *, FILE *, factelem *);
147 static void fact_unique(const char *, FILE *, factelem *);
148 static int matchgroup(gid_t);
149 static void mlsname(FILE *, factelem *);
150 static void replydirname(const char *, const char *);
153 const char *name; /* name of fact */
154 int enabled; /* if fact is enabled */
155 void (*display)(const char *, FILE *, factelem *);
156 /* function to display fact */
159 struct ftpfact facttab[] = {
160 { "Type", 1, fact_type },
162 { "Size", 1, fact_size },
163 { "Modify", 1, fact_modify },
164 { "Perm", 1, fact_perm },
165 { "Unique", 1, fact_unique },
172 #define FACTTABSIZE (sizeof(facttab) / sizeof(struct ftpfact))
174 static char cached_path[MAXPATHLEN + 1] = "/";
175 static void discover_path(char *, const char *);
178 cwd(const char *path)
182 perror_reply(550, path);
184 show_chdir_messages(250);
186 if (getcwd(cached_path, MAXPATHLEN) == NULL) {
187 discover_path(cached_path, path);
193 delete(const char *name)
197 if (remove(name) < 0) {
199 perror_reply(550, name);
202 logxfer("delete", -1, name, NULL, NULL, p);
210 reply(-211, "Features supported");
211 cprintf(stdout, " MDTM\r\n");
212 cprintf(stdout, " MLST ");
213 for (i = 0; i < FACTTABSIZE; i++)
214 cprintf(stdout, "%s%s;", facttab[i].name,
215 facttab[i].enabled ? "*" : "");
216 cprintf(stdout, "\r\n");
217 cprintf(stdout, " REST STREAM\r\n");
218 cprintf(stdout, " SIZE\r\n");
219 cprintf(stdout, " TVFS\r\n");
224 makedir(const char *name)
228 if (mkdir(name, 0777) < 0) {
230 perror_reply(550, name);
232 replydirname(name, "directory created.");
233 logxfer("mkdir", -1, name, NULL, NULL, p);
237 mlsd(const char *path)
240 struct stat sb, pdirstat;
244 char name[MAXPATHLEN];
247 hastypefact = facttab[FACT_TYPE].enabled;
250 if (stat(path, &pdirstat) == -1) {
252 perror_reply(550, path);
255 if (! S_ISDIR(pdirstat.st_mode)) {
257 perror_reply(501, path);
260 if ((dirp = opendir(path)) == NULL)
263 dout = dataconn("MLSD", (off_t)-1, "w");
267 memset(&f, 0, sizeof(f));
270 while ((dp = readdir(dirp)) != NULL) {
271 snprintf(name, sizeof(name), "%s/%s", path, dp->d_name);
272 if (ISDOTDIR(dp->d_name)) { /* special case curdir: */
275 f.pdirstat = NULL; /* require stat of parent */
276 f.display = path; /* set name to real name */
277 f.flags |= FE_ISCURDIR; /* flag name is curdir */
279 if (ISDOTDOTDIR(dp->d_name)) {
284 f.pdirstat = &pdirstat; /* cache parent stat */
285 f.display = dp->d_name;
286 f.flags &= ~FE_ISCURDIR;
288 if (stat(name, &sb) == -1)
293 (void)closedir(dirp);
295 if (ferror(dout) != 0)
296 perror_reply(550, "Data connection");
298 reply(226, "MLSD complete.");
305 mlst(const char *path)
312 if (stat(path, &sb) == -1) {
313 perror_reply(550, path);
316 reply(-250, "MLST %s", path);
317 memset(&f, 0, sizeof(f));
329 opts(const char *command)
334 if ((ep = strchr(command, ' ')) != NULL)
336 c = lookup(cmdtab, command);
338 reply(502, "Unknown command '%s'.", command);
341 if (! CMD_IMPLEMENTED(c)) {
342 reply(502, "%s command not implemented.", c->name);
345 if (! CMD_HAS_OPTIONS(c)) {
346 reply(501, "%s command does not support persistent options.",
351 /* special case: MLST */
352 if (strcasecmp(command, "MLST") == 0) {
353 int enabled[FACTTABSIZE];
358 for (i = 0; i < sizeof(enabled) / sizeof(int); i++)
360 if (ep == NULL || *ep == '\0')
361 goto displaymlstopts;
363 /* don't like spaces, and need trailing ; */
365 if (strchr(ep, ' ') != NULL || ep[len - 1] != ';') {
367 reply(501, "Invalid MLST options");
371 while ((p = strsep(&ep, ";")) != NULL) {
374 for (i = 0; i < FACTTABSIZE; i++)
375 if (strcasecmp(p, facttab[i].name) == 0) {
382 for (i = 0; i < FACTTABSIZE; i++)
383 facttab[i].enabled = enabled[i];
384 cprintf(stdout, "200 MLST OPTS");
385 for (i = onedone = 0; i < FACTTABSIZE; i++) {
386 if (facttab[i].enabled) {
387 cprintf(stdout, "%s%s;", onedone ? "" : " ",
392 cprintf(stdout, "\r\n");
398 if (ep != NULL && *ep != '\0')
399 REASSIGN(c->options, xstrdup(ep));
400 if (c->options != NULL)
401 reply(200, "Options for %s are '%s'.", c->name,
404 reply(200, "No options defined for %s.", c->name);
410 char path[MAXPATHLEN];
412 if (getcwd(path, sizeof(path) - 1) == NULL) {
413 if (chdir(cached_path) < 0) {
414 reply(550, "Can't get the current directory: %s.",
418 (void)strlcpy(path, cached_path, MAXPATHLEN);
420 replydirname(path, "is the current directory.");
424 removedir(const char *name)
428 if (rmdir(name) < 0) {
430 perror_reply(550, name);
433 logxfer("rmdir", -1, name, NULL, NULL, p);
437 renamefrom(const char *name)
441 if (stat(name, &st) < 0) {
442 perror_reply(550, name);
445 reply(350, "File exists, ready for destination name");
446 return (xstrdup(name));
450 renamecmd(const char *from, const char *to)
454 if (rename(from, to) < 0) {
456 perror_reply(550, "rename");
459 logxfer("rename", -1, from, to, NULL, p);
463 sizecmd(const char *filename)
470 if (stat(filename, &stbuf) < 0 || !S_ISREG(stbuf.st_mode))
471 reply(550, "%s: not a plain file.", filename);
473 reply(213, ULLF, (ULLT)stbuf.st_size);
482 fin = fopen(filename, "r");
484 perror_reply(550, filename);
487 if (fstat(fileno(fin), &stbuf) < 0 || !S_ISREG(stbuf.st_mode)) {
488 reply(550, "%s: not a plain file.", filename);
492 if (stbuf.st_size > 10240) {
493 reply(550, "%s: file too large for SIZE.", filename);
499 while((c = getc(fin)) != EOF) {
500 if (c == '\n') /* will get expanded to \r\n */
506 reply(213, LLF, (LLT)count);
510 reply(504, "SIZE not implemented for Type %c.", "?AEIL"[type]);
515 statfilecmd(const char *filename)
520 char *argv[] = { INTERNAL_LS, "-lgA", "", NULL };
522 argv[2] = (char *)filename;
523 fin = ftpd_popen(argv, "r", STDOUT_FILENO);
524 reply(-211, "status of %s:", filename);
525 /* XXX: use fgetln() or fparseln() here? */
527 while ((c = getc(fin)) != EOF) {
530 perror_reply(421, "control connection");
531 (void) ftpd_pclose(fin);
536 perror_reply(551, filename);
537 (void) ftpd_pclose(fin);
542 if (atstart && isdigit(c))
545 atstart = (c == '\n');
547 (void) ftpd_pclose(fin);
548 reply(211, "End of Status");
557 reply(250, "%s command successful.", s);
561 * Encode len bytes starting at clear using base64 encoding into encoded,
562 * which should be at least ((len + 2) * 4 / 3 + 1) in size.
563 * If nulterm is non-zero, terminate with \0 otherwise pad to 3 byte boundary
567 base64_encode(const char *clear, size_t len, char *encoded, int nulterm)
569 static const char base64[] =
570 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
575 /* determine whether to pad with '=' or NUL terminate */
576 termchar = nulterm ? '\0' : '=';
579 /* convert all but last 2 bytes */
580 for (i = len; i > 2; i -= 3, c += 3) {
581 *e++ = base64[(c[0] >> 2) & 0x3f];
582 *e++ = base64[((c[0] << 4) & 0x30) | ((c[1] >> 4) & 0x0f)];
583 *e++ = base64[((c[1] << 2) & 0x3c) | ((c[2] >> 6) & 0x03)];
584 *e++ = base64[(c[2]) & 0x3f];
586 /* handle slop at end */
588 *e++ = base64[(c[0] >> 2) & 0x3f];
589 *e++ = base64[((c[0] << 4) & 0x30) |
590 (i > 1 ? ((c[1] >> 4) & 0x0f) : 0)];
591 *e++ = (i > 1) ? base64[(c[1] << 2) & 0x3c] : termchar;
598 fact_modify(const char *fact, FILE *fd, factelem *fe)
602 t = gmtime(&(fe->stat->st_mtime));
603 cprintf(fd, "%s=%04d%02d%02d%02d%02d%02d;", fact,
604 TM_YEAR_BASE + t->tm_year,
605 t->tm_mon+1, t->tm_mday,
606 t->tm_hour, t->tm_min, t->tm_sec);
610 fact_perm(const char *fact, FILE *fd, factelem *fe)
612 int rok, wok, xok, pdirwok;
615 if (fe->stat->st_uid == geteuid()) {
616 rok = ((fe->stat->st_mode & S_IRUSR) != 0);
617 wok = ((fe->stat->st_mode & S_IWUSR) != 0);
618 xok = ((fe->stat->st_mode & S_IXUSR) != 0);
619 } else if (matchgroup(fe->stat->st_gid)) {
620 rok = ((fe->stat->st_mode & S_IRGRP) != 0);
621 wok = ((fe->stat->st_mode & S_IWGRP) != 0);
622 xok = ((fe->stat->st_mode & S_IXGRP) != 0);
624 rok = ((fe->stat->st_mode & S_IROTH) != 0);
625 wok = ((fe->stat->st_mode & S_IWOTH) != 0);
626 xok = ((fe->stat->st_mode & S_IXOTH) != 0);
629 cprintf(fd, "%s=", fact);
632 * if parent info not provided, look it up, but
633 * only if the current class has modify rights,
634 * since we only need this info in such a case.
637 if (pdir == NULL && CURCLASS_FLAGS_ISSET(modify)) {
639 char realdir[MAXPATHLEN], *p;
642 len = strlcpy(realdir, fe->path, sizeof(realdir));
643 if (len < sizeof(realdir) - 4) {
644 if (S_ISDIR(fe->stat->st_mode))
645 strlcat(realdir, "/..", sizeof(realdir));
647 /* if has a /, move back to it */
648 /* otherwise use '..' */
649 if ((p = strrchr(realdir, '/')) != NULL) {
654 strlcpy(realdir, "..", sizeof(realdir));
656 if (stat(realdir, &dir) == 0)
662 if (pdir->st_uid == geteuid())
663 pdirwok = ((pdir->st_mode & S_IWUSR) != 0);
664 else if (matchgroup(pdir->st_gid))
665 pdirwok = ((pdir->st_mode & S_IWGRP) != 0);
667 pdirwok = ((pdir->st_mode & S_IWOTH) != 0);
670 /* 'a': can APPE to file */
671 if (wok && CURCLASS_FLAGS_ISSET(upload) && S_ISREG(fe->stat->st_mode))
674 /* 'c': can create or append to files in directory */
675 if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
678 /* 'd': can delete file or directory */
679 if (pdirwok && CURCLASS_FLAGS_ISSET(modify)) {
683 if (S_ISDIR(fe->stat->st_mode)) {
687 if ((dirp = opendir(fe->display)) == NULL)
690 while ((dp = readdir(dirp)) != NULL) {
691 if (ISDOTDIR(dp->d_name) ||
692 ISDOTDOTDIR(dp->d_name))
704 /* 'e': can enter directory */
705 if (xok && S_ISDIR(fe->stat->st_mode))
708 /* 'f': can rename file or directory */
709 if (pdirwok && CURCLASS_FLAGS_ISSET(modify))
712 /* 'l': can list directory */
713 if (rok && xok && S_ISDIR(fe->stat->st_mode))
716 /* 'm': can create directory */
717 if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
720 /* 'p': can remove files in directory */
721 if (wok && CURCLASS_FLAGS_ISSET(modify) && S_ISDIR(fe->stat->st_mode))
724 /* 'r': can RETR file */
725 if (rok && S_ISREG(fe->stat->st_mode))
728 /* 'w': can STOR file */
729 if (wok && CURCLASS_FLAGS_ISSET(upload) && S_ISREG(fe->stat->st_mode))
736 fact_size(const char *fact, FILE *fd, factelem *fe)
739 if (S_ISREG(fe->stat->st_mode))
740 cprintf(fd, "%s=" LLF ";", fact, (LLT)fe->stat->st_size);
744 fact_type(const char *fact, FILE *fd, factelem *fe)
747 cprintf(fd, "%s=", fact);
748 switch (fe->stat->st_mode & S_IFMT) {
750 if (fe->flags & FE_MLSD) {
751 if ((fe->flags & FE_ISCURDIR) || ISDOTDIR(fe->display))
753 else if (ISDOTDOTDIR(fe->display))
765 cprintf(fd, "OS.unix=fifo");
767 case S_IFLNK: /* XXX: probably a NO-OP with stat() */
768 cprintf(fd, "OS.unix=slink");
771 cprintf(fd, "OS.unix=socket");
775 cprintf(fd, "OS.unix=%s-%d/%d",
776 S_ISBLK(fe->stat->st_mode) ? "blk" : "chr",
777 major(fe->stat->st_rdev), minor(fe->stat->st_rdev));
780 cprintf(fd, "OS.unix=UNKNOWN(0%o)", fe->stat->st_mode & S_IFMT);
787 fact_unique(const char *fact, FILE *fd, factelem *fe)
789 char obuf[(sizeof(dev_t) + sizeof(ino_t) + 2) * 4 / 3 + 2];
790 char tbuf[sizeof(dev_t) + sizeof(ino_t)];
793 (char *)&(fe->stat->st_dev), sizeof(dev_t));
794 memcpy(tbuf + sizeof(dev_t),
795 (char *)&(fe->stat->st_ino), sizeof(ino_t));
796 base64_encode(tbuf, sizeof(dev_t) + sizeof(ino_t), obuf, 1);
797 cprintf(fd, "%s=%s;", fact, obuf);
801 matchgroup(gid_t gid)
805 for (i = 0; i < gidcount; i++)
806 if (gid == gidlist[i])
812 mlsname(FILE *fp, factelem *fe)
814 char realfile[MAXPATHLEN];
817 for (i = 0; i < FACTTABSIZE; i++) {
818 if (facttab[i].enabled)
819 (facttab[i].display)(facttab[i].name, fp, fe);
821 if ((fe->flags & FE_MLSD) &&
822 !(fe->flags & FE_ISCURDIR) && !ISDOTDIR(fe->display)) {
823 /* if MLSD and not "." entry, display as-is */
826 /* if MLST, or MLSD and "." entry, realpath(3) it */
827 if (realpath(fe->display, realfile) != NULL)
830 cprintf(fp, " %s\r\n", userf ? realfile : fe->display);
834 replydirname(const char *name, const char *message)
837 char npath[MAXPATHLEN * 2];
840 ep = &npath[sizeof(npath) - 1];
854 reply(257, "\"%s\" %s", npath, message);
858 discover_path(last_path, new_path)
860 const char *new_path;
862 char tp[MAXPATHLEN + 1] = "";
863 char tq[MAXPATHLEN + 1] = "";
868 struct stat st1, st2;
870 if (new_path[0] != '/') {
871 (void)strlcpy(tp, last_path, MAXPATHLEN);
872 (void)strlcat(tp, "/", MAXPATHLEN);
874 (void)strlcat(tp, new_path, MAXPATHLEN);
875 (void)strlcat(tp, "/", MAXPATHLEN);
878 * resolve symlinks. A symlink may introduce another symlink, so we
879 * loop trying to resolve symlinks until we don't find any of them.
882 /* Collapse any // into / */
883 while ((cp = strstr(tp, "//")) != NULL)
884 (void)memmove(cp, cp + 1, strlen(cp) - 1 + 1);
886 /* Collapse any /./ into / */
887 while ((cp = strstr(tp, "/./")) != NULL)
888 (void)memmove(cp, cp + 2, strlen(cp) - 2 + 1);
893 while ((cp = strstr(++cp, "/")) != NULL) {
894 sz1 = (u_long)cp - (u_long)tp;
895 if (sz1 > MAXPATHLEN)
898 sz2 = readlink(tp, tq, MAXPATHLEN);
901 /* If this is not a symlink, move to next / */
906 * We found a symlink, so we will have to
907 * do one more pass to check there is no
908 * more symlink in the path
913 * Null terminate the string and remove trailing /
917 if (tq[sz2 - 1] == '/')
921 * Is this an absolute link or a relative link?
925 if (strlen(cp) + sz2 > MAXPATHLEN)
927 memmove(tp + sz2, cp, strlen(cp) + 1);
931 for (cq = cp - 1; *cq != '/'; cq--);
932 if (strlen(tp) - ((u_long)cq - (u_long)cp)
933 + 1 + sz2 > MAXPATHLEN)
935 (void)memmove(cq + 1 + sz2,
937 (void)memcpy(cq + 1, tq, sz2);
941 * start over, looking for new symlinks
945 } while (nomorelink == 0);
947 /* Collapse any /foo/../ into /foo/ */
948 while ((cp = strstr(tp, "/../")) != NULL) {
949 /* ^/../foo/ becomes ^/foo/ */
951 (void)memmove(cp, cp + 3,
954 for (cq = cp - 1; *cq != '/'; cq--);
955 (void)memmove(cq, cp + 3,
960 /* strip strailing / */
962 tp[strlen(tp) - 1] = '\0';
964 /* check that the path is correct */
967 if ((st1.st_dev != st2.st_dev) || (st1.st_ino != st2.st_ino))
970 (void)strlcpy(last_path, tp, MAXPATHLEN);
974 (void)strlcat(last_path, "/", MAXPATHLEN);
975 (void)strlcat(last_path, new_path, MAXPATHLEN);