Verify that name lengths are at most AFSNAMEMAX and symlink
[openafs.git] / src / afs / VNOPS / afs_vnop_symlink.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * afs_vnop_symlink.c - symlink and readlink vnodeops.
12  *
13  * Implements:
14  * afs_symlink
15  * afs_MemHandleLink
16  * afs_UFSHandleLink
17  * afs_readlink
18  *
19  */
20
21 #include <afsconfig.h>
22 #include "../afs/param.h"
23
24 RCSID("$Header$");
25
26 #include "../afs/sysincludes.h" /* Standard vendor system headers */
27 #include "../afs/afsincludes.h" /* Afs-based standard headers */
28 #include "../afs/afs_stats.h" /* statistics */
29 #include "../afs/afs_cbqueue.h"
30 #include "../afs/nfsclient.h"
31 #include "../afs/afs_osidnlc.h"
32
33 extern afs_rwlock_t afs_xvcache;
34 extern afs_rwlock_t afs_xcbhash;
35
36 /* Note: There is the bare bones beginning of symlink hints in the now
37  * defunct afs/afs_lookup.c file. Since they are not in use, making the call
38  * is just a performance hit.
39  */
40
41
42 /* don't set CDirty in here because RPC is called synchronously */
43 afs_symlink
44 #ifdef  AFS_OSF_ENV
45   (ndp, attrs, atargetName)
46     struct nameidata *ndp;
47     struct vattr *attrs;
48     register char *atargetName; {
49     register struct vcache *adp = (struct vcache *)ndp->ni_dvp;
50     char *aname = ndp->ni_dent.d_name;
51     struct ucred *acred = ndp->ni_cred;
52 #else   /* AFS_OSF_ENV */
53   (OSI_VC_ARG(adp), aname, attrs, atargetName, acred)
54     OSI_VC_DECL(adp);
55     char *atargetName;
56     char *aname;
57     struct vattr *attrs;
58     struct AFS_UCRED *acred; {
59 #endif
60     afs_uint32 now;
61     struct vrequest treq;
62     afs_int32 code;
63     struct conn *tc;
64     struct VenusFid newFid;
65     struct dcache *tdc;
66     afs_size_t offset, len;
67     afs_int32 alen;
68     struct server *hostp=0;
69     struct vcache *tvc;
70     struct AFSStoreStatus InStatus;
71     struct AFSFetchStatus OutFidStatus, OutDirStatus;
72     struct AFSCallBack CallBack;
73     struct AFSVolSync tsync;
74     struct volume*    volp=0;
75     XSTATS_DECLS
76     OSI_VC_CONVERT(adp)
77     
78     AFS_STATCNT(afs_symlink);
79     afs_Trace2(afs_iclSetp, CM_TRACE_SYMLINK, ICL_TYPE_POINTER, adp,
80                 ICL_TYPE_STRING, aname);
81
82     if (strlen(aname) > AFSNAMEMAX || strlen(atargetName) > AFSPATHMAX) {
83         code = ENAMETOOLONG;
84         goto done2;
85     }
86
87     if (afs_IsDynroot(adp)) {
88         code = afs_DynrootVOPSymlink(adp, acred, aname, atargetName);
89         goto done2;
90     }
91
92     if (code = afs_InitReq(&treq, acred))
93         goto done2;
94
95     code = afs_VerifyVCache(adp, &treq);
96     if (code) { 
97       code = afs_CheckCode(code, &treq, 30);
98       goto done2;
99     }
100
101     /** If the volume is read-only, return error without making an RPC to the
102       * fileserver
103       */
104     if ( adp->states & CRO ) {
105         code = EROFS;
106         goto done2;
107     }
108
109     InStatus.Mask = AFS_SETMODTIME | AFS_SETMODE;
110     InStatus.ClientModTime = osi_Time();
111     alen = strlen(atargetName);     /* we want it to include the null */
112     if (*atargetName == '#' || *atargetName == '%') {
113         InStatus.UnixModeBits = 0644;   /* mt pt: null from "." at end */
114         if (alen == 1) alen++;  /* Empty string */
115     } else {
116         InStatus.UnixModeBits = 0755;
117         alen++;     /* add in the null */
118     }
119     tdc = afs_GetDCache(adp, (afs_size_t) 0, &treq, &offset, &len, 1);
120     volp = afs_FindVolume(&adp->fid, READ_LOCK); /*parent is also in same vol*/
121     ObtainWriteLock(&adp->lock,156);
122     ObtainWriteLock(&tdc->lock, 636);
123     ObtainSharedLock(&afs_xvcache,17);  /* prevent others from creating this entry */
124     /* XXX Pay attention to afs_xvcache around the whole thing!! XXX */
125     do {
126         tc = afs_Conn(&adp->fid, &treq, SHARED_LOCK);
127         if (tc) {
128             hostp = tc->srvr->server;
129             XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SYMLINK);
130             if (adp->states & CForeign) {
131                 now = osi_Time();
132 #ifdef RX_ENABLE_LOCKS
133                 AFS_GUNLOCK();
134 #endif /* RX_ENABLE_LOCKS */
135                 code = RXAFS_DFSSymlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
136                                      atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
137                                      &OutFidStatus, &OutDirStatus, &CallBack, &tsync);
138 #ifdef RX_ENABLE_LOCKS
139                 AFS_GLOCK();
140 #endif /* RX_ENABLE_LOCKS */
141             } else {
142 #ifdef RX_ENABLE_LOCKS
143                 AFS_GUNLOCK();
144 #endif /* RX_ENABLE_LOCKS */
145                 code = RXAFS_Symlink(tc->id, (struct AFSFid *) &adp->fid.Fid, aname,
146                                      atargetName, &InStatus, (struct AFSFid *) &newFid.Fid,
147                                      &OutFidStatus, &OutDirStatus, &tsync);
148 #ifdef RX_ENABLE_LOCKS
149                 AFS_GLOCK();
150 #endif /* RX_ENABLE_LOCKS */
151             }
152           XSTATS_END_TIME;
153         }
154         else code = -1;
155     } while 
156        (afs_Analyze(tc, code, &adp->fid, &treq,
157                    AFS_STATS_FS_RPCIDX_SYMLINK, SHARED_LOCK, (struct cell *)0));
158
159     UpgradeSToWLock(&afs_xvcache,40);
160     if (code) {
161         if (code < 0) {
162           ObtainWriteLock(&afs_xcbhash, 499);
163           afs_DequeueCallback(adp);
164           adp->states &= ~CStatd;
165           ReleaseWriteLock(&afs_xcbhash);
166           osi_dnlc_purgedp(adp);
167         }
168         ReleaseWriteLock(&adp->lock);
169         ReleaseWriteLock(&afs_xvcache);
170         if (tdc) {
171             ReleaseWriteLock(&tdc->lock);
172             afs_PutDCache(tdc);
173         }
174         goto done;
175     }
176     /* otherwise, we should see if we can make the change to the dir locally */
177     if (afs_LocalHero(adp, tdc, &OutDirStatus, 1)) {
178         /* we can do it locally */
179         code = afs_dir_Create(&tdc->f.inode, aname, &newFid.Fid);
180         if (code) { 
181             ZapDCE(tdc);        /* surprise error -- use invalid value */
182             DZap(&tdc->f.inode);
183         }
184     }
185     if (tdc) {
186         ReleaseWriteLock(&tdc->lock);
187         afs_PutDCache(tdc);
188     }
189     newFid.Cell = adp->fid.Cell;
190     newFid.Fid.Volume = adp->fid.Fid.Volume;
191     ReleaseWriteLock(&adp->lock);
192
193     /* now we're done with parent dir, create the link's entry.  Note that
194      * no one can get a pointer to the new cache entry until we release 
195      * the xvcache lock. */
196     tvc = afs_NewVCache(&newFid, hostp, 1, WRITE_LOCK);
197     ObtainWriteLock(&tvc->lock,157);
198     ObtainWriteLock(&afs_xcbhash, 500);
199     tvc->states |= CStatd;              /* have valid info */
200     tvc->states &= ~CBulkFetching;
201
202     if (adp->states & CForeign) {
203         tvc->states |= CForeign;
204         /* We don't have to worry about losing the callback since we're doing it 
205          * under the afs_xvcache lock actually, afs_NewVCache may drop the 
206          * afs_xvcache lock, if it calls afs_FlushVCache */
207         tvc->cbExpires = CallBack.ExpirationTime + now;
208         afs_QueueCallback(tvc, CBHash(CallBack.ExpirationTime), volp);
209     } else {
210         tvc->cbExpires = 0x7fffffff;    /* never expires, they can't change */
211      /* since it never expires, we don't have to queue the callback */
212     }
213     ReleaseWriteLock(&afs_xcbhash);
214     afs_ProcessFS(tvc, &OutFidStatus, &treq);
215
216     if (!tvc->linkData) {
217         tvc->linkData = (char *) afs_osi_Alloc(alen);
218         strncpy(tvc->linkData, atargetName, alen-1);
219         tvc->linkData[alen-1] = 0;
220     }
221     ReleaseWriteLock(&tvc->lock);
222     ReleaseWriteLock(&afs_xvcache);
223     afs_PutVCache(tvc, WRITE_LOCK);
224     code = 0;
225 done:
226     if ( volp ) 
227         afs_PutVolume(volp, READ_LOCK);
228     code = afs_CheckCode(code, &treq, 31);
229 done2:
230 #ifdef  AFS_OSF_ENV
231     AFS_RELE(ndp->ni_dvp);
232 #endif  /* AFS_OSF_ENV */
233     return code;
234 }
235
236 afs_MemHandleLink(avc, areq)
237      register struct vcache *avc;
238      struct vrequest *areq;
239   {
240       register struct dcache *tdc;
241       register char *tp, *rbuf;
242       afs_size_t offset, len;
243       afs_int32 tlen, alen;
244       register afs_int32 code;
245
246       AFS_STATCNT(afs_MemHandleLink);
247       /* two different formats, one for links protected 644, have a "." at
248          the end of the file name, which we turn into a null.  Others, 
249          protected 755, we add a null to the end of */
250       if (!avc->linkData) {
251           void *addr;
252           tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
253           if (!tdc) {
254               return EIO;
255           }
256           /* otherwise we have the data loaded, go for it */
257           if (len > 1024) {
258               afs_PutDCache(tdc);
259               return EFAULT;
260           }
261           if (avc->m.Mode       & 0111) alen = len+1;   /* regular link */
262           else alen = len;                      /* mt point */
263           rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
264           ObtainReadLock(&tdc->lock);
265           addr = afs_MemCacheOpen(tdc->f.inode);
266           tlen = len;
267           code = afs_MemReadBlk(addr, 0, rbuf, tlen);
268           afs_MemCacheClose(addr);
269           ReleaseReadLock(&tdc->lock);
270           afs_PutDCache(tdc);
271           rbuf[alen-1] = 0;
272           alen = strlen(rbuf) + 1;
273           tp = afs_osi_Alloc(alen); /* make room for terminating null */
274           memcpy(tp, rbuf, alen);
275           osi_FreeLargeSpace(rbuf);
276           if (code != len) {
277               afs_osi_Free(tp, alen);
278               return EIO;
279           }
280           avc->linkData = tp;
281       }
282       return 0;
283   }
284
285 afs_UFSHandleLink(avc, areq)
286     register struct vcache *avc;
287     struct vrequest *areq; 
288 {
289     register struct dcache *tdc;
290     register char *tp, *rbuf;
291     char *tfile;
292     afs_size_t offset, len;
293     afs_int32 tlen, alen;
294     register afs_int32 code;
295
296     /* two different formats, one for links protected 644, have a "." at the end
297         of the file name, which we turn into a null.  Others, protected 755,
298         we add a null to the end of */
299    AFS_STATCNT(afs_UFSHandleLink);
300     if (!avc->linkData) {
301         tdc = afs_GetDCache(avc, (afs_size_t) 0, areq, &offset, &len, 0);
302         afs_Trace3(afs_iclSetp, CM_TRACE_UFSLINK, ICL_TYPE_POINTER, avc,
303                         ICL_TYPE_POINTER, tdc,
304                         ICL_TYPE_OFFSET, ICL_HANDLE_OFFSET(avc->m.Length));
305         if (!tdc) {
306             return EIO;
307         }
308         /* otherwise we have the data loaded, go for it */
309         if (len > 1024) {
310             afs_PutDCache(tdc);
311             return EFAULT;
312         }
313         if (avc->m.Mode & 0111) alen = len+1;   /* regular link */
314         else alen = len;                        /* mt point */
315         rbuf = (char *) osi_AllocLargeSpace(AFS_LRALLOCSIZ);
316         tlen = len;
317         ObtainReadLock(&tdc->lock);
318         tfile = osi_UFSOpen (tdc->f.inode);
319         code = afs_osi_Read(tfile, -1, rbuf, tlen);
320         osi_UFSClose(tfile);
321         ReleaseReadLock(&tdc->lock);
322         afs_PutDCache(tdc);
323         rbuf[alen-1] = '\0';
324         alen = strlen(rbuf) + 1;
325         tp = afs_osi_Alloc(alen);       /* make room for terminating null */
326         memcpy(tp, rbuf, alen);
327         osi_FreeLargeSpace(rbuf);
328         if (code != tlen) {
329             afs_osi_Free(tp, alen);
330             return EIO;
331         }
332         avc->linkData = tp;
333     }
334     return 0;
335 }
336
337 afs_readlink(OSI_VC_ARG(avc), auio, acred)
338     OSI_VC_DECL(avc);
339     struct uio *auio;
340     struct AFS_UCRED *acred; 
341 {
342     register afs_int32 code;
343     struct vrequest treq;
344     register char *tp;
345     OSI_VC_CONVERT(avc)
346
347     AFS_STATCNT(afs_readlink);
348     afs_Trace1(afs_iclSetp, CM_TRACE_READLINK, ICL_TYPE_POINTER, avc);
349     if (code = afs_InitReq(&treq, acred)) return code;
350     code = afs_VerifyVCache(avc, &treq);
351     if (code) goto done;
352     if (vType(avc) != VLNK) {
353         code = EINVAL;
354         goto done;
355     }
356     ObtainWriteLock(&avc->lock,158);
357     code = afs_HandleLink(avc, &treq);
358     /* finally uiomove it to user-land */
359     if (code == 0) {
360         tp = avc->linkData;
361         if (tp) AFS_UIOMOVE(tp, strlen(tp), UIO_READ, auio, code);
362         else {
363             code = EIO;
364         }
365     }
366     ReleaseWriteLock(&avc->lock);
367 done:
368     code = afs_CheckCode(code, &treq, 32);
369     return code;
370 }