| 1 |
/* |
| 2 |
* Copyright (C) 2006 by Latchesar Ionkov <[email protected]> |
| 3 |
* Copyright (C) 2010-2014 by Lawrence Livermore National Security, LLC |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 20 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 21 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 22 |
* DEALINGS IN THE SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#if HAVE_CONFIG_H |
| 26 |
#include "config.h" |
| 27 |
#endif |
| 28 |
#include <stdlib.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <string.h> |
| 31 |
#include <stdarg.h> |
| 32 |
#include <unistd.h> |
| 33 |
#include <pthread.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <sys/socket.h> |
| 36 |
#include <stdint.h> |
| 37 |
#include <inttypes.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <fcntl.h> |
| 40 |
|
| 41 |
#include "9p.h" |
| 42 |
#include "npfs.h" |
| 43 |
#include "npclient.h" |
| 44 |
#include "npcimpl.h" |
| 45 |
|
| 46 |
void |
| 47 |
npc_finish (Npcfsys *fs) |
| 48 |
{ |
| 49 |
if (fs->disconnect) |
| 50 |
fs->disconnect (fs); |
| 51 |
fs->decref (fs); |
| 52 |
} |
| 53 |
|
| 54 |
Npcfsys* |
| 55 |
npc_start (int rfd, int wfd, int msize, int flags) |
| 56 |
{ |
| 57 |
Npcfsys *fs; |
| 58 |
Npfcall *tc = NULL, *rc = NULL; |
| 59 |
|
| 60 |
if ((flags & NPC_MULTI_RPC)) |
| 61 |
fs = npc_create_mtfsys (rfd, wfd, msize, flags); |
| 62 |
else |
| 63 |
fs = npc_create_fsys (rfd, wfd, msize, flags); |
| 64 |
if (!fs) |
| 65 |
goto done; |
| 66 |
if (!(tc = np_create_tversion (msize, "9P2000.L"))) { |
| 67 |
np_uerror (ENOMEM); |
| 68 |
goto done; |
| 69 |
} |
| 70 |
if (fs->rpc (fs, tc, &rc) < 0) |
| 71 |
goto done; |
| 72 |
if (rc->u.rversion.msize < msize) |
| 73 |
fs->msize = rc->u.rversion.msize; |
| 74 |
if (np_strcmp (&rc->u.rversion.version, "9P2000.L") != 0) { |
| 75 |
np_uerror(EIO); |
| 76 |
goto done; |
| 77 |
} |
| 78 |
done: |
| 79 |
if (tc) |
| 80 |
free (tc); |
| 81 |
if (rc) |
| 82 |
free (rc); |
| 83 |
if (np_rerror () && fs) { |
| 84 |
npc_finish (fs); |
| 85 |
fs = NULL; |
| 86 |
} |
| 87 |
return fs; |
| 88 |
} |
| 89 |
|
| 90 |
Npcfid* |
| 91 |
npc_auth (Npcfsys *fs, char *aname, u32 uid, AuthFun auth) |
| 92 |
{ |
| 93 |
Npcfid *afid = NULL; |
| 94 |
Npfcall *tc = NULL, *rc = NULL; |
| 95 |
|
| 96 |
if (!(afid = npc_fid_alloc (fs))) |
| 97 |
goto done; |
| 98 |
if (!(tc = np_create_tauth (afid->fid, NULL, aname, uid))) { |
| 99 |
np_uerror (ENOMEM); |
| 100 |
npc_fid_free (afid); |
| 101 |
afid = NULL; |
| 102 |
goto done; |
| 103 |
} |
| 104 |
if (afid->fsys->rpc (afid->fsys, tc, &rc) < 0) { |
| 105 |
npc_fid_free (afid); |
| 106 |
afid = NULL; |
| 107 |
if (np_rerror() == 0 || np_rerror() == ENOENT) |
| 108 |
np_uerror(0); /* auth not required */ |
| 109 |
goto done; |
| 110 |
} |
| 111 |
if (auth && auth (afid, uid) < 0) { |
| 112 |
int saved_err = np_rerror (); |
| 113 |
(void)npc_clunk (afid); |
| 114 |
afid = NULL; |
| 115 |
np_uerror (saved_err); |
| 116 |
goto done; |
| 117 |
} |
| 118 |
done: |
| 119 |
if (tc) |
| 120 |
free(tc); |
| 121 |
if (rc) |
| 122 |
free(rc); |
| 123 |
return afid; |
| 124 |
} |
| 125 |
|
| 126 |
Npcfid * |
| 127 |
npc_attach (Npcfsys *fs, Npcfid *afid, char *aname, uid_t uid) |
| 128 |
{ |
| 129 |
Npfcall *tc = NULL, *rc = NULL; |
| 130 |
Npcfid *fid = NULL; |
| 131 |
|
| 132 |
if (!(fid = npc_fid_alloc (fs))) |
| 133 |
goto done; |
| 134 |
if (!(tc = np_create_tattach (fid->fid, afid ? afid->fid : P9_NOFID, |
| 135 |
NULL, aname, uid))) { |
| 136 |
np_uerror (ENOMEM); |
| 137 |
goto done; |
| 138 |
} |
| 139 |
if (fs->rpc (fs, tc, &rc) < 0) |
| 140 |
goto done; |
| 141 |
done: |
| 142 |
if (tc) |
| 143 |
free (tc); |
| 144 |
if (rc) |
| 145 |
free (rc); |
| 146 |
if (np_rerror () && fid) { |
| 147 |
npc_fid_free (fid); |
| 148 |
fid = NULL; |
| 149 |
} |
| 150 |
return fid; |
| 151 |
} |
| 152 |
|
| 153 |
int |
| 154 |
npc_clunk (Npcfid *fid) |
| 155 |
{ |
| 156 |
Npfcall *tc = NULL, *rc = NULL; |
| 157 |
int ret = -1; |
| 158 |
|
| 159 |
if (!(tc = np_create_tclunk (fid->fid))) { |
| 160 |
np_uerror (ENOMEM); |
| 161 |
goto done; |
| 162 |
} |
| 163 |
if (fid->fsys->rpc (fid->fsys, tc, &rc) < 0) |
| 164 |
goto done; |
| 165 |
npc_fid_free(fid); |
| 166 |
ret = 0; |
| 167 |
done: |
| 168 |
if (tc) |
| 169 |
free (tc); |
| 170 |
if (rc) |
| 171 |
free (rc); |
| 172 |
return ret; |
| 173 |
} |
| 174 |
|
| 175 |
Npcfid * |
| 176 |
npc_mount (int rfd, int wfd, int msize, char *aname, AuthFun auth) |
| 177 |
{ |
| 178 |
Npcfsys *fs; |
| 179 |
Npcfid *afid, *fid; |
| 180 |
int flags = 0; |
| 181 |
|
| 182 |
//flags |= NPC_SHORTREAD_EOF; |
| 183 |
if (!(fs = npc_start (rfd, wfd, msize, flags))) |
| 184 |
return NULL; |
| 185 |
if (!(afid = npc_auth (fs, aname, geteuid (), auth)) && np_rerror ()) { |
| 186 |
npc_finish (fs); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
if (!(fid = npc_attach (fs, afid, aname, geteuid ()))) { |
| 190 |
int saved_err = np_rerror (); |
| 191 |
if (afid) |
| 192 |
(void)npc_clunk (afid); |
| 193 |
npc_finish (fs); |
| 194 |
np_uerror (saved_err); |
| 195 |
return NULL; |
| 196 |
} |
| 197 |
if (afid) |
| 198 |
(void)npc_clunk (afid); |
| 199 |
return fid; |
| 200 |
}; |
| 201 |
|
| 202 |
void |
| 203 |
npc_umount (Npcfid *fid) |
| 204 |
{ |
| 205 |
Npcfsys *fs = fid->fsys; |
| 206 |
|
| 207 |
(void)npc_clunk (fid); |
| 208 |
npc_finish (fs); |
| 209 |
} |