Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 1 | /* cryptmodule.c - by Steve Majewski |
| 2 | */ |
| 3 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 5 | |
| 6 | #include <sys/types.h> |
Miss Islington (bot) | 80285ec | 2021-09-29 16:02:11 -0700 | [diff] [blame^] | 7 | #ifdef HAVE_CRYPT_H |
| 8 | #include <crypt.h> |
| 9 | #endif |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 11 | /* Module crypt */ |
| 12 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 13 | /*[clinic input] |
| 14 | module crypt |
| 15 | [clinic start generated code]*/ |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 16 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6252cf4f2f2ae81]*/ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 17 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 18 | #include "clinic/_cryptmodule.c.h" |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 19 | |
| 20 | /*[clinic input] |
| 21 | crypt.crypt |
| 22 | |
Serhiy Storchaka | 7e810a6 | 2015-05-30 11:09:35 +0300 | [diff] [blame] | 23 | word: str |
| 24 | salt: str |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 25 | / |
| 26 | |
| 27 | Hash a *word* with the given *salt* and return the hashed password. |
| 28 | |
| 29 | *word* will usually be a user's password. *salt* (either a random 2 or 16 |
| 30 | character string, possibly prefixed with $digit$ to indicate the method) |
| 31 | will be used to perturb the encryption algorithm and produce distinct |
| 32 | results for a given *word*. |
| 33 | |
| 34 | [clinic start generated code]*/ |
| 35 | |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 36 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 37 | crypt_crypt_impl(PyObject *module, const char *word, const char *salt) |
| 38 | /*[clinic end generated code: output=0512284a03d2803c input=0e8edec9c364352b]*/ |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 39 | { |
Gregory P. Smith | 387512c | 2018-12-30 15:42:32 -0800 | [diff] [blame] | 40 | char *crypt_result; |
| 41 | #ifdef HAVE_CRYPT_R |
| 42 | struct crypt_data data; |
| 43 | memset(&data, 0, sizeof(data)); |
| 44 | crypt_result = crypt_r(word, salt, &data); |
| 45 | #else |
| 46 | crypt_result = crypt(word, salt); |
| 47 | #endif |
Antonio Gutierrez | 0d3fe8a | 2019-10-08 06:22:17 +0200 | [diff] [blame] | 48 | if (crypt_result == NULL) { |
| 49 | return PyErr_SetFromErrno(PyExc_OSError); |
| 50 | } |
Gregory P. Smith | 387512c | 2018-12-30 15:42:32 -0800 | [diff] [blame] | 51 | return Py_BuildValue("s", crypt_result); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Fred Drake | a664dbb | 2000-02-01 20:12:39 +0000 | [diff] [blame] | 54 | |
Roger E. Masse | 56c345b | 1996-12-09 23:14:26 +0000 | [diff] [blame] | 55 | static PyMethodDef crypt_methods[] = { |
Antoine Pitrou | f5207e6 | 2014-01-14 21:00:27 +0100 | [diff] [blame] | 56 | CRYPT_CRYPT_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 60 | static PyModuleDef_Slot _crypt_slots[] = { |
| 61 | {0, NULL} |
| 62 | }; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 63 | |
| 64 | static struct PyModuleDef cryptmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | PyModuleDef_HEAD_INIT, |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 66 | "_crypt", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | NULL, |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 68 | 0, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | crypt_methods, |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 70 | _crypt_slots, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | NULL, |
| 72 | NULL, |
| 73 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 76 | PyMODINIT_FUNC |
Sean Reifscheider | e2dfefb | 2011-02-22 10:55:44 +0000 | [diff] [blame] | 77 | PyInit__crypt(void) |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 78 | { |
Hai Shi | b2b6e27 | 2020-02-17 17:11:34 +0800 | [diff] [blame] | 79 | return PyModuleDef_Init(&cryptmodule); |
Guido van Rossum | e4c6131 | 1994-05-06 14:25:39 +0000 | [diff] [blame] | 80 | } |