summaryrefslogtreecommitdiffstats
path: root/docs/sudo_plugin_python.man.in
blob: 9410271eebcfdd56053c9967796a19869993ac09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
187
188
189
190
191
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
376
377
378
379
380
381
382
383
384
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
.\" Automatically generated from an mdoc input file.  Do not edit.
.\"
.\" SPDX-License-Identifier: ISC
.\"
.\" Copyright (c) 2019-2021 Robert Manner <robert.manner@oneidentity.com>
.\" Copyright (c) 2019-2023 Todd C. Miller <Todd.Miller@sudo.ws>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.TH "SUDO_PLUGIN_PYTHON" "5" "January 16, 2023" "Sudo @PACKAGE_VERSION@" "File Formats Manual"
.nh
.if n .ad l
.SH "NAME"
\fBsudo_plugin_python\fR
\- Sudo Plugin API (Python)
.SH "DESCRIPTION"
Starting with version 1.9,
\fBsudo\fR
plugins can be written in python.
The API closely follows the C
\fBsudo\fR
plugin API described by
sudo_plugin(@mansectform@).
.PP
The supported plugins types are:
.PP
.RS 1n
.PD 0
.TP 3n
\fB\(bu\fR
Policy plugin
.TP 3n
\fB\(bu\fR
I/O plugin
.TP 3n
\fB\(bu\fR
Audit plugin
.TP 3n
\fB\(bu\fR
Approval plugin
.TP 3n
\fB\(bu\fR
Group provider plugin
.RE
.PD
.PP
Python plugin support needs to be explicitly enabled at build time
with the configure option
\(lq--enable-python\(rq.
Python version 3.0 or higher is required.
.SS "Sudo Python Plugin Base"
A plugin written in Python should be a class in a python file that
inherits from
\fIsudo.Plugin\fR.
The
\fIsudo.Plugin\fR
base class has no real purpose other than to identify this class as a plugin.
.PP
The only implemented method is a constructor, which stores the
keyword arguments it receives as fields (member variables) in the object.
This is intended as a convenience to allow you to avoid writing the
constructor yourself.
.PP
For example:
.nf
.sp
.RS 4n
import sudo

class MySudoPlugin(sudo.Plugin):
    # example constructor (optional)
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    # example destructor (optional)
    def __del__(self):
        pass
.RE
.fi
.PP
Both the constructor and destructor are optional and can be omitted.
.PP
The customized Plugin class should define a few plugin-specific methods.
When the plugin loads,
\fBsudo\fR
will create an instance of this class and call the methods.
The actual methods required depent on the type of the plugin,
but most return an
\fIint\fR
result code, as documented in
sudo_plugin(@mansectform@),
that indicates whether or not the method was successful.
The Python sudo module defines the following constants to improve readability:
.RS 4n
.TS
l l.
.PP
\fBDefine\fR	\fBValue\fR
.PP
\fRsudo.RC.OK\fR	1
.PP
\fRsudo.RC.ACCEPT\fR	1
.PP
\fRsudo.RC.REJECT\fR	0
.PP
\fRsudo.RC.ERROR\fR	\-1
.PP
\fRsudo.RC.USAGE_ERROR\fR	\-2
.TE
.RE
.PP
If a function returns
\fRNone\fR
(for example, if it does not call return),
it will be considered to have returned
\fRsudo.RC.OK\fR.
If an exception is raised (other than sudo.PluginException), the
backtrace will be shown to the user and the plugin function will return
\fRsudo.RC.ERROR\fR.
If that is not acceptable, you must catch the exception and handle it yourself.
.PP
Instead of just returning
\fRsudo.RC.ERROR\fR
or
\fRsudo.RC.REJECT\fR
result code the plugin can also provide a message describing the problem.
This can be done by raising one of the special exceptions:
.nf
.sp
.RS 4n
raise sudo.PluginError("Message")
raise sudo.PluginReject("Message")
.RE
.fi
.PP
This added message will be used by the audit plugins.
Both exceptions inherit from
\fRsudo.PluginException\fR
.SS "Python Plugin Loader"
Running the Python interpreter and bridging between C and Python is
handled by the
\fBsudo\fR
plugin
\fI@python_plugin@\fR.
This shared object can be loaded like any other dynamic
\fBsudo\fR
plugin and should receive the path and the class name of the Python
plugin it is loading as arguments.
.PP
Example usage in
sudo.conf(@mansectform@):
.nf
.sp
.RS 4n
Plugin python_policy @python_plugin@ ModulePath=<path> ClassName=<class>
Plugin python_io @python_plugin@ ModulePath=<path> ClassName=<class>
Plugin python_audit @python_plugin@ ModulePath=<path> ClassName=<class>
Plugin python_approval @python_plugin@ ModulePath=<path> ClassName=<class>
.RE
.fi
.PP
Example group provider plugin usage in the
\fIsudoers\fR
file:
.nf
.sp
.RS 4n
Defaults group_plugin="@python_plugin@ ModulePath=<path> ClassName=<class>"
.RE
.fi
.PP
The plugin arguments are as follows:
.TP 6n
ModulePath
The path of a python file which contains the class of the sudo Python plugin.
It must be either an absolute path or a path relative to the sudo Python plugin
directory,
\fI@plugindir@/python\fR.
The parent directory of
\fIModulePath\fR
will be appended to Python's module search path (there is currently no
way to force Python to load a module from a fully-qualified path).
It is good practice to use a prefix for the module file that is unlikely
to conflict with other installed Python modules, for example,
\fIsudo_policy.py\fR.
Otherwise, if the there is an installed Python module with the same
file name as the sudo Python plugin file (without the directory),
the wrong file will be loaded.
.TP 6n
ClassName
(Optional.) The name of the class implementing the sudo Python plugin.
If not supplied, the one and only sudo.Plugin that is present in the module
will be used.
If there are multiple such plugins in the module (or none), it
will result in an error.
.SS "Policy plugin API"
Policy plugins must be registered in
sudo.conf(@mansectform@).
For example:
.nf
.sp
.RS 4n
Plugin python_policy @python_plugin@ ModulePath=<path> ClassName=<class>
.RE
.fi
.PP
Currently, only a single policy plugin may be specified in
sudo.conf(@mansectform@).
.PP
A policy plugin may have the following member functions:
.TP 6n
\fIconstructor\fR
.nf
.RS 6n
__init__(self, user_env: Tuple[str, ...], settings: Tuple[str, ...],
    version: str, user_info: Tuple[str, ...],
    plugin_options: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
Implementing this function is optional.
The default constructor will set the keyword arguments it receives
as member variables in the object.
.sp
The constructor matches the
\fBopen\fR()
function in the C
\fBsudo\fR
plugin API.
.sp
The function arguments are as follows:
.TP 6n
\fIuser_env\fR
The user's environment as a tuple of strings in
\(lqkey=value\(rq
format.
.TP 6n
\fIsettings\fR
A tuple of user-supplied
\fIsudo\fR
settings in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIversion\fR
The version of the Python Policy Plugin API.
.TP 6n
\fIuser_info\fR
A tuple of information about the user running the command in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIplugin_options\fR
The plugin options passed as arguments in the
sudo.conf(@mansectform@)
plugin registration.
This is a tuple of strings, usually (but not necessarily) in
\(lqkey=value\(rq
format.
.PP
The
\fBsudo.options_as_dict\fR()
convenience function can be used to convert
\(lqkey=value\(rq
pairs to a dictionary.
For a list of recognized keys and their supported values,
see the policy plugin
\fBopen\fR()
documentation in
sudo_plugin(@mansectform@).
.RE
.TP 6n
\fIcheck_policy\fR
.nf
.RS 6n
check_policy(self, argv: Tuple[str, ...], env_add: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
The
\fBcheck_policy\fR()
function is called by
\fBsudo\fR
to determine whether the user is allowed to run the specified command.
Implementing this function is mandatory for a policy plugin.
.sp
The function arguments are as follows:
.TP 6n
\fIargv\fR
A tuple describing the command the user wishes to run.
.TP 6n
\fIenv_add\fR
Additional environment variables specified by the user on the command line in
the form of a tuple of
\(lqkey=value\(rq
pairs.
The
\fBsudo.options_as_dict\fR()
convenience function can be used to convert them to a dictionary.
.PP
This function should return a result code or a tuple in the following format:
.nf
.sp
.RS 10n
return (rc, command_info_out, argv_out, user_env_out)
.RE
.fi
.sp
The tuple values are as follows:
.TP 6n
\fIrc\fR
The result of the policy check, one of the
\fRsudo.RC.*\fR
constants.
\fRsudo.RC.ACCEPT\fR
if the command is allowed,
\fRsudo.RC.REJECT\fR
if not allowed,
\fRsudo.RC.ERROR\fR
for a general error, or
\fRsudo.RC.USAGE_ERROR\fR
for a usage error.
.TP 6n
\fIcommand_info_out\fR
Optional (only required when the command is accepted).
Information about the command being run in the form of
\(lqkey=value\(rq
strings.
.sp
To accept a command, at the very minimum the plugin must set in the
\fIcommand\fR,
\fIrunas_uid\fR,
and
\fIrunas_gid\fR
keys.
.sp
For a list of recognized keys and supported values,
see the
\fBcheck_policy\fR()
documentation in
sudo_plugin(@mansectform@).
.TP 6n
\fIargv_out\fR
Optional (only required when the command is accepted).
The arguments to pass to the
execve(2)
system call when executing the command.
.TP 6n
\fIuser_env_out\fR
Optional (only required when the command is accepted).
The environment to use when executing the command in the form of a
tuple of strings in
\(lqkey=value\(rq
format.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIinit_session\fR
.nf
.RS 6n
init_session(self, user_pwd: Tuple, user_env: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
Perform session setup (optional).
The
\fBinit_session\fR()
function is called before
\fBsudo\fR
sets up the
execution environment for the command before any user-ID or group-ID changes.
.sp
The function arguments are as follows:
.TP 6n
\fIuser_pwd\fR
A tuple describing the user's passwd entry.
Convertible to
\fIpwd.struct_passwd or\fR
\fRNone\fR
if the user is not present in the password database.
.sp
Example conversion:
.nf
.RS 12n
user_pwd = pwd.struct_passwd(user_pwd) if user_pwd else None
.RE
.fi
.TP 6n
\fIuser_env\fR
The environment the command will run in.
This is a tuple of strings in
\(lqkey=value\(rq
format.
.PP
This function should return a result code or a tuple in the following format:
.nf
.sp
.RS 10n
return (rc, user_env_out)
.RE
.fi
.sp
The tuple values are as follows:
.TP 6n
\fIrc\fR
The result of the session init, one of the
\fRsudo.RC.*\fR
constants.
\fRsudo.RC.OK\fR
on success, 0 on failure, or
\fRsudo.RC.ERROR\fR
if an error occurred.
.TP 6n
\fIuser_env_out\fR
Optional.
If the
\fBinit_session\fR()
function needs to modify the user environment, it can return the new
environment in
\fIuser_env_out\fR.
If this is omitted, no changes will be made to
\fIuser_env\fR.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIlist\fR
.nf
.RS 6n
list(self, argv: Tuple[str, ...], is_verbose: int, user: str)
.RE
.fi
.RS 6n
.sp
List available privileges for the invoking user.
.sp
The function arguments are as follows:
.TP 6n
\fIargv\fR
If not set to
\fRNone\fR,
an argument vector describing a command the user wishes to check
against the policy.
.TP 6n
\fIis_verbose\fR
Flag indicating whether to list in verbose mode or not.
.TP 6n
\fIuser\fR
The name of a different user to list privileges for if the policy allows it.
If
\fRNone\fR,
the plugin should list the privileges of the invoking user.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIvalidate\fR
.nf
.RS 6n
validate(self)
.RE
.fi
.RS 6n
.sp
For policy plugins that cache authentication credentials, this function is used to validate and cache the credentials (optional).
.RE
.TP 6n
\fIinvalidate\fR
.nf
.RS 6n
invalidate(self, remove: int)
.RE
.fi
.RS 6n
.sp
For policy plugins that cache authentication credentials, this function is used to invalidate the credentials (optional).
.sp
The function arguments are as follows:
.TP 6n
\fIremove\fR
If this flag is set, the plugin may remove the credentials instead of simply
invalidating them.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIshow_version\fR
.nf
.RS 6n
show_version(self, is_verbose: int)
.RE
.fi
.RS 6n
.sp
Display the plugin version information to the user.
The
\fBsudo.log_info\fR()
function should be used.
.sp
The function arguments are as follows:
.TP 6n
\fIis_verbose\fR
A flag to indicate displaying more verbose information.
Currently this is 1 if
\(oqsudo -V\(cq
is run as the root user.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIclose\fR
.br
.nf
.RS 6n
close(self, exit_status: int, error: int)
.RE
.fi
.RS 6n
.sp
Called when a command finishes executing.
.sp
Works the same as the
\fBclose\fR()
function in the C
\fBsudo\fR
plugin API, except that it only gets called if
\fBsudo\fR
attempts to execute the command.
.sp
The function arguments are as follows:
.TP 6n
\fIexit_status\fR
The exit status of the command if was executed, otherwise \-1.
.TP 6n
\fIerror\fR
.br
If the command could not be executed, this is set to the value of
errno set by the
execve(2)
system call, otherwise 0.
.PD 0
.PP
.RE
.PD
.SS "Policy plugin example"
Sudo ships with an example Python policy plugin.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 0n
Plugin python_policy @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_policy_plugin.py \e
    ClassName=SudoPolicyPlugin
.RE
.fi
.PP
Only one policy plugin can be enabled at a time so you must disable
any other policy plugin listed in
\fI@sysconfdir@/sudo.conf\fR,
such as
sudoers(@mansectform@).
.SS "I/O plugin API"
I/O plugins must be registered in
sudo.conf(@mansectform@).
For example:
.nf
.sp
.RS 4n
Plugin python_io @python_plugin@ ModulePath=<path> ClassName=<class>
.RE
.fi
.PP
Sudo supports loading multiple I/O plugins.
Currently only 8 python I/O plugins can be loaded at once.
.PP
An I/O plugin may have the following member functions:
.TP 6n
\fIconstructor\fR
.nf
.RS 6n
__init__(self, user_env: Tuple[str, ...], settings: Tuple[str, ...],
    version: str, user_info: Tuple[str, ...],
    plugin_options: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
Implementing this function is optional.
The default constructor will set the keyword arguments it receives
as member variables in the object.
.sp
The constructor matches the
\fBopen\fR()
function in the C
\fBsudo\fR
plugin API.
.sp
The function arguments are as follows:
.TP 6n
\fIuser_env\fR
The user's environment as a tuple of strings in
\(lqkey=value\(rq
format.
.TP 6n
\fIsettings\fR
A tuple of user-supplied
\fIsudo\fR
settings in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIversion\fR
The version of the Python I/O Plugin API.
.TP 6n
\fIuser_info\fR
A tuple of information about the user running the command in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIplugin_options\fR
The plugin options passed as arguments in the
sudo.conf(@mansectform@)
plugin registration.
This is a tuple of strings, usually (but not necessarily) in
\(lqkey=value\(rq
format.
.PP
The
\fBsudo.options_as_dict\fR()
convenience function can be used to convert
\(lqkey=value\(rq
pairs to a dictionary.
For a list of recognized keys and their supported values,
see the I/O plugin
\fBopen\fR()
documentation in
sudo_plugin(@mansectform@).
.RE
.TP 6n
\fIopen\fR
.nf
.RS 6n
open(self, argv: Tuple[str, ...],
    command_info: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
Receives the command the user wishes to run.
.sp
Works the same as the
\fBopen\fR()
function in the C
\fBsudo\fR
plugin API except that:
.sp
.RS 7n
.PD 0
.TP 3n
\fB\(bu\fR
It only gets called when there is a command to be executed
(and not for a version query for example).
.TP 3n
\fB\(bu\fR
Other arguments of the C API
\fBopen\fR()
function are received through the constructor.
.RE
.sp
The function arguments are as follows:
.PD
.TP 6n
\fIargv\fR
A tuple of the arguments describing the command the user wishes to run.
.TP 6n
\fIcommand_info\fR
Information about the command being run in the form of
\(lqkey=value\(rq
strings.
.PP
The
\fBsudo.options_as_dict\fR()
convenience function can be used to convert
\(lqkey=value\(rq
pairs to a dictionary.
For a list of recognized keys and their supported values,
see the I/O plugin
\fBopen\fR()
documentation in
sudo_plugin(@mansectform@).
.sp
The
\fBopen\fR()
function should return a result code, one of the
\fRsudo.RC.*\fR
constants.
If the function returns
\fRsudo.RC.REJECT\fR,
no I/O will be sent to the plugin.
.RE
.TP 6n
\fIlog_ttyin\fR, \fIlog_ttyout\fR, \fIlog_stdin\fR, \fIlog_stdout\fR, \fIlog_stderr\fR
.nf
.RS 6n
log_ttyin(self, buf: str) -> int
log_ttyout(self, buf: str) -> int
log_stdin(self, buf: str) -> int
log_stdout(self, buf: str) -> int
log_stderr(self, buf: str) -> int
.RE
.fi
.RS 6n
.sp
Receive the user input or output of the terminal device and
application standard input, standard output, or standard error.
See the matching calls in
sudo_plugin(@mansectform@).
.sp
The function arguments are as follows:
.TP 6n
\fIbuf\fR
The input (or output) buffer in the form of a string.
.PP
The function should return a result code, one of the
\fRsudo.RC.*\fR
constants.
.sp
If
\fRsudo.RC.ERROR\fR
is returned, the running command will be terminated and all of the
plugin's logging functions will be disabled.
Other I/O logging plugins will still receive any remaining
input or output that has not yet been processed.
.sp
If an input logging function rejects the data by returning
\fRsudo.RC.REJECT\fR,
the command will be terminated and the data will not be passed to the
command, though it will still be sent to any other I/O logging plugins.
If an output logging function rejects the data by returning
\fRsudo.RC.REJECT\fR,
the command will be terminated and the data will not be written to the
terminal, though it will still be sent to any other I/O logging plugins.
.RE
.TP 6n
\fIchange_winsize\fR
.nf
.RS 6n
change_winsize(self, line: int, cols: int) -> int
.RE
.fi
.RS 6n
.sp
Called whenever the window size of the terminal changes.
The function arguments are as follows:
.TP 6n
\fIline\fR
The number of lines of the terminal.
.TP 6n
\fIcols\fR
The number of columns of the terminal.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIlog_suspend\fR
.nf
.RS 6n
log_suspend(self, signo: int) -> int
.RE
.fi
.RS 6n
Called whenever a command is suspended or resumed.
.sp
The function arguments are as follows:
.TP 6n
\fIsigno\fR
.br
The number of the signal that caused the command to be suspended or
\fRSIGCONT\fR
if the command was resumed.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIshow_version\fR
.nf
.RS 6n
show_version(self, is_verbose: int)
.RE
.fi
.RS 6n
Display the plugin version information to the user.
The
\fBsudo.log_info\fR()
function should be used.
.sp
The function arguments are as follows:
.TP 6n
\fIis_verbose\fR
A flag to indicate displaying more verbose information.
Currently this is 1 if
\(oqsudo -V\(cq
is run as the root user.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIclose\fR
.br
.nf
.RS 6n
close(self, exit_status: int, error: int) -> None
.RE
.fi
.RS 6n
Called when a command finishes execution.
.sp
Works the same as the
\fBclose\fR()
function in the C
\fBsudo\fR
plugin API, except that it only gets called if
\fBsudo\fR
attempts to execute the command.
.sp
The function arguments are as follows:
.TP 6n
\fIexit_status\fR
The exit status of the command if was executed, otherwise \-1.
.TP 6n
\fIerror\fR
.br
If the command could not be executed, this is set to the value of
errno set by the
execve(2)
system call, otherwise 0.
.PD 0
.PP
.RE
.PD
.SS "I/O plugin example"
Sudo ships with a Python I/O plugin example.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 4n
Plugin python_io @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_io_plugin.py \e
    ClassName=SudoIOPlugin
.RE
.fi
.SS "Audit plugin API"
Audit plugins must be registered in
sudo.conf(@mansectform@).
For example:
.nf
.sp
.RS 4n
Plugin python_audit @python_plugin@ ModulePath=<path> ClassName=<class>
.RE
.fi
.PP
Sudo supports loading multiple audit plugins.
Currently only 8 python audit plugins can be loaded at once.
.PP
An audit plugin may have the following member functions (all of which are optional):
.TP 6n
\fIconstructor\fR
.nf
.RS 6n
__init__(self, user_env: Tuple[str, ...], settings: Tuple[str, ...],
    version: str, user_info: Tuple[str, ...], plugin_options: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
The default constructor will set the keyword arguments it receives
as member variables in the object.
.sp
The constructor matches the
\fBopen\fR()
function in the C
\fBsudo\fR
plugin API.
.sp
The function arguments are as follows:
.TP 6n
\fIuser_env\fR
The user's environment as a tuple of strings in
\(lqkey=value\(rq
format.
.TP 6n
\fIsettings\fR
A tuple of user-supplied
\fIsudo\fR
settings in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIversion\fR
The version of the Python Audit Plugin API.
.TP 6n
\fIuser_info\fR
A tuple of information about the user running the command in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIplugin_options\fR
The plugin options passed as arguments in the
sudo.conf(@mansectform@)
plugin registration.
This is a tuple of strings, usually (but not necessarily) in
\(lqkey=value\(rq
format.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIopen\fR
.nf
.RS 6n
open(self, submit_optind: int,
    submit_argv: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
The function arguments are as follows:
.TP 6n
\fIsubmit_optind\fR
The index into
\fIsubmit_argv\fR
that corresponds to the first entry that is not a command line option.
.TP 6n
\fIsubmit_argv\fR
The argument vector sudo was invoked with, including all command line options.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIclose\fR
.br
.nf
.RS 6n
close(self, status_type: int, status: int) -> None
.RE
.fi
.RS 6n
.sp
Called when sudo is finished, shortly before it exits.
.sp
The function arguments are as follows:
.TP 6n
\fIstatus_type\fR
The type of status being passed.
One of the
\fRsudo.EXIT_REASON.*\fR
constants.
.TP 6n
\fIstatus\fR
Depending on the value of
\fIstatus_type\fR,
this value is either
ignored, the command's exit status as returned by the
wait(2)
system call, the value of
\fIerrno\fR
set by the
execve(2)
system call, or the value of
\fIerrno\fR
resulting from an error in the
\fBsudo\fR
front-end.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIshow_version\fR
.nf
.RS 6n
show_version(self, is_verbose: int) -> int
.RE
.fi
.RS 6n
.sp
Display the plugin version information to the user.
The
\fBsudo.log_info\fR()
function should be used.
.sp
The function arguments are as follows:
.TP 6n
\fIis_verbose\fR
A flag to indicate displaying more verbose information.
Currently this is 1 if
\(oqsudo -V\(cq
is run as the root user.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIaccept\fR
.nf
.RS 6n
accept(self, plugin_name: str, plugin_type: int, command_info: Tuple[str, ...],
       run_argv: Tuple[str, ...], run_envp: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
This function is called when a command or action is accepted by a policy
or approval plugin.
The function arguments are as follows:
.TP 6n
plugin_name
The name of the plugin that accepted the command or
\(lqsudo\(rq
for the
\fBsudo\fR
front-end.
.TP 6n
plugin_type
The type of plugin that accepted the command, currently either
\fRsudo.PLUGIN_TYPE.POLICY\fR,
\fRsudo.PLUGIN_TYPE.APPROVAL\fR,
or
\fRsudo.PLUGIN_TYPE.SUDO\fR.
The
\fBaccept\fR()
function is called multiple times--once for each policy or approval
plugin that succeeds and once for the sudo front-end.
When called on behalf of the sudo front-end,
\fIcommand_info\fR
may include information from an I/O logging plugin as well.
.sp
Typically, an audit plugin is interested in either the accept status from
the
\fBsudo\fR
front-end or from the various policy and approval plugins, but not both.
It is possible for the policy plugin to accept a command that is
later rejected by an approval plugin, in which case the audit
plugin's
\fBaccept\fR()
and
\fBreject\fR()
functions will
\fIboth\fR
be called.
.TP 6n
command_info
A vector of information describing the command being run.
See the
sudo_plugin(@mansectform@)
manual for possible values.
.TP 6n
run_argv
Argument vector describing a command that will be run.
.TP 6n
run_envp
The environment the command will be run with.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIreject\fR
.nf
.RS 6n
reject(self, plugin_name: str, plugin_type: int, audit_msg: str,
       command_info: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
This function is called when a command or action is rejected by the policy
plugin.
The function arguments are as follows:
.TP 6n
plugin_name
The name of the plugin that rejected the command.
.TP 6n
plugin_type
The type of plugin that rejected the command, currently either
\fRsudo.PLUGIN_TYPE.POLICY\fR,
\fRsudo.PLUGIN_TYPE.APPROVAL\fR,
or
\fRsudo.PLUGIN_TYPE.IO\fR.
.sp
Unlike the
\fBaccept\fR()
function, the
\fBreject\fR()
function is not called on behalf of the
\fBsudo\fR
front-end.
.TP 6n
audit_msg
An optional string describing the reason the command was rejected by the plugin.
If the plugin did not provide a reason, audit_msg will be
\fRNone\fR.
.TP 6n
command_info
A vector of information describing the rejected command.
See the
sudo_plugin(@mansectform@)
manual for possible values.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIerror\fR
.br
.nf
.RS 6n
error(self, plugin_name: str, plugin_type: int, audit_msg: str,
      command_info: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
This function is called when a plugin or the
\fBsudo\fR
front-end returns an error.
The function arguments are as follows:
.TP 6n
plugin_name
The name of the plugin that generated the error or
\(lqsudo\(rq
for the
\fBsudo\fR
front-end.
.TP 6n
plugin_type
The type of plugin that generated the error, or
\fRSUDO_FRONT_END\fR
for the
\fBsudo\fR
front-end.
.TP 6n
audit_msg
An optional string describing the plugin error.
If the plugin did not provide a description, it will be
\fRNone\fR.
.TP 6n
command_info
A vector of information describing the command.
See the
sudo_plugin(@mansectform@)
manual for possible values.
.PD 0
.PP
.RE
.PD
.SS "Audit plugin example"
Sudo ships with a Python Audit plugin example.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 4n
Plugin python_audit @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_audit_plugin.py \e
    ClassName=SudoAuditPlugin
.RE
.fi
.PP
It will log the plugin accept / reject / error results to the output.
.SS "Approval plugin API"
Approval plugins must be registered in
sudo.conf(@mansectform@).
For example:
.nf
.sp
.RS 4n
Plugin python_approval @python_plugin@ ModulePath=<path> ClassName=<class>
.RE
.fi
.PP
Sudo supports loading multiple approval plugins.
Currently only 8 python approval plugins can be loaded at once.
.PP
An approval plugin may have the following member functions:
.TP 6n
\fIconstructor\fR
.nf
.RS 6n
__init__(self, user_env: Tuple[str, ...], settings: Tuple[str, ...],
    version: str, user_info: Tuple[str, ...], plugin_options: Tuple[str, ...],
    submit_optind: int, submit_argv: Tuple[str, ...])
.RE
.fi
.RS 6n
.sp
Optional.
The default constructor will set the keyword arguments it receives
as member variables in the object.
.sp
The constructor matches the
\fBopen\fR()
function in the C
\fBsudo\fR
plugin API.
.sp
The function arguments are as follows:
.TP 6n
\fIuser_env\fR
The user's environment as a tuple of strings in
\(lqkey=value\(rq
format.
.TP 6n
\fIsettings\fR
A tuple of user-supplied
\fIsudo\fR
settings in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIversion\fR
The version of the Python Approval Plugin API.
.TP 6n
\fIuser_info\fR
A tuple of information about the user running the command in the form of
\(lqkey=value\(rq
strings.
.TP 6n
\fIplugin_options\fR
The plugin options passed as arguments in the
sudo.conf(@mansectform@)
plugin registration.
This is a tuple of strings, usually (but not necessarily) in
\(lqkey=value\(rq
format.
.TP 6n
\fIsubmit_optind\fR
The index into
\fIsubmit_argv\fR
that corresponds to the first entry that is not a command line option.
.TP 6n
\fIsubmit_argv\fR
The argument vector sudo was invoked with, including all command line options.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIshow_version\fR
.nf
.RS 6n
show_version(self, is_verbose: int) -> int
.RE
.fi
.RS 6n
.sp
Display the version.
(Same as for all the other plugins.)
.RE
.TP 6n
\fIcheck\fR
.br
.nf
.RS 6n
check(self, command_info: Tuple[str, ...], run_argv: Tuple[str, ...],
      run_env: Tuple[str, ...]) -> int
.RE
.fi
.RS 6n
.sp
This function is called after policy plugin's check_policy has succeeded.
It can reject execution of the command by returning sudo.RC.REJECT or
raising the special exception:
.nf
.sp
.RS 10n
raise sudo.PluginReject("some message")
.RE
.fi
.sp
with the message describing the problem.
In the latter case, the audit plugins will get the description.
.sp
The function arguments are as follows:
.TP 6n
command_info
A vector of information describing the command that will run.
See the
sudo_plugin(@mansectform@)
manual for possible values.
.TP 6n
run_argv
Argument vector describing a command that will be run.
.TP 6n
run_env
The environment the command will be run with.
.PD 0
.PP
.RE
.PD
.SS "Approval plugin example"
Sudo ships with a Python Approval plugin example.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 4n
Plugin python_approval @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_approval_plugin.py \e
    ClassName=BusinessHoursApprovalPlugin
.RE
.fi
.PP
It will only allow execution of commands in the "business hours" (from Monday
to Friday between 8:00 and 17:59:59).
.SS "Sudoers group provider plugin API"
A group provider plugin is registered in the
sudoers(@mansectform@)
file.
For example:
.nf
.sp
.RS 4n
Defaults group_plugin="@python_plugin@ ModulePath=<path> ClassName=<class>"
.RE
.fi
.PP
Currently, only a single group plugin can be registered in
\fIsudoers\fR.
.PP
A group provider plugin may have the following member functions:
.TP 6n
\fIconstructor\fR
.nf
.RS 6n
__init__(self, args: Tuple[str, ...], version: str)
.RE
.fi
.RS 6n
.sp
Implementing this function is optional.
The default constructor will set the keyword arguments it receives
as member variables in the object.
.sp
The function arguments are as follows:
.TP 6n
\fIargs\fR
The plugin options passed as arguments in the
\fIsudoers\fR
file plugin registration.
All the arguments are free form strings (not necessarily in
\(lqkey=value\(rq
format).
.TP 6n
\fIversion\fR
The version of the Python Group Plugin API.
.PD 0
.PP
.RE
.PD
.TP 6n
\fIquery\fR
.br
.nf
.RS 6n
query(self, user: str, group: str, user_pwd: Tuple)
.RE
.fi
.RS 6n
.sp
The
\fBquery\fR()
function is used to ask the group plugin whether
\fIuser\fR
is a member of
\fIgroup\fR.
This method is required.
.RE
.PP
The function arguments are as follows:
.TP 6n
\fIuser\fR
The name of the user being looked up in the external group database.
.TP 6n
\fIgroup\fR
.br
The name of the group being queried.
.TP 6n
\fIuser_pwd\fR
The password database entry for the user, if any.
If
\fIuser\fR
is not present in the password database,
\fIuser_pwd\fR
will be
\fRNULL\fR.
.SS "Group plugin example"
Sudo ships with a Python group plugin example.
To try it, register it in the
\fIsudoers\fR
file by adding the following lines:
.nf
.sp
.RS 4n
Defaults group_plugin="@python_plugin@ \e
    ModulePath=@EXAMPLES@/example_group_plugin.py \e
    ClassName=SudoGroupPlugin"
.RE
.fi
.PP
The example plugin will tell
\fBsudo\fR
that the user
\fItest\fR
is part of the non-Unix group
\fImygroup\fR.
If you add a rule that uses this group, it will affect the
\fItest\fR
user.
For example:
.nf
.sp
.RS 4n
%:mygroup ALL=(ALL) NOPASSWD: ALL
.RE
.fi
.PP
Will allow user
\fItest\fR
to run
\fBsudo\fR
without a password.
.SS "Hook function API"
The hook function API is currently not supported for plugins
written in Python.
.SS "Conversation API"
A Python plugin can interact with the user using the
\fBsudo.conv\fR()
function which displays one or more messages described by the
\fRsudo.ConvMessage\fR
class.
This is the Python equivalent of the
\fBconversation\fR()
function in the C
\fBsudo\fR
plugin API.
A plugin should not attempt to read directly from the standard input or
the user's tty (neither of which are guaranteed to exist).
.PP
The
\fRsudo.ConvMessage\fR
class specifies how the user interaction should occur:
.nf
.sp
.RS 4n
sudo.ConvMessage(msg_type: int, msg: str, timeout: int)
.RE
.fi
.PP
\fRsudo.ConvMessage\fR
member variables:
.TP 6n
\fImsg_type\fR
Specifies the type of the conversation.
See the
\fRsudo.CONV.*\fR
constants below.
.TP 6n
\fImsg\fR
The message to display to the user.
The caller must include a trailing newline in
\fImsg\fR
if one is to be displayed.
.TP 6n
\fItimeout\fR
Optional.
The maximum amount of time for the conversation in seconds.
If the timeout is exceeded, the
\fBsudo.conv\fR()
function will raise a
\fRsudo.ConversationInterrupted\fR
exception.
The default is to wait forever (no timeout).
.PP
To specify the message type, the following constants are available:
.PP
.RS 1n
.PD 0
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.PROMPT_ECHO_OFF\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.PROMPT_ECHO_ON\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.ERROR_MSG\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.INFO_MSG\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.PROMPT_MASK\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.PROMPT_ECHO_OK\fR
.TP 3n
\fB\(bu\fR
\fRsudo.CONV.PREFER_TTY\fR
.RE
.PD
.PP
See the
sudo_plugin(@mansectform@)
manual for a description of the message types.
.PP
The
\fBsudo.conv\fR()
function performs the actual user interaction:
.nf
.sp
.RS 4n
sudo.conv(message(s), on_suspend=suspend_function,
    on_resume=resume_function)
.RE
.fi
.PP
The function arguments are as follows:
.TP 6n
\fImessage(s)\fR
One of more messages (of type
\fRsudo.ConvMessage\fR),
each describing a conversation.
At least one message is required.
.TP 6n
\fIon_suspend\fR
An optional callback function which gets called if the conversation
is suspended, for example by the user pressing control-Z.
The specified function must take a single argument which will be filled
with the number of the signal that caused the process to be suspended.
.TP 6n
\fIon_resume\fR
An optional callback function which gets called when the previously
suspended conversation is resumed.
The specified function must take a single argument which will be filled
with the number of the signal that caused the process to be suspended.
.PP
The
\fBsudo.conv\fR()
function can raise the following exceptions:
.TP 6n
\fRsudo.SudoException\fR
If the conversation fails, for example when the conversation function is not
available.
.TP 6n
\fRsudo.ConversationInterrupted\fR
If the conversation function returns an error, e.g., the timeout passed
or the user interrupted the conversation by pressing control-C.
.SS "Conversation example"
Sudo ships with an example plugin demonstrating the Python conversation API.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 4n
Plugin python_io @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_conversation.py \e
    ClassName=ReasonLoggerIOPlugin
.RE
.fi
.SS "Information / error display API"
.nf
.RS 0n
sudo.log_info(string(s), sep=" ", end="\en")
sudo.log_error(string(s), sep=" ", end="\en")
.RE
.fi
.PP
To display information to the user, the
\fBsudo.log_info\fR()
function can be used.
To display error messages, use
\fBsudo.log_error\fR().
The syntax is similar to the Python
\fBprint\fR()
function.
.PP
The function arguments are as follows:
.TP 6n
\fIstring(s)\fR
One or more strings to display.
.TP 6n
\fIsep\fR
An optional string which will be used as the separator between the
specified strings.
The default is a space character,
(\(oq\ \(cq).
.TP 6n
\fIend\fR
An optional string which will be displayed at the end of the message.
The default is a new line character
(\(oq\en\(cq).
.SS "Debug API"
Debug messages are not visible to the user and are only logged debugging
is explicitly enabled in
sudo.conf(@mansectform@).
Python plugins can use the
\fBsudo.debug\fR()
function to make use of
\fBsudo\fR's
debug system.
.PP
\fIEnabling debugging in sudo.conf\fR
.PP
To enable debug messages, add a
\fIDebug\fR
line to
sudo.conf(@mansectform@)
with the program set to
\fI@python_plugin@\fR.
For example, to store debug output in
\fI@log_dir@/sudo_python_debug\fR,
use a line like the following:
.nf
.sp
.RS 4n
Debug @python_plugin@ @log_dir@/sudo_python_debug \e
    plugin@trace,c_calls@trace
.RE
.fi
.PP
The debug options are in the form of multiple
\(lqsubsystem@level\(rq
strings, separated by commas
(\(oq\&,\(cq).
For example to just see the debug output of
\fBsudo.debug\fR()
calls, use:
.nf
.sp
.RS 4n
Debug @python_plugin@ @log_dir@/sudo_python_debug plugin@trace
.RE
.fi
.PP
See
sudo_conf(@mansectform@)
for more details.
.PP
The most interesting subsystems for Python plugin development are:
.TP 6n
\fIplugin\fR
Logs each
\fBsudo.debug\fR()
API call.
.TP 6n
\fIpy_calls\fR
Logs whenever a C function calls into the python module.
For example, calling the
\fB__init__\fR()
function.
.TP 6n
\fIc_calls\fR
Logs whenever python calls into a C
\fBsudo\fR
API function.
.TP 6n
\fIinternal\fR
Logs internal functions of the python language wrapper plugin.
.TP 6n
\fIsudo_cb\fR
Logs when
\fBsudo\fR
calls into the python plugin API.
.TP 6n
\fIload\fR
Logs python plugin loading / unloading events.
.PP
You can also specify
\(lqall\(rq
as the subsystem name to log debug messages for all subsystems.
.PP
The
\fBsudo.debug\fR()
function is defined as:
.nf
.sp
.RS 4n
sudo.debug(level, message(s))
.RE
.fi
.PP
The function arguments are as follows:
.TP 6n
\fIlevel\fR
.br
an integer, use one of the log level constants below
.TP 6n
\fImessage(s)\fR
one or more messages to log
.PP
\fIAvailable log levels:\fR
.TS
l l l.
.PP
\fBsudo.conf name\fR	\fBPython constant\fR	\fBdescription\fR
.PP
crit	\fRsudo.DEBUG.CRIT\fR	only critical messages
.PP
err	\fRsudo.DEBUG.ERROR\fR	
.PP
warn	\fRsudo.DEBUG.WARN\fR	
.PP
notice	\fRsudo.DEBUG.NOTICE\fR	
.PP
diag	\fRsudo.DEBUG.DIAG\fR	
.PP
info	\fRsudo.DEBUG.INFO\fR	
.PP
trace	\fRsudo.DEBUG.TRACE\fR	
.PP
debug	\fRsudo.DEBUG.DEBUG\fR	very extreme verbose debugging
.TE
.PP
\fIUsing the logging module\fR
.PP
Alternatively, a plugin can use the built in logging module of Python as well.
Sudo adds its log handler to the root logger, so by default all output of a
logger will get forwarded to sudo log system, as it would call sudo.debug.
.PP
The log handler of sudo will map each Python log level of a message to
the appropriate sudo debug level.
The sudo debug system will only receive messages that are not filtered
out by the Python loggers.
For example, the log level of the python logger will be an additional
filter for the log messages, and is usually very different from
what level is set in sudo.conf for the sudo debug system.
.SS "Debug example"
Sudo ships with an example debug plugin.
To try it, register it by adding the following lines to
\fI@sysconfdir@/sudo.conf\fR:
.nf
.sp
.RS 4n
Plugin python_io @python_plugin@ \e
    ModulePath=@EXAMPLES@/example_debugging.py \e
    ClassName=DebugDemoPlugin

Debug @python_plugin@ \e
    @log_dir@/sudo_python_debug plugin@trace,c_calls@trace
.RE
.fi
.SS "Option conversion API"
The Python plugin API includes two convenience functions to
convert options in
\(lqkey=value\(rq
format to a dictionary and vice versa.
.TP 6n
options_as_dict
.nf
.RS 6n
options_as_dict(options)
.RE
.fi
.RS 6n
.sp
The function arguments are as follows:
.TP 6n
\fIoptions\fR
An iterable (tuple, list, etc.) of strings, each in
\(lqkey=value\(rq
format.
This is how the plugin API passes options and settings to a Python plugin.
.PP
The function returns the resulting dictionary.
Each string of the passed in
\fIoptions\fR
will be split at the first equal sign
(\(oq\&=\(cq)
into a
\fIkey\fR
and
\fIvalue\fR.
Dictionary keys will never contain this symbol (but values may).
.RE
.TP 6n
options_from_dict
.nf
.RS 6n
options_from_dict(options_dict)
.RE
.fi
.RS 6n
.sp
The function arguments are as follows:
.TP 6n
\fIoptions_dict\fR
A dictionary where both the key and the value are strings.
The key should not contain an equal sign
(\(oq\&=\(cq),
otherwise the resulting string will have a different meaning.
However, this is not currently enforced.
.PP
The function returns a tuple containing the strings in
\(lqkey=value\(rq
form for each key and value in the
\fIoptions_dict\fR
dictionary passed in.
This is how the plugin API accepts options and settings.
.RE
.SH "PLUGIN API CHANGELOG (Python)"
None yet
.SH "LIMITATIONS"
A maximum of 8 python I/O plugins can be loaded at once.
If
\fI@sysconfdir@/sudo.conf\fR
contains more, those will be rejected with a warning message.
.PP
The Event API and the hook function API is currently not accessible
for Python plugins.
.SH "SEE ALSO"
sudo.conf(@mansectform@),
sudo_plugin(@mansectform@),
sudoers(@mansectform@),
sudo(@mansectsu@)
.SH "AUTHORS"
Many people have worked on
\fBsudo\fR
over the years; this version consists of code written primarily by:
.sp
.RS 6n
Todd C. Miller
.RE
.PP
See the CONTRIBUTORS.md file in the
\fBsudo\fR
distribution (https://www.sudo.ws/about/contributors/) for an
exhaustive list of people who have contributed to
\fBsudo\fR.
.SH "BUGS"
Python plugin support is currently considered experimental.
.PP
If you believe you have found a bug in
\fBsudo\fR,
you can submit a bug report at https://bugzilla.sudo.ws/
.SH "SECURITY CONSIDERATIONS"
All Python plugin handling is implemented inside the
\fI@python_plugin@\fR
dynamic plugin.
Therefore, if no Python plugin is registered in
sudo.conf(@mansectform@)
or the
\fIsudoers\fR
file,
\fBsudo\fR
will not load the Python interpreter or the Python libraries.
.PP
As
\fBsudo\fR
runs plugins as
\fBroot\fR,
care must be taken when writing Python plugins to avoid creating
security vulnerabilities, just as one would when writing plugins
in C.
.SH "SUPPORT"
Limited free support is available via the sudo-users mailing list,
see https://www.sudo.ws/mailman/listinfo/sudo-users to subscribe or
search the archives.
.SH "DISCLAIMER"
\fBsudo\fR
is provided
\(lqAS IS\(rq
and any express or implied warranties, including, but not limited
to, the implied warranties of merchantability and fitness for a
particular purpose are disclaimed.
See the LICENSE.md file distributed with
\fBsudo\fR
or https://www.sudo.ws/about/license/ for complete details.