Changeset 388 for python/vendor/current/Modules/nismodule.c
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Modules/nismodule.c
r2 r388 1 1 /*********************************************************** 2 2 Written by: 3 4 5 6 7 8 3 Fred Gansevles <Fred.Gansevles@cs.utwente.nl> 4 B&O group, 5 Faculteit der Informatica, 6 Universiteit Twente, 7 Enschede, 8 the Netherlands. 9 9 ******************************************************************/ 10 10 … … 24 24 #endif 25 25 26 PyDoc_STRVAR(get_default_domain__doc__, 26 PyDoc_STRVAR(get_default_domain__doc__, 27 27 "get_default_domain() -> str\n\ 28 28 Corresponds to the C library yp_get_default_domain() call, returning\n\ … … 50 50 nis_error (int err) 51 51 { 52 53 52 PyErr_SetString(NisError, yperr_string(err)); 53 return NULL; 54 54 } 55 55 56 56 static struct nis_map { 57 58 59 57 char *alias; 58 char *map; 59 int fix; 60 60 } aliases [] = { 61 {"passwd", "passwd.byname",0},62 {"group", "group.byname",0},63 {"networks", "networks.byaddr",0},64 {"hosts", "hosts.byname",0},65 {"protocols", "protocols.bynumber",0},66 {"services", "services.byname",0},67 {"aliases", "mail.aliases",1}, /* created with 'makedbm -a' */68 {"ethers", "ethers.byname",0},69 {0L, 0L,0}61 {"passwd", "passwd.byname", 0}, 62 {"group", "group.byname", 0}, 63 {"networks", "networks.byaddr", 0}, 64 {"hosts", "hosts.byname", 0}, 65 {"protocols", "protocols.bynumber", 0}, 66 {"services", "services.byname", 0}, 67 {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */ 68 {"ethers", "ethers.byname", 0}, 69 {0L, 0L, 0} 70 70 }; 71 71 … … 73 73 nis_mapname (char *map, int *pfix) 74 74 { 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 75 int i; 76 77 *pfix = 0; 78 for (i=0; aliases[i].alias != 0L; i++) { 79 if (!strcmp (aliases[i].alias, map)) { 80 *pfix = aliases[i].fix; 81 return aliases[i].map; 82 } 83 if (!strcmp (aliases[i].map, map)) { 84 *pfix = aliases[i].fix; 85 return aliases[i].map; 86 } 87 } 88 89 return map; 90 90 } 91 91 … … 97 97 98 98 struct ypcallback_data { 99 PyObject*dict;100 intfix;101 99 PyObject *dict; 100 int fix; 101 PyThreadState *state; 102 102 }; 103 103 … … 106 106 int invallen, struct ypcallback_data *indata) 107 107 { 108 if (instatus == YP_TRUE) { 109 PyObject *key; 110 PyObject *val; 111 int err; 112 113 PyEval_RestoreThread(indata->state); 114 if (indata->fix) { 115 if (inkeylen > 0 && inkey[inkeylen-1] == '\0') 116 inkeylen--; 117 if (invallen > 0 && inval[invallen-1] == '\0') 118 invallen--; 119 } 120 key = PyString_FromStringAndSize(inkey, inkeylen); 121 val = PyString_FromStringAndSize(inval, invallen); 122 if (key == NULL || val == NULL) { 123 /* XXX error -- don't know how to handle */ 124 PyErr_Clear(); 125 Py_XDECREF(key); 126 Py_XDECREF(val); 127 return 1; 128 } 129 err = PyDict_SetItem(indata->dict, key, val); 130 Py_DECREF(key); 131 Py_DECREF(val); 132 if (err != 0) 133 PyErr_Clear(); 134 indata->state = PyEval_SaveThread(); 135 if (err != 0) 136 return 1; 137 return 0; 138 } 139 return 1; 108 if (instatus == YP_TRUE) { 109 PyObject *key; 110 PyObject *val; 111 int err; 112 113 PyEval_RestoreThread(indata->state); 114 if (indata->fix) { 115 if (inkeylen > 0 && inkey[inkeylen-1] == '\0') 116 inkeylen--; 117 if (invallen > 0 && inval[invallen-1] == '\0') 118 invallen--; 119 } 120 key = PyString_FromStringAndSize(inkey, inkeylen); 121 val = PyString_FromStringAndSize(inval, invallen); 122 if (key == NULL || val == NULL) { 123 /* XXX error -- don't know how to handle */ 124 PyErr_Clear(); 125 Py_XDECREF(key); 126 Py_XDECREF(val); 127 indata->state = PyEval_SaveThread(); 128 return 1; 129 } 130 err = PyDict_SetItem(indata->dict, key, val); 131 Py_DECREF(key); 132 Py_DECREF(val); 133 if (err != 0) 134 PyErr_Clear(); 135 indata->state = PyEval_SaveThread(); 136 if (err != 0) 137 return 1; 138 return 0; 139 } 140 return 1; 140 141 } 141 142 … … 143 144 nis_get_default_domain (PyObject *self) 144 145 { 145 146 147 148 149 150 151 152 153 146 char *domain; 147 int err; 148 PyObject *res; 149 150 if ((err = yp_get_default_domain(&domain)) != 0) 151 return nis_error(err); 152 153 res = PyString_FromStringAndSize (domain, strlen(domain)); 154 return res; 154 155 } 155 156 … … 157 158 nis_match (PyObject *self, PyObject *args, PyObject *kwdict) 158 159 { 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 160 char *match; 161 char *domain = NULL; 162 int keylen, len; 163 char *key, *map; 164 int err; 165 PyObject *res; 166 int fix; 167 static char *kwlist[] = {"key", "map", "domain", NULL}; 168 169 if (!PyArg_ParseTupleAndKeywords(args, kwdict, 170 "t#s|s:match", kwlist, 171 &key, &keylen, &map, &domain)) 172 return NULL; 173 if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) 174 return nis_error(err); 175 map = nis_mapname (map, &fix); 176 if (fix) 177 keylen++; 178 Py_BEGIN_ALLOW_THREADS 179 err = yp_match (domain, map, key, keylen, &match, &len); 180 Py_END_ALLOW_THREADS 181 if (fix) 182 len--; 183 if (err != 0) 184 return nis_error(err); 185 res = PyString_FromStringAndSize (match, len); 186 free (match); 187 return res; 187 188 } 188 189 … … 190 191 nis_cat (PyObject *self, PyObject *args, PyObject *kwdict) 191 192 { 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 193 char *domain = NULL; 194 char *map; 195 struct ypall_callback cb; 196 struct ypcallback_data data; 197 PyObject *dict; 198 int err; 199 static char *kwlist[] = {"map", "domain", NULL}; 200 201 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "s|s:cat", 202 kwlist, &map, &domain)) 203 return NULL; 204 if (!domain && ((err = yp_get_default_domain(&domain)) != 0)) 205 return nis_error(err); 206 dict = PyDict_New (); 207 if (dict == NULL) 208 return NULL; 209 cb.foreach = (foreachfunc)nis_foreach; 210 data.dict = dict; 211 map = nis_mapname (map, &data.fix); 212 cb.data = (char *)&data; 213 data.state = PyEval_SaveThread(); 214 err = yp_all (domain, map, &cb); 215 PyEval_RestoreThread(data.state); 216 if (err != 0) { 217 Py_DECREF(dict); 218 return nis_error(err); 219 } 220 return dict; 220 221 } 221 222 … … 223 224 This is not portable to machines with 16-bit ints and no prototypes */ 224 225 #ifndef YPPROC_MAPLIST 225 #define YPPROC_MAPLIST 226 #define YPPROC_MAPLIST 11 226 227 #endif 227 228 #ifndef YPPROG 228 #define YPPROG 229 #define YPPROG 100004 229 230 #endif 230 231 #ifndef YPVERS 231 #define YPVERS 232 #define YPVERS 2 232 233 #endif 233 234 … … 236 237 237 238 enum nisstat { 238 239 240 241 242 243 244 245 246 247 248 239 NIS_TRUE = 1, 240 NIS_NOMORE = 2, 241 NIS_FALSE = 0, 242 NIS_NOMAP = -1, 243 NIS_NODOM = -2, 244 NIS_NOKEY = -3, 245 NIS_BADOP = -4, 246 NIS_BADDB = -5, 247 NIS_YPERR = -6, 248 NIS_BADARGS = -7, 249 NIS_VERS = -8 249 250 }; 250 251 typedef enum nisstat nisstat; 251 252 252 253 struct nismaplist { 253 254 254 mapname map; 255 struct nismaplist *next; 255 256 }; 256 257 typedef struct nismaplist nismaplist; 257 258 258 259 struct nisresp_maplist { 259 260 260 nisstat stat; 261 nismaplist *maps; 261 262 }; 262 263 typedef struct nisresp_maplist nisresp_maplist; … … 268 269 nis_xdr_domainname(XDR *xdrs, domainname *objp) 269 270 { 270 271 272 273 271 if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { 272 return (FALSE); 273 } 274 return (TRUE); 274 275 } 275 276 … … 278 279 nis_xdr_mapname(XDR *xdrs, mapname *objp) 279 280 { 280 281 282 283 281 if (!xdr_string(xdrs, objp, YPMAXMAP)) { 282 return (FALSE); 283 } 284 return (TRUE); 284 285 } 285 286 … … 288 289 nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) 289 290 { 290 291 292 293 294 295 296 297 298 291 if (!nis_xdr_mapname(xdrs, &objp->map)) { 292 return (FALSE); 293 } 294 if (!xdr_pointer(xdrs, (char **)&objp->next, 295 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) 296 { 297 return (FALSE); 298 } 299 return (TRUE); 299 300 } 300 301 … … 303 304 nis_xdr_ypstat(XDR *xdrs, nisstat *objp) 304 305 { 305 306 307 308 306 if (!xdr_enum(xdrs, (enum_t *)objp)) { 307 return (FALSE); 308 } 309 return (TRUE); 309 310 } 310 311 … … 314 315 nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) 315 316 { 316 317 318 319 320 321 322 323 324 317 if (!nis_xdr_ypstat(xdrs, &objp->stat)) { 318 return (FALSE); 319 } 320 if (!xdr_pointer(xdrs, (char **)&objp->maps, 321 sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) 322 { 323 return (FALSE); 324 } 325 return (TRUE); 325 326 } 326 327 … … 330 331 nisproc_maplist_2(domainname *argp, CLIENT *clnt) 331 332 { 332 333 334 335 336 337 338 339 340 341 342 333 static nisresp_maplist res; 334 335 memset(&res, 0, sizeof(res)); 336 if (clnt_call(clnt, YPPROC_MAPLIST, 337 (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, 338 (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, 339 TIMEOUT) != RPC_SUCCESS) 340 { 341 return (NULL); 342 } 343 return (&res); 343 344 } 344 345 … … 347 348 nis_maplist (char *dom) 348 349 { 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 350 nisresp_maplist *list; 351 CLIENT *cl; 352 char *server = NULL; 353 int mapi = 0; 354 355 while (!server && aliases[mapi].map != 0L) { 356 yp_master (dom, aliases[mapi].map, &server); 357 mapi++; 358 } 359 if (!server) { 360 PyErr_SetString(NisError, "No NIS master found for any map"); 361 return NULL; 362 } 363 cl = clnt_create(server, YPPROG, YPVERS, "tcp"); 364 if (cl == NULL) { 365 PyErr_SetString(NisError, clnt_spcreateerror(server)); 366 goto finally; 367 } 368 list = nisproc_maplist_2 (&dom, cl); 369 clnt_destroy(cl); 370 if (list == NULL) 371 goto finally; 372 if (list->stat != NIS_TRUE) 373 goto finally; 374 375 free(server); 376 return list->maps; 376 377 377 378 finally: 378 379 379 free(server); 380 return NULL; 380 381 } 381 382 … … 383 384 nis_maps (PyObject *self, PyObject *args, PyObject *kwdict) 384 385 { 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 386 char *domain = NULL; 387 nismaplist *maps; 388 PyObject *list; 389 int err; 390 static char *kwlist[] = {"domain", NULL}; 391 392 if (!PyArg_ParseTupleAndKeywords(args, kwdict, 393 "|s:maps", kwlist, &domain)) 394 return NULL; 395 if (!domain && ((err = yp_get_default_domain (&domain)) != 0)) { 396 nis_error(err); 397 return NULL; 398 } 399 400 if ((maps = nis_maplist (domain)) == NULL) 401 return NULL; 402 if ((list = PyList_New(0)) == NULL) 403 return NULL; 404 for (maps = maps; maps; maps = maps->next) { 405 PyObject *str = PyString_FromString(maps->map); 406 if (!str || PyList_Append(list, str) < 0) 407 { 408 Py_DECREF(list); 409 list = NULL; 410 break; 411 } 412 Py_DECREF(str); 413 } 414 /* XXX Shouldn't we free the list of maps now? */ 415 return list; 415 416 } 416 417 417 418 static PyMethodDef nis_methods[] = { 418 {"match",(PyCFunction)nis_match,419 420 421 {"cat",(PyCFunction)nis_cat,422 423 424 {"maps",(PyCFunction)nis_maps,425 426 427 {"get_default_domain",(PyCFunction)nis_get_default_domain,428 429 430 {NULL, NULL}/* Sentinel */419 {"match", (PyCFunction)nis_match, 420 METH_VARARGS | METH_KEYWORDS, 421 match__doc__}, 422 {"cat", (PyCFunction)nis_cat, 423 METH_VARARGS | METH_KEYWORDS, 424 cat__doc__}, 425 {"maps", (PyCFunction)nis_maps, 426 METH_VARARGS | METH_KEYWORDS, 427 maps__doc__}, 428 {"get_default_domain", (PyCFunction)nis_get_default_domain, 429 METH_NOARGS, 430 get_default_domain__doc__}, 431 {NULL, NULL} /* Sentinel */ 431 432 }; 432 433 … … 437 438 initnis (void) 438 439 { 439 440 441 442 443 444 445 446 447 } 440 PyObject *m, *d; 441 m = Py_InitModule3("nis", nis_methods, nis__doc__); 442 if (m == NULL) 443 return; 444 d = PyModule_GetDict(m); 445 NisError = PyErr_NewException("nis.error", NULL, NULL); 446 if (NisError != NULL) 447 PyDict_SetItemString(d, "error", NisError); 448 }
Note:
See TracChangeset
for help on using the changeset viewer.