Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Written by: |
| 3 | Fred Gansevles <Fred.Gansevles@cs.utwente.nl> |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 4 | B&O group, |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 5 | Faculteit der Informatica, |
| 6 | Universiteit Twente, |
| 7 | Enschede, |
| 8 | the Netherlands. |
| 9 | ******************************************************************/ |
| 10 | |
| 11 | /* NIS module implementation */ |
| 12 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 13 | #include "Python.h" |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 15 | #include <sys/time.h> |
| 16 | #include <sys/types.h> |
| 17 | #include <rpc/rpc.h> |
| 18 | #include <rpcsvc/yp_prot.h> |
Guido van Rossum | 8a170cb | 1996-08-08 19:11:41 +0000 | [diff] [blame] | 19 | #include <rpcsvc/ypclnt.h> |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 259552d | 1996-12-09 18:46:28 +0000 | [diff] [blame] | 21 | #ifdef __sgi |
| 22 | /* This is missing from rpcsvc/ypclnt.h */ |
Thomas Wouters | bd4bc4e | 2000-07-22 23:57:55 +0000 | [diff] [blame] | 23 | extern int yp_get_default_domain(char **); |
Guido van Rossum | 259552d | 1996-12-09 18:46:28 +0000 | [diff] [blame] | 24 | #endif |
| 25 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 26 | static PyObject *NisError; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 27 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 28 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 29 | nis_error (int err) |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 30 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 31 | PyErr_SetString(NisError, yperr_string(err)); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 32 | return NULL; |
| 33 | } |
| 34 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 35 | static struct nis_map { |
| 36 | char *alias; |
| 37 | char *map; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 38 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 39 | } aliases [] = { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 40 | {"passwd", "passwd.byname", 0}, |
| 41 | {"group", "group.byname", 0}, |
| 42 | {"networks", "networks.byaddr", 0}, |
| 43 | {"hosts", "hosts.byname", 0}, |
| 44 | {"protocols", "protocols.bynumber", 0}, |
| 45 | {"services", "services.byname", 0}, |
| 46 | {"aliases", "mail.aliases", 1}, /* created with 'makedbm -a' */ |
| 47 | {"ethers", "ethers.byname", 0}, |
| 48 | {0L, 0L, 0} |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static char * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 52 | nis_mapname (char *map, int *pfix) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 53 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 54 | int i; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 56 | *pfix = 0; |
| 57 | for (i=0; aliases[i].alias != 0L; i++) { |
| 58 | if (!strcmp (aliases[i].alias, map)) { |
| 59 | *pfix = aliases[i].fix; |
| 60 | return aliases[i].map; |
| 61 | } |
| 62 | if (!strcmp (aliases[i].map, map)) { |
| 63 | *pfix = aliases[i].fix; |
| 64 | return aliases[i].map; |
| 65 | } |
| 66 | } |
| 67 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 68 | return map; |
| 69 | } |
| 70 | |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 71 | typedef int (*foreachfunc)(int, char *, int, char *, int, char *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 73 | struct ypcallback_data { |
| 74 | PyObject *dict; |
| 75 | int fix; |
| 76 | }; |
| 77 | |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 78 | static int |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 79 | nis_foreach (int instatus, char *inkey, int inkeylen, char *inval, |
| 80 | int invallen, struct ypcallback_data *indata) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 81 | { |
| 82 | if (instatus == YP_TRUE) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 83 | PyObject *key; |
| 84 | PyObject *val; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 85 | int err; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 86 | |
| 87 | if (indata->fix) { |
| 88 | inkeylen--; |
| 89 | invallen--; |
| 90 | } |
| 91 | key = PyString_FromStringAndSize(inkey, inkeylen); |
| 92 | val = PyString_FromStringAndSize(inval, invallen); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 93 | if (key == NULL || val == NULL) { |
| 94 | /* XXX error -- don't know how to handle */ |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 95 | PyErr_Clear(); |
| 96 | Py_XDECREF(key); |
| 97 | Py_XDECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 98 | return 1; |
| 99 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 100 | err = PyDict_SetItem(indata->dict, key, val); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 101 | Py_DECREF(key); |
| 102 | Py_DECREF(val); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 103 | if (err != 0) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 104 | PyErr_Clear(); |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 105 | return 1; |
| 106 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | return 1; |
| 110 | } |
| 111 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 112 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 113 | nis_match (PyObject *self, PyObject *args) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 115 | char *match; |
| 116 | char *domain; |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 117 | int keylen, len; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 118 | char *key, *map; |
| 119 | int err; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 120 | PyObject *res; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 121 | int fix; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | 7e48898 | 1998-10-08 02:25:24 +0000 | [diff] [blame] | 123 | if (!PyArg_Parse(args, "(t#s)", &key, &keylen, &map)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 124 | return NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 125 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 126 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 127 | map = nis_mapname (map, &fix); |
| 128 | if (fix) |
| 129 | keylen++; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 130 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | e77a757 | 1993-11-03 15:01:26 +0000 | [diff] [blame] | 131 | err = yp_match (domain, map, key, keylen, &match, &len); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 132 | Py_END_ALLOW_THREADS |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 133 | if (fix) |
| 134 | len--; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 135 | if (err != 0) |
| 136 | return nis_error(err); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 137 | res = PyString_FromStringAndSize (match, len); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 138 | free (match); |
| 139 | return res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 142 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 143 | nis_cat (PyObject *self, PyObject *args) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 144 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 145 | char *domain; |
| 146 | char *map; |
| 147 | struct ypall_callback cb; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 148 | struct ypcallback_data data; |
| 149 | PyObject *dict; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 150 | int err; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 151 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 152 | if (!PyArg_Parse(args, "s", &map)) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 153 | return NULL; |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 154 | if ((err = yp_get_default_domain(&domain)) != 0) |
| 155 | return nis_error(err); |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 156 | dict = PyDict_New (); |
| 157 | if (dict == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 158 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 159 | cb.foreach = (foreachfunc)nis_foreach; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 160 | data.dict = dict; |
| 161 | map = nis_mapname (map, &data.fix); |
| 162 | cb.data = (char *)&data; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 163 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 164 | err = yp_all (domain, map, &cb); |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 165 | Py_END_ALLOW_THREADS |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 166 | if (err != 0) { |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 167 | Py_DECREF(dict); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 168 | return nis_error(err); |
| 169 | } |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 170 | return dict; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 173 | /* These should be u_long on Sun h/w but not on 64-bit h/w. |
| 174 | This is not portable to machines with 16-bit ints and no prototypes */ |
| 175 | #ifndef YPPROC_MAPLIST |
| 176 | #define YPPROC_MAPLIST 11 |
| 177 | #endif |
| 178 | #ifndef YPPROG |
| 179 | #define YPPROG 100004 |
| 180 | #endif |
| 181 | #ifndef YPVERS |
| 182 | #define YPVERS 2 |
| 183 | #endif |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 184 | |
| 185 | typedef char *domainname; |
| 186 | typedef char *mapname; |
| 187 | |
| 188 | enum nisstat { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 189 | NIS_TRUE = 1, |
| 190 | NIS_NOMORE = 2, |
| 191 | NIS_FALSE = 0, |
| 192 | NIS_NOMAP = -1, |
| 193 | NIS_NODOM = -2, |
| 194 | NIS_NOKEY = -3, |
| 195 | NIS_BADOP = -4, |
| 196 | NIS_BADDB = -5, |
| 197 | NIS_YPERR = -6, |
| 198 | NIS_BADARGS = -7, |
| 199 | NIS_VERS = -8 |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 200 | }; |
| 201 | typedef enum nisstat nisstat; |
| 202 | |
| 203 | struct nismaplist { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 204 | mapname map; |
| 205 | struct nismaplist *next; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 206 | }; |
| 207 | typedef struct nismaplist nismaplist; |
| 208 | |
| 209 | struct nisresp_maplist { |
| 210 | nisstat stat; |
| 211 | nismaplist *maps; |
| 212 | }; |
| 213 | typedef struct nisresp_maplist nisresp_maplist; |
| 214 | |
| 215 | static struct timeval TIMEOUT = { 25, 0 }; |
| 216 | |
| 217 | static |
| 218 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 219 | nis_xdr_domainname(XDR *xdrs, domainname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 220 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 221 | if (!xdr_string(xdrs, objp, YPMAXDOMAIN)) { |
| 222 | return (FALSE); |
| 223 | } |
| 224 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static |
| 228 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 229 | nis_xdr_mapname(XDR *xdrs, mapname *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 230 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 231 | if (!xdr_string(xdrs, objp, YPMAXMAP)) { |
| 232 | return (FALSE); |
| 233 | } |
| 234 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static |
| 238 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 239 | nis_xdr_ypmaplist(XDR *xdrs, nismaplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 240 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 241 | if (!nis_xdr_mapname(xdrs, &objp->map)) { |
| 242 | return (FALSE); |
| 243 | } |
| 244 | if (!xdr_pointer(xdrs, (char **)&objp->next, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 245 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 246 | { |
| 247 | return (FALSE); |
| 248 | } |
| 249 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static |
| 253 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 254 | nis_xdr_ypstat(XDR *xdrs, nisstat *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 255 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 256 | if (!xdr_enum(xdrs, (enum_t *)objp)) { |
| 257 | return (FALSE); |
| 258 | } |
| 259 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | static |
| 264 | bool_t |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 265 | nis_xdr_ypresp_maplist(XDR *xdrs, nisresp_maplist *objp) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 266 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 267 | if (!nis_xdr_ypstat(xdrs, &objp->stat)) { |
| 268 | return (FALSE); |
| 269 | } |
| 270 | if (!xdr_pointer(xdrs, (char **)&objp->maps, |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 271 | sizeof(nismaplist), (xdrproc_t)nis_xdr_ypmaplist)) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 272 | { |
| 273 | return (FALSE); |
| 274 | } |
| 275 | return (TRUE); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | |
| 279 | static |
| 280 | nisresp_maplist * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 281 | nisproc_maplist_2(domainname *argp, CLIENT *clnt) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 282 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 283 | static nisresp_maplist res; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 284 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 285 | memset(&res, 0, sizeof(res)); |
Guido van Rossum | c501583 | 1998-10-07 16:36:14 +0000 | [diff] [blame] | 286 | if (clnt_call(clnt, YPPROC_MAPLIST, |
| 287 | (xdrproc_t)nis_xdr_domainname, (caddr_t)argp, |
| 288 | (xdrproc_t)nis_xdr_ypresp_maplist, (caddr_t)&res, |
| 289 | TIMEOUT) != RPC_SUCCESS) |
| 290 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 291 | return (NULL); |
| 292 | } |
| 293 | return (&res); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static |
| 297 | nismaplist * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 298 | nis_maplist (void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 299 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 300 | nisresp_maplist *list; |
| 301 | char *dom; |
Thomas Wouters | e75e6d0 | 2001-01-21 23:34:12 +0000 | [diff] [blame] | 302 | CLIENT *cl; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 303 | char *server = NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 304 | int mapi = 0; |
Barry Warsaw | 1abda0f | 1996-12-11 01:00:46 +0000 | [diff] [blame] | 305 | int err; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 306 | |
Barry Warsaw | 1abda0f | 1996-12-11 01:00:46 +0000 | [diff] [blame] | 307 | if ((err = yp_get_default_domain (&dom)) != 0) { |
| 308 | nis_error(err); |
| 309 | return NULL; |
| 310 | } |
Barry Warsaw | 3696c52 | 1996-12-11 00:29:14 +0000 | [diff] [blame] | 311 | |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 312 | while (!server && aliases[mapi].map != 0L) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 313 | yp_master (dom, aliases[mapi].map, &server); |
| 314 | mapi++; |
| 315 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 316 | if (!server) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 317 | PyErr_SetString(NisError, "No NIS master found for any map"); |
| 318 | return NULL; |
| 319 | } |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 320 | cl = clnt_create(server, YPPROG, YPVERS, "tcp"); |
| 321 | if (cl == NULL) { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 322 | PyErr_SetString(NisError, clnt_spcreateerror(server)); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 323 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 324 | } |
| 325 | list = nisproc_maplist_2 (&dom, cl); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 326 | clnt_destroy(cl); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 327 | if (list == NULL) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 328 | goto finally; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 329 | if (list->stat != NIS_TRUE) |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 330 | goto finally; |
| 331 | |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 332 | free(server); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 333 | return list->maps; |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 334 | |
| 335 | finally: |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 336 | free(server); |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 337 | return NULL; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 340 | static PyObject * |
Peter Schneider-Kamp | 39e0e5a | 2000-07-10 13:12:27 +0000 | [diff] [blame] | 341 | nis_maps (PyObject *self, PyObject *args) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 342 | { |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 343 | nismaplist *maps; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 344 | PyObject *list; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 345 | |
Barry Warsaw | 3696c52 | 1996-12-11 00:29:14 +0000 | [diff] [blame] | 346 | if (!PyArg_NoArgs(args)) |
| 347 | return NULL; |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 348 | if ((maps = nis_maplist ()) == NULL) |
| 349 | return NULL; |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 350 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 351 | return NULL; |
Guido van Rossum | 61b705a | 2000-02-29 15:52:40 +0000 | [diff] [blame] | 352 | for (maps = maps; maps; maps = maps->next) { |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 353 | PyObject *str = PyString_FromString(maps->map); |
| 354 | if (!str || PyList_Append(list, str) < 0) |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 355 | { |
| 356 | Py_DECREF(list); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 357 | list = NULL; |
| 358 | break; |
| 359 | } |
Barry Warsaw | 4bc9d39 | 1997-01-09 22:22:05 +0000 | [diff] [blame] | 360 | Py_DECREF(str); |
Guido van Rossum | 3562d52 | 1992-08-12 15:26:16 +0000 | [diff] [blame] | 361 | } |
| 362 | /* XXX Shouldn't we free the list of maps now? */ |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 363 | return list; |
| 364 | } |
| 365 | |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 366 | static PyMethodDef nis_methods[] = { |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 367 | {"match", nis_match}, |
| 368 | {"cat", nis_cat}, |
| 369 | {"maps", nis_maps}, |
| 370 | {NULL, NULL} /* Sentinel */ |
| 371 | }; |
| 372 | |
| 373 | void |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 374 | initnis (void) |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 375 | { |
Barry Warsaw | adbf4e6 | 1996-12-11 00:15:58 +0000 | [diff] [blame] | 376 | PyObject *m, *d; |
| 377 | m = Py_InitModule("nis", nis_methods); |
| 378 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 379 | NisError = PyErr_NewException("nis.error", NULL, NULL); |
| 380 | if (NisError != NULL) |
| 381 | PyDict_SetItemString(d, "error", NisError); |
Guido van Rossum | 9de7a01 | 1992-08-12 14:57:12 +0000 | [diff] [blame] | 382 | } |